-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CITY_1_1_1.pas
716 lines (637 loc) · 21.1 KB
/
CITY_1_1_1.pas
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
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
CITY hash calculation - version 1.1.1
Implementation of CITY hash version 1.1.1.
Version 2.1.1 (2023-04-15)
Last change 2024-04-28
©2016-2023 František Milt
Contacts:
František Milt: frantisek.milt@gmail.com
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.CityHash
Dependencies:
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
BasicUIM - github.com/TheLazyTomcat/Lib.BasicUIM
BitOps - github.com/TheLazyTomcat/Lib.BitOps
HashBase - github.com/TheLazyTomcat/Lib.HashBase
* SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
SimpleCPUID is required only when PurePascal symbol is not defined.
Library SimpleCPUID might also be required as an indirect dependency.
Indirect dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
StaticMemoryStream - github.com/TheLazyTomcat/Lib.StaticMemoryStream
StrRect - github.com/TheLazyTomcat/Lib.StrRect
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit CITY_1_1_1;
{$INCLUDE 'CITY_defs.inc'}
interface
uses
AuxTypes,
CITY_Common;
{===============================================================================
Utility functions - declaration
===============================================================================}
// Hash 128 input bits down to 64 bits of output.
// This is intended to be a reasonably good hash function.
Function Hash128to64(const x: UInt128): UInt64;{$IFDEF CanInline} inline;{$ENDIF}
{===============================================================================
Main hash functions - declaration
===============================================================================}
// Hash function for a byte array. Most useful in 32-bit binaries.
Function CityHash32(s: Pointer; len: TMemSize): UInt32;
// Hash function for a byte array.
Function CityHash64(s: Pointer; len: TMemSize): UInt64;
// Hash function for a byte array. For convenience, a 64-bit seed is also
// hashed into the result.
Function CityHash64WithSeed(s: Pointer; len: TMemSize; seed: UInt64): UInt64;{$IFDEF CanInline} inline;{$ENDIF}
// Hash function for a byte array. For convenience, two seeds are also
// hashed into the result.
Function CityHash64WithSeeds(s: Pointer; len: TMemSize; seed0,seed1: UInt64): UInt64;{$IF Defined(CanInline) and not Defined(FPC)} inline;{$IFEND}
// Hash function for a byte array. For convenience, a 128-bit seed is also
// hashed into the result.
Function CityHash128WithSeed(s: Pointer; len: TMemSize; seed: UInt128): UInt128;
// Hash function for a byte array.
Function CityHash128(s: Pointer; len: TMemSize): UInt128;
{===============================================================================
CRC hash functions - declaration
===============================================================================}
// Hash function for a byte array. Sets result[0] ... result[3].
procedure CityHashCrc256(s: Pointer; len: TMemSize; out result: UInt256);
// Hash function for a byte array. For convenience, a 128-bit seed is also
// hashed into the result.
Function CityHashCrc128WithSeed(s: Pointer; len: TMemSize; seed: UInt128): UInt128;
// Hash function for a byte array.
Function CityHashCrc128(s: Pointer; len: TMemSize): UInt128;
implementation
{===============================================================================
Utility functions - implementation
===============================================================================}
Function Hash128to64(const x: UInt128): UInt64;
begin
Result := CITY_Common.Hash128to64(x);
end;
{===============================================================================
Main hash functions - implementation
===============================================================================}
{-------------------------------------------------------------------------------
Main hash functions - internals
-------------------------------------------------------------------------------}
Function Hash32Len0to4(s: Pointer; len: TMemSize): UInt32;
var
b,c: UInt32;
i: TMemSize;
v: Int8;
begin
b := 0;
c := 9;
If Len > 0 then
For i := 0 to Pred(Len) do
begin
v := PInt8(PTR_ADVANCE(s,i))^;
b := UInt32((Int64(b) * c1) + v);
c := c xor b;
end;
Result := fmix(Mur(b,Mur(len,c)));
end;
//------------------------------------------------------------------------------
Function Hash32Len5to12(s: Pointer; len: TMemSize): UInt32;
var
a,b,c,d: UInt32;
begin
a := len; b := len * 5; c := 9; d := b;
a := a + Fetch32(s);
b := b + Fetch32(s,len - 4);
c := c + Fetch32(s ,(len shr 1) and 4);
Result := fmix(Mur(c,Mur(b,Mur(a,d))));
end;
//------------------------------------------------------------------------------
Function Hash32Len13to24(s: Pointer; len: TMemSize): UInt32;
var
a,b,c,d,e,f,h: UInt32;
begin
a := Fetch32(s,PtrUInt(Int64(-4) + (len shr 1)));
b := Fetch32(s,4);
c := Fetch32(s,len - 8);
d := Fetch32(s,len shr 1);
e := Fetch32(s);
f := Fetch32(s,len - 4);
h := len;
result := fmix(Mur(f,Mur(e,Mur(d,Mur(c,Mur(b,Mur(a,h)))))));
end;
//------------------------------------------------------------------------------
Function HashLen0to16(s: Pointer; len: TMemSize): UInt64;
var
a,b,c,d: UInt64;
mul: UInt64;
y,z: UInt32;
begin
case len of
1..3:
begin
a := PUInt8(s)^;
b := PUInt8(PTR_ADVANCE(s,len shr 1))^;
c := PUInt8(PTR_ADVANCE(s,len - 1))^;
y := UInt32(a) + (UInt32(b) shl 8);
z := len + (UInt32(c) shl 2);
Result := ShiftMix((y * k2) xor (z * k0)) * k2;
end;
4..7:
begin
mul := k2 + UInt64(len) * 2;
a := Fetch32(s);
Result := HashLen16(len + (a shl 3),Fetch32(s,len - 4),mul);
end;
8..High(len):
begin
mul := k2 + UInt64(len) * 2;
a := Fetch64(s) + k2;
b := Fetch64(s,len - 8);
c := Rotate(b,37) * mul + a;
d := (Rotate(a,25) + b) * mul;
Result := HashLen16(c,d,mul);
end;
else
Result := k2;
end;
end;
//------------------------------------------------------------------------------
// This probably works well for 16-byte strings as well, but it may be overkill
// in that case.
Function HashLen17to32(s: Pointer; len: TMemSize): UInt64;
var
a,b,c,d: UInt64;
mul: UInt64;
begin
mul := k2 + UInt64(len) * 2;
a := Fetch64(s) * k1;
b := Fetch64(s,8);
c := Fetch64(s,len - 8) * mul;
d := Fetch64(s,len - 16) * k2;
Result := HashLen16(Rotate(a + b,43) + Rotate(c,30) + d,a + Rotate(b + k2,18) + c,mul);
end;
//------------------------------------------------------------------------------
// Return a 16-byte hash for 48 bytes. Quick and dirty.
// Callers do best to use "random-looking" values for a and b.
Function WeakHashLen32WithSeeds(w,x,y,z,a,b: UInt64): UInt128; overload;
var
c: UInt64;
begin
a := a + w;
b := Rotate(b + a + z,21);
c := a;
a := a + x;
a := a + y;
b := b + Rotate(a,44);
Result.First := a + z;
Result.Second := b + c;
end;
//------------------------------------------------------------------------------
// Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
Function WeakHashLen32WithSeeds(s: Pointer; a,b: UInt64): UInt128; overload;
begin
Result := WeakHashLen32WithSeeds(Fetch64(s),Fetch64(s,8),Fetch64(s,16),Fetch64(s,24),a,b);
end;
//------------------------------------------------------------------------------
// Return an 8-byte hash for 33 to 64 bytes.
Function HashLen33to64(s: Pointer; len: TMemSize): UInt64;
var
mul,a,b,c,d,e,f,g,h,u,v,w,x,y,z: UInt64;
begin
mul := k2 + UInt64(len) * 2;
a := Fetch64(s) * k2;
b := Fetch64(s,8);
c := Fetch64(s,len - 24);
d := Fetch64(s,len - 32);
e := Fetch64(s,16) * k2;
f := Fetch64(s,24) * 9;
g := Fetch64(s,len - 8);
h := Fetch64(s,len - 16) * mul;
u := Rotate(a + g,43) + (Rotate(b,30) + c) * 9;
v := ((a + g) xor d) + f + 1;
w := EndianSwap((u + v) * mul) + h;
x := Rotate(e + f,42) + c;
y := (EndianSwap((v + w) * mul) + g) * mul;
z := e + f + c;
a := EndianSwap((x + z) * mul + y) + b;
b := ShiftMix((z + a) * mul + d + h) * mul;
Result := b + x;
end;
//------------------------------------------------------------------------------
// A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
// of any length representable in signed long. Based on City and Murmur.
Function CityMurmur(s: Pointer; len: TMemSize; seed: UInt128): UInt128;
var
a,b,c,d: UInt64;
l: PtrInt;
s_temp: Pointer;
begin
s_temp := s;
a := UInt128Low64(seed);
b := UInt128High64(seed);
l := len - 16;
If l <= 0 then // len <= 16
begin
a := ShiftMix(a * k1) * k1;
c := b * k1 + HashLen0to16(s_temp,len);
If len >= 8 then
d := ShiftMix(a + Fetch64(s_temp))
else
d := ShiftMix(a + c);
end
else
begin
c := HashLen16(Fetch64(s_temp,len - 8) + k1,a);
d := HashLen16(b + len,c + Fetch64(s_temp,len - 16));
a := a + d;
repeat
a := a xor (ShiftMix(Fetch64(s_temp) * k1) * k1);
a := a * k1;
b := b xor a;
c := c xor (ShiftMix(Fetch64(s_temp,8) * k1) * k1);
c := c * k1;
d := d xor c;
PTR_ADVANCEVAR(s_temp,16);
l := l - 16;
until l <= 0;
end;
a := HashLen16(a,c);
b := HashLen16(d,b);
Result := UInt128Make(a xor b,HashLen16(b,a));
end;
{-------------------------------------------------------------------------------
Main hash functions - public functions
-------------------------------------------------------------------------------}
Function CityHash32(s: Pointer; len: TMemSize): UInt32;
var
h,g,f: UInt32;
a0,a1,a2,a3,a4: UInt32;
iters: TMemSize;
s_temp: Pointer;
begin
s_temp := s;
If len <= 24 then
begin
case len of
0..4: Result := Hash32Len0to4(s_temp,len);
5..12: Result := Hash32Len5to12(s_temp,len);
else
Result := Hash32Len13to24(s_temp,len);
end;
Exit;
end;
// len > 24
h := len; g := c1 * len; f := g;
a0 := Rotate32(Fetch32(s_temp,len - 4) * c1,17) * c2;
a1 := Rotate32(Fetch32(s_temp,len - 8) * c1,17) * c2;
a2 := Rotate32(Fetch32(s_temp,len - 16) * c1,17) * c2;
a3 := Rotate32(Fetch32(s_temp,len - 12) * c1,17) * c2;
a4 := Rotate32(Fetch32(s_temp,len - 20) * c1,17) * c2;
h := h xor a0;
h := Rotate32(h,19);
h := h * 5 + $e6546b64;
h := h xor a2;
h := Rotate32(h,19);
h := h * 5 + $e6546b64;
g := g xor a1;
g := Rotate32(g,19);
g := g * 5 + $e6546b64;
g := g xor a3;
g := Rotate32(g,19);
g := g * 5 + $e6546b64;
f := f + a4;
f := Rotate32(f,19);
f := f * 5 + $e6546b64;
iters := (len - 1) div 20;
repeat
a0 := Rotate32(Fetch32(s_temp) * c1,17) * c2;
a1 := Fetch32(s_temp,4);
a2 := Rotate32(Fetch32(s_temp,8) * c1,17) * c2;
a3 := Rotate32(Fetch32(s_temp,12) * c1,17) * c2;
a4 := Fetch32(s_temp,16);
h := h xor a0;
h := Rotate32(h,18);
h := h * 5 + $e6546b64;
f := f + a1;
f := Rotate32(f,19);
f := f * c1;
g := g + a2;
g := Rotate32(g,18);
g := g * 5 + $e6546b64;
h := h xor (a3 + a1);
h := Rotate32(h,19);
h := h * 5 + $e6546b64;
g := g xor a4;
g := EndianSwap(g) * 5;
h := h + (a4 * 5);
h := EndianSwap(h);
f := f + a0;
PERMUTE3(f,h,g);
PTR_ADVANCEVAR(s_temp,20);
Dec(iters);
until iters <= 0;
g := Rotate32(g,11) * c1;
g := Rotate32(g,17) * c1;
f := Rotate32(f,11) * c1;
f := Rotate32(f,17) * c1;
h := Rotate32(h + g, 19);
h := h * 5 + $e6546b64;
h := Rotate32(h,17) * c1;
h := Rotate32(h + f, 19);
h := h * 5 + $e6546b64;
h := Rotate32(h,17) * c1;
Result := h;
end;
//------------------------------------------------------------------------------
Function CityHash64(s: Pointer; len: TMemSize): UInt64;
var
x,y,z: UInt64;
v,w: UInt128;
begin
If len <= 64 then
begin
case len of
0..16: Result := HashLen0To16(s,len);
17..32: Result := HashLen17To32(s,len);
else
{33..64}
Result := HashLen33to64(s,len);
end;
Exit;
end;
// For strings over 64 bytes we hash the end first, and then as we
// loop we keep 56 bytes of state: v, w, x, y, and z.
x := Fetch64(s,len - 40);
y := Fetch64(s,len - 16) + Fetch64(s,len - 56);
z := HashLen16(Fetch64(s,len - 48) + len,Fetch64(s,len - 24));
v := WeakHashLen32WithSeeds(PTR_ADVANCE(s,len - 64),len,z);
w := WeakHashLen32WithSeeds(PTR_ADVANCE(s,len - 32),y + k1,x);
x := x * k1 + Fetch64(s);
// Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
len := (len - 1) and not TMemSize(63);
repeat
x := Rotate(x + y + v.First + Fetch64(s,8), 37) * k1;
y := Rotate(y + v.Second + Fetch64(s,48), 42) * k1;
x := x xor w.Second;
y := y + (v.First + Fetch64(s,40));
z := Rotate(z + w.First,33) * k1;
v := WeakHashLen32WithSeeds(s,v.Second * k1,x + w.First);
w := WeakHashLen32WithSeeds(PTR_ADVANCE(s,32),z + w.Second,y + Fetch64(s,16));
SWAP(z,x);
PTR_ADVANCEVAR(s,64);
len := len - 64;
until len <= 0;
Result := HashLen16(HashLen16(v.First,w.First) + ShiftMix(y) * k1 + z,
HashLen16(v.Second,w.Second) + x);
end;
//------------------------------------------------------------------------------
Function CityHash64WithSeed(s: Pointer; len: TMemSize; seed: UInt64): UInt64;
begin
Result := CityHash64WithSeeds(s,len,k2,seed);
end;
//------------------------------------------------------------------------------
Function CityHash64WithSeeds(s: Pointer; len: TMemSize; seed0,seed1: UInt64): UInt64;
begin
Result := HashLen16(CityHash64(s,len) - seed0,seed1);
end;
//------------------------------------------------------------------------------
Function CityHash128WithSeed(s: Pointer; len: TMemSize; seed: UInt128): UInt128;
var
v,w: UInt128;
x,y,z: UInt64;
tail_done: TMemSize;
s_temp: Pointer;
begin
s_temp := s;
If len < 128 then
begin
Result := CityMurmur(s_temp,len,seed);
Exit;
end;
// We expect len >= 128 to be the common case. Keep 56 bytes of state:
// v, w, x, y, and z.
x := UInt128Low64(seed);
y := UInt128High64(seed);
z := len * k1;
v.First := Rotate(y xor k1,49) * k1 + Fetch64(s_temp);
v.Second := Rotate(v.First,42) * k1 + Fetch64(s_temp,8);
w.First := Rotate(y + z,35) * k1 + x;
w.Second := Rotate(x + Fetch64(s_temp,88),53) * k1;
// This is the same inner loop as CityHash64(), manually unrolled.
repeat
x := Rotate(x + y + v.First + Fetch64(s_temp,8),37) * k1;
y := Rotate(y + v.Second + Fetch64(s_temp,48),42) * k1;
x := x xor w.second;
y := y + (v.First + Fetch64(s_temp,40));
z := Rotate(z + w.First,33) * k1;
v := WeakHashLen32WithSeeds(s_temp,v.Second * k1,x + w.First);
w := WeakHashLen32WithSeeds(PTR_ADVANCE(s_temp,32),z + w.Second,y + Fetch64(s_temp,16));
SWAP(z,x);
PTR_ADVANCEVAR(s_temp,64);
x := Rotate(x + y + v.First + Fetch64(s_temp,8),37) * k1;
y := Rotate(y + v.Second + Fetch64(s_temp,48),42) * k1;
x := x xor w.second;
y := y + (v.First + Fetch64(s_temp,40));
z := Rotate(z + w.First,33) * k1;
v := WeakHashLen32WithSeeds(s_temp,v.Second * k1,x + w.First);
w := WeakHashLen32WithSeeds(PTR_ADVANCE(s_temp,32),z + w.Second,y + Fetch64(s_temp,16));
SWAP(z,x);
PTR_ADVANCEVAR(s_temp,64);
len := len - 128;
until len < 128;
x := x + Rotate(v.First + z,49) * k0;
y := y * k0 + Rotate(w.Second,37);
z := z * k0 + Rotate(w.First,27);
w.First := w.First * 9;
v.First := v.First * k0;
// If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
tail_done := 0;
while tail_done < len do
begin
tail_done := tail_done + 32;
y := Rotate(x + y,42) * k0 + v.Second;
w.First := w.First + Fetch64(s_temp,len - tail_done + 16);
x := x * k0 + w.First;
z := z + (w.Second + Fetch64(s_temp,len - tail_done));
w.Second := w.Second + v.First;
v := WeakHashLen32WithSeeds(PTR_ADVANCE(s_temp,len - tail_done),v.First + z,v.Second);
v.First := v.First * k0;
end;
// At this point our 56 bytes of state should contain more than
// enough information for a strong 128-bit hash. We use two
// different 56-byte-to-8-byte hashes to get a 16-byte final result.
x := HashLen16(x,v.First);
y := HashLen16(y + z,w.First);
Result := UInt128Make(HashLen16(x + v.Second,w.Second) + y,
HashLen16(x + w.Second,y + v.Second));
end;
//------------------------------------------------------------------------------
Function CityHash128(s: Pointer; len: TMemSize): UInt128;
begin
If Len >= 16 then
Result := CityHash128WithSeed(PTR_ADVANCE(s,16),len - 16,UInt128Make(Fetch64(s),Fetch64(s,8) + k0))
else
Result := CityHash128WithSeed(s,len,UInt128Make(k0,k1));
end;
{===============================================================================
CRC hash functions - implementation
===============================================================================}
{-------------------------------------------------------------------------------
CRC hash functions - internals
-------------------------------------------------------------------------------}
// Requires len >= 240.
procedure CityHashCrc256Long(s: Pointer; len: TMemSize; seed: UInt32; out result: UInt256);
var
a,b,c,d,e,f,g,h,x,y,z: UInt64;
iters: TMemSize;
s_temp: Pointer;
procedure CHUNK(r: Integer);
begin
PERMUTE3(x,z,y);
b := b + Fetch64(s_temp);
c := c + Fetch64(s_temp,8);
d := d + Fetch64(s_temp,16);
e := e + Fetch64(s_temp,24);
f := f + Fetch64(s_temp,32);
a := a + b;
h := h + f;
b := b + c;
f := f + d;
g := g + e;
e := e + z;
g := g + x;
z := _mm_crc32_u64(z,b + g);
y := _mm_crc32_u64(y,e + h);
x := _mm_crc32_u64(x,f + a);
e := Rotate(e,r);
c := c + e;
PTR_ADVANCEVAR(s_temp,40);
end;
begin
s_temp := s;
a := Fetch64(s_temp,56) + k0;
b := Fetch64(s_temp,96) + k0;
result[0] := HashLen16(b,len);
c := result[0];
result[1] := Fetch64(s_temp,120) * k0 + len;
d := result[1];
e := Fetch64(s_temp,184) + seed;
f := 0;
g := 0;
h := c + d;
x := seed;
y := 0;
z := 0;
// 240 bytes of input per iter.
iters := len div 240;
len := len + TMemSize(-(Int64(iters) * 240));
repeat
CHUNK(0); PERMUTE3(a,h,c);
CHUNK(33); PERMUTE3(a,h,f);
CHUNK(0); PERMUTE3(b,h,f);
CHUNK(42); PERMUTE3(b,h,d);
CHUNK(0); PERMUTE3(b,h,e);
CHUNK(33); PERMUTE3(a,h,e);
Dec(iters);
until iters <= 0;
while len >= 40 do
begin
CHUNK(29);
e := e xor Rotate(a,20);
h := h + Rotate(b,30);
g := g xor Rotate(c,40);
f := f + Rotate(d,34);
PERMUTE3(c,h,g);
len := len - 40;
end;
If len > 0 then
begin
PTR_ADVANCEVAR(s_temp,len - 40);
CHUNK(33);
e := e xor Rotate(a,43);
h := h + Rotate(b,42);
g := g xor Rotate(c,41);
f := f + Rotate(d,40);
end;
result[0] := result[0] xor h;
result[1] := result[1] xor g;
g := g + h;
a := HashLen16(a,g + z);
x := x + (y shl 32);
b := b + x;
c := HashLen16(c,z) + h;
d := HashLen16(d,e + result[0]);
g := g + e;
h := h + HashLen16(x,f);
e := HashLen16(a,d) + g;
z := HashLen16(b,c) + a;
y := HashLen16(g,h) + c;
result[0] := e + z + y + x;
a := ShiftMix((a + y) * k0) * k0 + b;
result[1] := result[1] + a + result[0];
a := ShiftMix(a * k0) * k0 + c;
result[2] := a + result[1];
a := ShiftMix((a + e) * k0) * k0;
result[3] := a + result[2];
end;
//------------------------------------------------------------------------------
// Requires len < 240.
procedure CityHashCrc256Short(s: Pointer; len: TMemSize; out result: UInt256);
var
buf: array[0..239] of Byte;
begin
Move(s^,Addr(buf)^,len);
FillChar(buf[len],240 - len,0);
CityHashCrc256Long(@buf,240,not UInt32(len),result)
end;
{-------------------------------------------------------------------------------
CRC hash functions - public functions
-------------------------------------------------------------------------------}
procedure CityHashCrc256(s: Pointer; len: TMemSize; out result: UInt256);
begin
If len >= 240 then
CityHashCrc256Long(s,len,0,result)
else
CityHashCrc256Short(s,len,result);
end;
//------------------------------------------------------------------------------
Function CityHashCrc128WithSeed(s: Pointer; len: TMemSize; seed: UInt128): UInt128;
var
res: UInt256;
u,v: UInt64;
begin
If len <= 900 then
Result := CityHash128WithSeed(s,len,seed)
else
begin
CityHashCrc256(s,len,res);
u := UInt128High64(seed) + res[0];
v := UInt128Low64(seed) + res[1];
Result := UInt128Make(HashLen16(u,v + res[2]),HashLen16(Rotate(v,32),u * k0 + res[3]));
end;
end;
//------------------------------------------------------------------------------
Function CityHashCrc128(s: Pointer; len: TMemSize): UInt128;
var
res: UInt256;
begin
If len <= 900 then
Result := CityHash128(s,len)
else
begin
CityHashCrc256(s,len,res);
Result := UInt128Make(res[2],res[3]);
end;
end;
end.