Skip to content

Commit

Permalink
Factorize some platform methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ogxd committed Dec 30, 2023
1 parent e18043b commit 559b565
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 39 deletions.
19 changes: 0 additions & 19 deletions src/gxhash/platform/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {

Expand Down
19 changes: 19 additions & 0 deletions src/gxhash/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ pub(crate) const VECTOR_SIZE: usize = size_of::<State>();
// 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;
Expand All @@ -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,
Expand Down
20 changes: 0 additions & 20 deletions src/gxhash/platform/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 559b565

Please sign in to comment.