Skip to main content

Crate hamming_bitwise_fast

Crate hamming_bitwise_fast 

Source
Expand description

Fast bitwise Hamming distance using auto-vectorization with runtime SIMD detection on x86.

§Quick Start

use hamming_bitwise_fast::array;

let a: [u8; 128] = [0xFF; 128];  // 1024-bit vectors
let b: [u8; 128] = [0x00; 128];

// Single comparison
let distance = array::distance(&a, &b);  // 1024

// One source vs many targets
let targets = vec![a, b];
let mut distances = vec![0u32; 2];
array::batch(&a, &targets, &mut distances);

§Choosing an API

§Fixed-size arrays vs slices

If the vector size is known at compile time (e.g., 1024-bit embeddings are [u8; 128]), use the array module for the best performance.

Use slice when sizes vary at runtime or are not known until program execution.

§Single vs batch

Use array::batch or slice::batch when comparing one source against many targets. Batch is the fastest approach for one-to-many comparisons.

§Platform Behavior

PlatformConfigurationBehavior
x86/x86_64DefaultRuntime CPU detection via multiversion (AVX-512/AVX2/SSE4.2)
x86/x86_64default-features = falseBaseline SSE2 only (slow)
ARMDefaultNEON is baseline; already optimized

On x86, the default build automatically detects and uses the best available SIMD instructions at runtime:

cargo add hamming-bitwise-fast

For best single-call performance on x86, enable LTO so the compiler can auto-vectorize across the crate boundary:

[profile.release]
lto = true

For maximum performance, also compile with -C target-cpu=native (eliminates runtime dispatch overhead, at the cost of portability).

On ARM (including Apple Silicon), the default build is already fast.

§Feature Flags

  • multiversion_x86 (enabled by default): Enables runtime CPU detection for optimal SIMD on x86 via the multiversion crate. Disable with default-features = false if you need zero dependencies or are targeting a known CPU with -C target-cpu=native.

Modules§

array
Fixed-size array APIs for bitwise Hamming distance.
slice
Variable-length slice APIs for bitwise Hamming distance.

Functions§

hamming_bitwise_fast
Convenience alias for slice::distance that matches the crate name.