#include <string.h>
static int
_t1(rev_fwd_reversible, Scalar)(const Int* iblock, const Scalar* fblock, uint n, int emax)
{
cache_align_(Scalar gblock[BLOCK_SIZE]);
_t1(rev_inv_cast, Scalar)(iblock, gblock, n, emax);
return !memcmp(fblock, gblock, n * sizeof(*fblock));
}
static void
_t1(rev_fwd_cast, Scalar)(Int* iblock, const Scalar* fblock, uint n, int emax)
{
if (emax != -EBIAS)
_t1(fwd_cast, Scalar)(iblock, fblock, BLOCK_SIZE, emax);
else
while (n--)
*iblock++ = 0;
}
static void
_t1(rev_fwd_reinterpret, Scalar)(Int* iblock, const Scalar* fblock, uint n)
{
memcpy(iblock, fblock, n * sizeof(*iblock));
while (n--) {
Int x = *iblock;
if (x < 0)
*iblock = (Int)((UInt)x ^ TCMASK);
iblock++;
}
}
static uint
_t2(rev_encode_block, Scalar, DIMS)(zfp_stream* zfp, const Scalar* fblock)
{
uint bits = 0;
cache_align_(Int iblock[BLOCK_SIZE]);
int emax = _t1(exponent_block, Scalar)(fblock, BLOCK_SIZE);
_t1(rev_fwd_cast, Scalar)(iblock, fblock, BLOCK_SIZE, emax);
if (_t1(rev_fwd_reversible, Scalar)(iblock, fblock, BLOCK_SIZE, emax)) {
uint e = emax + EBIAS;
if (e) {
bits += 2;
stream_write_bits(zfp->stream, 1, 2);
bits += EBITS;
stream_write_bits(zfp->stream, e, EBITS);
}
else {
bits++;
stream_write_bit(zfp->stream, 0);
return bits;
}
}
else {
_t1(rev_fwd_reinterpret, Scalar)(iblock, fblock, BLOCK_SIZE);
bits += 2;
stream_write_bits(zfp->stream, 3, 2);
}
bits += _t2(rev_encode_block, Int, DIMS)(zfp->stream, zfp->minbits - bits, zfp->maxbits - bits, zfp->maxprec, iblock);
return bits;
}