Debian Codegraph @

debian/rust-vendor/crc-fast/src/feature_detection.rs

raw
1 // Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0.
2
3 //! Feature detection system for safe and efficient hardware acceleration across different
4 //! platforms.
5
6 #[cfg(all(
7 not(feature = "std"),
8 any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
9 ))]
10 use spin::Once;
11 #[cfg(all(
12 feature = "std",
13 any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
14 ))]
15 use std::sync::OnceLock;
16
17 #[cfg(all(feature = "alloc", not(feature = "std")))]
18 extern crate alloc;
19 #[cfg(all(
20 feature = "alloc",
21 not(feature = "std"),
22 any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
23 ))]
24 use alloc::string::{String, ToString};
25 #[cfg(any(
26 test,
27 all(
28 feature = "std",
29 any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
30 )
31 ))]
32 use std::string::{String, ToString};
33
34 /// Global ArchOps instance cache - initialized once based on feature detection results
35 #[cfg(all(
36 feature = "std",
37 any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
38 ))]
39 static ARCH_OPS_INSTANCE: OnceLock<ArchOpsInstance> = OnceLock::new();
40 #[cfg(all(
41 not(feature = "std"),
42 any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
43 ))]
44 static ARCH_OPS_INSTANCE: Once<ArchOpsInstance> = Once::new();
45
46 /// Performance tiers representing different hardware capability levels
47 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
48 #[allow(dead_code)] // Some variants may not be constructed on all target architectures
49 pub enum PerformanceTier {
50 // AArch64 tiers
51 AArch64AesSha3,
52 AArch64Aes,
53
54 // x86_64 tiers
55 X86_64Avx512Vpclmulqdq,
56 X86_64Avx512Pclmulqdq,
57 X86_64SsePclmulqdq,
58
59 // x86 tiers
60 X86SsePclmulqdq,
61
62 // Fallback
63 SoftwareTable,
64 }
65
66 /// Architecture-specific capabilities
67 #[derive(Debug, Clone, Copy)]
68 #[allow(dead_code)] // Some fields may not be read on all target architectures
69 pub struct ArchCapabilities {
70 // AArch64 features
71 pub has_aes: bool, // provides PMULL support for CRC calculations (NEON is implicit)
72 pub has_crc: bool, // provides native CRC32 instructions for fusion techniques
73 pub has_sha3: bool, // requires 'aes', provides EOR3 for XOR3 operations
74
75 // x86/x86_64 features
76 pub has_sse41: bool,
77 pub has_sse42: bool, // provides native CRC32C instructions for fusion techniques
78 pub has_pclmulqdq: bool,
79 pub has_avx512vl: bool, // implicitly enables avx512f, has XOR3 operations
80 pub has_vpclmulqdq: bool,
81
82 // Rust version gates
83 pub rust_version_supports_avx512: bool,
84 }
85
86 /// Helper function to convert a performance tier to a human-readable target string
87 /// Format: {architecture}-{intrinsics-family}-{intrinsics-features}
88 #[cfg(any(
89 test,
90 all(
91 feature = "alloc",
92 any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
93 )
94 ))]
95 #[inline(always)]
96 fn tier_to_target_string(tier: PerformanceTier) -> String {
97 match tier {
98 PerformanceTier::AArch64AesSha3 => "aarch64-neon-pmull-sha3".to_string(),
99 PerformanceTier::AArch64Aes => "aarch64-neon-pmull".to_string(),
100 PerformanceTier::X86_64Avx512Vpclmulqdq => "x86_64-avx512-vpclmulqdq".to_string(),
101 PerformanceTier::X86_64Avx512Pclmulqdq => "x86_64-avx512-pclmulqdq".to_string(),
102 PerformanceTier::X86_64SsePclmulqdq => "x86_64-sse-pclmulqdq".to_string(),
103 PerformanceTier::X86SsePclmulqdq => "x86-sse-pclmulqdq".to_string(),
104 PerformanceTier::SoftwareTable => "software-fallback-tables".to_string(),
105 }
106 }
107
108 /// Detect architecture-specific capabilities combining compile-time and runtime checks
109 ///
110 /// # Safety
111 /// Uses runtime feature detection which may access CPU-specific registers
112 #[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
113 unsafe fn detect_arch_capabilities() -> ArchCapabilities {
114 #[cfg(target_arch = "aarch64")]
115 {
116 detect_aarch64_features()
117 }
118
119 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
120 {
121 detect_x86_features()
122 }
123
124 #[cfg(not(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")))]
125 {
126 // Other architectures use software fallback
127 ArchCapabilities {
128 has_aes: false,
129 has_crc: false,
130 has_sha3: false,
131 has_sse41: false,
132 has_sse42: false,
133 has_pclmulqdq: false,
134 has_avx512vl: false,
135 has_vpclmulqdq: false,
136 rust_version_supports_avx512: false,
137 }
138 }
139 }
140
141 /// AArch64-specific feature detection
142 ///
143 /// Note: NEON is always available on AArch64 and is implicitly enabled by AES support.
144 /// AES support provides the PMULL instructions needed for CRC calculations.
145 #[inline(always)]
146 #[cfg(all(target_arch = "aarch64", feature = "std"))]
147 unsafe fn detect_aarch64_features() -> ArchCapabilities {
148 use std::arch::is_aarch64_feature_detected;
149
150 // AES is available on essentially all AArch64 CPUs and provides the PMULL instructions
151 let has_aes = is_aarch64_feature_detected!("aes");
152
153 // CRC provides native CRC32 instructions for fusion techniques
154 let has_crc = is_aarch64_feature_detected!("crc");
155
156 // SHA3 is available on modern Aarch64 CPUs, and provides the EOR3 instruction for efficient
157 // XOR3 operations.
158 let has_sha3 = is_aarch64_feature_detected!("sha3");
159
160 ArchCapabilities {
161 has_aes,
162 has_crc,
163 has_sha3,
164 has_sse41: false,
165 has_sse42: false,
166 has_pclmulqdq: false,
167 has_avx512vl: false,
168 has_vpclmulqdq: false,
169 rust_version_supports_avx512: false,
170 }
171 }
172
173 #[inline(always)]
174 #[cfg(all(target_arch = "aarch64", not(feature = "std")))]
175 unsafe fn detect_aarch64_features() -> ArchCapabilities {
176 ArchCapabilities {
177 has_aes: cfg!(target_feature = "aes"),
178 has_crc: cfg!(target_feature = "crc"),
179 has_sha3: cfg!(target_feature = "sha3"),
180 has_sse41: false,
181 has_sse42: false,
182 has_pclmulqdq: false,
183 has_avx512vl: false,
184 has_vpclmulqdq: false,
185 rust_version_supports_avx512: false,
186 }
187 }
188
189 /// x86/x86_64-specific feature detection
190 #[inline(always)]
191 #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "std"))]
192 unsafe fn detect_x86_features() -> ArchCapabilities {
193 use std::arch::is_x86_feature_detected;
194
195 // Check Rust version support for VPCLMULQDQ (requires 1.89+)
196 let rust_version_supports_avx512 = check_rust_version_supports_avx512();
197
198 // SSE 4.1 and PCLMULQDQ support are the baseline for hardware acceleration
199 let has_sse41 = is_x86_feature_detected!("sse4.1");
200 let has_pclmulqdq = has_sse41 && is_x86_feature_detected!("pclmulqdq");
201
202 // SSE 4.2 provides native CRC32C instructions for fusion techniques
203 let has_sse42 = is_x86_feature_detected!("sse4.2");
204
205 // After Rust 1.89, AVX-512VL and VPCLMULQDQ can be used if available
206 let has_avx512vl =
207 has_pclmulqdq && rust_version_supports_avx512 && is_x86_feature_detected!("avx512vl");
208 let has_vpclmulqdq =
209 has_avx512vl && rust_version_supports_avx512 && is_x86_feature_detected!("vpclmulqdq");
210
211 ArchCapabilities {
212 has_aes: false,
213 has_crc: false,
214 has_sha3: false,
215 has_sse41,
216 has_sse42,
217 has_pclmulqdq,
218 has_avx512vl,
219 has_vpclmulqdq,
220 rust_version_supports_avx512,
221 }
222 }
223
224 #[inline(always)]
225 #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), not(feature = "std")))]
226 unsafe fn detect_x86_features() -> ArchCapabilities {
227 let rust_version_supports_avx512 = check_rust_version_supports_avx512();
228
229 let has_sse41 = cfg!(target_feature = "sse4.1");
230 let has_sse42 = cfg!(target_feature = "sse4.2");
231 let has_pclmulqdq = has_sse41 && cfg!(target_feature = "pclmulqdq");
232 let has_avx512vl =
233 has_pclmulqdq && rust_version_supports_avx512 && cfg!(target_feature = "avx512vl");
234 let has_vpclmulqdq =
235 has_avx512vl && rust_version_supports_avx512 && cfg!(target_feature = "vpclmulqdq");
236
237 ArchCapabilities {
238 has_aes: false,
239 has_crc: false,
240 has_sha3: false,
241 has_sse41,
242 has_sse42,
243 has_pclmulqdq,
244 has_avx512vl,
245 has_vpclmulqdq,
246 rust_version_supports_avx512,
247 }
248 }
249
250 /// Check if the current Rust version supports VPCLMULQDQ intrinsics
251 /// VPCLMULQDQ intrinsics were stabilized in Rust 1.89
252 #[rustversion::since(1.89)]
253 #[inline(always)]
254 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
255 pub(crate) fn check_rust_version_supports_avx512() -> bool {
256 true
257 }
258
259 /// Check if the current Rust version supports VPCLMULQDQ intrinsics
260 /// VPCLMULQDQ intrinsics were stabilized in Rust 1.89
261 #[rustversion::before(1.89)]
262 #[inline(always)]
263 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
264 pub(crate) fn check_rust_version_supports_avx512() -> bool {
265 false
266 }
267
268 /// Select the appropriate performance tier based on detected capabilities
269 #[inline(always)]
270 #[allow(unused)]
271 pub(crate) fn select_performance_tier(capabilities: &ArchCapabilities) -> PerformanceTier {
272 #[cfg(target_arch = "aarch64")]
273 {
274 if capabilities.has_sha3 && capabilities.has_aes {
275 return PerformanceTier::AArch64AesSha3;
276 }
277
278 if capabilities.has_aes {
279 return PerformanceTier::AArch64Aes;
280 }
281 }
282
283 #[cfg(target_arch = "x86_64")]
284 {
285 if capabilities.has_vpclmulqdq {
286 return PerformanceTier::X86_64Avx512Vpclmulqdq;
287 }
288 if capabilities.has_avx512vl {
289 return PerformanceTier::X86_64Avx512Pclmulqdq;
290 }
291 if capabilities.has_pclmulqdq {
292 return PerformanceTier::X86_64SsePclmulqdq;
293 }
294 }
295
296 #[cfg(target_arch = "x86")]
297 {
298 if capabilities.has_pclmulqdq {
299 return PerformanceTier::X86SsePclmulqdq;
300 }
301 }
302
303 // Fallback to software implementation
304 PerformanceTier::SoftwareTable
305 }
306
307 /// Enum that holds the different ArchOps implementations for compile-time dispatch
308 /// This avoids the need for trait objects while still providing factory-based selection
309 #[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
310 #[rustversion::since(1.89)]
311 #[derive(Debug, Clone, Copy)]
312 pub enum ArchOpsInstance {
313 #[cfg(target_arch = "aarch64")]
314 Aarch64Aes(crate::arch::aarch64::aes::Aarch64AesOps),
315 #[cfg(target_arch = "aarch64")]
316 Aarch64AesSha3(crate::arch::aarch64::aes_sha3::Aarch64AesSha3Ops),
317 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
318 X86SsePclmulqdq(crate::arch::x86::sse::X86SsePclmulqdqOps),
319 #[cfg(target_arch = "x86_64")]
320 X86_64Avx512Pclmulqdq(crate::arch::x86_64::avx512::X86_64Avx512PclmulqdqOps),
321 #[cfg(target_arch = "x86_64")]
322 X86_64Avx512Vpclmulqdq(crate::arch::x86_64::avx512_vpclmulqdq::X86_64Avx512VpclmulqdqOps),
323 /// Software fallback - no ArchOps struct needed
324 SoftwareFallback,
325 }
326
327 #[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
328 #[rustversion::before(1.89)]
329 #[derive(Debug, Clone, Copy)]
330 pub enum ArchOpsInstance {
331 #[cfg(target_arch = "aarch64")]
332 Aarch64Aes(crate::arch::aarch64::aes::Aarch64AesOps),
333 #[cfg(target_arch = "aarch64")]
334 Aarch64AesSha3(crate::arch::aarch64::aes_sha3::Aarch64AesSha3Ops),
335 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
336 X86SsePclmulqdq(crate::arch::x86::sse::X86SsePclmulqdqOps),
337 /// Software fallback - no ArchOps struct needed
338 SoftwareFallback,
339 }
340
341 #[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
342 impl ArchOpsInstance {
343 #[inline(always)]
344 #[rustversion::since(1.89)]
345 pub fn get_tier(&self) -> PerformanceTier {
346 match self {
347 #[cfg(target_arch = "aarch64")]
348 ArchOpsInstance::Aarch64Aes(_) => PerformanceTier::AArch64Aes,
349 #[cfg(target_arch = "aarch64")]
350 ArchOpsInstance::Aarch64AesSha3(_) => PerformanceTier::AArch64AesSha3,
351 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
352 ArchOpsInstance::X86SsePclmulqdq(_) => PerformanceTier::X86SsePclmulqdq,
353 #[cfg(target_arch = "x86_64")]
354 ArchOpsInstance::X86_64Avx512Pclmulqdq(_) => PerformanceTier::X86_64Avx512Pclmulqdq,
355 #[cfg(target_arch = "x86_64")]
356 ArchOpsInstance::X86_64Avx512Vpclmulqdq(_) => PerformanceTier::X86_64Avx512Vpclmulqdq,
357 ArchOpsInstance::SoftwareFallback => PerformanceTier::SoftwareTable,
358 }
359 }
360
361 #[inline(always)]
362 #[rustversion::before(1.89)]
363 pub fn get_tier(&self) -> PerformanceTier {
364 match self {
365 #[cfg(target_arch = "aarch64")]
366 ArchOpsInstance::Aarch64Aes(_) => PerformanceTier::AArch64Aes,
367 #[cfg(target_arch = "aarch64")]
368 ArchOpsInstance::Aarch64AesSha3(_) => PerformanceTier::AArch64AesSha3,
369 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
370 ArchOpsInstance::X86SsePclmulqdq(_) => PerformanceTier::X86SsePclmulqdq,
371 ArchOpsInstance::SoftwareFallback => PerformanceTier::SoftwareTable,
372 }
373 }
374
375 /// Get a human-readable target string describing the active configuration
376 #[cfg(feature = "alloc")]
377 #[inline(always)]
378 pub fn get_target_string(&self) -> String {
379 tier_to_target_string(self.get_tier())
380 }
381 }
382
383 /// Get the global ArchOps instance (thread-safe, initialized once based on feature detection)
384 ///
385 /// This function provides access to the cached ArchOps instance that was selected based on
386 /// feature detection results at library initialization time, eliminating runtime feature
387 /// detection overhead from hot paths.
388 #[cfg(all(
389 feature = "std",
390 any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
391 ))]
392 pub fn get_arch_ops() -> &'static ArchOpsInstance {
393 ARCH_OPS_INSTANCE.get_or_init(create_arch_ops)
394 }
395
396 #[cfg(all(
397 not(feature = "std"),
398 any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
399 ))]
400 pub fn get_arch_ops() -> &'static ArchOpsInstance {
401 ARCH_OPS_INSTANCE.call_once(create_arch_ops)
402 }
403
404 /// Factory function that creates the appropriate ArchOps struct based on cached feature detection
405 ///
406 /// This function uses the cached feature detection results to select the optimal
407 /// architecture-specific implementation at library initialization time, eliminating
408 /// runtime feature detection overhead from hot paths.
409 #[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
410 fn create_arch_ops() -> ArchOpsInstance {
411 let capabilities = unsafe { detect_arch_capabilities() };
412 let tier = select_performance_tier(&capabilities);
413
414 create_arch_ops_from_tier(tier)
415 }
416
417 /// Helper function to create ArchOpsInstance from a performance tier for Rust 1.89+ (when AVX512
418 /// stabilized)
419 #[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
420 #[rustversion::since(1.89)]
421 fn create_arch_ops_from_tier(tier: PerformanceTier) -> ArchOpsInstance {
422 match tier {
423 #[cfg(target_arch = "aarch64")]
424 PerformanceTier::AArch64AesSha3 => {
425 use crate::arch::aarch64::aes_sha3::Aarch64AesSha3Ops;
426 ArchOpsInstance::Aarch64AesSha3(Aarch64AesSha3Ops::new())
427 }
428 #[cfg(target_arch = "aarch64")]
429 PerformanceTier::AArch64Aes => {
430 use crate::arch::aarch64::aes::Aarch64AesOps;
431 ArchOpsInstance::Aarch64Aes(Aarch64AesOps)
432 }
433 #[cfg(target_arch = "x86_64")]
434 PerformanceTier::X86_64Avx512Vpclmulqdq => {
435 use crate::arch::x86_64::avx512_vpclmulqdq::X86_64Avx512VpclmulqdqOps;
436 ArchOpsInstance::X86_64Avx512Vpclmulqdq(X86_64Avx512VpclmulqdqOps::new())
437 }
438 #[cfg(target_arch = "x86_64")]
439 PerformanceTier::X86_64Avx512Pclmulqdq => {
440 use crate::arch::x86_64::avx512::X86_64Avx512PclmulqdqOps;
441 ArchOpsInstance::X86_64Avx512Pclmulqdq(X86_64Avx512PclmulqdqOps::new())
442 }
443 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
444 PerformanceTier::X86_64SsePclmulqdq | PerformanceTier::X86SsePclmulqdq => {
445 create_x86_sse_pclmulqdq_ops()
446 }
447 PerformanceTier::SoftwareTable => {
448 // Use software fallback
449 ArchOpsInstance::SoftwareFallback
450 }
451 // Handle cases where the performance tier doesn't match the current architecture
452 _ => {
453 // This can happen when a tier is selected for a different architecture
454 // Fall back to software implementation
455 ArchOpsInstance::SoftwareFallback
456 }
457 }
458 }
459
460 /// Helper function to create ArchOpsInstance from a performance tier for Rust <1.89 (before AVX512
461 /// stabilized)
462 #[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
463 #[rustversion::before(1.89)]
464 fn create_arch_ops_from_tier(tier: PerformanceTier) -> ArchOpsInstance {
465 match tier {
466 #[cfg(target_arch = "aarch64")]
467 PerformanceTier::AArch64AesSha3 => {
468 use crate::arch::aarch64::aes_sha3::Aarch64AesSha3Ops;
469 ArchOpsInstance::Aarch64AesSha3(Aarch64AesSha3Ops::new())
470 }
471 #[cfg(target_arch = "aarch64")]
472 PerformanceTier::AArch64Aes => {
473 use crate::arch::aarch64::aes::Aarch64AesOps;
474 ArchOpsInstance::Aarch64Aes(Aarch64AesOps)
475 }
476 #[cfg(target_arch = "x86_64")]
477 PerformanceTier::X86_64Avx512Vpclmulqdq => {
478 // VPCLMULQDQ and AVX512 not available in older Rust versions, fall back to SSE
479 create_x86_sse_pclmulqdq_ops()
480 }
481 #[cfg(target_arch = "x86_64")]
482 PerformanceTier::X86_64Avx512Pclmulqdq => {
483 // AVX512 not available in older Rust versions, fall back to SSE
484 create_x86_sse_pclmulqdq_ops()
485 }
486 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
487 PerformanceTier::X86_64SsePclmulqdq | PerformanceTier::X86SsePclmulqdq => {
488 create_x86_sse_pclmulqdq_ops()
489 }
490 PerformanceTier::SoftwareTable => {
491 // Use software fallback
492 ArchOpsInstance::SoftwareFallback
493 }
494 // Handle cases where the performance tier doesn't match the current architecture
495 _ => {
496 // This can happen when a tier is selected for a different architecture
497 // Fall back to software implementation
498 ArchOpsInstance::SoftwareFallback
499 }
500 }
501 }
502
503 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
504 fn create_x86_sse_pclmulqdq_ops() -> ArchOpsInstance {
505 use crate::arch::x86::sse::X86SsePclmulqdqOps;
506 ArchOpsInstance::X86SsePclmulqdq(X86SsePclmulqdqOps)
507 }
508 /// Test-specific tier selection that works across all architectures for comprehensive testing
509 #[cfg(test)]
510 pub fn select_performance_tier_for_test(capabilities: &ArchCapabilities) -> PerformanceTier {
511 // AArch64 tier selection - SHA3 requires AES to be available
512 if capabilities.has_sha3 && capabilities.has_aes {
513 return PerformanceTier::AArch64AesSha3;
514 }
515
516 if capabilities.has_aes {
517 return PerformanceTier::AArch64Aes;
518 }
519
520 // x86_64 tier selection - VPCLMULQDQ requires AVX512VL
521 if capabilities.has_vpclmulqdq
522 && capabilities.has_avx512vl
523 && capabilities.rust_version_supports_avx512
524 {
525 return PerformanceTier::X86_64Avx512Vpclmulqdq;
526 }
527
528 // AVX512VL requires PCLMULQDQ and SSE4.1
529 if capabilities.has_avx512vl
530 && capabilities.has_pclmulqdq
531 && capabilities.rust_version_supports_avx512
532 {
533 return PerformanceTier::X86_64Avx512Pclmulqdq;
534 }
535
536 // PCLMULQDQ requires SSE4.1
537 if capabilities.has_pclmulqdq && capabilities.has_sse41 {
538 return PerformanceTier::X86_64SsePclmulqdq;
539 }
540
541 // Fallback to software implementation
542 PerformanceTier::SoftwareTable
543 }
544
545 #[cfg(test)]
546 mod tests {
547 use super::*;
548
549 #[test]
550 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
551 fn test_rust_version_check() {
552 let supports_vpclmulqdq = check_rust_version_supports_avx512();
553
554 // Should return a boolean without panicking
555 let _ = supports_vpclmulqdq;
556 }
557
558 #[test]
559 fn test_aarch64_tier_selection() {
560 // Test that aarch64 tier selection follows the expected hierarchy
561
562 // Test SHA3 + AES (highest tier) - NEON is implicit with AES
563 let capabilities_sha3 = ArchCapabilities {
564 has_aes: true,
565 has_crc: false,
566 has_sha3: true,
567 has_sse41: false,
568 has_sse42: false,
569 has_pclmulqdq: false,
570 has_avx512vl: false,
571 has_vpclmulqdq: false,
572 rust_version_supports_avx512: false,
573 };
574 assert_eq!(
575 select_performance_tier_for_test(&capabilities_sha3),
576 PerformanceTier::AArch64AesSha3
577 );
578
579 // Test AES only (baseline tier) - NEON is implicit with AES
580 let capabilities_aes = ArchCapabilities {
581 has_aes: true,
582 has_crc: false,
583 has_sha3: false,
584 has_sse41: false,
585 has_sse42: false,
586 has_pclmulqdq: false,
587 has_avx512vl: false,
588 has_vpclmulqdq: false,
589 rust_version_supports_avx512: false,
590 };
591 assert_eq!(
592 select_performance_tier_for_test(&capabilities_aes),
593 PerformanceTier::AArch64Aes
594 );
595
596 // Test missing AES (should fall back to software)
597 let capabilities_no_aes = ArchCapabilities {
598 has_aes: false,
599 has_crc: false,
600 has_sha3: false,
601 has_sse41: false,
602 has_sse42: false,
603 has_pclmulqdq: false,
604 has_avx512vl: false,
605 has_vpclmulqdq: false,
606 rust_version_supports_avx512: false,
607 };
608 assert_eq!(
609 select_performance_tier_for_test(&capabilities_no_aes),
610 PerformanceTier::SoftwareTable
611 );
612 }
613
614 #[test]
615 fn test_aarch64_feature_hierarchy() {
616 // Test that AArch64 feature hierarchy is properly maintained
617 // NEON is always available on AArch64 and implicit with AES
618 // AES provides PMULL instructions, SHA3 provides EOR3
619
620 // Create test capabilities with AES support (NEON is implicit)
621 let capabilities_with_aes = ArchCapabilities {
622 has_aes: true,
623 has_crc: false,
624 has_sha3: false,
625 has_sse41: false,
626 has_sse42: false,
627 has_pclmulqdq: false,
628 has_avx512vl: false,
629 has_vpclmulqdq: false,
630 rust_version_supports_avx512: false,
631 };
632
633 // AES support means we have PMULL instructions available for CRC calculations
634 assert!(capabilities_with_aes.has_aes);
635
636 // SHA3 requires AES to be available first
637 let capabilities_with_sha3 = ArchCapabilities {
638 has_aes: true,
639 has_crc: false,
640 has_sha3: true,
641 has_sse41: false,
642 has_sse42: false,
643 has_pclmulqdq: false,
644 has_avx512vl: false,
645 has_vpclmulqdq: false,
646 rust_version_supports_avx512: false,
647 };
648
649 assert!(capabilities_with_sha3.has_aes);
650 assert!(capabilities_with_sha3.has_sha3);
651 }
652
653 #[test]
654 #[cfg(target_arch = "x86_64")]
655 fn test_x86_64_tier_selection() {
656 // Test that x86_64 tier selection follows the expected hierarchy
657
658 // Test VPCLMULQDQ + AVX512 (highest tier) on Rust 1.89+
659 let capabilities_vpclmulqdq = ArchCapabilities {
660 has_aes: false,
661 has_crc: false,
662 has_sha3: false,
663 has_sse41: true,
664 has_sse42: false,
665 has_pclmulqdq: true,
666 has_avx512vl: true,
667 has_vpclmulqdq: true,
668 rust_version_supports_avx512: true,
669 };
670 assert_eq!(
671 select_performance_tier_for_test(&capabilities_vpclmulqdq),
672 PerformanceTier::X86_64Avx512Vpclmulqdq
673 );
674
675 // Test AVX512 + PCLMULQDQ (mid-tier) on Rust 1.89+
676 let capabilities_avx512 = ArchCapabilities {
677 has_aes: false,
678 has_crc: false,
679 has_sha3: false,
680 has_sse41: true,
681 has_sse42: false,
682 has_pclmulqdq: true,
683 has_avx512vl: true,
684 has_vpclmulqdq: false,
685 rust_version_supports_avx512: true,
686 };
687 assert_eq!(
688 select_performance_tier_for_test(&capabilities_avx512),
689 PerformanceTier::X86_64Avx512Pclmulqdq
690 );
691
692 // Test VPCLMULQDQ + AVX512 (highest tier) on Rust < 1.89
693 let capabilities_vpclmulqdq = ArchCapabilities {
694 has_aes: false,
695 has_crc: false,
696 has_sha3: false,
697 has_sse41: true,
698 has_sse42: false,
699 has_pclmulqdq: true,
700 has_avx512vl: true,
701 has_vpclmulqdq: true,
702 rust_version_supports_avx512: false,
703 };
704 assert_eq!(
705 select_performance_tier_for_test(&capabilities_vpclmulqdq),
706 PerformanceTier::X86_64SsePclmulqdq
707 );
708
709 // Test AVX512 + PCLMULQDQ (mid-tier) on Rust < 1.89
710 let capabilities_avx512 = ArchCapabilities {
711 has_aes: false,
712 has_crc: false,
713 has_sha3: false,
714 has_sse41: true,
715 has_sse42: false,
716 has_pclmulqdq: true,
717 has_avx512vl: true,
718 has_vpclmulqdq: false,
719 rust_version_supports_avx512: false,
720 };
721 assert_eq!(
722 select_performance_tier_for_test(&capabilities_avx512),
723 PerformanceTier::X86_64SsePclmulqdq
724 );
725
726 // Test SSE + PCLMULQDQ (baseline tier)
727 let capabilities_sse = ArchCapabilities {
728 has_aes: false,
729 has_crc: false,
730 has_sha3: false,
731 has_sse41: true,
732 has_sse42: false,
733 has_pclmulqdq: true,
734 has_avx512vl: false,
735 has_vpclmulqdq: false,
736 rust_version_supports_avx512: false,
737 };
738 assert_eq!(
739 select_performance_tier_for_test(&capabilities_sse),
740 PerformanceTier::X86_64SsePclmulqdq
741 );
742
743 // Test missing PCLMULQDQ (should fall back to software)
744 let capabilities_no_pclmul = ArchCapabilities {
745 has_aes: false,
746 has_crc: false,
747 has_sha3: false,
748 has_sse41: true,
749 has_sse42: false,
750 has_pclmulqdq: false,
751 has_avx512vl: false,
752 has_vpclmulqdq: false,
753 rust_version_supports_avx512: false,
754 };
755 assert_eq!(
756 select_performance_tier_for_test(&capabilities_no_pclmul),
757 PerformanceTier::SoftwareTable
758 );
759 }
760
761 #[test]
762 #[cfg(target_arch = "x86")]
763 fn test_x86_tier_selection() {
764 // Test that x86 (32-bit) tier selection works correctly
765
766 // Test SSE + PCLMULQDQ (only available tier for x86)
767 let capabilities_sse = ArchCapabilities {
768 has_aes: false,
769 has_crc: false,
770 has_sha3: false,
771 has_sse41: true,
772 has_sse42: false,
773 has_pclmulqdq: true,
774 has_avx512vl: false,
775 has_vpclmulqdq: false,
776 rust_version_supports_avx512: false,
777 };
778 assert_eq!(
779 select_performance_tier_for_test(&capabilities_sse),
780 PerformanceTier::X86_64SsePclmulqdq
781 );
782
783 // For x86 32-bit testing, we need a special case since AVX512VL indicates x86_64
784 // Create capabilities without AVX512VL to simulate x86 32-bit
785 let capabilities_x86_sse = ArchCapabilities {
786 has_aes: false,
787 has_crc: false,
788 has_sha3: false,
789 has_sse41: true,
790 has_sse42: false,
791 has_pclmulqdq: true,
792 has_avx512vl: false, // No AVX512 on 32-bit x86
793 has_vpclmulqdq: false,
794 rust_version_supports_avx512: false,
795 };
796 // This should select x86_64 tier since we're testing the general case
797 assert_eq!(
798 select_performance_tier_for_test(&capabilities_x86_sse),
799 PerformanceTier::X86_64SsePclmulqdq
800 );
801
802 // Test missing PCLMULQDQ (should fall back to software)
803 let capabilities_no_pclmul = ArchCapabilities {
804 has_aes: false,
805 has_crc: false,
806 has_sha3: false,
807 has_sse41: true,
808 has_sse42: false,
809 has_pclmulqdq: false,
810 has_avx512vl: false,
811 has_vpclmulqdq: false,
812 rust_version_supports_avx512: false,
813 };
814 assert_eq!(
815 select_performance_tier_for_test(&capabilities_no_pclmul),
816 PerformanceTier::SoftwareTable
817 );
818 }
819
820 #[test]
821 fn test_x86_feature_hierarchy() {
822 // Test that x86 feature hierarchy is properly maintained
823 // SSE4.1 is required for PCLMULQDQ
824 // AVX512VL requires PCLMULQDQ
825 // VPCLMULQDQ requires AVX512VL and Rust 1.89+
826
827 // Test feature dependencies are enforced
828 let capabilities_full = ArchCapabilities {
829 has_aes: false,
830 has_crc: false,
831 has_sha3: false,
832 has_sse41: true,
833 has_sse42: false,
834 has_pclmulqdq: true,
835 has_avx512vl: true,
836 has_vpclmulqdq: true,
837 rust_version_supports_avx512: true,
838 };
839
840 // All x86 features should be available when hierarchy is satisfied
841 assert!(capabilities_full.has_sse41);
842 assert!(capabilities_full.has_pclmulqdq);
843 assert!(capabilities_full.has_avx512vl);
844 assert!(capabilities_full.has_vpclmulqdq);
845 assert!(capabilities_full.rust_version_supports_avx512);
846 }
847
848 #[test]
849 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
850 fn test_rust_version_gating() {
851 // Test that VPCLMULQDQ is properly gated by Rust version
852 let rust_support = check_rust_version_supports_avx512();
853
854 // Should return a boolean based on Rust version
855 // This will be true for Rust 1.89+ and false for earlier versions
856 assert!(rust_support == true || rust_support == false);
857 }
858
859 // Mock tests for compile-time and runtime feature agreement scenarios
860 mod mock_feature_agreement_tests {
861 use super::*;
862
863 #[test]
864 fn test_rust_version_gating_scenarios() {
865 // Test VPCLMULQDQ with different Rust version scenarios
866
867 // All features available but Rust version too old
868 let capabilities_old_rust = ArchCapabilities {
869 has_aes: false,
870 has_crc: false,
871 has_sha3: false,
872 has_sse41: true,
873 has_sse42: false,
874 has_pclmulqdq: true,
875 has_avx512vl: true,
876 has_vpclmulqdq: true, // Hardware supports it
877 rust_version_supports_avx512: false, // But Rust version is too old
878 };
879
880 // Should not select VPCLMULQDQ or AVX512 tiers due to Rust version constraint
881 let tier = select_performance_tier_for_test(&capabilities_old_rust);
882 assert_ne!(tier, PerformanceTier::X86_64Avx512Vpclmulqdq);
883 assert_ne!(tier, PerformanceTier::X86_64Avx512Pclmulqdq);
884 assert_eq!(tier, PerformanceTier::X86_64SsePclmulqdq);
885 }
886
887 #[test]
888 fn test_feature_dependency_validation() {
889 // Test that feature dependencies are properly validated
890
891 // SHA3 without AES should not be possible
892 let invalid_sha3_caps = ArchCapabilities {
893 has_aes: false, // Missing required dependency
894 has_crc: false,
895 has_sha3: true, // This should be impossible in real detection
896 has_sse41: false,
897 has_sse42: false,
898 has_pclmulqdq: false,
899 has_avx512vl: false,
900 has_vpclmulqdq: false,
901 rust_version_supports_avx512: false,
902 };
903
904 // Should fall back to software since AES is required for SHA3
905 assert_eq!(
906 select_performance_tier_for_test(&invalid_sha3_caps),
907 PerformanceTier::SoftwareTable
908 );
909
910 // VPCLMULQDQ without AVX512VL should not be possible
911 let invalid_vpclmul_caps = ArchCapabilities {
912 has_aes: false,
913 has_crc: false,
914 has_sha3: false,
915 has_sse41: true,
916 has_sse42: false,
917 has_pclmulqdq: true,
918 has_avx512vl: false, // Missing required dependency
919 has_vpclmulqdq: true, // This should be impossible in real detection
920 rust_version_supports_avx512: true,
921 };
922
923 // Should fall back to SSE tier since AVX512VL is required for VPCLMULQDQ
924 assert_eq!(
925 select_performance_tier_for_test(&invalid_vpclmul_caps),
926 PerformanceTier::X86_64SsePclmulqdq
927 );
928 }
929 }
930
931 // Comprehensive tier selection tests across different hardware configurations
932 mod tier_selection_comprehensive_tests {
933 use super::*;
934
935 #[test]
936 fn test_all_aarch64_tier_combinations() {
937 // Test all possible AArch64 capability combinations
938
939 // No features - software fallback
940 let no_features = ArchCapabilities {
941 has_aes: false,
942 has_crc: false,
943 has_sha3: false,
944 has_sse41: false,
945 has_sse42: false,
946 has_pclmulqdq: false,
947 has_avx512vl: false,
948 has_vpclmulqdq: false,
949 rust_version_supports_avx512: false,
950 };
951 assert_eq!(
952 select_performance_tier_for_test(&no_features),
953 PerformanceTier::SoftwareTable
954 );
955
956 // AES only - baseline AArch64 tier
957 let aes_only = ArchCapabilities {
958 has_aes: true,
959 has_crc: false,
960 has_sha3: false,
961 has_sse41: false,
962 has_sse42: false,
963 has_pclmulqdq: false,
964 has_avx512vl: false,
965 has_vpclmulqdq: false,
966 rust_version_supports_avx512: false,
967 };
968 assert_eq!(
969 select_performance_tier_for_test(&aes_only),
970 PerformanceTier::AArch64Aes
971 );
972
973 // AES + SHA3 - highest AArch64 tier
974 let aes_sha3 = ArchCapabilities {
975 has_aes: true,
976 has_crc: false,
977 has_sha3: true,
978 has_sse41: false,
979 has_sse42: false,
980 has_pclmulqdq: false,
981 has_avx512vl: false,
982 has_vpclmulqdq: false,
983 rust_version_supports_avx512: false,
984 };
985 assert_eq!(
986 select_performance_tier_for_test(&aes_sha3),
987 PerformanceTier::AArch64AesSha3
988 );
989 }
990
991 #[test]
992 fn test_all_x86_64_tier_combinations() {
993 // Test all possible x86_64 capability combinations
994
995 // No features - software fallback
996 let no_features = ArchCapabilities {
997 has_aes: false,
998 has_crc: false,
999 has_sha3: false,
1000 has_sse41: false,
1001 has_sse42: false,
1002 has_pclmulqdq: false,
1003 has_avx512vl: false,
1004 has_vpclmulqdq: false,
1005 rust_version_supports_avx512: false,
1006 };
1007 assert_eq!(
1008 select_performance_tier_for_test(&no_features),
1009 PerformanceTier::SoftwareTable
1010 );
1011
1012 // SSE4.1 only - software fallback (PCLMULQDQ required)
1013 let sse_only = ArchCapabilities {
1014 has_aes: false,
1015 has_crc: false,
1016 has_sha3: false,
1017 has_sse41: true,
1018 has_sse42: false,
1019 has_pclmulqdq: false,
1020 has_avx512vl: false,
1021 has_vpclmulqdq: false,
1022 rust_version_supports_avx512: false,
1023 };
1024 assert_eq!(
1025 select_performance_tier_for_test(&sse_only),
1026 PerformanceTier::SoftwareTable
1027 );
1028
1029 // SSE4.1 + PCLMULQDQ - baseline x86_64 tier
1030 let sse_pclmul = ArchCapabilities {
1031 has_aes: false,
1032 has_crc: false,
1033 has_sha3: false,
1034 has_sse41: true,
1035 has_sse42: false,
1036 has_pclmulqdq: true,
1037 has_avx512vl: false,
1038 has_vpclmulqdq: false,
1039 rust_version_supports_avx512: false,
1040 };
1041 assert_eq!(
1042 select_performance_tier_for_test(&sse_pclmul),
1043 PerformanceTier::X86_64SsePclmulqdq
1044 );
1045
1046 // SSE4.1 + PCLMULQDQ + AVX512VL but old Rust - should fall back to SSE tier
1047 let avx512_pclmul_old_rust = ArchCapabilities {
1048 has_aes: false,
1049 has_crc: false,
1050 has_sha3: false,
1051 has_sse41: true,
1052 has_sse42: false,
1053 has_pclmulqdq: true,
1054 has_avx512vl: true,
1055 has_vpclmulqdq: false,
1056 rust_version_supports_avx512: false, // Old Rust version
1057 };
1058 assert_eq!(
1059 select_performance_tier_for_test(&avx512_pclmul_old_rust),
1060 PerformanceTier::X86_64SsePclmulqdq
1061 );
1062
1063 // SSE4.1 + PCLMULQDQ + AVX512VL with new Rust - mid-tier
1064 let avx512_pclmul_new_rust = ArchCapabilities {
1065 has_aes: false,
1066 has_crc: false,
1067 has_sha3: false,
1068 has_sse41: true,
1069 has_sse42: false,
1070 has_pclmulqdq: true,
1071 has_avx512vl: true,
1072 has_vpclmulqdq: false,
1073 rust_version_supports_avx512: true, // New Rust version
1074 };
1075 assert_eq!(
1076 select_performance_tier_for_test(&avx512_pclmul_new_rust),
1077 PerformanceTier::X86_64Avx512Pclmulqdq
1078 );
1079
1080 // All features + old Rust - should fall back to SSE tier
1081 let all_features_old_rust = ArchCapabilities {
1082 has_aes: false,
1083 has_crc: false,
1084 has_sha3: false,
1085 has_sse41: true,
1086 has_sse42: false,
1087 has_pclmulqdq: true,
1088 has_avx512vl: true,
1089 has_vpclmulqdq: true,
1090 rust_version_supports_avx512: false, // Old Rust version
1091 };
1092 assert_eq!(
1093 select_performance_tier_for_test(&all_features_old_rust),
1094 PerformanceTier::X86_64SsePclmulqdq
1095 );
1096
1097 // All features + new Rust - highest tier
1098 let all_features_new_rust = ArchCapabilities {
1099 has_aes: false,
1100 has_crc: false,
1101 has_sha3: false,
1102 has_sse41: true,
1103 has_sse42: false,
1104 has_pclmulqdq: true,
1105 has_avx512vl: true,
1106 has_vpclmulqdq: true,
1107 rust_version_supports_avx512: true, // New Rust version
1108 };
1109 assert_eq!(
1110 select_performance_tier_for_test(&all_features_new_rust),
1111 PerformanceTier::X86_64Avx512Vpclmulqdq
1112 );
1113 }
1114
1115 #[test]
1116 fn test_x86_32bit_tier_combinations() {
1117 // Test x86 (32-bit) capability combinations
1118
1119 // No features - software fallback
1120 let no_features = ArchCapabilities {
1121 has_aes: false,
1122 has_crc: false,
1123 has_sha3: false,
1124 has_sse41: false,
1125 has_sse42: false,
1126 has_pclmulqdq: false,
1127 has_avx512vl: false,
1128 has_vpclmulqdq: false,
1129 rust_version_supports_avx512: false,
1130 };
1131 assert_eq!(
1132 select_performance_tier_for_test(&no_features),
1133 PerformanceTier::SoftwareTable
1134 );
1135
1136 // SSE4.1 + PCLMULQDQ - for x86 32-bit testing, we expect x86_64 tier in our test function
1137 // since the test function doesn't distinguish between x86 and x86_64 architectures
1138 let sse_pclmul = ArchCapabilities {
1139 has_aes: false,
1140 has_crc: false,
1141 has_sha3: false,
1142 has_sse41: true,
1143 has_sse42: false,
1144 has_pclmulqdq: true,
1145 has_avx512vl: false, // AVX512 not available on 32-bit x86
1146 has_vpclmulqdq: false,
1147 rust_version_supports_avx512: false,
1148 };
1149 // The test function will return x86_64 tier since it doesn't distinguish architectures
1150 assert_eq!(
1151 select_performance_tier_for_test(&sse_pclmul),
1152 PerformanceTier::X86_64SsePclmulqdq
1153 );
1154 }
1155
1156 #[test]
1157 fn test_target_string_consistency() {
1158 // Test that target strings are consistent with selected tiers
1159
1160 let test_cases = [
1161 (PerformanceTier::AArch64AesSha3, "aarch64-neon-pmull-sha3"),
1162 (PerformanceTier::AArch64Aes, "aarch64-neon-pmull"),
1163 (
1164 PerformanceTier::X86_64Avx512Vpclmulqdq,
1165 "x86_64-avx512-vpclmulqdq",
1166 ),
1167 (
1168 PerformanceTier::X86_64Avx512Pclmulqdq,
1169 "x86_64-avx512-pclmulqdq",
1170 ),
1171 (PerformanceTier::X86_64SsePclmulqdq, "x86_64-sse-pclmulqdq"),
1172 (PerformanceTier::X86SsePclmulqdq, "x86-sse-pclmulqdq"),
1173 (PerformanceTier::SoftwareTable, "software-fallback-tables"),
1174 ];
1175
1176 for (tier, expected_string) in test_cases {
1177 assert_eq!(tier_to_target_string(tier), expected_string);
1178 }
1179 }
1180 }
1181
1182 // Tests for graceful degradation between performance tiers
1183 mod graceful_degradation_tests {
1184 use super::*;
1185
1186 #[test]
1187 fn test_aarch64_degradation_path() {
1188 // Test the degradation path for AArch64: SHA3+AES -> AES -> Software
1189
1190 // Start with highest tier capabilities
1191 let mut capabilities = ArchCapabilities {
1192 has_aes: true,
1193 has_crc: false,
1194 has_sha3: true,
1195 has_sse41: false,
1196 has_sse42: false,
1197 has_pclmulqdq: false,
1198 has_avx512vl: false,
1199 has_vpclmulqdq: false,
1200 rust_version_supports_avx512: false,
1201 };
1202
1203 // Should select highest tier
1204 assert_eq!(
1205 select_performance_tier_for_test(&capabilities),
1206 PerformanceTier::AArch64AesSha3
1207 );
1208
1209 // Remove SHA3 - should degrade to AES tier
1210 capabilities.has_sha3 = false;
1211 assert_eq!(
1212 select_performance_tier_for_test(&capabilities),
1213 PerformanceTier::AArch64Aes
1214 );
1215
1216 // Remove AES - should degrade to software
1217 capabilities.has_aes = false;
1218 assert_eq!(
1219 select_performance_tier_for_test(&capabilities),
1220 PerformanceTier::SoftwareTable
1221 );
1222 }
1223
1224 #[test]
1225 fn test_x86_64_degradation_path() {
1226 // Test the degradation path for x86_64: VPCLMULQDQ -> AVX512 -> SSE -> Software
1227
1228 // Start with highest tier capabilities
1229 let mut capabilities = ArchCapabilities {
1230 has_aes: false,
1231 has_crc: false,
1232 has_sha3: false,
1233 has_sse41: true,
1234 has_sse42: false,
1235 has_pclmulqdq: true,
1236 has_avx512vl: true,
1237 has_vpclmulqdq: true,
1238 rust_version_supports_avx512: true,
1239 };
1240
1241 // Should select highest tier
1242 assert_eq!(
1243 select_performance_tier_for_test(&capabilities),
1244 PerformanceTier::X86_64Avx512Vpclmulqdq
1245 );
1246
1247 // Remove VPCLMULQDQ - should degrade to AVX512 tier
1248 capabilities.has_vpclmulqdq = false;
1249 assert_eq!(
1250 select_performance_tier_for_test(&capabilities),
1251 PerformanceTier::X86_64Avx512Pclmulqdq
1252 );
1253
1254 // Remove AVX512VL - should degrade to SSE tier
1255 capabilities.has_avx512vl = false;
1256 assert_eq!(
1257 select_performance_tier_for_test(&capabilities),
1258 PerformanceTier::X86_64SsePclmulqdq
1259 );
1260
1261 // Remove PCLMULQDQ - should degrade to software
1262 capabilities.has_pclmulqdq = false;
1263 assert_eq!(
1264 select_performance_tier_for_test(&capabilities),
1265 PerformanceTier::SoftwareTable
1266 );
1267
1268 // Remove SSE4.1 - should still be software (already at lowest tier)
1269 capabilities.has_sse41 = false;
1270 assert_eq!(
1271 select_performance_tier_for_test(&capabilities),
1272 PerformanceTier::SoftwareTable
1273 );
1274 }
1275
1276 #[test]
1277 fn test_rust_version_degradation() {
1278 // Test degradation when Rust version doesn't support VPCLMULQDQ
1279
1280 let capabilities_with_vpclmulqdq = ArchCapabilities {
1281 has_aes: false,
1282 has_crc: false,
1283 has_sha3: false,
1284 has_sse41: true,
1285 has_sse42: false,
1286 has_pclmulqdq: true,
1287 has_avx512vl: true,
1288 has_vpclmulqdq: true,
1289 rust_version_supports_avx512: false, // Old Rust version
1290 };
1291
1292 // Should degrade from VPCLMULQDQ tier to SSE tier due to Rust version
1293 assert_eq!(
1294 select_performance_tier_for_test(&capabilities_with_vpclmulqdq),
1295 PerformanceTier::X86_64SsePclmulqdq
1296 );
1297 }
1298
1299 #[test]
1300 fn test_partial_feature_availability() {
1301 // Test scenarios where only some features in a tier are available
1302
1303 // AArch64: SHA3 available but AES not (impossible in real hardware, but test safety)
1304 let aarch64_partial = ArchCapabilities {
1305 has_aes: false,
1306 has_crc: false,
1307 has_sha3: true, // This would be impossible in real detection
1308 has_sse41: false,
1309 has_sse42: false,
1310 has_pclmulqdq: false,
1311 has_avx512vl: false,
1312 has_vpclmulqdq: false,
1313 rust_version_supports_avx512: false,
1314 };
1315 // Should fall back to software since AES is required for SHA3
1316 assert_eq!(
1317 select_performance_tier_for_test(&aarch64_partial),
1318 PerformanceTier::SoftwareTable
1319 );
1320
1321 // x86_64: VPCLMULQDQ available but AVX512VL not (impossible in real hardware)
1322 let x86_64_partial = ArchCapabilities {
1323 has_aes: false,
1324 has_crc: false,
1325 has_sha3: false,
1326 has_sse41: true,
1327 has_sse42: false,
1328 has_pclmulqdq: true,
1329 has_avx512vl: false,
1330 has_vpclmulqdq: true, // This would be impossible in real detection
1331 rust_version_supports_avx512: true,
1332 };
1333 // Should fall back to SSE tier since AVX512VL is required for VPCLMULQDQ
1334 assert_eq!(
1335 select_performance_tier_for_test(&x86_64_partial),
1336 PerformanceTier::X86_64SsePclmulqdq
1337 );
1338 }
1339 }
1340 }
1341
1342 #[cfg(test)]
1343 mod software_fallback_tests {
1344 use super::*;
1345 #[test]
1346 fn test_aarch64_without_aes_falls_back_to_software() {
1347 // Test that AArch64 without AES support falls back to software implementation
1348 let capabilities_no_aes = ArchCapabilities {
1349 has_aes: false, // No AES support
1350 has_crc: false,
1351 has_sha3: false, // SHA3 requires AES, so also false
1352 has_sse41: false,
1353 has_sse42: false,
1354 has_pclmulqdq: false,
1355 has_avx512vl: false,
1356 has_vpclmulqdq: false,
1357 rust_version_supports_avx512: false,
1358 };
1359
1360 let tier = select_performance_tier_for_test(&capabilities_no_aes);
1361 assert_eq!(
1362 tier,
1363 PerformanceTier::SoftwareTable,
1364 "AArch64 without AES should fall back to software implementation"
1365 );
1366 }
1367
1368 #[test]
1369 fn test_x86_without_pclmulqdq_falls_back_to_software() {
1370 // Test that x86 without SSE4.1/PCLMULQDQ falls back to software implementation
1371 let capabilities_no_pclmul = ArchCapabilities {
1372 has_aes: false,
1373 has_crc: false,
1374 has_sha3: false,
1375 has_sse41: true, // SSE4.1 available
1376 has_sse42: false,
1377 has_pclmulqdq: false, // But PCLMULQDQ not available
1378 has_avx512vl: false,
1379 has_vpclmulqdq: false,
1380 rust_version_supports_avx512: false,
1381 };
1382
1383 let tier = select_performance_tier_for_test(&capabilities_no_pclmul);
1384 assert_eq!(
1385 tier,
1386 PerformanceTier::SoftwareTable,
1387 "x86 without PCLMULQDQ should fall back to software implementation"
1388 );
1389
1390 // Test x86 without SSE4.1
1391 let capabilities_no_sse = ArchCapabilities {
1392 has_aes: false,
1393 has_crc: false,
1394 has_sha3: false,
1395 has_sse41: false, // No SSE4.1 support
1396 has_sse42: false,
1397 has_pclmulqdq: false, // PCLMULQDQ requires SSE4.1
1398 has_avx512vl: false,
1399 has_vpclmulqdq: false,
1400 rust_version_supports_avx512: false,
1401 };
1402
1403 let tier = select_performance_tier_for_test(&capabilities_no_sse);
1404 assert_eq!(
1405 tier,
1406 PerformanceTier::SoftwareTable,
1407 "x86 without SSE4.1 should fall back to software implementation"
1408 );
1409 }
1410
1411 #[test]
1412 fn test_conditional_compilation_coverage() {
1413 // Test that software fallback is properly conditionally compiled
1414 // This test ensures the conditional compilation logic is working correctly
1415
1416 // Software fallback should be available when needed
1417 #[cfg(any(
1418 not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")),
1419 target_arch = "x86",
1420 all(target_arch = "aarch64", not(target_feature = "aes"))
1421 ))]
1422 {
1423 // Software fallback should be compiled in these cases
1424 use crate::arch::software;
1425 let _test_fn = software::update;
1426 // This test passes if it compiles successfully
1427 }
1428
1429 // For x86_64, software fallback should not be needed since SSE4.1/PCLMULQDQ are always available
1430 // But it may still be compiled for testing purposes
1431 }
1432 }