| 1 |
# [RustCrypto]: CPU Feature Detection |
| 2 |
|
| 3 |
[![crate][crate-image]][crate-link] |
| 4 |
[![Docs][docs-image]][docs-link] |
| 5 |
[![Build Status][build-image]][build-link] |
| 6 |
![Apache2/MIT licensed][license-image] |
| 7 |
![Rust Version][rustc-image] |
| 8 |
[![Project Chat][chat-image]][chat-link] |
| 9 |
|
| 10 |
Lightweight and efficient runtime CPU feature detection for `aarch64`, `loongarch64`, and |
| 11 |
`x86`/`x86_64` targets. |
| 12 |
|
| 13 |
Supports `no_std` as well as mobile targets including iOS and Android, |
| 14 |
providing an alternative to the `std`-dependent `is_x86_feature_detected!` |
| 15 |
macro. |
| 16 |
|
| 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 |
```rust |
| 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 |
| 60 |
|
| 61 |
*NOTE: target features with an asterisk are unstable (nightly-only) and subject |
| 62 |
to change to match upstream name changes in the Rust standard library.* |
| 63 |
|
| 64 |
### `aarch64` |
| 65 |
|
| 66 |
Linux, iOS, and macOS/ARM only (ARM64 does not support OS-independent feature detection) |
| 67 |
|
| 68 |
Target features: |
| 69 |
|
| 70 |
- `aes`* |
| 71 |
- `sha2`* |
| 72 |
- `sha3`* |
| 73 |
|
| 74 |
### `loongarch64` |
| 75 |
|
| 76 |
Linux only (`loongarch64` does not support OS-independent feature detection) |
| 77 |
|
| 78 |
Target features: |
| 79 |
|
| 80 |
- `32s`* |
| 81 |
- `f`* |
| 82 |
- `d`* |
| 83 |
- `frecipe`* |
| 84 |
- `div32`* |
| 85 |
- `lsx`* |
| 86 |
- `lasx`* |
| 87 |
- `lam-bh`* |
| 88 |
- `lamcas`* |
| 89 |
- `ld-seq-sa`* |
| 90 |
- `scq`* |
| 91 |
- `lbt`* |
| 92 |
- `lvz`* |
| 93 |
- `ual`* |
| 94 |
|
| 95 |
### `x86`/`x86_64` |
| 96 |
|
| 97 |
OS independent and `no_std`-friendly |
| 98 |
|
| 99 |
Target features: |
| 100 |
|
| 101 |
- `adx` |
| 102 |
- `aes` |
| 103 |
- `avx` |
| 104 |
- `avx2` |
| 105 |
- `avx512bw`* |
| 106 |
- `avx512cd`* |
| 107 |
- `avx512dq`* |
| 108 |
- `avx512er`* |
| 109 |
- `avx512f`* |
| 110 |
- `avx512ifma`* |
| 111 |
- `avx512pf`* |
| 112 |
- `avx512vl`* |
| 113 |
- `avx512vbmi`* |
| 114 |
- `avx512vbmi2`* |
| 115 |
- `bmi1` |
| 116 |
- `bmi2` |
| 117 |
- `fma` |
| 118 |
- `mmx` |
| 119 |
- `pclmulqdq` |
| 120 |
- `popcnt` |
| 121 |
- `rdrand` |
| 122 |
- `rdseed` |
| 123 |
- `sgx` |
| 124 |
- `sha` |
| 125 |
- `sse` |
| 126 |
- `sse2` |
| 127 |
- `sse3` |
| 128 |
- `sse4.1` |
| 129 |
- `sse4.2` |
| 130 |
- `ssse3` |
| 131 |
- `sha512` |
| 132 |
- `sm3` |
| 133 |
- `sm4` |
| 134 |
|
| 135 |
If you would like detection support for a target feature which is not on |
| 136 |
this list, please [open a GitHub issue][github-issue]. |
| 137 |
|
| 138 |
## License |
| 139 |
|
| 140 |
Licensed under either of: |
| 141 |
|
| 142 |
* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) |
| 143 |
* [MIT license](http://opensource.org/licenses/MIT) |
| 144 |
|
| 145 |
at your option. |
| 146 |
|
| 147 |
### Contribution |
| 148 |
|
| 149 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 150 |
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 151 |
dual licensed as above, without any additional terms or conditions. |
| 152 |
|
| 153 |
[//]: # (badges) |
| 154 |
|
| 155 |
[crate-image]: https://img.shields.io/crates/v/cpufeatures.svg |
| 156 |
[crate-link]: https://crates.io/crates/cpufeatures |
| 157 |
[docs-image]: https://docs.rs/cpufeatures/badge.svg |
| 158 |
[docs-link]: https://docs.rs/cpufeatures/ |
| 159 |
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg |
| 160 |
[rustc-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg |
| 161 |
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg |
| 162 |
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260052-utils |
| 163 |
[build-image]: https://github.com/RustCrypto/utils/actions/workflows/cpufeatures.yml/badge.svg?branch=master |
| 164 |
[build-link]: https://github.com/RustCrypto/utils/actions/workflows/cpufeatures.yml?query=branch:master |
| 165 |
|
| 166 |
[//]: # (general links) |
| 167 |
|
| 168 |
[RustCrypto]: https://github.com/RustCrypto |
| 169 |
[RustCrypto/utils#378]: https://github.com/RustCrypto/utils/issues/378 |
| 170 |
[RFC 2725]: https://github.com/rust-lang/rfcs/pull/2725 |
| 171 |
[github-issue]: https://github.com/RustCrypto/utils/issues/new?title=cpufeatures:%20requesting%20support%20for%20CHANGEME%20target%20feature |