|
| 1 | +// This file is part of the uutils coreutils package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +//! CPU hardware capability detection for cksum --debug |
| 7 | +//! |
| 8 | +//! This module detects available CPU features that affect cksum performance, |
| 9 | +//! matching GNU cksum's --debug behavior. |
| 10 | +
|
| 11 | +use std::sync::Once; |
| 12 | + |
| 13 | +/// CPU features that affect cksum performance |
| 14 | +#[derive(Debug, Clone, Copy)] |
| 15 | +pub struct CpuFeatures { |
| 16 | + pub avx512: bool, |
| 17 | + pub avx2: bool, |
| 18 | + pub pclmul: bool, |
| 19 | + pub vmull: bool, |
| 20 | +} |
| 21 | + |
| 22 | +impl CpuFeatures { |
| 23 | + /// Detect available CPU features (cached after first call) |
| 24 | + pub fn detect() -> Self { |
| 25 | + static ONCE: Once = Once::new(); |
| 26 | + static mut FEATURES: CpuFeatures = CpuFeatures { |
| 27 | + avx512: false, |
| 28 | + avx2: false, |
| 29 | + pclmul: false, |
| 30 | + vmull: false, |
| 31 | + }; |
| 32 | + |
| 33 | + unsafe { |
| 34 | + ONCE.call_once(|| { |
| 35 | + FEATURES = Self { |
| 36 | + avx512: has_avx512(), |
| 37 | + avx2: has_avx2(), |
| 38 | + pclmul: has_pclmul(), |
| 39 | + vmull: has_vmull(), |
| 40 | + }; |
| 41 | + }); |
| 42 | + FEATURES |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Print debug information to stderr |
| 47 | + /// Outputs CPU feature availability in GNU cksum format |
| 48 | + pub fn print_debug(&self) { |
| 49 | + self.print_feature("avx512", self.avx512); |
| 50 | + self.print_feature("avx2", self.avx2); |
| 51 | + self.print_feature("pclmul", self.pclmul); |
| 52 | + if cfg!(target_arch = "aarch64") { |
| 53 | + self.print_feature("vmull", self.vmull); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fn print_feature(&self, name: &str, available: bool) { |
| 58 | + let status = if available { |
| 59 | + format!("using {name} hardware support") |
| 60 | + } else { |
| 61 | + format!("{name} support not detected") |
| 62 | + }; |
| 63 | + eprintln!("cksum: {status}"); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// CPU feature detection functions |
| 68 | +// These use cpufeatures crate for cross-platform detection |
| 69 | + |
| 70 | +#[cfg(all( |
| 71 | + any(target_arch = "x86_64", target_arch = "x86"), |
| 72 | + not(target_os = "android") |
| 73 | +))] |
| 74 | +fn has_avx512() -> bool { |
| 75 | + cpufeatures::new!(cpuid_avx512, "avx512f", "avx512bw"); |
| 76 | + cpuid_avx512::get() |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(not(all( |
| 80 | + any(target_arch = "x86_64", target_arch = "x86"), |
| 81 | + not(target_os = "android") |
| 82 | +)))] |
| 83 | +fn has_avx512() -> bool { |
| 84 | + false |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(all( |
| 88 | + any(target_arch = "x86_64", target_arch = "x86"), |
| 89 | + not(target_os = "android") |
| 90 | +))] |
| 91 | +fn has_avx2() -> bool { |
| 92 | + cpufeatures::new!(cpuid_avx2, "avx2"); |
| 93 | + cpuid_avx2::get() |
| 94 | +} |
| 95 | + |
| 96 | +#[cfg(not(all( |
| 97 | + any(target_arch = "x86_64", target_arch = "x86"), |
| 98 | + not(target_os = "android") |
| 99 | +)))] |
| 100 | +fn has_avx2() -> bool { |
| 101 | + false |
| 102 | +} |
| 103 | + |
| 104 | +#[cfg(all( |
| 105 | + any(target_arch = "x86_64", target_arch = "x86"), |
| 106 | + not(target_os = "android") |
| 107 | +))] |
| 108 | +fn has_pclmul() -> bool { |
| 109 | + cpufeatures::new!(cpuid_pclmul, "pclmulqdq"); |
| 110 | + cpuid_pclmul::get() |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg(not(all( |
| 114 | + any(target_arch = "x86_64", target_arch = "x86"), |
| 115 | + not(target_os = "android") |
| 116 | +)))] |
| 117 | +fn has_pclmul() -> bool { |
| 118 | + false |
| 119 | +} |
| 120 | + |
| 121 | +#[cfg(target_arch = "aarch64")] |
| 122 | +fn has_vmull() -> bool { |
| 123 | + // ARM NEON support detection |
| 124 | + // This would require platform-specific code |
| 125 | + // For now, return false as a safe default |
| 126 | + false |
| 127 | +} |
| 128 | + |
| 129 | +#[cfg(not(target_arch = "aarch64"))] |
| 130 | +fn has_vmull() -> bool { |
| 131 | + false |
| 132 | +} |
| 133 | + |
| 134 | +#[cfg(test)] |
| 135 | +mod tests { |
| 136 | + use super::*; |
| 137 | + |
| 138 | + #[test] |
| 139 | + #[cfg(not(target_os = "android"))] |
| 140 | + fn test_cpu_features_detection() { |
| 141 | + let features = CpuFeatures::detect(); |
| 142 | + // Features should be valid booleans - just verify they can be detected |
| 143 | + let _ = features.avx512; |
| 144 | + let _ = features.avx2; |
| 145 | + let _ = features.pclmul; |
| 146 | + let _ = features.vmull; |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + #[cfg(not(target_os = "android"))] |
| 151 | + fn test_cpu_features_cached() { |
| 152 | + let features1 = CpuFeatures::detect(); |
| 153 | + let features2 = CpuFeatures::detect(); |
| 154 | + // Should return same values (cached) |
| 155 | + assert_eq!(features1.avx512, features2.avx512); |
| 156 | + assert_eq!(features1.avx2, features2.avx2); |
| 157 | + assert_eq!(features1.pclmul, features2.pclmul); |
| 158 | + assert_eq!(features1.vmull, features2.vmull); |
| 159 | + } |
| 160 | + |
| 161 | + #[test] |
| 162 | + #[cfg(all( |
| 163 | + any(target_arch = "x86_64", target_arch = "x86"), |
| 164 | + not(target_os = "android") |
| 165 | + ))] |
| 166 | + fn test_cpu_features_on_x86() { |
| 167 | + let features = CpuFeatures::detect(); |
| 168 | + // On x86/x86_64, at least one feature should be detected or all false |
| 169 | + // (depending on CPU capabilities) |
| 170 | + let _ = features; |
| 171 | + } |
| 172 | +} |
0 commit comments