#[cfg(target_arch = "x86_64")]
use std::is_x86_feature_detected;
use std::sync::OnceLock;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DispatchKind {
Scalar,
#[cfg(target_arch = "x86_64")]
Avx2,
#[cfg(target_arch = "aarch64")]
Neon,
}
static DISPATCH: OnceLock<DispatchKind> = OnceLock::new();
#[must_use]
pub fn current() -> DispatchKind {
*DISPATCH.get_or_init(detect)
}
#[doc(hidden)]
#[allow(clippy::panic)]
pub fn force(kind: DispatchKind) {
assert!(
DISPATCH.set(kind).is_ok(),
"dispatch cache already populated; call force() before current()"
);
}
fn detect() -> DispatchKind {
#[cfg(target_arch = "x86_64")]
{
if is_x86_feature_detected!("avx2") && is_x86_feature_detected!("fma") {
return DispatchKind::Avx2;
}
}
#[cfg(target_arch = "aarch64")]
{
return DispatchKind::Neon;
}
#[allow(unreachable_code)]
DispatchKind::Scalar
}