Skip to content

Commit 3195562

Browse files
committed
Add std_detect::detect::features() -> impl Iterator<Item=(&'static str, bool)> API
1 parent 701c607 commit 3195562

File tree

9 files changed

+196
-12
lines changed

9 files changed

+196
-12
lines changed

crates/std_detect/src/detect/arch/aarch64.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ macro_rules! is_aarch64_feature_detected {
7878
/// PLEASE: do not use this, it is an implementation detail subject to change.
7979
#[doc(hidden)]
8080
#[allow(non_camel_case_types)]
81+
#[derive(Copy, Clone)]
8182
#[repr(u8)]
8283
#[unstable(feature = "stdsimd_internal", issue = "0")]
8384
pub enum Feature {
@@ -103,4 +104,26 @@ pub enum Feature {
103104
rcpc,
104105
/// Vector Dot-Product (ASIMDDP)
105106
dotprod,
107+
108+
// Do not add variants after last:
109+
_last,
110+
}
111+
112+
impl Feature {
113+
pub fn to_str(self) -> &'static str {
114+
match self {
115+
Feature::asimd => "asimd",
116+
Feature::pmull => "pmull",
117+
Feature::fp => "fp",
118+
Feature::fp16 => "fp16",
119+
Feature::sve => "sve",
120+
Feature::crc => "crc",
121+
Feature::crypto => "crypto",
122+
Feature::lse => "lse",
123+
Feature::rdm => "rdm",
124+
Feature::rcpc => "rcpc",
125+
Feature::dotprod => "dotprod",
126+
Feature::_last => unreachable!(),
127+
}
128+
}
106129
}

crates/std_detect/src/detect/arch/arm.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,25 @@ macro_rules! is_arm_feature_detected {
2929
/// PLEASE: do not use this, it is an implementation detail subject to change.
3030
#[doc(hidden)]
3131
#[allow(non_camel_case_types)]
32+
#[derive(Copy, Clone)]
3233
#[repr(u8)]
3334
#[unstable(feature = "stdsimd_internal", issue = "0")]
3435
pub enum Feature {
3536
/// ARM Advanced SIMD (NEON) - Aarch32
3637
neon,
3738
/// Polynomial Multiply
3839
pmull,
40+
41+
// Do not add variants after last:
42+
_last,
43+
}
44+
45+
impl Feature {
46+
pub fn to_str(self) -> &'static str {
47+
match self {
48+
Feature::neon => "neon",
49+
Feature::pmull => "pmull",
50+
Feature::_last => unreachable!(),
51+
}
52+
}
3953
}

crates/std_detect/src/detect/arch/mips.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,22 @@ macro_rules! is_mips_feature_detected {
2121
/// PLEASE: do not use this, it is an implementation detail subject to change.
2222
#[doc(hidden)]
2323
#[allow(non_camel_case_types)]
24+
#[derive(Copy, Clone)]
2425
#[repr(u8)]
2526
#[unstable(feature = "stdsimd_internal", issue = "0")]
2627
pub enum Feature {
2728
/// MIPS SIMD Architecture (MSA)
2829
msa,
30+
31+
// Do not add variants after last:
32+
_last,
33+
}
34+
35+
impl Feature {
36+
pub fn to_str(self) -> &'static str {
37+
match self {
38+
Feature::msa => "msa",
39+
Feature::_last => unreachable!(),
40+
}
41+
}
2942
}

crates/std_detect/src/detect/arch/mips64.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,22 @@ macro_rules! is_mips64_feature_detected {
2121
/// PLEASE: do not use this, it is an implementation detail subject to change.
2222
#[doc(hidden)]
2323
#[allow(non_camel_case_types)]
24+
#[derive(Copy, Clone)]
2425
#[repr(u8)]
2526
#[unstable(feature = "stdsimd_internal", issue = "0")]
2627
pub enum Feature {
2728
/// MIPS SIMD Architecture (MSA)
2829
msa,
30+
31+
// Do not add variants after last:
32+
_last,
33+
}
34+
35+
impl Feature {
36+
pub fn to_str(self) -> &'static str {
37+
match self {
38+
Feature::msa => "msa",
39+
Feature::_last => unreachable!(),
40+
}
41+
}
2942
}

crates/std_detect/src/detect/arch/powerpc.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ macro_rules! is_powerpc_feature_detected {
3030
/// PLEASE: do not use this, it is an implementation detail subject to change.
3131
#[doc(hidden)]
3232
#[allow(non_camel_case_types)]
33+
#[derive(Copy, Clone)]
3334
#[repr(u8)]
3435
#[unstable(feature = "stdsimd_internal", issue = "0")]
3536
pub enum Feature {
@@ -39,4 +40,18 @@ pub enum Feature {
3940
vsx,
4041
/// Power8
4142
power8,
43+
44+
// Do not add variants after last:
45+
_last,
46+
}
47+
48+
impl Feature {
49+
pub fn to_str(self) -> &'static str {
50+
match self {
51+
Feature::altivec => "altivec",
52+
Feature::vsx => "vsx",
53+
Feature::power8 => "power8",
54+
Feature::_last => unreachable!(),
55+
}
56+
}
4257
}

crates/std_detect/src/detect/arch/powerpc64.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ macro_rules! is_powerpc64_feature_detected {
3030
/// PLEASE: do not use this, it is an implementation detail subject to change.
3131
#[doc(hidden)]
3232
#[allow(non_camel_case_types)]
33+
#[derive(Copy, Clone)]
3334
#[repr(u8)]
3435
#[unstable(feature = "stdsimd_internal", issue = "0")]
3536
pub enum Feature {
@@ -39,4 +40,18 @@ pub enum Feature {
3940
vsx,
4041
/// Power8
4142
power8,
43+
44+
// Do not add variants after last:
45+
_last,
46+
}
47+
48+
impl Feature {
49+
pub fn to_str(self) -> &'static str {
50+
match self {
51+
Feature::altivec => "altivec",
52+
Feature::vsx => "vsx",
53+
Feature::power8 => "power8",
54+
Feature::_last => unreachable!(),
55+
}
56+
}
4257
}

crates/std_detect/src/detect/arch/x86.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ macro_rules! is_x86_feature_detected {
258258
/// This is an unstable implementation detail subject to change.
259259
#[allow(non_camel_case_types)]
260260
#[repr(u8)]
261+
#[derive(Copy, Clone)]
261262
#[doc(hidden)]
262263
#[unstable(feature = "stdsimd_internal", issue = "0")]
263264
pub enum Feature {
@@ -345,4 +346,54 @@ pub enum Feature {
345346
adx,
346347
/// RTM, Intel (Restricted Transactional Memory)
347348
rtm,
349+
// Do not add variants after last:
350+
_last,
351+
}
352+
353+
impl Feature {
354+
pub fn to_str(self) -> &'static str {
355+
match self {
356+
Feature::aes => "aes",
357+
Feature::pclmulqdq => "pclmulqdq",
358+
Feature::rdrand => "rdrand",
359+
Feature::rdseed => "rdseed",
360+
Feature::tsc => "tsc",
361+
Feature::mmx => "mmx",
362+
Feature::sse => "sse",
363+
Feature::sse2 => "sse2",
364+
Feature::sse3 => "sse3",
365+
Feature::ssse3 => "ssse3",
366+
Feature::sse4_1 => "sse4.1",
367+
Feature::sse4_2 => "sse4.2",
368+
Feature::sse4a => "sse4a",
369+
Feature::sha => "sha",
370+
Feature::avx => "avx",
371+
Feature::avx2 => "avx2",
372+
Feature::avx512f => "avx512f",
373+
Feature::avx512cd => "avx512cd",
374+
Feature::avx512er => "avx512er",
375+
Feature::avx512pf => "avx512pf",
376+
Feature::avx512bw => "avx512bw",
377+
Feature::avx512dq => "avx512dq",
378+
Feature::avx512vl => "avx512vl",
379+
Feature::avx512_ifma => "avx512ifma",
380+
Feature::avx512_vbmi => "avx512vbmi",
381+
Feature::avx512_vpopcntdq => "avx512vpopcntdq",
382+
Feature::fma => "fma",
383+
Feature::bmi => "bmi",
384+
Feature::bmi2 => "bmi2",
385+
Feature::abm => "abm",
386+
Feature::tbm => "tbm",
387+
Feature::popcnt => "popcnt",
388+
Feature::fxsr => "fxsr",
389+
Feature::xsave => "xsave",
390+
Feature::xsaveopt => "xsaveopt",
391+
Feature::xsaves => "xsaves",
392+
Feature::xsavec => "xsavec",
393+
Feature::cmpxchg16b => "cmpxchg16b",
394+
Feature::adx => "adx",
395+
Feature::rtm => "rtm",
396+
Feature::_last => unreachable!(),
397+
}
398+
}
348399
}

crates/std_detect/src/detect/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,36 @@ cfg_if! {
8383
}
8484
}
8585
pub use self::os::check_for;
86+
87+
/// Returns an `Iterator<Item=(&'static str, bool)>` where
88+
/// `Item.0` is the feature name, and `Item.1` is a `bool` which
89+
/// is `true` if the feature is supported by the host and `false` otherwise.
90+
#[unstable(feature = "stdsimd", issue = "27731")]
91+
pub fn features() -> impl Iterator<Item = (&'static str, bool)> {
92+
cfg_if! {
93+
if #[cfg(any(
94+
target_arch = "x86",
95+
target_arch = "x86_64",
96+
target_arch = "arm",
97+
target_arch = "aarch64",
98+
target_arch = "powerpc",
99+
target_arch = "powerpc64",
100+
target_arch = "mips",
101+
target_arch = "mips64",
102+
))] {
103+
fn impl_() -> impl Iterator<Item=(&'static str, bool)> {
104+
(0_u8..Feature::_last as u8).map(|discriminant: u8| {
105+
let f: Feature = unsafe { crate::mem::transmute(discriminant) };
106+
let name: &'static str = f.to_str();
107+
let enabled: bool = check_for(f);
108+
(name, enabled)
109+
})
110+
}
111+
} else {
112+
fn impl_() -> impl Iterator<Item=(&'static str, bool)> {
113+
(0_u8..0_u8).map(|_x: u8| ("", false))
114+
}
115+
}
116+
}
117+
impl_()
118+
}

crates/std_detect/tests/cpu-detection.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#![feature(stdsimd)]
22
#![cfg_attr(stdsimd_strict, deny(warnings))]
33
#![allow(clippy::option_unwrap_used, clippy::use_debug, clippy::print_stdout)]
4-
5-
#[cfg(any(
4+
#![cfg(any(
65
target_arch = "arm",
76
target_arch = "aarch64",
87
target_arch = "x86",
98
target_arch = "x86_64",
109
target_arch = "powerpc",
1110
target_arch = "powerpc64"
1211
))]
12+
1313
#[macro_use]
1414
extern crate std_detect;
1515

