-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjenkins.go
1138 lines (1030 loc) · 25.1 KB
/
jenkins.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
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
// Copyright © 2014 Lawrence E. Bakst. All rights reserved.
// This package contains various transliteration of Jenkins hash funcions.
// This includes lookup3.c and some other Jenkins functions that appear to be based on lookup2.c.
// See http://burtleburtle.net/bob/c/lookup3.c and http://burtleburtle.net/bob/hash/evahash.html
package jenkins
import (
"fmt"
"hash"
"leb.io/hashland/nhash"
"unsafe"
)
// Make sure interfaces are correctly implemented. Stolen from another implementation.
// I did something similar in another package to verify the interface but didn't know you could elide the variable in a var.
// What a cute wart it is.
var (
//_ hash.Hash = new(Digest)
_ hash.Hash32 = new(State332c)
_ nhash.HashF32 = new(State332c)
)
/*
uint32_t jenkins_one_at_a_time_hash(char *key, size_t len)
{
uint32_t hash, i;
for(hash = i = 0; i < len; ++i)
{
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
*/
// This function mixes the state
func mix32(a, b, c uint32) (uint32, uint32, uint32) {
a = a - b
a = a - c
a = a ^ (c >> 13)
b = b - c
b = b - a
b = b ^ (a << 8)
c = c - a
c = c - b
c = c ^ (b >> 13)
a = a - b
a = a - c
a = a ^ (c >> 12)
b = b - c
b = b - a
b = b ^ (a << 16)
c = c - a
c = c - b
c = c ^ (b >> 5)
a = a - b
a = a - c
a = a ^ (c >> 3)
b = b - c
b = b - a
b = b ^ (a << 10)
c = c - a
c = c - b
c = c ^ (b >> 15)
return a, b, c
}
func mix32a(a, b, c uint32) (uint32, uint32, uint32) {
a = a - b - c ^ (c >> 13)
b = b - c - a ^ (a << 8)
c = c - a - b ^ (b >> 13)
a = a - b - c ^ (c >> 12)
b = b - c - a ^ (a << 16)
c = c - a - b ^ (b >> 5)
a = a - b - c ^ (c >> 3)
b = b - c - a ^ (a << 10)
c = c - a - b ^ (b >> 15)
return a, b, c
}
func Check() {
var a, b, c uint32
var init = func() {
a, b, c = 0x12345678, 0x87654321, 0xdeadbeef
}
init()
a = a - b
a = a - c
fmt.Printf("a=0x%08x\n", a)
a = a ^ (c >> 13)
fmt.Printf("a=0x%08x\n", a)
fmt.Printf("a=0x%08x, b=0x%08x, c=0x%08x\n", a, b, c)
init()
a = ((a - b) - c)
fmt.Printf("a=0x%08x\n", a)
a = a ^ (c >> 13)
fmt.Printf("a=0x%08x\n", a)
fmt.Printf("a=0x%08x, b=0x%08x, c=0x%08x\n", a, b, c)
}
// This makes a new slice of uint64 that points to the same slice passed in as []byte.
// We should check alignment for architectures that don't handle unaligned reads.
// Fallback to a copy or maybe use encoding/binary?
// Not sure what the right thing to do is for little vs big endian?
// What are the right test vevtors for big-endian machines.
func sliceUI32(in []byte) []uint32 {
return (*(*[]uint32)(unsafe.Pointer(&in)))[:len(in)/4]
}
// Jenkin's second generation 32 bit hash.
// Benchmarked with 4 byte key, inlining, and no store of hash at:
// benchmark32: 55 Mhashes/sec
// benchmark32: 219 MB/sec
func Hash232(k []byte, seed uint32) uint32 {
var fast = true // fast is really much faster
l := uint32(len(k))
a := uint32(0x9e3779b9) // the golden ratio; an arbitrary value
b := a
c := seed // variable initialization of internal state
if fast {
k32 := sliceUI32(k)
cnt := 0
for ; l >= 12; l -= 12 {
a += k32[0+cnt]
b += k32[1+cnt]
c += k32[2+cnt]
a, b, c = mix32(a, b, c)
k = k[12:]
cnt += 3
}
} else {
for ; l >= 12; l -= 12 {
a += uint32(k[0]) + uint32(k[1])<<8 + uint32(k[2])<<16 + uint32(k[3])<<24
b += uint32(k[4]) + uint32(k[5])<<8 + uint32(k[6])<<16 + uint32(k[7])<<24
c += uint32(k[8]) + uint32(k[9])<<8 + uint32(k[10])<<16 + uint32(k[11])<<24
a, b, c = mix32(a, b, c)
k = k[12:]
}
}
c += l
switch l {
case 11:
c += uint32(k[10]) << 24
fallthrough
case 10:
c += uint32(k[9]) << 16
fallthrough
case 9:
c += uint32(k[8]) << 8
fallthrough
case 8:
b += uint32(k[7]) << 24 // the first byte of c is reserved for the length
fallthrough
case 7:
b += uint32(k[6]) << 16
fallthrough
case 6:
b += uint32(k[5]) << 8
fallthrough
case 5:
b += uint32(k[4])
fallthrough
case 4:
a += uint32(k[3]) << 24
fallthrough
case 3:
a += uint32(k[2]) << 16
fallthrough
case 2:
a += uint32(k[1]) << 8
fallthrough
case 1:
c += uint32(k[0])
fallthrough
case 0:
break
default:
panic("HashWords32")
}
a, b, c = mix32(a, b, c)
return c
}
// original mix function
func mix64(a, b, c uint64) (uint64, uint64, uint64) {
a = a - b
a = a - c
a = a ^ (c >> 43)
b = b - c
b = b - a
b = b ^ (a << 9)
c = c - a
c = c - b
c = c ^ (b >> 8)
a = a - b
a = a - c
a = a ^ (c >> 38)
b = b - c
b = b - a
b = b ^ (a << 23)
c = c - a
c = c - b
c = c ^ (b >> 5)
a = a - b
a = a - c
a = a ^ (c >> 35)
b = b - c
b = b - a
b = b ^ (a << 49)
c = c - a
c = c - b
c = c ^ (b >> 11)
a = a - b
a = a - c
a = a ^ (c >> 12)
b = b - c
b = b - a
b = b ^ (a << 18)
c = c - a
c = c - b
c = c ^ (b >> 22)
return a, b, c
}
// restated mix function better for gofmt
func mix64alt(a, b, c uint64) (uint64, uint64, uint64) {
a -= b - c ^ (c >> 43)
b -= c - a ^ (a << 9)
c -= a - b ^ (b >> 8)
a -= b - c ^ (c >> 38)
b -= c - a ^ (a << 23)
c -= a - b ^ (b >> 5)
a -= b - c ^ (c >> 35)
b -= c - a ^ (a << 49)
c -= a - b ^ (b >> 11)
a -= b - c ^ (c >> 12)
b -= c - a ^ (a << 18)
c -= a - b ^ (b >> 22)
return a, b, c
}
// the following functions can be inlined
func mix64a(a, b, c uint64) (uint64, uint64, uint64) {
a -= b - c ^ (c >> 43)
b -= c - a ^ (a << 9)
return a, b, c
}
func mix64b(a, b, c uint64) (uint64, uint64, uint64) {
c -= a - b ^ (b >> 8)
a -= b - c ^ (c >> 38)
return a, b, c
}
func mix64c(a, b, c uint64) (uint64, uint64, uint64) {
b -= c - a ^ (a << 23)
c -= a - b ^ (b >> 5)
return a, b, c
}
func mix64d(a, b, c uint64) (uint64, uint64, uint64) {
a -= b - c ^ (c >> 35)
b -= c - a ^ (a << 49)
return a, b, c
}
func mix64e(a, b, c uint64) (uint64, uint64, uint64) {
c -= a - b ^ (b >> 11)
a -= b - c ^ (c >> 12)
return a, b, c
}
func mix64f(a, b, c uint64) (uint64, uint64, uint64) {
b -= c - a ^ (a << 18)
c -= a - b ^ (b >> 22)
return a, b, c
}
// This makes a new slice of uint64 that points to the same slice passed in as []byte.
// We should check alignment for architectures that don't handle unaligned reads.
// Fallback to a copy or maybe use encoding/binary?
// Not sure what the right thing to do is for little vs big endian?
// What are the right test vevtors for big-endian machines.
func sliceUI64(in []byte) []uint64 {
return (*(*[]uint64)(unsafe.Pointer(&in)))[:len(in)/8]
}
// Jenkin's second generation 64 bit hash.
// Benchmarked with 24 byte key, inlining, store of hash in memory (cache miss every 4 hashes) and fast=true at:
// benchmark64: 26 Mhashes/sec
// benchmark64: 623 MB/sec
func Hash264(k []byte, seed uint64) uint64 {
var fast = true // fast is really much faster
//fmt.Printf("k=%v\n", k)
//fmt.Printf("length=%d, len(k)=%d\n", length, len(k))
//The 64-bit golden ratio is 0x9e3779b97f4a7c13LL
length := uint64(len(k))
a := uint64(0x9e3779b97f4a7c13)
b := a
c := seed
if fast {
k64 := sliceUI64(k)
cnt := 0
for i := length; i >= 24; i -= 24 {
a += k64[0+cnt]
b += k64[1+cnt]
c += k64[2+cnt]
// inlining is slightly faster
a, b, c = mix64a(a, b, c)
a, b, c = mix64b(a, b, c)
a, b, c = mix64c(a, b, c)
a, b, c = mix64d(a, b, c)
a, b, c = mix64e(a, b, c)
a, b, c = mix64f(a, b, c)
k = k[24:]
cnt += 3
length -= 24
}
} else {
for i := length; i >= 24; i -= 24 {
a += uint64(k[0]) | uint64(k[1])<<8 | uint64(k[2])<<16 | uint64(k[3])<<24 | uint64(k[4])<<32 | uint64(k[5])<<40 | uint64(k[6])<<48 | uint64(k[7])<<56
b += uint64(k[8]) | uint64(k[9])<<8 | uint64(k[10])<<16 | uint64(k[11])<<24 | uint64(k[12])<<32 | uint64(k[13])<<40 | uint64(k[14])<<48 | uint64(k[15])<<56
c += uint64(k[16]) | uint64(k[17])<<8 | uint64(k[18])<<16 | uint64(k[19])<<24 | uint64(k[20])<<32 | uint64(k[21])<<40 | uint64(k[22])<<48 | uint64(k[23])<<56
a, b, c = mix64alt(a, b, c)
k = k[24:]
length -= 24
}
}
c += length
if len(k) > 23 {
panic("Hash264")
}
switch length {
case 23:
c += uint64(k[22]) << 56
fallthrough
case 22:
c += uint64(k[21]) << 48
fallthrough
case 21:
c += uint64(k[20]) << 40
fallthrough
case 20:
c += uint64(k[19]) << 32
fallthrough
case 19:
c += uint64(k[18]) << 24
fallthrough
case 18:
c += uint64(k[17]) << 16
fallthrough
case 17:
c += uint64(k[16]) << 8
fallthrough
case 16:
b += uint64(k[15]) << 56 // the first byte of c is reserved for the length
fallthrough
case 15:
b += uint64(k[14]) << 48
fallthrough
case 14:
b += uint64(k[13]) << 40
fallthrough
case 13:
b += uint64(k[12]) << 32
fallthrough
case 12:
b += uint64(k[11]) << 24
fallthrough
case 11:
b += uint64(k[10]) << 16
fallthrough
case 10:
b += uint64(k[9]) << 8
fallthrough
case 9:
b += uint64(k[8])
fallthrough
case 8:
a += uint64(k[7]) << 56
fallthrough
case 7:
a += uint64(k[6]) << 48
fallthrough
case 6:
a += uint64(k[5]) << 40
fallthrough
case 5:
a += uint64(k[4]) << 32
fallthrough
case 4:
a += uint64(k[3]) << 24
fallthrough
case 3:
a += uint64(k[2]) << 16
fallthrough
case 2:
a += uint64(k[1]) << 8
fallthrough
case 1:
a += uint64(k[0])
case 0:
break
default:
panic("HashWords64")
}
a, b, c = mix64alt(a, b, c)
return c
}
/*
func omix(a, b, c uint32) (uint32, uint32, uint32) {
a -= c; a ^= rot(c, 4); c += b;
b -= a; b ^= rot(a, 6); a += c;
c -= b; c ^= rot(b, 8); b += a;
a -= c; a ^= rot(c,16); c += b;
b -= a; b ^= rot(a,19); a += c;
c -= b; c ^= rot(b, 4); b += a;
return a, b, c
}
func ofinal(a, b, c uint32) (uint32, uint32, uint32) {
c ^= b; c -= rot(b,14);
a ^= c; a -= rot(c,11);
b ^= a; b -= rot(a,25);
c ^= b; c -= rot(b,16);
a ^= c; a -= rot(c,4);
b ^= a; b -= rot(a,14);
c ^= b; c -= rot(b,24);
return a, b, c
}
func mix(a, b, c uint32) (uint32, uint32, uint32) {
a -= c; a ^= c << 4 | c >> (32 - 4); c += b;
b -= a; b ^= a << 6 | a >> (32 - 6); a += c;
c -= b; c ^= b << 8 | b >> (32 - 8); b += a;
a -= c; a ^= c << 16 | c >> (32 - 16); c += b;
b -= a; b ^= a << 19 | a >> (32 - 19); a += c;
c -= b; c ^= b << 4 | b >> (32 - 4); b += a;
return a, b, c
}
func final(a, b, c uint32) (uint32, uint32, uint32) {
c ^= b; c -= b << 14 | b >> (32 - 14);
a ^= c; a -= c << 11 | c >> (32 - 11);
b ^= a; b -= a << 25 | a >> (32 - 25);
c ^= b; c -= b << 16 | b >> (32 - 16);
a ^= c; a -= c << 4 | c >> (32 - 4);
b ^= a; b -= a << 14 | a >> (32 - 14);
c ^= b; c -= b << 24 | b >> (32 - 24);
return a, b, c
}
*/
//var a, b, c uint32
func rot(x, k uint32) uint32 {
return x<<k | x>>(32-k)
}
// current gc compilers can't inline long functions so we have to split mix into 2
func mix1(a, b, c uint32) (uint32, uint32, uint32) {
a -= c
a ^= rot(c, 4)
c += b
b -= a
b ^= rot(a, 6)
a += c
c -= b
c ^= rot(b, 8)
b += a
//a -= c; a ^= c << 4 | c >> (32 - 4); c += b;
//b -= a; b ^= a << 6 | a >> (32 - 6); a += c;
return a, b, c
}
func mix2(a, b, c uint32) (uint32, uint32, uint32) {
a -= c
a ^= rot(c, 16)
c += b
b -= a
b ^= rot(a, 19)
a += c
c -= b
c ^= rot(b, 4)
b += a
// c -= b; c ^= b << 8 | b >> (32 - 8); b += a;
// a -= c; a ^= c << 16 | c >> (32 - 16); c += b;
return a, b, c
}
/*
func mix3(a, b, c uint32) (uint32, uint32, uint32) {
b -= a; b ^= a << 19 | a >> (32 - 19); a += c;
c -= b; c ^= b << 4 | b >> (32 - 4); b += a;
return a, b, c
}
*/
func final1(a, b, c uint32) (uint32, uint32, uint32) {
c ^= b
c -= rot(b, 14)
a ^= c
a -= rot(c, 11)
b ^= a
b -= rot(a, 25)
c ^= b
c -= rot(b, 16)
//c ^= b; c -= b << 14 | b >> (32 - 14);
//a ^= c; a -= c << 11 | c >> (32 - 11);
//b ^= a; b -= a << 25 | a >> (32 - 25);
//c ^= b; c -= b << 16 | b >> (32 - 16);
return a, b, c
}
func final2(a, b, c uint32) (uint32, uint32, uint32) {
a ^= c
a -= rot(c, 4)
b ^= a
b -= rot(a, 14)
c ^= b
c -= rot(b, 24)
//a ^= c; a -= c << 4 | c >> (32 - 4);
//b ^= a; b -= a << 14 | a >> (32 - 14);
//c ^= b; c -= b << 24 | b >> (32 - 24);
return a, b, c
}
func HashWords332(k []uint32, seed uint32) uint32 {
var a, b, c uint32
length := uint32(len(k))
a = 0xdeadbeef + length<<2 + seed
b, c = a, a
i := 0
for ; length > 3; length -= 3 {
a += k[i+0]
b += k[i+1]
c += k[i+2]
a, b, c = mix1(a, b, c)
a, b, c = mix2(a, b, c)
i += 3
}
switch length {
case 3:
c += k[i+2]
fallthrough
case 2:
b += k[i+1]
fallthrough
case 1:
a += k[i+0]
a, b, c = final1(a, b, c)
a, b, c = final2(a, b, c)
case 0:
break
}
return c
}
func HashWordsLen(k []uint32, length int, seed uint32) uint32 {
var a, b, c uint32
//fmt.Printf("k=%v\n", k)
//fmt.Printf("length=%d, len(k)=%d\n", length, len(k))
if length > len(k)*4 {
fmt.Printf("length=%d, len(k)=%d\n", length, len(k))
panic("HashWords")
}
ul := uint32(len(k))
a = 0xdeadbeef + ul<<2 + seed
b, c = a, a
i := 0
//length := 0
for ; length > 3; length -= 3 {
a += k[i+0]
b += k[i+1]
c += k[i+2]
a, b, c = mix1(a, b, c)
a, b, c = mix2(a, b, c)
//a, b, c = mix3(a, b, c)
i += 3
}
//fmt.Printf("remaining length=%d, len(k)=%d, i=%d, k[i + 2]=%d, k[i + 1]=%d, k[i + 0]=%d\n", length, len(k), i, k[i + 2], k[i + 1], k[i + 0])
switch length {
case 3:
c += k[i+2]
fallthrough
case 2:
b += k[i+1]
fallthrough
case 1:
a += k[i+0]
a, b, c = final1(a, b, c)
a, b, c = final2(a, b, c)
case 0:
break
}
//fmt.Printf("end\n")
return c
}
// This is an example of how I could like to code hash functions like this.
// Using closures over the state and expecting thme to be inlined
func XHashWords(k []uint32, length int, seed uint32) uint32 {
var a, b, c uint32
var rot = func(x, k uint32) uint32 {
return x<<k | x>>(32-k)
}
var mix = func() {
a -= c
a ^= rot(c, 4)
c += b
b -= a
b ^= rot(a, 6)
a += c
c -= b
c ^= rot(b, 8)
b += a
a -= c
a ^= rot(c, 16)
c += b
b -= a
b ^= rot(a, 19)
a += c
c -= b
c ^= rot(b, 4)
b += a
}
var final = func() {
c ^= b
c -= rot(b, 14)
a ^= c
a -= rot(c, 11)
b ^= a
b -= rot(a, 25)
c ^= b
c -= rot(b, 16)
a ^= c
a -= rot(c, 4)
b ^= a
b -= rot(a, 14)
c ^= b
c -= rot(b, 24)
}
ul := uint32(len(k))
a = 0xdeadbeef + ul<<2 + seed
b, c = a, a
i := 0
//length := 0
for length = len(k); length > 3; length -= 3 {
a += k[i+0]
b += k[i+1]
c += k[i+2]
mix()
i += 3
}
switch length {
case 3:
c += k[i+2]
fallthrough
case 2:
b += k[i+1]
fallthrough
case 1:
a += k[i+0]
final()
case 0:
break
}
return c
}
// jenkins364: return 2 32-bit hash values.
// Returns two 32-bit hash values instead of just one.
// This is good enough for hash table lookup with 2^^64 buckets,
// or if you want a second hash if you're not happy with the first,
// or if you want a probably-unique 64-bit ID for the key.
// *pc is better mixed than *pb, so use *pc first.
// If you want a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)"
func Jenkins364(k []byte, length int, pc, pb uint32) (rpc, rpb uint32) {
var a, b, c uint32
//fmt.Printf("Jenkins364: k=%v, len(k)=%d\n", k, len(k))
if length == 0 {
length = len(k)
}
/*
var rot = func(x, k uint32) uint32 {
return x << k | x >> (32 - k)
}
var mix = func() {
a -= c; a ^= rot(c, 4); c += b;
b -= a; b ^= rot(a, 6); a += c;
c -= b; c ^= rot(b, 8); b += a;
a -= c; a ^= rot(c,16); c += b;
b -= a; b ^= rot(a,19); a += c;
c -= b; c ^= rot(b, 4); b += a;
}
var final = func() {
c ^= b; c -= rot(b,14);
a ^= c; a -= rot(c,11);
b ^= a; b -= rot(a,25);
c ^= b; c -= rot(b,16);
a ^= c; a -= rot(c,4);
b ^= a; b -= rot(a,14);
c ^= b; c -= rot(b,24);
}
*/
ul := uint32(len(k))
/* Set up the internal state */
a = 0xdeadbeef + ul + pc
b, c = a, a
c += pb
for ; length > 12; length -= 12 {
//fmt.Printf("k=%q, length=%d\n", k, length)
a += *(*uint32)(unsafe.Pointer(&k[0]))
b += *(*uint32)(unsafe.Pointer(&k[4]))
c += *(*uint32)(unsafe.Pointer(&k[8]))
a, b, c = mix1(a, b, c)
a, b, c = mix2(a, b, c)
k = k[12:]
}
//fmt.Printf("k=%q, length=%d\n", k, length)
/* handle the last (probably partial) block */
/*
* "k[2]&0xffffff" actually reads beyond the end of the string, but
* then masks off the part it's not allowed to read. Because the
* string is aligned, the masked-off tail is in the same word as the
* rest of the string. Every machine with memory protection I've seen
* does it on word boundaries, so is OK with this. But VALGRIND will
* still catch it and complain. The masking trick does make the hash
* noticably faster for short strings (like English words).
*/
//fmt.Printf("length now=%d\n", length)
switch length {
case 12:
a += *(*uint32)(unsafe.Pointer(&k[0]))
b += *(*uint32)(unsafe.Pointer(&k[4]))
c += *(*uint32)(unsafe.Pointer(&k[8]))
case 11:
c += uint32(k[10]) << 16
fallthrough
case 10:
c += uint32(k[9]) << 8
fallthrough
case 9:
c += uint32(k[8])
fallthrough
case 8:
a += *(*uint32)(unsafe.Pointer(&k[0]))
b += *(*uint32)(unsafe.Pointer(&k[4]))
break
case 7:
b += uint32(k[6]) << 16
fallthrough
case 6:
b += uint32(k[5]) << 8
fallthrough
case 5:
b += uint32(k[4])
fallthrough
case 4:
a += *(*uint32)(unsafe.Pointer(&k[0]))
break
case 3:
a += uint32(k[2]) << 16
fallthrough
case 2:
a += uint32(k[1]) << 8
fallthrough
case 1:
a += uint32(k[0])
break
case 0:
//fmt.Printf("case 0\n")
return c, b /* zero length strings require no mixing */
}
a, b, c = final1(a, b, c)
a, b, c = final2(a, b, c)
return c, b
}
func HashString(s string, pc, pb uint32) (rpc, rpb uint32) {
k := ([]byte)(s)
rpc, rpb = Jenkins364(k, len(k), pc, pb)
return
}
func HashBytesLength(k []byte, length int, seed uint32) uint32 {
if length > len(k) {
fmt.Printf("len(k)=%d, length=%d\n", len(k), length)
panic("HashBytesLength")
}
ret, _ := Jenkins364(k, length, seed, 0)
return ret
}
//
// Streaming interface and new interface
type State332 struct {
hash uint32
seed uint32
pc uint32
pb uint32
clen int
tail []byte
}
type State332c struct {
State332
}
type State332b struct {
State332
}
type State364 struct {
hash uint64
seed uint64
pc uint32
pb uint32
clen int
tail []byte
}
type State264 struct {
hash uint64
seed uint64
}
type State232 struct {
hash uint32
seed uint32
clen int
tail []byte
}
// const Size = 4
// Sum32 returns the 32 bit hash of data given the seed.
// This is code is what I started with before I added the hash.Hash and hash.Hash32 interfaces.
func Sum32(data []byte, seed uint32) uint32 {
rpc, _ := Jenkins364(data, len(data), seed, seed)
return rpc
}
// New returns a new hash.Hash32 interface that computes a 32 bit jenkins lookup3 hash.
func New(seed uint32) hash.Hash32 {
s := new(State332c)
s.seed = seed
s.Reset()
return s
}
// New returns a new nhash.HashF32 interface that computes a 32 bit jenkins lookup3 hash.
func New332c(seed uint32) nhash.HashF32 {
s := new(State332c)
s.seed = seed
s.Reset()
return s
}
/*
func New332b(seed uint32) nhash.HashF32 {
s := new(State332b)
s.seed = seed
s.Reset()
return s
}
*/
// Return the size of the resulting hash.
func (d *State332c) Size() int { return 4 }
// Return the blocksize of the hash which in this case is 1 byte.
func (d *State332c) BlockSize() int { return 1 }
// Return the maximum number of seed bypes required. In this case 2 x 32
func (d *State332c) NumSeedBytes() int {
return 8
}
// Return the number of bits the hash function outputs.
func (d *State332c) HashSizeInBits() int {
return 32
}
// Reset the hash state.
func (d *State332c) Reset() {
d.pc = d.seed
d.pb = d.seed
d.clen = 0
d.tail = nil
}
// Accept a byte stream p used for calculating the hash. For now this call is lazy and the actual hash calculations take place in Sum() and Sum32().
func (d *State332c) Write(p []byte) (nn int, err error) {
l := len(p)
d.clen += l
d.tail = append(d.tail, p...)
return l, nil
}
// Return the current hash as a byte slice.
func (d *State332c) Sum(b []byte) []byte {
d.pc, d.pb = Jenkins364(d.tail, len(d.tail), d.pc, d.pb)
d.hash = d.pc
h := d.hash
return append(b, byte(h>>24), byte(h>>16), byte(h>>8), byte(h))
}
// Return the current hash as a 32 bit unsigned type.
func (d *State332c) Sum32() uint32 {
d.pc, d.pb = Jenkins364(d.tail, len(d.tail), d.pc, d.pb)
d.hash = d.pc
return d.hash
}
// Given b as input and an optional 32 bit seed return the Jenkins lookup3 hash c bits.
func (d *State332c) Hash32(b []byte, seeds ...uint32) uint32 {
//fmt.Printf("len(b)=%d, b=%x\n", len(b), b)
/*
switch len(seeds) {
case 2:
d.pb = seeds[1]
fallthrough
case 1:
d.pc = seeds[0]
default:
d.pc, d.pb = 0, 0
}
*/
d.pc, d.pb = 0, 0
d.pc, d.pb = Jenkins364(b, len(b), d.pc, d.pb)
d.hash = d.pc
return d.hash
}
// ----
// changed this from HashF64 to Hash64 to get streaming functions, what did I break? 7-22-15
func New364(seed uint64) nhash.Hash64 {
s := new(State364)
s.seed = seed
s.Reset()
return s
}
// Return the size of the resulting hash.
func (d *State364) Size() int { return 8 }
// Return the blocksize of the hash which in this case is 1 byte.
func (d *State364) BlockSize() int { return 1 }
// Return the maximum number of seed bypes required. In this case 2 x 32
func (d *State364) NumSeedBytes() int {
return 8
}
// Return the number of bits the hash function outputs.
func (d *State364) HashSizeInBits() int {
return 64
}
// Reset the hash state.
func (d *State364) Reset() {
d.pc = uint32(d.seed)
d.pb = uint32(d.seed >> 32)
d.clen = 0
d.tail = nil
}
// Accept a byte stream p used for calculating the hash. For now this call is lazy and the actual hash calculations take place in Sum() and Sum32().