| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
#[derive(Clone, Copy)] |
| 4 |
#[repr(transparent)] |
| 5 |
pub(crate) struct CpuInfo(u32); |
| 6 |
|
| 7 |
impl CpuInfo { |
| 8 |
#[inline] |
| 9 |
fn set(&mut self, bit: CpuInfoFlag) { |
| 10 |
self.0 = set(self.0, bit as u32); |
| 11 |
} |
| 12 |
#[inline] |
| 13 |
#[must_use] |
| 14 |
fn test(self, bit: CpuInfoFlag) -> bool { |
| 15 |
test(self.0, bit as u32) |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
#[inline] |
| 20 |
#[must_use] |
| 21 |
fn set(x: u32, bit: u32) -> u32 { |
| 22 |
x | (1 << bit) |
| 23 |
} |
| 24 |
#[inline] |
| 25 |
#[must_use] |
| 26 |
fn test(x: u32, bit: u32) -> bool { |
| 27 |
x & (1 << bit) != 0 |
| 28 |
} |
| 29 |
|
| 30 |
#[inline] |
| 31 |
pub(crate) fn detect() -> CpuInfo { |
| 32 |
use core::sync::atomic::{AtomicU32, Ordering}; |
| 33 |
|
| 34 |
static CACHE: AtomicU32 = AtomicU32::new(0); |
| 35 |
let mut info = CpuInfo(CACHE.load(Ordering::Relaxed)); |
| 36 |
if info.0 != 0 { |
| 37 |
return info; |
| 38 |
} |
| 39 |
info.set(CpuInfoFlag::Init); |
| 40 |
// Note: detect_false cfg is intended to make it easy for developers to test |
| 41 |
// cases where features usually available is not available, and is not a public API. |
| 42 |
if !cfg!(portable_atomic_test_detect_false) { |
| 43 |
info = _detect(info); |
| 44 |
} |
| 45 |
if cfg!(any( |
| 46 |
all( |
| 47 |
target_arch = "x86_64", |
| 48 |
not(any(target_feature = "cmpxchg16b", portable_atomic_target_feature = "cmpxchg16b")), |
| 49 |
), |
| 50 |
all( |
| 51 |
target_arch = "powerpc64", |
| 52 |
not(any( |
| 53 |
target_feature = "quadword-atomics", |
| 54 |
portable_atomic_target_feature = "quadword-atomics", |
| 55 |
)), |
| 56 |
), |
| 57 |
all( |
| 58 |
any(target_arch = "riscv32", target_arch = "riscv64"), |
| 59 |
not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")), |
| 60 |
), |
| 61 |
)) { |
| 62 |
// Use CAS if the detection result affects lock-freeness. |
| 63 |
match CACHE.compare_exchange(0, info.0, Ordering::Relaxed, Ordering::Relaxed) { |
| 64 |
Ok(_) => info, |
| 65 |
Err(info) => { |
| 66 |
debug_assert_ne!(info, 0); |
| 67 |
CpuInfo(info) |
| 68 |
} |
| 69 |
} |
| 70 |
} else { |
| 71 |
CACHE.store(info.0, Ordering::Relaxed); |
| 72 |
info |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
macro_rules! flags { |
| 77 |
($( |
| 78 |
$(#[$attr:meta])* |
| 79 |
$func:ident($name:literal, any($($cfg:ident),*)), |
| 80 |
)*) => { |
| 81 |
#[allow(dead_code, non_camel_case_types)] |
| 82 |
#[derive(Clone, Copy)] |
| 83 |
#[cfg_attr(test, derive(PartialEq, Eq, PartialOrd, Ord))] |
| 84 |
#[repr(u32)] |
| 85 |
enum CpuInfoFlag { |
| 86 |
Init = 0, |
| 87 |
$($func,)* |
| 88 |
} |
| 89 |
impl CpuInfo { |
| 90 |
$( |
| 91 |
$(#[$attr])* |
| 92 |
#[cfg(any(test, not(any($($cfg = $name),*))))] |
| 93 |
#[inline] |
| 94 |
#[must_use] |
| 95 |
pub(crate) fn $func(self) -> bool { |
| 96 |
self.test(CpuInfoFlag::$func) |
| 97 |
} |
| 98 |
)* |
| 99 |
#[cfg(test)] // for test |
| 100 |
const ALL_FLAGS: &'static [(&'static str, CpuInfoFlag, bool)] = &[$( |
| 101 |
($name, CpuInfoFlag::$func, cfg!(any($($cfg = $name),*))), |
| 102 |
)*]; |
| 103 |
} |
| 104 |
#[test] |
| 105 |
#[cfg_attr(portable_atomic_test_detect_false, ignore = "detection disabled")] |
| 106 |
fn test_detect() {$( |
| 107 |
$(#[$attr])* |
| 108 |
{ |
| 109 |
const _: u32 = 1_u32 << CpuInfoFlag::$func as u32; |
| 110 |
assert_eq!($name.replace(|c: char| c == '-' || c == '.', "_"), stringify!($func)); |
| 111 |
if detect().$func() { |
| 112 |
assert!(detect().test(CpuInfoFlag::$func)); |
| 113 |
} else { |
| 114 |
assert!(!detect().test(CpuInfoFlag::$func)); |
| 115 |
} |
| 116 |
} |
| 117 |
)*} |
| 118 |
}; |
| 119 |
} |
| 120 |
|
| 121 |
// rustc definitions: https://github.com/rust-lang/rust/blob/ddaf12390d3ffb7d5ba74491a48f3cd528e5d777/compiler/rustc_target/src/target_features.rs |
| 122 |
|
| 123 |
// LLVM definitions: https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/AArch64/AArch64Features.td |
| 124 |
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))] |
| 125 |
flags! { |
| 126 |
// The Armv8.1 architecture extension |
| 127 |
// https://developer.arm.com/documentation/109697/2025_06/Feature-descriptions/The-Armv8-1-architecture-extension |
| 128 |
// FEAT_LSE, Large System Extensions |
| 129 |
// > This feature is supported in AArch64 state only. |
| 130 |
// > FEAT_LSE is OPTIONAL from Armv8.0. |
| 131 |
// > FEAT_LSE is mandatory from Armv8.1. |
| 132 |
lse("lse", any(target_feature /* 1.61+ */, portable_atomic_target_feature)), |
| 133 |
|
| 134 |
// The Armv8.3 architecture extension |
| 135 |
// https://developer.arm.com/documentation/109697/2025_06/Feature-descriptions/The-Armv8-3-architecture-extension |
| 136 |
// FEAT_LRCPC, Load-Acquire RCpc instructions |
| 137 |
// > These instructions are added to the A64 instruction set only. |
| 138 |
// > FEAT_LRCPC is OPTIONAL from Armv8.2. |
| 139 |
// > FEAT_LRCPC is mandatory from Armv8.3. |
| 140 |
#[cfg(test)] // test-only |
| 141 |
rcpc("rcpc", any(target_feature /* 1.61+ */)), |
| 142 |
|
| 143 |
// The Armv8.4 architecture extension |
| 144 |
// https://developer.arm.com/documentation/109697/2025_06/Feature-descriptions/The-Armv8-4-architecture-extension |
| 145 |
// FEAT_LSE2, Large System Extensions version 2 |
| 146 |
// > This feature is supported in AArch64 state only. |
| 147 |
// > FEAT_LSE2 is OPTIONAL from Armv8.2. |
| 148 |
// > FEAT_LSE2 is mandatory from Armv8.4. |
| 149 |
#[cfg_attr(not(test), allow(dead_code))] |
| 150 |
lse2("lse2", any(target_feature /* nightly */, portable_atomic_target_feature)), |
| 151 |
// FEAT_LRCPC2, Load-Acquire RCpc instructions version 2 |
| 152 |
// > These instructions are added to the A64 instruction set only. |
| 153 |
// > FEAT_LRCPC2 is OPTIONAL from Armv8.2. |
| 154 |
// > FEAT_LRCPC2 is mandatory from Armv8.4. |
| 155 |
// > If FEAT_LRCPC2 is implemented, then FEAT_LRCPC is implemented. |
| 156 |
#[cfg(test)] // test-only |
| 157 |
rcpc2("rcpc2", any(target_feature /* 1.61+ */)), |
| 158 |
|
| 159 |
// The Armv8.9 architecture extension |
| 160 |
// https://developer.arm.com/documentation/109697/2025_06/Feature-descriptions/The-Armv8-9-architecture-extension |
| 161 |
// FEAT_LRCPC3, Load-Acquire RCpc instructions version 3 |
| 162 |
// > This feature is supported in AArch64 state only. |
| 163 |
// > FEAT_LRCPC3 is OPTIONAL from Armv8.2. |
| 164 |
// > If FEAT_LRCPC3 is implemented, then FEAT_LRCPC2 is implemented. |
| 165 |
#[cfg_attr(not(test), allow(dead_code))] |
| 166 |
rcpc3("rcpc3", any(target_feature /* nightly */, portable_atomic_target_feature)), |
| 167 |
|
| 168 |
// The Armv9.4 architecture extension |
| 169 |
// https://developer.arm.com/documentation/109697/2025_06/Feature-descriptions/The-Armv9-4-architecture-extension |
| 170 |
// FEAT_LSE128, 128-bit Atomics |
| 171 |
// > This feature is supported in AArch64 state only. |
| 172 |
// > FEAT_LSE128 is OPTIONAL from Armv9.3. |
| 173 |
// > If FEAT_LSE128 is implemented, then FEAT_LSE is implemented. |
| 174 |
#[cfg_attr(not(test), allow(dead_code))] |
| 175 |
lse128("lse128", any(target_feature /* nightly */, portable_atomic_target_feature)), |
| 176 |
|
| 177 |
// The Armv9.6 architecture extension |
| 178 |
// https://developer.arm.com/documentation/109697/2025_06/Feature-descriptions/The-Armv9-6-architecture-extension |
| 179 |
// FEAT_LS64WB, LS64 for Write-back cacheable memory |
| 180 |
// > This feature is supported in AArch64 state only. |
| 181 |
// > FEAT_LS64WB is OPTIONAL from Armv9.2. |
| 182 |
// > If FEAT_LS64WB is implemented, then FEAT_LS64_ACCDATA is implemented. |
| 183 |
#[cfg(test)] // test-only |
| 184 |
ls64wb("ls64wb", any(/* no corresponding target feature */)), |
| 185 |
// FEAT_LSFE, Large System Float Extension |
| 186 |
// > This feature is supported in AArch64 state only. |
| 187 |
// > FEAT_LSFE is OPTIONAL from Armv9.3. |
| 188 |
// > If FEAT_LSFE is implemented, then FEAT_FP is implemented. |
| 189 |
#[cfg(test)] // test-only |
| 190 |
lsfe("lsfe", any(target_feature /* N/A */, portable_atomic_target_feature)), |
| 191 |
|
| 192 |
#[cfg(test)] // test-only |
| 193 |
cpuid("cpuid", any(/* no corresponding target feature */)), |
| 194 |
} |
| 195 |
|
| 196 |
// LLVM definitions: https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/ARM/ARMFeatures.td |
| 197 |
#[cfg(target_arch = "arm")] |
| 198 |
flags! { |
| 199 |
#[cfg(test)] // test-only |
| 200 |
lpae("lpae", any(/* no corresponding target feature */)), |
| 201 |
} |
| 202 |
|
| 203 |
// LLVM definitions: https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/PowerPC/PPC.td |
| 204 |
#[cfg(target_arch = "powerpc64")] |
| 205 |
flags! { |
| 206 |
// lqarx and stqcx. |
| 207 |
quadword_atomics("quadword-atomics", any(target_feature /* nightly */, portable_atomic_target_feature)), |
| 208 |
} |
| 209 |
|
| 210 |
// LLVM definitions: https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/RISCV/RISCVFeatures.td |
| 211 |
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] |
| 212 |
flags! { |
| 213 |
// amocas.{w,d,q} |
| 214 |
zacas("zacas", any(target_feature /* 1.94+ */, portable_atomic_target_feature)), |
| 215 |
#[cfg(test)] // test-only |
| 216 |
zabha("zabha", any(target_feature /* 1.94+ */, portable_atomic_target_feature)), |
| 217 |
#[cfg(test)] // test-only |
| 218 |
zalasr("zalasr", any(/* no corresponding target feature */)), |
| 219 |
} |
| 220 |
|
| 221 |
// LLVM definitions: https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/X86/X86.td |
| 222 |
#[cfg(target_arch = "x86_64")] |
| 223 |
flags! { |
| 224 |
// avx |
| 225 |
#[cfg(target_feature = "sse")] |
| 226 |
avx("avx", any(target_feature)), |
| 227 |
// cmpxchg16b |
| 228 |
cmpxchg16b("cmpxchg16b", any(target_feature /* 1.69+ */, portable_atomic_target_feature)), |
| 229 |
} |
| 230 |
|
| 231 |
#[allow( |
| 232 |
clippy::alloc_instead_of_core, |
| 233 |
clippy::std_instead_of_alloc, |
| 234 |
clippy::std_instead_of_core, |
| 235 |
clippy::undocumented_unsafe_blocks, |
| 236 |
clippy::wildcard_imports |
| 237 |
)] |
| 238 |
#[cfg(test)] |
| 239 |
mod tests_common { |
| 240 |
use std::{collections::BTreeSet, vec}; |
| 241 |
|
| 242 |
use super::*; |
| 243 |
|
| 244 |
#[test] |
| 245 |
fn test_bit_flags() { |
| 246 |
let mut flags = vec![("init", CpuInfoFlag::Init)]; |
| 247 |
flags.extend(CpuInfo::ALL_FLAGS.iter().map(|&(name, flag, _)| (name, flag))); |
| 248 |
let flag_set = flags.iter().map(|(_, flag)| flag).collect::<BTreeSet<_>>(); |
| 249 |
let name_set = flags.iter().map(|(name, _)| name).collect::<BTreeSet<_>>(); |
| 250 |
if flag_set.len() != flags.len() { |
| 251 |
panic!("CpuInfo flag values must be unique") |
| 252 |
} |
| 253 |
if name_set.len() != flags.len() { |
| 254 |
panic!("CpuInfo flag names must be unique") |
| 255 |
} |
| 256 |
|
| 257 |
let mut x = CpuInfo(0); |
| 258 |
for &(_, f) in &flags { |
| 259 |
assert!(!x.test(f)); |
| 260 |
} |
| 261 |
for i in 0..flags.len() { |
| 262 |
x.set(flags[i].1); |
| 263 |
for &(_, f) in &flags[..i + 1] { |
| 264 |
assert!(x.test(f)); |
| 265 |
} |
| 266 |
for &(_, f) in &flags[i + 1..] { |
| 267 |
assert!(!x.test(f)); |
| 268 |
} |
| 269 |
} |
| 270 |
for &(_, f) in &flags { |
| 271 |
assert!(x.test(f)); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
#[test] |
| 276 |
fn print_features() { |
| 277 |
use std::{fmt::Write as _, string::String}; |
| 278 |
|
| 279 |
let mut features = String::new(); |
| 280 |
features.push_str("\nfeatures:\n"); |
| 281 |
for &(name, flag, compile_time) in CpuInfo::ALL_FLAGS { |
| 282 |
let run_time = detect().test(flag); |
| 283 |
if run_time == compile_time { |
| 284 |
let _ = writeln!(features, " {}: {}", name, run_time); |
| 285 |
} else { |
| 286 |
let _ = writeln!( |
| 287 |
features, |
| 288 |
" {}: {} (compile-time), {} (run-time)", |
| 289 |
name, compile_time, run_time |
| 290 |
); |
| 291 |
} |
| 292 |
} |
| 293 |
test_helper::eprintln_nocapture!("{}", features); |
| 294 |
} |
| 295 |
|
| 296 |
// Static assertions for C type definitions. |
| 297 |
// Assertions with core::ffi types are in crate::utils::ffi module. |
| 298 |
#[cfg(not(any(windows, target_arch = "x86", target_arch = "x86_64")))] |
| 299 |
const _: fn() = || { |
| 300 |
use test_helper::sys; |
| 301 |
let _: crate::utils::ffi::c_char = 0 as sys::c_char; |
| 302 |
}; |
| 303 |
} |