uucore 0.5.0

uutils ~ 'core' uutils code library (cross-platform)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

//! CPU hardware capability detection for performance-sensitive utilities
//!
//! This module provides a unified interface for detecting CPU features and
//! respecting environment-based SIMD policies (e.g., GLIBC_TUNABLES).
//!
//! It provides 2 structures, from which we can get capabilities:
//! - [`CpuFeatures`], which contains the raw available CPU features;
//! - [`SimdPolicy`], which relies on [`CpuFeatures`] and the `GLIBC_TUNABLES`
//!   environment variable to get the *enabled* CPU features
//!
//! # Use Cases
//!
//! - `cksum --debug`: Report hardware acceleration capabilities
//! - `wc --debug`: Report SIMD usage and GLIBC_TUNABLES restrictions
//! - Runtime decisions: Enable/disable SIMD paths based on environment
//!
//! # Examples
//!
//! ```no_run
//! use uucore::hardware::{CpuFeatures, SimdPolicy, HasHardwareFeatures as _};
//!
//! // Simple hardware detection
//! let features = CpuFeatures::detect();
//! if features.has_avx2() {
//!     println!("CPU has AVX2 support");
//! }
//!
//! // Check SIMD policy (respects GLIBC_TUNABLES)
//! let policy = SimdPolicy::detect();
//! if policy.has_avx2() {
//!     println!("CPU has AVX2 support and it is not disabled by env");
//! }
//! if policy.allows_simd() {
//!     // Use SIMD-accelerated path
//! } else {
//!     // Fall back to scalar implementation
//! }
//! ```

use std::collections::BTreeSet;
use std::env;
use std::sync::OnceLock;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum HardwareFeature {
    /// AVX-512 support (x86/x86_64 only)
    Avx512,
    /// AVX2 support (x86/x86_64 only)
    Avx2,
    /// PCLMULQDQ support for CRC acceleration (x86/x86_64 only)
    PclMul,
    /// VMULL support for CRC acceleration (ARM only)
    Vmull,
    /// SSE2 support (x86/x86_64 only)
    Sse2,
    /// ARM ASIMD/NEON support (aarch64 only)
    Asimd,
}

pub struct InvalidHardwareFeature;

impl TryFrom<&str> for HardwareFeature {
    type Error = InvalidHardwareFeature;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        use HardwareFeature::*;
        match value {
            "AVX512" | "AVX512F" => Ok(Avx512),
            "AVX2" => Ok(Avx2),
            "PCLMUL" | "PMULL" => Ok(PclMul),
            "VMULL" => Ok(Vmull),
            "SSE2" => Ok(Sse2),
            "ASIMD" => Ok(Asimd),
            _ => Err(InvalidHardwareFeature),
        }
    }
}

/// Trait for implementing common hardware feature checks.
///
/// This is used for the `CpuFeatures` struct, that holds the CPU capabilities,
/// and for the `SimdPolicy` type that computes the enabled features with the
/// environment variables.
pub trait HasHardwareFeatures {
    fn has_feature(&self, feat: HardwareFeature) -> bool;

    fn iter_features(&self) -> impl Iterator<Item = HardwareFeature>;

    /// Check if AVX-512 is available (x86/x86_64 only)
    #[inline]
    fn has_avx512(&self) -> bool {
        self.has_feature(HardwareFeature::Avx512)
    }

    /// Check if AVX2 is available (x86/x86_64 only)
    #[inline]
    fn has_avx2(&self) -> bool {
        self.has_feature(HardwareFeature::Avx2)
    }

    /// Check if PCLMULQDQ is available (x86/x86_64 only)
    #[inline]
    fn has_pclmul(&self) -> bool {
        self.has_feature(HardwareFeature::PclMul)
    }

    /// Check if VMULL is available (ARM only)
    #[inline]
    fn has_vmull(&self) -> bool {
        self.has_feature(HardwareFeature::Vmull)
    }

    /// Check if SSE2 is available (x86/x86_64 only)
    #[inline]
    fn has_sse2(&self) -> bool {
        self.has_feature(HardwareFeature::Sse2)
    }

    /// Check if ARM ASIMD/NEON is available (aarch64 only)
    #[inline]
    fn has_asimd(&self) -> bool {
        self.has_feature(HardwareFeature::Asimd)
    }
}

