#[inline(always)]
#[cfg(target_endian = "little")]
pub(crate) const fn mismatch_byte_index(diff: usize) -> usize {
diff.trailing_zeros() as usize / 8
}
#[inline(always)]
#[cfg(target_endian = "big")]
pub(crate) const fn mismatch_byte_index(diff: usize) -> usize {
diff.leading_zeros() as usize / 8
}
#[inline(always)]
pub(crate) unsafe fn common_prefix_len_scalar_ptr(
lhs: *const u8,
rhs: *const u8,
mut off: usize,
max: usize,
) -> usize {
let chunk = core::mem::size_of::<usize>();
while off + chunk <= max {
let lhs_word = unsafe { core::ptr::read_unaligned(lhs.add(off) as *const usize) };
let rhs_word = unsafe { core::ptr::read_unaligned(rhs.add(off) as *const usize) };
let diff = lhs_word ^ rhs_word;
if diff != 0 {
return off + mismatch_byte_index(diff);
}
off += chunk;
}
while off < max {
if unsafe { *lhs.add(off) != *rhs.add(off) } {
break;
}
off += 1;
}
off
}
#[inline(always)]
pub(crate) unsafe fn common_prefix_len_ptr(lhs: *const u8, rhs: *const u8, max: usize) -> usize {
unsafe { common_prefix_len_scalar_ptr(lhs, rhs, 0, max) }
}
#[cfg_attr(
all(
target_arch = "aarch64",
target_endian = "little",
feature = "kernel-neon"
),
allow(dead_code)
)]
#[inline(always)]
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;