]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/misc/MurmurHash.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / misc / MurmurHash.cpp
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.
4  */
5
6 #include "misc/MurmurHash.h"
7
8 using namespace antlr4::misc;
9
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.
13
14 // Platform-specific functions and macros
15
16 // Microsoft Visual Studio
17
18 #if defined(_MSC_VER)
19
20 #define FORCE_INLINE    __forceinline
21
22 #include <stdlib.h>
23
24 #define ROTL32(x,y)     _rotl(x,y)
25 #define ROTL64(x,y)     _rotl64(x,y)
26
27 #define BIG_CONSTANT(x) (x)
28
29 #else   // defined(_MSC_VER)
30
31 // Other compilers
32
33 #define FORCE_INLINE inline __attribute__((always_inline))
34
35 inline uint32_t rotl32 (uint32_t x, int8_t r)
36 {
37   return (x << r) | (x >> (32 - r));
38 }
39
40 inline uint64_t rotl64 (uint64_t x, int8_t r)
41 {
42   return (x << r) | (x >> (64 - r));
43 }
44
45 #define ROTL32(x,y)     rotl32(x,y)
46 #define ROTL64(x,y)     rotl64(x,y)
47
48 #define BIG_CONSTANT(x) (x##LLU)
49
50 #endif // !defined(_MSC_VER)
51
52 size_t MurmurHash::initialize() {
53   return initialize(DEFAULT_SEED);
54 }
55
56 size_t MurmurHash::initialize(size_t seed) {
57   return seed;
58 }
59
60 #if defined(_WIN32) || defined(_WIN64)
61   #if _WIN64
62     #define ENVIRONMENT64
63   #else
64     #define ENVIRONMENT32
65   #endif
66 #endif
67
68 #if defined(__GNUC__)
69   #if defined(__x86_64__) || defined(__ppc64__)
70     #define ENVIRONMENT64
71   #else
72     #define ENVIRONMENT32
73   #endif
74 #endif
75
76 #if defined(ENVIRONMENT32)
77
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;
81
82   size_t k1 = value;
83   k1 *= c1;
84   k1 = ROTL32(k1, 15);
85   k1 *= c2;
86
87   hash ^= k1;
88   hash = ROTL32(hash, 13);
89   hash = hash * 5 + 0xE6546B64;
90
91   return hash;
92 }
93
94
95 size_t MurmurHash::finish(size_t hash, size_t entryCount) {
96   hash ^= entryCount * 4;
97   hash ^= hash >> 16;
98   hash *= 0x85EBCA6B;
99   hash ^= hash >> 13;
100   hash *= 0xC2B2AE35;
101   hash ^= hash >> 16;
102   return hash;
103 }
104
105 #else
106
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);
110
111   size_t k1 = value;
112   k1 *= c1;
113   k1 = ROTL64(k1, 31);
114   k1 *= c2;
115
116   hash ^= k1;
117   hash = ROTL64(hash, 27);
118   hash = hash * 5 + 0x52dce729;
119
120   return hash;
121 }
122
123
124 size_t MurmurHash::finish(size_t hash, size_t entryCount) {
125   hash ^= entryCount * 8;
126   hash ^= hash >> 33;
127   hash *= 0xff51afd7ed558ccd;
128   hash ^= hash >> 33;
129   hash *= 0xc4ceb9fe1a85ec53;
130   hash ^= hash >> 33;
131   return hash;
132 }
133
134 #endif