-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CITY_1_0_3.pas
556 lines (486 loc) · 17.4 KB
/
CITY_1_0_3.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
{-------------------------------------------------------------------------------
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.0.3
Implementation of CITY hash version 1.0.3.
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_0_3;
{$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.
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 HashLen0to16(s: Pointer; len: TMemSize): UInt64;
var
a,b,c: 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 := UInt32(len) + (UInt32(c) shl 2);
Result := ShiftMix((UInt64(y) * k2) xor (UInt64(z) * k3)) * k2;
end;
4..8:
begin
a := Fetch32(s);
Result := HashLen16(len + UInt64(a shl 3),Fetch32(s,len - 4));
end;
9..High(len):
begin
a := Fetch64(s);
b := Fetch64(s,len - 8);
Result := HashLen16(a,RotateByAtLeast1(b + len,len)) xor b;
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;
begin
a := Fetch64(s) * k1;
b := Fetch64(s,8);
c := Fetch64(s,len - 8) * k2;
d := Fetch64(s,len - 16) * k0;
Result := HashLen16(Rotate(a - b,43) + Rotate(c,30) + d,a + Rotate(b xor k3,20) - c + len);
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
a,b,c,z,vf,vs,wf,ws,r: UInt64;
begin
z := Fetch64(s,24);
a := Fetch64(s) + (len + Fetch64(s,len - 16)) * k0;
b := Rotate(a + z,52);
c := Rotate(a,37);
a := a + Fetch64(s,8);
c := c + Rotate(a,7);
a := a + Fetch64(s,16);
vf := a + z;
vs := b + Rotate(a,31) + c;
a := Fetch64(s,16) + Fetch64(s,len - 32);
z := Fetch64(s, len - 8);
b := Rotate(a + z,52);
c := Rotate(a,37);
a := a + Fetch64(s,len - 24);
c := c + Rotate(a,7);
a := a + Fetch64(s,len - 16);
wf := a + z;
ws := b + Rotate(a,31) + c;
r := ShiftMix((vf + ws) * k2 + (wf + vs) * k0);
Result := ShiftMix(r * k0 + vs) * k2;
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 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;
z := z + Rotate(w.First,37) * 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);
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) xor k3,Fetch64(s,8)))
else If len >= 8 then
Result := CityHash128WithSeed(nil,0,UInt128Make(Fetch64(s) xor (len * k0),Fetch64(s,len - 8) xor k1))
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,i,j,t: UInt64;
iters: TMemSize;
s_temp: Pointer;
procedure CHUNK(multiplier,z: UInt64);
var
old_a: Uint64;
begin
old_a := a;
a := Rotate(b,41 xor z) * multiplier + Fetch64(s_temp);
b := Rotate(c,27 xor z) * multiplier + Fetch64(s_temp,8);
c := Rotate(d,41 xor z) * multiplier + Fetch64(s_temp,16);
d := Rotate(e,33 xor z) * multiplier + Fetch64(s_temp,24);
e := Rotate(t,25 xor z) * multiplier + Fetch64(s_temp,32);
t := old_a;
f := _mm_crc32_u64(f,a);
g := _mm_crc32_u64(g,b);
h := _mm_crc32_u64(h,c);
i := _mm_crc32_u64(i,d);
j := _mm_crc32_u64(j,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 := seed;
g := 0;
h := 0;
i := 0;
j := 0;
t := c + d;
// 240 bytes of input per iter.
iters := len div 240;
len := TMemSize(UInt64(len) - (UInt64(iters) * 240));
repeat
CHUNK(1,1); CHUNK(k0,0);
CHUNK(1,1); CHUNK(k0,0);
CHUNK(1,1); CHUNK(k0,0);
Dec(iters);
until iters <= 0;
while len >= 40 do
begin
CHUNK(k0,0);
len := len - 40;
end;
If len > 0 then
begin
PTR_ADVANCEVAR(s_temp,len - 40);
CHUNK(k0,0);
end;
j := j + (i shl 32);
a := HashLen16(a,j);
h := h + (g shl 32);
b := b + h;
c := HashLen16(c,f) + i;
d := HashLen16(d,e + result[0]);
j := j + e;
i := i + HashLen16(h,t);
e := HashLen16(a,d) + j;
f := HashLen16(b,c) + a;
g := HashLen16(j,i) + c;
result[0] := e + f + g + h;
a := ShiftMix((a + g) * 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.