#![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 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 {
use super::*;
use alloc::vec::Vec;
#[test]
fn simd128_prefix_len_matches_scalar_on_long_run() {
let a = b"abcdefghijklmnopqrstuvwxyz0123456789-+=*";
let mut b: Vec<u8> = a.to_vec();
b[25] = b'!';
let max = a.len();
let got = unsafe { common_prefix_len_ptr(a.as_ptr(), b.as_ptr(), max) };
let scl = unsafe { scalar::common_prefix_len_ptr(a.as_ptr(), b.as_ptr(), max) };
assert_eq!(got, scl);
assert_eq!(got, 25);
}
#[test]
fn simd128_handles_short_input() {
let a = b"abc";
let b = b"abc";
assert_eq!(
unsafe { common_prefix_len_ptr(a.as_ptr(), b.as_ptr(), a.len()) },
3
);
}
}