-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMlpSetUInt64.cpp
More file actions
1577 lines (1453 loc) · 42.6 KB
/
MlpSetUInt64.cpp
File metadata and controls
1577 lines (1453 loc) · 42.6 KB
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
#include "MlpSetUInt64.h"
namespace MlpSetUInt64
{
// Vectorized XXHash utility
// The hash function is a slightly modified XXH32, to fix a known deficiency
// The 3 hash functions are XXHashFn1, XXHashFn2, XXHashFn3
// XXHashArray computes 18 hashes from the prefixes of a key
// using vectorization (detail in function comment)
//
namespace XXH
{
static const uint32_t XXH_SEED1 = 1192827283U;
static const uint32_t XXH_SEED2 = 534897851U;
static const uint32_t PRIME32_1 = 2654435761U;
static const uint32_t PRIME32_2 = 2246822519U;
static const uint32_t PRIME32_3 = 3266489917U;
static const uint32_t PRIME32_4 = 668265263U;
static const uint32_t PRIME32_5 = 374761393U;
#define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
uint32_t XXH32_avalanche(uint32_t h32)
{
h32 ^= h32 >> 15;
h32 *= PRIME32_2;
h32 ^= h32 >> 13;
h32 *= PRIME32_3;
h32 ^= h32 >> 16;
return h32;
}
uint32_t XXH32_CoreLogic(uint64_t key, uint32_t len, uint32_t seed, uint32_t multiplier)
{
key >>= (8-len)*8;
key <<= (8-len)*8;
uint32_t low = key;
uint32_t high = key >> 32;
uint32_t h32 = PRIME32_5 + seed + len;
h32 ^= high * multiplier;
h32 = XXH_rotl32(h32, 17) * PRIME32_4;
if (len > 4)
{
h32 ^= low * multiplier;
h32 = XXH_rotl32(h32, 17) * PRIME32_4;
}
return XXH32_avalanche(h32);
}
uint32_t XXHashFn1(uint64_t key, uint32_t len)
{
return XXH32_CoreLogic(key, len, XXH_SEED1, PRIME32_1);
}
uint32_t XXHashFn2(uint64_t key, uint32_t len)
{
return XXH32_CoreLogic(key, len, XXH_SEED2, PRIME32_3);
}
uint32_t XXHashFn3(uint64_t key, uint32_t len)
{
return XXH32_CoreLogic(key, len, 0 /*seed*/, PRIME32_3);
}
static const __m128i PRIME32_1_ARRAY = _mm_set_epi32(PRIME32_1, PRIME32_1, PRIME32_1, PRIME32_1);
static const __m128i PRIME32_2_ARRAY = _mm_set_epi32(PRIME32_2, PRIME32_2, PRIME32_2, PRIME32_2);
static const __m128i PRIME32_3_ARRAY = _mm_set_epi32(PRIME32_3, PRIME32_3, PRIME32_3, PRIME32_3);
static const __m128i PRIME32_4_ARRAY = _mm_set_epi32(PRIME32_4, PRIME32_4, PRIME32_4, PRIME32_4);
static const __m128i XXH_OUT1_INIT = _mm_set_epi32(PRIME32_5 + XXH_SEED1 + 8U,
PRIME32_5 + XXH_SEED1 + 7U,
PRIME32_5 + XXH_SEED1 + 6U,
PRIME32_5 + XXH_SEED1 + 5U);
static const __m128i XXH_OUT2_INIT = _mm_set_epi32(PRIME32_5 + XXH_SEED2 + 8U,
PRIME32_5 + XXH_SEED2 + 7U,
PRIME32_5 + XXH_SEED2 + 6U,
PRIME32_5 + XXH_SEED2 + 5U);
static const __m128i XXH_OUT3_INIT = _mm_set_epi32(PRIME32_5 + 8U,
PRIME32_5 + 7U,
PRIME32_5 + 6U,
PRIME32_5 + 5U);
static const __m128i XXH_OUT4_INIT = _mm_set_epi32(PRIME32_5 + XXH_SEED1 + 4U,
PRIME32_5 + XXH_SEED1 + 3U,
PRIME32_5 + XXH_SEED2 + 4U,
PRIME32_5 + XXH_SEED2 + 3U);
static const __m128i XXH_LOW_MASK = _mm_set_epi32(0xffffffffU,
0xffffff00U,
0xffff0000U,
0xff000000U);
void XXHExecuteRotlAndMult(__m128i& data)
{
__m128i tmp = _mm_srli_epi32(data, 15);
data = _mm_slli_epi32(data, 17);
data = _mm_or_si128(data, tmp);
data = _mm_mullo_epi32(data, PRIME32_4_ARRAY);
}
void XXHExecuteAvalanche(__m128i& data)
{
__m128i tmp = _mm_srli_epi32(data, 15);
data = _mm_xor_si128(data, tmp);
data = _mm_mullo_epi32(data, PRIME32_2_ARRAY);
tmp = _mm_srli_epi32(data, 13);
data = _mm_xor_si128(data, tmp);
data = _mm_mullo_epi32(data, PRIME32_3_ARRAY);
tmp = _mm_srli_epi32(data, 16);
data = _mm_xor_si128(data, tmp);
}
// high to low:
// out1: h1(8), h1(7), h1(6), h1(5)
// out2: h2(8), h2(7), h2(6), h2(5)
// out3: h3(8), h3(7), h3(6), h3(5)
// out4: h1(4), h1(3), h2(4), h2(3)
// out5: h3(4), h3(3)
//
void XXHashArray(uint64_t key, __m128i& out1, __m128i& out2, __m128i& out3, __m128i& out4, uint64_t& out5)
{
uint32_t low = key;
uint32_t high = key >> 32;
uint32_t x1 = high * PRIME32_1;
uint32_t x2 = high * PRIME32_3;
out1 = _mm_set1_epi32(x1);
out1 = _mm_xor_si128(out1, XXH_OUT1_INIT);
XXHExecuteRotlAndMult(out1);
out2 = _mm_set1_epi32(x2);
out2 = _mm_xor_si128(out2, XXH_OUT2_INIT);
XXHExecuteRotlAndMult(out2);
out3 = _mm_set1_epi32(x2);
out3 = _mm_xor_si128(out3, XXH_OUT3_INIT);
XXHExecuteRotlAndMult(out3);
uint32_t high3byte = high & 0xffffff00U;
uint32_t high3byteP1 = high3byte * PRIME32_1;
uint32_t high3byteP3 = high3byte * PRIME32_3;
out4 = _mm_set_epi32(x1, high3byteP1, x2, high3byteP3);
out4 = _mm_xor_si128(out4, XXH_OUT4_INIT);
XXHExecuteRotlAndMult(out4);
__m128i v1 = _mm_set1_epi32(low);
v1 = _mm_and_si128(v1, XXH_LOW_MASK);
__m128i v2 = _mm_mullo_epi32(v1, PRIME32_1_ARRAY);
__m128i v3 = _mm_mullo_epi32(v1, PRIME32_3_ARRAY);
out1 = _mm_xor_si128(out1, v2);
XXHExecuteRotlAndMult(out1);
out2 = _mm_xor_si128(out2, v3);
XXHExecuteRotlAndMult(out2);
out3 = _mm_xor_si128(out3, v3);
XXHExecuteRotlAndMult(out3);
XXHExecuteAvalanche(out1);
XXHExecuteAvalanche(out2);
XXHExecuteAvalanche(out3);
XXHExecuteAvalanche(out4);
uint32_t h34 = (PRIME32_5 + 4U) ^ x2;
h34 = XXH_rotl32(h34, 17) * PRIME32_4;
h34 = XXH32_avalanche(h34);
uint32_t h33 = (PRIME32_5 + 3U) ^ high3byteP3;
h33 = XXH_rotl32(h33, 17) * PRIME32_4;
h33 = XXH32_avalanche(h33);
assert(_mm_extract_epi32(out1, 3) == XXHashFn1(key, 8));
assert(_mm_extract_epi32(out1, 2) == XXHashFn1(key, 7));
assert(_mm_extract_epi32(out1, 1) == XXHashFn1(key, 6));
assert(_mm_extract_epi32(out1, 0) == XXHashFn1(key, 5));
assert(_mm_extract_epi32(out2, 3) == XXHashFn2(key, 8));
assert(_mm_extract_epi32(out2, 2) == XXHashFn2(key, 7));
assert(_mm_extract_epi32(out2, 1) == XXHashFn2(key, 6));
assert(_mm_extract_epi32(out2, 0) == XXHashFn2(key, 5));
assert(_mm_extract_epi32(out3, 3) == XXHashFn3(key, 8));
assert(_mm_extract_epi32(out3, 2) == XXHashFn3(key, 7));
assert(_mm_extract_epi32(out3, 1) == XXHashFn3(key, 6));
assert(_mm_extract_epi32(out3, 0) == XXHashFn3(key, 5));
assert(_mm_extract_epi32(out4, 3) == XXHashFn1(key, 4));
assert(_mm_extract_epi32(out4, 2) == XXHashFn1(key, 3));
assert(_mm_extract_epi32(out4, 1) == XXHashFn2(key, 4));
assert(_mm_extract_epi32(out4, 0) == XXHashFn2(key, 3));
assert(h34 == XXHashFn3(key, 4));
assert(h33 == XXHashFn3(key, 3));
out5 = (uint64_t(h34) << 32) | h33;
}
} // namespace XXH
void CuckooHashTableNode::Init(int ilen, int dlen, uint64_t dkey, uint32_t hash18bit, int firstChild)
{
assert(!IsOccupied());
assert(1 <= ilen && ilen <= 8 && 1 <= dlen && dlen <= 8 && -1 <= firstChild && firstChild <= 255);
hash = 0x80000000U | ((ilen - 1) << 27) | ((dlen - 1) << 24) | hash18bit;
minKey = dkey;
childMap = firstChild;
}
int CuckooHashTableNode::FindNeighboringEmptySlot()
{
int lowbits = reinterpret_cast<uintptr_t>(this) & 63;
if (lowbits < 32)
{
// favors same cache line slot most
//
if (!this[1].IsOccupied())
{
return 1;
}
else if (!this[-1].IsOccupied())
{
return -1;
}
}
// TODO:
// according to DramSpeedTest.HWAdjacentPrefetcher test results,
// we should only return a slot residing in the same 128-byte line
//
rep(i, 1, 3)
{
if (!this[i].IsOccupied())
{
return i;
}
if (!this[-i].IsOccupied())
{
return -i;
}
}
return 0;
}
void CuckooHashTableNode::BitMapSet(int child)
{
assert(IsNode() && !IsLeaf() && !IsUsingInternalChildMap());
assert(0 <= child && child <= 255);
if (unlikely(IsExternalPointerBitMap()))
{
uint64_t* ptr = reinterpret_cast<uint64_t*>(childMap);
ptr[child / 64] |= uint64_t(1) << (child % 64);
}
else
{
if (child < 64)
{
childMap |= uint64_t(1) << child;
}
else
{
if (child == 94 || child == 95)
{
hash |= 1 << (child - 76);
}
else
{
int offset = ((hash >> 21) & 7) - 4;
uint64_t* ptr = reinterpret_cast<uint64_t*>(&(this[offset]));
ptr[child / 64 - 1] |= uint64_t(1) << (child % 64);
}
}
}
}
uint64_t* CuckooHashTableNode::AllocateExternalBitMap()
{
uint64_t* ptr = new uint64_t[4];
memset(ptr, 0, 32);
return ptr;
}
void CuckooHashTableNode::ExtendToBitMap()
{
assert(IsNode() && !IsLeaf() && IsUsingInternalChildMap() && GetChildNum() == 8);
uint64_t children = childMap;
int offset = FindNeighboringEmptySlot() + 4;
assert(1 <= offset && offset <= 7);
hash &= 0xff03ffffU;
hash |= (offset << 21);
if (offset == 4)
{
uint64_t* ptr = AllocateExternalBitMap();
childMap = reinterpret_cast<uintptr_t>(ptr);
}
else
{
childMap = 0;
uint64_t* ptr = reinterpret_cast<uint64_t*>(&(this[offset-4]));
memset(ptr, 0, sizeof(CuckooHashTableNode));
this[offset-4].hash = 0xc0000000U;
}
rep(i,0,7)
{
int child = children & 255;
children >>= 8;
BitMapSet(child);
}
}
static int Bitmap256LowerBound(uint64_t* ptr, uint32_t child)
{
assert(0 <= child && child <= 255);
int idx = child / 64;
uint64_t x = ptr[idx] >> (child % 64);
if (x)
{
return __builtin_ctzll(x) + child;
}
idx++;
while (idx < 4)
{
if (ptr[idx] != 0)
{
return __builtin_ctzll(ptr[idx]) + idx * 64;
}
idx++;
}
return -1;
}
int CuckooHashTableNode::LowerBoundChild(uint32_t child)
{
assert(IsNode() && !IsLeaf());
assert(0 <= child && child <= 255);
if (IsUsingInternalChildMap())
{
if (child == 0) { return childMap & 255; }
int k = GetChildNum();
__m64 z = _mm_cvtsi64_m64(childMap);
__m64 cmpTarget = _mm_set1_pi8(child - 1);
__m64 res = _mm_max_pu8(cmpTarget, z);
res = _mm_cmpeq_pi8(cmpTarget, res);
int msk = _mm_movemask_pi8(res);
msk &= (1<<k)-1;
msk++;
int pos = __builtin_ffs(msk);
assert(1 <= pos && pos <= k + 1);
if (pos == k+1)
{
return -1;
}
return (childMap >> ((pos-1)*8)) & 255;
}
else if (unlikely(IsExternalPointerBitMap()))
{
uint64_t* ptr = reinterpret_cast<uint64_t*>(childMap);
return Bitmap256LowerBound(ptr, child);
}
else
{
int offset = (hash >> 21) & 7;
if (child < 64)
{
uint64_t x = childMap >> child;
if (x)
{
return __builtin_ctzll(x) + child;
}
uint64_t* ptr = reinterpret_cast<uint64_t*>(&(this[offset-4]));
x = ptr[0] & 0xffffffff3fffffffULL;
x |= uint64_t((hash >> 18) & 3) << 30;
if (x)
{
return __builtin_ctzll(x) + 64;
}
rep(k, 1, 2)
{
if (ptr[k])
{
return __builtin_ctzll(ptr[k]) + (k+1) * 64;
}
}
return -1;
}
else if (child < 128)
{
uint64_t* ptr = reinterpret_cast<uint64_t*>(&(this[offset-4]));
uint64_t x = ptr[0] & 0xffffffff3fffffffULL;
x |= uint64_t((hash >> 18) & 3) << 30;
x >>= (child - 64);
if (x)
{
return __builtin_ctzll(x) + child;
}
rep(k, 1, 2)
{
if (ptr[k])
{
return __builtin_ctzll(ptr[k]) + (k+1) * 64;
}
}
return -1;
}
else
{
uint64_t* ptr = reinterpret_cast<uint64_t*>(&(this[offset-4]));
int idx = child / 64 - 1;
uint64_t x = ptr[idx] >> (child % 64);
if (x)
{
return __builtin_ctzll(x) + child;
}
if (idx < 2)
{
if (ptr[2])
{
return __builtin_ctzll(ptr[2]) + 192;
}
}
return -1;
}
}
}
bool CuckooHashTableNode::ExistChild(int child)
{
assert(IsNode() && !IsLeaf());
assert(0 <= child && child <= 255);
if (IsUsingInternalChildMap())
{
int k = GetChildNum();
__m64 z = _mm_cvtsi64_m64(childMap);
__m64 cmpTarget = _mm_set1_pi8(child);
__m64 res = _mm_cmpeq_pi8(cmpTarget, z);
int msk = _mm_movemask_pi8(res);
msk &= (1<<k)-1;
bool result = (msk != 0);
#ifndef NDEBUG
bool bruteForceResult = false;
uint64_t c = childMap;
rep(i,0,k-1)
{
int x = c & 255;
c >>= 8;
if (x == child) { bruteForceResult = true; break; }
}
assert(result == bruteForceResult);
#endif
return result;
}
else if (unlikely(IsExternalPointerBitMap()))
{
uint64_t* ptr = reinterpret_cast<uint64_t*>(childMap);
return (ptr[child / 64] & (uint64_t(1) << (child % 64))) != 0;
}
else
{
if (child < 64)
{
return (childMap & (uint64_t(1) << child)) != 0;
}
else if (unlikely(child == 94 || child == 95))
{
return (hash & (1 << (child - 76))) != 0;
}
else
{
int offset = ((hash >> 21) & 7) - 4;
uint64_t* ptr = reinterpret_cast<uint64_t*>(&(this[offset]));
return (ptr[child / 64 - 1] & (uint64_t(1) << (child % 64))) != 0;
}
}
}
void CuckooHashTableNode::AddChild(int child)
{
assert(IsNode() && !IsLeaf());
assert(0 <= child && child <= 255);
assert(!ExistChild(child));
if (IsUsingInternalChildMap())
{
int k = GetChildNum();
if (likely(k < 8))
{
SetChildNum(k+1);
__m64 z = _mm_cvtsi64_m64(childMap);
__m64 cmpTarget = _mm_set1_pi8(child);
__m64 res = _mm_max_pu8(cmpTarget, z);
res = _mm_cmpeq_pi8(cmpTarget, res);
int msk = _mm_movemask_pi8(res);
msk &= (1<<k)-1;
msk++;
int pos = __builtin_ffs(msk);
assert(1 <= pos && pos <= k + 1);
uint64_t larger = (pos == 8) ? 0 : (childMap >> ((pos-1)*8) << (pos*8));
uint64_t smaller = childMap & ((uint64_t(1) << ((pos-1)*8)) - 1);
childMap = smaller | (uint64_t(child) << ((pos - 1)*8)) | larger;
#ifndef NDEBUG
uint64_t tmp = childMap;
int last = tmp % 256;
rep(i, 1, k)
{
tmp /= 256;
int cur = tmp % 256;
assert(cur > last);
last = cur;
}
#endif
return;
}
ExtendToBitMap();
}
BitMapSet(child);
}
vector<int> CuckooHashTableNode::GetAllChildren()
{
assert(IsNode());
if (IsLeaf())
{
return vector<int>();
}
vector<int> ret;
if (IsUsingInternalChildMap())
{
uint64_t c = childMap;
int k = GetChildNum();
rep(i,0,k-1)
{
ret.push_back(c & 255);
c >>= 8;
}
rep(i, 1, k-1)
{
assert(ret[i] > ret[i-1]);
}
}
else if (unlikely(IsExternalPointerBitMap()))
{
uint64_t* ptr = reinterpret_cast<uint64_t*>(childMap);
rep(i,0,255)
{
if (ptr[i/64] & (uint64_t(1) << (i%64)))
{
ret.push_back(i);
}
}
}
else
{
rep(i,0,63)
{
if (childMap & (uint64_t(1) << i))
{
ret.push_back(i);
}
}
int offset = ((hash >> 21) & 7) - 4;
uint64_t* ptr = reinterpret_cast<uint64_t*>(&(this[offset]));
rep(i, 64, 93)
{
if (ptr[i/64-1] & (uint64_t(1) << (i%64)))
{
ret.push_back(i);
}
}
rep(i, 94, 95)
{
if (hash & (1 << (i - 76)))
{
ret.push_back(i);
}
}
rep(i, 96, 255)
{
if (ptr[i/64-1] & (uint64_t(1) << (i%64)))
{
ret.push_back(i);
}
}
}
return ret;
}
uint64_t* CuckooHashTableNode::CopyToExternalBitMap()
{
assert(IsNode() && !IsLeaf() && !IsUsingInternalChildMap() && !IsExternalPointerBitMap());
uint64_t* ptr = AllocateExternalBitMap();
int offset = (hash >> 21) & 7;
ptr[0] = childMap;
memcpy(ptr+1, &(this[offset-4]), sizeof(CuckooHashTableNode));
ptr[1] &= 0xffffffff3fffffffULL;
ptr[1] |= uint64_t((hash >> 18) & 3) << 30;
return ptr;
}
void CuckooHashTableNode::MoveNode(CuckooHashTableNode* target)
{
*target = *this;
if (IsUsingInternalChildMap() || IsExternalPointerBitMap())
{
memset(this, 0, sizeof(CuckooHashTableNode));
return;
}
int offset = (hash >> 21) & 7;
int targetOffset = target->FindNeighboringEmptySlot() + 4;
target->hash &= 0xff1fffffU;
target->hash |= (targetOffset << 21);
if (targetOffset != 4)
{
memcpy(&(target[targetOffset-4]), &(this[offset-4]), sizeof(CuckooHashTableNode));
}
else
{
uint64_t* ptr = CopyToExternalBitMap();
target->childMap = reinterpret_cast<uint64_t>(ptr);
}
memset(this, 0, sizeof(CuckooHashTableNode));
memset(&(this[offset-4]), 0, sizeof(CuckooHashTableNode));
#ifndef NDEBUG
assert(target->IsNode());
if (!target->IsUsingInternalChildMap() && !target->IsExternalPointerBitMap())
{
int o = (target->hash >> 21) & 7;
assert(target[o-4].IsOccupied() && !target[o-4].IsNode());
}
#endif
}
void CuckooHashTableNode::RelocateBitMap()
{
assert(IsNode() && !IsLeaf() && !IsUsingInternalChildMap() && !IsExternalPointerBitMap());
uint64_t children = childMap;
int offset = FindNeighboringEmptySlot() + 4;
assert(1 <= offset && offset <= 7);
int oldOffset = (hash >> 21) & 7;
assert(offset != oldOffset);
if (offset == 4)
{
uint64_t* ptr = CopyToExternalBitMap();
childMap = reinterpret_cast<uint64_t>(ptr);
}
else
{
memcpy(&(this[offset-4]), &(this[oldOffset-4]), sizeof(CuckooHashTableNode));
}
memset(&(this[oldOffset-4]), 0, sizeof(CuckooHashTableNode));
hash &= 0xfff1fffffU;
hash |= offset << 21;
assert(offset == 4 || (this[offset-4].IsOccupied() && !this[offset-4].IsNode()));
}
static const __m128i HASH18_MASK = _mm_set_epi32(0x3ffffU, 0x3ffffU, 0x3ffffU, 0x3ffffU);
static const __m128i HASH_EXPECT_MASK1 = _mm_set_epi32(0x80000000U | (7U << 27),
0x80000000U | (6U << 27),
0x80000000U | (5U << 27),
0x80000000U | (4U << 27));
static const __m128i HASH_EXPECT_MASK2 = _mm_set_epi32(0xf803ffffU, 0xf803ffffU, 0xf803ffffU, 0xf803ffffU);
static inline uint64_t RoundUpToNearestMultipleOf(uint64_t x, uint64_t y)
{
if (x % y == 0) return x;
return x / y * y + y;
}
static inline uint64_t RoundUpToNearestPowerOf2(uint64_t x)
{
uint64_t z = 1;
while (z < x) z *= 2;
return z;
}
static inline void MultiplyBy3(const __m128i& input, __m128i& output)
{
output = _mm_add_epi32(input, input);
output = _mm_add_epi32(input, output);
}
#ifdef ENABLE_STATS
CuckooHashTable::Stats::Stats()
: m_slowpathCount(0)
, m_movedNodesCount(0)
, m_relocatedBitmapsCount(0)
{
memset(m_lcpResultHistogram, 0, sizeof m_lcpResultHistogram);
}
void CuckooHashTable::Stats::ClearStats()
{
m_slowpathCount = 0;
m_movedNodesCount = 0;
m_relocatedBitmapsCount = 0;
memset(m_lcpResultHistogram, 0, sizeof m_lcpResultHistogram);
}
void CuckooHashTable::Stats::ReportStats()
{
printf("Cuckoo HashTable stats:\n");
printf("\tQueryLCP slow-path count = %u\n", m_slowpathCount);
printf("\tInsertion moved nodes count = %u\n", m_movedNodesCount);
printf("\tInsertion relocated bitmaps count = %u\n", m_relocatedBitmapsCount);
printf("\tQueryLCP result histogram (result node IndexLen, not actual LCP):\n");
rep(i, 2, 8)
{
printf("\t\tLCP = %d: %u\n", i, m_lcpResultHistogram[i]);
}
}
#endif
CuckooHashTable::CuckooHashTable()
: ht(nullptr)
, htMask(0)
#ifdef ENABLE_STATS
, stats()
#endif
#ifndef NDEBUG
, m_hasCalledInit(false)
#endif
{ }
void CuckooHashTable::Init(CuckooHashTableNode* _ht, uint64_t _mask)
{
assert(!m_hasCalledInit);
#ifndef NDEBUG
m_hasCalledInit = true;
#endif
ht = _ht;
htMask = _mask;
assert(reinterpret_cast<uintptr_t>(_ht) % 128 == 0);
assert(RoundUpToNearestPowerOf2(_mask + 1) == _mask + 1);
}
uint32_t CuckooHashTable::ReservePositionForInsert(int ilen, uint64_t dkey, uint32_t hash18bit, bool& exist, bool& failed)
{
assert(m_hasCalledInit);
exist = false;
failed = false;
uint32_t expectedHash = hash18bit | ((ilen-1) << 27) | 0x80000000U;
int shiftLen = 64 - 8 * ilen;
uint64_t shiftedKey = dkey >> shiftLen;
uint32_t h1, h2;
h1 = XXH::XXHashFn1(dkey, ilen) & htMask;
h2 = XXH::XXHashFn2(dkey, ilen) & htMask;
if (ht[h1].IsEqual(expectedHash, shiftLen, shiftedKey))
{
exist = true;
return h1;
}
if (ht[h2].IsEqual(expectedHash, shiftLen, shiftedKey))
{
exist = true;
return h2;
}
if (!ht[h1].IsOccupied())
{
return h1;
}
if (!ht[h2].IsOccupied())
{
return h2;
}
uint32_t victimPosition = rand()%2 ? h1 : h2;
HashTableCuckooDisplacement(victimPosition, 1, failed);
if (failed)
{
return -1;
}
assert(!ht[victimPosition].IsOccupied());
return victimPosition;
}
uint32_t CuckooHashTable::Insert(int ilen, int dlen, uint64_t dkey, int firstChild, bool& exist, bool& failed)
{
assert(m_hasCalledInit);
uint32_t hash18bit = XXH::XXHashFn3(dkey, ilen);
hash18bit = hash18bit & ((1<<18) - 1);
uint32_t pos = ReservePositionForInsert(ilen, dkey, hash18bit, exist, failed);
if (!exist && !failed)
{
ht[pos].Init(ilen, dlen, dkey, hash18bit, firstChild);
}
return pos;
}
uint32_t CuckooHashTable::Lookup(int ilen, uint64_t ikey, bool& found)
{
assert(m_hasCalledInit);
found = false;
uint32_t hash18bit = XXH::XXHashFn3(ikey, ilen);
hash18bit = hash18bit & ((1<<18) - 1);
uint32_t expectedHash = hash18bit | ((ilen-1) << 27) | 0x80000000U;
int shiftLen = 64 - 8 * ilen;
uint64_t shiftedKey = ikey >> shiftLen;
uint32_t h1, h2;
h1 = XXH::XXHashFn1(ikey, ilen) & htMask;
h2 = XXH::XXHashFn2(ikey, ilen) & htMask;
MEM_PREFETCH(ht[h1]);
MEM_PREFETCH(ht[h2]);
if (ht[h1].IsEqual(expectedHash, shiftLen, shiftedKey))
{
found = true;
return h1;
}
if (ht[h2].IsEqual(expectedHash, shiftLen, shiftedKey))
{
found = true;
return h2;
}
return -1;
}
CuckooHashTable::LookupMustExistPromise CuckooHashTable::GetLookupMustExistPromise(int ilen, uint64_t ikey)
{
assert(m_hasCalledInit);
uint32_t hash18bit = XXH::XXHashFn3(ikey, ilen);
hash18bit = hash18bit & ((1<<18) - 1);
uint32_t expectedHash = hash18bit | ((ilen-1) << 27) | 0x80000000U;
int shiftLen = 64 - 8 * ilen;
uint64_t shiftedKey = ikey >> shiftLen;
uint32_t h1, h2;
h1 = XXH::XXHashFn1(ikey, ilen) & htMask;
h2 = XXH::XXHashFn2(ikey, ilen) & htMask;
return LookupMustExistPromise(true /*valid*/,
shiftLen,
ht + h1,
ht + h2,
expectedHash,
shiftedKey);
}
int ALWAYS_INLINE CuckooHashTable::QueryLCP(uint64_t key,
uint32_t& idxLen,
uint32_t* allPositions1,
uint32_t* allPositions2,
uint32_t* expectedHash)
{
assert(m_hasCalledInit);
__m128i h1, h2, h3, h4;
uint64_t h5;
XXH::XXHashArray(key, h1, h2, h3, h4, h5);
__m128i hashModMask = _mm_set1_epi32(htMask);
h1 = _mm_and_si128(h1, hashModMask);
h2 = _mm_and_si128(h2, hashModMask);
h4 = _mm_and_si128(h4, hashModMask);
_mm_storeu_si128(reinterpret_cast<__m128i*>(allPositions1 + 4), h1);
_mm_storeu_si128(reinterpret_cast<__m128i*>(allPositions2 + 4), h2);
_mm_storeu_si128(reinterpret_cast<__m128i*>(allPositions1), h4);
*reinterpret_cast<uint64_t*>(allPositions2 + 2) = *reinterpret_cast<uint64_t*>(allPositions1);
MEM_PREFETCH(ht[allPositions1[2]]);
MEM_PREFETCH(ht[allPositions1[3]]);
MEM_PREFETCH(ht[allPositions1[4]]);
MEM_PREFETCH(ht[allPositions1[5]]);
MEM_PREFETCH(ht[allPositions1[6]]);
MEM_PREFETCH(ht[allPositions2[2]]);
MEM_PREFETCH(ht[allPositions2[3]]);
MEM_PREFETCH(ht[allPositions2[4]]);
MEM_PREFETCH(ht[allPositions2[5]]);
MEM_PREFETCH(ht[allPositions2[6]]);
__m128i expect1 = _mm_and_si128(h3, HASH18_MASK);
expect1 = _mm_or_si128(expect1, HASH_EXPECT_MASK1);
_mm_storeu_si128(reinterpret_cast<__m128i*>(expectedHash + 4), expect1);
h5 &= 0x3ffff0003ffffULL;
h5 |= 0x8000000080000000ULL | (3ULL << 59) | (2ULL << 27);
*reinterpret_cast<uint64_t*>(expectedHash + 2) = h5;
int len = 7;
for (; len >= 2; len --)
{
if ((ht[allPositions1[len]].hash & 0xf803ffffU) == expectedHash[len])
{
break;
}
if ((ht[allPositions2[len]].hash & 0xf803ffffU) == expectedHash[len])
{
allPositions1[len] = allPositions2[len];
break;
}
else
{
allPositions1[len] = 0;
}
}
if (len < 2)
{
#ifdef ENABLE_STATS
stats.m_lcpResultHistogram[2]++;
#endif
return 2;
}
#ifndef NDEBUG
{
uint32_t hash18bit = XXH::XXHashFn3(key, len + 1);
hash18bit = hash18bit & ((1<<18) - 1);
uint32_t expectedx = hash18bit | (len << 27) | 0x80000000U;
assert((ht[allPositions1[len]].hash & 0xf803ffffU) == expectedx);
}
#endif
int shiftLen = 64 - 8 * (len + 1);
if (unlikely((ht[allPositions1[len]].minKey >> shiftLen) != (key >> shiftLen))) goto _slowpath;
idxLen = len + 1;
#ifdef ENABLE_STATS
stats.m_lcpResultHistogram[idxLen]++;
#endif
{
uint64_t xorValue = key ^ ht[allPositions1[len]].minKey;
if (!xorValue) return 8;
int z = __builtin_clzll(xorValue);
return z / 8;
}
// slow path handling hash conflict
//
_slowpath:
{
#ifdef ENABLE_STATS
stats.m_slowpathCount++;
#endif
if (ht[allPositions1[7]].IsEqualNoHash(key, 8)) { idxLen = 8; goto _slowpath_end; }
if (ht[allPositions2[7]].IsEqualNoHash(key, 8)) { allPositions1[7] = allPositions2[7]; idxLen = 8; goto _slowpath_end; }
if (ht[allPositions1[6]].IsEqualNoHash(key, 7)) { idxLen = 7; goto _slowpath_end; }
if (ht[allPositions2[6]].IsEqualNoHash(key, 7)) { allPositions1[6] = allPositions2[6]; idxLen = 7; goto _slowpath_end; }
if (ht[allPositions1[5]].IsEqualNoHash(key, 6)) { idxLen = 6; goto _slowpath_end; }
if (ht[allPositions2[5]].IsEqualNoHash(key, 6)) { allPositions1[5] = allPositions2[5]; idxLen = 6; goto _slowpath_end; }
if (ht[allPositions1[4]].IsEqualNoHash(key, 5)) { idxLen = 5; goto _slowpath_end; }
if (ht[allPositions2[4]].IsEqualNoHash(key, 5)) { allPositions1[4] = allPositions2[4]; idxLen = 5; goto _slowpath_end; }
if (ht[allPositions1[3]].IsEqualNoHash(key, 4)) { idxLen = 4; goto _slowpath_end; }
if (ht[allPositions2[3]].IsEqualNoHash(key, 4)) { allPositions1[3] = allPositions2[3]; idxLen = 4; goto _slowpath_end; }
if (ht[allPositions1[2]].IsEqualNoHash(key, 3)) { idxLen = 3; goto _slowpath_end; }
if (ht[allPositions2[2]].IsEqualNoHash(key, 3)) { allPositions1[2] = allPositions2[2]; idxLen = 3; goto _slowpath_end; }
#ifdef ENABLE_STATS
stats.m_lcpResultHistogram[2]++;
#endif
return 2;
_slowpath_end:
#ifdef ENABLE_STATS
stats.m_lcpResultHistogram[idxLen]++;
#endif
uint64_t xorValue = key ^ ht[allPositions1[idxLen-1]].minKey;
if (!xorValue) return 8;
int z = __builtin_clzll(xorValue);
return z / 8;
}
}
void CuckooHashTable::HashTableCuckooDisplacement(uint32_t victimPosition, int rounds, bool& failed)
{
if (rounds > 1000)
{
failed = true;
return;
}
assert(ht[victimPosition].IsOccupied());
if (likely(ht[victimPosition].IsNode()))
{
int ilen = ht[victimPosition].GetIndexKeyLen();
uint64_t ikey = ht[victimPosition].GetIndexKey();
uint32_t h1, h2;
h1 = XXH::XXHashFn1(ikey, ilen) & htMask;
h2 = XXH::XXHashFn2(ikey, ilen) & htMask;
if (h1 == victimPosition)
{
swap(h1, h2);
}
assert(h2 == victimPosition);
if (ht[h1].IsOccupied())
{
HashTableCuckooDisplacement(h1, rounds+1, failed);
if (failed) return;
}
assert(!ht[h1].IsOccupied());