lovevanilla
Version:
Celebrating the Art of Simplicity in Web Development
44 lines (39 loc) • 1.11 kB
JavaScript
export function murmurhash3_32_gc(key, seed = 0) {
let remainder = key.length & 3; // key.length % 4
let bytes = key.length - remainder;
let h1 = seed;
let k1 = 0;
for (let i = 0; i < bytes; i += 4) {
k1 =
(key.charCodeAt(i) & 0xff) |
((key.charCodeAt(i + 1) & 0xff) << 8) |
((key.charCodeAt(i + 2) & 0xff) << 16) |
((key.charCodeAt(i + 3) & 0xff) << 24);
k1 = Math.imul(k1, 0xcc9e2d51);
k1 = (k1 << 15) | (k1 >>> 17);
k1 = Math.imul(k1, 0x1b873593);
h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19);
h1 = Math.imul(h1, 5) + 0xe6546b64;
}
k1 = 0;
switch (remainder) {
case 3:
k1 ^= (key.charCodeAt(bytes + 2) & 0xff) << 16;
case 2:
k1 ^= (key.charCodeAt(bytes + 1) & 0xff) << 8;
case 1:
k1 ^= key.charCodeAt(bytes) & 0xff;
k1 = Math.imul(k1, 0xcc9e2d51);
k1 = (k1 << 15) | (k1 >>> 17);
k1 = Math.imul(k1, 0x1b873593);
h1 ^= k1;
}
h1 ^= key.length;
h1 ^= h1 >>> 16;
h1 = Math.imul(h1, 0x85ebca6b);
h1 ^= h1 >>> 13;
h1 = Math.imul(h1, 0xc2b2ae35);
h1 ^= h1 >>> 16;
return h1 >>> 0;
}