codekingpro/portable-devtools
114k
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-exception6// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCUDACXX___FUNCTIONAL_HASH_H11#define _LIBCUDACXX___FUNCTIONAL_HASH_H12 13#ifndef __cuda_std__14#include <__config>15#include <cstring>16#endif // __cuda_std__17 18#include "../__functional/invoke.h"19#include "../__functional/unary_function.h"20#include "../__fwd/hash.h"21#include "../__type_traits/enable_if.h"22#include "../__type_traits/integral_constant.h"23#include "../__type_traits/is_enum.h"24#include "../__type_traits/is_copy_constructible.h"25#include "../__type_traits/is_default_constructible.h"26#include "../__type_traits/is_move_constructible.h"27#include "../__type_traits/underlying_type.h"28#include "../__utility/forward.h"29#include "../__utility/move.h"30#include "../__utility/pair.h"31#include "../__utility/swap.h"32#include "../cstdint"33 34#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)35# pragma GCC system_header36#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)37# pragma clang system_header38#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)39# pragma system_header40#endif // no system header41 42#ifndef __cuda_std__43 44_LIBCUDACXX_BEGIN_NAMESPACE_STD45 46template <class _Size>47inline _LIBCUDACXX_INLINE_VISIBILITY48_Size49__loadword(const void* __p)50{51 _Size __r;52 std::memcpy(&__r, __p, sizeof(__r));53 return __r;54}55 56// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t57// is 64 bits. This is because cityhash64 uses 64bit x 64bit58// multiplication, which can be very slow on 32-bit systems.59template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>60struct __murmur2_or_cityhash;61 62template <class _Size>63struct __murmur2_or_cityhash<_Size, 32>64{65 inline _Size operator()(const void* __key, _Size __len)66 _LIBCUDACXX_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;67};68 69// murmur270template <class _Size>71_Size72__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)73{74 const _Size __m = 0x5bd1e995;75 const _Size __r = 24;76 _Size __h = __len;77 const unsigned char* __data = static_cast<const unsigned char*>(__key);78 for (; __len >= 4; __data += 4, __len -= 4)79 {80 _Size __k = __loadword<_Size>(__data);81 __k *= __m;82 __k ^= __k >> __r;83 __k *= __m;84 __h *= __m;85 __h ^= __k;86 }87 switch (__len)88 {89 case 3:90 __h ^= static_cast<_Size>(__data[2] << 16);91 _LIBCUDACXX_FALLTHROUGH();92 case 2:93 __h ^= static_cast<_Size>(__data[1] << 8);94 _LIBCUDACXX_FALLTHROUGH();95 case 1:96 __h ^= __data[0];97 __h *= __m;98 }99 __h ^= __h >> 13;100 __h *= __m;101 __h ^= __h >> 15;102 return __h;103}104 105template <class _Size>106struct __murmur2_or_cityhash<_Size, 64>107{108 inline _Size operator()(const void* __key, _Size __len) _LIBCUDACXX_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;109 110 private:111 // Some primes between 2^63 and 2^64.112 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;113 static const _Size __k1 = 0xb492b66fbe98f273ULL;114 static const _Size __k2 = 0x9ae16a3b2f90404fULL;115 static const _Size __k3 = 0xc949d7c7509e6557ULL;116 117 static _Size __rotate(_Size __val, int __shift) {118 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));119 }120 121 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {122 return (__val >> __shift) | (__val << (64 - __shift));123 }124 125 static _Size __shift_mix(_Size __val) {126 return __val ^ (__val >> 47);127 }128 129 static _Size __hash_len_16(_Size __u, _Size __v)130 _LIBCUDACXX_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK131 {132 const _Size __mul = 0x9ddfea08eb382d69ULL;133 _Size __a = (__u ^ __v) * __mul;134 __a ^= (__a >> 47);135 _Size __b = (__v ^ __a) * __mul;136 __b ^= (__b >> 47);137 __b *= __mul;138 return __b;139 }140 141 static _Size __hash_len_0_to_16(const char* __s, _Size __len)142 _LIBCUDACXX_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK143 {144 if (__len > 8) {145 const _Size __a = __loadword<_Size>(__s);146 const _Size __b = __loadword<_Size>(__s + __len - 8);147 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;148 }149 if (__len >= 4) {150 const uint32_t __a = __loadword<uint32_t>(__s);151 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);152 return __hash_len_16(__len + (static_cast<_Size>(__a) << 3), __b);153 }154 if (__len > 0) {155 const unsigned char __a = static_cast<unsigned char>(__s[0]);156 const unsigned char __b = static_cast<unsigned char>(__s[__len >> 1]);157 const unsigned char __c = static_cast<unsigned char>(__s[__len - 1]);158 const uint32_t __y = static_cast<uint32_t>(__a) +159 (static_cast<uint32_t>(__b) << 8);160 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);161 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;162 }163 return __k2;164 }165 166 static _Size __hash_len_17_to_32(const char *__s, _Size __len)167 _LIBCUDACXX_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK168 {169 const _Size __a = __loadword<_Size>(__s) * __k1;170 const _Size __b = __loadword<_Size>(__s + 8);171 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;172 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;173 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,174 __a + __rotate(__b ^ __k3, 20) - __c + __len);175 }176 177 // Return a 16-byte hash for 48 bytes. Quick and dirty.178 // Callers do best to use "random-looking" values for a and b.179 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(180 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b)181 _LIBCUDACXX_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK182 {183 __a += __w;184 __b = __rotate(__b + __a + __z, 21);185 const _Size __c = __a;186 __a += __x;187 __a += __y;188 __b += __rotate(__a, 44);189 return pair<_Size, _Size>(__a + __z, __b + __c);190 }191 192 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.193 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(194 const char* __s, _Size __a, _Size __b)195 _LIBCUDACXX_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK196 {197 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),198 __loadword<_Size>(__s + 8),199 __loadword<_Size>(__s + 16),200 __loadword<_Size>(__s + 24),201 __a,202 __b);203 }204 205 // Return an 8-byte hash for 33 to 64 bytes.206 static _Size __hash_len_33_to_64(const char *__s, size_t __len)207 _LIBCUDACXX_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK208 {209 _Size __z = __loadword<_Size>(__s + 24);210 _Size __a = __loadword<_Size>(__s) +211 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;212 _Size __b = __rotate(__a + __z, 52);213 _Size __c = __rotate(__a, 37);214 __a += __loadword<_Size>(__s + 8);215 __c += __rotate(__a, 7);216 __a += __loadword<_Size>(__s + 16);217 _Size __vf = __a + __z;218 _Size __vs = __b + __rotate(__a, 31) + __c;219 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);220 __z += __loadword<_Size>(__s + __len - 8);221 __b = __rotate(__a + __z, 52);222 __c = __rotate(__a, 37);223 __a += __loadword<_Size>(__s + __len - 24);224 __c += __rotate(__a, 7);225 __a += __loadword<_Size>(__s + __len - 16);226 _Size __wf = __a + __z;227 _Size __ws = __b + __rotate(__a, 31) + __c;228 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);229 return __shift_mix(__r * __k0 + __vs) * __k2;230 }231};232 233// cityhash64234template <class _Size>235_Size236__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)237{238 const char* __s = static_cast<const char*>(__key);239 if (__len <= 32) {240 if (__len <= 16) {241 return __hash_len_0_to_16(__s, __len);242 } else {243 return __hash_len_17_to_32(__s, __len);244 }245 } else if (__len <= 64) {246 return __hash_len_33_to_64(__s, __len);247 }248 249 // For strings over 64 bytes we hash the end first, and then as we250 // loop we keep 56 bytes of state: v, w, x, y, and z.251 _Size __x = __loadword<_Size>(__s + __len - 40);252 _Size __y = __loadword<_Size>(__s + __len - 16) +253 __loadword<_Size>(__s + __len - 56);254 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,255 __loadword<_Size>(__s + __len - 24));256 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);257 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);258 __x = __x * __k1 + __loadword<_Size>(__s);259 260 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.261 __len = (__len - 1) & ~static_cast<_Size>(63);262 do {263 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;264 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;265 __x ^= __w.second;266 __y += __v.first + __loadword<_Size>(__s + 40);267 __z = __rotate(__z + __w.first, 33) * __k1;268 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);269 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,270 __y + __loadword<_Size>(__s + 16));271 _CUDA_VSTD::swap(__z, __x);272 __s += 64;273 __len -= 64;274 } while (__len != 0);275 return __hash_len_16(276 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,277 __hash_len_16(__v.second, __w.second) + __x);278}279 280template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>281struct __scalar_hash;282 283template <class _Tp>284struct __scalar_hash<_Tp, 0>285 : public __unary_function<_Tp, size_t>286{287 _LIBCUDACXX_INLINE_VISIBILITY288 size_t operator()(_Tp __v) const noexcept289 {290 union291 {292 _Tp __t;293 size_t __a;294 } __u;295 __u.__a = 0;296 __u.__t = __v;297 return __u.__a;298 }299};300 301template <class _Tp>302struct __scalar_hash<_Tp, 1>303 : public __unary_function<_Tp, size_t>304{305 _LIBCUDACXX_INLINE_VISIBILITY306 size_t operator()(_Tp __v) const noexcept307 {308 union309 {310 _Tp __t;311 size_t __a;312 } __u;313 __u.__t = __v;314 return __u.__a;315 }316};317 318template <class _Tp>319struct __scalar_hash<_Tp, 2>320 : public __unary_function<_Tp, size_t>321{322 _LIBCUDACXX_INLINE_VISIBILITY323 size_t operator()(_Tp __v) const noexcept324 {325 union326 {327 _Tp __t;328 struct329 {330 size_t __a;331 size_t __b;332 } __s;333 } __u;334 __u.__t = __v;335 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));336 }337};338 339template <class _Tp>340struct __scalar_hash<_Tp, 3>341 : public __unary_function<_Tp, size_t>342{343 _LIBCUDACXX_INLINE_VISIBILITY344 size_t operator()(_Tp __v) const noexcept345 {346 union347 {348 _Tp __t;349 struct350 {351 size_t __a;352 size_t __b;353 size_t __c;354 } __s;355 } __u;356 __u.__t = __v;357 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));358 }359};360 361template <class _Tp>362struct __scalar_hash<_Tp, 4>363 : public __unary_function<_Tp, size_t>364{365 _LIBCUDACXX_INLINE_VISIBILITY366 size_t operator()(_Tp __v) const noexcept367 {368 union369 {370 _Tp __t;371 struct372 {373 size_t __a;374 size_t __b;375 size_t __c;376 size_t __d;377 } __s;378 } __u;379 __u.__t = __v;380 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));381 }382};383 384struct _PairT {385 size_t first;386 size_t second;387};388 389_LIBCUDACXX_INLINE_VISIBILITY390inline size_t __hash_combine(size_t __lhs, size_t __rhs) noexcept {391 typedef __scalar_hash<_PairT> _HashT;392 const _PairT __p = {__lhs, __rhs};393 return _HashT()(__p);394}395 396template<class _Tp>397struct _LIBCUDACXX_TEMPLATE_VIS hash<_Tp*>398 : public __unary_function<_Tp*, size_t>399{400 _LIBCUDACXX_INLINE_VISIBILITY401 size_t operator()(_Tp* __v) const noexcept402 {403 union404 {405 _Tp* __t;406 size_t __a;407 } __u;408 __u.__t = __v;409 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));410 }411};412 413template <>414struct _LIBCUDACXX_TEMPLATE_VIS hash<bool>415 : public __unary_function<bool, size_t>416{417 _LIBCUDACXX_INLINE_VISIBILITY418 size_t operator()(bool __v) const noexcept {return static_cast<size_t>(__v);}419};420 421template <>422struct _LIBCUDACXX_TEMPLATE_VIS hash<char>423 : public __unary_function<char, size_t>424{425 _LIBCUDACXX_INLINE_VISIBILITY426 size_t operator()(char __v) const noexcept {return static_cast<size_t>(__v);}427};428 429template <>430struct _LIBCUDACXX_TEMPLATE_VIS hash<signed char>431 : public __unary_function<signed char, size_t>432{433 _LIBCUDACXX_INLINE_VISIBILITY434 size_t operator()(signed char __v) const noexcept {return static_cast<size_t>(__v);}435};436 437template <>438struct _LIBCUDACXX_TEMPLATE_VIS hash<unsigned char>439 : public __unary_function<unsigned char, size_t>440{441 _LIBCUDACXX_INLINE_VISIBILITY442 size_t operator()(unsigned char __v) const noexcept {return static_cast<size_t>(__v);}443};444 445#ifndef _LIBCUDACXX_HAS_NO_UNICODE_CHARS446template <>447struct _LIBCUDACXX_TEMPLATE_VIS hash<char16_t>448 : public __unary_function<char16_t, size_t>449{450 _LIBCUDACXX_INLINE_VISIBILITY451 size_t operator()(char16_t __v) const noexcept {return static_cast<size_t>(__v);}452};453 454template <>455struct _LIBCUDACXX_TEMPLATE_VIS hash<char32_t>456 : public __unary_function<char32_t, size_t>457{458 _LIBCUDACXX_INLINE_VISIBILITY459 size_t operator()(char32_t __v) const noexcept {return static_cast<size_t>(__v);}460};461#endif // _LIBCUDACXX_HAS_NO_UNICODE_CHARS462 463#ifndef _LIBCUDACXX_HAS_NO_WIDE_CHARACTERS464template <>465struct _LIBCUDACXX_TEMPLATE_VIS hash<wchar_t>466 : public __unary_function<wchar_t, size_t>467{468 _LIBCUDACXX_INLINE_VISIBILITY469 size_t operator()(wchar_t __v) const noexcept {return static_cast<size_t>(__v);}470};471#endif // _LIBCUDACXX_HAS_NO_WIDE_CHARACTERS472 473template <>474struct _LIBCUDACXX_TEMPLATE_VIS hash<short>475 : public __unary_function<short, size_t>476{477 _LIBCUDACXX_INLINE_VISIBILITY478 size_t operator()(short __v) const noexcept {return static_cast<size_t>(__v);}479};480 481template <>482struct _LIBCUDACXX_TEMPLATE_VIS hash<unsigned short>483 : public __unary_function<unsigned short, size_t>484{485 _LIBCUDACXX_INLINE_VISIBILITY486 size_t operator()(unsigned short __v) const noexcept {return static_cast<size_t>(__v);}487};488 489template <>490struct _LIBCUDACXX_TEMPLATE_VIS hash<int>491 : public __unary_function<int, size_t>492{493 _LIBCUDACXX_INLINE_VISIBILITY494 size_t operator()(int __v) const noexcept {return static_cast<size_t>(__v);}495};496 497template <>498struct _LIBCUDACXX_TEMPLATE_VIS hash<unsigned int>499 : public __unary_function<unsigned int, size_t>500{501 _LIBCUDACXX_INLINE_VISIBILITY502 size_t operator()(unsigned int __v) const noexcept {return static_cast<size_t>(__v);}503};504 505template <>506struct _LIBCUDACXX_TEMPLATE_VIS hash<long>507 : public __unary_function<long, size_t>508{509 _LIBCUDACXX_INLINE_VISIBILITY510 size_t operator()(long __v) const noexcept {return static_cast<size_t>(__v);}511};512 513template <>514struct _LIBCUDACXX_TEMPLATE_VIS hash<unsigned long>515 : public __unary_function<unsigned long, size_t>516{517 _LIBCUDACXX_INLINE_VISIBILITY518 size_t operator()(unsigned long __v) const noexcept {return static_cast<size_t>(__v);}519};520 521template <>522struct _LIBCUDACXX_TEMPLATE_VIS hash<long long>523 : public __scalar_hash<long long>524{525};526 527template <>528struct _LIBCUDACXX_TEMPLATE_VIS hash<unsigned long long>529 : public __scalar_hash<unsigned long long>530{531};532 533#ifndef _LIBCUDACXX_HAS_NO_INT128534 535template <>536struct _LIBCUDACXX_TEMPLATE_VIS hash<__int128_t>537 : public __scalar_hash<__int128_t>538{539};540 541template <>542struct _LIBCUDACXX_TEMPLATE_VIS hash<__uint128_t>543 : public __scalar_hash<__uint128_t>544{545};546 547#endif548 549template <>550struct _LIBCUDACXX_TEMPLATE_VIS hash<float>551 : public __scalar_hash<float>552{553 _LIBCUDACXX_INLINE_VISIBILITY554 size_t operator()(float __v) const noexcept555 {556 // -0.0 and 0.0 should return same hash557 if (__v == 0.0f)558 return 0;559 return __scalar_hash<float>::operator()(__v);560 }561};562 563template <>564struct _LIBCUDACXX_TEMPLATE_VIS hash<double>565 : public __scalar_hash<double>566{567 _LIBCUDACXX_INLINE_VISIBILITY568 size_t operator()(double __v) const noexcept569 {570 // -0.0 and 0.0 should return same hash571 if (__v == 0.0)572 return 0;573 return __scalar_hash<double>::operator()(__v);574 }575};576 577template <>578struct _LIBCUDACXX_TEMPLATE_VIS hash<long double>579 : public __scalar_hash<long double>580{581 _LIBCUDACXX_INLINE_VISIBILITY582 size_t operator()(long double __v) const noexcept583 {584 // -0.0 and 0.0 should return same hash585 if (__v == 0.0L)586 return 0;587#if defined(__i386__) || (defined(__x86_64__) && defined(__ILP32__))588 // Zero out padding bits589 union590 {591 long double __t;592 struct593 {594 size_t __a;595 size_t __b;596 size_t __c;597 size_t __d;598 } __s;599 } __u;600 __u.__s.__a = 0;601 __u.__s.__b = 0;602 __u.__s.__c = 0;603 __u.__s.__d = 0;604 __u.__t = __v;605 return __u.__s.__a ^ __u.__s.__b ^ __u.__s.__c ^ __u.__s.__d;606#elif defined(__x86_64__)607 // Zero out padding bits608 union609 {610 long double __t;611 struct612 {613 size_t __a;614 size_t __b;615 } __s;616 } __u;617 __u.__s.__a = 0;618 __u.__s.__b = 0;619 __u.__t = __v;620 return __u.__s.__a ^ __u.__s.__b;621#else622 return __scalar_hash<long double>::operator()(__v);623#endif624 }625};626 627template <class _Tp, bool = is_enum<_Tp>::value>628struct _LIBCUDACXX_TEMPLATE_VIS __enum_hash629 : public __unary_function<_Tp, size_t>630{631 _LIBCUDACXX_INLINE_VISIBILITY632 size_t operator()(_Tp __v) const noexcept633 {634 typedef typename underlying_type<_Tp>::type type;635 return hash<type>()(static_cast<type>(__v));636 }637};638template <class _Tp>639struct _LIBCUDACXX_TEMPLATE_VIS __enum_hash<_Tp, false> {640 __enum_hash() = delete;641 __enum_hash(__enum_hash const&) = delete;642 __enum_hash& operator=(__enum_hash const&) = delete;643};644 645template <class _Tp>646struct _LIBCUDACXX_TEMPLATE_VIS hash : public __enum_hash<_Tp>647{648};649 650#if _LIBCUDACXX_STD_VER > 14651 652template <>653struct _LIBCUDACXX_TEMPLATE_VIS hash<nullptr_t>654 : public __unary_function<nullptr_t, size_t>655{656 _LIBCUDACXX_INLINE_VISIBILITY657 size_t operator()(nullptr_t) const noexcept {658 return 662607004ull;659 }660};661#endif662 663template <class _Key, class _Hash>664using __check_hash_requirements _LIBCUDACXX_NODEBUG_TYPE = integral_constant<bool,665 is_copy_constructible<_Hash>::value &&666 is_move_constructible<_Hash>::value &&667 __invokable_r<size_t, _Hash, _Key const&>::value668>;669 670template <class _Key, class _Hash = hash<_Key> >671using __has_enabled_hash _LIBCUDACXX_NODEBUG_TYPE = integral_constant<bool,672 __check_hash_requirements<_Key, _Hash>::value &&673 is_default_constructible<_Hash>::value674>;675 676#if _LIBCUDACXX_STD_VER > 14677template <class _Type, class>678using __enable_hash_helper_imp _LIBCUDACXX_NODEBUG_TYPE = _Type;679 680template <class _Type, class ..._Keys>681using __enable_hash_helper _LIBCUDACXX_NODEBUG_TYPE = __enable_hash_helper_imp<_Type,682 __enable_if_t<__all<__has_enabled_hash<_Keys>::value...>::value>683>;684#else685template <class _Type, class ...>686using __enable_hash_helper _LIBCUDACXX_NODEBUG_TYPE = _Type;687#endif688 689_LIBCUDACXX_END_NAMESPACE_STD690 691#endif // __cuda_std__692 693#endif // _LIBCUDACXX___FUNCTIONAL_HASH_H694 