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