Skip to content

Commit

Permalink
Fix quality for 256-bit state version
Browse files Browse the repository at this point in the history
  • Loading branch information
ogxd committed Oct 29, 2023
1 parent 2a875c5 commit db63372
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 31 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ jobs:
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
run: cargo test --verbose
- name: Run tests (AVX2)
run: cargo test --verbose --features avx2
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "0.1.0"
edition = "2021"

[features]
# The 256-bit state GxHash is faster for large inputs than the default 128-bit state implementation.
# The 256-bit state GxHash is faster for large inputs than the default 128-bit state implementation, but faster on smaller hashes.
# Please not however that the 256-bit GxHash and the 128-bit GxHash don't generate the same hashes for a same input.
# Requires AVX2 and VAES (X86).
avx2 = []
Expand Down
28 changes: 0 additions & 28 deletions src/gxhash/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod platform;
use platform::*;

#[cfg(not(feature = "avx2"))]
#[inline(always)]
pub fn gxhash32(input: &[u8], seed: i32) -> u32 {
unsafe {
Expand All @@ -10,20 +9,6 @@ pub fn gxhash32(input: &[u8], seed: i32) -> u32 {
}
}

// Since the 256-bit runs AES operations on two 128-bit lanes, we need to extract
// the hash from the center, picking the same entropy amount from the two lanes
#[cfg(feature = "avx2")]
#[inline(always)]
pub fn gxhash32(input: &[u8], seed: i32) -> u32 {
unsafe {
let p = &gxhash(input, seed) as *const state as *const u8;
let offset = std::mem::size_of::<state>() / 2 - std::mem::size_of::<u32>() / 2 - 1;
let shifted_ptr = p.offset(offset as isize) as *const u32;
*shifted_ptr
}
}

#[cfg(not(feature = "avx2"))]
#[inline(always)]
pub fn gxhash64(input: &[u8], seed: i32) -> u64 {
unsafe {
Expand All @@ -32,19 +17,6 @@ pub fn gxhash64(input: &[u8], seed: i32) -> u64 {
}
}

// Since the 256-bit runs AES operations on two 128-bit lanes, we need to extract
// the hash from the center, picking the same entropy amount from the two lanes
#[cfg(feature = "avx2")]
#[inline(always)]
pub fn gxhash64(input: &[u8], seed: i32) -> u64 {
unsafe {
let p = &gxhash(input, seed) as *const state as *const u8;
let offset = std::mem::size_of::<state>() / 2 - std::mem::size_of::<u64>() / 2 - 1;
let shifted_ptr = p.offset(offset as isize) as *const u64;
*shifted_ptr
}
}

const VECTOR_SIZE: isize = std::mem::size_of::<state>() as isize;

#[inline(always)]
Expand Down
3 changes: 2 additions & 1 deletion src/gxhash/platform/x86_256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ pub unsafe fn finalize(hash: state, seed: i32) -> state {
hash = _mm256_aesenc_epi128(hash, keys_2);
hash = _mm256_aesenclast_epi128(hash, keys_3);

hash
let permuted = _mm256_permute2x128_si256(hash, hash, 0x21);
_mm256_xor_si256(hash, permuted)
}

0 comments on commit db63372

Please sign in to comment.