1use core::fmt;
6use std::sync::OnceLock;
7
8macro_rules! features {
9 ($($(#[$doc:meta])* $variant:ident = $name:literal,)*) => {
10 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
13 #[non_exhaustive]
14 pub enum Feature {
15 $($(#[$doc])* $variant,)*
16 }
17
18 impl Feature {
19 const ALL: &[Feature] = &[$(Feature::$variant,)*];
20
21 #[must_use]
27 pub const fn name(self) -> &'static str {
28 match self {
29 $(Feature::$variant => $name,)*
30 }
31 }
32 }
33 };
34}
35
36features! {
37 Sse2 = "sse2",
39 Sse3 = "sse3",
41 Ssse3 = "ssse3",
43 Sse41 = "sse4.1",
45 Sse42 = "sse4.2",
47 Popcnt = "popcnt",
49 Avx = "avx",
51 Avx2 = "avx2",
53 Fma = "fma",
55 F16c = "f16c",
57 Bmi1 = "bmi1",
59 Bmi2 = "bmi2",
61 Lzcnt = "lzcnt",
63 Movbe = "movbe",
65 Avx512f = "avx512f",
67 Avx512bw = "avx512bw",
69 Avx512cd = "avx512cd",
71 Avx512dq = "avx512dq",
73 Avx512vl = "avx512vl",
75 Avx512vnni = "avx512vnni",
77 Avx512vbmi = "avx512vbmi",
79 AvxVnni = "avxvnni",
81 Aes = "aes",
83 Pclmulqdq = "pclmulqdq",
85 Vaes = "vaes",
87 Vpclmulqdq = "vpclmulqdq",
89 Sha = "sha",
91 Rdrand = "rdrand",
93 Rdseed = "rdseed",
95 Adx = "adx",
97 Gfni = "gfni",
99 Neon = "neon",
101 Crc32 = "crc",
103 AesArm = "aes",
105 Pmull = "pmull",
107 Sha2 = "sha2",
109 Sha3 = "sha3",
111 Dotprod = "dotprod",
113 Fp16 = "fp16",
115 Lse = "lse",
117 Sve = "sve",
119 Sve2 = "sve2",
121 Rcpc = "rcpc",
123 Bf16 = "bf16",
125 I8mm = "i8mm",
127}
128
129#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
131pub struct Features {
132 bits: u64,
133}
134
135impl Features {
136 fn bit(feature: Feature) -> u64 {
137 1 << (feature as u32)
138 }
139
140 fn insert(&mut self, feature: Feature) {
141 self.bits |= Self::bit(feature);
142 }
143
144 #[must_use]
146 pub fn contains(&self, feature: Feature) -> bool {
147 self.bits & Self::bit(feature) != 0
148 }
149
150 pub fn iter(&self) -> impl Iterator<Item = Feature> + '_ {
152 Feature::ALL.iter().copied().filter(|f| self.contains(*f))
153 }
154
155 #[must_use]
157 pub fn len(&self) -> usize {
158 self.bits.count_ones() as usize
159 }
160
161 #[must_use]
163 pub fn is_empty(&self) -> bool {
164 self.bits == 0
165 }
166}
167
168impl fmt::Display for Features {
169 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171 for (i, feature) in self.iter().enumerate() {
172 if i > 0 {
173 f.write_str(" ")?;
174 }
175 f.write_str(feature.name())?;
176 }
177 Ok(())
178 }
179}
180
181#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
182fn detect() -> Features {
183 let mut set = Features::default();
184 macro_rules! check {
185 ($($variant:ident: $name:tt),* $(,)?) => {
186 $(if std::arch::is_x86_feature_detected!($name) { set.insert(Feature::$variant); })*
187 };
188 }
189 check!(
190 Sse2: "sse2", Sse3: "sse3", Ssse3: "ssse3", Sse41: "sse4.1", Sse42: "sse4.2",
191 Popcnt: "popcnt", Avx: "avx", Avx2: "avx2", Fma: "fma", F16c: "f16c",
192 Bmi1: "bmi1", Bmi2: "bmi2", Lzcnt: "lzcnt", Movbe: "movbe",
193 Avx512f: "avx512f", Avx512bw: "avx512bw", Avx512cd: "avx512cd",
194 Avx512dq: "avx512dq", Avx512vl: "avx512vl", Avx512vnni: "avx512vnni",
195 Avx512vbmi: "avx512vbmi", AvxVnni: "avxvnni", Aes: "aes",
196 Pclmulqdq: "pclmulqdq", Vaes: "vaes", Vpclmulqdq: "vpclmulqdq", Sha: "sha",
197 Rdrand: "rdrand", Rdseed: "rdseed", Adx: "adx", Gfni: "gfni",
198 );
199 set
200}
201
202#[cfg(target_arch = "aarch64")]
203fn detect() -> Features {
204 let mut set = Features::default();
205 macro_rules! check {
206 ($($variant:ident: $name:tt),* $(,)?) => {
207 $(if std::arch::is_aarch64_feature_detected!($name) { set.insert(Feature::$variant); })*
208 };
209 }
210 check!(
211 Neon: "neon", Crc32: "crc", AesArm: "aes", Pmull: "pmull", Sha2: "sha2",
212 Sha3: "sha3", Dotprod: "dotprod", Fp16: "fp16", Lse: "lse", Sve: "sve",
213 Sve2: "sve2", Rcpc: "rcpc", Bf16: "bf16", I8mm: "i8mm",
214 );
215 set
216}
217
218#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
219fn detect() -> Features {
220 Features::default()
221}
222
223#[must_use]
229pub fn features() -> &'static Features {
230 static FEATURES: OnceLock<Features> = OnceLock::new();
231 FEATURES.get_or_init(detect)
232}
233
234#[must_use]
246pub fn has(feature: Feature) -> bool {
247 features().contains(feature)
248}
249
250#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
252#[non_exhaustive]
253pub enum X86Level {
254 V1,
256 V2,
258 V3,
260 V4,
262}
263
264#[cfg(target_arch = "x86_64")]
265fn level() -> Option<X86Level> {
266 use std::arch::is_x86_feature_detected as has;
267 let v2 = has!("sse3")
268 && has!("ssse3")
269 && has!("sse4.1")
270 && has!("sse4.2")
271 && has!("popcnt")
272 && has!("cmpxchg16b");
273 let v3 = v2
274 && has!("avx")
275 && has!("avx2")
276 && has!("bmi1")
277 && has!("bmi2")
278 && has!("f16c")
279 && has!("fma")
280 && has!("lzcnt")
281 && has!("movbe")
282 && has!("xsave");
283 let v4 = v3
284 && has!("avx512f")
285 && has!("avx512bw")
286 && has!("avx512cd")
287 && has!("avx512dq")
288 && has!("avx512vl");
289 Some(match (v2, v3, v4) {
290 (_, _, true) => X86Level::V4,
291 (_, true, _) => X86Level::V3,
292 (true, _, _) => X86Level::V2,
293 _ => X86Level::V1,
294 })
295}
296
297#[cfg(not(target_arch = "x86_64"))]
298fn level() -> Option<X86Level> {
299 None
300}
301
302#[must_use]
309pub fn x86_level() -> Option<X86Level> {
310 static LEVEL: OnceLock<Option<X86Level>> = OnceLock::new();
311 *LEVEL.get_or_init(level)
312}
313
314#[cfg(test)]
315mod tests {
316 use super::*;
317
318 #[test]
319 fn test_cpu_features_fit_the_bitset_and_names_are_unique_per_arch() {
320 assert!(Feature::ALL.len() <= 64);
321 let mut x86: Vec<&str> = Feature::ALL[..Feature::ALL
322 .iter()
323 .position(|f| *f == Feature::Neon)
324 .unwrap()]
325 .iter()
326 .map(|f| f.name())
327 .collect();
328 x86.sort_unstable();
329 x86.dedup();
330 assert_eq!(x86.len(), 31);
331 }
332
333 #[test]
334 fn test_cpu_features_agree_with_std_and_the_level() {
335 let set = features();
336 assert_eq!(set.len(), set.iter().count());
337 assert_eq!(
338 set.to_string().split(' ').filter(|s| !s.is_empty()).count(),
339 set.len()
340 );
341 #[cfg(target_arch = "x86_64")]
342 {
343 assert!(has(Feature::Sse2), "SSE2 is the x86-64 baseline");
344 assert_eq!(
345 has(Feature::Avx2),
346 std::arch::is_x86_feature_detected!("avx2")
347 );
348 let level = x86_level().unwrap();
349 assert!(level >= X86Level::V1);
350 if level >= X86Level::V3 {
351 assert!(has(Feature::Avx2) && has(Feature::Fma));
352 }
353 }
354 #[cfg(target_arch = "aarch64")]
355 {
356 assert!(has(Feature::Neon), "NEON is the AArch64 baseline");
357 assert_eq!(x86_level(), None);
358 }
359 }
360}