Skip to content

Commit

Permalink
Change seed from i64 to u64 (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
ogxd authored Jun 12, 2024
1 parent c09e22a commit f1ba437
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion benches/throughput/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() {

// GxHash
let gxhash_name = if cfg!(hybrid) { "gxhash-hybrid" } else { "gxhash" };
benchmark(processor.as_mut(), slice, gxhash_name, |data: &[u8], seed: i64| -> u64 {
benchmark(processor.as_mut(), slice, gxhash_name, |data: &[u8], seed: u64| -> u64 {
gxhash64(data, seed)
});

Expand Down
2 changes: 1 addition & 1 deletion benches/throughput_criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn benchmark_all(c: &mut Criterion) {
// GxHash
let gxhash_name = if cfg!(hybrid) { "gxhash-hybrid" } else { "gxhash" };
benchmark(&mut group, slice, gxhash_name, |data: &[u8], seed: i32| -> u64 {
gxhash64(data, seed as i64)
gxhash64(data, seed as u64)
});

// AHash
Expand Down
12 changes: 6 additions & 6 deletions src/gxhash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use platform::*;
/// println!("Hash is {:x}!", gxhash::gxhash32(&bytes, seed));
/// ```
#[inline(always)]
pub fn gxhash32(input: &[u8], seed: i64) -> u32 {
pub fn gxhash32(input: &[u8], seed: u64) -> u32 {
unsafe {
let p = &gxhash(input, create_seed(seed)) as *const State as *const u32;
*p
Expand All @@ -29,7 +29,7 @@ pub fn gxhash32(input: &[u8], seed: i64) -> u32 {
/// println!("Hash is {:x}!", gxhash::gxhash64(&bytes, seed));
/// ```
#[inline(always)]
pub fn gxhash64(input: &[u8], seed: i64) -> u64 {
pub fn gxhash64(input: &[u8], seed: u64) -> u64 {
unsafe {
let p = &gxhash(input, create_seed(seed)) as *const State as *const u64;
*p
Expand All @@ -46,7 +46,7 @@ pub fn gxhash64(input: &[u8], seed: i64) -> u64 {
/// println!("Hash is {:x}!", gxhash::gxhash128(&bytes, seed));
/// ```
#[inline(always)]
pub fn gxhash128(input: &[u8], seed: i64) -> u128 {
pub fn gxhash128(input: &[u8], seed: u64) -> u128 {
unsafe {
let p = &gxhash(input, create_seed(seed)) as *const State as *const u128;
*p
Expand Down Expand Up @@ -219,8 +219,8 @@ mod tests {
assert_eq!(4243413987, gxhash32(&[0u8; 1], 0));
assert_eq!(2401749549, gxhash32(&[0u8; 1000], 0));
assert_eq!(4156851105, gxhash32(&[42u8; 4242], 42));
assert_eq!(1981427771, gxhash32(&[42u8; 4242], -42));
assert_eq!(1156095992, gxhash32(b"Hello World", i64::MAX));
assert_eq!(540827083, gxhash32(b"Hello World", i64::MIN));
assert_eq!(1981427771, gxhash32(&[42u8; 4242], -42i64 as u64));
assert_eq!(1156095992, gxhash32(b"Hello World", i64::MAX as u64));
assert_eq!(540827083, gxhash32(b"Hello World", i64::MIN as u64));
}
}
4 changes: 2 additions & 2 deletions src/gxhash/platform/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub unsafe fn create_empty() -> State {
}

#[inline(always)]
pub unsafe fn create_seed(seed: i64) -> State {
vreinterpretq_s8_s64(vdupq_n_s64(seed))
pub unsafe fn create_seed(seed: u64) -> State {
vreinterpretq_s8_p64(vdupq_n_p64(seed))
}

#[inline(always)]
Expand Down
4 changes: 2 additions & 2 deletions src/gxhash/platform/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub unsafe fn create_empty() -> State {
}

#[inline(always)]
pub unsafe fn create_seed(seed: i64) -> State {
_mm_set1_epi64x(seed)
pub unsafe fn create_seed(seed: u64) -> State {
_mm_set1_epi64x(seed as i64)
}

#[inline(always)]
Expand Down
4 changes: 2 additions & 2 deletions src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl GxHasher {
/// println!("Hash is {:x}!", hasher.finish());
/// ```
#[inline]
pub fn with_seed(seed: i64) -> GxHasher {
pub fn with_seed(seed: u64) -> GxHasher {
// Use gxhash64 to generate an initial state from a seed
GxHasher::with_state(unsafe { create_seed(seed) })
}
Expand Down Expand Up @@ -144,7 +144,7 @@ impl GxBuildHasher {
/// Hardcoding a seed may make your [`Hasher`] vulnerable to DOS attacks.
/// It is recommended to use [`GxBuildHasher::default()`] for improved DOS resistance.
#[inline]
pub fn with_seed(seed: i64) -> GxBuildHasher {
pub fn with_seed(seed: u64) -> GxBuildHasher {
// Use gxhash64 to generate an initial state from a seed
GxBuildHasher(unsafe { create_seed(seed) })
}
Expand Down

0 comments on commit f1ba437

Please sign in to comment.