Add SpookyHashV2
[folly.git] / folly / Hash.h
1 /*
2  * Copyright 2012 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef FOLLY_BASE_HASH_H_
18 #define FOLLY_BASE_HASH_H_
19
20 #include <cstring>
21 #include <stdint.h>
22 #include <string>
23 #include <utility>
24
25 #include "folly/SpookyHashV1.h"
26 #include "folly/SpookyHashV2.h"
27
28 /*
29  * Various hashing functions.
30  */
31
32 namespace folly { namespace hash {
33
34 // This is a general-purpose way to create a single hash from multiple
35 // hashable objects. It relies on std::hash<T> being available for all
36 // relevant types and combines those hashes in an order-dependent way
37 // to yield a new hash.
38
39 // Never used, but gcc demands it.
40 inline size_t hash_combine() {
41   return 0;
42 }
43
44 // This is the Hash128to64 function from Google's cityhash (available
45 // under the MIT License).  We use it to reduce multiple 64 bit hashes
46 // into a single hash.
47 inline size_t hash_128_to_64(const size_t upper, const size_t lower) {
48   // Murmur-inspired hashing.
49   const size_t kMul = 0x9ddfea08eb382d69ULL;
50   size_t a = (lower ^ upper) * kMul;
51   a ^= (a >> 47);
52   size_t b = (upper ^ a) * kMul;
53   b ^= (b >> 47);
54   b *= kMul;
55   return b;
56 }
57
58 template <typename T, typename... Ts>
59 size_t hash_combine(const T& t, const Ts&... ts) {
60   size_t seed = std::hash<T>()(t);
61   if (sizeof...(ts) == 0) {
62     return seed;
63   }
64   size_t remainder = hash_combine(ts...);
65   return hash_128_to_64(seed, remainder);
66 }
67
68 //////////////////////////////////////////////////////////////////////
69
70 /*
71  * Thomas Wang 64 bit mix hash function
72  */
73
74 inline uint64_t twang_mix64(uint64_t key) {
75   key = (~key) + (key << 21);  // key *= (1 << 21) - 1; key -= 1;
76   key = key ^ (key >> 24);
77   key = key + (key << 3) + (key << 8);  // key *= 1 + (1 << 3) + (1 << 8)
78   key = key ^ (key >> 14);
79   key = key + (key << 2) + (key << 4);  // key *= 1 + (1 << 2) + (1 << 4)
80   key = key ^ (key >> 28);
81   key = key + (key << 31);  // key *= 1 + (1 << 31)
82   return key;
83 }
84
85 /*
86  * Inverse of twang_mix64
87  *
88  * Note that twang_unmix64 is significantly slower than twang_mix64.
89  */
90
91 inline uint64_t twang_unmix64(uint64_t key) {
92   // See the comments in jenkins_rev_unmix32 for an explanation as to how this
93   // was generated
94   key *= 4611686016279904257U;
95   key ^= (key >> 28) ^ (key >> 56);
96   key *= 14933078535860113213U;
97   key ^= (key >> 14) ^ (key >> 28) ^ (key >> 42) ^ (key >> 56);
98   key *= 15244667743933553977U;
99   key ^= (key >> 24) ^ (key >> 48);
100   key = (key + 1) * 9223367638806167551U;
101   return key;
102 }
103
104 /*
105  * Thomas Wang downscaling hash function
106  */
107
108 inline uint32_t twang_32from64(uint64_t key) {
109   key = (~key) + (key << 18);
110   key = key ^ (key >> 31);
111   key = key * 21;
112   key = key ^ (key >> 11);
113   key = key + (key << 6);
114   key = key ^ (key >> 22);
115   return (uint32_t) key;
116 }
117
118 /*
119  * Robert Jenkins' reversible 32 bit mix hash function
120  */
121
122 inline uint32_t jenkins_rev_mix32(uint32_t key) {
123   key += (key << 12);  // key *= (1 + (1 << 12))
124   key ^= (key >> 22);
125   key += (key << 4);   // key *= (1 + (1 << 4))
126   key ^= (key >> 9);
127   key += (key << 10);  // key *= (1 + (1 << 10))
128   key ^= (key >> 2);
129   // key *= (1 + (1 << 7)) * (1 + (1 << 12))
130   key += (key << 7);
131   key += (key << 12);
132   return key;
133 }
134
135 /*
136  * Inverse of jenkins_rev_mix32
137  *
138  * Note that jenkinks_rev_unmix32 is significantly slower than
139  * jenkins_rev_mix32.
140  */
141
142 inline uint32_t jenkins_rev_unmix32(uint32_t key) {
143   // These are the modular multiplicative inverses (in Z_2^32) of the
144   // multiplication factors in jenkins_rev_mix32, in reverse order.  They were
145   // computed using the Extended Euclidean algorithm, see
146   // http://en.wikipedia.org/wiki/Modular_multiplicative_inverse
147   key *= 2364026753U;
148
149   // The inverse of a ^= (a >> n) is
150   // b = a
151   // for (int i = n; i < 32; i += n) {
152   //   b ^= (a >> i);
153   // }
154   key ^=
155     (key >> 2) ^ (key >> 4) ^ (key >> 6) ^ (key >> 8) ^
156     (key >> 10) ^ (key >> 12) ^ (key >> 14) ^ (key >> 16) ^
157     (key >> 18) ^ (key >> 20) ^ (key >> 22) ^ (key >> 24) ^
158     (key >> 26) ^ (key >> 28) ^ (key >> 30);
159   key *= 3222273025U;
160   key ^= (key >> 9) ^ (key >> 18) ^ (key >> 27);
161   key *= 4042322161U;
162   key ^= (key >> 22);
163   key *= 16773121U;
164   return key;
165 }
166
167 /*
168  * Fowler / Noll / Vo (FNV) Hash
169  *     http://www.isthe.com/chongo/tech/comp/fnv/
170  */
171
172 const uint32_t FNV_32_HASH_START = 216613626UL;
173 const uint64_t FNV_64_HASH_START = 14695981039346656037ULL;
174
175 inline uint32_t fnv32(const char* s,
176                       uint32_t hash = FNV_32_HASH_START) {
177   for (; *s; ++s) {
178     hash += (hash << 1) + (hash << 4) + (hash << 7) +
179             (hash << 8) + (hash << 24);
180     hash ^= *s;
181   }
182   return hash;
183 }
184
185 inline uint32_t fnv32_buf(const void* buf,
186                           int n,
187                           uint32_t hash = FNV_32_HASH_START) {
188   const char* char_buf = reinterpret_cast<const char*>(buf);
189
190   for (int i = 0; i < n; ++i) {
191     hash += (hash << 1) + (hash << 4) + (hash << 7) +
192             (hash << 8) + (hash << 24);
193     hash ^= char_buf[i];
194   }
195
196   return hash;
197 }
198
199 inline uint32_t fnv32(const std::string& str,
200                       uint64_t hash = FNV_32_HASH_START) {
201   return fnv32_buf(str.data(), str.size(), hash);
202 }
203
204 inline uint64_t fnv64(const char* s,
205                       uint64_t hash = FNV_64_HASH_START) {
206   for (; *s; ++s) {
207     hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
208       (hash << 8) + (hash << 40);
209     hash ^= *s;
210   }
211   return hash;
212 }
213
214 inline uint64_t fnv64_buf(const void* buf,
215                           int n,
216                           uint64_t hash = FNV_64_HASH_START) {
217   const char* char_buf = reinterpret_cast<const char*>(buf);
218
219   for (int i = 0; i < n; ++i) {
220     hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
221       (hash << 8) + (hash << 40);
222     hash ^= char_buf[i];
223   }
224   return hash;
225 }
226
227 inline uint64_t fnv64(const std::string& str,
228                       uint64_t hash = FNV_64_HASH_START) {
229   return fnv64_buf(str.data(), str.size(), hash);
230 }
231
232 /*
233  * Paul Hsieh: http://www.azillionmonkeys.com/qed/hash.html
234  */
235
236 #define get16bits(d) (*((const uint16_t*) (d)))
237
238 inline uint32_t hsieh_hash32_buf(const void* buf, int len) {
239   const char* s = reinterpret_cast<const char*>(buf);
240   uint32_t hash = len;
241   uint32_t tmp;
242   int rem;
243
244   if (len <= 0 || buf == 0) {
245     return 0;
246   }
247
248   rem = len & 3;
249   len >>= 2;
250
251   /* Main loop */
252   for (;len > 0; len--) {
253     hash  += get16bits (s);
254     tmp    = (get16bits (s+2) << 11) ^ hash;
255     hash   = (hash << 16) ^ tmp;
256     s  += 2*sizeof (uint16_t);
257     hash  += hash >> 11;
258   }
259
260   /* Handle end cases */
261   switch (rem) {
262   case 3:
263     hash += get16bits(s);
264     hash ^= hash << 16;
265     hash ^= s[sizeof (uint16_t)] << 18;
266     hash += hash >> 11;
267     break;
268   case 2:
269     hash += get16bits(s);
270     hash ^= hash << 11;
271     hash += hash >> 17;
272     break;
273   case 1:
274     hash += *s;
275     hash ^= hash << 10;
276     hash += hash >> 1;
277   }
278
279   /* Force "avalanching" of final 127 bits */
280   hash ^= hash << 3;
281   hash += hash >> 5;
282   hash ^= hash << 4;
283   hash += hash >> 17;
284   hash ^= hash << 25;
285   hash += hash >> 6;
286
287   return hash;
288 };
289
290 #undef get16bits
291
292 inline uint32_t hsieh_hash32(const char* s) {
293   return hsieh_hash32_buf(s, std::strlen(s));
294 }
295
296 inline uint32_t hsieh_hash32_str(const std::string& str) {
297   return hsieh_hash32_buf(str.data(), str.size());
298 }
299
300 //////////////////////////////////////////////////////////////////////
301
302 } // namespace hash
303
304 template<class Key>
305 struct hasher;
306
307 template<> struct hasher<int32_t> {
308   size_t operator()(int32_t key) const {
309     return hash::jenkins_rev_mix32(uint32_t(key));
310   }
311 };
312
313 template<> struct hasher<uint32_t> {
314   size_t operator()(uint32_t key) const {
315     return hash::jenkins_rev_mix32(key);
316   }
317 };
318
319 template<> struct hasher<int64_t> {
320   size_t operator()(int64_t key) const {
321     return hash::twang_mix64(uint64_t(key));
322   }
323 };
324
325 template<> struct hasher<uint64_t> {
326   size_t operator()(uint64_t key) const {
327     return hash::twang_mix64(key);
328   }
329 };
330
331 } // namespace folly
332
333 // Custom hash functions.
334 namespace std {
335   // Hash function for pairs. Requires default hash functions for both
336   // items in the pair.
337   template <typename T1, typename T2>
338   class hash<std::pair<T1, T2> > {
339   public:
340     size_t operator()(const std::pair<T1, T2>& x) const {
341       return folly::hash::hash_combine(x.first, x.second);
342     }
343   };
344 } // namespace std
345
346 #endif