cpufeatures/
lib.rs

1//! This crate provides macros for runtime CPU feature detection. It's intended
2//! as a stopgap until Rust [RFC 2725] adding first-class target feature detection
3//! macros to `libcore` is implemented.
4//!
5//! Supported target architectures:
6//! - `aarch64`: Linux and macOS/M4 only (ARM64 does not support OS-independent feature detection)
7//!   - Target features: `aes`, `sha2`, `sha3`
8//! - `x86`/`x86_64`: OS independent and `no_std`-friendly
9//!   - Target features: `adx`, `aes`, `avx`, `avx2`, `bmi1`, `bmi2`, `fma`,
10//!     `mmx`, `pclmulqdq`, `popcnt`, `rdrand`, `rdseed`, `sgx`, `sha`, `sse`,
11//!     `sse2`, `sse3`, `sse4.1`, `sse4.2`, `ssse3`
12//!
13//! If you would like detection support for a target feature which is not on
14//! this list, please [open a GitHub issue][gh].
15//!
16//! # Example
17//! ```
18//! # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
19//! # {
20//! // This macro creates `cpuid_aes_sha` module
21//! cpufeatures::new!(cpuid_aes_sha, "aes", "sha");
22//!
23//! // `token` is a Zero Sized Type (ZST) value, which guarantees
24//! // that underlying static storage got properly initialized,
25//! // which allows to omit initialization branch
26//! let token: cpuid_aes_sha::InitToken = cpuid_aes_sha::init();
27//!
28//! if token.get() {
29//!     println!("CPU supports both SHA and AES extensions");
30//! } else {
31//!     println!("SHA and AES extensions are not supported");
32//! }
33//!
34//! // If stored value needed only once you can get stored value
35//! // omitting the token
36//! let val = cpuid_aes_sha::get();
37//! assert_eq!(val, token.get());
38//!
39//! // Additionally you can get both token and value
40//! let (token, val) = cpuid_aes_sha::init_get();
41//! assert_eq!(val, token.get());
42//! # }
43//! ```
44//!
45//! Note that if all tested target features are enabled via compiler options
46//! (e.g. by using `RUSTFLAGS`), the `get` method will always return `true`
47//! and `init` will not use CPUID instruction. Such behavior allows
48//! compiler to completely eliminate fallback code.
49//!
50//! After first call macro caches result and returns it in subsequent
51//! calls, thus runtime overhead for them is minimal.
52//!
53//! [RFC 2725]: https://github.com/rust-lang/rfcs/pull/2725
54//! [gh]: https://github.com/RustCrypto/utils/issues/new?title=cpufeatures:%20requesting%20support%20for%20CHANGEME%20target%20feature
55
56#![no_std]
57#![doc(
58    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
59    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
60    html_root_url = "https://docs.rs/cpufeatures/0.2.1"
61)]
62
63#[cfg(all(target_arch = "aarch64"))]
64#[doc(hidden)]
65pub mod aarch64;
66
67#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
68mod x86;
69
70#[cfg(not(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")))]
71compile_error!("This crate works only on `aarch64`, `x86`, and `x86-64` targets.");
72
73/// Create module with CPU feature detection code.
74#[macro_export]
75macro_rules! new {
76    ($mod_name:ident, $($tf:tt),+ $(,)?) => {
77        mod $mod_name {
78            use core::sync::atomic::{AtomicU8, Ordering::Relaxed};
79
80            const UNINIT: u8 = u8::max_value();
81            static STORAGE: AtomicU8 = AtomicU8::new(UNINIT);
82
83            /// Initialization token
84            #[derive(Copy, Clone, Debug)]
85            pub struct InitToken(());
86
87            impl InitToken {
88                /// Get initialized value
89                #[inline(always)]
90                pub fn get(&self) -> bool {
91                    $crate::__unless_target_features! {
92                        $($tf),+ => {
93                            STORAGE.load(Relaxed) == 1
94                        }
95                    }
96                }
97            }
98
99            /// Initialize underlying storage if needed and get
100            /// stored value and initialization token.
101            #[inline]
102            pub fn init_get() -> (InitToken, bool) {
103                let res = $crate::__unless_target_features! {
104                    $($tf),+ => {
105                        // Relaxed ordering is fine, as we only have a single atomic variable.
106                        let val = STORAGE.load(Relaxed);
107
108                        if val == UNINIT {
109                            let res = $crate::__detect_target_features!($($tf),+);
110                            STORAGE.store(res as u8, Relaxed);
111                            res
112                        } else {
113                            val == 1
114                        }
115                    }
116                };
117
118                (InitToken(()), res)
119            }
120
121            /// Initialize underlying storage if needed and get
122            /// initialization token.
123            #[inline]
124            pub fn init() -> InitToken {
125                init_get().0
126            }
127
128            /// Initialize underlying storage if needed and get
129            /// stored value.
130            #[inline]
131            pub fn get() -> bool {
132                init_get().1
133            }
134        }
135    };
136}