~brianaker/libmemcached/1164440

« back to all changes in this revision

Viewing changes to libmemcached/murmur_hash.c

  • Committer: patg@patg.net
  • Date: 2008-05-05 03:32:10 UTC
  • mfrom: (317.1.71)
  • Revision ID: patg@patg.net-20080505033210-yxv0us02t8lb44dy
Merge 477:75823cad36b7 with -r 476

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
}