-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Hashes.cpp
1382 lines (1234 loc) · 40.9 KB
/
Hashes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _HASHES_CPP
#include "Hashes.h"
#include "Random.h"
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
//#include <emmintrin.h>
//#include <xmmintrin.h>
// ----------------------------------------------------------------------------
//fake / bad hashes
// objsize: 0x2f-0x0: 47
void
BadHash(const void *key, int len, uint32_t seed, void *out)
{
uint32_t h = seed;
const uint8_t *data = (const uint8_t *)key;
const uint8_t *const end = &data[len];
while (data < end) {
h ^= h >> 3;
h ^= h << 5;
h ^= *data++;
}
*(uint32_t *) out = h;
}
// objsize: 0x19b-0x30: 363
void
sumhash(const void *key, int len, uint32_t seed, void *out)
{
uint32_t h = seed;
const uint8_t *data = (const uint8_t *)key;
const uint8_t *const end = &data[len];
while (data < end) {
h += *data++;
}
*(uint32_t *) out = h;
}
// objsize: 0x4ff-0x1a0: 863
void
sumhash32(const void *key, int len, uint32_t seed, void *out)
{
uint32_t h = seed;
const uint32_t *data = (const uint32_t *)key;
const uint32_t *const end = &data[len/4];
while (data < end) {
h += *data++;
}
if (len & 3) {
uint8_t *dc = (uint8_t*)data; //byte stepper
const uint8_t *const endc = &((const uint8_t*)key)[len];
while (dc < endc) {
h += *dc++ * UINT64_C(11400714819323198485);
}
}
*(uint32_t *) out = h;
}
// objsize: 0x50d-0x500: 13
void
DoNothingHash(const void *, int, uint32_t, void *)
{
}
// objsize: 0x53f-0x510: 47
void
NoopOAATReadHash(const void *key, int len, uint32_t seed, void *out)
{
uint32_t h = seed;
const uint8_t *data = (const uint8_t *)key;
const uint8_t *const end = &data[len];
while (data < end) {
h = *data++;
}
*(uint32_t *) out = h;
}
//-----------------------------------------------------------------------------
//One - byte - at - a - time hash based on Murmur 's mix
// objsize: 0x540-0x56f: 47
uint32_t MurmurOAAT(const char *key, int len, uint32_t hash)
{
const uint8_t *data = (const uint8_t *)key;
const uint8_t *const end = &data[len];
while (data < end) {
hash ^= *data++;
hash *= 0x5bd1e995;
hash ^= hash >> 15;
}
return hash;
}
//----------------------------------------------------------------------------
// objsize: 0x5a0-0xc3c: 1692
size_t
fibonacci(const char *key, int len, uint32_t seed)
{
size_t h = (size_t)seed;
size_t *dw = (size_t *)key; //word stepper
const size_t *const endw = &((const size_t*)key)[len/sizeof(size_t)];
while (dw < endw) {
h += *dw++ * UINT64_C(11400714819323198485);
}
if (len & (sizeof(size_t)-1)) {
uint8_t *dc = (uint8_t*)dw; //byte stepper
const uint8_t *const endc = &((const uint8_t*)key)[len];
while (dc < endc) {
h += *dc++ * UINT64_C(11400714819323198485);
}
}
return h;
}
// objsize: 0xc40-0xd56: 278
size_t
FNV2(const char *key, int len, size_t seed)
{
size_t h;
size_t *dw = (size_t *)key; //word stepper
const size_t *const endw = &((const size_t*)key)[len/sizeof(size_t)];
#ifdef HAVE_BIT32
h = seed ^ UINT32_C(2166136261);
#else
h = seed ^ UINT64_C(0xcbf29ce484222325);
#endif
#ifdef HAVE_ALIGNED_ACCESS_REQUIRED
// avoid ubsan, misaligned writes
int i = (uintptr_t)dw % sizeof (size_t);
if (i) {
uint8_t *dc = (uint8_t*)key;
switch (i) {
case 1:
h ^= *dc++;
#ifdef HAVE_BIT32
h *= UINT32_C(16777619);
#else
h *= UINT64_C(0x100000001b3);
#endif
case 2:
h ^= *dc++;
#ifdef HAVE_BIT32
h *= UINT32_C(16777619);
#else
h *= UINT64_C(0x100000001b3);
#endif
case 3:
h ^= *dc++;
#ifdef HAVE_BIT32
h *= UINT32_C(16777619);
#else
h *= UINT64_C(0x100000001b3);
#endif
#ifndef HAVE_BIT32
case 4:
h ^= *dc++;
h *= UINT64_C(0x100000001b3);
case 5:
h ^= *dc++;
h *= UINT64_C(0x100000001b3);
case 6:
h ^= *dc++;
h *= UINT64_C(0x100000001b3);
case 7:
h ^= *dc++;
h *= UINT64_C(0x100000001b3);
#endif
default:
break;
}
dw = (size_t*)dc; //word stepper
}
#endif
while (dw < endw) {
h ^= *dw++;
#ifdef HAVE_BIT32
h *= UINT32_C(16777619);
#else
h *= UINT64_C(0x100000001b3);
#endif
}
if (len & (sizeof(size_t)-1)) {
uint8_t *dc = (uint8_t*)dw; //byte stepper
const uint8_t *const endc = &((const uint8_t*)key)[len];
while (dc < endc) {
h ^= *dc++;
#ifdef HAVE_BIT32
h *= UINT32_C(16777619);
#else
h *= UINT64_C(0x100000001b3);
#endif
}
}
return h;
}
// i.e. FNV1a
// objsize: 0xd60-0xe2c: 204
uint32_t
FNV32a(const void *key, int len, uint32_t seed)
{
uint32_t h = seed;
const uint8_t *data = (const uint8_t *)key;
h ^= UINT32_C(2166136261);
for (int i = 0; i < len; i++) {
h ^= data[i];
h *= 16777619;
}
return h;
}
// objsize: 0xe30-0xf71: 321
uint32_t
FNV32a_YoshimitsuTRIAD(const char *key, int len, uint32_t seed)
{
const uint8_t *p = (const uint8_t *)key;
const uint32_t PRIME = 709607;
uint32_t hash32A = seed ^ UINT32_C(2166136261);
uint32_t hash32B = UINT32_C(2166136261) + len;
uint32_t hash32C = UINT32_C(2166136261);
for (; len >= 3 * 2 * sizeof(uint32_t); len -= 3 * 2 * sizeof(uint32_t), p += 3 * 2 * sizeof(uint32_t)) {
hash32A = (hash32A ^ (ROTL32(*(uint32_t *) (p + 0), 5) ^ *(uint32_t *) (p + 4))) * PRIME;
hash32B = (hash32B ^ (ROTL32(*(uint32_t *) (p + 8), 5) ^ *(uint32_t *) (p + 12))) * PRIME;
hash32C = (hash32C ^ (ROTL32(*(uint32_t *) (p + 16), 5) ^ *(uint32_t *) (p + 20))) * PRIME;
}
if (p != (const uint8_t *)key) {
hash32A = (hash32A ^ ROTL32(hash32C, 5)) * PRIME;
}
//Cases 0. .31
if (len & 4 * sizeof(uint32_t)) {
hash32A = (hash32A ^ (ROTL32(*(uint32_t *) (p + 0), 5) ^ *(uint32_t *) (p + 4))) * PRIME;
hash32B = (hash32B ^ (ROTL32(*(uint32_t *) (p + 8), 5) ^ *(uint32_t *) (p + 12))) * PRIME;
p += 8 * sizeof(uint16_t);
}
//Cases 0. .15
if (len & 2 * sizeof(uint32_t)) {
hash32A = (hash32A ^ *(uint32_t *) (p + 0)) * PRIME;
hash32B = (hash32B ^ *(uint32_t *) (p + 4)) * PRIME;
p += 4 * sizeof(uint16_t);
}
//Cases:0. .7
if (len & sizeof(uint32_t)) {
hash32A = (hash32A ^ *(uint16_t *) (p + 0)) * PRIME;
hash32B = (hash32B ^ *(uint16_t *) (p + 2)) * PRIME;
p += 2 * sizeof(uint16_t);
}
//Cases:0. .3
if (len & sizeof(uint16_t)) {
hash32A = (hash32A ^ *(uint16_t *) p) * PRIME;
p += sizeof(uint16_t);
}
if (len & 1)
hash32A = (hash32A ^ *p) * PRIME;
hash32A = (hash32A ^ ROTL32(hash32B, 5)) * PRIME;
return hash32A ^ (hash32A >> 16);
}
#ifdef HAVE_INT64
// objsize: 0xf80-0x108e: 270
uint32_t
FNV1A_Totenschiff(const char *key, int len, uint32_t seed)
{
#define _PADr_KAZE(x, n) (((x) << (n)) >> (n))
const char *p = (char *)key;
const uint32_t PRIME = 591798841;
uint32_t hash32;
uint64_t hash64 = (uint64_t)seed ^ UINT64_C(14695981039346656037);
uint64_t PADDEDby8;
for (; len > 8; len -= 8, p += 8) {
PADDEDby8 = *(uint64_t *)(p + 0);
hash64 = (hash64 ^ PADDEDby8) * PRIME;
}
// Here len is 1..8. when (8-8) the QWORD remains intact
PADDEDby8 = _PADr_KAZE(*(uint64_t *)(p + 0), (8 - len) << 3);
hash64 = (hash64 ^ PADDEDby8) * PRIME;
hash32 = (uint32_t)(hash64 ^ (hash64 >> 32));
return hash32 ^ (hash32 >> 16);
#undef _PADr_KAZE
}
// Dedicated to Pippip, the main character in the 'Das Totenschiff' roman, actually the B.Traven himself, his real name was Hermann Albert Otto Maksymilian Feige.
// CAUTION: Add 8 more bytes to the buffer being hashed, usually malloc(...+8) - to prevent out of boundary reads!
// Many thanks go to Yurii 'Hordi' Hordiienko, he lessened with 3 instructions the original 'Pippip', thus:
// objsize: 0x1090-0x1123: 147
uint32_t
FNV1A_Pippip_Yurii(const char *key, int wrdlen, uint32_t seed)
{
#define _PADr_KAZE(x, n) ( ((x) << (n))>>(n) )
const char *str = (char *)key;
const uint32_t PRIME = 591798841;
uint32_t hash32;
uint64_t hash64 = (uint64_t)seed ^ UINT64_C(14695981039346656037);
size_t Cycles, NDhead;
if (wrdlen > 8) {
Cycles = ((wrdlen - 1) >> 4) + 1;
NDhead = wrdlen - (Cycles << 3);
#pragma nounroll
for (; Cycles--; str += 8) {
hash64 = (hash64 ^ (*(uint64_t *)(str))) * PRIME;
hash64 = (hash64 ^ (*(uint64_t *)(str + NDhead))) * PRIME;
}
} else {
hash64 = (hash64 ^ _PADr_KAZE(*(uint64_t *)(str + 0), (8 - wrdlen) << 3)) *
PRIME;
}
hash32 = (uint32_t)(hash64 ^ (hash64 >> 32));
return hash32 ^ (hash32 >> 16);
#undef _PADr_KAZE
} // Last update: 2019-Oct-30, 14 C lines strong, Kaze.
// objsize: 0x1090-0x10df: 79
uint64_t
FNV64a(const char *key, int len, uint64_t seed)
{
uint64_t h = seed;
uint8_t *data = (uint8_t *)key;
const uint8_t *const end = &data[len];
h ^= UINT64_C(0xcbf29ce484222325);
while (data < end) {
h ^= *data++;
h *= UINT64_C(0x100000001b3);
}
return h;
}
#endif
// objsize: 0x105cb-0x10520: 171
// ported from https://github.com/golang/go/blob/master/src/hash/fnv/fnv.go
void
FNV128(uint64_t buf[2], const char *key, int len, uint64_t seed)
{
uint8_t *data = (uint8_t *)key;
const uint8_t *const end = &data[len];
const uint64_t prime128Lower = 0x13b;
const uint64_t prime128Shift = 24;
buf[0] = 0x6c62272e07bb0142 ^ seed;
buf[1] = 0x62b821756295c58d;
while (data < end) {
uint64_t a = prime128Lower;
uint64_t b = buf[1];
// TODO Should be improved with native int128 mult.
// But not even gcc has this yet
uint64_t a_lo = (uint32_t) a;
uint64_t a_hi = a >> 32;
uint64_t b_lo = (uint32_t) b;
uint64_t b_hi = b >> 32;
uint64_t a_x_b_hi = a_hi * b_hi;
uint64_t a_x_b_mid = a_hi * b_lo;
uint64_t b_x_a_mid = b_hi * a_lo;
uint64_t a_x_b_lo = a_lo * b_lo;
uint64_t carry_bit
= ((uint64_t) (uint32_t) a_x_b_mid + (uint64_t) (uint32_t) b_x_a_mid
+ (a_x_b_lo >> 32))
>> 32;
uint64_t multhi
= a_x_b_hi + (a_x_b_mid >> 32) + (b_x_a_mid >> 32) + carry_bit;
uint64_t s0 = multhi; // high
uint64_t s1 = prime128Lower * buf[1]; // low
s0 += (buf[1] << prime128Shift) + prime128Lower * buf[0];
// Update the values
buf[1] = s1;
buf[0] = s0;
buf[1] ^= (uint64_t) *data++;
}
}
//-----------------------------------------------------------------------------
// objsize: 0x1090-0x10df: 79
uint32_t
x17(const char *key, int len, uint32_t h)
{
uint8_t *data = (uint8_t *)key;
const uint8_t *const end = &data[len];
while (data < end) {
h = 17 * h + (*data++ - ' ');
}
return h ^ (h >> 16);
}
//64bit, ZFS
//note the original fletcher2 assumes 128bit aligned data, and
//can hereby advance the inner loop by 2 64bit words.
//both fletcher's return 4 words, 256 bit. Both are nevertheless very weak hashes.
// objsize: 0x1120-0x1218: 248
uint64_t
fletcher2(const char *key, int len, uint64_t seed)
{
uint64_t *dataw = (uint64_t *)key;
const uint64_t *const endw = &((const uint64_t*)key)[len/8];
uint64_t A = seed, B = 0;
for (; dataw < endw; dataw++) {
A += *dataw;
B += A;
}
if (len & 7) {
uint8_t *datac = (uint8_t*)dataw; //byte stepper
const uint8_t *const endc = &((const uint8_t*)key)[len];
for (; datac < endc; datac++) {
A += *datac;
B += A;
}
}
return B;
}
//64bit, ZFS
// objsize: 0x1220-0x1393: 371
uint64_t
fletcher4(const char *key, int len, uint64_t seed)
{
uint32_t *dataw = (uint32_t *)key;
const uint32_t *const endw = &((const uint32_t*)key)[len/4];
uint64_t A = seed, B = 0, C = 0, D = 0;
while (dataw < endw) {
A += *dataw++;
B += A;
C += B;
D += C;
}
if (len & 3) {
uint8_t *datac = (uint8_t*)dataw; //byte stepper
const uint8_t *const endc = &((const uint8_t*)key)[len];
while (datac < endc) {
A += *datac++;
B += A;
C += B;
D += C;
}
}
return D;
}
//-----------------------------------------------------------------------------
//also used in perl5 as djb2
// objsize: 0x13a0-0x13c9: 41
uint32_t
Bernstein(const char *key, int len, uint32_t seed)
{
const uint8_t *data = (const uint8_t *)key;
const uint8_t *const end = &data[len];
while (data < end) {
//seed = ((seed << 5) + seed) + *data++;
seed = 33 * seed + *data++;
}
return seed;
}
//as used in perl5
// objsize: 0x13a0-0x13c9: 41
uint32_t
sdbm(const char *key, int len, uint32_t hash)
{
unsigned char *str = (unsigned char *)key;
const unsigned char *const end = (const unsigned char *)str + len;
//note that perl5 adds the seed to the end of key, which looks like cargo cult
while (str < end) {
hash = (hash << 6) + (hash << 16) - hash + *str++;
}
return hash;
}
//as used in perl5 as one_at_a_time_hard
// objsize: 0x1400-0x1499: 153
uint32_t
JenkinsOOAT(const char *key, int len, uint32_t hash)
{
unsigned char *str = (unsigned char *)key;
const unsigned char *const end = (const unsigned char *)str + len;
uint64_t s = (uint64_t) hash;
unsigned char *seed = (unsigned char *)&s;
//unsigned char seed[8];
//note that perl5 adds the seed to the end of key, which looks like cargo cult
while (str < end) {
hash += (hash << 10);
hash ^= (hash >> 6);
hash += *str++;
}
hash += (hash << 10);
hash ^= (hash >> 6);
hash += seed[4];
hash += (hash << 10);
hash ^= (hash >> 6);
hash += seed[5];
hash += (hash << 10);
hash ^= (hash >> 6);
hash += seed[6];
hash += (hash << 10);
hash ^= (hash >> 6);
hash += seed[7];
hash += (hash << 10);
hash ^= (hash >> 6);
hash += (hash << 3);
hash ^= (hash >> 11);
hash = hash + (hash << 15);
return hash;
}
//as used in perl5 until 5.17(one_at_a_time_old)
// objsize: 0x14a0-0x14e1: 65
uint32_t JenkinsOOAT_perl(const char *key, int len, uint32_t hash)
{
unsigned char *str = (unsigned char *)key;
const unsigned char *const end = (const unsigned char *)str + len;
while (str < end) {
hash += *str++;
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash = hash + (hash << 15);
return hash;
}
// as used in gcc/libiberty htab_hash_string(), just without seed.
// also in gcc libcpp, just with len added.
// objsize: 0xf5c0-0xf5e5: 37
uint32_t libiberty_hash(unsigned char *str, int len, uint32_t seed)
{
const unsigned char *const end = (const unsigned char *)str + len;
uint32_t r = seed;
unsigned char c;
while (str < end) {
c = *str++;
r = r * 67 + (c - 113);
}
return r;
}
//------------------------------------------------
// One of a smallest non-multiplicative One-At-a-Time function
// that passes whole SMHasher.
// Author: Sokolov Yura aka funny-falcon <[email protected]>
// objsize: 0x14f0-0x15dd: 237
uint32_t
GoodOAAT(const char *key, int len, uint32_t seed) {
#define grol(x,n) (((x)<<(n))|((x)>>(32-(n))))
#define gror(x,n) (((x)>>(n))|((x)<<(32-(n))))
unsigned char *str = (unsigned char *)key;
const unsigned char *const end = (const unsigned char *)str + len;
uint32_t h1 = seed ^ 0x3b00;
uint32_t h2 = grol(seed, 15);
for (;str != end; str++) {
h1 += str[0];
h1 += h1 << 3; // h1 *= 9
h2 += h1;
// the rest could be as in MicroOAAT: h1 = grol(h1, 7)
// but clang doesn't generate ROTL instruction then.
h2 = grol(h2, 7);
h2 += h2 << 2; // h2 *= 5
}
h1 ^= h2;
/* now h1 passes all collision checks,
* so it is suitable for hash-tables with prime numbers. */
h1 += grol(h2, 14);
h2 ^= h1; h2 += gror(h1, 6);
h1 ^= h2; h1 += grol(h2, 5);
h2 ^= h1; h2 += gror(h1, 8);
return h2;
#undef grol
#undef gror
}
// MicroOAAT suitable for hash-tables using prime numbers.
// It passes all collision checks.
// Author: Sokolov Yura aka funny-falcon <[email protected]>
// objsize: 0x15e0-0x1624: 68
uint32_t
MicroOAAT(const char *key, int len, uint32_t seed) {
#define grol(x,n) (((x)<<(n))|((x)>>(32-(n))))
#define gror(x,n) (((x)>>(n))|((x)<<(32-(n))))
unsigned char *str = (unsigned char *)key;
const unsigned char *const end = (const unsigned char *)str + len;
uint32_t h1 = seed ^ 0x3b00;
uint32_t h2 = grol(seed, 15);
while (str < end) {
h1 += *str++;
h1 += h1 << 3; // h1 *= 9
h2 -= h1;
// unfortunately, clang produces bad code here,
// cause it doesn't generate rotl instruction.
h1 = grol(h1, 7);
}
return h1 ^ h2;
#undef grol
#undef gror
}
//-----------------------------------------------------------------------------
//Crap8 hash from http://www.team5150.com / ~andrew / noncryptohashzoo / Crap8.html
// objsize: 0x1630-0x1786: 342
uint32_t
Crap8(const uint8_t * key, uint32_t len, uint32_t seed)
{
#define c8fold( a, b, y, z ) { p = (uint32_t)(a) * (uint64_t)(b); y ^= (uint32_t)p; z ^= (uint32_t)(p >> 32); }
#define c8mix( in ) { h *= m; c8fold( in, m, k, h ); }
const uint32_t m = 0x83d2e73b, n = 0x97e1cc59, *key4 = (const uint32_t *)key;
uint32_t h = len + seed, k = n + len;
uint64_t p;
while (len >= 8) {
c8mix(key4[0]) c8mix(key4[1]) key4 += 2;
len -= 8;
}
if (len >= 4) {
c8mix(key4[0]) key4 += 1;
len -= 4;
}
if (len) {
c8mix(key4[0] & ((1 << (len * 8)) - 1))
}
c8fold(h ^ k, n, k, k)
return k;
}
extern "C" {
#ifdef HAVE_SSE2
void hasshe2 (const void *input, int len, uint32_t seed, void *out);
#endif
#if defined(HAVE_SSE42)
# ifndef HAVE_BROKEN_MSVC_CRC32C_HW
uint32_t crc32c_hw(const void *input, int len, uint32_t seed);
uint64_t crc64c_hw(const void *input, int len, uint32_t seed);
# endif
#if !defined(_MSC_VER)
uint32_t crc32c(const void *input, size_t len, uint32_t seed);
# endif
#endif
}
#if defined(HAVE_SSE2)
void
hasshe2_test(const void *input, int len, uint32_t seed, void *out)
{
if (!len) {
*(uint32_t *) out = 0;
return;
}
if (len % 16) {
//add pad NUL
len += 16 - (len % 16);
}
// objsize: 0-1bd: 445
hasshe2(input, len, seed, out);
}
#endif
#ifdef HAVE_SSE42
# ifndef HAVE_BROKEN_MSVC_CRC32C_HW
//#if defined(__SSE4_2__) && (defined(__i686__) || defined(_M_IX86) || defined(__x86_64__))
/* Compute CRC-32C using the Intel hardware instruction.
TODO: arm8
*/
void
crc32c_hw_test(const void *input, int len, uint32_t seed, void *out)
{
if (!len) {
*(uint32_t *) out = 0;
return;
}
// objsize: 0-28d: 653
*(uint32_t *) out = crc32c_hw(input, len, seed);
}
/* Compute CRC-64C using the Intel hardware instruction. */
void
crc64c_hw_test(const void *input, int len, uint32_t seed, void *out)
{
if (!len) {
*(uint64_t *) out = 0;
return;
}
// objsize: 0x290-0x51c: 652
*(uint64_t *) out = crc64c_hw(input, len, seed);
}
# endif
# if defined(__SSE4_2__) && (defined(__i686__) || defined(__x86_64__)) && !defined(_MSC_VER)
/* Faster Adler SSE4.2 crc32 on Intel HW only. FIXME aarch64 */
void
crc32c_hw1_test(const void *input, int len, uint32_t seed, void *out)
{
if (!len) {
*(uint32_t *) out = 0;
return;
}
// objsize: 0-29f: 671
*(uint32_t *) out = crc32c(input, len, seed);
}
# endif
#endif
#if 0 && defined(__x86_64__) && (defined(__linux__) || defined(__APPLE__))
/* asm */
extern "C" {
int fhtw_hash(const void* key, int key_len);
}
void
fhtw_test(const void *input, int len, uint32_t seed, void *out)
{
*(uint32_t *) out = fhtw_hash(input, len);
}
#endif
/* https://github.com/floodyberry/siphash */
void
siphash_test(const void *input, int len, uint32_t seed, void *out)
{
/* 128bit state, filled with a 32bit seed */
unsigned char key[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
if (!len) {
*(uint32_t *) out = 0;
return;
}
memcpy(key, &seed, sizeof(seed));
// objsize: 0-0x42f: 1071
*(uint64_t *) out = siphash(key, (const unsigned char *)input, (size_t) len);
}
void
siphash13_test(const void *input, int len, uint32_t seed, void *out)
{
unsigned char key[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
if (!len) {
*(uint32_t *) out = 0;
return;
}
memcpy(key, &seed, sizeof(seed));
// objsize: 0x450-0x75a: 778
*(uint64_t *) out = siphash13(key, (const unsigned char *)input, (size_t) len);
}
void
halfsiphash_test(const void *input, int len, uint32_t seed, void *out)
{
unsigned char key[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
if (!len) {
*(uint32_t *) out = 0;
return;
}
memcpy(key, &seed, sizeof(seed));
// objsize: 0x780-0xa3c: 700
*(uint32_t *) out = halfsiphash(key, (const unsigned char *)input, (size_t) len);
}
/* https://github.com/gamozolabs/falkhash */
#if defined(__SSE4_2__) && defined(__x86_64__) && !defined(_WIN32)
extern "C" {
uint64_t falkhash_test(uint8_t *data, uint64_t len, uint32_t seed, void *out);
}
void
falkhash_test_cxx(const void *input, int len, uint32_t seed, void *out)
{
uint64_t hash[2] = {0ULL, 0ULL};
if (!len) {
*(uint32_t *) out = 0;
return;
}
// objsize: 0-0x108: 264
falkhash_test((uint8_t *)input, (uint64_t)len, seed, hash);
*(uint64_t *) out = hash[0];
}
#endif
#if defined(HAVE_SSE42) && defined(__x86_64__)
#include "clhash.h"
static char clhash_random[RANDOM_BYTES_NEEDED_FOR_CLHASH];
void clhash_test (const void * key, int len, uint32_t seed, void * out) {
memcpy(clhash_random, &seed, 4);
// objsize: 0-0x711: 1809
*(uint64_t*)out = clhash(&clhash_random, (char*)key, (size_t)len);
}
void clhash_init()
{
void* data = get_random_key_for_clhash(UINT64_C(0xb3816f6a2c68e530), 711);
memcpy(clhash_random, data, RANDOM_BYTES_NEEDED_FOR_CLHASH);
free_random_key_for_clhash(data);
}
bool clhash_bad_seeds(std::vector<uint64_t> &seeds)
{
seeds = std::vector<uint64_t> { UINT64_C(0) };
return true;
}
void clhash_seed_init(size_t &seed)
{
// reject bad seeds
const std::vector<uint64_t> bad_seeds = { UINT64_C(0) };
while (std::find(bad_seeds.begin(), bad_seeds.end(), (uint64_t)seed) != bad_seeds.end())
seed++;
memcpy(clhash_random, &seed, sizeof(seed));
}
#endif
#ifndef _MSC_VER
// FIXME stopped working on MSVC
#include "halftime-hash.hpp"
alignas(64) static uint64_t
halftime_hash_random[8 * ((halftime_hash::kEntropyBytesNeeded / 64) + 1)];
void halftime_hash_style64_test(const void *key, int len, uint32_t seed, void *out) {
*(uint64_t *)out =
halftime_hash::HalftimeHashStyle64(halftime_hash_random, (char *)key, (size_t)len);
}
void halftime_hash_style128_test(const void *key, int len, uint32_t seed, void *out) {
*(uint64_t *)out =
halftime_hash::HalftimeHashStyle128(halftime_hash_random, (char *)key, (size_t)len);
}
void halftime_hash_style256_test(const void *key, int len, uint32_t seed, void *out) {
*(uint64_t *)out =
halftime_hash::HalftimeHashStyle256(halftime_hash_random, (char *)key, (size_t)len);
}
void halftime_hash_style512_test(const void *key, int len, uint32_t seed, void *out) {
*(uint64_t *)out =
halftime_hash::HalftimeHashStyle512(halftime_hash_random, (char *)key, (size_t)len);
}
void halftime_hash_init() {
size_t seed =
#ifdef HAVE_BIT32
0xcc70c4c1ULL;
#else
0xcc70c4c1798e4a6fUL; // 64bit only
#endif
halftime_hash_seed_init(seed);
}
// romu random number generator for seeding the HalftimeHash entropy
// TODO: align and increase size of outut random array
#if defined(__AVX512F__)
#include <immintrin.h>
void romuQuad32simd(const __m512i seeds[4], uint64_t *output, size_t count) {
__m512i wState = seeds[0], xState = seeds[1], yState = seeds[2],
zState = seeds[3];
const auto m = _mm512_set1_epi32(3323815723u);
for (size_t i = 0; i < count; i += 8) {
__m512i wp = wState, xp = xState, yp = yState, zp = zState;
wState = _mm512_mullo_epi32(m, zp);
xState = _mm512_add_epi32(zp, _mm512_rol_epi32(wp, 26));
yState = _mm512_sub_epi32(yp, xp);
zState = _mm512_add_epi32(yp, wp);
zState = _mm512_rol_epi32(zState, 9);
_mm512_store_epi64(&output[i], xp);
}
}
void halftime_hash_seed_init(size_t &seed) {
__m512i seeds[4] = {
{
(long long)seed ^ (long long)0x9a9b4c4e44dd48d1,
(long long)seed ^ (long long)0xf8b0cd76a61945b1,
(long long)seed ^ (long long)0x86268b0ae8494ce2,
(long long)seed ^ (long long)0x7d31e5469df4484d,
(long long)seed ^ (long long)0x62cb7b3e5e334aab,
(long long)seed ^ (long long)0xc4c4065529834f39,
(long long)seed ^ (long long)0xcc7972121c52411f,
(long long)seed ^ (long long)0x7e08efb9ea5a434f,
},
{
(long long)seed ^ (long long)0xccbc1ec6f244430c,
(long long)seed ^ (long long)0xecf76d38f32b4296,
(long long)seed ^ (long long)0xdf061d7c86664fa2,
(long long)seed ^ (long long)0x08e0da9580d44252,
(long long)seed ^ (long long)0xd074f3685aeb4f71,
(long long)seed ^ (long long)0x3f83eb99126d4a74,
(long long)seed ^ (long long)0xb5d24f61b4f540fa,
(long long)seed ^ (long long)0x33f248aa4b3c4aaf,
},
{
(long long)seed ^ (long long)0xd292ecaddb1c4dc1,
(long long)seed ^ (long long)0x94489307a0d041ed,
(long long)seed ^ (long long)0x25a4752be4bd4b84,
(long long)seed ^ (long long)0xa1d4010ab16c4b96,
(long long)seed ^ (long long)0x87175e8421534efa,
(long long)seed ^ (long long)0x0df85252bb894d2b,
(long long)seed ^ (long long)0x1d43b52179374cb4,
(long long)seed ^ (long long)0x5586b8bf3d4f4ca7,
},
{
(long long)seed ^ (long long)0x7275e2473e0f4618,
(long long)seed ^ (long long)0x2340093a933a4191,
(long long)seed ^ (long long)0x849ec473349843ac,
(long long)seed ^ (long long)0x9b8873c068ac4e41,
(long long)seed ^ (long long)0x3b8a6084e4ec44a7,
(long long)seed ^ (long long)0x341dadfa6e524396,
(long long)seed ^ (long long)0xb735256ca12649e9,
(long long)seed ^ (long long)0x1bd21c39a0694d4f,
},
};
romuQuad32simd(seeds, halftime_hash_random,
sizeof(halftime_hash_random) / sizeof(halftime_hash_random[0]));
}
#else
void halftime_hash_seed_init(size_t &seed)
{
#define ROTL(d,lrot) ((d<<(lrot)) | (d>>(8*sizeof(d)-(lrot))))
uint64_t wState = seed, xState= 0xecfc1357d65941ae, yState=0xbe1927f97b8c43f1,
zState=0xf4d4beb14ae042bb;
for (unsigned i = 0; i < sizeof(halftime_hash_random) / sizeof(halftime_hash_random[0]);
++i) {
const uint64_t wp = wState, xp = xState, yp = yState, zp = zState;
wState = 15241094284759029579u * zp; // a-mult
xState = zp + ROTL(wp, 52); // b-rotl, c-add
yState = yp - xp; // d-sub
zState = yp + wp; // e-add
zState = ROTL(zState, 19); // f-rotl
halftime_hash_random[i] = xp;
}
#undef ROTL
}
#endif
#endif // !_MSC_VER
// Multiply shift from
// Thorup "High Speed Hashing for Integers and Strings" 2018
// https://arxiv.org/pdf/1504.06804.pdf
//
#ifdef __SIZEOF_INT128__
const static int MULTIPLY_SHIFT_RANDOM_WORDS = 1<<8;
static __uint128_t multiply_shift_random[MULTIPLY_SHIFT_RANDOM_WORDS];
const static __uint128_t multiply_shift_r = ((__uint128_t)0x75f17d6b3588f843 << 64) | 0xb13dea7c9c324e51;
void multiply_shift(const void * key, int len_bytes, uint32_t seed, void * out) {
const uint8_t* buf = (const uint8_t*) key;
const int len = len_bytes/8;
// The output is 64 bits, and we consider the input 64 bit as well,
// so our intermediate values are 128.
// We mix in len_bytes in the basis, since smhasher considers two keys
// of different length to be different, even if all the extra bits are 0.
// This is needed for the AppendZero test.
uint64_t h = (seed + len_bytes) * multiply_shift_r >> 64;
for (int i = 0; i < len; i++, buf += 8)
h += multiply_shift_random[i % MULTIPLY_SHIFT_RANDOM_WORDS] * take64(buf) >> 64;
// Now get the last bytes
int remaining_bytes = len_bytes & 7;
if (remaining_bytes) {
uint64_t last = 0;
if (remaining_bytes & 4) {last = take32(buf); buf += 4;}
if (remaining_bytes & 2) {last = (last << 16) | take16(buf); buf += 2;}
if (remaining_bytes & 1) {last = (last << 8) | take08(buf);}
h += multiply_shift_random[len % MULTIPLY_SHIFT_RANDOM_WORDS] * last >> 64;
}