use super::short;
use super::spec::{self, finish_lanes, Words};
use core::sync::atomic::{AtomicUsize, Ordering};
pub type Hash64 = unsafe fn(*const u8, usize, u64) -> u64;
pub type Hash128 = unsafe fn(*const u8, usize, u64) -> u128;
pub type Absorb = unsafe fn(&mut Words, *const u8, usize);
pub type Finish = unsafe fn(&Words, *const u8, usize, *const u8, usize, u64) -> u128;
pub type Short64 = unsafe fn(&[u8], u64, u64) -> u64;
pub type Short128 = unsafe fn(&[u8], u64, u64) -> u128;
#[derive(Clone, Copy)]
pub struct Backend {
pub name: &'static str,
pub hash64: Hash64,
pub hash128: Hash128,
pub absorb: Absorb,
pub finish: Finish,
pub short64_le32: Short64,
pub short64_le64: Short64,
pub short128_le32: Short128,
pub short128_le64: Short128,
}
unsafe fn hash64_spec(p: *const u8, n: usize, ks: u64) -> u64 {
finish_lanes::<false>(&spec::lanes(core::slice::from_raw_parts(p, n), ks), ks, n) as u64
}
unsafe fn hash128_spec(p: *const u8, n: usize, ks: u64) -> u128 {
finish_lanes::<true>(&spec::lanes(core::slice::from_raw_parts(p, n), ks), ks, n)
}
unsafe fn absorb_spec(w: &mut Words, p: *const u8, nblocks: usize) {
let mut a = w.to_state();
spec::absorb(&mut a, core::slice::from_raw_parts(p, nblocks * spec::BLOCK));
*w = Words::from_state(&a);
}
unsafe fn finish_spec(w: &Words, leading: *const u8, r: usize, closing: *const u8, n: usize, ks: u64) -> u128 {
let m = spec::finish(&w.to_state(), core::slice::from_raw_parts(leading, r * spec::STRIPE), &*(closing as *const [u8; 64]), n);
finish_lanes::<true>(&m, ks, n)
}
pub const SPEC: Backend = Backend {
name: "spec",
hash64: hash64_spec,
hash128: hash128_spec,
absorb: absorb_spec,
finish: finish_spec,
short64_le32: short::short64_le32,
short64_le64: short::short64_le64,
short128_le32: short::short128_le32,
short128_le64: short::short128_le64,
};
#[allow(unused_macros)] macro_rules! backend {
($name:ident, $s:literal, $m:path, $sh:path) => {
pub const $name: Backend = {
use $m as k;
use $sh as sh;
Backend {
name: $s,
hash64: k::hash64,
hash128: k::hash128,
absorb: k::absorb,
finish: k::finish,
short64_le32: sh::short64_le32,
short64_le64: sh::short64_le64,
short128_le32: sh::short128_le32,
short128_le64: sh::short128_le64,
}
};
};
}
#[cfg(all(target_arch = "x86_64", not(feature = "force-fallback")))]
backend!(SSE2, "sse2", super::x86::sse2, super::short);
#[cfg(all(target_arch = "x86_64", not(feature = "force-fallback")))]
backend!(AVX2, "avx2", super::x86::avx2, super::short::bmi2);
#[cfg(all(target_arch = "x86_64", not(feature = "force-fallback")))]
backend!(AVX512, "avx512", super::x86::avx512, super::short::bmi2);
#[cfg(all(target_arch = "aarch64", not(feature = "force-fallback")))]
backend!(NEON, "neon", super::arm, super::short);
#[cfg(all(target_arch = "wasm32", target_feature = "simd128", not(feature = "force-fallback")))]
backend!(SIMD128, "simd128", super::wasm, super::short);
#[cfg(all(target_arch = "x86_64", not(feature = "force-fallback")))]
#[inline(always)]
pub fn has_avx512() -> bool {
if cfg!(all(target_feature = "avx512f", target_feature = "avx512vl", target_feature = "avx2", target_feature = "bmi2")) {
return true;
}
#[cfg(feature = "std")]
{
return std::is_x86_feature_detected!("avx512f") && std::is_x86_feature_detected!("avx512vl") && std::is_x86_feature_detected!("avx2") && std::is_x86_feature_detected!("bmi2");
}
#[allow(unreachable_code)]
false
}
#[cfg(all(target_arch = "x86_64", not(feature = "force-fallback")))]
#[inline(always)]
pub fn has_avx2() -> bool {
if cfg!(all(target_feature = "avx2", target_feature = "bmi2")) {
return true;
}
#[cfg(feature = "std")]
{
return std::is_x86_feature_detected!("avx2") && std::is_x86_feature_detected!("bmi2");
}
#[allow(unreachable_code)]
false
}
fn select() -> &'static Backend {
#[cfg(all(target_arch = "x86_64", not(feature = "force-fallback")))]
{
if has_avx512() {
return &AVX512;
}
if has_avx2() {
return &AVX2;
}
return &SSE2;
}
#[cfg(all(target_arch = "aarch64", not(feature = "force-fallback")))]
{
return &NEON;
}
#[cfg(all(target_arch = "wasm32", target_feature = "simd128", not(feature = "force-fallback")))]
{
return &SIMD128;
}
#[allow(unreachable_code)]
&SPEC
}
static CACHE: AtomicUsize = AtomicUsize::new(0);
#[inline(always)]
pub fn backend() -> &'static Backend {
#[cfg(all(target_arch = "x86_64", not(feature = "force-fallback"), target_feature = "avx512f", target_feature = "avx512vl", target_feature = "avx2", target_feature = "bmi2"))]
{
return &AVX512;
}
#[cfg(all(target_arch = "aarch64", not(feature = "force-fallback")))]
{
return &NEON;
}
#[cfg(all(target_arch = "wasm32", target_feature = "simd128", not(feature = "force-fallback")))]
{
return &SIMD128;
}
#[allow(unreachable_code)]
backend_detected()
}
#[inline(always)]
fn backend_detected() -> &'static Backend {
let p = CACHE.load(Ordering::Relaxed);
if p != 0 {
return unsafe { &*(p as *const Backend) };
}
let b = select();
CACHE.store(b as *const Backend as usize, Ordering::Relaxed);
b
}
#[inline(always)]
pub fn hash64_keyed(bytes: &[u8], ka: u64, kb: u64, ks: u64) -> u64 {
let n = bytes.len();
if n <= spec::SHORT_MAX {
return short::short64_keyed(bytes, ka, kb);
}
unsafe { (backend().hash64)(bytes.as_ptr(), n, ks) }
}
#[inline(always)]
pub fn hash128_keyed(bytes: &[u8], ka: u64, kb: u64, ks: u64) -> u128 {
let n = bytes.len();
if n <= spec::SHORT_MAX {
return short::short128_keyed(bytes, ka, kb);
}
unsafe { (backend().hash128)(bytes.as_ptr(), n, ks) }
}
#[inline(always)]
pub fn hash64(bytes: &[u8], seed: u64) -> u64 {
let (ka, kb, ks) = spec::keys(seed);
hash64_keyed(bytes, ka, kb, ks)
}
#[inline(always)]
pub fn hash128(bytes: &[u8], seed: u64) -> u128 {
let (ka, kb, ks) = spec::keys(seed);
hash128_keyed(bytes, ka, kb, ks)
}
#[inline(always)]
pub fn hash64_fixed<const N: usize>(key: &[u8; N], seed: u64) -> u64 {
let (ka, kb, ks) = spec::keys(seed);
if N <= spec::SHORT_MAX {
short::short128_inline::<false>(key, ka, kb) as u64
} else {
hash64_keyed(key, ka, kb, ks)
}
}
#[inline(always)]
pub fn hash128_fixed<const N: usize>(key: &[u8; N], seed: u64) -> u128 {
let (ka, kb, ks) = spec::keys(seed);
if N <= spec::SHORT_MAX {
short::short128_inline::<true>(key, ka, kb)
} else {
hash128_keyed(key, ka, kb, ks)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Keyed {
ka: u64,
kb: u64,
ks: u64,
}
impl Keyed {
pub const fn new(seed: u64) -> Self {
let (ka, kb, ks) = spec::keys(seed);
Keyed { ka, kb, ks }
}
#[inline(always)]
pub fn hash64(&self, bytes: &[u8]) -> u64 {
hash64_keyed(bytes, self.ka, self.kb, self.ks)
}
#[inline(always)]
pub fn hash128(&self, bytes: &[u8]) -> u128 {
hash128_keyed(bytes, self.ka, self.kb, self.ks)
}
#[inline(always)]
pub fn hash64_fixed<const N: usize>(&self, key: &[u8; N]) -> u64 {
if N <= spec::SHORT_MAX {
short::short128_inline::<false>(key, self.ka, self.kb) as u64
} else {
hash64_keyed(key, self.ka, self.kb, self.ks)
}
}
}