16+
#[test]
17+
fn all() {
18+
for (f, e) in std_detect::detect::features() {
19+
println!("{}: {}", f, e);
20+
}
21+
}
22+
1623
#[test]
1724
#[cfg(all(target_arch = "arm", any(target_os = "linux", target_os = "android")))]
1825
fn arm_linux() {
@@ -74,17 +81,17 @@ fn x86_all() {
7481
println!("sha: {:?}", is_x86_feature_detected!("sha"));
7582
println!("avx: {:?}", is_x86_feature_detected!("avx"));
7683
println!("avx2: {:?}", is_x86_feature_detected!("avx2"));
77-
println!("avx512f {:?}", is_x86_feature_detected!("avx512f"));
78-
println!("avx512cd {:?}", is_x86_feature_detected!("avx512cd"));
79-
println!("avx512er {:?}", is_x86_feature_detected!("avx512er"));
80-
println!("avx512pf {:?}", is_x86_feature_detected!("avx512pf"));
81-
println!("avx512bw {:?}", is_x86_feature_detected!("avx512bw"));
82-
println!("avx512dq {:?}", is_x86_feature_detected!("avx512dq"));
83-
println!("avx512vl {:?}", is_x86_feature_detected!("avx512vl"));
84-
println!("avx512_ifma {:?}", is_x86_feature_detected!("avx512ifma"));
85-
println!("avx512_vbmi {:?}", is_x86_feature_detected!("avx512vbmi"));
84+
println!("avx512f: {:?}", is_x86_feature_detected!("avx512f"));
85+
println!("avx512cd: {:?}", is_x86_feature_detected!("avx512cd"));
86+
println!("avx512er: {:?}", is_x86_feature_detected!("avx512er"));
87+
println!("avx512pf: {:?}", is_x86_feature_detected!("avx512pf"));
88+
println!("avx512bw: {:?}", is_x86_feature_detected!("avx512bw"));
89+
println!("avx512dq: {:?}", is_x86_feature_detected!("avx512dq"));
90+
println!("avx512vl: {:?}", is_x86_feature_detected!("avx512vl"));
91+
println!("avx512ifma: {:?}", is_x86_feature_detected!("avx512ifma"));
92+
println!("avx512vbmi: {:?}", is_x86_feature_detected!("avx512vbmi"));
8693
println!(
87-
"avx512_vpopcntdq {:?}",
94+
"avx512vpopcntdq: {:?}",
8895
is_x86_feature_detected!("avx512vpopcntdq")
8996
);
9097
println!("f16c: {:?}", is_x86_feature_detected!("f16c"));

0 commit comments

Comments
 (0)