Blob


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