From 559b5652274a3e170ecd9aeda6abbef46304b0b8 Mon Sep 17 00:00:00 2001 From: Olivier Giniaux Date: Sat, 30 Dec 2023 22:49:36 +0100 Subject: [PATCH] Factorize some platform methods --- src/gxhash/platform/arm.rs | 19 ------------------- src/gxhash/platform/mod.rs | 19 +++++++++++++++++++ src/gxhash/platform/x86.rs | 20 -------------------- 3 files changed, 19 insertions(+), 39 deletions(-) diff --git a/src/gxhash/platform/arm.rs b/src/gxhash/platform/arm.rs index 31aa6a1..90269a8 100644 --- a/src/gxhash/platform/arm.rs +++ b/src/gxhash/platform/arm.rs @@ -22,16 +22,6 @@ pub unsafe fn load_unaligned(p: *const State) -> State { vld1q_s8(p as *const i8) } -#[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 { - get_partial_safe(p, len) - } -} - #[inline(never)] pub unsafe fn get_partial_safe(data: *const State, len: usize) -> State { // Temporary buffer filled with zeros @@ -76,15 +66,6 @@ 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())); - hash = aes_encrypt(hash, ld(KEYS.as_ptr().offset(4))); - hash = aes_encrypt_last(hash, ld(KEYS.as_ptr().offset(8))); - - hash -} - #[inline(always)] pub unsafe fn compress_8(mut ptr: *const State, end_address: usize, hash_vector: State, len: usize) -> State { diff --git a/src/gxhash/platform/mod.rs b/src/gxhash/platform/mod.rs index 29e2311..0feb928 100644 --- a/src/gxhash/platform/mod.rs +++ b/src/gxhash/platform/mod.rs @@ -14,6 +14,16 @@ pub(crate) const VECTOR_SIZE: usize = size_of::(); // 4KiB is the default page size for most systems, and conservative for other systems such as MacOS ARM (16KiB) const PAGE_SIZE: usize = 0x1000; +#[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 { + get_partial_safe(p, len) + } +} + #[inline(always)] unsafe fn check_same_page(ptr: *const State) -> bool { let address = ptr as usize; @@ -23,6 +33,15 @@ unsafe fn check_same_page(ptr: *const State) -> bool { offset_within_page < PAGE_SIZE - VECTOR_SIZE } +#[inline(always)] +pub unsafe fn finalize(hash: State) -> State { + let mut hash = aes_encrypt(hash, ld(KEYS.as_ptr())); + hash = aes_encrypt(hash, ld(KEYS.as_ptr().offset(4))); + hash = aes_encrypt_last(hash, ld(KEYS.as_ptr().offset(8))); + + hash +} + pub const KEYS: [u32; 12] = [0xF2784542, 0xB09D3E21, 0x89C222E5, 0xFC3BC28E, 0x03FCE279, 0xCB6B2E9B, 0xB361DC58, 0x39132BD9, diff --git a/src/gxhash/platform/x86.rs b/src/gxhash/platform/x86.rs index d2f0e2b..980c08d 100644 --- a/src/gxhash/platform/x86.rs +++ b/src/gxhash/platform/x86.rs @@ -22,16 +22,6 @@ pub unsafe fn load_unaligned(p: *const State) -> State { _mm_loadu_si128(p) } -#[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 { - get_partial_safe(p, len) - } -} - #[inline(always)] pub unsafe fn get_partial_safe(data: *const State, len: usize) -> State { // Temporary buffer filled with zeros @@ -69,16 +59,6 @@ pub unsafe fn ld(array: *const u32) -> State { _mm_loadu_si128(array as *const State) } -#[inline(always)] -#[allow(overflowing_literals)] -pub unsafe fn finalize(hash: State) -> State { - let mut hash = _mm_aesenc_si128(hash, ld(KEYS.as_ptr())); - hash = _mm_aesenc_si128(hash, ld(KEYS.as_ptr().offset(4))); - hash = _mm_aesenclast_si128(hash, ld(KEYS.as_ptr().offset(8))); - - hash -} - #[cfg(not(hybrid))] #[inline(always)] pub unsafe fn compress_8(mut ptr: *const State, end_address: usize, hash_vector: State, len: usize) -> State {