Skip to content
Systems programming for what’s next.
— Vex

Bit Namespace ​

Bit.pext and Bit.pdep extract and deposit bits using a mask. They are builtin operations; the current source declares scalar (u64, u64) -> u64 signatures. Do not assume a scalar call accepts arrays, spans, or tensors, or that it emits a single SIMD instruction.

Quick Example ​

Save this complete program as bit-example.vx:

vex
fn main(): i32 {
    let source: u64 = 0b10100110;
    let sparse: u64 = 0b10100110;
    let mixed: u64 = 0b10000010;
    let full: u64 = 0xffffffffffffffff;
    $println(Bit.pext(source, 0 as u64));
    $println(Bit.pext(source, full));
    $println(Bit.pext(source, sparse));
    $println(Bit.pext(mixed, sparse));
    let deposited = Bit.pdep(0b1011 as u64, sparse);
    $println(deposited);
    $println(Bit.pext(deposited, sparse));
    $println(Bit.pdep(Bit.pext(mixed, sparse), sparse));
    return 0;
}

A local macOS arm64 development build printed 0, 166, 15, 9, 134, 11, and 130, each on its own line. These cover an empty mask, a full 64-bit mask, source equal to a sparse mask, a sparse selection containing zeros, and both directions of a masked round trip.

Parallel Bit Deposit ​

How PDEP Works ​

Deposit the least-significant source bit into the least-significant set mask position, then advance through set mask positions from least to most significant. With source 0b1011 and mask 0b1010_0110, source bits 1, 1, 0, 1 go to mask positions 1, 2, 5, 7; the result is 0b1000_0110 (134).

Parallel Bit Extract ​

How PEXT Works ​

Extract mask-selected source bits in ascending position order, packing the first selected bit into result bit 0. With source = mask = 0b1010_0110, positions 1, 2, 5, 7 are all set, producing 0b1111 (15). With source 0b1000_0010 and the same mask, selected bits are 1, 0, 0, 1, producing 0b1001 (9).

For a mask with n set bits, pext(pdep(x, mask), mask) keeps the low n bits of x; pdep(pext(x, mask), mask) equals x & mask. Bits outside the mask cannot be recovered.

Hardware Support ​

In the current compiler source, x86-64 targets with +bmi2 select LLVM PEXT/PDEP intrinsics; other targets use software lowering. CPU support, compiler target settings, optimization, and generated machine code determine the actual path. No throughput or latency is promised here; benchmark the specific binary on your target.

Released under the MIT License.