#[cfg(target_arch = "aarch64")]
mod aarch64;
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
mod wasm32;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod x86_64;
use crate::backend::SimdBackend;
pub(super) type SearchFn = unsafe fn(&[u8; 16], &[u8; 16], &[u8]) -> Option<usize>;
#[inline(always)]
pub(super) fn search_fn<const REVERT: bool>(backend: SimdBackend) -> SearchFn {
match backend {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
SimdBackend::Avx2 => x86_64::truffle_avx2::<REVERT>,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
SimdBackend::Ssse3 | SimdBackend::Sse4_2 => x86_64::truffle_ssse3::<REVERT>,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
SimdBackend::Avx512 | SimdBackend::Avx512Vbmi => x86_64::truffle_avx512::<REVERT>,
#[cfg(target_arch = "aarch64")]
SimdBackend::Neon => aarch64::search::<REVERT>,
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
SimdBackend::WasmSimd128 => wasm32::search::<REVERT>,
_ => search_fallback::<REVERT>,
}
}
#[inline(always)]
pub(super) fn search_fallback<const REVERT: bool>(
lower: &[u8; 16],
upper: &[u8; 16],
haystack: &[u8],
) -> Option<usize> {
haystack
.iter()
.position(|&byte| find::<REVERT>(lower, upper, byte))
}
#[inline(always)]
pub(super) fn rsearch_fn<const REVERT: bool>(backend: SimdBackend) -> SearchFn {
match backend {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
SimdBackend::Avx2 => x86_64::rtruffle_avx2::<REVERT>,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
SimdBackend::Ssse3 | SimdBackend::Sse4_2 => x86_64::rtruffle_ssse3::<REVERT>,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
SimdBackend::Avx512 | SimdBackend::Avx512Vbmi => x86_64::rtruffle_avx512::<REVERT>,
#[cfg(target_arch = "aarch64")]
SimdBackend::Neon => aarch64::rsearch::<REVERT>,
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
SimdBackend::WasmSimd128 => wasm32::rsearch::<REVERT>,
_ => rsearch_fallback::<REVERT>,
}
}
#[inline(always)]
pub(super) fn rsearch_fallback<const REVERT: bool>(
lower: &[u8; 16],
upper: &[u8; 16],
haystack: &[u8],
) -> Option<usize> {
haystack
.iter()
.rposition(|&byte| find::<REVERT>(lower, upper, byte))
}
#[inline(always)]
fn find<const REVERT: bool>(lower: &[u8; 16], upper: &[u8; 16], byte: u8) -> bool {
let half = if byte < 128 { lower } else { upper };
let flags = unsafe { half.get_unchecked((byte & 0b0000_1111) as usize) };
(flags & (1 << ((byte >> 4) & 0b0000_0111)) != 0) != REVERT
}