-
Notifications
You must be signed in to change notification settings - Fork 15
/
cityhash.go
595 lines (511 loc) · 13.9 KB
/
cityhash.go
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
/*
* Go implementation of Google city hash (MIT license)
* https://code.google.com/p/cityhash/
*
* MIT License http://www.opensource.org/licenses/mit-license.php
*
* I don't even want to pretend to understand the details of city hash.
* I am only reproducing the logic in Go as faithfully as I can.
*
*/
package cityhash
import (
"encoding/binary"
"unsafe"
)
/*
var (
little bool
)
func init() {
if IsLittleEndian() {
little = true
} else {
little = false
}
}
*/
func IsLittleEndian() bool {
var i int32 = 0x01020304
u := unsafe.Pointer(&i)
pb := (*byte)(u)
b := *pb
return (b == 0x04)
}
func unalignedLoad64(p []byte) (result uint64) {
return binary.LittleEndian.Uint64(p)
/*
if little {
result = binary.LittleEndian.Uint64(p)
} else {
result = binary.BigEndian.Uint64(p)
}
return result
*/
}
func unalignedLoad32(p []byte) (result uint32) {
return binary.LittleEndian.Uint32(p)
/*
if little {
result = binary.LittleEndian.Uint32(p)
} else {
result = binary.BigEndian.Uint32(p)
}
return result
*/
}
func bswap64(x uint64) uint64 {
// Copied from netbsd's bswap64.c
return ((x << 56) & 0xff00000000000000) |
((x << 40) & 0x00ff000000000000) |
((x << 24) & 0x0000ff0000000000) |
((x << 8) & 0x000000ff00000000) |
((x >> 8) & 0x00000000ff000000) |
((x >> 24) & 0x0000000000ff0000) |
((x >> 40) & 0x000000000000ff00) |
((x >> 56) & 0x00000000000000ff)
}
func bswap32(x uint32) uint32 {
// Copied from netbsd's bswap32.c
return ((x << 24) & 0xff000000) |
((x << 8) & 0x00ff0000) |
((x >> 8) & 0x0000ff00) |
((x >> 24) & 0x000000ff)
}
func uint32InExpectedOrder(x uint32) uint32 {
/*
if !little {
return bswap32(x)
}
*/
return x
}
func uint64InExpectedOrder(x uint64) uint64 {
/*
if !little {
return bswap64(x)
}
*/
return x
}
// If I understand the original code correctly, it's expecting to load either 8 or 4
// byes in little endian order. so let's just simplify it a bit since we will do that
// anyway..
// https://code.google.com/p/cityhash/source/browse/trunk/src/city.cc#112
func fetch64(p []byte) uint64 {
return binary.LittleEndian.Uint64(p)
//return uint64InExpectedOrder(unalignedLoad64(p))
}
func fetch32(p []byte) uint32 {
return binary.LittleEndian.Uint32(p)
//return uint32InExpectedOrder(unalignedLoad32(p))
}
const (
k0 uint64 = 0xc3a5c85c97cb3127
k1 uint64 = 0xb492b66fbe98f273
k2 uint64 = 0x9ae16a3b2f90404f
c1 uint32 = 0xcc9e2d51
c2 uint32 = 0x1b873593
)
func fmix(h uint32) uint32 {
h ^= h >> 16
h *= 0x85ebca6b
h ^= h >> 13
h *= 0xc2b2ae35
h ^= h >> 16
return h
}
func rotate64(val uint64, shift uint32) uint64 {
// Avoid shifting by 64: doing so yields an undefined result.
if shift != 0 {
return ((val >> shift) | (val << (64 - shift)))
}
return val
}
func rotate32(val uint32, shift uint32) uint32 {
// Avoid shifting by 32: doing so yields an undefined result.
if shift != 0 {
return ((val >> shift) | (val << (32 - shift)))
}
return val
}
func swap64(a, b *uint64) {
*a, *b = *b, *a
}
func swap32(a, b *uint32) {
*a, *b = *b, *a
}
func permute3(a, b, c *uint32) {
swap32(a, b)
swap32(a, c)
}
func mur(a, h uint32) uint32 {
a *= c1
a = rotate32(a, 17)
a *= c2
h ^= a
h = rotate32(h, 19)
//return h * 5 + 0xe6546b64
z := h*5 + 0xe6546b64
return z
}
func hash32Len13to24(s []byte, length uint32) uint32 {
var a uint32 = fetch32(s[(length>>1)-4:])
var b uint32 = fetch32(s[4:])
var c uint32 = fetch32(s[length-8:])
var d uint32 = fetch32(s[(length >> 1):])
var e uint32 = fetch32(s)
var f uint32 = fetch32(s[length-4:])
var h uint32 = length
return fmix(mur(f, mur(e, mur(d, mur(c, mur(b, mur(a, h)))))))
}
func hash32Len0to4(s []byte, length uint32) uint32 {
var b, c uint32 = 0, 9
tmp := s[:length]
for _, v := range tmp {
b = uint32(int64(b)*int64(c1) + int64(int8(v)))
c ^= b
}
return fmix(mur(b, mur(length, c)))
}
func hash32Len5to12(s []byte, length uint32) uint32 {
var a, b, c uint32 = length, length * 5, 9
var d uint32 = b
a += fetch32(s)
b += fetch32(s[length-4:])
c += fetch32(s[((length >> 1) & 4):])
return fmix(mur(c, mur(b, mur(a, d))))
}
func CityHash32(s []byte, length uint32) uint32 {
if length <= 4 {
return hash32Len0to4(s, length)
} else if length <= 12 {
return hash32Len5to12(s, length)
} else if length <= 24 {
return hash32Len13to24(s, length)
}
// length > 24
var h uint32 = length
var g uint32 = c1 * length
var f uint32 = g
var a0 uint32 = rotate32(fetch32(s[length-4:])*c1, 17) * c2
var a1 uint32 = rotate32(fetch32(s[length-8:])*c1, 17) * c2
var a2 uint32 = rotate32(fetch32(s[length-16:])*c1, 17) * c2
var a3 uint32 = rotate32(fetch32(s[length-12:])*c1, 17) * c2
var a4 uint32 = rotate32(fetch32(s[length-20:])*c1, 17) * c2
h ^= a0
h = rotate32(h, 19)
h = h*5 + 0xe6546b64
h ^= a2
h = rotate32(h, 19)
h = h*5 + 0xe6546b64
g ^= a1
g = rotate32(g, 19)
g = g*5 + 0xe6546b64
g ^= a3
g = rotate32(g, 19)
g = g*5 + 0xe6546b64
f += a4
f = rotate32(f, 19)
f = f*5 + 0xe6546b64
var iters uint32 = (length - 1) / 20
for {
var a0 uint32 = rotate32(fetch32(s)*c1, 17) * c2
var a1 uint32 = fetch32(s[4:])
var a2 uint32 = rotate32(fetch32(s[8:])*c1, 17) * c2
var a3 uint32 = rotate32(fetch32(s[12:])*c1, 17) * c2
var a4 uint32 = fetch32(s[16:])
h ^= a0
h = rotate32(h, 18)
h = h*5 + 0xe6546b64
f += a1
f = rotate32(f, 19)
f = f * c1
g += a2
g = rotate32(g, 18)
g = g*5 + 0xe6546b64
h ^= a3 + a1
h = rotate32(h, 19)
h = h*5 + 0xe6546b64
g ^= a4
g = bswap32(g) * 5
h += a4 * 5
h = bswap32(h)
f += a0
permute3(&f, &h, &g)
s = s[20:]
iters--
if iters == 0 {
break
}
}
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 + 0xe6546b64
h = rotate32(h, 17) * c1
h = rotate32(h+f, 19)
h = h*5 + 0xe6546b64
h = rotate32(h, 17) * c1
return h
}
func shiftMix(val uint64) uint64 {
return val ^ (val >> 47)
}
type Uint128 [2]uint64
func (this *Uint128) setLower64(l uint64) {
this[0] = l
}
func (this *Uint128) setHigher64(h uint64) {
this[1] = h
}
func (this Uint128) Lower64() uint64 {
return this[0]
}
func (this Uint128) Higher64() uint64 {
return this[1]
}
func (this Uint128) Bytes() []byte {
b := make([]byte, 16)
binary.LittleEndian.PutUint64(b, this[0])
binary.LittleEndian.PutUint64(b[8:], this[1])
return b
}
func hash128to64(x Uint128) uint64 {
// Murmur-inspired hashing.
const kMul uint64 = 0x9ddfea08eb382d69
var a uint64 = (x.Lower64() ^ x.Higher64()) * kMul
a ^= (a >> 47)
var b uint64 = (x.Higher64() ^ a) * kMul
b ^= (b >> 47)
b *= kMul
return b
}
func hashLen16(u, v uint64) uint64 {
return hash128to64(Uint128{u, v})
}
func hashLen16_3(u, v, mul uint64) uint64 {
// Murmur-inspired hashing.
var a uint64 = (u ^ v) * mul
a ^= (a >> 47)
var b uint64 = (v ^ a) * mul
b ^= (b >> 47)
b *= mul
return b
}
func hashLen0to16(s []byte, length uint32) uint64 {
if length >= 8 {
var mul uint64 = k2 + uint64(length)*2
var a uint64 = fetch64(s) + k2
var b uint64 = fetch64(s[length-8:])
var c uint64 = rotate64(b, 37)*mul + a
var d uint64 = (rotate64(a, 25) + b) * mul
return hashLen16_3(c, d, mul)
}
if length >= 4 {
var mul uint64 = k2 + uint64(length)*2
var a uint64 = uint64(fetch32(s))
return hashLen16_3(uint64(length)+(a<<3), uint64(fetch32(s[length-4:])), mul)
}
if length > 0 {
var a uint8 = uint8(s[0])
var b uint8 = uint8(s[length>>1])
var c uint8 = uint8(s[length-1])
var y uint32 = uint32(a) + (uint32(b) << 8)
var z uint32 = length + (uint32(c) << 2)
return shiftMix(uint64(y)*k2^uint64(z)*k0) * k2
}
return k2
}
func hashLen17to32(s []byte, length uint32) uint64 {
var mul uint64 = k2 + uint64(length)*2
var a uint64 = fetch64(s) * k1
var b uint64 = fetch64(s[8:])
var c uint64 = fetch64(s[length-8:]) * mul
var d uint64 = fetch64(s[length-16:]) * k2
return hashLen16_3(rotate64(a+b, 43)+rotate64(c, 30)+d, a+rotate64(b+k2, 18)+c, mul)
}
func weakHashLen32WithSeeds(w, x, y, z, a, b uint64) Uint128 {
a += w
b = rotate64(b+a+z, 21)
var c uint64 = a
a += x
a += y
b += rotate64(a, 44)
return Uint128{a + z, b + c}
}
func weakHashLen32WithSeeds_3(s []byte, a, b uint64) Uint128 {
return weakHashLen32WithSeeds(fetch64(s), fetch64(s[8:]), fetch64(s[16:]), fetch64(s[24:]), a, b)
}
func hashLen33to64(s []byte, length uint32) uint64 {
var mul uint64 = k2 + uint64(length)*2
var a uint64 = fetch64(s) * k2
var b uint64 = fetch64(s[8:])
var c uint64 = fetch64(s[length-24:])
var d uint64 = fetch64(s[length-32:])
var e uint64 = fetch64(s[16:]) * k2
var f uint64 = fetch64(s[24:]) * 9
var g uint64 = fetch64(s[length-8:])
var h uint64 = fetch64(s[length-16:]) * mul
var u uint64 = rotate64(a+g, 43) + (rotate64(b, 30)+c)*9
var v uint64 = ((a + g) ^ d) + f + 1
var w uint64 = bswap64((u+v)*mul) + h
var x uint64 = rotate64(e+f, 42) + c
var y uint64 = (bswap64((v+w)*mul) + g) * mul
var z uint64 = e + f + c
a = bswap64((x+z)*mul+y) + b
b = shiftMix((z+a)*mul+d+h) * mul
return b + x
}
func CityHash64(s []byte, length uint32) uint64 {
if length <= 32 {
if length <= 16 {
return hashLen0to16(s, length)
} else {
return hashLen17to32(s, length)
}
} else if length <= 64 {
return hashLen33to64(s, length)
}
var x uint64 = fetch64(s[length-40:])
var y uint64 = fetch64(s[length-16:]) + fetch64(s[length-56:])
var z uint64 = hashLen16(fetch64(s[length-48:])+uint64(length), fetch64(s[length-24:]))
var v Uint128 = weakHashLen32WithSeeds_3(s[length-64:], uint64(length), z)
var w Uint128 = weakHashLen32WithSeeds_3(s[length-32:], y+k1, x)
x = x*k1 + fetch64(s)
length = (length - 1) & ^uint32(63)
for {
x = rotate64(x+y+v.Lower64()+fetch64(s[8:]), 37) * k1
y = rotate64(y+v.Higher64()+fetch64(s[48:]), 42) * k1
x ^= w.Higher64()
y += v.Lower64() + fetch64(s[40:])
z = rotate64(z+w.Lower64(), 33) * k1
v = weakHashLen32WithSeeds_3(s, v.Higher64()*k1, x+w.Lower64())
w = weakHashLen32WithSeeds_3(s[32:], z+w.Higher64(), y+fetch64(s[16:]))
swap64(&z, &x)
s = s[64:]
length -= 64
if length == 0 {
break
}
}
return hashLen16(hashLen16(v.Lower64(), w.Lower64())+shiftMix(y)*k1+z, hashLen16(v.Higher64(), w.Higher64())+x)
}
func CityHash64WithSeed(s []byte, length uint32, seed uint64) uint64 {
return CityHash64WithSeeds(s, length, k2, seed)
}
func CityHash64WithSeeds(s []byte, length uint32, seed0, seed1 uint64) uint64 {
return hashLen16(CityHash64(s, length)-seed0, seed1)
}
func cityMurmur(s []byte, length uint32, seed Uint128) Uint128 {
var a uint64 = seed.Lower64()
var b uint64 = seed.Higher64()
var c uint64 = 0
var d uint64 = 0
var l int32 = int32(length) - 16
if l <= 0 { // len <= 16
a = shiftMix(a*k1) * k1
c = b*k1 + hashLen0to16(s, length)
if length >= 8 {
d = shiftMix(a + fetch64(s))
} else {
d = shiftMix(a + c)
}
} else { // len > 16
c = hashLen16(fetch64(s[length-8:])+k1, a)
d = hashLen16(b+uint64(length), c+fetch64(s[length-16:]))
a += d
for {
a ^= shiftMix(fetch64(s)*k1) * k1
a *= k1
b ^= a
c ^= shiftMix(fetch64(s[8:])*k1) * k1
c *= k1
d ^= c
s = s[16:]
l -= 16
if l <= 0 {
break
}
}
}
a = hashLen16(a, c)
b = hashLen16(d, b)
return Uint128{a ^ b, hashLen16(b, a)}
}
func CityHash128WithSeed(s []byte, length uint32, seed Uint128) Uint128 {
if length < 128 {
return cityMurmur(s, length, seed)
}
orig_length := length
var t []byte = s
// We expect length >= 128 to be the common case. Keep 56 bytes of state:
// v, w, x, y, and z.
var v, w Uint128
var x uint64 = seed.Lower64()
var y uint64 = seed.Higher64()
var z uint64 = uint64(length) * k1
v.setLower64(rotate64(y^k1, 49)*k1 + fetch64(s))
v.setHigher64(rotate64(v.Lower64(), 42)*k1 + fetch64(s[8:]))
w.setLower64(rotate64(y+z, 35)*k1 + x)
w.setHigher64(rotate64(x+fetch64(s[88:]), 53) * k1)
// This is the same inner loop as CityHash64(), manually unrolled.
for {
x = rotate64(x+y+v.Lower64()+fetch64(s[8:]), 37) * k1
y = rotate64(y+v.Higher64()+fetch64(s[48:]), 42) * k1
x ^= w.Higher64()
y += v.Lower64() + fetch64(s[40:])
z = rotate64(z+w.Lower64(), 33) * k1
v = weakHashLen32WithSeeds_3(s, v.Higher64()*k1, x+w.Lower64())
w = weakHashLen32WithSeeds_3(s[32:], z+w.Higher64(), y+fetch64(s[16:]))
swap64(&z, &x)
s = s[64:]
x = rotate64(x+y+v.Lower64()+fetch64(s[8:]), 37) * k1
y = rotate64(y+v.Higher64()+fetch64(s[48:]), 42) * k1
x ^= w.Higher64()
y += v.Lower64() + fetch64(s[40:])
z = rotate64(z+w.Lower64(), 33) * k1
v = weakHashLen32WithSeeds_3(s, v.Higher64()*k1, x+w.Lower64())
w = weakHashLen32WithSeeds_3(s[32:], z+w.Higher64(), y+fetch64(s[16:]))
swap64(&z, &x)
s = s[64:]
length -= 128
if length < 128 {
break
}
}
x += rotate64(v.Lower64()+z, 49) * k0
y = y*k0 + rotate64(w.Higher64(), 37)
z = z*k0 + rotate64(w.Lower64(), 27)
w.setLower64(w.Lower64() * 9)
v.setLower64(v.Lower64() * k0)
// If 0 < length < 128, hash up to 4 chunks of 32 bytes each from the end of s.
var tail_done uint32
for tail_done = 0; tail_done < length; {
tail_done += 32
y = rotate64(x+y, 42)*k0 + v.Higher64()
w.setLower64(w.Lower64() + fetch64(t[orig_length-tail_done+16:]))
x = x*k0 + w.Lower64()
z += w.Higher64() + fetch64(t[orig_length-tail_done:])
w.setHigher64(w.Higher64() + v.Lower64())
v = weakHashLen32WithSeeds_3(t[orig_length-tail_done:], v.Lower64()+z, v.Higher64())
v.setLower64(v.Lower64() * k0)
}
// 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.Lower64())
y = hashLen16(y+z, w.Lower64())
return Uint128{hashLen16(x+v.Higher64(), w.Higher64()) + y,
hashLen16(x+w.Higher64(), y+v.Higher64())}
}
func CityHash128(s []byte, length uint32) (result Uint128) {
if length >= 16 {
result = CityHash128WithSeed(s[16:length], length-16, Uint128{fetch64(s), fetch64(s[8:length]) + k0})
} else {
result = CityHash128WithSeed(s, length, Uint128{k0, k1})
}
return
}