ihash 0.1.2

World's fastest high-quality non-cryptographic hash, deterministic cross-platform XOF, and seekable stream cipher
Documentation
use core::arch::x86_64::{
  __m128i, __m256i, __m512i, _mm512_add_epi64, _mm512_aesenc_epi128, _mm512_aesenclast_epi128,
  _mm512_broadcast_i32x4, _mm512_castsi512_si256, _mm512_extracti64x4_epi64, _mm512_loadu_si512,
  _mm512_setzero_si512, _mm512_shuffle_i64x2, _mm512_storeu_si512, _mm512_xor_si512,
  _mm512_zextsi128_si512,
};

use super::{
  aes128::X86_128,
  vaes256::{Vaes256, ctr_with_rk},
};
use crate::{
  arch::{
    tier::def_hw_tier,
    wide::{Lane512x2, Wide512, WideAes, ctr_4_wide},
  },
  backend::ctr::{CtrKernel, split_root},
};

#[derive(Clone, Copy)]
struct Vaes512;

impl WideAes for Vaes512 {
  type L = X86_128;
  type W = __m512i;
  const BLOCK_PER_VEC: usize = 4;

  #[inline(always)]
  fn lane(self) -> X86_128 {
    X86_128
  }

  #[inline(always)]
  unsafe fn zero_w(self) -> Self::W {
    // SAFETY: caller ensures avx512f.
    unsafe { _mm512_setzero_si512() }
  }

  #[inline(always)]
  unsafe fn ld_w(self, ptr: *const u8) -> Self::W {
    // SAFETY: caller ensures avx512f and >= 64 readable bytes at ptr.
    unsafe { _mm512_loadu_si512(ptr.cast()) }
  }

  #[inline(always)]
  unsafe fn st_w(self, ptr: *mut u8, v: Self::W) {
    // SAFETY: caller ensures avx512f and >= 64 writable bytes at ptr.
    unsafe { _mm512_storeu_si512(ptr.cast(), v) };
  }

  #[inline(always)]
  unsafe fn zext128(self, v: __m128i) -> Self::W {
    // SAFETY: caller ensures avx512f.
    unsafe { _mm512_zextsi128_si512(v) }
  }

  #[inline(always)]
  unsafe fn broadcast(self, v: __m128i) -> Self::W {
    // SAFETY: caller ensures avx512f.
    unsafe { _mm512_broadcast_i32x4(v) }
  }

  #[inline(always)]
  unsafe fn xor_w(self, a: Self::W, b: Self::W) -> Self::W {
    // SAFETY: caller ensures avx512f.
    unsafe { _mm512_xor_si512(a, b) }
  }

  #[inline(always)]
  unsafe fn add_u64_w(self, a: Self::W, b: Self::W) -> Self::W {
    // SAFETY: caller ensures avx512f.
    unsafe { _mm512_add_epi64(a, b) }
  }

  #[inline(always)]
  unsafe fn aesenc_w(self, a: Self::W, rk: Self::W) -> Self::W {
    // SAFETY: caller ensures avx512f+vaes.
    unsafe { _mm512_aesenc_epi128(a, rk) }
  }

  #[inline(always)]
  unsafe fn aesenclast_w(self, a: Self::W, rk: Self::W) -> Self::W {
    // SAFETY: caller ensures avx512f+vaes.
    unsafe { _mm512_aesenclast_epi128(a, rk) }
  }
}

impl Lane512x2 for Vaes512 {
  type Half = Vaes256;

  #[inline(always)]
  fn half(self) -> Vaes256 {
    Vaes256
  }

  #[inline(always)]
  unsafe fn split_half(self, v: __m512i) -> (__m256i, __m256i) {
    // SAFETY: caller ensures avx512f.
    unsafe { (_mm512_castsi512_si256(v), _mm512_extracti64x4_epi64::<1>(v)) }
  }

  #[inline(always)]
  unsafe fn swap32_in_64(self, v: __m512i) -> __m512i {
    // SAFETY: caller ensures avx512f.
    unsafe { _mm512_shuffle_i64x2::<0x4E>(v, v) }
  }
}

impl CtrKernel for Vaes512 {
  #[inline(always)]
  unsafe fn ctr<const XOR: bool>(self, root: &[u8; 32], mut blk: u64, buf: &mut [u8]) {
    // SAFETY: caller ensures avx512f+vaes+avx2+aes and buf.len() is a multiple of BLOCK_LEN.
    unsafe {
      let rk_li = split_root(self.lane(), root);
      let mut ptr = buf.as_mut_ptr();
      let mut rem = buf.len();
      ctr_4_wide::<Vaes512, XOR>(Vaes512, rk_li, &mut blk, &mut ptr, &mut rem);
      if rem > 0 {
        ctr_with_rk::<XOR>(rk_li, blk, ptr, rem);
      }
    }
  }
}

def_hw_tier!(
  Wide512(Vaes512),
  Vaes512,
  __m128i,
  all(
    target_feature = "avx512f",
    target_feature = "vaes",
    target_feature = "avx2",
    target_feature = "aes",
    target_feature = "sse2"
  ),
  all(
    target_feature = "avx512f",
    target_feature = "vaes",
    target_feature = "avx2",
    target_feature = "aes",
    target_feature = "sse2"
  ),
  all(target_feature = "aes", target_feature = "sse2"),
  "avx512f,vaes,avx2,aes"
);