Created
August 23, 2022 20:58
-
-
Save vbezhenar/03ab89a22128e54872677516952ac5a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Performs Murmur3_32 hashing. | |
* https://en.wikipedia.org/wiki/MurmurHash | |
* @param key Int32Array input key | |
* @param seed int32 input seed | |
* @returns int32 value | |
*/ | |
export default function murmur332(key, seed) { | |
let hash = seed; | |
/* eslint-disable no-bitwise */ | |
for (let i = 0; i < key.length; i += 1) { | |
let k = key[i]; | |
k = Math.imul(k, -862048943); | |
k = (k << 15) | (k >>> 17); | |
k = Math.imul(k, 461845907); | |
hash ^= k; | |
hash = (hash << 13) | (hash >>> 19); | |
hash = (Math.imul(hash, 5) + -430675100) | 0; | |
} | |
hash ^= (key.length << 2); | |
hash ^= (hash >>> 16); | |
hash = Math.imul(hash, -2048144789); | |
hash ^= (hash >>> 13); | |
hash = Math.imul(hash, -1028477387); | |
hash ^= (hash >>> 16); | |
/* eslint-enable no-bitwise */ | |
return hash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment