From 4f52e1aeded8d3deb56a95c6c66772822de35609 Mon Sep 17 00:00:00 2001 From: notsatvrn Date: Sat, 30 Dec 2023 09:17:52 -0800 Subject: [PATCH] improve target support --- README.md | 6 +++--- src/gxhash/platform/{aarch64.rs => arm.rs} | 14 +++++++++----- src/gxhash/platform/mod.rs | 8 ++++---- src/gxhash/platform/{x86_64.rs => x86.rs} | 3 +++ 4 files changed, 19 insertions(+), 12 deletions(-) rename src/gxhash/platform/{aarch64.rs => arm.rs} (97%) rename src/gxhash/platform/{x86_64.rs => x86.rs} (98%) diff --git a/README.md b/README.md index a782d1e..167a2b6 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,10 @@ Check out the [paper](https://github.com/ogxd/gxhash-rust/blob/main/article/arti ### Architecture Compatibility GxHash is compatible with: -- X86 processors with `AES-NI` intrinsics -- ARM processors with `NEON` intrinsics +- X86 processors with `AES-NI` & `SSE2` intrinsics +- ARM processors with `AES` & `NEON` intrinsics > **Warning** -> Other platforms are currently not supported (there is no fallback). The behavior on these platforms is undefined. +> Other platforms are currently not supported (there is no fallback). GxHash will not build on these platforms. ### Hashes Stability All generated hashes for a given version of GxHash are stable, meaning that for a given input the output hash will be the same across all supported platforms. diff --git a/src/gxhash/platform/aarch64.rs b/src/gxhash/platform/arm.rs similarity index 97% rename from src/gxhash/platform/aarch64.rs rename to src/gxhash/platform/arm.rs index 89ac301..31aa6a1 100644 --- a/src/gxhash/platform/aarch64.rs +++ b/src/gxhash/platform/arm.rs @@ -1,3 +1,6 @@ +#[cfg(target_arch = "arm")] +use core::arch::arm::*; +#[cfg(target_arch = "aarch64")] use core::arch::aarch64::*; use super::*; @@ -21,6 +24,7 @@ pub unsafe fn load_unaligned(p: *const State) -> State { #[inline(always)] pub unsafe fn get_partial(p: *const State, len: usize) -> State { + // Safety check if check_same_page(p) { get_partial_unsafe(p, len) } else { @@ -47,11 +51,6 @@ pub unsafe fn get_partial_unsafe(data: *const State, len: usize) -> State { vaddq_s8(partial_vector, vdupq_n_s8(len as i8)) } -#[inline(always)] -pub unsafe fn ld(array: *const u32) -> State { - vreinterpretq_s8_u32(vld1q_u32(array)) -} - #[inline(always)] // See https://blog.michaelbrase.com/2018/05/08/emulating-x86-aes-intrinsics-on-armv8-a pub unsafe fn aes_encrypt(data: State, keys: State) -> State { @@ -72,6 +71,11 @@ pub unsafe fn aes_encrypt_last(data: State, keys: State) -> State { vreinterpretq_s8_u8(veorq_u8(encrypted, vreinterpretq_u8_s8(keys))) } +#[inline(always)] +pub unsafe fn ld(array: *const u32) -> State { + vreinterpretq_s8_u32(vld1q_u32(array)) +} + #[inline(always)] pub unsafe fn finalize(hash: State) -> State { let mut hash = aes_encrypt(hash, ld(KEYS.as_ptr())); diff --git a/src/gxhash/platform/mod.rs b/src/gxhash/platform/mod.rs index 2d0a90c..29e2311 100644 --- a/src/gxhash/platform/mod.rs +++ b/src/gxhash/platform/mod.rs @@ -1,9 +1,9 @@ -#[cfg(target_arch = "aarch64")] -#[path = "aarch64.rs"] +#[cfg(all(any(target_arch = "arm", target_arch = "aarch64"), target_feature = "aes", target_feature = "neon"))] +#[path = "arm.rs"] mod platform; -#[cfg(target_arch = "x86_64")] -#[path = "x86_64.rs"] +#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", target_feature = "sse2"))] +#[path = "x86.rs"] mod platform; pub use platform::*; diff --git a/src/gxhash/platform/x86_64.rs b/src/gxhash/platform/x86.rs similarity index 98% rename from src/gxhash/platform/x86_64.rs rename to src/gxhash/platform/x86.rs index d8fd98e..d2f0e2b 100644 --- a/src/gxhash/platform/x86_64.rs +++ b/src/gxhash/platform/x86.rs @@ -1,3 +1,6 @@ +#[cfg(target_arch = "x86")] +use core::arch::x86::*; +#[cfg(target_arch = "x86_64")] use core::arch::x86_64::*; use super::*;