MCPcopy Create free account
hub / github.com/ElementsProject/elements / MurmurHash3

Function MurmurHash3

src/hash.cpp:17–73  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15}
16
17unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash)
18{
19 // The following is MurmurHash3 (x86_32), see https://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
20 uint32_t h1 = nHashSeed;
21 const uint32_t c1 = 0xcc9e2d51;
22 const uint32_t c2 = 0x1b873593;
23
24 const int nblocks = vDataToHash.size() / 4;
25
26 //----------
27 // body
28 const uint8_t* blocks = vDataToHash.data();
29
30 for (int i = 0; i < nblocks; ++i) {
31 uint32_t k1 = ReadLE32(blocks + i*4);
32
33 k1 *= c1;
34 k1 = ROTL32(k1, 15);
35 k1 *= c2;
36
37 h1 ^= k1;
38 h1 = ROTL32(h1, 13);
39 h1 = h1 * 5 + 0xe6546b64;
40 }
41
42 //----------
43 // tail
44 const uint8_t* tail = vDataToHash.data() + nblocks * 4;
45
46 uint32_t k1 = 0;
47
48 switch (vDataToHash.size() & 3) {
49 case 3:
50 k1 ^= tail[2] << 16;
51 [[fallthrough]];
52 case 2:
53 k1 ^= tail[1] << 8;
54 [[fallthrough]];
55 case 1:
56 k1 ^= tail[0];
57 k1 *= c1;
58 k1 = ROTL32(k1, 15);
59 k1 *= c2;
60 h1 ^= k1;
61 }
62
63 //----------
64 // finalization
65 h1 ^= vDataToHash.size();
66 h1 ^= h1 >> 16;
67 h1 *= 0x85ebca6b;
68 h1 ^= h1 >> 13;
69 h1 *= 0xc2b2ae35;
70 h1 ^= h1 >> 16;
71
72 return h1;
73}
74

Callers 2

HashMethod · 0.85
RollingBloomHashFunction · 0.85

Calls 4

ReadLE32Function · 0.85
ROTL32Function · 0.85
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected