50.6.1Integer overflow¶
The two overflowing statements must be replaced with an explicit call to the intrinsic wrapping multiplication. This has the effect to never overflow:
fn hash(key: u64) -> u32
{
let mut h = key;
h ^= h >> 33;
h = h.wrapping_mul(0xff51afd7ed558ccd);
h ^= h >> 33;
h = h.wrapping_mul(0xc4ceb9fe1a85ec53);
h ^= h >> 33;
h as u32
}Here are the results for three integers:
for i in [128u64, 367u64, 1056u64] {
println!("hash({i}): {}", hash(i));
}hash(128): 2342296294
hash(367): 715564590
hash(1056): 3728619970
()