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

Zigzag (two's complement to magnitude-sign) encoding helpers. More...

#include <cstdint>
#include <type_traits>

Go to the source code of this file.

Classes

struct  fz::Zigzag< T >
 

Detailed Description

Zigzag (two's complement to magnitude-sign) encoding helpers.

Zigzag (TCMS - Two's Complement to Magnitude-Sign) encoding.

Maps signed integers to unsigned integers so that small-magnitude values (both positive and negative) map to small non-negative integers:

encode: u = (x << 1) ^ (x >> (W-1)) // signed → unsigned decode: x = (u >> 1) ^ -(u & 1) // unsigned → signed

Design constraints:

  • All functions are host device so they are callable from both CUDA device kernels and host-side C++ code.
  • The template is parameterised on the signed type T; the matching unsigned type UInt is derived automatically via make_unsigned_t.
  • T must be a signed integer type (enforced by static_assert).
  • encode()/decode() use the exact-bitwidth companion type to avoid undefined behaviour from signed right shift on non-matching types.

Usage: using ZZ = fz::Zigzag<int32_t>; uint32_t u = ZZ::encode(-3); // → 5 int32_t x = ZZ::decode(5u); // → -3