diff --git a/src/hash.rs b/src/hash.rs index 6790092..49022a9 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -1,23 +1,35 @@ -pub fn hash_u32(n: u32, seed: u32) -> u32 { - let mut hash = n; - for _ in 0..3 { - hash = hash.wrapping_mul(0x736caf6f); - hash ^= hash.wrapping_shr(16); - hash ^= seed; - } +/// A fast seedable 32-bit hash function. +/// +/// Based on . +pub fn hash_u32(mut n: u32, seed: u32) -> u32 { + n ^= 0xe6fe3beb; // So zero doesn't map to zero. + n ^= seed; - hash + n ^= n >> 16; + n = n.wrapping_mul(0x21f0aaad); + n ^= seed; + n ^= n >> 15; + n = n.wrapping_mul(0xd35a2d97); + n ^= n >> 15; + + n } -pub fn hash_u64(n: u64, seed: u64) -> u64 { - let mut hash = n; - for _ in 0..4 { - hash = hash.wrapping_mul(32_416_190_071 * 314_604_959); - hash ^= hash.wrapping_shr(32); - hash ^= seed; - } +/// A fast seedable 64-bit hash function. +/// +/// Based on . +pub fn hash_u64(mut n: u64, seed: u64) -> u64 { + n ^= 0x4acc3f27cc712c9d; // So zero doesn't map to zero. + n ^= seed; - hash + n ^= n >> 30; + n = n.wrapping_mul(0xbf58476d1ce4e5b9); + n ^= seed; + n ^= n >> 27; + n = n.wrapping_mul(0x94d049bb133111eb); + n ^= n >> 31; + + n } /// Returns a random float in [0, 1] based on 'n' and a seed.