jadefin/utils/cyrb53.js

31 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2024-03-01 21:01:22 +01:00
// https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
/*
cyrb53 (c) 2018 bryc (github.com/bryc)
License: Public domain. Attribution appreciated.
A fast and simple 53-bit string hash function with decent collision resistance.
Largely inspired by MurmurHash2/3, but with a focus on speed/simplicity.
*/
/*
cyrb53a (c) 2023 bryc (github.com/bryc)
License: Public domain. Attribution appreciated.
The original cyrb53 has a slight mixing bias in the low bits of h1.
This shouldn't be a huge problem, but I want to try to improve it.
This new version should have improved avalanche behavior, but
it is not quite final, I may still find improvements.
So don't expect it to always produce the same output.
*/
export default function cyrb53a(str, seed = 0) {
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
for(let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 0x85ebca77);
h2 = Math.imul(h2 ^ ch, 0xc2b2ae3d);
}
h1 ^= Math.imul(h1 ^ (h2 >>> 15), 0x735a2d97);
h2 ^= Math.imul(h2 ^ (h1 >>> 15), 0xcaf649a9);
h1 ^= h2 >>> 16; h2 ^= h1 >>> 16;
return 2097152 * (h2 >>> 0) + (h1 >>> 11);
};