Skip to main content

rill_core/math/vector/simd/
mod.rs

1//! # SIMD implementations for vector operations
2//!
3//! This module contains platform-dependent SIMD implementations of vector operations.
4//!
5//! ## CPU feature detection
6//! The system automatically determines available SIMD instructions at runtime
7//! and selects the optimal implementation.
8//!
9//! ## Supported architectures
10//! - x86/x86_64: SSE2, SSE4.1, AVX, AVX2, AVX512
11//! - ARM: NEON (AArch64)
12//! - WebAssembly: SIMD128
13//!
14//! ## Usage
15//! Users typically don't interact with this module directly,
16//! but use the high-level abstractions from `vector::traits`.
17
18#![allow(unused_imports)]
19#![allow(dead_code)]
20
21// Cross-platform SIMD implementation via the wide crate (requires simd feature)
22#[cfg(feature = "simd")]
23pub mod wide;
24
25#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
26pub mod x86;
27
28#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
29pub mod arm;
30
31#[cfg(target_arch = "wasm32")]
32pub mod wasm;
33
34/// SIMD capability detector for the CPU
35pub struct SimdDetector {
36    has_sse2: bool,
37    has_sse4_1: bool,
38    has_avx: bool,
39    has_avx2: bool,
40    has_avx512: bool,
41    has_neon: bool,
42    has_wasm_simd128: bool,
43}
44
45impl Default for SimdDetector {
46    fn default() -> Self {
47        Self::new()
48    }
49}
50
51impl SimdDetector {
52    /// Creates a detector and determines the current CPU's capabilities.
53    ///
54    /// Uses `std::arch::is_x86_feature_detected!` on x86/x86_64
55    /// and `std::arch::is_aarch64_feature_detected!` on AArch64.
56    /// On other targets, all features are reported as unavailable.
57    pub fn new() -> Self {
58        Self {
59            #[cfg(target_arch = "x86_64")]
60            has_sse2: std::arch::is_x86_feature_detected!("sse2"),
61            #[cfg(target_arch = "x86")]
62            has_sse2: std::arch::is_x86_feature_detected!("sse2"),
63            #[cfg(target_arch = "x86_64")]
64            has_sse4_1: std::arch::is_x86_feature_detected!("sse4.1"),
65            #[cfg(target_arch = "x86")]
66            has_sse4_1: std::arch::is_x86_feature_detected!("sse4.1"),
67            #[cfg(target_arch = "x86_64")]
68            has_avx: std::arch::is_x86_feature_detected!("avx"),
69            #[cfg(target_arch = "x86")]
70            has_avx: std::arch::is_x86_feature_detected!("avx"),
71            #[cfg(target_arch = "x86_64")]
72            has_avx2: std::arch::is_x86_feature_detected!("avx2"),
73            #[cfg(target_arch = "x86")]
74            has_avx2: std::arch::is_x86_feature_detected!("avx2"),
75            #[cfg(target_arch = "x86_64")]
76            has_avx512: std::arch::is_x86_feature_detected!("avx512f"),
77            #[cfg(target_arch = "x86")]
78            has_avx512: std::arch::is_x86_feature_detected!("avx512f"),
79            #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
80            has_sse2: false,
81            #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
82            has_sse4_1: false,
83            #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
84            has_avx: false,
85            #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
86            has_avx2: false,
87            #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
88            has_avx512: false,
89            #[cfg(target_arch = "aarch64")]
90            has_neon: std::arch::is_aarch64_feature_detected!("neon"),
91            #[cfg(not(target_arch = "aarch64"))]
92            has_neon: false,
93            #[cfg(target_arch = "wasm32")]
94            has_wasm_simd128: true,
95            #[cfg(not(target_arch = "wasm32"))]
96            has_wasm_simd128: false,
97        }
98    }
99
100    /// Returns the maximum recommended SIMD width for the current platform.
101    ///
102    /// On x86_64: 8 if AVX available, 4 if SSE2 available.
103    /// On AArch64: 4 if NEON available.
104    /// On wasm32: 4.
105    /// Fallback: 1 (scalar).
106    pub fn recommended_simd_width<T: crate::Transcendental>() -> usize {
107        let det = Self::new();
108        if det.has_avx {
109            return 8;
110        }
111        if det.has_sse2 || det.has_neon || det.has_wasm_simd128 {
112            return 4;
113        }
114        1
115    }
116}
117
118// Re-exports
119#[cfg(feature = "simd")]
120pub use wide::*;
121
122// Platform-specific re-exports (not yet implemented)
123// #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
124// pub use x86::*;
125
126// #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
127// pub use arm::*;
128
129// #[cfg(target_arch = "wasm32")]
130// pub use wasm::*;