1#[cfg(feature = "std")]
2static HAS_BMI2: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
3
4#[cfg(feature = "std")]
5pub fn has_bmi2() -> bool {
6 if cfg!(miri) {
7 return false;
8 }
9 *HAS_BMI2.get_or_init(detect_bmi2)
10}
11
12#[cfg(not(feature = "std"))]
13pub fn has_bmi2() -> bool {
14 compile_time_bmi2()
15}
16
17#[cfg(feature = "std")]
18fn detect_bmi2() -> bool {
19 #[cfg(target_arch = "x86_64")]
20 {
21 std::arch::is_x86_feature_detected!("bmi2")
22 }
23 #[cfg(not(target_arch = "x86_64"))]
24 {
25 false
26 }
27}
28
29#[cfg(not(feature = "std"))]
30fn compile_time_bmi2() -> bool {
31 #[cfg(target_arch = "x86_64")]
32 {
33 cfg!(target_feature = "bmi2")
34 }
35 #[cfg(not(target_arch = "x86_64"))]
36 {
37 false
38 }
39}