New version update
[m6w6/libmemcached] / libmemcached / murmur_hash.c
1 #include "common.h"
2
3 /*
4 "Murmur"hash provided by Austin, tanjent@gmail.com
5 */
6
7 #define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
8
9 uint32_t murmur_hash(char *key, size_t length)
10 {
11 const uint32_t m= 0x5bd1e995;
12 const int r= 16;
13 uint32_t h= length * m;
14 uint32_t k = 0;
15
16 while(length >= 4)
17 {
18 k = *(uint32_t*)key;
19 MIX(h,k,m);
20
21 key += 4;
22 length -= 4;
23 }
24
25 switch(length)
26 {
27 case 3: k += key[2] << 16;
28 case 2: k += key[1] << 8;
29 case 1: k += key[0];
30 MIX(h,k,m);
31 };
32
33 h *= m;
34 h ^= h >> 10;
35 h *= m;
36 h ^= h >> 17;
37
38 return h;
39 }