1 4545b700 2021-10-15 thomas //-----------------------------------------------------------------------------
2 4545b700 2021-10-15 thomas // MurmurHash2 was written by Austin Appleby, and is placed in the public
3 4545b700 2021-10-15 thomas // domain. The author hereby disclaims copyright to this source code.
5 4545b700 2021-10-15 thomas /* Obtained from https://github.com/aappleby/smhasher */
7 4545b700 2021-10-15 thomas #include <stdint.h>
8 f1b55c5c 2022-07-16 thomas #include <string.h>
10 4545b700 2021-10-15 thomas #include "murmurhash2.h"
13 f1b55c5c 2022-07-16 thomas murmurhash2(const unsigned char * key, int len, uint32_t seed)
15 4545b700 2021-10-15 thomas // 'm' and 'r' are mixing constants generated offline.
16 4545b700 2021-10-15 thomas // They're not really 'magic', they just happen to work well.
18 4545b700 2021-10-15 thomas const uint32_t m = 0x5bd1e995;
19 4545b700 2021-10-15 thomas const int r = 24;
21 4545b700 2021-10-15 thomas // Initialize the hash to a 'random' value
23 4545b700 2021-10-15 thomas uint32_t h = seed ^ len;
25 4545b700 2021-10-15 thomas // Mix 4 bytes at a time into the hash
27 f1b55c5c 2022-07-16 thomas const unsigned char *data = key;
29 4545b700 2021-10-15 thomas while(len >= 4)
31 f1b55c5c 2022-07-16 thomas uint32_t k;
33 f1b55c5c 2022-07-16 thomas memcpy(&k, data, sizeof(k));
36 4545b700 2021-10-15 thomas k ^= k >> r;
42 4545b700 2021-10-15 thomas data += 4;
46 4545b700 2021-10-15 thomas // Handle the last few bytes of the input array
48 4545b700 2021-10-15 thomas switch(len)
50 4545b700 2021-10-15 thomas case 3: h ^= data[2] << 16;
51 4545b700 2021-10-15 thomas case 2: h ^= data[1] << 8;
52 4545b700 2021-10-15 thomas case 1: h ^= data[0];
56 4545b700 2021-10-15 thomas // Do a few final mixes of the hash to ensure the last few
57 4545b700 2021-10-15 thomas // bytes are well-incorporated.
59 4545b700 2021-10-15 thomas h ^= h >> 13;
61 4545b700 2021-10-15 thomas h ^= h >> 15;