2 * Copyright 2012 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_BASE_HASH_H_
18 #define FOLLY_BASE_HASH_H_
26 * Various hashing functions.
29 namespace folly { namespace hash {
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.
36 // Never used, but gcc demands it.
37 inline size_t hash_combine() {
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;
49 size_t b = (upper ^ a) * kMul;
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) {
61 size_t remainder = hash_combine(ts...);
62 return hash_128_to_64(seed, remainder);
65 //////////////////////////////////////////////////////////////////////
68 * Thomas Wang 64 bit mix hash function
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);
83 * Thomas Wang downscaling hash function
86 inline uint32_t twang_32from64(uint64_t key) {
87 key = (~key) + (key << 18);
88 key = key ^ (key >> 31);
90 key = key ^ (key >> 11);
91 key = key + (key << 6);
92 key = key ^ (key >> 22);
93 return (uint32_t) key;
97 * Robert Jenkins' reversible 32 bit mix hash function
100 inline uint32_t jenkins_rev_mix32(uint32_t key) {
113 * Fowler / Noll / Vo (FNV) Hash
114 * http://www.isthe.com/chongo/tech/comp/fnv/
117 const uint32_t FNV_32_HASH_START = 216613626UL;
118 const uint64_t FNV_64_HASH_START = 14695981039346656037ULL;
120 inline uint32_t fnv32(const char* s,
121 uint32_t hash = FNV_32_HASH_START) {
123 hash += (hash << 1) + (hash << 4) + (hash << 7) +
124 (hash << 8) + (hash << 24);
130 inline uint32_t fnv32_buf(const void* buf,
132 uint32_t hash = FNV_32_HASH_START) {
133 const char* char_buf = reinterpret_cast<const char*>(buf);
135 for (int i = 0; i < n; ++i) {
136 hash += (hash << 1) + (hash << 4) + (hash << 7) +
137 (hash << 8) + (hash << 24);
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);
149 inline uint64_t fnv64(const char* s,
150 uint64_t hash = FNV_64_HASH_START) {
152 hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
153 (hash << 8) + (hash << 40);
159 inline uint64_t fnv64_buf(const void* buf,
161 uint64_t hash = FNV_64_HASH_START) {
162 const char* char_buf = reinterpret_cast<const char*>(buf);
164 for (int i = 0; i < n; ++i) {
165 hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
166 (hash << 8) + (hash << 40);
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);
178 * Paul Hsieh: http://www.azillionmonkeys.com/qed/hash.html
181 #define get16bits(d) (*((const uint16_t*) (d)))
183 inline uint32_t hsieh_hash32_buf(const void* buf, int len) {
184 const char* s = reinterpret_cast<const char*>(buf);
189 if (len <= 0 || buf == 0) {
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);
205 /* Handle end cases */
208 hash += get16bits(s);
210 hash ^= s[sizeof (uint16_t)] << 18;
214 hash += get16bits(s);
224 /* Force "avalanching" of final 127 bits */
237 inline uint32_t hsieh_hash32(const char* s) {
238 return hsieh_hash32_buf(s, std::strlen(s));
241 inline uint32_t hsieh_hash32_str(const std::string& str) {
242 return hsieh_hash32_buf(str.data(), str.size());
245 //////////////////////////////////////////////////////////////////////
252 template<> struct hasher<int32_t> {
253 size_t operator()(int32_t key) const {
254 return hash::jenkins_rev_mix32(uint32_t(key));
258 template<> struct hasher<uint32_t> {
259 size_t operator()(uint32_t key) const {
260 return hash::jenkins_rev_mix32(key);
264 template<> struct hasher<int64_t> {
265 size_t operator()(int64_t key) const {
266 return hash::twang_mix64(uint64_t(key));
270 template<> struct hasher<uint64_t> {
271 size_t operator()(uint64_t key) const {
272 return hash::twang_mix64(key);
278 // Custom hash functions.
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> > {
285 size_t operator()(const std::pair<T1, T2>& x) const {
286 return folly::hash::hash_combine(x.first, x.second);
290 // Same as above, but for arbitrary tuples.
291 template <typename... Ts>
292 class hash<std::tuple<Ts...> > {
294 size_t operator()(const Ts&... ts) const {
295 return folly::hash::hash_combine(ts...);