ihash 0.1.2

World's fastest high-quality non-cryptographic hash, deterministic cross-platform XOF, and seekable stream cipher
Documentation
#[cfg(any(
  feature = "cpu",
  not(any(
    all(
      target_arch = "aarch64",
      target_endian = "little",
      target_feature = "aes",
      target_feature = "neon"
    ),
    all(
      any(target_arch = "x86", target_arch = "x86_64"),
      target_endian = "little",
      target_feature = "aes",
      target_feature = "sse2"
    )
  ))
))]
pub(crate) mod soft;

#[cfg(any(
  all(
    target_arch = "aarch64",
    target_endian = "little",
    any(feature = "std", all(target_feature = "aes", target_feature = "neon"))
  ),
  all(
    any(target_arch = "x86", target_arch = "x86_64"),
    target_endian = "little",
    any(feature = "std", all(target_feature = "aes", target_feature = "sse2")),
    not(all(
      target_feature = "vaes",
      target_feature = "avx2",
      target_feature = "aes",
      target_feature = "sse2"
    ))
  )
))]
mod ctr_simd;

#[cfg(all(
  any(target_arch = "x86", target_arch = "x86_64"),
  target_endian = "little",
  any(
    feature = "std",
    all(
      target_feature = "vaes",
      target_feature = "avx2",
      target_feature = "aes",
      target_feature = "sse2"
    )
  )
))]
mod wide;

#[cfg(any(
  all(
    target_arch = "aarch64",
    target_endian = "little",
    any(feature = "std", all(target_feature = "aes", target_feature = "neon"))
  ),
  all(
    any(target_arch = "x86", target_arch = "x86_64"),
    target_endian = "little",
    any(feature = "std", all(target_feature = "aes", target_feature = "sse2"))
  )
))]
mod tier;

#[cfg(all(
  feature = "std",
  any(
    all(
      target_arch = "aarch64",
      target_endian = "little",
      not(all(target_feature = "aes", target_feature = "neon"))
    ),
    all(
      any(target_arch = "x86", target_arch = "x86_64"),
      target_endian = "little",
      not(any(
        all(
          target_arch = "x86_64",
          target_feature = "avx512f",
          target_feature = "vaes",
          target_feature = "avx2",
          target_feature = "aes",
          target_feature = "sse2"
        ),
        all(
          target_arch = "x86",
          target_feature = "vaes",
          target_feature = "avx2",
          target_feature = "aes",
          target_feature = "sse2"
        )
      ))
    )
  )
))]
mod dispatch;

#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
mod arm;
#[cfg(all(
  any(target_arch = "x86", target_arch = "x86_64"),
  target_endian = "little"
))]
mod x86;

#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
pub(crate) use arm::ArmPlatform as Active;
#[cfg(not(any(
  all(target_arch = "aarch64", target_endian = "little"),
  all(
    any(target_arch = "x86", target_arch = "x86_64"),
    target_endian = "little"
  )
)))]
pub(crate) use soft::Soft128 as Active;
#[cfg(all(
  any(target_arch = "x86", target_arch = "x86_64"),
  target_endian = "little"
))]
pub(crate) use x86::X86Platform as Active;

mod inline_entry {
  use crate::{
    backend::{
      group::{GroupV, LaneGroup},
      tree::{cold, compress_0_16_entry, compress_17_128, pack_root_v},
    },
    state::{BLOCK_LEN, SHORT_MAX},
  };

  #[inline(always)]
  unsafe fn hash_short_or<G: LaneGroup, R, const HI: bool, const SEED0: bool>(
    ctx: G,
    data: &[u8],
    seed: u64,
    from_v: impl FnOnce(G::L, GroupV<G>, GroupV<G>) -> R,
    on_long: impl FnOnce(&[u8], u64) -> R,
  ) -> R {
    // SAFETY: caller ensures target features matching `ctx` and `on_long`.
    unsafe {
      let l = ctx.lane();
      let (lo, hi) = if data.len() <= BLOCK_LEN {
        compress_0_16_entry::<G::L, HI, SEED0>(l, data, seed)
      } else if data.len() <= SHORT_MAX {
        compress_17_128::<G, HI, SEED0>(ctx, data, seed)
      } else {
        cold();
        return on_long(data, seed);
      };
      from_v(l, lo, hi)
    }
  }

  /// Shared inline-short / cold-long digest entry parameterized by long-input handler.
  #[inline(always)]
  pub(super) unsafe fn hash_lo_or<G: LaneGroup, R, const SEED0: bool>(
    ctx: G,
    data: &[u8],
    seed: u64,
    from_v: impl FnOnce(G::L, GroupV<G>) -> R,
    on_long: impl FnOnce(&[u8], u64) -> R,
  ) -> R {
    // SAFETY: forwarded from caller contract.
    unsafe {
      hash_short_or::<G, R, false, SEED0>(ctx, data, seed, |l, lo, _| from_v(l, lo), on_long)
    }
  }

  /// Shared inline-short / cold-long 256-bit digest entry parameterized by long-input handler.
  #[inline(always)]
  pub(super) unsafe fn hash256_or<G: LaneGroup, const SEED0: bool>(
    ctx: G,
    data: &[u8],
    seed: u64,
    on_long: impl FnOnce(&[u8], u64) -> [u8; 32],
  ) -> [u8; 32] {
    // SAFETY: forwarded from caller contract.
    unsafe {
      hash_short_or::<G, [u8; 32], true, SEED0>(
        ctx,
        data,
        seed,
        |l, lo, hi| pack_root_v(l, lo, hi),
        on_long,
      )
    }
  }
}