1pub mod affine;
2pub mod affine2x;
3pub mod altmap;
4pub mod create;
5pub mod decoder;
6pub mod encoder;
7pub mod gf16;
8pub mod gf16_mac;
9pub mod layout;
10pub mod matrix;
11pub mod ops;
12pub mod packet;
13pub mod packet_reader;
14pub mod recovery_set;
15pub mod repair;
16pub mod shuffle2x;
17#[cfg(test)]
18pub(crate) mod test_support;
19pub mod verify;
20pub mod worker;
21pub mod yenc;
22
23pub use encoder::{
24 affine2x_buffer_size, affine_buffer_size, altmap_buffer_size, shuffle2x_buffer_size,
25};
26
27pub const DISPLAY_VERSION: &str = env!("CARGO_PKG_VERSION");
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, clap::ValueEnum)]
32pub enum SimdPath {
33 #[default]
35 Auto,
36 Scalar,
38 Ssse3,
40 Avx2,
42 Avx2Gfni,
44 Avx512Gfni,
46 Avx512Shuffle,
48 Neon,
50}
51
52impl std::fmt::Display for SimdPath {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 match self {
55 Self::Auto => write!(f, "auto"),
56 Self::Scalar => write!(f, "scalar"),
57 Self::Ssse3 => write!(f, "ssse3"),
58 Self::Avx2 => write!(f, "avx2"),
59 Self::Avx2Gfni => write!(f, "avx2-gfni"),
60 Self::Avx512Gfni => write!(f, "avx512-gfni"),
61 Self::Avx512Shuffle => write!(f, "avx512-shuffle"),
62 Self::Neon => write!(f, "neon"),
63 }
64 }
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, clap::ValueEnum)]
72pub enum EncoderLayout {
73 #[default]
74 Smart,
75 Normal,
76 Affine,
77 Affine512,
78 Shuffle2x,
79}
80
81impl std::fmt::Display for EncoderLayout {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 match self {
84 Self::Smart => write!(f, "smart"),
85 Self::Normal => write!(f, "normal"),
86 Self::Affine => write!(f, "affine"),
87 Self::Affine512 => write!(f, "affine512"),
88 Self::Shuffle2x => write!(f, "shuffle2x"),
89 }
90 }
91}
92
93pub fn detect_simd() -> &'static str {
95 #[cfg(target_arch = "x86_64")]
96 if std::is_x86_feature_detected!("avx512f")
97 && std::is_x86_feature_detected!("avx512bw")
98 && std::is_x86_feature_detected!("gfni")
99 {
100 return "AVX-512/GFNI";
101 }
102 #[cfg(target_arch = "x86_64")]
103 if std::is_x86_feature_detected!("avx2") && std::is_x86_feature_detected!("gfni") {
104 return "AVX2/GFNI";
105 }
106 #[cfg(target_arch = "x86_64")]
107 if std::is_x86_feature_detected!("avx512f") && std::is_x86_feature_detected!("avx512bw") {
108 return "AVX-512/Shuffle";
109 }
110 #[cfg(target_arch = "x86_64")]
111 if std::is_x86_feature_detected!("avx2") {
112 return "AVX2";
113 }
114 #[cfg(target_arch = "x86_64")]
115 if std::is_x86_feature_detected!("ssse3") {
116 return "SSSE3";
117 }
118 #[cfg(target_arch = "aarch64")]
119 {
120 return "NEON";
121 }
122 #[allow(unreachable_code)]
123 "scalar"
124}
125
126pub fn physical_core_count() -> usize {
130 #[cfg(target_os = "linux")]
131 {
132 use std::collections::HashSet;
133 if let Ok(info) = std::fs::read_to_string("/proc/cpuinfo") {
134 let mut seen: HashSet<(String, String)> = HashSet::new();
135 let (mut phys, mut core): (Option<String>, Option<String>) = (None, None);
136 for line in info.lines() {
137 if line.trim().is_empty() {
138 if let (Some(p), Some(c)) = (phys.take(), core.take()) {
139 seen.insert((p, c));
140 }
141 } else if let Some((key, val)) = line.split_once(':') {
142 match key.trim() {
143 "physical id" => phys = Some(val.trim().to_string()),
144 "core id" => core = Some(val.trim().to_string()),
145 _ => {}
146 }
147 }
148 }
149 if !seen.is_empty() {
150 return seen.len();
151 }
152 }
153 }
154 std::thread::available_parallelism()
155 .map(|n| n.get())
156 .unwrap_or(1)
157}
158
159pub fn performance_core_count() -> usize {
174 let usable = std::thread::available_parallelism()
175 .map(|n| n.get())
176 .unwrap_or(usize::MAX);
177 host_performance_core_count().min(usable).max(1)
178}
179
180fn host_performance_core_count() -> usize {
182 #[cfg(target_os = "linux")]
183 {
184 use std::collections::HashSet;
185
186 let Ok(entries) = std::fs::read_dir("/sys/devices/system/cpu") else {
187 return physical_core_count();
188 };
189
190 let mut paired_leaders: HashSet<usize> = HashSet::new();
191 let mut solo: HashSet<usize> = HashSet::new();
192
193 for entry in entries.flatten() {
194 let name = entry.file_name();
195 let name_s = name.to_string_lossy();
196 let Some(cpu_num) = name_s
197 .strip_prefix("cpu")
198 .and_then(|s| s.parse::<usize>().ok())
199 else {
200 continue;
201 };
202 let sib_path = entry.path().join("topology/thread_siblings_list");
203 let Ok(sib) = std::fs::read_to_string(sib_path) else {
204 continue;
205 };
206 let leader = sib
207 .trim()
208 .split([',', '-'])
209 .next()
210 .and_then(|s| s.parse::<usize>().ok())
211 .unwrap_or(cpu_num);
212 let count = sib.trim().split([',', '-']).count();
213 if count >= 2 {
214 paired_leaders.insert(leader);
215 } else {
216 solo.insert(cpu_num);
217 }
218 }
219
220 if !paired_leaders.is_empty() && !solo.is_empty() {
221 return paired_leaders.len() + solo.len();
222 }
223 }
224 physical_core_count()
225}
226
227#[cfg(test)]
228mod tests {
229 #[test]
230 fn performance_core_count_stays_within_usable_cpus() {
231 let usable = std::thread::available_parallelism().map_or(usize::MAX, |n| n.get());
232 let cores = super::performance_core_count();
233 assert!(
234 (1..=usable).contains(&cores),
235 "{cores} cores, {usable} usable"
236 );
237 }
238}