Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
hash-fct.C
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
32# include <ctime>
33# include <stdexcept>
34# include <limits>
35# include <mutex>
36# include <shared_mutex>
37# include <atomic>
38# include "hash-fct.H"
39
40# include <ah-errors.H>
41# include <ah-random.H>
42
43namespace Aleph
44{
45
46const unsigned Default_Hash_Seed = 52679987;
47
48static long tab[256];
49
50// once_flag guards the one-time population of tab[].
51// is_jsw_initialized() needs a separate atomic so it can be queried without
52// re-entering the once_flag.
53static std::once_flag jsw_init_flag;
54static std::atomic<bool> init{false};
55
56// shared_mutex: readers (jsw_hash) acquire shared lock; writer (init_jsw(seed))
57// acquires exclusive lock, preventing data races during re-seed.
58static std::shared_mutex jsw_mtx;
59
60// ============================================================================
61// Helpers
62// ============================================================================
63
64#ifdef __GNUC__
65#define FORCE_INLINE __attribute__((always_inline)) inline
66#else
67#define FORCE_INLINE inline
68#endif
69
70static FORCE_INLINE uint32_t rotl32 ( uint32_t x, int8_t r )
71{
72 return (x << r) | (x >> (32 - r));
73}
74
75static FORCE_INLINE uint64_t rotl64 ( uint64_t x, int8_t r )
76{
77 return (x << r) | (x >> (64 - r));
78}
79
80#define ROTL32(x,y) rotl32(x,y)
81#define ROTL64(x,y) rotl64(x,y)
82
83#define BIG_CONSTANT(x) (x##LLU)
84
85//-----------------------------------------------------------------------------
86// Block read - if your platform needs to do endian-swapping or can only
87// handle aligned reads, do the conversion here
88
89#define getblock(p, i) (p[i])
90
91//-----------------------------------------------------------------------------
92// Finalization mix - force all bits of a hash block to avalanche
93
94static FORCE_INLINE uint32_t fmix32 ( uint32_t h )
95{
96 h ^= h >> 16;
97 h *= 0x85ebca6b;
98 h ^= h >> 13;
99 h *= 0xc2b2ae35;
100 h ^= h >> 16;
101
102 return h;
103}
104
105static FORCE_INLINE uint64_t fmix64 ( uint64_t k )
106{
107 k ^= k >> 33;
108 k *= BIG_CONSTANT(0xff51afd7ed558ccd);
109 k ^= k >> 33;
110 k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
111 k ^= k >> 33;
112
113 return k;
114}
115
116static FORCE_INLINE size_t fold_hash64_to_size_t(std::uint64_t hash) noexcept
117{
118 if constexpr (std::numeric_limits<size_t>::digits >= 64)
119 return static_cast<size_t>(hash);
120 else
121 {
122 constexpr unsigned bits = std::numeric_limits<size_t>::digits;
123 const std::uint64_t mask = (std::uint64_t{1} << bits) - 1;
124 hash ^= hash >> bits;
125 return static_cast<size_t>(hash & mask);
126 }
127}
128
129static FORCE_INLINE std::uint32_t read_le32(const std::uint8_t * p) noexcept
130{
131 return static_cast<std::uint32_t>(p[0])
132 | (static_cast<std::uint32_t>(p[1]) << 8)
133 | (static_cast<std::uint32_t>(p[2]) << 16)
134 | (static_cast<std::uint32_t>(p[3]) << 24);
135}
136
137static FORCE_INLINE std::uint64_t read_le64(const std::uint8_t * p) noexcept
138{
139 return static_cast<std::uint64_t>(p[0])
140 | (static_cast<std::uint64_t>(p[1]) << 8)
141 | (static_cast<std::uint64_t>(p[2]) << 16)
142 | (static_cast<std::uint64_t>(p[3]) << 24)
143 | (static_cast<std::uint64_t>(p[4]) << 32)
144 | (static_cast<std::uint64_t>(p[5]) << 40)
145 | (static_cast<std::uint64_t>(p[6]) << 48)
146 | (static_cast<std::uint64_t>(p[7]) << 56);
147}
148
149static FORCE_INLINE void write_le32(std::uint8_t * p, std::uint32_t value) noexcept
150{
151 p[0] = static_cast<std::uint8_t>(value);
152 p[1] = static_cast<std::uint8_t>(value >> 8);
153 p[2] = static_cast<std::uint8_t>(value >> 16);
154 p[3] = static_cast<std::uint8_t>(value >> 24);
155}
156
157static FORCE_INLINE void write_le64(std::uint8_t * p, std::uint64_t value) noexcept
158{
159 write_le32(p, static_cast<std::uint32_t>(value));
160 write_le32(p + 4, static_cast<std::uint32_t>(value >> 32));
161}
162
163static FORCE_INLINE std::uint64_t mul_xor_fold64(std::uint64_t lhs,
164 std::uint64_t rhs) noexcept
165{
166#ifdef __SIZEOF_INT128__
167 const __uint128_t prod = static_cast<__uint128_t>(lhs) * rhs;
168 return static_cast<std::uint64_t>(prod)
169 ^ static_cast<std::uint64_t>(prod >> 64);
170#else
171 const std::uint64_t lhs_lo = lhs & 0xffffffffULL;
172 const std::uint64_t lhs_hi = lhs >> 32;
173 const std::uint64_t rhs_lo = rhs & 0xffffffffULL;
174 const std::uint64_t rhs_hi = rhs >> 32;
175 const std::uint64_t ll = lhs_lo * rhs_lo;
176 const std::uint64_t lh = lhs_lo * rhs_hi;
177 const std::uint64_t hl = lhs_hi * rhs_lo;
178 const std::uint64_t hh = lhs_hi * rhs_hi;
179 // Accumulate the low 64 bits in two separate additions and track carries
180 // individually. A single `if (low < ll)` would miss a carry from the
181 // second addition when both additions overflow.
182 const std::uint64_t mid = lh << 32;
183 std::uint64_t low = ll + mid;
184 std::uint64_t carry = low < ll ? 1u : 0u;
185 const std::uint64_t mid2 = hl << 32;
186 low += mid2;
187 carry += low < mid2 ? 1u : 0u;
188 std::uint64_t high = hh + (lh >> 32) + (hl >> 32) + carry;
189 return low ^ high;
190#endif
191}
192
193static FORCE_INLINE std::uint64_t xxh64_round(std::uint64_t acc,
194 std::uint64_t input) noexcept
195{
196 constexpr std::uint64_t prime2 = 14029467366897019727ULL;
197 constexpr std::uint64_t prime1 = 11400714785074694791ULL;
198 acc += input * prime2;
199 acc = ROTL64(acc, 31);
200 acc *= prime1;
201 return acc;
202}
203
204static FORCE_INLINE std::uint64_t xxh64_merge_round(std::uint64_t acc,
205 std::uint64_t value) noexcept
206{
207 constexpr std::uint64_t prime1 = 11400714785074694791ULL;
208 constexpr std::uint64_t prime4 = 9650029242287828579ULL;
209 value = xxh64_round(0, value);
210 acc ^= value;
211 acc = acc * prime1 + prime4;
212 return acc;
213}
214
215static FORCE_INLINE std::uint64_t wyhash_mix(std::uint64_t lhs,
216 std::uint64_t rhs) noexcept
217{
218 return mul_xor_fold64(lhs, rhs);
219}
220
221// ============================================================================
222// Initialization and classic hashes
223// ============================================================================
224
225// Internal helper: populate tab[] from a given seed, with the numbers that
226// the gsl_rng_mt19937 of GSL gives for it, so that the table is the one of
227// before. Must be called either via std::call_once or with jsw_mtx held.
228static void jsw_fill_table(std::uint32_t seed) noexcept
229{
231 for (int i = 0; i < 256; ++i)
232 tab[i] = r.get();
233 // Release store: any thread that subsequently reads init==true with an
234 // acquire load is guaranteed to observe the fully populated tab[].
235 init.store(true, std::memory_order_release);
236}
237
238bool is_jsw_initialized() noexcept
239{
240 return init.load(std::memory_order_acquire);
241}
242
243// No-arg overload uses a fixed deterministic seed so results are
244// reproducible across runs. Call init_jsw(custom_seed) before first
245// hash use if a custom seed is required.
246void init_jsw() noexcept
247{
248 // std::call_once guarantees exactly-once, race-free execution even when
249 // multiple threads call jsw_hash() for the first time concurrently.
250 try
251 {
253 }
254 catch (const std::system_error &)
255 {
256 // call_once failed to acquire its internal synchronization primitive;
257 // fall back to a mutex-protected direct init so this noexcept function
258 // does not terminate the process.
259 std::unique_lock<std::shared_mutex> lock(jsw_mtx);
260 if (not init.load(std::memory_order_acquire))
262 }
263}
264
265void init_jsw(std::uint32_t seed) noexcept
266{
267 // Exclusive lock: blocks all concurrent jsw_hash() readers (shared_lock)
268 // until the new table is fully written, preventing data races during re-seed.
269 // Bypasses call_once intentionally so re-seeding with a different seed works.
270 std::unique_lock<std::shared_mutex> lock(jsw_mtx);
272}
273
274size_t jsw_hash(const void * key, size_t len) noexcept
275{
276 if (not init.load(std::memory_order_acquire))
277 {
278 // Serialize lazy init with init_jsw(seed) by acquiring the exclusive lock
279 // before call_once, so both paths go through the same mutex+once-flag.
280 std::unique_lock<std::shared_mutex> init_lock(jsw_mtx);
281 try
282 {
284 }
285 catch (const std::system_error &)
286 {
287 // Mutex already held; call directly as fallback to stay noexcept.
288 if (not init.load(std::memory_order_acquire))
290 }
291 }
292
293 // Shared lock: allows concurrent readers; excluded only during init.
294 std::shared_lock<std::shared_mutex> lock(jsw_mtx);
295 const unsigned char *p = (const unsigned char*) key;
296 size_t h = 16777551;
297
298 for (size_t i = 0; i < len; i++)
299 h = (h << 1 | h >> 31) ^ tab[p[i]];
300
301 return h;
302}
303
304size_t jsw_hash(const char * key) noexcept
305{
306 if (not init.load(std::memory_order_acquire))
307 {
308 std::unique_lock<std::shared_mutex> init_lock(jsw_mtx);
309 try
310 {
312 }
313 catch (const std::system_error &)
314 {
315 // Mutex already held; call directly as fallback to stay noexcept.
316 if (not init.load(std::memory_order_acquire))
318 }
319 }
320
321 std::shared_lock<std::shared_mutex> lock(jsw_mtx);
322 const unsigned char * p = (const unsigned char*) key;
323 size_t h = 16777551;
324
325 while (*p)
326 h = (h << 1 | h >> 31) ^ tab[*p++];
327
328 return h;
329}
330
331#define jen_mix(a,b,c) \
332{ \
333 a -= c; a ^= ROTL32(c, 4); c += b; \
334 b -= a; b ^= ROTL32(a, 6); a += c; \
335 c -= b; c ^= ROTL32(b, 8); b += a; \
336 a -= c; a ^= ROTL32(c,16); c += b; \
337 b -= a; b ^= ROTL32(a,19); a += c; \
338 c -= b; c ^= ROTL32(b, 4); b += a; \
339}
340
341#define jen_final(a,b,c) \
342{ \
343 c ^= b; c -= ROTL32(b,14); \
344 a ^= c; a -= ROTL32(c,11); \
345 b ^= a; b -= ROTL32(a,25); \
346 c ^= b; c -= ROTL32(b,16); \
347 a ^= c; a -= ROTL32(c,4); \
348 b ^= a; b -= ROTL32(a,14); \
349 c ^= b; c -= ROTL32(b,24); \
350}
351
352size_t jen_hash(const void *key, size_t length, unsigned initval) noexcept
353{
354 uint32_t a,b,c; /* internal state */
355 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
356
357 const uint8_t *p = static_cast<const uint8_t *>(key);
358
359 /*------ all but the last block: safe reads and jen_mix() ------*/
360 while (length > 12)
361 {
362 uint32_t k[3];
363 memcpy(k, p, 12);
364 a += k[0];
365 b += k[1];
366 c += k[2];
367 jen_mix(a,b,c);
368 length -= 12;
369 p += 12;
370 }
371
372 /*----------------------------- handle the last (up to 11) bytes */
373 switch(length)
374 {
375 case 12: c+=((uint32_t)p[11])<<24; [[fallthrough]];
376 case 11: c+=((uint32_t)p[10])<<16; [[fallthrough]];
377 case 10: c+=((uint32_t)p[9])<<8; [[fallthrough]];
378 case 9 : c+=p[8]; [[fallthrough]];
379 case 8 : b+=((uint32_t)p[7])<<24; [[fallthrough]];
380 case 7 : b+=((uint32_t)p[6])<<16; [[fallthrough]];
381 case 6 : b+=((uint32_t)p[5])<<8; [[fallthrough]];
382 case 5 : b+=p[4]; [[fallthrough]];
383 case 4 : a+=((uint32_t)p[3])<<24; [[fallthrough]];
384 case 3 : a+=((uint32_t)p[2])<<16; [[fallthrough]];
385 case 2 : a+=((uint32_t)p[1])<<8; [[fallthrough]];
386 case 1 : a+=p[0];
387 break;
388 case 0 : return static_cast<size_t>(c);
389 }
390
391 jen_final(a,b,c);
392 return static_cast<size_t>(c);
393}
394
395// ============================================================================
396// Modern hashes
397// ============================================================================
398
399void MurmurHash3_x86_32 ( const void * key, int len,
400 uint32_t seed, void * out )
401{
402 const uint8_t * data = (const uint8_t*)key;
403 const int nblocks = len / 4;
404 int i;
405
406 uint32_t h1 = seed;
407
408 uint32_t c1 = 0xcc9e2d51;
409 uint32_t c2 = 0x1b873593;
410
411 //----------
412 // body
413
414 for(i = 0; i < nblocks; i++)
415 {
416 uint32_t k1 = read_le32(data + i*4);
417
418 k1 *= c1;
419 k1 = ROTL32(k1,15);
420 k1 *= c2;
421
422 h1 ^= k1;
423 h1 = ROTL32(h1,13);
424 h1 = h1*5+0xe6546b64;
425 }
426
427 //----------
428 // tail
429
430 const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
431
432 uint32_t k1 = 0;
433
434 switch(len & 3)
435 {
436 case 3: k1 ^= tail[2] << 16; [[fallthrough]];
437 case 2: k1 ^= tail[1] << 8; [[fallthrough]];
438 case 1: k1 ^= tail[0];
439 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
440 };
441
442 //----------
443 // finalization
444
445 h1 ^= len;
446
447 h1 = fmix32(h1);
448
449 write_le32(static_cast<std::uint8_t *>(out), h1);
450}
451
452void MurmurHash3_x86_128 ( const void * key, const int len,
453 uint32_t seed, void * out )
454{
455 const uint8_t * data = (const uint8_t*)key;
456 const int nblocks = len / 16;
457 int i;
458
459 uint32_t h1 = seed;
460 uint32_t h2 = seed;
461 uint32_t h3 = seed;
462 uint32_t h4 = seed;
463
464 uint32_t c1 = 0x239b961b;
465 uint32_t c2 = 0xab0e9789;
466 uint32_t c3 = 0x38b34ae5;
467 uint32_t c4 = 0xa1e38b93;
468
469 //----------
470 // body
471
472 for(i = 0; i < nblocks; i++)
473 {
474 const uint8_t * block = data + i*16;
475 uint32_t k1 = read_le32(block);
476 uint32_t k2 = read_le32(block + 4);
477 uint32_t k3 = read_le32(block + 8);
478 uint32_t k4 = read_le32(block + 12);
479
480 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
481
482 h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
483
484 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
485
486 h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
487
488 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
489
490 h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
491
492 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
493
494 h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
495 }
496
497 //----------
498 // tail
499
500 const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
501
502 uint32_t k1 = 0;
503 uint32_t k2 = 0;
504 uint32_t k3 = 0;
505 uint32_t k4 = 0;
506
507 switch(len & 15)
508 {
509 case 15: k4 ^= tail[14] << 16; [[fallthrough]];
510 case 14: k4 ^= tail[13] << 8; [[fallthrough]];
511 case 13: k4 ^= tail[12] << 0;
512 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
513 [[fallthrough]];
514 case 12: k3 ^= tail[11] << 24; [[fallthrough]];
515 case 11: k3 ^= tail[10] << 16; [[fallthrough]];
516 case 10: k3 ^= tail[ 9] << 8; [[fallthrough]];
517 case 9: k3 ^= tail[ 8] << 0;
518 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
519 [[fallthrough]];
520 case 8: k2 ^= tail[ 7] << 24; [[fallthrough]];
521 case 7: k2 ^= tail[ 6] << 16; [[fallthrough]];
522 case 6: k2 ^= tail[ 5] << 8; [[fallthrough]];
523 case 5: k2 ^= tail[ 4] << 0;
524 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
525 [[fallthrough]];
526 case 4: k1 ^= tail[ 3] << 24; [[fallthrough]];
527 case 3: k1 ^= tail[ 2] << 16; [[fallthrough]];
528 case 2: k1 ^= tail[ 1] << 8; [[fallthrough]];
529 case 1: k1 ^= tail[ 0] << 0;
530 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
531 };
532
533 //----------
534 // finalization
535
536 h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
537
538 h1 += h2; h1 += h3; h1 += h4;
539 h2 += h1; h3 += h1; h4 += h1;
540
541 h1 = fmix32(h1);
542 h2 = fmix32(h2);
543 h3 = fmix32(h3);
544 h4 = fmix32(h4);
545
546 h1 += h2; h1 += h3; h1 += h4;
547 h2 += h1; h3 += h1; h4 += h1;
548
549 auto * out_bytes = static_cast<std::uint8_t *>(out);
550 write_le32(out_bytes, h1);
551 write_le32(out_bytes + 4, h2);
552 write_le32(out_bytes + 8, h3);
553 write_le32(out_bytes + 12, h4);
554}
555
556void MurmurHash3_x64_128 ( const void * key, const int len,
557 const uint32_t seed, void * out )
558{
559 const uint8_t * data = (const uint8_t*)key;
560 const int nblocks = len / 16;
561 int i;
562
563 uint64_t h1 = seed;
564 uint64_t h2 = seed;
565
566 uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
567 uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
568
569 //----------
570 // body
571
572 for(i = 0; i < nblocks; i++)
573 {
574 const uint8_t * block = data + i*16;
575 uint64_t k1 = read_le64(block);
576 uint64_t k2 = read_le64(block + 8);
577
578 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
579
580 h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
581
582 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
583
584 h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
585 }
586
587 //----------
588 // tail
589
590 const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
591
592 uint64_t k1 = 0;
593 uint64_t k2 = 0;
594
595 switch(len & 15)
596 {
597 case 15: k2 ^= (uint64_t)(tail[14]) << 48; [[fallthrough]];
598 case 14: k2 ^= (uint64_t)(tail[13]) << 40; [[fallthrough]];
599 case 13: k2 ^= (uint64_t)(tail[12]) << 32; [[fallthrough]];
600 case 12: k2 ^= (uint64_t)(tail[11]) << 24; [[fallthrough]];
601 case 11: k2 ^= (uint64_t)(tail[10]) << 16; [[fallthrough]];
602 case 10: k2 ^= (uint64_t)(tail[ 9]) << 8; [[fallthrough]];
603 case 9: k2 ^= (uint64_t)(tail[ 8]) << 0;
604 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
605 [[fallthrough]];
606 case 8: k1 ^= (uint64_t)(tail[ 7]) << 56; [[fallthrough]];
607 case 7: k1 ^= (uint64_t)(tail[ 6]) << 48; [[fallthrough]];
608 case 6: k1 ^= (uint64_t)(tail[ 5]) << 40; [[fallthrough]];
609 case 5: k1 ^= (uint64_t)(tail[ 4]) << 32; [[fallthrough]];
610 case 4: k1 ^= (uint64_t)(tail[ 3]) << 24; [[fallthrough]];
611 case 3: k1 ^= (uint64_t)(tail[ 2]) << 16; [[fallthrough]];
612 case 2: k1 ^= (uint64_t)(tail[ 1]) << 8; [[fallthrough]];
613 case 1: k1 ^= (uint64_t)(tail[ 0]) << 0;
614 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
615 };
616
617 //----------
618 // finalization
619
620 h1 ^= len; h2 ^= len;
621
622 h1 += h2;
623 h2 += h1;
624
625 h1 = fmix64(h1);
626 h2 = fmix64(h2);
627
628 h1 += h2;
629 h2 += h1;
630
631 auto * out_bytes = static_cast<std::uint8_t *>(out);
632 write_le64(out_bytes, h1);
633 write_le64(out_bytes + 8, h2);
634}
635
636size_t xxhash64_hash(const void * key, size_t len, std::uint64_t seed) noexcept
637{
638 constexpr std::uint64_t prime1 = 11400714785074694791ULL;
639 constexpr std::uint64_t prime2 = 14029467366897019727ULL;
640 constexpr std::uint64_t prime3 = 1609587929392839161ULL;
641 constexpr std::uint64_t prime4 = 9650029242287828579ULL;
642 constexpr std::uint64_t prime5 = 2870177450012600261ULL;
643
644 const auto * p = static_cast<const std::uint8_t *>(key);
645
646 // Guard against nullptr + 0 UB; compute canonical empty-buffer result.
647 if (len == 0)
648 {
649 std::uint64_t hash = seed + prime5;
650 hash ^= hash >> 33;
651 hash *= prime2;
652 hash ^= hash >> 29;
653 hash *= prime3;
654 hash ^= hash >> 32;
655 return fold_hash64_to_size_t(hash);
656 }
657
658 const auto * const end = p + len;
659 std::uint64_t hash = 0;
660
661 if (len >= 32)
662 {
663 const auto * const limit = end - 32;
664 std::uint64_t v1 = seed + prime1 + prime2;
665 std::uint64_t v2 = seed + prime2;
666 std::uint64_t v3 = seed + 0;
667 std::uint64_t v4 = seed - prime1;
668
669 do
670 {
671 v1 = xxh64_round(v1, read_le64(p)); p += 8;
672 v2 = xxh64_round(v2, read_le64(p)); p += 8;
673 v3 = xxh64_round(v3, read_le64(p)); p += 8;
674 v4 = xxh64_round(v4, read_le64(p)); p += 8;
675 }
676 while (p <= limit);
677
678 hash = ROTL64(v1, 1) + ROTL64(v2, 7) + ROTL64(v3, 12) + ROTL64(v4, 18);
679 hash = xxh64_merge_round(hash, v1);
680 hash = xxh64_merge_round(hash, v2);
681 hash = xxh64_merge_round(hash, v3);
682 hash = xxh64_merge_round(hash, v4);
683 }
684 else
685 hash = seed + prime5;
686
687 hash += len;
688
689 while (p + 8 <= end)
690 {
691 const std::uint64_t k1 = xxh64_round(0, read_le64(p));
692 hash ^= k1;
693 hash = ROTL64(hash, 27) * prime1 + prime4;
694 p += 8;
695 }
696
697 if (p + 4 <= end)
698 {
699 hash ^= static_cast<std::uint64_t>(read_le32(p)) * prime1;
700 hash = ROTL64(hash, 23) * prime2 + prime3;
701 p += 4;
702 }
703
704 while (p < end)
705 {
706 hash ^= static_cast<std::uint64_t>(*p++) * prime5;
707 hash = ROTL64(hash, 11) * prime1;
708 }
709
710 hash ^= hash >> 33;
711 hash *= prime2;
712 hash ^= hash >> 29;
713 hash *= prime3;
714 hash ^= hash >> 32;
715
716 return fold_hash64_to_size_t(hash);
717}
718
719size_t wyhash_hash(const void * key, size_t len, std::uint64_t seed) noexcept
720{
721 static constexpr std::uint64_t secret[] =
722 {
723 0xa0761d6478bd642fULL,
724 0xe7037ed1a0b428dbULL,
725 0x8ebc6af09c88c6e3ULL,
726 0x589965cc75374cc3ULL
727 };
728
729 const auto * p = static_cast<const std::uint8_t *>(key);
730 std::uint64_t a = 0;
731 std::uint64_t b = 0;
732 std::uint64_t remaining = len;
733
734 seed ^= secret[0];
735
736 auto read_wyr3 = [] (const std::uint8_t * data, size_t n) noexcept
737 {
738 return (static_cast<std::uint64_t>(data[0]) << 16)
739 | (static_cast<std::uint64_t>(data[n >> 1]) << 8)
740 | static_cast<std::uint64_t>(data[n - 1]);
741 };
742
743 if (remaining <= 16)
744 {
745 if (remaining >= 4)
746 {
747 const size_t delta = (remaining >> 3) << 2;
748 a = (static_cast<std::uint64_t>(read_le32(p)) << 32)
749 | read_le32(p + delta);
750 b = (static_cast<std::uint64_t>(read_le32(p + remaining - 4)) << 32)
751 | read_le32(p + remaining - 4 - delta);
752 }
753 else if (remaining > 0)
754 a = read_wyr3(p, remaining);
755 }
756 else
757 {
758 if (remaining > 48)
759 {
760 std::uint64_t see1 = seed;
761 std::uint64_t see2 = seed;
762 do
763 {
764 seed = wyhash_mix(read_le64(p) ^ secret[1], read_le64(p + 8) ^ seed);
765 see1 = wyhash_mix(read_le64(p + 16) ^ secret[2],
766 read_le64(p + 24) ^ see1);
767 see2 = wyhash_mix(read_le64(p + 32) ^ secret[3],
768 read_le64(p + 40) ^ see2);
769 p += 48;
770 remaining -= 48;
771 }
772 while (remaining > 48);
773 seed ^= see1 ^ see2;
774 }
775
776 while (remaining > 16)
777 {
778 seed = wyhash_mix(read_le64(p) ^ secret[1], read_le64(p + 8) ^ seed);
779 p += 16;
780 remaining -= 16;
781 }
782
783 a = read_le64(p + remaining - 16);
784 b = read_le64(p + remaining - 8);
785 }
786
787 return static_cast<size_t>(
788 wyhash_mix(secret[1] ^ len, wyhash_mix(a ^ secret[1], b ^ seed)));
789}
790
791size_t siphash24_hash(const void * key, size_t len,
792 std::uint64_t key0, std::uint64_t key1) noexcept
793{
794 const auto * in = static_cast<const std::uint8_t *>(key);
795
796 std::uint64_t v0 = 0x736f6d6570736575ULL ^ key0;
797 std::uint64_t v1 = 0x646f72616e646f6dULL ^ key1;
798 std::uint64_t v2 = 0x6c7967656e657261ULL ^ key0;
799 std::uint64_t v3 = 0x7465646279746573ULL ^ key1;
800
801 auto sipround = [&]() noexcept
802 {
803 v0 += v1;
804 v1 = ROTL64(v1, 13);
805 v1 ^= v0;
806 v0 = ROTL64(v0, 32);
807 v2 += v3;
808 v3 = ROTL64(v3, 16);
809 v3 ^= v2;
810 v0 += v3;
811 v3 = ROTL64(v3, 21);
812 v3 ^= v0;
813 v2 += v1;
814 v1 = ROTL64(v1, 17);
815 v1 ^= v2;
816 v2 = ROTL64(v2, 32);
817 };
818
819 // Guard against nullptr + 0 UB when computing end; also handles empty input.
820 if (len == 0)
821 {
822 const std::uint64_t b = 0; // len << 56 where len = 0
823 v3 ^= b;
824 sipround(); sipround();
825 v0 ^= b;
826 v2 ^= 0xff;
827 sipround(); sipround(); sipround(); sipround();
828 return static_cast<size_t>(v0 ^ v1 ^ v2 ^ v3);
829 }
830
831 const auto * const end = in + (len - (len & 7));
832
833 for (; in != end; in += 8)
834 {
835 const std::uint64_t m = read_le64(in);
836 v3 ^= m;
837 sipround();
838 sipround();
839 v0 ^= m;
840 }
841
842 std::uint64_t b = static_cast<std::uint64_t>(len) << 56;
843 switch (len & 7)
844 {
845 case 7: b |= static_cast<std::uint64_t>(in[6]) << 48; [[fallthrough]];
846 case 6: b |= static_cast<std::uint64_t>(in[5]) << 40; [[fallthrough]];
847 case 5: b |= static_cast<std::uint64_t>(in[4]) << 32; [[fallthrough]];
848 case 4: b |= static_cast<std::uint64_t>(in[3]) << 24; [[fallthrough]];
849 case 3: b |= static_cast<std::uint64_t>(in[2]) << 16; [[fallthrough]];
850 case 2: b |= static_cast<std::uint64_t>(in[1]) << 8; [[fallthrough]];
851 case 1: b |= static_cast<std::uint64_t>(in[0]); [[fallthrough]];
852 default: break;
853 }
854
855 v3 ^= b;
856 sipround();
857 sipround();
858 v0 ^= b;
859 v2 ^= 0xff;
860 sipround();
861 sipround();
862 sipround();
863 sipround();
864
865 return static_cast<size_t>(v0 ^ v1 ^ v2 ^ v3);
866}
867
868} // end namespace Aleph
Exception handling system with formatted messages for Aleph-w.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
long double h
Definition btreepic.C:154
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
unsigned long get() noexcept
The next number, as operator()().
Definition ah-random.H:289
size_t jsw_hash(const void *key, size_t len) noexcept
JSW hash (Julienne Walker)
Definition hash-fct.C:274
size_t xxhash64_hash(const void *key, size_t len, std::uint64_t seed) noexcept
xxHash64 from the xxHash family.
Definition hash-fct.C:636
size_t jen_hash(const void *key, size_t length, unsigned initval) noexcept
Jenkins hash (lookup3)
Definition hash-fct.C:352
size_t siphash24_hash(const void *key, size_t len, std::uint64_t key0, std::uint64_t key1) noexcept
SipHash-2-4 keyed hash.
Definition hash-fct.C:791
bool is_jsw_initialized() noexcept
Checks if the jsw_hash() lookup table has been initialized.
Definition hash-fct.C:238
void init_jsw() noexcept
Initializes the randomized lookup table used by jsw_hash().
Definition hash-fct.C:246
size_t wyhash_hash(const void *key, size_t len, std::uint64_t seed) noexcept
wyhash non-cryptographic hash.
Definition hash-fct.C:719
#define ROTL64(x, y)
Definition hash-fct.C:81
#define jen_final(a, b, c)
Definition hash-fct.C:341
#define BIG_CONSTANT(x)
Definition hash-fct.C:83
#define jen_mix(a, b, c)
Definition hash-fct.C:331
#define ROTL32(x, y)
Definition hash-fct.C:80
#define FORCE_INLINE
Definition hash-fct.C:67
Standard hash functions for Aleph types.
Main namespace for Aleph-w library functions.
static void write_le32(std::uint8_t *p, std::uint32_t value) noexcept
Definition hash-fct.C:149
static uint32_t fmix32(uint32_t h)
Definition hash-fct.C:94
void MurmurHash3_x86_32(const void *key, int len, uint32_t seed, void *out)
Definition hash-fct.C:399
static std::uint64_t xxh64_round(std::uint64_t acc, std::uint64_t input) noexcept
Definition hash-fct.C:193
void MurmurHash3_x64_128(const void *key, const int len, const uint32_t seed, void *out)
Definition hash-fct.C:556
static std::once_flag jsw_init_flag
Definition hash-fct.C:53
static std::uint64_t mul_xor_fold64(std::uint64_t lhs, std::uint64_t rhs) noexcept
Definition hash-fct.C:163
static std::shared_mutex jsw_mtx
Definition hash-fct.C:58
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
static uint32_t rotl32(uint32_t x, int8_t r)
Definition hash-fct.C:70
static std::uint64_t wyhash_mix(std::uint64_t lhs, std::uint64_t rhs) noexcept
Definition hash-fct.C:215
static long tab[256]
Definition hash-fct.C:48
static uint64_t fmix64(uint64_t k)
Definition hash-fct.C:105
static uint64_t rotl64(uint64_t x, int8_t r)
Definition hash-fct.C:75
static void write_le64(std::uint8_t *p, std::uint64_t value) noexcept
Definition hash-fct.C:157
void MurmurHash3_x86_128(const void *key, const int len, uint32_t seed, void *out)
Definition hash-fct.C:452
static std::uint64_t read_le64(const std::uint8_t *p) noexcept
Definition hash-fct.C:137
const unsigned Default_Hash_Seed
Definition hash-fct.C:46
static size_t fold_hash64_to_size_t(std::uint64_t hash) noexcept
Definition hash-fct.C:116
static std::uint32_t read_le32(const std::uint8_t *p) noexcept
Definition hash-fct.C:129
static void jsw_fill_table(std::uint32_t seed) noexcept
Definition hash-fct.C:228
static std::atomic< bool > init
Definition hash-fct.C:54
static std::uint64_t xxh64_merge_round(std::uint64_t acc, std::uint64_t value) noexcept
Definition hash-fct.C:204
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
ValueArg< size_t > seed
Definition testHash.C:53
static int * k
Gsl_Compatible_Rng r