#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SimdBackend {
Fallback,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Ssse3,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Sse4_2,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Avx2,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Avx512,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Avx512Vbmi,
#[cfg(target_arch = "aarch64")]
Neon,
#[cfg(target_arch = "wasm32")]
WasmSimd128,
}
impl SimdBackend {
#[inline]
pub fn new() -> Self {
let backends = [
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx512Vbmi,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx512,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx2,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Sse4_2,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Ssse3,
#[cfg(target_arch = "aarch64")]
Self::Neon,
#[cfg(target_arch = "wasm32")]
Self::WasmSimd128,
];
backends
.into_iter()
.find(Self::is_available)
.unwrap_or(Self::Fallback)
}
pub fn backends() -> impl Iterator<Item = Self> {
[
Self::Fallback,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Ssse3,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Sse4_2,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx2,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx512,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx512Vbmi,
#[cfg(target_arch = "aarch64")]
Self::Neon,
#[cfg(target_arch = "wasm32")]
Self::WasmSimd128,
]
.into_iter()
}
#[inline]
pub fn is_available(&self) -> bool {
#[cfg(not(feature = "std"))]
{
match self {
Self::Fallback => true,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Ssse3 => cfg!(target_feature = "ssse3"),
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Sse4_2 => cfg!(all(target_feature = "sse4.2", target_feature = "popcnt")),
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx2 => cfg!(target_feature = "avx2"),
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx512 => cfg!(target_feature = "avx512bw"),
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx512Vbmi => cfg!(target_feature = "avx512vbmi"),
#[cfg(target_arch = "aarch64")]
Self::Neon => cfg!(target_feature = "neon"),
#[cfg(target_arch = "wasm32")]
Self::WasmSimd128 => cfg!(target_feature = "simd128"),
}
}
#[cfg(feature = "std")]
{
match self {
Self::Fallback => true,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Ssse3 => std::is_x86_feature_detected!("ssse3"),
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Sse4_2 => {
std::is_x86_feature_detected!("sse4.2")
&& std::is_x86_feature_detected!("popcnt")
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx2 => std::is_x86_feature_detected!("avx2"),
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx512 => std::is_x86_feature_detected!("avx512bw"),
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Avx512Vbmi => std::is_x86_feature_detected!("avx512vbmi"),
#[cfg(target_arch = "aarch64")]
Self::Neon => std::arch::is_aarch64_feature_detected!("neon"),
#[cfg(target_arch = "wasm32")]
Self::WasmSimd128 => {
cfg!(target_feature = "simd128")
}
}
}
}
}
impl Default for SimdBackend {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::SimdBackend;
#[test]
fn new_selects_the_highest_priority_available_backend() {
let selected = SimdBackend::new();
let expected = SimdBackend::backends()
.filter(SimdBackend::is_available)
.last()
.unwrap();
assert_eq!(selected, expected);
}
}