Add IEC binary unit prefixes
[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 /*
26  * Various hashing functions.
27  */
28
29 namespace folly { namespace hash {
30
31 // This is a general-purpose way to create a single hash from multiple
32 // hashable objects. It relies on std::hash<T> being available for all
33 // relevant types and combines those hashes in an order-dependent way
34 // to yield a new hash.
35
36 // Never used, but gcc demands it.
37 inline size_t hash_combine() {
38   return 0;
39 }
40
41 // This is the Hash128to64 function from Google's cityhash (available
42 // under the MIT License).  We use it to reduce multiple 64 bit hashes
43 // into a single hash.
44 inline size_t hash_128_to_64(const size_t upper, const size_t lower) {
45   // Murmur-inspired hashing.
46   const size_t kMul = 0x9ddfea08eb382d69ULL;
47   size_t a = (lower ^ upper) * kMul;
48   a ^= (a >> 47);
49   size_t b = (upper ^ a) * kMul;
50   b ^= (b >> 47);
51   b *= kMul;
52   return b;
53 }
54
55 template <typename T, typename... Ts>
56 size_t hash_combine(const T& t, const Ts&... ts) {
57   size_t seed = std::hash<T>()(t);
58   if (sizeof...(ts) == 0) {
59     return seed;
60   }
61   size_t remainder = hash_combine(ts...);
62   return hash_128_to_64(seed, remainder);
63 }
64
65 //////////////////////////////////////////////////////////////////////
66
67 /*
68  * Thomas Wang 64 bit mix hash function
69  */
70
71 inline uint64_t twang_mix64(uint64_t key) {
72   key = (~key) + (key << 21);
73   key = key ^ (key >> 24);
74   key = (key + (key << 3)) + (key << 8);
75   key = key ^ (key >> 14);
76   key = (key + (key << 2)) + (key << 4);
77   key = key ^ (key >> 28);
78   key = key + (key << 31);
79   return key;
80 }
81
82 /*
83  * Thomas Wang downscaling hash function
84  */
85
86 inline uint32_t twang_32from64(uint64_t key) {
87   key = (~key) + (key << 18);
88   key = key ^ (key >> 31);
89   key = key * 21;
90   key = key ^ (key >> 11);
91   key = key + (key << 6);
92   key = key ^ (key >> 22);
93   return (uint32_t) key;
94 }
95
96 /*
97  * Robert Jenkins' reversible 32 bit mix hash function
98  */
99
100 inline uint32_t jenkins_rev_mix32(uint32_t key) {
101   key += (key << 12);
102   key ^= (key >> 22);
103   key += (key << 4);
104   key ^= (key >> 9);
105   key += (key << 10);
106   key ^= (key >> 2);
107   key += (key << 7);
108   key += (key << 12);
109   return key;
110 }
111
112 /*
113  * Fowler / Noll / Vo (FNV) Hash
114  *     http://www.isthe.com/chongo/tech/comp/fnv/
115  */
116
117 const uint32_t FNV_32_HASH_START = 216613626UL;
118 const uint64_t FNV_64_HASH_START = 14695981039346656037ULL;
119
120 inline uint32_t fnv32(const char* s,
121                       uint32_t hash = FNV_32_HASH_START) {
122   for (; *s; ++s) {
123     hash += (hash << 1) + (hash << 4) + (hash << 7) +
124             (hash << 8) + (hash << 24);
125     hash ^= *s;
126   }
127   return hash;
128 }
129
130 inline uint32_t fnv32_buf(const void* buf,
131                           int n,
132                           uint32_t hash = FNV_32_HASH_START) {
133   const char* char_buf = reinterpret_cast<const char*>(buf);
134
135   for (int i = 0; i < n; ++i) {
136     hash += (hash << 1) + (hash << 4) + (hash << 7) +
137             (hash << 8) + (hash << 24);
138     hash ^= char_buf[i];
139   }
140
141   return hash;
142 }
143
144 inline uint32_t fnv32(const std::string& str,
145                       uint64_t hash = FNV_32_HASH_START) {
146   return fnv32_buf(str.data(), str.size(), hash);
147 }
148
149 inline uint64_t fnv64(const char* s,
150                       uint64_t hash = FNV_64_HASH_START) {
151   for (; *s; ++s) {
152     hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
153       (hash << 8) + (hash << 40);
154     hash ^= *s;
155   }
156   return hash;
157 }
158
159 inline uint64_t fnv64_buf(const void* buf,
160                           int n,
161                           uint64_t hash = FNV_64_HASH_START) {
162   const char* char_buf = reinterpret_cast<const char*>(buf);
163
164   for (int i = 0; i < n; ++i) {
165     hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
166       (hash << 8) + (hash << 40);
167     hash ^= char_buf[i];
168   }
169   return hash;
170 }
171
172 inline uint64_t fnv64(const std::string& str,
173                       uint64_t hash = FNV_64_HASH_START) {
174   return fnv64_buf(str.data(), str.size(), hash);
175 }
176
177 /*
178  * Paul Hsieh: http://www.azillionmonkeys.com/qed/hash.html
179  */
180
181 #define get16bits(d) (*((const uint16_t*) (d)))
182
183 inline uint32_t hsieh_hash32_buf(const void* buf, int len) {
184   const char* s = reinterpret_cast<const char*>(buf);
185   uint32_t hash = len;
186   uint32_t tmp;
187   int rem;
188
189   if (len <= 0 || buf == 0) {
190     return 0;
191   }
192
193   rem = len & 3;
194   len >>= 2;
195
196   /* Main loop */
197   for (;len > 0; len--) {
198     hash  += get16bits (s);
199     tmp    = (get16bits (s+2) << 11) ^ hash;
200     hash   = (hash << 16) ^ tmp;
201     s  += 2*sizeof (uint16_t);
202     hash  += hash >> 11;
203   }
204
205   /* Handle end cases */
206   switch (rem) {
207   case 3:
208     hash += get16bits(s);
209     hash ^= hash << 16;
210     hash ^= s[sizeof (uint16_t)] << 18;
211     hash += hash >> 11;
212     break;
213   case 2:
214     hash += get16bits(s);
215     hash ^= hash << 11;
216     hash += hash >> 17;
217     break;
218   case 1:
219     hash += *s;
220     hash ^= hash << 10;
221     hash += hash >> 1;
222   }
223
224   /* Force "avalanching" of final 127 bits */
225   hash ^= hash << 3;
226   hash += hash >> 5;
227   hash ^= hash << 4;
228   hash += hash >> 17;
229   hash ^= hash << 25;
230   hash += hash >> 6;
231
232   return hash;
233 };
234
235 #undef get16bits
236
237 inline uint32_t hsieh_hash32(const char* s) {
238   return hsieh_hash32_buf(s, std::strlen(s));
239 }
240
241 inline uint32_t hsieh_hash32_str(const std::string& str) {
242   return hsieh_hash32_buf(str.data(), str.size());
243 }
244
245 //////////////////////////////////////////////////////////////////////
246
247 } // namespace hash
248
249 template<class Key>
250 struct hasher;
251
252 template<> struct hasher<int32_t> {
253   size_t operator()(int32_t key) const {
254     return hash::jenkins_rev_mix32(uint32_t(key));
255   }
256 };
257
258 template<> struct hasher<uint32_t> {
259   size_t operator()(uint32_t key) const {
260     return hash::jenkins_rev_mix32(key);
261   }
262 };
263
264 template<> struct hasher<int64_t> {
265   size_t operator()(int64_t key) const {
266     return hash::twang_mix64(uint64_t(key));
267   }
268 };
269
270 template<> struct hasher<uint64_t> {
271   size_t operator()(uint64_t key) const {
272     return hash::twang_mix64(key);
273   }
274 };
275
276 } // namespace folly
277
278 // Custom hash functions.
279 namespace std {
280   // Hash function for pairs. Requires default hash functions for both
281   // items in the pair.
282   template <typename T1, typename T2>
283   class hash<std::pair<T1, T2> > {
284   public:
285     size_t operator()(const std::pair<T1, T2>& x) const {
286       return folly::hash::hash_combine(x.first, x.second);
287     }
288   };
289
290   // Same as above, but for arbitrary tuples.
291   template <typename... Ts>
292   class hash<std::tuple<Ts...> > {
293   public:
294     size_t operator()(const Ts&... ts) const {
295       return folly::hash::hash_combine(ts...);
296     }
297   };
298 } // namespace std
299
300 #endif