/// CPU hardware features that affect performance
///
/// Provides platform-specific CPU feature detection with caching.
/// Detection is performed once and cached for the lifetime of the process.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CpuFeatures {
    set: BTreeSet<HardwareFeature>,
}
impl CpuFeatures {
    pub fn detect() -> &'static Self {
        static FEATURES: OnceLock<CpuFeatures> = OnceLock::new();
        FEATURES.get_or_init(Self::detect_impl)
    }

    fn detect_impl() -> Self {
        let set = [
            (HardwareFeature::Avx512, detect_avx512 as fn() -> bool),
            (HardwareFeature::Avx2, detect_avx2),
            (HardwareFeature::PclMul, detect_pclmul),
            (HardwareFeature::Vmull, detect_vmull),
            (HardwareFeature::Sse2, detect_sse2),
            (HardwareFeature::Asimd, detect_asimd),
        ]
        .into_iter()
        .filter_map(|(feat, detect)| detect().then_some(feat))
        .collect();

        Self { set }
    }
}

impl HasHardwareFeatures for CpuFeatures {
    fn has_feature(&self, feat: HardwareFeature) -> bool {
        self.set.contains(&feat)
    }

    fn iter_features(&self) -> impl Iterator<Item = HardwareFeature> {
        self.set.iter().copied()
    }
}

/// SIMD policy based on environment variables
///
/// Respects GLIBC_TUNABLES environment variable to disable specific CPU features.
/// This is used by GNU utilities to allow users to disable hardware acceleration.
#[derive(Debug, Clone)]
pub struct SimdPolicy {
    /// Features disabled via GLIBC_TUNABLES (e.g., ["AVX2", "AVX512F"])
    disabled_by_env: BTreeSet<HardwareFeature>,
    hardware_features: &'static CpuFeatures,
}

impl SimdPolicy {
    /// Get the global SIMD policy (cached)
    ///
    /// This checks both hardware capabilities and the GLIBC_TUNABLES environment
    /// variable. The result is cached for the lifetime of the process.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use uucore::hardware::SimdPolicy;
    ///
    /// let policy = SimdPolicy::detect();
    /// if policy.allows_simd() {
    ///     println!("SIMD is enabled");
    /// } else {
    ///     println!("SIMD disabled by: {:?}", policy.disabled_features());
    /// }
    /// ```
    pub fn detect() -> &'static Self {
        static POLICY: OnceLock<SimdPolicy> = OnceLock::new();
        POLICY.get_or_init(Self::detect_impl)
    }

    fn detect_impl() -> Self {
        let tunables = env::var("GLIBC_TUNABLES").unwrap_or_default();
        let disabled_by_env = parse_disabled_features(&tunables);
        let hardware_features = CpuFeatures::detect();

        Self {
            disabled_by_env,
            hardware_features,
        }
    }

    pub fn allows_simd(&self) -> bool {
        self.disabled_by_env.is_empty()
    }

    pub fn disabled_features(&self) -> Vec<HardwareFeature> {
        self.disabled_by_env.iter().copied().collect()
    }
}

impl HasHardwareFeatures for SimdPolicy {
    fn has_feature(&self, feat: HardwareFeature) -> bool {
        self.hardware_features.has_feature(feat) && !self.disabled_by_env.contains(&feat)
    }

    fn iter_features(&self) -> impl Iterator<Item = HardwareFeature> {
        self.hardware_features
            .set
            .difference(&self.disabled_by_env)
            .copied()
    }
}

