You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Bitstream reading is the foundation of all entropy decoding (Huffman + FSE). The C reference uses carefully tuned branchless operations that the Rust bit reader partially lacks.
C reference optimizations (bitstream.h, bits.h)
1. Bit container sizing
Uses size_t as bit container (64-bit on x86-64)
STREAM_ACCUMULATOR_MIN_64 = 57 bits — reload threshold tuned per architecture
Forward write / backward read (LIFO stack) for natural FSE decode order
2. BMI2 fast path
BIT_getLowerBits() uses _bzhi_u64() (BMI2 instruction) for O(1) bit masking
Summary
Bitstream reading is the foundation of all entropy decoding (Huffman + FSE). The C reference uses carefully tuned branchless operations that the Rust bit reader partially lacks.
C reference optimizations (bitstream.h, bits.h)
1. Bit container sizing
size_tas bit container (64-bit on x86-64)STREAM_ACCUMULATOR_MIN_64 = 57bits — reload threshold tuned per architecture2. BMI2 fast path
BIT_getLowerBits()uses_bzhi_u64()(BMI2 instruction) for O(1) bit maskingBIT_mask[]pre-computed lookup table (32 entries)3. CTZ/CLZ intrinsics
ZSTD_countTrailingZeros32/64()→__builtin_ctz()(Rust:trailing_zeros())ZSTD_highBit32()→__builtin_clz()with DeBruijn fallback4. Branchless reload
Current Rust implementation
bit_io/bit_reader.rsandbit_reader_reverse.rs— functional but with branchesringbuffer.rs:200, 240— branch-heavy logic in hot path (noted as TODO)trailing_zeros()/leading_zeros()(good, but no bulk masking)What needs to be implemented
BIT_mask[33]equivalent for fast masking#[cfg(target_feature = "bmi2")]for_bzhi_u64Performance impact estimate
Acceptance criteria
Time estimate
2d