@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
40 lines (32 loc) • 1.19 kB
JavaScript
/**
* NOTE: based on https://github.com/garycourt/murmurhash-js/blob/master/murmurhash3_gc.js
* @param {Uint32Array} key
* @param {number} seed
* @returns {number}
*/
export function murmur3_32(key, seed) {
let h1 = seed;
let k1;
let i = 0;
const key_length = key.length;
while (i < key_length) {
k1 = key[i];
++i;
k1 = ((((k1 & 0xffff) * 0xcc9e2d51) + ((((k1 >>> 16) * 0xcc9e2d51) & 0xffff) << 16)));
k1 = (k1 << 15) | (k1 >>> 17);
k1 = ((((k1 & 0xffff) * 0x1b873593) + ((((k1 >>> 16) * 0x1b873593) & 0xffff) << 16)));
h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19);
h1 = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16)));
h1 = (((h1 & 0xffff) + 0x6b64) + ((((h1 >>> 16) + 0xe654) & 0xffff) << 16));
}
// mix in array length
h1 ^= key_length;
h1 ^= h1 >>> 16;
h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16));
h1 ^= h1 >>> 13;
h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16)));
h1 ^= h1 >>> 16;
// cast to uint32
return h1 >>> 0;
}