forked from JackTrapper/murmur-delphi
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMurmurHash.pas
More file actions
1293 lines (1114 loc) · 28.2 KB
/
MurmurHash.pas
File metadata and controls
1293 lines (1114 loc) · 28.2 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
{------------------------------------------------------------------------------
MurmurHash was written by Austin Appleby, and is placed in the public
domain. The author hereby disclaims copyright to this source code.
Note - This code makes a few assumptions about how your machine behaves -
1. We can read a 4-byte value from any address without crashing
2. sizeof(int) == 4
And it has a few limitations -
1. It will not work incrementally.
2. It will not produce the same results on little-endian and big-endian
machines.
Note :
The x86 and x64 versions do _not_ produce the same results, as the
algorithms are optimized for their respective platforms. You can still
compile and run any of them on any platform, but your performance with the
non-native version will be less than optimal.
}
unit MurMurHash;
interface
uses
SysUtils;
type
TMurMur1 = class
public
class function Hash(const Key; const KeyLen, Seed: UInt32): UInt32;
class function HashAligned(const Key; const KeyLen, Seed: UInt32): UInt32;
end;
{
Includes a sample implementation of MurmurHash2A designed to work incrementally.
Usage:
var
hasher: TMurMur2;
hash: UInt32;
begin
hasher := hasher.Create(seed);
hasher.Add(data1, size1);
hasher.Add(data2, size2);
// ...
hasher.Add(dataN, sizeN);
hash := hasher.Hash;
end;
}
TMurMur2 = class(TObject)
strict private
fHash,
fTail,
fCount,
fSize: UInt32;
const
m: UInt32 = $5bd1e995;
r: UInt32 = 24;
procedure MixTail(const data: PByteArray; var dataLen: UInt32; var idx: Integer);
public
class function Hash(const Key; const KeyLen, Seed: UInt32): UInt32;
class function HashA(const Key; const KeyLen, Seed: UInt32): UInt32;
class function Hash64A(const Key; const KeyLen: UInt32; const Seed: UInt64): UInt64;
class function Hash64B(const Key; const KeyLen: UInt32; const Seed: UInt64): UInt64;
class function HashNeutral(const Key; const KeyLen, Seed: UInt32): UInt32;
class function HashAligned(const Key; const KeyLen, Seed: UInt32): UInt32;
constructor Create(Seed: UInt32);
procedure Add(const value; dataLen: UInt32);
function Calculate: UInt32;
end;
TMurMur3 = class
public
class function Hash_x86_32(const Key; const KeyLen, Seed: UInt32): UInt32;
class function Hash_x86_128(const Key; const KeyLen, Seed: UInt32): string;
class function Hash_x64_128(const Key; const KeyLen, Seed: UInt32): string;
class function Hash(const Key; const KeyLen, Seed: UInt32): string;
end;
implementation
uses
Math;
{ Utils }
type
UInt32Array = array[0..8191] of UInt32;
PUInt32Array = ^UInt32Array;
UInt64Array = array[0..4095] of UInt64;
PUInt64Array = ^UInt64Array;
const
SH_AMNT_4 = 4;
SH_AMNT_8 = 8;
SH_AMNT_10 = 10;
SH_AMNT_13 = 13;
SH_AMNT_15 = 15;
SH_AMNT_16 = 16;
SH_AMNT_17 = 17;
SH_AMNT_24 = 24;
SH_AMNT_32 = 32;
SH_AMNT_47 = 47;
SH_AMNT_64 = 64;
function ROTL32(x: UInt32; n: Int8): UInt32; inline;
begin
Result := (x shl n) or (x shr (SH_AMNT_32 - n));
end;
function ROTL64(x: UInt64; n: Int8): UInt64; inline;
begin
Result := (x shl n) or (x shr (SH_AMNT_64 - n));
end;
{------------------------------------------------------------------------------
Finalization mix - force all bits of a hash block to avalanche
}
function fmix32(h: UInt32): UInt32; inline;
begin
h := h xor (h shr SH_AMNT_16);
h := h * $85ebca6b;
h := h xor (h shr SH_AMNT_13);
h := h * $c2b2ae35;
h := h xor (h shr SH_AMNT_16);
Result := h;
end;
function fmix64(h: UInt64): UInt64; inline;
begin
h := h xor (h shr 33);
h := h * $ff51afd7ed558ccd;
h := h xor (h shr 33);
h := h * $c4ceb9fe1a85ec53;
h := h xor (h shr 33);
Result := h;
end;
procedure mmix(var hash, k: UInt32; m, r: UInt32); inline;
begin
k := k * m;
k := k xor (k shr r);
k := k * m;
hash := hash * m;
hash := hash xor k;
end;
{------------------------------------------------------------------------------
Block read - if your platform needs to do endian-swapping or can only
handle aligned reads, do the conversion here
}
function getblock32(const ptr: PUInt32Array; idx: Integer): UInt32; inline;
begin
Result := ptr[idx];
end;
function getblock64(const ptr: PUInt64Array; idx: Integer): UInt64; inline;
begin
Result := ptr[idx];
end;
{ TMurMur1 }
// https://github.com/rurban/smhasher/blob/master/MurmurHash1.cpp
// objsize: 0-0x157: 343
class function TMurMur1.Hash(const Key; const KeyLen, Seed: UInt32): UInt32;
const
m: UInt32 = $c6a4a793;
var
k: UInt32;
data: PByteArray;
i, len: Integer;
label
case_0, case_1, case_2, case_3;
begin
Result := Seed xor (KeyLen * m);
data := PByteArray(@Key);
len := KeyLen;
i := 0;
while len >= 4 do
begin
k := PUInt32(@(data[i]))^;
Result := Result + k;
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_16);
Inc(i, 4);
Dec(len, 4);
end;
case len of
3: goto case_3;
2: goto case_2;
1: goto case_1;
0: goto case_0;
end;
case_3:
Result := Result + (data[i + 2] shl SH_AMNT_16);
case_2:
Result := Result + (data[i + 1] shl SH_AMNT_8);
case_1:
Result := Result + data[i];
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_16);
case_0:
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_10);
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_17);
end;
{------------------------------------------------------------------------------
TMurmur1.HashAligned, by Austin Appleby
Same algorithm as TMurmur1.Hash, but only does aligned reads - should be
safer on certain platforms.
Performance should be equal to or better than the simple version.
objsize: 0x160-0x4e3: 899
}
class function TMurMur1.HashAligned(const Key; const KeyLen, Seed: UInt32): UInt32;
const
m: UInt32 = $c6a4a793;
var
t, d: UInt32;
data: PByteArray;
i, len, align, sl, sr, pack: Integer;
label
align_case_1, align_case_2, align_case_3,
pack_case_0, pack_case_1, pack_case_2, pack_case_3,
case_0, case_1, case_2, case_3;
begin
Result := Seed xor (KeyLen * m);
data := PByteArray(@Key);
len := KeyLen;
i := 0;
align := UInt64(data[i]) and 3;
if (align > 0) and (len >= 4) then
begin
// Pre-load the temp registers
t := 0;
case align of
1: goto align_case_1;
2: goto align_case_2;
3: goto align_case_3;
end;
align_case_1:
t := t or (data[2] shl SH_AMNT_16);
align_case_2:
t := t or (data[1] shl SH_AMNT_8);
align_case_3:
t := t or data[0];
t := t shl (SH_AMNT_8 * align);
Inc(i, 4 - align);
Dec(len, 4 - align);
sl := SH_AMNT_8 * (4 - align);
sr := SH_AMNT_8 * align;
// Mix
while len >= 4 do
begin
d := PUInt32(@(data[i]))^;
t := (t shr sr) or (d shl sl);
Result := Result + t;
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_16);
t := d;
Inc(i, 4);
Dec(len, 4);
end;
pack := Min(len, align);
d := 0;
case pack of
3: goto pack_case_3;
2: goto pack_case_2;
1: goto pack_case_1;
0: goto pack_case_0;
end;
pack_case_3:
d := d or (data[i + 2] shl SH_AMNT_16);
pack_case_2:
d := d or (data[i + 1] shl SH_AMNT_8);
pack_case_1:
d := d or data[i];
pack_case_0:
Result := Result + ((SH_AMNT_16 shr sr) or (d shl sl));
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_16);
Inc(i, pack);
Dec(len, pack);
end else
begin
while len >= 4 do
begin
Result := Result + PUInt32(@(data[i]))^;
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_16);
Inc(i, 4);
Dec(len, 4);
end;
end;
// Handle tail bytes
case len of
3: goto case_3;
2: goto case_2;
1: goto case_1;
0: goto case_0;
end;
case_3:
Result := Result + (data[i + 2] shl SH_AMNT_16);
case_2:
Result := Result + (data[i + 1] shl SH_AMNT_8);
case_1:
Result := Result + data[i];
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_16);
case_0:
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_10);
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_17);
end;
{ TMurMur2 }
// https://github.com/rurban/smhasher/blob/master/MurmurHash2.cpp
// objsize: 0-0x166: 358
class function TMurMur2.Hash(const Key; const KeyLen, Seed: UInt32): UInt32;
// 'm' and 'r' are mixing constants generated offline. They're not really 'magic', they just happen to work well.
const
m: UInt32 = $5bd1e995;
var
k: UInt32;
data: PByteArray;
i, len: Integer;
label
case_0, case_1, case_2, case_3;
begin
// Initialize the hash to a 'random' value
Result := Seed or KeyLen;
data := PByteArray(@Key);
len := KeyLen;
i := 0;
// Mix 4 bytes at a time into the hash
while len >= 4 do
begin
k := PUInt32(@(data[i]))^;
mmix(Result, k, m, SH_AMNT_24);
Inc(i, 4);
Dec(len, 4);
end;
// Handle the last few bytes of the input array
case len of
3: goto case_3;
2: goto case_2;
1: goto case_1;
0: goto case_0;
end;
case_3:
Result := Result xor data[i + 2] shl SH_AMNT_16;
case_2:
Result := Result xor data[i + 1] shl SH_AMNT_8;
case_1:
Result := Result xor data[i];
Result := Result * m;
case_0:
// Do a few final mixes of the hash to ensure the last few bytes are well-incorporated.
Result := Result xor (Result shr SH_AMNT_13);
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_15);
end;
{------------------------------------------------------------------------------
TMurMur2.HashA, by Austin Appleby
This is a variant of TMurMur2.Hash modified to use the Merkle-Damgard
construction. Bulk speed should be identical to TMurMur2.Hash, small-key speed
will be 10%-20% slower due to the added overhead at the end of the hash.
This variant fixes a minor issue where null keys were more likely to
collide with each other than expected, and also makes the function
more amenable to incremental implementations.
objsize: 0x500-0x697: 407
}
class function TMurMur2.HashA(const Key; const KeyLen, Seed: UInt32): UInt32;
const
m: UInt32 = $5bd1e995;
var
k, l, t: UInt32;
data: PByteArray;
i, len: Integer;
label
case_0, case_1, case_2, case_3;
begin
Result := Seed;
data := PByteArray(@Key);
len := KeyLen;
l := len;
i := 0;
while len >= 4 do
begin
k := PUInt32(@(data[i]))^;
mmix(Result, k, m, SH_AMNT_24);
Inc(i, 4);
Dec(len, 4);
end;
t := 0;
case len of
3: goto case_3;
2: goto case_2;
1: goto case_1;
0: goto case_0;
end;
case_3:
t := t xor (data[i + 2] shl SH_AMNT_16);
case_2:
t := t xor (data[i + 1] shl SH_AMNT_8);
case_1:
t := t xor data[i];
case_0:
mmix(Result, t, m, SH_AMNT_24);
mmix(Result, l, m, SH_AMNT_24);
Result := Result xor (Result shr SH_AMNT_13);
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_15);
end;
{------------------------------------------------------------------------------
TMurMur2.Hash64A, 64-bit versions, by Austin Appleby
The same caveats as 32-bit TMurMur2.Hash apply here - beware of alignment
and endian-ness issues if used across multiple platforms.
64-bit hash for 64-bit platforms
objsize: 0x170-0x321: 433
}
class function TMurMur2.Hash64A(const Key; const KeyLen: UInt32; const Seed: UInt64): UInt64;
const
m: UInt64 = $c6a4a7935bd1e995;
var
k: UInt64;
data: PByteArray;
i, len: Integer;
label
case_0, case_1, case_2, case_3, case_4, case_5, case_6, case_7;
begin
Result := Seed xor (KeyLen * m);
data := PByteArray(@Key);
len := KeyLen;
i := 0;
while len >= 8 do
begin
k := PUInt64(@(data[i]))^;
k := k * m;
k := k xor (k shr SH_AMNT_47);
k := k * m;
Result := Result xor k;
Result := Result * m;
Inc(i, 8);
Dec(len, 8);
end;
case len of
7: goto case_7;
6: goto case_6;
5: goto case_5;
4: goto case_4;
3: goto case_3;
2: goto case_2;
1: goto case_1;
0: goto case_0;
end;
case_7:
Result := Result xor (UInt64(data[i + 6]) shl 48);
case_6:
Result := Result xor (UInt64(data[i + 5]) shl 40);
case_5:
Result := Result xor (UInt64(data[i + 4]) shl SH_AMNT_32);
case_4:
Result := Result xor (UInt64(data[i + 3]) shl SH_AMNT_24);
case_3:
Result := Result xor (UInt64(data[i + 2]) shl SH_AMNT_16);
case_2:
Result := Result xor (UInt64(data[i + 1]) shl SH_AMNT_8);
case_1:
Result := Result xor UInt64(data[i]);
Result := Result * m;
case_0:
Result := Result xor (Result shr SH_AMNT_47);
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_47);
end;
// 64-bit hash for 32-bit platforms
// objsize: 0x340-0x4fc: 444
class function TMurMur2.Hash64B(const Key; const KeyLen: UInt32; const Seed: UInt64): UInt64;
const
m: UInt32 = $5bd1e995;
var
h1, h2, k1, k2: UInt32;
data: PByteArray;
i, len: Integer;
label
case_0, case_1, case_2, case_3;
begin
h1 := UInt32(Seed) xor KeyLen;
h2 := UInt32(Seed shr SH_AMNT_32);
data := PByteArray(@Key);
len := KeyLen;
i := 0;
while len >= 8 do
begin
k1 := PUInt32(@(data[i]))^;
k1 := k1 * m;
k1 := k1 * (k1 shr SH_AMNT_24);
k1 := k1 * m;
h1 := h1 * m;
h1 := h1 xor k1;
Inc(i, 4);
Dec(len, 4);
k2 := PUInt32(@(data[i]))^;
k2 := k2 * m;
k2 := k2 xor (k2 shr SH_AMNT_24);
k2 := k2 * m;
h2 := h2 * m;
h2 := h2 xor k2;
Inc(i, 4);
Dec(len, 4);
end;
if len >= 4 then
begin
k1 := PUInt32(@(data[i]))^;
k1 := k1 * m;
k1 := k1 xor (k1 shr SH_AMNT_24);
k1 := k1 * m;
h1 := h1 * m;
h1 := h1 xor k1;
Inc(i, 4);
Dec(len, 4);
end;
case len of
3: goto case_3;
2: goto case_2;
1: goto case_1;
0: goto case_0;
end;
case_3:
h2 := h2 xor (data[i + 2] shl SH_AMNT_16);
case_2:
h2 := h2 xor (data[i + 1] shl SH_AMNT_8);
case_1:
h2 := h2 xor data[i];
h2 := h2 * m;
case_0:
h1 := h1 xor (h2 shr 18);
h1 := h1 * m;
h2 := h2 xor (h1 shr 22);
h2 := h2 * m;
h1 := h1 xor (h2 shr SH_AMNT_17);
h1 := h1 * m;
h2 := h2 xor (h1 shr 19);
h2 := h2 * m;
Result := h1;
Result := (Result shl SH_AMNT_32) or h2;
end;
{------------------------------------------------------------------------------
TMurMur2.HashNeutral, by Austin Appleby
Same as TMurMur2.Hash, but endian- and alignment-neutral.
Half the speed though, alas.
}
class function TMurMur2.HashNeutral(const Key; const KeyLen, Seed: UInt32): UInt32;
const
m: UInt32 = $5bd1e995;
var
k: UInt32;
data: PByteArray;
i, len: Integer;
label
case_0, case_1, case_2, case_3;
begin
Result := Seed xor KeyLen;
data := PByteArray(@Key);
len := KeyLen;
i := 0;
while len >= 4 do
begin
k := data[i];
k := k or (data[i + 1] shl SH_AMNT_8);
k := k or (data[i + 2] shl SH_AMNT_16);
k := k or (data[i + 3] shl SH_AMNT_24);
k := k * m;
k := k xor (k shr SH_AMNT_24);
k := k * m;
Result := Result * m;
Result := Result xor k;
Inc(i, 4);
Dec(len, 4);
end;
case len of
3: goto case_3;
2: goto case_2;
1: goto case_1;
0: goto case_0;
end;
case_3:
Result := Result xor data[i + 2] shl SH_AMNT_16;
case_2:
Result := Result xor data[i + 1] shl SH_AMNT_8;
case_1:
Result := Result xor data[i];
Result := Result * m;
case_0:
Result := Result xor (Result shr SH_AMNT_13);
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_15);
end;
{------------------------------------------------------------------------------
TMurMur2.HashAligned, by Austin Appleby
Same algorithm as TMurMur2.Hash, but only does aligned reads - should be safer
on certain platforms.
Performance will be lower than TMurMur2.Hash
}
class function TMurMur2.HashAligned(const Key; const KeyLen, Seed: UInt32): UInt32;
procedure MIX(var h, k: UInt32; m: UInt32); inline;
begin
k := k * m;
k := k xor (k shr SH_AMNT_24);
k := k * m;
h := h * m;
h := h xor k;
end;
const
m: UInt32 = $5bd1e995;
var
k, t, d: UInt32;
data: PByteArray;
i, len, align, sl, sr: Integer;
label
align_case_1, align_case_2, align_case_3,
pack_case_1, pack_case_2, pack_case_3,
t1_case_0, t1_case_1, t1_case_2, t1_case_3,
t2_case_0, t2_case_1, t2_case_2, t2_case_3,
t3_case_0, t3_case_1, t3_case_2, t3_case_3;
begin
Result := Seed xor KeyLen;
data := PByteArray(@Key);
len := KeyLen;
i := 0;
align := UInt64(data[i]) and 3;
if (align > 0) and (len >= 4) then
begin
// Pre-load the temp registers
t := 0;
case align of
1: goto align_case_1;
2: goto align_case_2;
3: goto align_case_3;
end;
align_case_1:
t := t or (data[2] shl SH_AMNT_16);
align_case_2:
t := t or (data[1] shl SH_AMNT_8);
align_case_3:
t := t or data[0];
t := t shl (SH_AMNT_8 * align);
Inc(i, 4 - align);
Dec(len, 4 - align);
sl := SH_AMNT_8 * (4 - align);
sr := SH_AMNT_8 * align;
// Mix
while len >= 4 do
begin
d := PUInt32(@(data[i]))^;
t := (t shr sr) or (d shl sl);
k := t;
MIX(Result, k, m);
t := d;
Inc(i, 4);
Dec(len, 4);
end;
// Handle leftover data in temp registers
d := 0;
if(len >= align) then
begin
case align of
3: goto pack_case_3;
2: goto pack_case_2;
1: goto pack_case_1;
end;
pack_case_3:
d := d or (data[i + 2] shl SH_AMNT_16);
pack_case_2:
d := d or (data[i + 1] shl SH_AMNT_8);
pack_case_1:
d := d or data[i];
k := (t shr sr) or (d shl sl);
MIX(Result, k, m);
Inc(i, align);
Dec(len, align);
//----------
// Handle tail bytes
case len of
3: goto t1_case_3;
2: goto t1_case_2;
1: goto t1_case_1;
0: goto t1_case_0;
end;
t1_case_3:
Result := Result xor (data[i + 2] shl SH_AMNT_16);
t1_case_2:
Result := Result xor (data[i + 1] shl SH_AMNT_8);
t1_case_1:
Result := Result xor data[i];
Result := Result * m;
end else
begin
case len of
3: goto t2_case_3;
2: goto t2_case_2;
1: goto t2_case_1;
0: goto t2_case_0;
end;
t2_case_3:
d := d or (data[i + 2] shl SH_AMNT_16);
t2_case_2:
d := d or (data[i + 1] shl SH_AMNT_8);
t2_case_1:
d := d or data[i];
t2_case_0:
Result := Result xor (t shr sr) or (d shl sl);
Result := Result * m;
end;
end else
begin
while len >= 4 do
begin
k := PUInt32(@(data[i]))^;
MIX(Result, k, m);
Inc(i, 4);
Dec(len, 4);
end;
//----------
// Handle tail bytes
case len of
3: goto t3_case_3;
2: goto t3_case_2;
1: goto t3_case_1;
0: goto t3_case_0;
end;
t3_case_3:
Result := Result xor (data[i + 2] shl SH_AMNT_16);
t3_case_2:
Result := Result xor (data[i + 1] shl SH_AMNT_8);
t3_case_1:
Result := Result xor data[i];
Result := Result * m;
end;
t1_case_0:
t3_case_0:
Result := Result xor (Result shr SH_AMNT_13);
Result := Result * m;
Result := Result xor (Result shr SH_AMNT_15);
end;
constructor TMurMur2.Create(Seed: UInt32);
begin
fHash := Seed;
fTail := 0;
fCount := 0;
fSize := 0;
end;
procedure TMurMur2.Add(const value; dataLen: UInt32);
var
data: PByteArray;
len, k: UInt32;
i: Integer;
begin
fSize := fSize + dataLen;
len := dataLen;
data := PByteArray(@value);
MixTail(data, len, i);
while len >= 4 do
begin
k := PUInt32(@(data[i]))^;
mmix(fHash, k, m, r);
Inc(i, 4);
Dec(len, 4);
end;
MixTail(data, len, i);
end;
function TMurMur2.Calculate: UInt32;
begin
mmix(fHash, fTail, m, r);
mmix(fHash, fSize, m, r);
fHash := fHash xor (fHash shr 13);
fHash := fHash * m;
fHash := fHash xor (fHash shr 15);
Result := fHash;
end;
procedure TMurMur2.MixTail(const data: PByteArray; var dataLen: UInt32; var idx: Integer);
begin
while (dataLen > 0) and ((dataLen < 4) or (fCount > 0)) do
begin
fTail := fTail or (PByte(@(data[idx]))^ shl (fCount * SH_AMNT_8));
Inc(idx);
Inc(fCount);
Dec(dataLen);
if fCount = 4 then
begin
mmix(fHash, fTail, m, r);
fTail := 0;
fCount := 0;
end;
end;
end;
{ TMurMur3 }
// https://github.com/rurban/smhasher/blob/master/MurmurHash3.cpp
//-----------------------------------------------------------------------------
// objsize: 0x0-0x15f: 351
class function TMurMur3.Hash_x86_32(const Key; const KeyLen, Seed: UInt32): UInt32;
const
c1: UInt32 = $cc9e2d51;
c2: UInt32 = $1b873593;
var
data: PByteArray;
blocks: PUInt32Array;
i, nBlocks: Integer;
len, k1: UInt32;
label
case_0, case_1, case_2, case_3;
begin
len := KeyLen;
data := PByteArray(@Key);
nblocks := Math.Floor(len / 4);
Result := Seed;
//----------
// body
blocks := PUInt32Array(data);
for i := 0 to nblocks - 1 do
begin
k1 := getblock32(blocks, i);
k1 := k1 * c1;
k1 := ROTL32(k1, SH_AMNT_15);
k1 := k1 * c2;
Result := Result xor k1;
Result := ROTL32(Result, SH_AMNT_13);
Result := Result * 5 + $e6546b64;
end;
//----------
// tail
i := nblocks * 4;
k1 := 0;
case len and 3 of
3: goto case_3;
2: goto case_2;
1: goto case_1;
0: goto case_0;
end;
case_3:
k1 := k1 xor (data[i + 2] shl SH_AMNT_16);
case_2:
k1 := k1 xor (data[i + 1] shl SH_AMNT_8);
case_1:
k1 := k1 xor data[i];
k1 := k1 * c1;
k1 := ROTL32(k1, SH_AMNT_15);
k1 := k1 * c2;
Result := Result xor k1;
case_0:
//----------
// finalization
Result := Result xor len;
Result := fmix32(Result);
end;
//-----------------------------------------------------------------------------
// objsize: 0x160-0x4bb: 859
class function TMurMur3.Hash_x86_128(const Key; const KeyLen, Seed: UInt32): string;
const
c1: UInt32 = $239b961b;
c2: UInt32 = $ab0e9789;
c3: UInt32 = $38b34ae5;
c4: UInt32 = $a1e38b93;
var
data: PByteArray;
blocks: PUInt32Array;
i, nBlocks: Integer;
len,
h1, h2, h3, h4,
k1, k2, k3, k4: UInt32;
label
case_0, case_1, case_2, case_3, case_4, case_5, case_6, case_7, case_8,
case_9, case_10, case_11, case_12, case_13, case_14, case_15;
begin
len := KeyLen;