cpudetect 0.2.0

Ergonomic helpers for CPU feature detection
Documentation

cpudetect

cpudetect is a Rust crate that provides ergonomic helpers for CPU feature detection. It exposes:

  • per-feature checks such as has_avx2() and has_neon()
  • per-family compatibility checks such as is_x86_64_v3_compatible()
  • a #[target_family(...)] procedural macro for expanding a CPU family into Rust #[target_feature] requirements

The crate currently targets x86_64 and aarch64.

Installation

Add the crate to your Cargo.toml:

[dependencies]
cpudetect = "0.1.0"

Cargo features

  • enabled (default): enables CPU detection
  • static: disables runtime detection and relies on compile-time target_feature / target-cpu settings

If you disable the default enabled feature, all detection helpers return false.

Usage

Check an x86_64 CPU feature

#[cfg(target_arch = "x86_64")]
fn main() {
    if cpudetect::has_avx2() {
        println!("AVX2 is available");
    }
}

#[cfg(not(target_arch = "x86_64"))]
fn main() {}

Check a baseline CPU family

#[cfg(target_arch = "x86_64")]
fn main() {
    if cpudetect::is_x86_64_v3_compatible() {
        println!("This machine satisfies the x86-64-v3 baseline");
    }
}

#[cfg(not(target_arch = "x86_64"))]
fn main() {}

Check an AArch64 feature set

#[cfg(target_arch = "aarch64")]
fn main() {
    if cpudetect::is_arm_v8_6a_compatible() {
        println!("This machine satisfies the Arm v8.6-A feature set");
    }
}

#[cfg(not(target_arch = "aarch64"))]
fn main() {}

Use target_family to enable code for a CPU family

#[cfg(target_arch = "x86_64")]
use cpudetect::target_family;

#[cfg(target_arch = "x86_64")]
#[target_family("x86_64_v3")]
unsafe fn x86_64_v3_only() -> u8 {
    3
}

Supported API shape

  • On x86_64, the crate exports feature helpers from src/x86_64/features.rs and family helpers from src/x86_64/families.rs.
  • On aarch64, the crate exports feature helpers from src/aarch64/features.rs and family helpers from src/aarch64/families.rs.
  • Family helpers cache runtime detection results on non-static builds.

Development

The repository includes a just precommit task that runs the configured Clippy checks.

just precommit

License

MIT