// Platform-specific feature detection

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn detect_avx512() -> bool {
    if cfg!(target_os = "android") {
        false
    } else {
        std::arch::is_x86_feature_detected!("avx512f")
            && std::arch::is_x86_feature_detected!("avx512bw")
    }
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn detect_avx512() -> bool {
    false
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn detect_avx2() -> bool {
    if cfg!(target_os = "android") {
        false
    } else {
        std::arch::is_x86_feature_detected!("avx2")
    }
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn detect_avx2() -> bool {
    false
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn detect_pclmul() -> bool {
    if cfg!(target_os = "android") {
        false
    } else {
        std::arch::is_x86_feature_detected!("pclmulqdq")
    }
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn detect_pclmul() -> bool {
    false
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn detect_sse2() -> bool {
    if cfg!(target_os = "android") {
        false
    } else {
        std::arch::is_x86_feature_detected!("sse2")
    }
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn detect_sse2() -> bool {
    false
}

#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
fn detect_asimd() -> bool {
    if cfg!(target_os = "android") {
        false
    } else {
        std::arch::is_aarch64_feature_detected!("asimd")
    }
}

#[cfg(not(all(target_arch = "aarch64", target_endian = "little")))]
fn detect_asimd() -> bool {
    false
}

#[cfg(target_arch = "aarch64")]
fn detect_vmull() -> bool {
    // VMULL is part of ARM NEON/ASIMD
    // For now, we use ASIMD as a proxy
    detect_asimd()
}

#[cfg(not(target_arch = "aarch64"))]
fn detect_vmull() -> bool {
    false
}

// GLIBC_TUNABLES parsing

/// Parse GLIBC_TUNABLES environment variable for disabled features
///
/// Format: `glibc.cpu.hwcaps=-AVX2,-AVX512F`
/// Multiple tunable sections can be separated by colons.
fn parse_disabled_features(tunables: &str) -> BTreeSet<HardwareFeature> {
    if tunables.is_empty() {
        return BTreeSet::new();
    }

    let mut disabled = BTreeSet::new();

    // GLIBC_TUNABLES format: "tunable1=value1:tunable2=value2"
    for entry in tunables.split(':') {
        let entry = entry.trim();
        let Some((name, raw_value)) = entry.split_once('=') else {
            continue;
        };

        // We only care about glibc.cpu.hwcaps
        if name.trim() != "glibc.cpu.hwcaps" {
            continue;
        }

        // Parse comma-separated features, disabled ones start with '-'
        for token in raw_value.split(',') {
            let token = token.trim();
            if let Some(feature) = token.strip_prefix('-') {
                let feature =
                    HardwareFeature::try_from(feature.trim().to_ascii_uppercase().as_str());
                if let Ok(feature) = feature {
                    disabled.insert(feature);
                }
            }
        }
    }

    disabled
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cpu_features_detection() {
        let features = CpuFeatures::detect();
        // Just verify it doesn't panic and returns consistent results
        let features2 = CpuFeatures::detect();
        assert_eq!(features, features2);
    }

    #[test]
    fn test_parse_disabled_features_empty() {
        assert_eq!(parse_disabled_features(""), BTreeSet::new());
    }

    #[test]
    fn test_parse_disabled_features_single() {
        let result = parse_disabled_features("glibc.cpu.hwcaps=-AVX2");
        let mut expected = BTreeSet::new();

        expected.insert(HardwareFeature::Avx2);

        assert_eq!(result, expected);
    }

    #[test]
    fn test_parse_disabled_features_multiple() {
        let result = parse_disabled_features("glibc.cpu.hwcaps=-AVX2,-AVX512F");
        let mut expected = BTreeSet::new();

        expected.insert(HardwareFeature::Avx2);
        expected.insert(HardwareFeature::Avx512);

        assert_eq!(result, expected);
    }

    #[test]
    fn test_parse_disabled_features_mixed() {
        let result = parse_disabled_features("glibc.cpu.hwcaps=-AVX2,SSE2,-AVX512F");
        let mut expected = BTreeSet::new();

        expected.insert(HardwareFeature::Avx2);
        expected.insert(HardwareFeature::Avx512);

        // Only features with '-' prefix are disabled
        assert_eq!(result, expected);
    }

    #[test]
    fn test_parse_disabled_features_with_other_tunables() {
        let result =
            parse_disabled_features("glibc.malloc.check=1:glibc.cpu.hwcaps=-AVX2:other=value");
        let mut expected = BTreeSet::new();

        expected.insert(HardwareFeature::Avx2);

        assert_eq!(result, expected);
    }

    #[test]
    fn test_parse_disabled_features_case_insensitive() {
        let result = parse_disabled_features("glibc.cpu.hwcaps=-avx2,-Avx512f");
        let mut expected = BTreeSet::new();

        expected.insert(HardwareFeature::Avx2);
        expected.insert(HardwareFeature::Avx512);

        // Only features with '-' prefix are disabled
        assert_eq!(result, expected);
    }

    #[test]
    fn test_simd_policy() {
        let policy = SimdPolicy::detect();
        // Just verify it works
        let _ = policy.allows_simd();
    }

    #[test]
    fn test_simd_policy_caching() {
        let policy1 = SimdPolicy::detect();
        let policy2 = SimdPolicy::detect();
        // Should be same instance (pointer equality)
        assert!(std::ptr::eq(policy1, policy2));
    }
}