#[cfg(all(target_arch = "x86_64", not(target_feature = "fma")))]
mod imp {
use std::sync::atomic::{AtomicU8, Ordering};
const UNINIT: u8 = 0;
const ENABLED: u8 = 1;
const DISABLED: u8 = 2;
static STATE: AtomicU8 = AtomicU8::new(UNINIT);
#[inline(always)]
pub(crate) fn avx2_fma_enabled() -> bool {
match STATE.load(Ordering::Relaxed) {
ENABLED => true,
DISABLED => false,
_ => init(),
}
}
#[cold]
fn init() -> bool {
let forced_off = std::env::var("SP_MATMUL_RS_FORCE_BASELINE")
.map(|v| !v.is_empty() && v != "0")
.unwrap_or(false);
let ok = !forced_off
&& std::arch::is_x86_feature_detected!("avx2")
&& std::arch::is_x86_feature_detected!("fma");
STATE.store(if ok { ENABLED } else { DISABLED }, Ordering::Relaxed);
ok
}
pub fn force_simd_baseline_for_tests(on: bool) {
STATE.store(if on { DISABLED } else { UNINIT }, Ordering::Relaxed);
}
pub fn runtime_simd_label() -> &'static str {
if avx2_fma_enabled() {
"avx2+fma"
} else {
"baseline"
}
}
}
#[cfg(all(target_arch = "x86_64", not(target_feature = "fma")))]
pub(crate) use imp::avx2_fma_enabled;
#[cfg(all(target_arch = "x86_64", not(target_feature = "fma")))]
pub use imp::force_simd_baseline_for_tests;
#[cfg(all(target_arch = "x86_64", not(target_feature = "fma")))]
pub use imp::runtime_simd_label;
#[cfg(not(all(target_arch = "x86_64", not(target_feature = "fma"))))]
mod imp {
pub fn force_simd_baseline_for_tests(_on: bool) {}
pub fn runtime_simd_label() -> &'static str {
#[cfg(any(target_arch = "aarch64", target_feature = "fma"))]
{
"compile-time"
}
#[cfg(not(any(target_arch = "aarch64", target_feature = "fma")))]
{
"baseline"
}
}
}
#[cfg(not(all(target_arch = "x86_64", not(target_feature = "fma"))))]
pub use imp::{force_simd_baseline_for_tests, runtime_simd_label};