Skip to content
0.2.0-dev Download ZIP
426 lines ·13.0 KB Raw
1/**************************************************************************/
2/* hashfuncs.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#pragma once
32
33#include "core/math/math_defs.h"
34#include "core/math/math_funcs.h"
35#include "core/typedefs.h"
36
37#ifdef _MSC_VER
38#include <intrin.h> // Needed for `__umulh` below.
39#endif
40
41template <typename F, typename S>
42struct Pair;
43
44/**
45 * Hashing functions
46 */
47
48/**
49 * DJB2 Hash function
50 * @param C String
51 * @return 32-bits hashcode
52 */
53_FORCE_INLINE_ uint32_t hash_djb2(const char *p_cstr) {
54 const unsigned char *chr = (const unsigned char *)p_cstr;
55 uint32_t hash = 5381;
56 uint32_t c = *chr++;
57
58 while (c) {
59 hash = ((hash << 5) + hash) ^ c; /* hash * 33 ^ c */
60 c = *chr++;
61 }
62
63 return hash;
64}
65
66_FORCE_INLINE_ uint32_t hash_djb2_buffer(const uint8_t *p_buff, int p_len, uint32_t p_prev = 5381) {
67 uint32_t hash = p_prev;
68
69 for (int i = 0; i < p_len; i++) {
70 hash = ((hash << 5) + hash) ^ p_buff[i]; /* hash * 33 + c */
71 }
72
73 return hash;
74}
75
76_FORCE_INLINE_ uint32_t hash_djb2_one_32(uint32_t p_in, uint32_t p_prev = 5381) {
77 return ((p_prev << 5) + p_prev) ^ p_in;
78}
79
80/**
81 * Thomas Wang's 64-bit to 32-bit Hash function:
82 * https://web.archive.org/web/20071223173210/https:/www.concentric.net/~Ttwang/tech/inthash.htm
83 *
84 * @param p_int - 64-bit unsigned integer key to be hashed
85 * @return unsigned 32-bit value representing hashcode
86 */
87_FORCE_INLINE_ uint32_t hash_one_uint64(const uint64_t p_int) {
88 uint64_t v = p_int;
89 v = (~v) + (v << 18); // v = (v << 18) - v - 1;
90 v = v ^ (v >> 31);
91 v = v * 21; // v = (v + (v << 2)) + (v << 4);
92 v = v ^ (v >> 11);
93 v = v + (v << 6);
94 v = v ^ (v >> 22);
95 return uint32_t(v);
96}
97
98_FORCE_INLINE_ uint64_t hash64_murmur3_64(uint64_t p_key, uint64_t p_seed) {
99 p_key ^= p_seed;
100 p_key ^= p_key >> 33;
101 p_key *= 0xff51afd7ed558ccd;
102 p_key ^= p_key >> 33;
103 p_key *= 0xc4ceb9fe1a85ec53;
104 p_key ^= p_key >> 33;
105 return p_key;
106}
107
108#define HASH_MURMUR3_SEED 0x7F07C65
109// Murmurhash3 32-bit version.
110// All MurmurHash versions are public domain software, and the author disclaims all copyright to their code.
111
112_FORCE_INLINE_ uint32_t hash_murmur3_one_32(uint32_t p_in, uint32_t p_seed = HASH_MURMUR3_SEED) {
113 p_in *= 0xcc9e2d51;
114 p_in = (p_in << 15) | (p_in >> 17);
115 p_in *= 0x1b873593;
116
117 p_seed ^= p_in;
118 p_seed = (p_seed << 13) | (p_seed >> 19);
119 p_seed = p_seed * 5 + 0xe6546b64;
120
121 return p_seed;
122}
123
124_FORCE_INLINE_ uint32_t hash_murmur3_one_64(uint64_t p_in, uint32_t p_seed = HASH_MURMUR3_SEED) {
125 p_seed = hash_murmur3_one_32(p_in & 0xFFFFFFFF, p_seed);
126 return hash_murmur3_one_32(p_in >> 32, p_seed);
127}
128
129uint32_t hash_murmur3_one_float(float p_in, uint32_t p_seed = HASH_MURMUR3_SEED);
130uint32_t hash_murmur3_one_double(double p_in, uint32_t p_seed = HASH_MURMUR3_SEED);
131
132_FORCE_INLINE_ uint32_t hash_murmur3_one_real(real_t p_in, uint32_t p_seed = HASH_MURMUR3_SEED) {
133#ifdef REAL_T_IS_DOUBLE
134 return hash_murmur3_one_double(p_in, p_seed);
135#else
136 return hash_murmur3_one_float(p_in, p_seed);
137#endif
138}
139
140_FORCE_INLINE_ uint32_t hash_rotl32(uint32_t p_x, int8_t p_r) {
141 return (p_x << p_r) | (p_x >> (32 - p_r));
142}
143
144_FORCE_INLINE_ uint32_t hash_fmix32(uint32_t p_h) {
145 p_h ^= p_h >> 16;
146 p_h *= 0x85ebca6b;
147 p_h ^= p_h >> 13;
148 p_h *= 0xc2b2ae35;
149 p_h ^= p_h >> 16;
150
151 return p_h;
152}
153
154uint32_t hash_murmur3_buffer(const void *p_key, int p_length, uint32_t p_seed = HASH_MURMUR3_SEED);
155
156uint32_t hash_djb2_one_float(double p_in, uint32_t p_prev = 5381);
157uint64_t hash_djb2_one_float_64(double p_in, uint64_t p_prev = 5381);
158
159_FORCE_INLINE_ uint64_t hash_djb2_one_64(uint64_t p_in, uint64_t p_prev = 5381) {
160 return ((p_prev << 5) + p_prev) ^ p_in;
161}
162
163template <typename, typename = std::void_t<>>
164struct has_hash_method : std::false_type {};
165
166template <typename T>
167struct has_hash_method<T, std::enable_if_t<std::is_same_v<decltype(std::declval<const T>().hash()), uint32_t>>> : std::true_type {};
168
169template <typename T>
170constexpr bool has_hash_method_v = has_hash_method<T>::value;
171
172template <typename T, typename = void>
173struct HashMapHasherDefaultImpl {
174};
175
176struct HashMapHasherDefault {
177 template <typename T>
178 static _FORCE_INLINE_ uint32_t hash(const T &p_type) {
179 return HashMapHasherDefaultImpl<std::decay_t<T>>::hash(p_type);
180 }
181};
182
183template <typename T>
184struct HashMapHasherDefaultImpl<T, std::enable_if_t<has_hash_method_v<T>>> {
185 // For self hashing types.
186 static _FORCE_INLINE_ uint32_t hash(const T &p_value) {
187 return p_value.hash();
188 }
189};
190
191template <typename T>
192struct HashMapHasherDefaultImpl<T *> {
193 // For pointer types.
194 static _FORCE_INLINE_ uint32_t hash(const T *p_pointer) { return hash_one_uint64((uint64_t)p_pointer); }
195};
196
197template <typename T>
198struct HashMapHasherDefaultImpl<T, std::enable_if_t<std::is_enum_v<T>>> {
199 // For all enums.
200 static _FORCE_INLINE_ uint32_t hash(T p_value) {
201 return HashMapHasherDefaultImpl<std::underlying_type_t<T>>::hash(static_cast<std::underlying_type_t<T>>(p_value));
202 }
203};
204
205template <>
206struct HashMapHasherDefaultImpl<char *> {
207 static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); }
208};
209
210template <>
211struct HashMapHasherDefaultImpl<const char *> : HashMapHasherDefaultImpl<char *> {};
212
213template <>
214struct HashMapHasherDefaultImpl<wchar_t> {
215 static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return hash_fmix32(uint32_t(p_wchar)); }
216};
217
218template <>
219struct HashMapHasherDefaultImpl<char16_t> {
220 static _FORCE_INLINE_ uint32_t hash(const char16_t p_uchar) { return hash_fmix32(uint32_t(p_uchar)); }
221};
222
223template <>
224struct HashMapHasherDefaultImpl<char32_t> {
225 static _FORCE_INLINE_ uint32_t hash(const char32_t p_uchar) { return hash_fmix32(uint32_t(p_uchar)); }
226};
227
228template <>
229struct HashMapHasherDefaultImpl<uint64_t> {
230 static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); }
231};
232
233template <>
234struct HashMapHasherDefaultImpl<int64_t> {
235 static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash_one_uint64(uint64_t(p_int)); }
236};
237
238template <>
239struct HashMapHasherDefaultImpl<float> {
240 static _FORCE_INLINE_ uint32_t hash(const float p_float) { return hash_murmur3_one_float(p_float); }
241};
242
243template <>
244struct HashMapHasherDefaultImpl<double> {
245 static _FORCE_INLINE_ uint32_t hash(const double p_double) { return hash_murmur3_one_double(p_double); }
246};
247
248template <>
249struct HashMapHasherDefaultImpl<uint32_t> {
250 static _FORCE_INLINE_ uint32_t hash(const uint32_t p_int) { return hash_fmix32(p_int); }
251};
252
253template <>
254struct HashMapHasherDefaultImpl<int32_t> {
255 static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return hash_fmix32(uint32_t(p_int)); }
256};
257
258template <>
259struct HashMapHasherDefaultImpl<uint16_t> {
260 static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return hash_fmix32(uint32_t(p_int)); }
261};
262
263template <>
264struct HashMapHasherDefaultImpl<int16_t> {
265 static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return hash_fmix32(uint32_t(p_int)); }
266};
267
268template <>
269struct HashMapHasherDefaultImpl<uint8_t> {
270 static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return hash_fmix32(uint32_t(p_int)); }
271};
272
273template <>
274struct HashMapHasherDefaultImpl<int8_t> {
275 static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return hash_fmix32(uint32_t(p_int)); }
276};
277
278template <typename F, typename S>
279struct HashMapHasherDefaultImpl<Pair<F, S>> {
280 static _FORCE_INLINE_ uint32_t hash(const Pair<F, S> &p_pair) {
281 uint64_t h1 = HashMapHasherDefault::hash(p_pair.first);
282 uint64_t h2 = HashMapHasherDefault::hash(p_pair.second);
283 return hash_one_uint64((h1 << 32) | h2);
284 }
285};
286
287template <typename, typename = std::void_t<>>
288struct has_is_same_method : std::false_type {};
289
290template <typename T>
291struct has_is_same_method<T, std::enable_if_t<std::is_same_v<decltype(std::declval<const T>().is_same(std::declval<const T>())), bool>>> : std::true_type {};
292
293template <typename T>
294constexpr bool has_is_same_method_v = has_is_same_method<T>::value;
295
296struct HashHasher {
297 static _FORCE_INLINE_ uint32_t hash(const int32_t p_hash) { return p_hash; }
298 static _FORCE_INLINE_ uint32_t hash(const uint32_t p_hash) { return p_hash; }
299 static _FORCE_INLINE_ uint64_t hash(const int64_t p_hash) { return p_hash; }
300 static _FORCE_INLINE_ uint64_t hash(const uint64_t p_hash) { return p_hash; }
301};
302
303template <typename T, typename = void>
304struct HashMapComparatorDefault {
305 static bool compare(const T &p_lhs, const T &p_rhs) {
306 return p_lhs == p_rhs;
307 }
308};
309
310template <>
311struct HashMapComparatorDefault<float> {
312 static bool compare(const float &p_lhs, const float &p_rhs) {
313 return Math::is_same(p_lhs, p_rhs);
314 }
315};
316
317template <>
318struct HashMapComparatorDefault<double> {
319 static bool compare(const double &p_lhs, const double &p_rhs) {
320 return Math::is_same(p_lhs, p_rhs);
321 }
322};
323
324template <typename T>
325struct HashMapComparatorDefault<T, std::enable_if_t<has_is_same_method_v<T>>> {
326 // For self comparing types.
327 static bool compare(const T &p_lhs, const T &p_rhs) {
328 return p_lhs.is_same(p_rhs);
329 }
330};
331
332constexpr uint32_t HASH_TABLE_SIZE_MAX = 29;
333
334inline constexpr uint32_t hash_table_size_primes[HASH_TABLE_SIZE_MAX] = {
335 5,
336 13,
337 23,
338 47,
339 97,
340 193,
341 389,
342 769,
343 1543,
344 3079,
345 6151,
346 12289,
347 24593,
348 49157,
349 98317,
350 196613,
351 393241,
352 786433,
353 1572869,
354 3145739,
355 6291469,
356 12582917,
357 25165843,
358 50331653,
359 100663319,
360 201326611,
361 402653189,
362 805306457,
363 1610612741,
364};
365
366// Computed with elem_i = UINT64_C (0 x FFFFFFFF FFFFFFFF ) / d_i + 1, where d_i is the i-th element of the above array.
367inline constexpr uint64_t hash_table_size_primes_inv[HASH_TABLE_SIZE_MAX] = {
368 3689348814741910324,
369 1418980313362273202,
370 802032351030850071,
371 392483916461905354,
372 190172619316593316,
373 95578984837873325,
374 47420935922132524,
375 23987963684927896,
376 11955116055547344,
377 5991147799191151,
378 2998982941588287,
379 1501077717772769,
380 750081082979285,
381 375261795343686,
382 187625172388393,
383 93822606204624,
384 46909513691883,
385 23456218233098,
386 11728086747027,
387 5864041509391,
388 2932024948977,
389 1466014921160,
390 733007198436,
391 366503839517,
392 183251896093,
393 91625960335,
394 45812983922,
395 22906489714,
396 11453246088
397};
398
399/**
400 * Fastmod computes ( n mod d ) given the precomputed c much faster than n % d.
401 * The implementation of fastmod is based on the following paper by Daniel Lemire et al.
402 * Faster Remainder by Direct Computation: Applications to Compilers and Software Libraries
403 * https://arxiv.org/abs/1902.01961
404 */
405static _FORCE_INLINE_ uint32_t fastmod(const uint32_t p_n, const uint64_t p_c, const uint32_t p_d) {
406#if defined(_MSC_VER)
407 // Returns the upper 64 bits of the product of two 64-bit unsigned integers.
408 // This intrinsic function is required since MSVC does not support unsigned 128-bit integers.
409#if defined(_M_X64) || defined(_M_ARM64)
410 return __umulh(p_c * p_n, p_d);
411#else
412 // Fallback to the slower method for 32-bit platforms.
413 return p_n % p_d;
414#endif // _M_X64 || _M_ARM64
415#else
416#ifdef __SIZEOF_INT128__
417 // Prevent compiler warning, because we know what we are doing.
418 uint64_t lowbits = p_c * p_n;
419 __extension__ typedef unsigned __int128 uint128;
420 return static_cast<uint64_t>(((uint128)lowbits * p_d) >> 64);
421#else
422 // Fallback to the slower method if no 128-bit unsigned integer type is available.
423 return p_n % p_d;
424#endif // __SIZEOF_INT128__
425#endif // _MSC_VER
426}