36# include <shared_mutex>
54static std::atomic<bool>
init{
false};
65#define FORCE_INLINE __attribute__((always_inline)) inline
67#define FORCE_INLINE inline
72 return (x <<
r) | (x >> (32 -
r));
77 return (x <<
r) | (x >> (64 -
r));
80#define ROTL32(x,y) rotl32(x,y)
81#define ROTL64(x,y) rotl64(x,y)
83#define BIG_CONSTANT(x) (x##LLU)
89#define getblock(p, i) (p[i])
118 if constexpr (std::numeric_limits<size_t>::digits >= 64)
119 return static_cast<size_t>(hash);
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);
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);
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);
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);
164 std::uint64_t rhs)
noexcept
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);
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;
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;
187 carry +=
low < mid2 ? 1u : 0u;
188 std::uint64_t high = hh + (lh >> 32) + (hl >> 32) + carry;
194 std::uint64_t input)
noexcept
196 constexpr std::uint64_t prime2 = 14029467366897019727ULL;
197 constexpr std::uint64_t prime1 = 11400714785074694791ULL;
198 acc += input * prime2;
205 std::uint64_t
value)
noexcept
207 constexpr std::uint64_t prime1 = 11400714785074694791ULL;
208 constexpr std::uint64_t prime4 = 9650029242287828579ULL;
211 acc = acc * prime1 + prime4;
216 std::uint64_t rhs)
noexcept
231 for (
int i = 0; i < 256; ++i)
235 init.store(
true, std::memory_order_release);
240 return init.load(std::memory_order_acquire);
254 catch (
const std::system_error &)
259 std::unique_lock<std::shared_mutex> lock(
jsw_mtx);
260 if (not
init.load(std::memory_order_acquire))
270 std::unique_lock<std::shared_mutex> lock(
jsw_mtx);
274size_t jsw_hash(
const void * key,
size_t len)
noexcept
276 if (not
init.load(std::memory_order_acquire))
280 std::unique_lock<std::shared_mutex> init_lock(
jsw_mtx);
285 catch (
const std::system_error &)
288 if (not
init.load(std::memory_order_acquire))
294 std::shared_lock<std::shared_mutex> lock(
jsw_mtx);
295 const unsigned char *p = (
const unsigned char*) key;
298 for (
size_t i = 0; i < len; i++)
299 h = (
h << 1 |
h >> 31) ^
tab[p[i]];
306 if (not
init.load(std::memory_order_acquire))
308 std::unique_lock<std::shared_mutex> init_lock(
jsw_mtx);
313 catch (
const std::system_error &)
316 if (not
init.load(std::memory_order_acquire))
321 std::shared_lock<std::shared_mutex> lock(
jsw_mtx);
322 const unsigned char * p = (
const unsigned char*) key;
326 h = (h << 1 | h >> 31) ^
tab[*p++];
331#define jen_mix(a,b,c) \
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; \
341#define jen_final(a,b,c) \
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); \
352size_t jen_hash(
const void *key,
size_t length,
unsigned initval)
noexcept
355 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
357 const uint8_t *p =
static_cast<const uint8_t *
>(key);
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]];
388 case 0 :
return static_cast<size_t>(c);
392 return static_cast<size_t>(c);
402 const uint8_t * data = (
const uint8_t*)key;
403 const int nblocks = len / 4;
408 uint32_t c1 = 0xcc9e2d51;
409 uint32_t c2 = 0x1b873593;
414 for(i = 0; i < nblocks; i++)
424 h1 = h1*5+0xe6546b64;
430 const uint8_t * tail = (
const uint8_t*)(data + nblocks*4);
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;
455 const uint8_t * data = (
const uint8_t*)key;
456 const int nblocks = len / 16;
464 uint32_t c1 = 0x239b961b;
465 uint32_t c2 = 0xab0e9789;
466 uint32_t c3 = 0x38b34ae5;
467 uint32_t c4 = 0xa1e38b93;
472 for(i = 0; i < nblocks; i++)
474 const uint8_t * block = data + i*16;
480 k1 *= c1; k1 =
ROTL32(k1,15); k1 *= c2; h1 ^= k1;
482 h1 =
ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
484 k2 *= c2; k2 =
ROTL32(k2,16); k2 *= c3; h2 ^= k2;
486 h2 =
ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
488 k3 *= c3; k3 =
ROTL32(k3,17); k3 *= c4; h3 ^= k3;
490 h3 =
ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
492 k4 *= c4; k4 =
ROTL32(k4,18); k4 *= c1; h4 ^= k4;
494 h4 =
ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
500 const uint8_t * tail = (
const uint8_t*)(data + nblocks*16);
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;
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;
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;
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;
536 h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
538 h1 += h2; h1 += h3; h1 += h4;
539 h2 += h1; h3 += h1; h4 += h1;
546 h1 += h2; h1 += h3; h1 += h4;
547 h2 += h1; h3 += h1; h4 += h1;
549 auto * out_bytes =
static_cast<std::uint8_t *
>(
out);
557 const uint32_t
seed,
void *
out )
559 const uint8_t * data = (
const uint8_t*)key;
560 const int nblocks = len / 16;
572 for(i = 0; i < nblocks; i++)
574 const uint8_t * block = data + i*16;
578 k1 *= c1; k1 =
ROTL64(k1,31); k1 *= c2; h1 ^= k1;
580 h1 =
ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
582 k2 *= c2; k2 =
ROTL64(k2,33); k2 *= c1; h2 ^= k2;
584 h2 =
ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
590 const uint8_t * tail = (
const uint8_t*)(data + nblocks*16);
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;
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;
620 h1 ^= len; h2 ^= len;
631 auto * out_bytes =
static_cast<std::uint8_t *
>(
out);
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;
644 const auto * p =
static_cast<const std::uint8_t *
>(key);
649 std::uint64_t hash =
seed + prime5;
658 const auto *
const end = p + len;
659 std::uint64_t hash = 0;
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;
685 hash =
seed + prime5;
693 hash =
ROTL64(hash, 27) * prime1 + prime4;
699 hash ^=
static_cast<std::uint64_t
>(
read_le32(p)) * prime1;
700 hash =
ROTL64(hash, 23) * prime2 + prime3;
706 hash ^=
static_cast<std::uint64_t
>(*p++) * prime5;
707 hash =
ROTL64(hash, 11) * prime1;
721 static constexpr std::uint64_t secret[] =
723 0xa0761d6478bd642fULL,
724 0xe7037ed1a0b428dbULL,
725 0x8ebc6af09c88c6e3ULL,
726 0x589965cc75374cc3ULL
729 const auto * p =
static_cast<const std::uint8_t *
>(key);
732 std::uint64_t remaining = len;
736 auto read_wyr3 = [] (
const std::uint8_t * data,
size_t n)
noexcept
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]);
747 const size_t delta = (remaining >> 3) << 2;
748 a = (
static_cast<std::uint64_t
>(
read_le32(p)) << 32)
750 b = (
static_cast<std::uint64_t
>(
read_le32(p + remaining - 4)) << 32)
753 else if (remaining > 0)
754 a = read_wyr3(p, remaining);
760 std::uint64_t see1 =
seed;
761 std::uint64_t see2 =
seed;
772 while (remaining > 48);
776 while (remaining > 16)
787 return static_cast<size_t>(
792 std::uint64_t key0, std::uint64_t key1)
noexcept
794 const auto * in =
static_cast<const std::uint8_t *
>(key);
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;
801 auto sipround = [&]()
noexcept
822 const std::uint64_t b = 0;
824 sipround(); sipround();
827 sipround(); sipround(); sipround(); sipround();
828 return static_cast<size_t>(v0 ^ v1 ^ v2 ^ v3);
831 const auto *
const end = in + (len - (len & 7));
833 for (; in != end; in += 8)
842 std::uint64_t b =
static_cast<std::uint64_t
>(len) << 56;
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]];
865 return static_cast<size_t>(v0 ^ v1 ^ v2 ^ v3);
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,...
size_t size_t int32_t value
size_t size_t int32_t * out
A random number generator with the sequences of GSL's gsl_rng_mt19937.
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
unsigned long get() noexcept
The next number, as operator()().
size_t jsw_hash(const void *key, size_t len) noexcept
JSW hash (Julienne Walker)
size_t xxhash64_hash(const void *key, size_t len, std::uint64_t seed) noexcept
xxHash64 from the xxHash family.
size_t jen_hash(const void *key, size_t length, unsigned initval) noexcept
Jenkins hash (lookup3)
size_t siphash24_hash(const void *key, size_t len, std::uint64_t key0, std::uint64_t key1) noexcept
SipHash-2-4 keyed hash.
bool is_jsw_initialized() noexcept
Checks if the jsw_hash() lookup table has been initialized.
void init_jsw() noexcept
Initializes the randomized lookup table used by jsw_hash().
size_t wyhash_hash(const void *key, size_t len, std::uint64_t seed) noexcept
wyhash non-cryptographic hash.
#define jen_final(a, b, c)
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
static uint32_t fmix32(uint32_t h)
void MurmurHash3_x86_32(const void *key, int len, uint32_t seed, void *out)
static std::uint64_t xxh64_round(std::uint64_t acc, std::uint64_t input) noexcept
void MurmurHash3_x64_128(const void *key, const int len, const uint32_t seed, void *out)
static std::once_flag jsw_init_flag
static std::uint64_t mul_xor_fold64(std::uint64_t lhs, std::uint64_t rhs) noexcept
static std::shared_mutex jsw_mtx
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)
static std::uint64_t wyhash_mix(std::uint64_t lhs, std::uint64_t rhs) noexcept
static uint64_t fmix64(uint64_t k)
static uint64_t rotl64(uint64_t x, int8_t r)
static void write_le64(std::uint8_t *p, std::uint64_t value) noexcept
void MurmurHash3_x86_128(const void *key, const int len, uint32_t seed, void *out)
static std::uint64_t read_le64(const std::uint8_t *p) noexcept
const unsigned Default_Hash_Seed
static size_t fold_hash64_to_size_t(std::uint64_t hash) noexcept
static std::uint32_t read_le32(const std::uint8_t *p) noexcept
static void jsw_fill_table(std::uint32_t seed) noexcept
static std::atomic< bool > init
static std::uint64_t xxh64_merge_round(std::uint64_t acc, std::uint64_t value) noexcept
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)