1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2 * Use of this file is governed by the BSD 3-clause license that
3 * can be found in the LICENSE.txt file in the project root.
6 #include "misc/MurmurHash.h"
8 using namespace antlr4::misc;
10 // A variation of the MurmurHash3 implementation (https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp)
11 // Here we unrolled the loop used there into individual calls to update(), as we usually hash object fields
12 // instead of entire buffers.
14 // Platform-specific functions and macros
16 // Microsoft Visual Studio
20 #define FORCE_INLINE __forceinline
24 #define ROTL32(x,y) _rotl(x,y)
25 #define ROTL64(x,y) _rotl64(x,y)
27 #define BIG_CONSTANT(x) (x)
29 #else // defined(_MSC_VER)
33 #define FORCE_INLINE inline __attribute__((always_inline))
35 inline uint32_t rotl32 (uint32_t x, int8_t r)
37 return (x << r) | (x >> (32 - r));
40 inline uint64_t rotl64 (uint64_t x, int8_t r)
42 return (x << r) | (x >> (64 - r));
45 #define ROTL32(x,y) rotl32(x,y)
46 #define ROTL64(x,y) rotl64(x,y)
48 #define BIG_CONSTANT(x) (x##LLU)
50 #endif // !defined(_MSC_VER)
52 size_t MurmurHash::initialize() {
53 return initialize(DEFAULT_SEED);
56 size_t MurmurHash::initialize(size_t seed) {
60 #if defined(_WIN32) || defined(_WIN64)
69 #if defined(__x86_64__) || defined(__ppc64__)
76 #if defined(ENVIRONMENT32)
78 size_t MurmurHash::update(size_t hash, size_t value) {
79 static const size_t c1 = 0xCC9E2D51;
80 static const size_t c2 = 0x1B873593;
88 hash = ROTL32(hash, 13);
89 hash = hash * 5 + 0xE6546B64;
95 size_t MurmurHash::finish(size_t hash, size_t entryCount) {
96 hash ^= entryCount * 4;
107 size_t MurmurHash::update(size_t hash, size_t value) {
108 static const size_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
109 static const size_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
117 hash = ROTL64(hash, 27);
118 hash = hash * 5 + 0x52dce729;
124 size_t MurmurHash::finish(size_t hash, size_t entryCount) {
125 hash ^= entryCount * 8;
127 hash *= 0xff51afd7ed558ccd;
129 hash *= 0xc4ceb9fe1a85ec53;