1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___FUNCTIONAL_HASH_H
10#define _LIBCPP___FUNCTIONAL_HASH_H
11
12#include <__config>
13#include <__cstddef/nullptr_t.h>
14#include <__functional/unary_function.h>
15#include <__fwd/functional.h>
16#include <__memory/addressof.h>
17#include <__type_traits/conjunction.h>
18#include <__type_traits/enable_if.h>
19#include <__type_traits/invoke.h>
20#include <__type_traits/is_constructible.h>
21#include <__type_traits/is_enum.h>
22#include <__type_traits/is_floating_point.h>
23#include <__type_traits/is_integral.h>
24#include <__type_traits/is_unqualified.h>
25#include <__type_traits/underlying_type.h>
26#include <__utility/swap.h>
27#include <cstdint>
28#include <cstring>
29
30#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31# pragma GCC system_header
32#endif
33
34_LIBCPP_BEGIN_NAMESPACE_STD
35
36template <class _Size>
37inline _LIBCPP_HIDE_FROM_ABI _Size __loadword(const void* __p) {
38 _Size __r;
39 std::memcpy(dest: std::addressof(__r), src: __p, n: sizeof(__r));
40 return __r;
41}
42
43struct _PairT {
44 size_t first;
45 size_t second;
46};
47
48// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
49// is 64 bits. This is because cityhash64 uses 64bit x 64bit
50// multiplication, which can be very slow on 32-bit systems.
51template <class _Size, size_t = sizeof(_Size) * __CHAR_BIT__>
52struct __murmur2_or_cityhash;
53
54template <class _Size>
55struct __murmur2_or_cityhash<_Size, 32> {
56 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK _Size
57 operator()(const void* __key, _Size __len) const {
58 // murmur2
59 const _Size __m = 0x5bd1e995;
60 const _Size __r = 24;
61 _Size __h = __len;
62 const unsigned char* __data = static_cast<const unsigned char*>(__key);
63 for (; __len >= 4; __data += 4, __len -= 4) {
64 _Size __k = std::__loadword<_Size>(__data);
65 __k *= __m;
66 __k ^= __k >> __r;
67 __k *= __m;
68 __h *= __m;
69 __h ^= __k;
70 }
71 switch (__len) {
72 case 3:
73 __h ^= static_cast<_Size>(__data[2] << 16);
74 [[__fallthrough__]];
75 case 2:
76 __h ^= static_cast<_Size>(__data[1] << 8);
77 [[__fallthrough__]];
78 case 1:
79 __h ^= __data[0];
80 __h *= __m;
81 }
82 __h ^= __h >> 13;
83 __h *= __m;
84 __h ^= __h >> 15;
85 return __h;
86 }
87};
88
89template <class _Size>
90struct __murmur2_or_cityhash<_Size, 64> {
91 // cityhash64
92 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK _Size
93 operator()(const void* __key, _Size __len) const {
94 const char* __s = static_cast<const char*>(__key);
95 if (__len <= 32) {
96 if (__len <= 16) {
97 return __hash_len_0_to_16(__s, __len);
98 } else {
99 return __hash_len_17_to_32(__s, __len);
100 }
101 } else if (__len <= 64) {
102 return __hash_len_33_to_64(__s, __len);
103 }
104
105 // For strings over 64 bytes we hash the end first, and then as we
106 // loop we keep 56 bytes of state: v, w, x, y, and z.
107 _Size __x = std::__loadword<_Size>(__s + __len - 40);
108 _Size __y = std::__loadword<_Size>(__s + __len - 16) + std::__loadword<_Size>(__s + __len - 56);
109 _Size __z =
110 __hash_len_16(u: std::__loadword<_Size>(__s + __len - 48) + __len, v: std::__loadword<_Size>(__s + __len - 24));
111 _PairT __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
112 _PairT __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
113 __x = __x * __k1 + std::__loadword<_Size>(__s);
114
115 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
116 __len = (__len - 1) & ~static_cast<_Size>(63);
117 do {
118 __x = __rotate(val: __x + __y + __v.first + std::__loadword<_Size>(__s + 8), shift: 37) * __k1;
119 __y = __rotate(val: __y + __v.second + std::__loadword<_Size>(__s + 48), shift: 42) * __k1;
120 __x ^= __w.second;
121 __y += __v.first + std::__loadword<_Size>(__s + 40);
122 __z = __rotate(val: __z + __w.first, shift: 33) * __k1;
123 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
124 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second, __y + std::__loadword<_Size>(__s + 16));
125 std::swap(__z, __x);
126 __s += 64;
127 __len -= 64;
128 } while (__len != 0);
129 return __hash_len_16(u: __hash_len_16(u: __v.first, v: __w.first) + __shift_mix(val: __y) * __k1 + __z,
130 v: __hash_len_16(u: __v.second, v: __w.second) + __x);
131 }
132
133private:
134 // Some primes between 2^63 and 2^64.
135 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
136 static const _Size __k1 = 0xb492b66fbe98f273ULL;
137 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
138 static const _Size __k3 = 0xc949d7c7509e6557ULL;
139
140 _LIBCPP_HIDE_FROM_ABI static _Size __rotate(_Size __val, int __shift) {
141 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
142 }
143
144 _LIBCPP_HIDE_FROM_ABI static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
145 return (__val >> __shift) | (__val << (64 - __shift));
146 }
147
148 _LIBCPP_HIDE_FROM_ABI static _Size __shift_mix(_Size __val) { return __val ^ (__val >> 47); }
149
150 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static _Size __hash_len_16(_Size __u, _Size __v) {
151 const _Size __mul = 0x9ddfea08eb382d69ULL;
152 _Size __a = (__u ^ __v) * __mul;
153 __a ^= (__a >> 47);
154 _Size __b = (__v ^ __a) * __mul;
155 __b ^= (__b >> 47);
156 __b *= __mul;
157 return __b;
158 }
159
160 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static _Size
161 __hash_len_0_to_16(const char* __s, _Size __len) {
162 if (__len > 8) {
163 const _Size __a = std::__loadword<_Size>(__s);
164 const _Size __b = std::__loadword<_Size>(__s + __len - 8);
165 return __hash_len_16(u: __a, v: __rotate_by_at_least_1(val: __b + __len, shift: __len)) ^ __b;
166 }
167 if (__len >= 4) {
168 const uint32_t __a = std::__loadword<uint32_t>(p: __s);
169 const uint32_t __b = std::__loadword<uint32_t>(__s + __len - 4);
170#ifdef _LIBCPP_ABI_FIX_CITYHASH_IMPLEMENTATION
171 return __hash_len_16(__len + (static_cast<_Size>(__a) << 3), __b);
172#else
173 return __hash_len_16(u: __len + (__a << 3), v: __b);
174#endif
175 }
176 if (__len > 0) {
177 const unsigned char __a = static_cast<unsigned char>(__s[0]);
178 const unsigned char __b = static_cast<unsigned char>(__s[__len >> 1]);
179 const unsigned char __c = static_cast<unsigned char>(__s[__len - 1]);
180 const uint32_t __y = static_cast<uint32_t>(__a) + (static_cast<uint32_t>(__b) << 8);
181 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
182 return __shift_mix(val: __y * __k2 ^ __z * __k3) * __k2;
183 }
184 return __k2;
185 }
186
187 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static _Size
188 __hash_len_17_to_32(const char* __s, _Size __len) {
189 const _Size __a = std::__loadword<_Size>(__s) * __k1;
190 const _Size __b = std::__loadword<_Size>(__s + 8);
191 const _Size __c = std::__loadword<_Size>(__s + __len - 8) * __k2;
192 const _Size __d = std::__loadword<_Size>(__s + __len - 16) * __k0;
193 return __hash_len_16(
194 u: __rotate(val: __a - __b, shift: 43) + __rotate(val: __c, shift: 30) + __d, v: __a + __rotate(val: __b ^ __k3, shift: 20) - __c + __len);
195 }
196
197 // Return a 16-byte hash for 48 bytes. Quick and dirty.
198 // Callers do best to use "random-looking" values for a and b.
199 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static _PairT
200 __weak_hash_len_32_with_seeds(_Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
201 __a += __w;
202 __b = __rotate(val: __b + __a + __z, shift: 21);
203 const _Size __c = __a;
204 __a += __x;
205 __a += __y;
206 __b += __rotate(val: __a, shift: 44);
207 _PairT __ret;
208 __ret.first = __a + __z;
209 __ret.second = __b + __c;
210 return __ret;
211 }
212
213 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
214 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static _PairT
215 __weak_hash_len_32_with_seeds(const char* __s, _Size __a, _Size __b) {
216 return __weak_hash_len_32_with_seeds(
217 std::__loadword<_Size>(__s),
218 std::__loadword<_Size>(__s + 8),
219 std::__loadword<_Size>(__s + 16),
220 std::__loadword<_Size>(__s + 24),
221 __a,
222 __b);
223 }
224
225 // Return an 8-byte hash for 33 to 64 bytes.
226 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static _Size
227 __hash_len_33_to_64(const char* __s, size_t __len) {
228 _Size __z = std::__loadword<_Size>(__s + 24);
229 _Size __a = std::__loadword<_Size>(__s) + (__len + std::__loadword<_Size>(__s + __len - 16)) * __k0;
230 _Size __b = __rotate(val: __a + __z, shift: 52);
231 _Size __c = __rotate(val: __a, shift: 37);
232 __a += std::__loadword<_Size>(__s + 8);
233 __c += __rotate(val: __a, shift: 7);
234 __a += std::__loadword<_Size>(__s + 16);
235 _Size __vf = __a + __z;
236 _Size __vs = __b + __rotate(val: __a, shift: 31) + __c;
237 __a = std::__loadword<_Size>(__s + 16) + std::__loadword<_Size>(__s + __len - 32);
238 __z += std::__loadword<_Size>(__s + __len - 8);
239 __b = __rotate(val: __a + __z, shift: 52);
240 __c = __rotate(val: __a, shift: 37);
241 __a += std::__loadword<_Size>(__s + __len - 24);
242 __c += __rotate(val: __a, shift: 7);
243 __a += std::__loadword<_Size>(__s + __len - 16);
244 _Size __wf = __a + __z;
245 _Size __ws = __b + __rotate(val: __a, shift: 31) + __c;
246 _Size __r = __shift_mix(val: (__vf + __ws) * __k2 + (__wf + __vs) * __k0);
247 return __shift_mix(val: __r * __k0 + __vs) * __k2;
248 }
249};
250
251#if _LIBCPP_AVAILABILITY_HAS_HASH_MEMORY
252_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
253[[__gnu__::__pure__]] _LIBCPP_EXPORTED_FROM_ABI size_t __hash_memory(_LIBCPP_NOESCAPE const void*, size_t) _NOEXCEPT;
254_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
255#else
256_LIBCPP_HIDE_FROM_ABI inline size_t __hash_memory(const void* __ptr, size_t __size) _NOEXCEPT {
257 return __murmur2_or_cityhash<size_t>()(__ptr, __size);
258}
259#endif
260
261template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
262struct __scalar_hash;
263
264template <class _Tp>
265struct __scalar_hash<_Tp, 0> : public __unary_function<_Tp, size_t> {
266 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
267 union {
268 _Tp __t;
269 size_t __a;
270 } __u;
271 __u.__a = 0;
272 __u.__t = __v;
273 return __u.__a;
274 }
275};
276
277template <class _Tp>
278struct __scalar_hash<_Tp, 1> : public __unary_function<_Tp, size_t> {
279 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
280 union {
281 _Tp __t;
282 size_t __a;
283 } __u;
284 __u.__t = __v;
285 return __u.__a;
286 }
287};
288
289template <class _Tp>
290struct __scalar_hash<_Tp, 2> : public __unary_function<_Tp, size_t> {
291 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
292 union {
293 _Tp __t;
294 struct {
295 size_t __a;
296 size_t __b;
297 } __s;
298 } __u;
299 __u.__t = __v;
300 return std::__hash_memory(std::addressof(__u), sizeof(__u));
301 }
302};
303
304template <class _Tp>
305struct __scalar_hash<_Tp, 3> : public __unary_function<_Tp, size_t> {
306 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
307 union {
308 _Tp __t;
309 struct {
310 size_t __a;
311 size_t __b;
312 size_t __c;
313 } __s;
314 } __u;
315 __u.__t = __v;
316 return std::__hash_memory(std::addressof(__u), sizeof(__u));
317 }
318};
319
320template <class _Tp>
321struct __scalar_hash<_Tp, 4> : public __unary_function<_Tp, size_t> {
322 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
323 union {
324 _Tp __t;
325 struct {
326 size_t __a;
327 size_t __b;
328 size_t __c;
329 size_t __d;
330 } __s;
331 } __u;
332 __u.__t = __v;
333 return std::__hash_memory(std::addressof(__u), sizeof(__u));
334 }
335};
336
337_LIBCPP_HIDE_FROM_ABI inline size_t __hash_combine(size_t __lhs, size_t __rhs) _NOEXCEPT {
338 typedef __scalar_hash<_PairT> _HashT;
339 const _PairT __p = {.first: __lhs, .second: __rhs};
340 return _HashT()(__p);
341}
342
343template <class _Tp>
344struct hash<_Tp*> : public __unary_function<_Tp*, size_t> {
345 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp* __v) const _NOEXCEPT {
346 union {
347 _Tp* __t;
348 size_t __a;
349 } __u;
350 __u.__t = __v;
351 return std::__hash_memory(std::addressof(__u), sizeof(__u));
352 }
353};
354
355template <class _Tp, class = void>
356struct __hash_impl {
357 __hash_impl() = delete;
358 __hash_impl(__hash_impl const&) = delete;
359 __hash_impl& operator=(__hash_impl const&) = delete;
360};
361
362template <class _Tp>
363struct __hash_impl<_Tp, __enable_if_t<is_enum<_Tp>::value && __is_unqualified_v<_Tp> > >
364 : __unary_function<_Tp, size_t> {
365 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
366 using type = __underlying_type_t<_Tp>;
367 return hash<type>()(static_cast<type>(__v));
368 }
369};
370
371template <class _Tp>
372struct __hash_impl<
373 _Tp,
374 __enable_if_t<is_integral<_Tp>::value && __is_unqualified_v<_Tp> && (sizeof(_Tp) <= sizeof(size_t))> >
375 : __unary_function<_Tp, size_t> {
376 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
377};
378
379template <class _Tp>
380struct __hash_impl<_Tp,
381 __enable_if_t<is_integral<_Tp>::value && __is_unqualified_v<_Tp> && (sizeof(_Tp) > sizeof(size_t))> >
382 : __scalar_hash<_Tp> {};
383
384template <class _Tp>
385struct __hash_impl<_Tp, __enable_if_t<is_floating_point<_Tp>::value && __is_unqualified_v<_Tp> > >
386 : __scalar_hash<_Tp> {
387 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
388 // -0.0 and 0.0 should return same hash
389 if (__v == 0.0f)
390 return 0;
391 return __scalar_hash<_Tp>::operator()(__v);
392 }
393};
394
395template <>
396struct __hash_impl<long double> : __scalar_hash<long double> {
397 _LIBCPP_HIDE_FROM_ABI size_t operator()(long double __v) const _NOEXCEPT {
398 // -0.0 and 0.0 should return same hash
399 if (__v == 0.0L)
400 return 0;
401#if defined(__i386__) || (defined(__x86_64__) && defined(__ILP32__))
402 // Zero out padding bits
403 union {
404 long double __t;
405 struct {
406 size_t __a;
407 size_t __b;
408 size_t __c;
409 size_t __d;
410 } __s;
411 } __u;
412 __u.__s.__a = 0;
413 __u.__s.__b = 0;
414 __u.__s.__c = 0;
415 __u.__s.__d = 0;
416 __u.__t = __v;
417 return __u.__s.__a ^ __u.__s.__b ^ __u.__s.__c ^ __u.__s.__d;
418#elif defined(__x86_64__)
419 // Zero out padding bits
420 union {
421 long double __t;
422 struct {
423 size_t __a;
424 size_t __b;
425 } __s;
426 } __u;
427 __u.__s.__a = 0;
428 __u.__s.__b = 0;
429 __u.__t = __v;
430 return __u.__s.__a ^ __u.__s.__b;
431#else
432 return __scalar_hash<long double>::operator()(__v);
433#endif
434 }
435};
436
437template <class _Tp>
438struct hash : public __hash_impl<_Tp> {};
439
440template <>
441struct hash<nullptr_t> : public __unary_function<nullptr_t, size_t> {
442 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t operator()(nullptr_t) const _NOEXCEPT { return 662607004ull; }
443};
444
445#ifndef _LIBCPP_CXX03_LANG
446template <class _Key, class _Hash>
447using __check_hash_requirements _LIBCPP_NODEBUG =
448 integral_constant<bool,
449 is_copy_constructible<_Hash>::value && is_move_constructible<_Hash>::value &&
450 __is_invocable_r_v<size_t, _Hash, _Key const&> >;
451
452template <class _Key, class _Hash = hash<_Key> >
453using __has_enabled_hash _LIBCPP_NODEBUG =
454 integral_constant<bool, __check_hash_requirements<_Key, _Hash>::value && is_default_constructible<_Hash>::value >;
455
456template <class _Type, class>
457using __enable_hash_helper_imp _LIBCPP_NODEBUG = _Type;
458
459template <class _Type, class... _Keys>
460using __enable_hash_helper _LIBCPP_NODEBUG =
461 __enable_hash_helper_imp<_Type, __enable_if_t<__all<__has_enabled_hash<_Keys>::value...>::value> >;
462#endif // !_LIBCPP_CXX03_LANG
463
464_LIBCPP_END_NAMESPACE_STD
465
466#endif // _LIBCPP___FUNCTIONAL_HASH_H
467