33//! Feature detection system for safe and efficient hardware acceleration across different
44//! platforms.
55
6- #[ cfg( not( feature = "std" ) ) ]
6+ #[ cfg( all(
7+ not( feature = "std" ) ,
8+ any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" )
9+ ) ) ]
710use spin:: Once ;
8- #[ cfg( feature = "std" ) ]
11+ #[ cfg( all(
12+ feature = "std" ,
13+ any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" )
14+ ) ) ]
915use std:: sync:: OnceLock ;
1016
1117#[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
1218extern crate alloc;
13- #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
19+ #[ cfg( all(
20+ feature = "alloc" ,
21+ not( feature = "std" ) ,
22+ any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" )
23+ ) ) ]
1424use alloc:: string:: { String , ToString } ;
15- #[ cfg( feature = "std" ) ]
25+ #[ cfg( all(
26+ feature = "std" ,
27+ any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" )
28+ ) ) ]
1629use std:: string:: { String , ToString } ;
1730
1831/// Global ArchOps instance cache - initialized once based on feature detection results
19- #[ cfg( feature = "std" ) ]
32+ #[ cfg( all(
33+ feature = "std" ,
34+ any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" )
35+ ) ) ]
2036static ARCH_OPS_INSTANCE : OnceLock < ArchOpsInstance > = OnceLock :: new ( ) ;
21- #[ cfg( not( feature = "std" ) ) ]
37+ #[ cfg( all(
38+ not( feature = "std" ) ,
39+ any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" )
40+ ) ) ]
2241static ARCH_OPS_INSTANCE : Once < ArchOpsInstance > = Once :: new ( ) ;
2342
2443/// Performance tiers representing different hardware capability levels
@@ -61,7 +80,10 @@ pub struct ArchCapabilities {
6180
6281/// Helper function to convert a performance tier to a human-readable target string
6382/// Format: {architecture}-{intrinsics-family}-{intrinsics-features}
64- #[ cfg( feature = "alloc" ) ]
83+ #[ cfg( all(
84+ feature = "alloc" ,
85+ any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" )
86+ ) ) ]
6587#[ inline( always) ]
6688fn tier_to_target_string ( tier : PerformanceTier ) -> String {
6789 match tier {
@@ -79,6 +101,7 @@ fn tier_to_target_string(tier: PerformanceTier) -> String {
79101///
80102/// # Safety
81103/// Uses runtime feature detection which may access CPU-specific registers
104+ #[ cfg( any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" ) ) ]
82105unsafe fn detect_arch_capabilities ( ) -> ArchCapabilities {
83106 #[ cfg( target_arch = "aarch64" ) ]
84107 {
@@ -258,6 +281,7 @@ pub(crate) fn select_performance_tier(capabilities: &ArchCapabilities) -> Perfor
258281
259282/// Enum that holds the different ArchOps implementations for compile-time dispatch
260283/// This avoids the need for trait objects while still providing factory-based selection
284+ #[ cfg( any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" ) ) ]
261285#[ rustversion:: since( 1.89 ) ]
262286#[ derive( Debug , Clone , Copy ) ]
263287pub enum ArchOpsInstance {
@@ -275,6 +299,7 @@ pub enum ArchOpsInstance {
275299 SoftwareFallback ,
276300}
277301
302+ #[ cfg( any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" ) ) ]
278303#[ rustversion:: before( 1.89 ) ]
279304#[ derive( Debug , Clone , Copy ) ]
280305pub enum ArchOpsInstance {
@@ -288,6 +313,7 @@ pub enum ArchOpsInstance {
288313 SoftwareFallback ,
289314}
290315
316+ #[ cfg( any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" ) ) ]
291317impl ArchOpsInstance {
292318 #[ inline( always) ]
293319 #[ rustversion:: since( 1.89 ) ]
@@ -334,12 +360,18 @@ impl ArchOpsInstance {
334360/// This function provides access to the cached ArchOps instance that was selected based on
335361/// feature detection results at library initialization time, eliminating runtime feature
336362/// detection overhead from hot paths.
337- #[ cfg( feature = "std" ) ]
363+ #[ cfg( all(
364+ feature = "std" ,
365+ any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" )
366+ ) ) ]
338367pub fn get_arch_ops ( ) -> & ' static ArchOpsInstance {
339368 ARCH_OPS_INSTANCE . get_or_init ( create_arch_ops)
340369}
341370
342- #[ cfg( not( feature = "std" ) ) ]
371+ #[ cfg( all(
372+ not( feature = "std" ) ,
373+ any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" )
374+ ) ) ]
343375pub fn get_arch_ops ( ) -> & ' static ArchOpsInstance {
344376 ARCH_OPS_INSTANCE . call_once ( create_arch_ops)
345377}
@@ -349,6 +381,7 @@ pub fn get_arch_ops() -> &'static ArchOpsInstance {
349381/// This function uses the cached feature detection results to select the optimal
350382/// architecture-specific implementation at library initialization time, eliminating
351383/// runtime feature detection overhead from hot paths.
384+ #[ cfg( any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" ) ) ]
352385fn create_arch_ops ( ) -> ArchOpsInstance {
353386 let capabilities = unsafe { detect_arch_capabilities ( ) } ;
354387 let tier = select_performance_tier ( & capabilities) ;
@@ -358,6 +391,7 @@ fn create_arch_ops() -> ArchOpsInstance {
358391
359392/// Helper function to create ArchOpsInstance from a performance tier for Rust 1.89+ (when AVX512
360393/// stabilized)
394+ #[ cfg( any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" ) ) ]
361395#[ rustversion:: since( 1.89 ) ]
362396fn create_arch_ops_from_tier ( tier : PerformanceTier ) -> ArchOpsInstance {
363397 match tier {
@@ -400,6 +434,7 @@ fn create_arch_ops_from_tier(tier: PerformanceTier) -> ArchOpsInstance {
400434
401435/// Helper function to create ArchOpsInstance from a performance tier for Rust <1.89 (before AVX512
402436/// stabilized)
437+ #[ cfg( any( target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" ) ) ]
403438#[ rustversion:: before( 1.89 ) ]
404439fn create_arch_ops_from_tier ( tier : PerformanceTier ) -> ArchOpsInstance {
405440 match tier {
0 commit comments