FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
negabinary.h
Go to the documentation of this file.
1#pragma once
2
38#include <cstdint>
39#include <type_traits>
40
41// nvcc defines __CUDACC__; hipcc/amdclang++ defines __HIPCC__ instead. Without
42// the second arm these helpers stay host-only under HIP and every __global__
43// caller fails to resolve them.
44#if defined(__CUDACC__) || defined(__HIPCC__)
45# define FZ_HOST_DEVICE __host__ __device__
46#else
47# define FZ_HOST_DEVICE
48#endif
49
50namespace fz {
51
65template <typename T>
66struct Negabinary {
67 static_assert(std::is_integral<T>::value && std::is_signed<T>::value,
68 "fz::Negabinary<T>: T must be a signed integer type "
69 "(int8_t, int16_t, int32_t, or int64_t).");
70
71 using SInt = T;
72 using UInt = typename std::make_unsigned<T>::type;
73
74 // Alternating-bit mask 0xAA…A for the UInt width.
75 // Truncating the 64-bit constant to UInt gives:
76 // uint8_t → 0xAA
77 // uint16_t → 0xAAAA
78 // uint32_t → 0xAAAAAAAA
79 // uint64_t → 0xAAAAAAAAAAAAAAAA
80 static constexpr UInt MASK =
81 static_cast<UInt>(static_cast<uint64_t>(0xAAAAAAAAAAAAAAAAULL));
82
88 FZ_HOST_DEVICE static constexpr UInt encode(SInt n) noexcept {
89 return (static_cast<UInt>(n) + MASK) ^ MASK;
90 }
91
100 FZ_HOST_DEVICE static constexpr SInt decode(UInt u) noexcept {
101 return static_cast<SInt>((u ^ MASK) - MASK);
102 }
103};
104
105// ---------------------------------------------------------------------------
106// Convenience aliases for the four standard widths
107// ---------------------------------------------------------------------------
108using Negabinary8 = Negabinary<int8_t>;
109using Negabinary16 = Negabinary<int16_t>;
110using Negabinary32 = Negabinary<int32_t>;
111using Negabinary64 = Negabinary<int64_t>;
112
113} // namespace fz
114
115#undef FZ_HOST_DEVICE
Definition algorithms.h:48
Definition negabinary.h:66
static FZ_HOST_DEVICE constexpr SInt decode(UInt u) noexcept
Definition negabinary.h:100
static FZ_HOST_DEVICE constexpr UInt encode(SInt n) noexcept
Definition negabinary.h:88