#![cfg(all(
target_arch = "wasm32",
target_feature = "simd128",
feature = "kernel_simd128"
))]
#![allow(dead_code)]
use core::arch::wasm32::{i8x16_bitmask, u8x16_eq, v128, v128_load};
use super::scalar;
pub(crate) const KERNEL_TAG: &str = "simd128";
#[inline]
pub(crate) fn hash_mix_u64(value: u64) -> u64 {
scalar::hash_mix_u64(value)
}
#[target_feature(enable = "simd128")]
#[inline]
pub(crate) unsafe fn prefix_len_simd(lhs: *const u8, rhs: *const u8, max: usize) -> usize {
let mut off = 0usize;
while off + 16 <= max {
let a: v128 = unsafe { v128_load(lhs.add(off) as *const v128) };
let b: v128 = unsafe { v128_load(rhs.add(off) as *const v128) };
let mask = i8x16_bitmask(u8x16_eq(a, b));
if mask != 0xFFFF {
return off + (!mask).trailing_zeros() as usize;
}
off += 16;
}
off
}
#[target_feature(enable = "simd128")]
#[inline]
pub(crate) unsafe fn common_prefix_len_ptr(lhs: *const u8, rhs: *const u8, max: usize) -> usize {
let chunk = core::mem::size_of::<usize>();
if chunk <= max {
let lhs_word = unsafe { core::ptr::read_unaligned(lhs.cast::<usize>()) };
let rhs_word = unsafe { core::ptr::read_unaligned(rhs.cast::<usize>()) };
let diff = lhs_word ^ rhs_word;
if diff != 0 {
return scalar::mismatch_byte_index(diff);
}
}
let off = unsafe { prefix_len_simd(lhs, rhs, max) };
unsafe { scalar::common_prefix_len_scalar_ptr(lhs, rhs, off, max) }
}
#[target_feature(enable = "simd128")]
#[inline]
pub(crate) unsafe fn count_match_from_indices(
concat: &[u8],
current_idx: usize,
candidate_idx: usize,
tail_limit: usize,
seed_len: usize,
) -> usize {
let seed = seed_len.min(tail_limit);
if seed == tail_limit {
return seed;
}
let remaining = tail_limit - seed;
let base = concat.as_ptr();
let lhs = unsafe { base.add(candidate_idx + seed) };
let rhs = unsafe { base.add(current_idx + seed) };
let extra = unsafe { common_prefix_len_ptr(lhs, rhs, remaining) };
seed + extra
}
#[cfg(test)]
mod tests;