]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/MurmurHash2.c
1 /* SPDX-License-Identifier: LicenseRef-murmurhash2-public-domain */
2 //-----------------------------------------------------------------------------
3 // MurmurHash2 was written by Austin Appleby, and is placed in the public
4 // domain. The author hereby disclaims copyright to this source code.
6 // Note - This code makes a few assumptions about how your machine behaves -
8 // 1. We can read a 4-byte value from any address without crashing
11 // And it has a few limitations -
13 // 1. It will not work incrementally.
14 // 2. It will not produce the same results on little-endian and big-endian
17 #include "MurmurHash2.h"
20 _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
23 //-----------------------------------------------------------------------------
24 // Platform-specific functions and macros
26 // Microsoft Visual Studio
30 #define BIG_CONSTANT(x) (x)
34 #else // defined(_MSC_VER)
36 #define BIG_CONSTANT(x) (x##LLU)
38 #endif // !defined(_MSC_VER)
40 //-----------------------------------------------------------------------------
42 uint32_t MurmurHash2 ( const void * key
, int len
, uint32_t seed
)
44 // 'm' and 'r' are mixing constants generated offline.
45 // They're not really 'magic', they just happen to work well.
47 const uint32_t m
= 0x5bd1e995;
50 // Initialize the hash to a 'random' value
52 uint32_t h
= seed
^ len
;
54 // Mix 4 bytes at a time into the hash
56 const unsigned char * data
= (const unsigned char *)key
;
60 uint32_t k
= *(uint32_t*)data
;
73 // Handle the last few bytes of the input array
77 case 3: h
^= data
[2] << 16; /* fall through */
78 case 2: h
^= data
[1] << 8; /* fall through */
79 case 1: h
^= data
[0]; /* fall through */
83 // Do a few final mixes of the hash to ensure the last few
84 // bytes are well-incorporated.