Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cpufeatures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ Target features:
- `adx`
- `aes`
- `avx`
- `avx10.1`*
- `avx10.2`*
- `avx2`
- `avx512bw`*
- `avx512cd`*
Expand Down
1 change: 1 addition & 0 deletions cpufeatures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ compile_error!("This crate works only on `aarch64`, `loongarch64`, `x86`, and `x
#[macro_export]
macro_rules! new {
($mod_name:ident, $($tf:tt),+ $(,)?) => {
#[allow(unexpected_cfgs)]
mod $mod_name {
use core::sync::atomic::{AtomicU8, Ordering::Relaxed};

Expand Down
25 changes: 24 additions & 1 deletion cpufeatures/src/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ macro_rules! __detect_target_features {
}

let cr = unsafe {
[cpuid(1), cpuid_count(7, 0), cpuid_count(7, 1)]
[
cpuid(1),
cpuid_count(7, 0),
cpuid_count(7, 1),
cpuid(0),
cpuid_count(0x24, 0),
]
};

$($crate::check!(cr, $tf) & )+ true
Expand Down Expand Up @@ -81,6 +87,23 @@ macro_rules! __expand_check_macro {
#[macro_export]
#[doc(hidden)]
macro_rules! check {
($cr:expr, "avx10.1") => {{
$crate::__xgetbv!($cr, 0b1110_0110)
& ($cr[3].eax >= 0x24)
& ($cr[2].edx & (1 << 19) != 0)
& (($cr[4].ebx & 0xff) >= 1)
& (($cr[4].ebx & (1 << 18)) != 0)
}};
($cr:expr, "avx10.2") => {{
$crate::__xgetbv!($cr, 0b1110_0110)
& ($cr[3].eax >= 0x24)
& ($cr[2].edx & (1 << 19) != 0)
& (($cr[4].ebx & 0xff) >= 2)
& (($cr[4].ebx & (1 << 18)) != 0)
& ($cr[2].eax & (1 << 4) != 0)
& ($cr[2].edx & (1 << 4) != 0)
& ($cr[2].edx & (1 << 10) != 0)
}};
$(
($cr:expr, $name) => {{
// Register bits are listed here:
Expand Down
168 changes: 168 additions & 0 deletions cpufeatures/tests/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![cfg(any(target_arch = "x86", target_arch = "x86_64"))]

cpufeatures::new!(cpuid, "aes", "sha");
cpufeatures::new!(cpuid_avx10, "avx10.1", "avx10.2");

#[test]
fn init() {
Expand All @@ -15,3 +16,170 @@ fn init_get() {
let (token, val) = cpuid::init_get();
assert_eq!(val, token.get());
}

#[test]
fn avx10_probe_does_not_fault() {
let (token, val) = cpuid_avx10::init_get();
assert_eq!(val, token.get());
let _ = cpuid_avx10::get();
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod avx10_logic {
#[cfg(target_arch = "x86")]
use core::arch::x86::CpuidResult;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::CpuidResult;

fn leaf1() -> CpuidResult {
CpuidResult {
eax: 0,
ebx: 0,
ecx: 0b11 << 26,
edx: 0,
}
}

fn blank() -> CpuidResult {
CpuidResult {
eax: 0,
ebx: 0,
ecx: 0,
edx: 0,
}
}

#[test]
fn version_zero_is_disabled() {
let cr = [
leaf1(),
blank(),
CpuidResult {
eax: 0,
ebx: 0,
ecx: 0,
edx: 1 << 19,
},
CpuidResult {
eax: 0x24,
ebx: 0,
ecx: 0,
edx: 0,
},
CpuidResult {
eax: 0,
ebx: (1 << 17) | (1 << 18),
ecx: 0,
edx: 0,
},
];
assert!(!cpufeatures::check!(cr, "avx10.1"));
assert!(!cpufeatures::check!(cr, "avx10.2"));
}

#[test]
fn max_leaf_guard_holds() {
let cr = [
leaf1(),
blank(),
CpuidResult {
eax: 0,
ebx: 0,
ecx: 0,
edx: 1 << 19,
},
CpuidResult {
eax: 0x16,
ebx: 0,
ecx: 0,
edx: 0,
},
CpuidResult {
eax: 0,
ebx: 1 | (1 << 17) | (1 << 18),
ecx: 0,
edx: 0,
},
];
assert!(!cpufeatures::check!(cr, "avx10.1"));
assert!(!cpufeatures::check!(cr, "avx10.2"));
}

#[test]
fn presence_bit_required() {
let cr = [
leaf1(),
blank(),
blank(),
CpuidResult {
eax: 0x24,
ebx: 0,
ecx: 0,
edx: 0,
},
CpuidResult {
eax: 0,
ebx: 1 | (1 << 17) | (1 << 18),
ecx: 0,
edx: 0,
},
];
assert!(!cpufeatures::check!(cr, "avx10.1"));
assert!(!cpufeatures::check!(cr, "avx10.2"));
}

#[test]
fn narrow_vector_without_512_bit_reports_false() {
let cr = [
leaf1(),
blank(),
CpuidResult {
eax: 0,
ebx: 0,
ecx: 0,
edx: 1 << 19,
},
CpuidResult {
eax: 0x24,
ebx: 0,
ecx: 0,
edx: 0,
},
CpuidResult {
eax: 0,
ebx: 1 | (1 << 17),
ecx: 0,
edx: 0,
},
];
assert!(!cpufeatures::check!(cr, "avx10.1"));
assert!(!cpufeatures::check!(cr, "avx10.2"));
}

#[test]
fn vnni_trio_required_for_avx10_2() {
let cr = [
leaf1(),
blank(),
CpuidResult {
eax: 0,
ebx: 0,
ecx: 0,
edx: 1 << 19,
},
CpuidResult {
eax: 0x24,
ebx: 0,
ecx: 0,
edx: 0,
},
CpuidResult {
eax: 0,
ebx: 2 | (1 << 18),
ecx: 0,
edx: 0,
},
];
assert!(!cpufeatures::check!(cr, "avx10.2"));
}
}