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[[__gnu__::__pure__]] _LIBCPP_EXPORTED_FROM_ABI size_t __hash_memory(_LIBCPP_NOESCAPE const void*, size_t) _NOEXCEPT;
253#else
254_LIBCPP_HIDE_FROM_ABI inline size_t __hash_memory(const void* __ptr, size_t __size) _NOEXCEPT {
255 return __murmur2_or_cityhash<size_t>()(__ptr, __size);
256}
257#endif
258
259template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
260struct __scalar_hash;
261
262template <class _Tp>
263struct __scalar_hash<_Tp, 0> : public __unary_function<_Tp, size_t> {
264 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
265 union {
266 _Tp __t;
267 size_t __a;
268 } __u;
269 __u.__a = 0;
270 __u.__t = __v;
271 return __u.__a;
272 }
273};
274
275template <class _Tp>
276struct __scalar_hash<_Tp, 1> : public __unary_function<_Tp, size_t> {
277 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
278 union {
279 _Tp __t;
280 size_t __a;
281 } __u;
282 __u.__t = __v;
283 return __u.__a;
284 }
285};
286
287template <class _Tp>
288struct __scalar_hash<_Tp, 2> : public __unary_function<_Tp, size_t> {
289 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
290 union {
291 _Tp __t;
292 struct {
293 size_t __a;
294 size_t __b;
295 } __s;
296 } __u;
297 __u.__t = __v;
298 return std::__hash_memory(std::addressof(__u), sizeof(__u));
299 }
300};
301
302template <class _Tp>
303struct __scalar_hash<_Tp, 3> : public __unary_function<_Tp, size_t> {
304 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
305 union {
306 _Tp __t;
307 struct {
308 size_t __a;
309 size_t __b;
310 size_t __c;
311 } __s;
312 } __u;
313 __u.__t = __v;
314 return std::__hash_memory(std::addressof(__u), sizeof(__u));
315 }
316};
317
318template <class _Tp>
319struct __scalar_hash<_Tp, 4> : public __unary_function<_Tp, size_t> {
320 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
321 union {
322 _Tp __t;
323 struct {
324 size_t __a;
325 size_t __b;
326 size_t __c;
327 size_t __d;
328 } __s;
329 } __u;
330 __u.__t = __v;
331 return std::__hash_memory(std::addressof(__u), sizeof(__u));
332 }
333};
334
335_LIBCPP_HIDE_FROM_ABI inline size_t __hash_combine(size_t __lhs, size_t __rhs) _NOEXCEPT {
336 typedef __scalar_hash<_PairT> _HashT;
337 const _PairT __p = {.first: __lhs, .second: __rhs};
338 return _HashT()(__p);
339}
340
341template <class _Tp>
342struct hash<_Tp*> : public __unary_function<_Tp*, size_t> {
343 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp* __v) const _NOEXCEPT {
344 union {
345 _Tp* __t;
346 size_t __a;
347 } __u;
348 __u.__t = __v;
349 return std::__hash_memory(std::addressof(__u), sizeof(__u));
350 }
351};
352
353template <class _Tp, class = void>
354struct __hash_impl {
355 __hash_impl() = delete;
356 __hash_impl(__hash_impl const&) = delete;
357 __hash_impl& operator=(__hash_impl const&) = delete;
358};
359
360template <class _Tp>
361struct __hash_impl<_Tp, __enable_if_t<is_enum<_Tp>::value && __is_unqualified_v<_Tp> > >
362 : __unary_function<_Tp, size_t> {
363 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
364 using type = __underlying_type_t<_Tp>;
365 return hash<type>()(static_cast<type>(__v));
366 }
367};
368
369template <class _Tp>
370struct __hash_impl<
371 _Tp,
372 __enable_if_t<is_integral<_Tp>::value && __is_unqualified_v<_Tp> && (sizeof(_Tp) <= sizeof(size_t))> >
373 : __unary_function<_Tp, size_t> {
374 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
375};
376
377template <class _Tp>
378struct __hash_impl<_Tp,
379 __enable_if_t<is_integral<_Tp>::value && __is_unqualified_v<_Tp> && (sizeof(_Tp) > sizeof(size_t))> >
380 : __scalar_hash<_Tp> {};
381
382template <class _Tp>
383struct __hash_impl<_Tp, __enable_if_t<is_floating_point<_Tp>::value && __is_unqualified_v<_Tp> > >
384 : __scalar_hash<_Tp> {
385 _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
386 // -0.0 and 0.0 should return same hash
387 if (__v == 0.0f)
388 return 0;
389 return __scalar_hash<_Tp>::operator()(__v);
390 }
391};
392
393template <>
394struct __hash_impl<long double> : __scalar_hash<long double> {
395 _LIBCPP_HIDE_FROM_ABI size_t operator()(long double __v) const _NOEXCEPT {
396 // -0.0 and 0.0 should return same hash
397 if (__v == 0.0L)
398 return 0;
399#if defined(__i386__) || (defined(__x86_64__) && defined(__ILP32__))
400 // Zero out padding bits
401 union {
402 long double __t;
403 struct {
404 size_t __a;
405 size_t __b;
406 size_t __c;
407 size_t __d;
408 } __s;
409 } __u;
410 __u.__s.__a = 0;
411 __u.__s.__b = 0;
412 __u.__s.__c = 0;
413 __u.__s.__d = 0;
414 __u.__t = __v;
415 return __u.__s.__a ^ __u.__s.__b ^ __u.__s.__c ^ __u.__s.__d;
416#elif defined(__x86_64__)
417 // Zero out padding bits
418 union {
419 long double __t;
420 struct {
421 size_t __a;
422 size_t __b;
423 } __s;
424 } __u;
425 __u.__s.__a = 0;
426 __u.__s.__b = 0;
427 __u.__t = __v;
428 return __u.__s.__a ^ __u.__s.__b;
429#else
430 return __scalar_hash<long double>::operator()(__v);
431#endif
432 }
433};
434
435template <class _Tp>
436struct hash : public __hash_impl<_Tp> {};
437
438template <>
439struct hash<nullptr_t> : public __unary_function<nullptr_t, size_t> {
440 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t operator()(nullptr_t) const _NOEXCEPT { return 662607004ull; }
441};
442
443#ifndef _LIBCPP_CXX03_LANG
444template <class _Key, class _Hash>
445using __check_hash_requirements _LIBCPP_NODEBUG =
446 integral_constant<bool,
447 is_copy_constructible<_Hash>::value && is_move_constructible<_Hash>::value &&
448 __is_invocable_r_v<size_t, _Hash, _Key const&> >;
449
450template <class _Key, class _Hash = hash<_Key> >
451using __has_enabled_hash _LIBCPP_NODEBUG =
452 integral_constant<bool, __check_hash_requirements<_Key, _Hash>::value && is_default_constructible<_Hash>::value >;
453
454template <class _Type, class>
455using __enable_hash_helper_imp _LIBCPP_NODEBUG = _Type;
456
457template <class _Type, class... _Keys>
458using __enable_hash_helper _LIBCPP_NODEBUG =
459 __enable_hash_helper_imp<_Type, __enable_if_t<__all<__has_enabled_hash<_Keys>::value...>::value> >;
460#endif // !_LIBCPP_CXX03_LANG
461
462_LIBCPP_END_NAMESPACE_STD
463
464#endif // _LIBCPP___FUNCTIONAL_HASH_H
465