r/cpp • u/mr_gnusi • Mar 05 '26
Extending Daniel Lemire's bit packing to uint64_t with C++ templates
Bit packing is a classic approach for compressing arrays of small integers. If your values only ever reach, say, 17, you only need 5 bits each and you can pack 6 of them into a single 32-bit word instead of storing one per word. This means less disk space and higher throughput for storage engines and search indexes.
Daniel Lemire's simdcomp is a great implementation of bitpacking for uint32_t. It provides a family of pack/unpack routines (one per bit width 1–32), originally generated by a script (interestingly there is no script for the SIMD version). The key benefit comes from unrolling everything statically without branches or loops and using hand-written AVX/SIMD intrinsics.
Our implementation extends this to uint64_t using C++ templates instead of a code-generation script and without hand-written intrinsics. We rely on the compiler to vectorize the code.
Another difference is block size. Lemire's SIMD version operates on 128 integers at a time (256 with AVX), which is great for throughput but requires buffering a large block before packing. Our version works on 32 values at a time for uint32_t and 64 for uint64_t. This finer granularity can be beneficial when you have smaller or irregular batch sizes — for example, packing the offsets of a single small posting list in a search index without needing to pad to 128 elements.
template<int N>
void Fastpack(const uint64_t* IRS_RESTRICT in,
uint64_t* IRS_RESTRICT out) noexcept {
static_assert(0 < N && N < 64);
// all offsets are constexpr — no branches, no loops
*out |= ((*in) % (1ULL << N)) << (N * 0) % 64;
if constexpr (((N * 1) % 64) < ((N * 0) % 64)) {
++out;
*out |= ((*in) % (1ULL << N)) >> (N - ((N * 1) % 64));
}
++in;
// ... repeated for all 64 values
}
if constexpr ensures that word-boundary crossings (the only real complexity) compile away entirely for a given bit width N. The result is a fully unrolled function without branches for each instantiation.
Check it out in Compiler Explorer to see what the compiler actually generates (clang 21, -O3, -mavx2). It's a dense set of XMM vectorized chunks (vpsllvd, vpand, vpor, vpblendd, vpunpckldq) interleaved with scalar shl/or/and sequences around word boundaries, all fully unrolled with every shift amount and mask baked into rodata as compile-time constants. It's not pretty to read, but it's branch-free and the CPU can execute it quite efficiently.
Of course the 64-bit variant is slower than its 32-bit counterpart. With 64-bit words you pack half as many values per word, the auto-vectorized paths are less efficient (fewer lanes in SIMD registers). If your values fit in 32 bits, don't use it.
That said, there are cases where bit packing over 64-bit words is a clear win over storing raw uint64_t arrays:
- File offsets are
uint64_t. Delta-encoding offsets within a segment often brings them down to just a few bits each. - Timestamps in microseconds or nanoseconds are 64-bit and time-series data is often nearly monotone after delta coding.
- Document/row IDs in large-scale systems don't fit 32-bit identifiers.
The implementation lives in bit_packing.hpp + bit_packing.cpp. It's part of SereneDB's storage layer but has no hard dependencies and should be straightforward to lift into other projects. The file is ~2300 lines of hand-written template expansions, created when you had to suffer through that yourself, before LLMs existed.
Happy to discuss tradeoffs vs. SIMD-explicit approaches (like those in streamvbyte or libFastPFOR). Would also be curious whether anyone has found this pattern useful for 64-bit workloads beyond the ones listed above.
Unfortunately there are no benchmarks in this post, but if there's interest I can put some together.
8
u/DerAlbi Mar 05 '26
GCC seems to break down with the SIMD.
Also, why is this better than, for example
Does the constexpr stuff really impact vectorization in a loop that can be unrolled anyway?
It is possible that I have misunderstood what the code does. Sorry if the question is stupid.
2
u/Successful_Yam_9023 Mar 21 '26
Can I assume that the AVX-512 bit-matrix transpose idea was not of interest?
5
u/KrishMandal Mar 05 '26
really , this is pretty neat. using if constexpr so the boundary cases disappear at compile time is a nice trick. unrolled packing like that usually ends up way cleaner in the generated asm. when i play with stuff like this i usually check godbolt and dump notes/benchmarks in a doc. recently tried runable and gamma also for turning quick experiment notes into small reports, along with godbolt + perf. kinda helps keep optimization experiments organized.
1
u/Successful_Yam_9023 Mar 06 '26 edited Mar 06 '26
With AVX512, another approach could be to bit-transpose 8 uint64_t into 64 uint8_t (two instructions: vpermb, vgf2p8affineqb) then do partially overlapping unaligned stores to keep only the first k bytes - it doesn't pack to exactly the same format but it still packs
E: unpacking would be similar - load (unaligned, masked), vpshufb, vgf2p8affineqb, vpermb. I can work them out into real code if you want.
0
u/[deleted] Mar 05 '26
[deleted] — view deleted comment