MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / pack_bits

Function pack_bits

nodedb-codec/src/fastlanes/bits.rs:23–54  ·  view source on GitHub ↗
(packed: &mut [u8], bit_offset: usize, value: u64, bit_width: u8)

Source from the content-addressed store, hash-verified

21/// Written as a tight loop over bytes for auto-vectorization.
22#[inline]
23pub(super) fn pack_bits(packed: &mut [u8], bit_offset: usize, value: u64, bit_width: u8) {
24 let bw = bit_width as usize;
25 if bw == 0 {
26 return;
27 }
28
29 let byte_idx = bit_offset / 8;
30 let bit_idx = bit_offset % 8;
31
32 // How many bits fit in the first byte.
33 let first_bits = (8 - bit_idx).min(bw);
34
35 // Write first partial byte.
36 packed[byte_idx] |= ((value & low_mask_u64(first_bits)) as u8) << bit_idx;
37
38 let mut remaining = bw - first_bits;
39 let mut val = value >> first_bits;
40 let mut bi = byte_idx + 1;
41
42 // Write full bytes.
43 while remaining >= 8 {
44 packed[bi] = (val & 0xFF) as u8;
45 val >>= 8;
46 remaining -= 8;
47 bi += 1;
48 }
49
50 // Write last partial byte.
51 if remaining > 0 {
52 packed[bi] |= (val & low_mask_u64(remaining)) as u8;
53 }
54}
55
56/// Unpack a value from a byte array at the given bit offset.
57#[inline]

Callers 3

pack_unpack_roundtripFunction · 0.85
pack_unpack_at_offsetsFunction · 0.85
encode_blockFunction · 0.85

Calls 1

low_mask_u64Function · 0.85

Tested by 2

pack_unpack_roundtripFunction · 0.68
pack_unpack_at_offsetsFunction · 0.68