simply_simd/dispatch.rs
1use std::sync::LazyLock;
2
3#[cfg(target_arch = "aarch64")]
4pub use crate::architectures::arch::Neon;
5pub use crate::architectures::arch::Scalar128;
6#[cfg(target_arch = "x86_64")]
7pub use crate::architectures::arch::{Avx2, Avx512, Sse};
8
9pub static DETECTED_ARCH: LazyLock<Architecture> = LazyLock::new(detect_architecture);
10
11pub enum Architecture {
12 #[cfg(target_arch = "x86_64")]
13 Sse,
14 #[cfg(target_arch = "x86_64")]
15 Avx2,
16 #[cfg(target_arch = "x86_64")]
17 Avx512,
18 #[cfg(target_arch = "aarch64")]
19 Neon,
20 Scalar128,
21}
22
23pub fn detect_architecture() -> Architecture {
24 #[cfg(target_arch = "x86_64")]
25 {
26 if is_x86_feature_detected!("fma") {
27 if is_x86_feature_detected!("avx512f") {
28 return Architecture::Avx512;
29 } else if is_x86_feature_detected!("avx2") {
30 return Architecture::Avx2;
31 }
32 }
33
34 if is_x86_feature_detected!("sse4.2") {
35 return Architecture::Sse;
36 }
37 }
38
39 #[cfg(target_arch = "aarch64")]
40 {
41 use std::arch::is_aarch64_feature_detected;
42
43 if is_aarch64_feature_detected!("neon") {
44 return Architecture::Neon;
45 }
46 }
47
48 Architecture::Scalar128
49}
50//
51// #[macro_export]
52// macro_rules! dispatch {
53// ($func:ident($($args:expr),*$(,)?)) => {
54// match *$crate::DETECTED_ARCH {
55// #[cfg(target_arch = "x86_64")]
56// $crate::Architecture::Sse => $func::<$crate::Sse>($($args),*),
57// #[cfg(target_arch = "x86_64")]
58// $crate::Architecture::Avx2 => $func::<$crate::Avx2>($($args),*),
59// #[cfg(target_arch = "x86_64")]
60// $crate::Architecture::Avx512 => $func::<$crate::Avx512>($($args),*),
61// #[cfg(target_arch = "aarch64")]
62// $crate::Architecture::Neon => $func::<$crate::Neon>($($args),*),
63// $crate::Architecture::Scalar => $func::<$crate::Scalar128>($($args),*)
64// }
65// };
66//
67// ($func:ident::<$($generics:ident),+$(,)?>($($args:expr),*$(,)?)) => {
68// match *$crate::DETECTED_ARCH {
69// #[cfg(target_arch = "x86_64")]
70// $crate::Architecture::Sse => $func::<$crate::Sse, $($generics),+>($($args),*),
71// #[cfg(target_arch = "x86_64")]
72// $crate::Architecture::Avx2 => $func::<$crate::Avx2, $($generics),+>($($args),*),
73// #[cfg(target_arch = "x86_64")]
74// $crate::Architecture::Avx512 => $func::<$crate::Avx512, $($generics),+>($($args),*),
75// #[cfg(target_arch = "aarch64")]
76// $crate::Architecture::Neon => $func::<$crate::Neon, $($generics),+>($($args),*),
77// $crate::Architecture::Scalar => $func::<$crate::Scalar128, $($generics),+>($($args),*)
78// }
79// };
80//
81// (Self::$func:ident($($args:expr),*$(,)?)) => {
82// match *$crate::DETECTED_ARCH {
83// #[cfg(target_arch = "x86_64")]
84// $crate::Architecture::Sse => Self::$func::<$crate::Sse>($($args),*),
85// #[cfg(target_arch = "x86_64")]
86// $crate::Architecture::Avx2 => Self::$func::<$crate::Avx2>($($args),*),
87// #[cfg(target_arch = "x86_64")]
88// $crate::Architecture::Avx512 => Self::$func::<$crate::Avx512>($($args),*),
89// #[cfg(target_arch = "aarch64")]
90// $crate::Architecture::Neon => Self::$func::<$crate::Neon>($($args),*),
91// $crate::Architecture::Scalar => Self::$func::<$crate::Scalar128>($($args),*)
92// }
93// };
94//
95// (Self::$func:ident::<$($generics:ident),+$(,)?>($($args:expr),*$(,)?)) => {
96// match *$crate::DETECTED_ARCH {
97// #[cfg(target_arch = "x86_64")]
98// $crate::Architecture::Sse => Self::$func::<$crate::Sse, $($generics),+>($($args),*),
99// #[cfg(target_arch = "x86_64")]
100// $crate::Architecture::Avx2 => Self::$func::<$crate::Avx2, $($generics),+>($($args),*),
101// #[cfg(target_arch = "x86_64")]
102// $crate::Architecture::Avx512 => Self::$func::<$crate::Avx512, $($generics),+>($($args),*),
103// #[cfg(target_arch = "aarch64")]
104// $crate::Architecture::Neon => Self::$func::<$crate::Neon, $($generics),+>($($args),*),
105// $crate::Architecture::Scalar => Self::$func::<$crate::Scalar128, $($generics),+>($($args),*)
106// }
107// };
108// }
109// pub use dispatch;
110
111// #[macro_export]
112// macro_rules! dispatch_async {
113// ($func:ident($($args:expr),*$(,)?)) => {
114// match *$crate::DETECTED_ARCH {
115// #[cfg(target_arch = "x86_64")]
116// $crate::Architecture::Sse => $func::<$crate::Sse>($($args),*).await,
117// #[cfg(target_arch = "x86_64")]
118// $crate::Architecture::Avx2 => $func::<$crate::Avx2>($($args),*).await,
119// #[cfg(target_arch = "x86_64")]
120// $crate::Architecture::Avx512 => $func::<$crate::Avx512>($($args),*).await,
121// #[cfg(target_arch = "aarch64")]
122// $crate::Architecture::Neon => $func::<$crate::Neon>($($args),*).await,
123// $crate::Architecture::Scalar => $func::<$crate::Scalar128>($($args),*).await
124// }
125// };
126//
127// ($func:ident::<$($generics:ident),+$(,)?>($($args:expr),*$(,)?)) => {
128// match *$crate::DETECTED_ARCH {
129// #[cfg(target_arch = "x86_64")]
130// $crate::Architecture::Sse => $func::<$crate::Sse, $($generics),+>($($args),*).await,
131// #[cfg(target_arch = "x86_64")]
132// $crate::Architecture::Avx2 => $func::<$crate::Avx2, $($generics),+>($($args),*).await,
133// #[cfg(target_arch = "x86_64")]
134// $crate::Architecture::Avx512 => $func::<$crate::Avx512, $($generics),+>($($args),*).await,
135// #[cfg(target_arch = "aarch64")]
136// $crate::Architecture::Neon => $func::<$crate::Neon, $($generics),+>($($args),*).await,
137// $crate::Architecture::Scalar => $func::<$crate::Scalar128, $($generics),+>($($args),*).await
138// }
139// };
140//
141// (Self::$func:ident($($args:expr),*$(,)?)) => {
142// match *$crate::DETECTED_ARCH {
143// #[cfg(target_arch = "x86_64")]
144// $crate::Architecture::Sse => Self::$func::<$crate::Sse>($($args),*).await,
145// #[cfg(target_arch = "x86_64")]
146// $crate::Architecture::Avx2 => Self::$func::<$crate::Avx2>($($args),*).await,
147// #[cfg(target_arch = "x86_64")]
148// $crate::Architecture::Avx512 => Self::$func::<$crate::Avx512>($($args),*).await,
149// #[cfg(target_arch = "aarch64")]
150// $crate::Architecture::Neon => Self::$func::<$crate::Neon>($($args),*).await,
151// $crate::Architecture::Scalar => Self::$func::<$crate::Scalar128>($($args),*).await
152// }
153// };
154//
155// (Self::$func:ident::<$($generics:ident),+$(,)?>($($args:expr),*$(,)?)) => {
156// match *$crate::DETECTED_ARCH {
157// #[cfg(target_arch = "x86_64")]
158// $crate::Architecture::Sse => Self::$func::<$crate::Sse, $($generics),+>($($args),*).await,
159// #[cfg(target_arch = "x86_64")]
160// $crate::Architecture::Avx2 => Self::$func::<$crate::Avx2, $($generics),+>($($args),*).await,
161// #[cfg(target_arch = "x86_64")]
162// $crate::Architecture::Avx512 => Self::$func::<$crate::Avx512, $($generics),+>($($args),*).await,
163// #[cfg(target_arch = "aarch64")]
164// $crate::Architecture::Neon => Self::$func::<$crate::Neon, $($generics),+>($($args),*).await,
165// $crate::Architecture::Scalar => Self::$func::<$crate::Scalar128, $($generics),+>($($args),*).await
166// }
167// };
168// }
169// pub use dispatch_async;
170
171// #[macro_export]
172// macro_rules! dispatch_fn {
173// ($($prefix:tt )* $vis:vis fn $name:ident($($arg:ident : $ty:ty),*) $(-> $ret:ty)? { $($body:tt)* }) => {
174// $($prefix )* $vis:vis fn $name($($arg: $ty),*) $(-> $ret)? {
175// $($prefix )* fn internal<A: $crate::architectures::interface::Arch>($($arg: $ty),*) $(-> $ret)? {
176// $($body)*
177// }
178// dispatch!(detect_architecture(), internal($($arg),*))
179// }
180// };
181// }
182// pub use dispatch_fn;
183
184// #[dispatch_arch(A)]
185// pub fn simd_work(arg1, arg2) {
186// simd_function::<A>(arg1, arg2);
187// }
188//
189// ->
190//
191// pub fn simd_work() {
192// {
193// fn simd_work_internal<A: Arch>() {
194// simd_function::<A>();
195// }
196//
197// dispatch!(simd_work_internal());
198// }
199// }