Created
November 4, 2024 17:08
-
-
Save jaburns/e49df6538e02f220333f7b57ddd2988b to your computer and use it in GitHub Desktop.
Murmur3 32-bit hash
This file contains hidden or 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
u32 murmur3_32_hash(void* data, size_t len) { | |
u8* bytes = (u8*)data; | |
i32 nblocks = len / 4; | |
u32 h1 = 0x87c263d1; // seed | |
u32* blocks = (u32*)(bytes + nblocks * 4); | |
for (i32 i = -nblocks; i; i++) { | |
u32 k1 = blocks[i]; | |
k1 *= 0xcc9e2d51; | |
k1 = (k1 << 15) | (k1 >> 17); | |
k1 *= 0x1b873593; | |
h1 ^= k1; | |
h1 = (h1 << 13) | (h1 >> 19); | |
h1 = h1 * 5 + 0xe6546b64; | |
} | |
u8* tail = bytes + nblocks * 4; | |
u32 k1 = 0; | |
switch (len & 3) { | |
case 3: | |
k1 ^= tail[2] << 16; | |
case 2: | |
k1 ^= tail[1] << 8; | |
case 1: | |
k1 ^= tail[0]; | |
k1 *= 0xcc9e2d51; | |
k1 = (k1 << 15) | (k1 >> 17); | |
k1 *= 0x1b873593; | |
h1 ^= k1; | |
}; | |
h1 ^= len; | |
h1 ^= h1 >> 16; | |
h1 *= 0x85ebca6b; | |
h1 ^= h1 >> 13; | |
h1 *= 0xc2b2ae35; | |
h1 ^= h1 >> 16; | |
return h1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment