use super::*;
#[inline(always)]
pub(crate) fn mix64(mut x: u64) -> u64 {
x = x.wrapping_add(MIX_INC);
x = (x ^ (x >> 30)).wrapping_mul(MIX_M1);
x = (x ^ (x >> 27)).wrapping_mul(MIX_M2);
x ^ (x >> 31)
}
#[inline(always)]
pub(crate) fn hash_one(bytes: &[u8]) -> u64 {
let mut h1: u64 = FNV_OFFSET ^ (bytes.len() as u64);
let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
for chunk in chunks {
let w: u64 = u64::from_le_bytes(*chunk);
h1 = (h1.rotate_left(19) ^ w).wrapping_mul(MIX_M1);
}
if !rem.is_empty() {
let mut tail: u64 = 0;
let mut shift: u32 = 0;
for &b in rem {
tail |= u64::from(b) << shift;
shift += 8;
}
h1 = (h1.rotate_left(19) ^ tail).wrapping_mul(MIX_M1);
}
mix64(h1)
}
#[inline(always)]
pub(crate) fn hash_pair(bytes: &[u8]) -> (u64, u64) {
let mut h1: u64 = FNV_OFFSET ^ (bytes.len() as u64);
let mut h2: u64 = FNV_OFFSET ^ SECOND_SEED;
let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
for chunk in chunks {
let w: u64 = u64::from_le_bytes(*chunk);
h1 = (h1.rotate_left(19) ^ w).wrapping_mul(MIX_M1);
h2 = (h2.rotate_left(23) ^ w.wrapping_mul(MIX_M2)).wrapping_mul(FNV_PRIME);
}
if !rem.is_empty() {
let mut tail: u64 = 0;
let mut shift: u32 = 0;
for &b in rem {
tail |= u64::from(b) << shift;
shift += 8;
}
h1 = (h1.rotate_left(19) ^ tail).wrapping_mul(MIX_M1);
h2 = (h2.rotate_left(23) ^ tail.wrapping_mul(MIX_M2)).wrapping_mul(FNV_PRIME);
}
(mix64(h1), mix64(h2))
}
#[inline(always)]
pub fn hash_key_pair<K>(key: &K) -> (u64, u64)
where
K: Hash + ?Sized,
{
let mut hasher: DefaultHasher = DefaultHasher::new();
key.hash(&mut hasher);
let raw: u64 = hasher.finish();
(mix64(raw), mix64(raw ^ SECOND_SEED))
}