proxygen
folly::hash Namespace Reference

Namespaces

 detail
 

Classes

class  SpookyHashV1
 
class  SpookyHashV2
 
class  StdHasher
 

Functions

uint64_t hash_128_to_64 (const uint64_t upper, const uint64_t lower) noexcept
 
uint64_t twang_mix64 (uint64_t key) noexcept
 
uint64_t twang_unmix64 (uint64_t key) noexcept
 
uint32_t twang_32from64 (uint64_t key) noexcept
 
uint32_t jenkins_rev_mix32 (uint32_t key) noexcept
 
uint32_t jenkins_rev_unmix32 (uint32_t key) noexcept
 
uint32_t fnv32 (const char *buf, uint32_t hash=FNV_32_HASH_START) noexcept
 
uint32_t fnv32_buf (const void *buf, size_t n, uint32_t hash=FNV_32_HASH_START) noexcept
 
uint32_t fnv32 (const std::string &str, uint32_t hash=FNV_32_HASH_START) noexcept
 
uint64_t fnv64 (const char *buf, uint64_t hash=FNV_64_HASH_START) noexcept
 
uint64_t fnv64_buf (const void *buf, size_t n, uint64_t hash=FNV_64_HASH_START) noexcept
 
uint64_t fnv64 (const std::string &str, uint64_t hash=FNV_64_HASH_START) noexcept
 
uint64_t fnva64_buf (const void *buf, size_t n, uint64_t hash=FNVA_64_HASH_START) noexcept
 
uint64_t fnva64 (const std::string &str, uint64_t hash=FNVA_64_HASH_START) noexcept
 
uint32_t hsieh_hash32_buf (const void *buf, size_t len) noexcept
 
uint32_t hsieh_hash32 (const char *s) noexcept
 
uint32_t hsieh_hash32_str (const std::string &str) noexcept
 
template<class Hash , class Value >
uint64_t commutative_hash_combine_value_generic (uint64_t seed, Hash const &hasher, Value const &value)
 
template<class Iter , class Hash = std::hash<typename std::iterator_traits<Iter>::value_type>>
uint64_t hash_range (Iter begin, Iter end, uint64_t hash=0, Hash hasher=Hash())
 
template<class Hash , class Iter >
uint64_t commutative_hash_combine_range_generic (uint64_t seed, Hash const &hasher, Iter first, Iter last)
 
template<class Iter >
uint64_t commutative_hash_combine_range (Iter first, Iter last)
 
template<class Hasher >
size_t hash_combine_generic (const Hasher &) noexcept
 
template<class Hasher , typename T , typename... Ts>
size_t hash_combine_generic (const Hasher &h, const T &t, const Ts &...ts) noexcept(noexcept(detail::c_array_size_t{h(t), h(ts)...}))
 
template<typename Hash , typename... Value>
uint64_t commutative_hash_combine_generic (uint64_t seed, Hash const &hasher, Value const &...value)
 
template<typename T , typename... Ts>
size_t hash_combine (const T &t, const Ts &...ts) noexcept(noexcept(hash_combine_generic(StdHasher{}, t, ts...)))
 
template<typename... Value>
uint64_t commutative_hash_combine (Value const &...value)
 

Variables

const uint32_t FNV_32_HASH_START = 2166136261UL
 
const uint64_t FNV_64_HASH_START = 14695981039346656037ULL
 
const uint64_t FNVA_64_HASH_START = 14695981039346656037ULL
 

Function Documentation

template<typename... Value>
uint64_t folly::hash::commutative_hash_combine ( Value const &...  value)

Definition at line 675 of file Hash.h.

References commutative_hash_combine_generic().

Referenced by TEST().

675  {
676  return commutative_hash_combine_generic(0, Hash{}, value...);
677 }
static const char *const value
Definition: Conv.cpp:50
uint64_t commutative_hash_combine_generic(uint64_t seed, Hash const &hasher, Value const &...value)
Definition: Hash.h:657
template<typename Hash , typename... Value>
uint64_t folly::hash::commutative_hash_combine_generic ( uint64_t  seed,
Hash const &  hasher,
Value const &...  value 
)

Definition at line 657 of file Hash.h.

References testing::_, commutative_hash_combine_value_generic(), seed, folly::T, and uint64_t.

Referenced by commutative_hash_combine().

