Update hash functions to known good ones instead of bespoke ones.

This commit is contained in:
Nathan Vegdahl 2022-07-21 05:22:22 -07:00
parent 0d71ae86db
commit 7082f2d7f4

View File

@ -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 <https://github.com/skeeto/hash-prospector>.
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 <https://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html>.
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.