]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/MurmurHash2.c
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"
19 _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
22 //-----------------------------------------------------------------------------
23 // Platform-specific functions and macros
25 // Microsoft Visual Studio
29 #define BIG_CONSTANT(x) (x)
33 #else // defined(_MSC_VER)
35 #define BIG_CONSTANT(x) (x##LLU)
37 #endif // !defined(_MSC_VER)
39 //-----------------------------------------------------------------------------
41 uint32_t MurmurHash2 ( const void * key
, int len
, uint32_t seed
)
43 // 'm' and 'r' are mixing constants generated offline.
44 // They're not really 'magic', they just happen to work well.
46 const uint32_t m
= 0x5bd1e995;
49 // Initialize the hash to a 'random' value
51 uint32_t h
= seed
^ len
;
53 // Mix 4 bytes at a time into the hash
55 const unsigned char * data
= (const unsigned char *)key
;
59 uint32_t k
= *(uint32_t*)data
;
72 // Handle the last few bytes of the input array
76 case 3: h
^= data
[2] << 16; /* fall through */
77 case 2: h
^= data
[1] << 8; /* fall through */
78 case 1: h
^= data
[0]; /* fall through */
82 // Do a few final mixes of the hash to ensure the last few
83 // bytes are well-incorporated.