use std::sync::OnceLock;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Isa {
Scalar,
Sse2,
Avx2,
Avx512,
Neon,
Sve,
}
impl Isa {
pub const fn as_str(self) -> &'static str {
match self {
Isa::Scalar => "scalar",
Isa::Sse2 => "sse2",
Isa::Avx2 => "avx2",
Isa::Avx512 => "avx512",
Isa::Neon => "neon",
Isa::Sve => "sve",
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Features {
pub sse2: bool,
pub ssse3: bool,
pub fsrm: bool,
pub intel: bool,
pub amd: bool,
pub xeon: bool,
pub epyc: bool,
pub avx2: bool,
pub avx512f: bool,
pub neon: bool,
pub sve: bool,
pub neoverse_v2: bool,
}
impl Features {
pub fn best(self) -> Isa {
if self.sve {
Isa::Sve
} else if self.neon {
Isa::Neon
} else if self.avx2 {
Isa::Avx2
} else if self.sse2 {
Isa::Sse2
} else {
Isa::Scalar
}
}
}
pub fn features() -> Features {
static CACHE: OnceLock<Features> = OnceLock::new();
*CACHE.get_or_init(detect)
}
pub fn best_isa() -> Isa {
features().best()
}
#[cfg(target_arch = "x86_64")]
fn brand_says(brand: &str, line: &str) -> bool {
debug_assert_eq!(line, line.to_ascii_uppercase());
brand
.as_bytes()
.windows(line.len())
.any(|w| w.eq_ignore_ascii_case(line.as_bytes()))
}
fn detect() -> Features {
let mut f = Features::default();
#[cfg(target_arch = "x86_64")]
{
f.sse2 = true;
f.ssse3 = std::arch::is_x86_feature_detected!("ssse3");
f.fsrm = unsafe { std::arch::x86_64::__cpuid_count(7, 0).edx & (1 << 4) != 0 };
let v = unsafe { std::arch::x86_64::__cpuid(0) };
let vendor = [v.ebx, v.edx, v.ecx];
f.intel = vendor == [0x756e_6547, 0x4965_6e69, 0x6c65_746e];
f.amd = vendor == [0x6874_7541, 0x6974_6e65, 0x444d_4163];
let mut brand = [0u8; 48];
let max_ext = unsafe { std::arch::x86_64::__cpuid(0x8000_0000).eax };
if max_ext >= 0x8000_0004 {
for (i, leaf) in [0x8000_0002u32, 0x8000_0003, 0x8000_0004]
.iter()
.enumerate()
{
let c = unsafe { std::arch::x86_64::__cpuid(*leaf) };
for (j, r) in [c.eax, c.ebx, c.ecx, c.edx].iter().enumerate() {
brand[i * 16 + j * 4..i * 16 + j * 4 + 4].copy_from_slice(&r.to_le_bytes());
}
}
}
let brand = String::from_utf8_lossy(&brand);
f.xeon = f.intel && brand_says(&brand, "XEON");
f.epyc = f.amd && brand_says(&brand, "EPYC");
f.avx2 = std::arch::is_x86_feature_detected!("avx2");
f.avx512f = std::arch::is_x86_feature_detected!("avx512f");
}
#[cfg(target_arch = "aarch64")]
{
f.neon = true;
f.sve = false;
f.neoverse_v2 = midr_says_neoverse_v2();
}
f
}
#[cfg(target_arch = "aarch64")]
fn midr_says_neoverse_v2() -> bool {
const PATH: &str = "/sys/devices/system/cpu/cpu0/regs/identification/midr_el1";
let Ok(text) = std::fs::read_to_string(PATH) else {
return false;
};
let Ok(midr) = u64::from_str_radix(text.trim().trim_start_matches("0x"), 16) else {
return false;
};
midr_is_neoverse_v2(midr)
}
#[cfg(target_arch = "aarch64")]
fn midr_is_neoverse_v2(midr: u64) -> bool {
let implementer = (midr >> 24) & 0xff;
let part = (midr >> 4) & 0xfff;
implementer == 0x41 && part == 0xd4f
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detection_is_consistent_with_target() {
let f = features();
if cfg!(target_arch = "x86_64") {
assert!(f.sse2, "SSE2 is part of the x86-64 baseline");
}
if cfg!(target_arch = "aarch64") {
assert!(f.neon, "NEON is mandatory in AArch64");
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn a_part_line_is_recognised_whatever_its_case() {
for brand in [
"Intel(R) Xeon(R) Platinum 8481C CPU @ 2.70GHz",
"INTEL(R) XEON(R) PLATINUM 8581C CPU @ 2.30GHz",
] {
assert!(super::brand_says(brand, "XEON"), "missed {brand}");
assert!(!super::brand_says(brand, "EPYC"), "false EPYC on {brand}");
}
assert!(super::brand_says("AMD EPYC 9B14", "EPYC"));
assert!(!super::brand_says("AMD EPYC 9B14", "XEON"));
for other in [
"12th Gen Intel(R) Core(TM) i7-1260P",
"VirtualApple @ 2.50GHz",
] {
assert!(!super::brand_says(other, "XEON"));
assert!(!super::brand_says(other, "EPYC"));
}
}
#[test]
fn detection_is_cached() {
let a = features();
let b = features();
assert_eq!(a.best(), b.best());
}
#[test]
fn avx512_is_never_selected_implicitly() {
let f = Features {
sse2: true,
ssse3: true,
fsrm: false,
intel: false,
amd: false,
xeon: false,
epyc: false,
avx2: true,
avx512f: true,
..Features::default()
};
assert_eq!(f.best(), Isa::Avx2, "AVX-512 must stay opt-in");
}
}