#![allow(clippy::manual_map)]
#[macro_use]
mod macros;
cfg_if! {
if #[cfg(all(
target_feature = "sse2",
any(target_arch = "x86", target_arch = "x86_64"),
not(miri)
))] {
mod sse2;
use sse2 as imp;
} else if #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] {
mod neon;
use neon as imp;
} else {
mod generic;
use generic as imp;
}
}
mod bitmask;
use crate::error::Error;
use crate::error::ErrorKind;
pub(crate) use self::imp::Group;
use core::mem;
#[derive(Debug)]
pub(crate) struct ProbeSeq {
pub(crate) pos: usize,
stride: usize,
}
impl ProbeSeq {
#[inline]
pub(crate) fn move_next(&mut self, bucket_mask: usize) -> Result<(), Error> {
if self.stride > bucket_mask {
return Err(Error::new(ErrorKind::StrideOutOfBounds {
index: self.stride,
len: bucket_mask,
}));
}
self.stride += Group::WIDTH;
self.pos += self.stride;
self.pos &= bucket_mask;
Ok(())
}
}
const MIN_HASH_LEN: usize = if mem::size_of::<usize>() < mem::size_of::<u64>() {
mem::size_of::<usize>()
} else {
mem::size_of::<u64>()
};
#[inline]
pub(crate) fn probe_seq(bucket_mask: usize, hash: u64) -> ProbeSeq {
ProbeSeq {
pos: h1(hash) & bucket_mask,
stride: 0,
}
}
#[inline]
#[allow(clippy::cast_possible_truncation)]
fn h1(hash: u64) -> usize {
hash as usize
}
#[inline]
#[allow(clippy::cast_possible_truncation)]
pub(crate) fn h2(hash: u64) -> u8 {
let top7 = hash >> (MIN_HASH_LEN * 8 - 7);
(top7 & 0x7f) as u8 }
pub(crate) const EMPTY: u8 = 0b1111_1111;
#[inline]
#[cfg(feature = "alloc")]
pub(crate) fn is_full(ctrl: u8) -> bool {
ctrl & 0x80 == 0
}
#[inline]
#[cfg(feature = "alloc")]
pub(crate) fn is_special(ctrl: u8) -> bool {
ctrl & 0x80 != 0
}
#[inline]
#[cfg(feature = "alloc")]
pub(crate) fn special_is_empty(ctrl: u8) -> bool {
debug_assert!(is_special(ctrl));
ctrl & 0x01 != 0
}
#[cfg_attr(target_os = "emscripten", inline(never))]
#[cfg_attr(not(target_os = "emscripten"), inline)]
#[cfg(feature = "alloc")]
pub(crate) fn capacity_to_buckets(cap: usize) -> Option<usize> {
if cap < 8 {
return Some(if cap < 4 { 4 } else { 8 });
}
let adjusted_cap = cap.checked_mul(8)? / 7;
Some(adjusted_cap.next_power_of_two())
}