simd_vector_engine/
cpu.rs1use std::sync::OnceLock;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
9pub enum Isa {
10 Scalar = 0,
11 Neon = 1,
12 Avx2 = 2,
13 Avx512 = 3,
14}
15
16impl Isa {
17 pub const fn name(self) -> &'static str {
18 match self {
19 Isa::Scalar => "Scalar",
20 Isa::Neon => "NEON",
21 Isa::Avx2 => "AVX2",
22 Isa::Avx512 => "AVX-512",
23 }
24 }
25
26 pub const fn register_bits(self) -> usize {
27 match self {
28 Isa::Scalar => 32,
29 Isa::Neon => 128,
30 Isa::Avx2 => 256,
31 Isa::Avx512 => 512,
32 }
33 }
34
35 pub const fn lanes_32bit(self) -> usize {
36 self.register_bits() / 32
37 }
38}
39
40#[derive(Debug, Clone, Copy, Default)]
41pub struct CpuFeatures {
42 pub sse2: bool,
43 pub sse42: bool,
44 pub avx: bool,
45 pub avx2: bool,
46 pub fma: bool,
47 pub popcnt: bool,
48 pub avx512f: bool,
49 pub avx512bw: bool,
50 pub avx512dq: bool,
51 pub avx512vl: bool,
52 pub neon: bool,
53}
54
55impl CpuFeatures {
56 pub fn enabled(&self) -> Vec<&'static str> {
57 let mut v = Vec::new();
58 for (on, name) in [
59 (self.sse2, "sse2"),
60 (self.sse42, "sse4.2"),
61 (self.popcnt, "popcnt"),
62 (self.avx, "avx"),
63 (self.avx2, "avx2"),
64 (self.fma, "fma"),
65 (self.avx512f, "avx512f"),
66 (self.avx512bw, "avx512bw"),
67 (self.avx512dq, "avx512dq"),
68 (self.avx512vl, "avx512vl"),
69 (self.neon, "neon"),
70 ] {
71 if on {
72 v.push(name);
73 }
74 }
75 v
76 }
77}
78
79fn detect() -> CpuFeatures {
80 let mut f = CpuFeatures::default();
81
82 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
83 {
84 f.sse2 = is_x86_feature_detected!("sse2");
85 f.sse42 = is_x86_feature_detected!("sse4.2");
86 f.popcnt = is_x86_feature_detected!("popcnt");
87 f.avx = is_x86_feature_detected!("avx");
88 f.avx2 = is_x86_feature_detected!("avx2");
89 f.fma = is_x86_feature_detected!("fma");
90 f.avx512f = is_x86_feature_detected!("avx512f");
91 f.avx512bw = is_x86_feature_detected!("avx512bw");
92 f.avx512dq = is_x86_feature_detected!("avx512dq");
93 f.avx512vl = is_x86_feature_detected!("avx512vl");
94 }
95
96 #[cfg(target_arch = "aarch64")]
97 {
98 f.neon = std::arch::is_aarch64_feature_detected!("neon");
99 }
100
101 f
102}
103
104pub fn features() -> &'static CpuFeatures {
105 static CACHE: OnceLock<CpuFeatures> = OnceLock::new();
106 CACHE.get_or_init(detect)
107}
108
109pub fn best_isa() -> Isa {
111 static CACHE: OnceLock<Isa> = OnceLock::new();
112 *CACHE.get_or_init(|| {
113 if let Ok(forced) = std::env::var("SVE_ISA") {
114 let want = match forced.to_ascii_lowercase().as_str() {
115 "scalar" => Some(Isa::Scalar),
116 "neon" => Some(Isa::Neon),
117 "avx2" => Some(Isa::Avx2),
118 "avx512" | "avx-512" => Some(Isa::Avx512),
119 _ => None,
120 };
121 if let Some(w) = want {
122 if available_isas().contains(&w) {
123 return w;
124 }
125 }
126 }
127 *available_isas().last().unwrap_or(&Isa::Scalar)
128 })
129}
130
131pub fn available_isas() -> Vec<Isa> {
133 let f = features();
134 let mut v = vec![Isa::Scalar];
135 if f.neon {
136 v.push(Isa::Neon);
137 }
138 if f.avx2 {
139 v.push(Isa::Avx2);
140 }
141 if f.avx512f {
142 v.push(Isa::Avx512);
143 }
144 v
145}
146
147#[cfg(test)]
148mod tests {
149 use super::*;
150
151 #[test]
152 fn detection_never_panics_and_is_monotonic() {
153 let isas = available_isas();
154 assert!(isas.contains(&Isa::Scalar));
155 let mut sorted = isas.clone();
156 sorted.sort();
157 assert_eq!(isas, sorted);
158 assert!(isas.contains(&best_isa()));
159 }
160}