1 b343c297 2021-10-11 stsp //-----------------------------------------------------------------------------
2 b343c297 2021-10-11 stsp // MurmurHash2 was written by Austin Appleby, and is placed in the public
3 b343c297 2021-10-11 stsp // domain. The author hereby disclaims copyright to this source code.
5 b343c297 2021-10-11 stsp /* Obtained from https://github.com/aappleby/smhasher */
7 b343c297 2021-10-11 stsp #include <stdint.h>
9 b343c297 2021-10-11 stsp #include "murmurhash2.h"
12 b343c297 2021-10-11 stsp murmurhash2(const void * key, int len, uint32_t seed)
14 b343c297 2021-10-11 stsp // 'm' and 'r' are mixing constants generated offline.
15 b343c297 2021-10-11 stsp // They're not really 'magic', they just happen to work well.
17 b343c297 2021-10-11 stsp const uint32_t m = 0x5bd1e995;
18 b343c297 2021-10-11 stsp const int r = 24;
20 b343c297 2021-10-11 stsp // Initialize the hash to a 'random' value
22 b343c297 2021-10-11 stsp uint32_t h = seed ^ len;
24 b343c297 2021-10-11 stsp // Mix 4 bytes at a time into the hash
26 b343c297 2021-10-11 stsp const unsigned char *data = (const unsigned char *)key;
28 b343c297 2021-10-11 stsp while(len >= 4)
30 b343c297 2021-10-11 stsp uint32_t k = *(uint32_t*)data;
33 b343c297 2021-10-11 stsp k ^= k >> r;
43 b343c297 2021-10-11 stsp // Handle the last few bytes of the input array
47 b343c297 2021-10-11 stsp case 3: h ^= data[2] << 16;
48 b343c297 2021-10-11 stsp case 2: h ^= data[1] << 8;
49 b343c297 2021-10-11 stsp case 1: h ^= data[0];
53 b343c297 2021-10-11 stsp // Do a few final mixes of the hash to ensure the last few
54 b343c297 2021-10-11 stsp // bytes are well-incorporated.
56 b343c297 2021-10-11 stsp h ^= h >> 13;
58 b343c297 2021-10-11 stsp h ^= h >> 15;