#![allow(dead_code)]
#![allow(unreachable_code)]
pub struct CpuFeatures;
impl CpuFeatures {
pub const NONE: usize = 0;
pub const AVX2: usize = 1;
}
#[inline(always)]
pub fn is_enabled_sse() -> bool {
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
#[cfg(feature = "std")]
return std::is_x86_feature_detected!("sse");
#[cfg(all(target_arch = "x86_64", not(feature = "std"), not(target_env = "sgx")))]
return x86_nostd::has(x86_nostd::SSE);
false
}
#[inline(always)]
pub fn is_enabled_sse42() -> bool {
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
#[cfg(feature = "std")]
return std::is_x86_feature_detected!("sse4.2");
#[cfg(all(target_arch = "x86_64", not(feature = "std"), not(target_env = "sgx")))]
return x86_nostd::has(x86_nostd::SSE42);
false
}
#[inline(always)]
pub fn is_enabled_avx2_and_bmi2() -> bool {
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
{
#[cfg(all(
target_feature = "avx2",
target_feature = "bmi1",
target_feature = "bmi2"
))]
return true;
#[cfg(feature = "std")]
{
use std::sync::atomic::{AtomicU32, Ordering};
static CACHE: AtomicU32 = AtomicU32::new(2);
return match CACHE.load(Ordering::Relaxed) {
0 => false,
1 => true,
_ => {
let detected = std::is_x86_feature_detected!("avx2")
&& std::is_x86_feature_detected!("bmi1")
&& std::is_x86_feature_detected!("bmi2");
CACHE.store(u32::from(detected), Ordering::Relaxed);
detected
}
};
}
#[cfg(all(target_arch = "x86_64", not(feature = "std"), not(target_env = "sgx")))]
return x86_nostd::has(x86_nostd::AVX2 | x86_nostd::BMI1 | x86_nostd::BMI2);
}
false
}
#[inline(always)]
pub fn is_enabled_avx512() -> bool {
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
#[cfg(feature = "std")]
return std::is_x86_feature_detected!("avx512f");
#[cfg(all(target_arch = "x86_64", not(feature = "std"), not(target_env = "sgx")))]
return x86_nostd::has(x86_nostd::AVX512F);
false
}
#[inline(always)]
pub fn is_enabled_pclmulqdq() -> bool {
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "std")]
return std::is_x86_feature_detected!("pclmulqdq") && std::is_x86_feature_detected!("sse4.1");
#[cfg(all(target_arch = "x86_64", not(feature = "std"), not(target_env = "sgx")))]
return x86_nostd::has(x86_nostd::PCLMULQDQ | x86_nostd::SSE41);
false
}
#[inline(always)]
pub fn is_enabled_neon() -> bool {
#[cfg(target_arch = "aarch64")]
{
#[cfg(target_feature = "neon")]
return true;
#[cfg(feature = "std")]
return std::arch::is_aarch64_feature_detected!("neon");
}
false
}
#[inline(always)]
pub fn is_enabled_crc() -> bool {
#[cfg(target_arch = "aarch64")]
#[cfg(feature = "std")]
return std::arch::is_aarch64_feature_detected!("crc");
false
}
#[inline(always)]
pub fn is_enabled_lsx() -> bool {
#[cfg(all(target_arch = "loongarch64", feature = "lsx"))]
#[cfg(feature = "std")]
return std::arch::is_loongarch_feature_detected!("lsx");
false
}
#[inline(always)]
pub fn is_enabled_simd128() -> bool {
#[cfg(target_arch = "wasm32")]
return cfg!(target_feature = "simd128");
false
}
#[cfg(all(target_arch = "x86_64", not(target_env = "sgx")))]
pub(crate) mod x86_nostd {
use core::sync::atomic::{AtomicU32, Ordering};
pub const SSE: u32 = 1 << 0;
pub const SSE41: u32 = 1 << 1;
pub const SSE42: u32 = 1 << 2;
pub const PCLMULQDQ: u32 = 1 << 3;
pub const AVX2: u32 = 1 << 4;
pub const BMI1: u32 = 1 << 5;
pub const BMI2: u32 = 1 << 6;
pub const AVX512F: u32 = 1 << 7;
const DETECTED: u32 = 1 << 31;
static CACHE: AtomicU32 = AtomicU32::new(0);
#[inline(always)]
pub fn has(features: u32) -> bool {
let mut v = CACHE.load(Ordering::Relaxed);
if v == 0 {
v = detect() | DETECTED;
CACHE.store(v, Ordering::Relaxed);
}
v & features == features
}
#[cold]
#[inline(never)]
#[allow(unused_unsafe)] pub(crate) fn detect() -> u32 {
use core::arch::x86_64::{__cpuid, __cpuid_count};
let max_leaf = unsafe { __cpuid(0) }.eax;
let l1 = unsafe { __cpuid(1) };
let mut f = 0;
let bit = |r: u32, b: u32| r & (1 << b) != 0;
if bit(l1.edx, 25) {
f |= SSE;
}
if bit(l1.ecx, 1) {
f |= PCLMULQDQ;
}
if bit(l1.ecx, 19) {
f |= SSE41;
}
if bit(l1.ecx, 20) {
f |= SSE42;
}
let osxsave = bit(l1.ecx, 27);
let avx = bit(l1.ecx, 28);
let xcr0 = if osxsave { unsafe { xgetbv0() } } else { 0 };
let os_avx = avx && xcr0 & 0b110 == 0b110;
let os_avx512 = os_avx && xcr0 & 0xe0 == 0xe0;
if max_leaf >= 7 {
let l7 = unsafe { __cpuid_count(7, 0) };
if bit(l7.ebx, 3) {
f |= BMI1;
}
if bit(l7.ebx, 8) {
f |= BMI2;
}
if os_avx && bit(l7.ebx, 5) {
f |= AVX2;
}
if os_avx512 && bit(l7.ebx, 16) {
f |= AVX512F;
}
}
f
}
#[target_feature(enable = "xsave")]
unsafe fn xgetbv0() -> u64 {
unsafe { core::arch::x86_64::_xgetbv(0) }
}
#[cfg(all(test, feature = "std"))]
#[test]
fn agrees_with_std_detection() {
let f = detect();
let cases = [
(SSE, std::is_x86_feature_detected!("sse"), "sse"),
(SSE41, std::is_x86_feature_detected!("sse4.1"), "sse4.1"),
(SSE42, std::is_x86_feature_detected!("sse4.2"), "sse4.2"),
(
PCLMULQDQ,
std::is_x86_feature_detected!("pclmulqdq"),
"pclmulqdq",
),
(AVX2, std::is_x86_feature_detected!("avx2"), "avx2"),
(BMI1, std::is_x86_feature_detected!("bmi1"), "bmi1"),
(BMI2, std::is_x86_feature_detected!("bmi2"), "bmi2"),
(AVX512F, std::is_x86_feature_detected!("avx512f"), "avx512f"),
];
for (flag, std_says, name) in cases {
assert_eq!(
f & flag != 0,
std_says,
"{name}: core detection disagrees with std"
);
}
}
}
pub fn simd_path() -> &'static str {
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
{
if is_enabled_avx2_and_bmi2() {
return "avx2";
}
if is_enabled_sse42() {
return "sse4.2";
}
}
#[cfg(target_arch = "aarch64")]
{
if is_enabled_neon() {
return "neon";
}
}
#[cfg(target_arch = "wasm32")]
{
if is_enabled_simd128() {
return "simd128";
}
}
"scalar"
}