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 // Note - This code makes a few assumptions about how your machine behaves -
7 // 1. We can read a 4-byte value from any address without crashing
10 // And it has a few limitations -
12 // 1. It will not work incrementally.
13 // 2. It will not produce the same results on little-endian and big-endian
16 #include "MurmurHash2.h"
18 //-----------------------------------------------------------------------------
19 // Platform-specific functions and macros
21 // Microsoft Visual Studio
25 #define BIG_CONSTANT(x) (x)
29 #else // defined(_MSC_VER)
31 #define BIG_CONSTANT(x) (x##LLU)
33 #endif // !defined(_MSC_VER)
35 //-----------------------------------------------------------------------------
37 uint32_t MurmurHash2 ( const void * key, int len, uint32_t seed )
39 // 'm' and 'r' are mixing constants generated offline.
40 // They're not really 'magic', they just happen to work well.
42 const uint32_t m = 0x5bd1e995;
45 // Initialize the hash to a 'random' value
47 uint32_t h = seed ^ len;
49 // Mix 4 bytes at a time into the hash
51 const unsigned char * data = (const unsigned char *)key;
55 uint32_t k = *(uint32_t*)data;
68 // Handle the last few bytes of the input array
72 case 3: h ^= data[2] << 16;
73 case 2: h ^= data[1] << 8;
78 // Do a few final mixes of the hash to ensure the last few
79 // bytes are well-incorporated.