use std::fmt;
use std::str::FromStr;
use std::sync::OnceLock;
pub const LEVEL_VARIABLE: &str = "PLTRS_CPU_LEVEL";
static LEVEL: OnceLock<CpuLevel> = OnceLock::new();
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CpuLevel {
Baseline,
V3,
V4,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct NumpyDispatch {
pub x86_v3: Option<bool>,
pub x86_v4: Option<bool>,
pub avx2: bool,
pub avx512_skx: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnknownLevel(String);
impl fmt::Display for UnknownLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"unknown processor level {:?}: expected v2, v3 or v4",
self.0
)
}
}
impl std::error::Error for UnknownLevel {}
impl CpuLevel {
pub const ALL: [Self; 3] = [Self::Baseline, Self::V3, Self::V4];
pub fn current() -> Self {
*LEVEL.get_or_init(|| {
std::env::var(LEVEL_VARIABLE)
.ok()
.and_then(|name| name.parse().ok())
.unwrap_or_else(Self::detect)
})
}
pub fn set(level: Self) -> Self {
*LEVEL.get_or_init(|| level)
}
pub fn detect() -> Self {
#[cfg(target_arch = "x86_64")]
{
use std::arch::is_x86_feature_detected as has;
let v3 = has!("avx")
&& has!("avx2")
&& has!("f16c")
&& has!("fma")
&& has!("bmi1")
&& has!("bmi2")
&& has!("lzcnt")
&& has!("movbe");
let v4 = v3
&& has!("avx512f")
&& has!("avx512cd")
&& has!("avx512bw")
&& has!("avx512dq")
&& has!("avx512vl");
if v4 && !cfg!(windows) {
Self::V4
} else if v3 {
Self::V3
} else {
Self::Baseline
}
}
#[cfg(not(target_arch = "x86_64"))]
{
Self::Baseline
}
}
#[must_use]
pub fn from_numpy(numpy: NumpyDispatch) -> Self {
let (v3, v4) = match (numpy.x86_v3, numpy.x86_v4) {
(Some(v3), Some(v4)) => (v3, v3 && v4),
_ => (numpy.avx2 || numpy.avx512_skx, numpy.avx512_skx),
};
if v4 {
Self::V4
} else if v3 {
Self::V3
} else {
Self::Baseline
}
}
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Baseline => "v2",
Self::V3 => "v3",
Self::V4 => "v4",
}
}
}
impl fmt::Display for CpuLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
impl FromStr for CpuLevel {
type Err = UnknownLevel;
fn from_str(name: &str) -> Result<Self, UnknownLevel> {
let lower = name.trim().to_ascii_lowercase();
match lower.strip_prefix("x86_").unwrap_or(&lower) {
"v2" | "baseline" => Ok(Self::Baseline),
"v3" => Ok(Self::V3),
"v4" => Ok(Self::V4),
_ => Err(UnknownLevel(name.to_owned())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names_round_trip() {
for level in CpuLevel::ALL {
assert_eq!(level.name().parse(), Ok(level));
assert_eq!(level.to_string(), level.name());
}
assert_eq!("X86_V3".parse(), Ok(CpuLevel::V3));
assert_eq!(" Baseline\n".parse(), Ok(CpuLevel::Baseline));
}
#[test]
fn unknown_names_are_refused() {
for name in ["", "v5", "avx2", "x86_", "3"] {
let error = name.parse::<CpuLevel>().unwrap_err();
assert!(error.to_string().contains("expected v2, v3 or v4"));
}
}
#[test]
fn numpys_groups_decide_where_it_has_them() {
let groups = |x86_v3, x86_v4| NumpyDispatch {
x86_v3: Some(x86_v3),
x86_v4: Some(x86_v4),
avx2: true,
avx512_skx: true,
};
assert_eq!(CpuLevel::from_numpy(groups(true, true)), CpuLevel::V4);
assert_eq!(CpuLevel::from_numpy(groups(true, false)), CpuLevel::V3);
assert_eq!(
CpuLevel::from_numpy(groups(false, false)),
CpuLevel::Baseline
);
assert_eq!(
CpuLevel::from_numpy(groups(false, true)),
CpuLevel::Baseline
);
}
#[test]
fn numpys_features_decide_before_the_groups() {
let features = |avx2, avx512_skx| NumpyDispatch {
avx2,
avx512_skx,
..NumpyDispatch::default()
};
assert_eq!(CpuLevel::from_numpy(features(true, true)), CpuLevel::V4);
assert_eq!(CpuLevel::from_numpy(features(true, false)), CpuLevel::V3);
assert_eq!(
CpuLevel::from_numpy(features(false, false)),
CpuLevel::Baseline
);
assert_eq!(
CpuLevel::from_numpy(NumpyDispatch::default()),
CpuLevel::Baseline
);
}
#[test]
fn levels_are_ordered() {
assert!(CpuLevel::Baseline < CpuLevel::V3 && CpuLevel::V3 < CpuLevel::V4);
assert_eq!(CpuLevel::ALL.iter().max(), Some(&CpuLevel::V4));
}
#[test]
fn the_level_in_force_never_changes() {
let first = CpuLevel::current();
for level in CpuLevel::ALL {
assert_eq!(CpuLevel::set(level), first);
}
assert_eq!(CpuLevel::current(), first);
}
#[test]
fn detection_agrees_with_the_architecture() {
let level = CpuLevel::detect();
if cfg!(target_arch = "x86_64") {
assert!(CpuLevel::ALL.contains(&level));
if cfg!(windows) {
assert_ne!(level, CpuLevel::V4, "numpy's Windows wheels have no V4");
}
} else {
assert_eq!(level, CpuLevel::Baseline);
}
}
}