Skip to main content

Crate simd_popcnt

Crate simd_popcnt 

Source
Expand description

§simd-popcnt

Count the number of 1 bits (bit population count, a.k.a. Hamming weight) in a byte slice as quickly as possible using specialized CPU instructions: POPCNT, AVX2 and AVX512 on x86/x86-64, and NEON and SVE on AArch64.

This is a Rust port of the C/C++ libpopcnt.h header-only library by Kim Walisch.

§Usage

let bytes = [0xFFu8; 16];
assert_eq!(simd_popcnt::popcnt(&bytes), 128);

§Performance

For the fastest possible code, compile with RUSTFLAGS="-C target-cpu=native". This lets the crate select the best SIMD path at compile time with zero runtime dispatch overhead. Otherwise the best available instruction set is detected once at runtime and cached.

Traits§

PopcntExt
Adds a popcnt method to slices of the built-in integer types, counting their bits without a manual byte cast. Implemented for slices, arrays and Vecs of u8/u16/u32/u64/u128/usize and their signed counterparts; bring it into scope with use simd_popcnt::PopcntExt;.

Functions§

popcnt
Counts the number of one bits (population count) in bytes.