0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MurmurHash.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // MurmurHash2, by Austin Appleby
3 // (public domain, cf. http://murmurhash.googlepages.com/)
4 
5 // Note - This code makes a few assumptions about how your machine behaves -
6 
7 // 1. We can read a 4-byte value from any address without crashing
8 // 2. sizeof(int) == 4
9 
10 // And it has a few limitations -
11 
12 // 1. It will not work incrementally.
13 // 2. It will not produce the same results on little-endian and big-endian
14 // machines.
15 
24 #include "Common/Compat.h"
25 #include "Common/MurmurHash.h"
26 
27 namespace Hypertable {
28 
29 uint32_t murmurhash2(const void *key, size_t len, uint32_t seed) {
30  // 'm' and 'r' are mixing constants generated offline.
31  // They're not really 'magic', they just happen to work well.
32  const uint32_t m = 0x5bd1e995;
33  const int r = 24;
34 
35  // Initialize the hash to a 'random' value
36  uint32_t h = seed ^ len;
37 
38  // Mix 4 bytes at a time into the hash
39  const unsigned char * data = (const unsigned char *)key;
40 
41  while (len >= 4) {
42  uint32_t k = *(uint32_t *)data;
43 
44  k *= m;
45  k ^= k >> r;
46  k *= m;
47 
48  h *= m;
49  h ^= k;
50 
51  data += 4;
52  len -= 4;
53  }
54 
55  // Handle the last few bytes of the input array
56  switch (len) {
57  case 3: h ^= data[2] << 16;
58  case 2: h ^= data[1] << 8;
59  case 1: h ^= data[0];
60  h *= m;
61  };
62 
63  // Do a few final mixes of the hash to ensure the last few
64  // bytes are well-incorporated.
65  h ^= h >> 13;
66  h *= m;
67  h ^= h >> 15;
68 
69  return h;
70 }
71 
72 } // namespace Hypertable
MurmurHash2 digest routine.
uint32_t murmurhash2(const void *key, size_t len, uint32_t seed)
The murmurhash2 implementation.
Definition: MurmurHash.cc:29
Compatibility Macros for C/C++.
Hypertable definitions