FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
negabinary.h File Reference

Negabinary (base -2) integer encoding helpers. More...

#include <cstdint>
#include <type_traits>

Go to the source code of this file.

Classes

struct  fz::Negabinary< T >
 

Detailed Description

Negabinary (base -2) integer encoding helpers.

Negabinary (base -2) encoding.

Maps a signed two's-complement integer to an unsigned negabinary representation using the compact XOR-mask formula:

encode: u = (n + MASK) ^ MASK // signed → unsigned decode: n = (SInt)((u ^ MASK) - MASK) // unsigned → signed (NOT self-inverse)

where MASK = 0xAA…A (alternating bits: 1010…1010) for the given width.

Properties:

  • After differencing smooth data the high-order bits of the negabinary representation vanish faster than zigzag encoding, producing denser zero runs at high bit-planes when followed by bitshuffle + RZE.
  • Size-preserving: input and output have the same byte width.
  • Lossless: decode(encode(n)) == n for all n.

Design constraints (mirrors zigzag.h):

  • All functions are host device — callable from both CUDA device kernels and host-side C++ code.
  • Template parameter T must be a signed integer type (static_assert).
  • The matching unsigned type UInt is derived via make_unsigned_t.
  • MASK is a compile-time constant derived by truncating 0xAAAAAAAAAAAAAAAA to the required width (gives 0xAA for 8-bit, 0xAAAA for 16-bit, etc.)

Usage: using NB = fz::Negabinary<int32_t>; uint32_t u = NB::encode(-3); // → negabinary representation int32_t n = NB::decode(u); // → -3