Skip to main content

compress

Function compress 

Source
pub fn compress(value: u64, mask: u64) -> u64
Expand description

Parallel bit extract: for each set bit in mask, extract the corresponding bit from value and pack them contiguously into the low bits of the return value.

Equivalent to the x86 BMI2 PEXT instruction. When compiled with the bmi2 target feature enabled (for example -C target-cpu=x86-64-v3) this lowers to the hardware pext instruction; otherwise it falls back to a portable scalar loop.

§Functional Example

Using 8 bits for brevity (the function operates on all 64). Each set bit in mask selects the bit at the same position in value; the selected bits are then shifted down so they are contiguous in the low bits of the result, in their original order:

bit:     7 6 5 4 3 2 1 0
value:   a b c d e f g h
mask:    0 1 1 0 1 1 0 1      set bits select b, c, e, f and h
           | |   | |   |
           v v   v v   v      copy the relevant bits into result
result:  0 0 0 b c e f h

§Code Example

assert_eq!(compress(0b1011_0100, 0b0110_1101), 0b0000_1010);