@@ -14,14 +14,54 @@ Supports `no_std` as well as mobile targets including iOS and Android,
1414providing an alternative to the ` std ` -dependent ` is_x86_feature_detected! `
1515macro.
1616
17- [ Documentation] [ docs-link ]
18-
19- # Supported target architectures
17+ This crate is intended as a stopgap until Rust [ RFC 2725] adding first-class
18+ target feature detection macros to ` libcore ` is implemented.
19+
20+ ## Example
21+ ```
22+ #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
23+ pub mod x86_backend {
24+ // This macro creates `cpuid_aes_sha` module
25+ cpufeatures::new!(cpuid_aes_sha, "aes", "sha");
26+
27+ pub fn run() {
28+ // `token` is a Zero Sized Type (ZST) value, which guarantees
29+ // that underlying static storage got properly initialized,
30+ // which allows to omit initialization branch
31+ let token: cpuid_aes_sha::InitToken = cpuid_aes_sha::init();
32+
33+ if token.get() {
34+ println!("CPU supports both SHA and AES extensions");
35+ } else {
36+ println!("SHA and AES extensions are not supported");
37+ }
38+
39+ // If stored value needed only once you can get stored value
40+ // omitting the token
41+ let val = cpuid_aes_sha::get();
42+ assert_eq!(val, token.get());
43+
44+ // Additionally you can get both token and value
45+ let (token, val) = cpuid_aes_sha::init_get();
46+ assert_eq!(val, token.get());
47+ }
48+ }
49+ ```
50+
51+ Note that if all tested target features are enabled via compiler options
52+ (e.g. by using ` RUSTFLAGS ` ), the ` get ` method will always return ` true `
53+ and ` init ` will not use CPUID instruction. Such behavior allows
54+ compiler to completely eliminate fallback code.
55+
56+ After first call macro caches result and returns it in subsequent
57+ calls, thus runtime overhead for them is minimal.
58+
59+ ## Supported target architectures
2060
2161* NOTE: target features with an asterisk are unstable (nightly-only) and subject
22- to change to match upstream name changes in the Rust standard library.
62+ to change to match upstream name changes in the Rust standard library.*
2363
24- ## ` aarch64 `
64+ ### ` aarch64 `
2565
2666Linux, iOS, and macOS/ARM only (ARM64 does not support OS-independent feature detection)
2767
@@ -31,7 +71,7 @@ Target features:
3171- ` sha2 ` *
3272- ` sha3 ` *
3373
34- ## ` loongarch64 `
74+ ### ` loongarch64 `
3575
3676Linux only (LoongArch64 does not support OS-independent feature detection)
3777
@@ -51,7 +91,7 @@ Target features:
5191- ` lbt.mips ` *
5292- ` ptw ` *
5393
54- ## ` x86 ` /` x86_64 `
94+ ### ` x86 ` /` x86_64 `
5595
5696OS independent and ` no_std ` -friendly
5797
@@ -73,7 +113,7 @@ Target features:
73113- ` avx512vbmi2 ` *
74114- ` bmi1 `
75115- ` bmi2 `
76- - ` fma ` ,
116+ - ` fma `
77117- ` mmx `
78118- ` pclmulqdq `
79119- ` popcnt `
@@ -87,12 +127,12 @@ Target features:
87127- ` sse4.1 `
88128- ` sse4.2 `
89129- ` ssse3 `
90- - ` sha512 ` *
91- - ` sm3 ` *
92- - ` sm4 ` *
130+ - ` sha512 `
131+ - ` sm3 `
132+ - ` sm4 `
93133
94134If you would like detection support for a target feature which is not on
95- this list, please [ open a GitHub issue] .
135+ this list, please [ open a GitHub issue] [ github-issue ] .
96136
97137## License
98138
@@ -124,6 +164,7 @@ dual licensed as above, without any additional terms or conditions.
124164
125165[ // ] : # ( general links )
126166
127- [ RustCrypto ] : https://github.com/rustcrypto
167+ [ RustCrypto ] : https://github.com/RustCrypto
128168[ RustCrypto/utils#378 ] : https://github.com/RustCrypto/utils/issues/378
129- [ open a GitHub issue ] : https://github.com/RustCrypto/utils/issues/new?title=cpufeatures:%20requesting%20support%20for%20CHANGEME%20target%20feature
169+ [ RFC 2725 ] : https://github.com/rust-lang/rfcs/pull/2725
170+ [ github-issue ] : https://github.com/RustCrypto/utils/issues/new?title=cpufeatures:%20requesting%20support%20for%20CHANGEME%20target%20feature
0 commit comments