660  {
661  // variadic foreach:
662  uint64_t _[] = {
664  (void)_;
665  return seed;
666 }
static const int seed
uint64_t commutative_hash_combine_value_generic(uint64_t seed, Hash const &hasher, Value const &value)
Definition: Hash.h:584
static const char *const value
Definition: Conv.cpp:50
const internal::AnythingMatcher _
template<class Iter >
uint64_t folly::hash::commutative_hash_combine_range ( Iter  first,
Iter  last 
)

Definition at line 624 of file Hash.h.

References commutative_hash_combine_range_generic(), and folly::gen::first.

Referenced by TEST().

624  {
625  return commutative_hash_combine_range_generic(0, Hash{}, first, last);
626 }
uint64_t commutative_hash_combine_range_generic(uint64_t seed, Hash const &hasher, Iter first, Iter last)
Definition: Hash.h:612
constexpr detail::First first
Definition: Base-inl.h:2553
template<class Hash , class Iter >
uint64_t folly::hash::commutative_hash_combine_range_generic ( uint64_t  seed,
Hash const &  hasher,
Iter  first,
Iter  last 
)

Definition at line 612 of file Hash.h.

References commutative_hash_combine_value_generic(), and seed.

Referenced by commutative_hash_combine_range(), and TEST().

616  {
617  while (first != last) {
619  }
620  return seed;
621 }
static const int seed
uint64_t commutative_hash_combine_value_generic(uint64_t seed, Hash const &hasher, Value const &value)
Definition: Hash.h:584
constexpr detail::First first
Definition: Base-inl.h:2553
template<class Hash , class Value >
uint64_t folly::hash::commutative_hash_combine_value_generic ( uint64_t  seed,
Hash const &  hasher,
Value const &  value 
)

Definition at line 584 of file Hash.h.

References gen_gtest_pred_impl::Iter(), twang_mix64(), and uint64_t.

Referenced by commutative_hash_combine_generic(), commutative_hash_combine_range_generic(), and TEST().

587  {
588  auto const x = hasher(value);
590  // Commutative accumulator taken from this paper:
591  // https://www.preprints.org/manuscript/201710.0192/v1/download
592  return 3860031 + (seed + y) * 2779 + (seed * y * 2);
593 }
Definition: InvokeTest.cpp:58
static const int seed
static const char *const value
Definition: Conv.cpp:50
uint64_t twang_mix64(uint64_t key) noexcept
Definition: Hash.h:49
Definition: InvokeTest.cpp:65
uint32_t folly::hash::fnv32 ( const char *  buf,
uint32_t  hash = FNV_32_HASH_START 
)
inlinenoexcept

Definition at line 149 of file Hash.h.

References s.

Referenced by TEST().

151  {
152  // forcing signed char, since other platforms can use unsigned
153  const signed char* s = reinterpret_cast<const signed char*>(buf);
154 
155  for (; *s; ++s) {
156  hash +=
157  (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
158  hash ^= *s;
159  }
160  return hash;
161 }
static set< string > s
uint32_t folly::hash::fnv32 ( const std::string str,
uint32_t  hash = FNV_32_HASH_START 
)
inlinenoexcept

Definition at line 179 of file Hash.h.

References fnv32_buf().

181  {
182  return fnv32_buf(str.data(), str.size(), hash);
183 }
uint32_t fnv32_buf(const void *buf, size_t n, uint32_t hash=FNV_32_HASH_START) noexcept
Definition: Hash.h:163
uint32_t folly::hash::fnv32_buf ( const void *  buf,
size_t  n,
uint32_t  hash = FNV_32_HASH_START 
)
inlinenoexcept

Definition at line 163 of file Hash.h.

References i.

Referenced by fnv32(), folly::IPAddressV4::hash(), and TEST().

166  {
167  // forcing signed char, since other platforms can use unsigned
168  const signed char* char_buf = reinterpret_cast<const signed char*>(buf);
169 
170  for (size_t i = 0; i < n; ++i) {
171  hash +=
172  (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
173  hash ^= char_buf[i];
174  }
175 
176  return hash;
177 }
uint64_t folly::hash::fnv64 ( const char *  buf,
uint64_t  hash = FNV_64_HASH_START 
)
inlinenoexcept

Definition at line 185 of file Hash.h.

References s.

Referenced by TEST().

187  {
188  // forcing signed char, since other platforms can use unsigned
189  const signed char* s = reinterpret_cast<const signed char*>(buf);
190 
191  for (; *s; ++s) {
192  hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
193  (hash << 8) + (hash << 40);
194  hash ^= *s;
195  }
196  return hash;
197 }
static set< string > s
uint64_t folly::hash::fnv64 ( const std::string str,
uint64_t  hash = FNV_64_HASH_START 
)
inlinenoexcept

Definition at line 214 of file Hash.h.

References fnv64_buf().

216  {
217  return fnv64_buf(str.data(), str.size(), hash);
218 }
uint64_t fnv64_buf(const void *buf, size_t n, uint64_t hash=FNV_64_HASH_START) noexcept
Definition: Hash.h:199
uint64_t folly::hash::fnv64_buf ( const void *  buf,
size_t  n,
uint64_t  hash = FNV_64_HASH_START 
)
inlinenoexcept

Definition at line 199 of file Hash.h.

References i.

Referenced by proxygen::RendezvousHash::computeHash(), fnv64(), folly::io::test::DataHolder::hash(), folly::io::test::hashIOBuf(), main(), detail::FNV64::operator()(), and TEST().

202  {
203  // forcing signed char, since other platforms can use unsigned
204  const signed char* char_buf = reinterpret_cast<const signed char*>(buf);
205 
206  for (size_t i = 0; i < n; ++i) {
207  hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
208  (hash << 8) + (hash << 40);
209  hash ^= char_buf[i];
210  }
211  return hash;
212 }
uint64_t folly::hash::fnva64 ( const std::string str,
uint64_t  hash = FNVA_64_HASH_START 
)
inlinenoexcept

Definition at line 234 of file Hash.h.

References fnva64_buf().

Referenced by TEST_P().

236  {
237  return fnva64_buf(str.data(), str.size(), hash);
238 }
uint64_t fnva64_buf(const void *buf, size_t n, uint64_t hash=FNVA_64_HASH_START) noexcept
Definition: Hash.h:220
uint64_t folly::hash::fnva64_buf ( const void *  buf,
size_t  n,
uint64_t  hash = FNVA_64_HASH_START 
)
inlinenoexcept

Definition at line 220 of file Hash.h.

References i, and uint8_t.

Referenced by fnva64(), and TEST_P().

223  {
224  const uint8_t* char_buf = reinterpret_cast<const uint8_t*>(buf);
225 
226  for (size_t i = 0; i < n; ++i) {
227  hash ^= char_buf[i];
228  hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
229  (hash << 8) + (hash << 40);
230  }
231  return hash;
232 }
uint64_t folly::hash::hash_128_to_64 ( const uint64_t  upper,
const uint64_t  lower 
)
inlinenoexcept

Definition at line 570 of file Hash.h.

References a, b, and uint64_t.

Referenced by hash_range(), folly::detail::integral_hasher< signed long long >::operator()(), and folly::Hash::operator()().

572  {
573  // Murmur-inspired hashing.
574  const uint64_t kMul = 0x9ddfea08eb382d69ULL;
575  uint64_t a = (lower ^ upper) * kMul;
576  a ^= (a >> 47);
577  uint64_t b = (upper ^ a) * kMul;
578  b ^= (b >> 47);
579  b *= kMul;
580  return b;
581 }
char b
char a
template<typename T , typename... Ts>
size_t folly::hash::hash_combine ( const T t,
const Ts &...  ts 
)
noexcept
template<class Hasher >
size_t folly::hash::hash_combine_generic ( const Hasher &  )
inlinenoexcept

Definition at line 634 of file Hash.h.

References folly::T.

Referenced by hash_combine(), and hash_combine_test().

634  {
635  return 0;
636 }
template<class Hasher , typename T , typename... Ts>
size_t folly::hash::hash_combine_generic ( const Hasher &  h,
const T t,
const Ts &...  ts 
)
noexcept

Definition at line 639 of file Hash.h.

References h, and folly::pushmi::detail::t.

642  {h(t),
643  h(ts)...})) {
644  size_t seed = h(t);
645  if (sizeof...(ts) == 0) {
646  return seed;
647  }
648  size_t remainder = hash_combine_generic(h, ts...);
649  if /* constexpr */ (sizeof(size_t) == sizeof(uint32_t)) {
650  return twang_32from64((uint64_t(seed) << 32) | remainder);
651  } else {
652  return static_cast<size_t>(hash_128_to_64(seed, remainder));
653  }
654 }
*than *hazptr_holder h
Definition: Hazptr.h:116
uint64_t hash_128_to_64(const uint64_t upper, const uint64_t lower) noexcept
Definition: Hash.h:570
size_t hash_combine_generic(const Hasher &h, const T &t, const Ts &...ts) noexcept(noexcept(detail::c_array_size_t{h(t), h(ts)...}))
Definition: Hash.h:639
static const int seed
uint32_t twang_32from64(uint64_t key) noexcept
Definition: Hash.h:83
template<class Iter , class Hash = std::hash<typename std::iterator_traits<Iter>::value_type>>
uint64_t folly::hash::hash_range ( Iter  begin,
Iter  end,
uint64_t  hash = 0,
Hash  hasher = Hash() 
)

Definition at line 604 of file Hash.h.

References folly::test::begin(), folly::test::end(), and hash_128_to_64().

Referenced by folly::dynamic::hash(), std::hash< StringVector >::operator()(), and TEST().

604  {
605  for (; begin != end; ++begin) {
606  hash = hash_128_to_64(hash, hasher(*begin));
607  }
608  return hash;
609 }
uint64_t hash_128_to_64(const uint64_t upper, const uint64_t lower) noexcept
Definition: Hash.h:570
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
uint32_t folly::hash::hsieh_hash32 ( const char *  s)
inlinenoexcept

Definition at line 301 of file Hash.h.

References hsieh_hash32_buf(), and s.

Referenced by TEST().

301  {
302  return hsieh_hash32_buf(s, std::strlen(s));
303 }
uint32_t hsieh_hash32_buf(const void *buf, size_t len) noexcept
Definition: Hash.h:246
static set< string > s
uint32_t folly::hash::hsieh_hash32_buf ( const void *  buf,
size_t  len 
)
inlinenoexcept

Definition at line 246 of file Hash.h.

References get16bits, s, uint16_t, and uint32_t.

Referenced by hsieh_hash32(), hsieh_hash32_str(), and TEST().

246  {
247  // forcing signed char, since other platforms can use unsigned
248  const unsigned char* s = reinterpret_cast<const unsigned char*>(buf);
249  uint32_t hash = static_cast<uint32_t>(len);
250  uint32_t tmp;
251  size_t rem;
252 
253  if (len <= 0 || buf == nullptr) {
254  return 0;
255  }
256 
257  rem = len & 3;
258  len >>= 2;
259 
260  /* Main loop */
261  for (; len > 0; len--) {
262  hash += get16bits(s);
263  tmp = (get16bits(s + 2) << 11) ^ hash;
264  hash = (hash << 16) ^ tmp;
265  s += 2 * sizeof(uint16_t);
266  hash += hash >> 11;
267  }
268 
269  /* Handle end cases */
270  switch (rem) {
271  case 3:
272  hash += get16bits(s);
273  hash ^= hash << 16;
274  hash ^= s[sizeof(uint16_t)] << 18;
275  hash += hash >> 11;
276  break;
277  case 2:
278  hash += get16bits(s);
279  hash ^= hash << 11;
280  hash += hash >> 17;
281  break;
282  case 1:
283  hash += *s;
284  hash ^= hash << 10;
285  hash += hash >> 1;
286  }
287 
288  /* Force "avalanching" of final 127 bits */
289  hash ^= hash << 3;
290  hash += hash >> 5;
291  hash ^= hash << 4;
292  hash += hash >> 17;
293  hash ^= hash << 25;
294  hash += hash >> 6;
295 
296  return hash;
297 }
#define get16bits(d)
Definition: Hash.h:244
static set< string > s
uint32_t folly::hash::hsieh_hash32_str ( const std::string str)
inlinenoexcept

Definition at line 305 of file Hash.h.

References hsieh_hash32_buf().

305  {
306  return hsieh_hash32_buf(str.data(), str.size());
307 }
uint32_t hsieh_hash32_buf(const void *buf, size_t len) noexcept
Definition: Hash.h:246
uint32_t folly::hash::jenkins_rev_mix32 ( uint32_t  key)
inlinenoexcept

Definition at line 97 of file Hash.h.

Referenced by createEntry(), folly::detail::integral_hasher< signed long long >::operator()(), and TEST().

97  {
98  key += (key << 12); // key *= (1 + (1 << 12))
99  key ^= (key >> 22);
100  key += (key << 4); // key *= (1 + (1 << 4))
101  key ^= (key >> 9);
102  key += (key << 10); // key *= (1 + (1 << 10))
103  key ^= (key >> 2);
104  // key *= (1 + (1 << 7)) * (1 + (1 << 12))
105  key += (key << 7);
106  key += (key << 12);
107  return key;
108 }
uint32_t folly::hash::jenkins_rev_unmix32 ( uint32_t  key)
inlinenoexcept

Definition at line 117 of file Hash.h.

Referenced by TEST().

117  {
118  // These are the modular multiplicative inverses (in Z_2^32) of the
119  // multiplication factors in jenkins_rev_mix32, in reverse order. They were
120  // computed using the Extended Euclidean algorithm, see
121  // http://en.wikipedia.org/wiki/Modular_multiplicative_inverse
122  key *= 2364026753U;
123 
124  // The inverse of a ^= (a >> n) is
125  // b = a
126  // for (int i = n; i < 32; i += n) {
127  // b ^= (a >> i);
128  // }
129  key ^= (key >> 2) ^ (key >> 4) ^ (key >> 6) ^ (key >> 8) ^ (key >> 10) ^
130  (key >> 12) ^ (key >> 14) ^ (key >> 16) ^ (key >> 18) ^ (key >> 20) ^
131  (key >> 22) ^ (key >> 24) ^ (key >> 26) ^ (key >> 28) ^ (key >> 30);
132  key *= 3222273025U;
133  key ^= (key >> 9) ^ (key >> 18) ^ (key >> 27);
134  key *= 4042322161U;
135  key ^= (key >> 22);
136  key *= 16773121U;
137  return key;
138 }
uint32_t folly::hash::twang_32from64 ( uint64_t  key)
inlinenoexcept

Definition at line 83 of file Hash.h.

References uint32_t.

Referenced by folly::HashingThreadId::get(), and TEST().

83  {
84  key = (~key) + (key << 18);
85  key = key ^ (key >> 31);
86  key = key * 21;
87  key = key ^ (key >> 11);
88  key = key + (key << 6);
89  key = key ^ (key >> 22);
90  return (uint32_t)key;
91 }
uint64_t folly::hash::twang_mix64 ( uint64_t  key)
inlinenoexcept

Definition at line 49 of file Hash.h.

Referenced by commutative_hash_combine_value_generic(), proxygen::RendezvousHash::computeHash(), folly::detail::MemoryIdler::getVariationTimeout(), folly::SocketAddress::hash(), ShardedAtomicInt::inc(), folly::detail::integral_hasher< signed long long >::operator()(), folly::detail::float_hasher< float >::operator()(), folly::ParkingLot< Data >::park_until(), TEST(), and folly::ParkingLot< Data >::unpark().

49  {
50  key = (~key) + (key << 21); // key *= (1 << 21) - 1; key -= 1;
51  key = key ^ (key >> 24);
52  key = key + (key << 3) + (key << 8); // key *= 1 + (1 << 3) + (1 << 8)
53  key = key ^ (key >> 14);
54  key = key + (key << 2) + (key << 4); // key *= 1 + (1 << 2) + (1 << 4)
55  key = key ^ (key >> 28);
56  key = key + (key << 31); // key *= 1 + (1 << 31)
57  return key;
58 }
uint64_t folly::hash::twang_unmix64 ( uint64_t  key)
inlinenoexcept

Definition at line 66 of file Hash.h.

Referenced by TEST().

66  {
67  // See the comments in jenkins_rev_unmix32 for an explanation as to how this
68  // was generated
69  key *= 4611686016279904257U;
70  key ^= (key >> 28) ^ (key >> 56);
71  key *= 14933078535860113213U;
72  key ^= (key >> 14) ^ (key >> 28) ^ (key >> 42) ^ (key >> 56);
73  key *= 15244667743933553977U;
74  key ^= (key >> 24) ^ (key >> 48);
75  key = (key + 1) * 9223367638806167551U;
76  return key;
77 }

Variable Documentation

const uint32_t folly::hash::FNV_32_HASH_START = 2166136261UL

Definition at line 145 of file Hash.h.

const uint64_t folly::hash::FNV_64_HASH_START = 14695981039346656037ULL

Definition at line 146 of file Hash.h.

Referenced by folly::io::test::hashIOBuf().

const uint64_t folly::hash::FNVA_64_HASH_START = 14695981039346656037ULL

Definition at line 147 of file Hash.h.