#![cfg_attr(portable_atomic_sanitize_thread, allow(dead_code))]
#[cfg(any(target_env = "sgx", miri))]
compile_error!("internal error: this module is not supported on this environment");
include!("common.rs");
#[cfg(not(portable_atomic_no_asm))]
use core::arch::asm;
use core::arch::x86_64::CpuidResult;
#[cfg(not(target_env = "sgx"))]
fn __cpuid(leaf: u32) -> CpuidResult {
let eax;
let mut ebx;
let ecx;
let edx;
unsafe {
asm!(
"mov r8, rbx", "cpuid",
"xchg r8, rbx", out("r8") ebx,
inout("eax") leaf => eax,
inout("ecx") 0 => ecx,
out("edx") edx,
options(nostack, preserves_flags),
);
}
CpuidResult { eax, ebx, ecx, edx }
}
#[cold]
fn _detect(info: &mut CpuInfo) {
let CpuidResult { ecx: proc_info_ecx, .. } = __cpuid(1);
if test(proc_info_ecx, 13) {
info.set(CpuInfoFlag::cmpxchg16b);
}
#[cfg(target_feature = "sse")]
{
use core::arch::x86_64::_xgetbv;
let cpu_xsave = test(proc_info_ecx, 26);
if cpu_xsave {
let cpu_osxsave = test(proc_info_ecx, 27);
if cpu_osxsave {
let xcr0 = unsafe { _xgetbv(0) };
let os_avx_support = xcr0 & 6 == 6;
if os_avx_support && test(proc_info_ecx, 28) {
info.set(CpuInfoFlag::avx);
}
}
}
}
}
#[allow(
clippy::alloc_instead_of_core,
clippy::std_instead_of_alloc,
clippy::std_instead_of_core,
clippy::undocumented_unsafe_blocks,
clippy::wildcard_imports
)]
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg_attr(portable_atomic_test_detect_false, ignore = "detection disabled")]
fn test_cpuid() {
#[cfg(target_vendor = "apple")]
assert_eq!(
std::is_x86_feature_detected!("avx"),
detect().avx() || cfg!(target_feature = "avx")
);
#[cfg(not(target_vendor = "apple"))]
assert_eq!(std::is_x86_feature_detected!("avx"), detect().avx());
assert_eq!(std::is_x86_feature_detected!("cmpxchg16b"), detect().cmpxchg16b());
}
}