-
Notifications
You must be signed in to change notification settings - Fork 2
/
spooky.go
214 lines (196 loc) · 3.47 KB
/
spooky.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
package chd
import (
"reflect"
"unsafe"
)
const sc = uint64(0xdeadbeefdeadbeef)
func rot64(x uint64, k uint) uint64 {
return (x << k) | (x >> (64 - k))
}
func shortMix(h0, h1, h2, h3 uint64) (uint64, uint64, uint64, uint64) {
h2 = rot64(h2, 50)
h2 += h3
h0 ^= h2
h3 = rot64(h3, 52)
h3 += h0
h1 ^= h3
h0 = rot64(h0, 30)
h0 += h1
h2 ^= h0
h1 = rot64(h1, 41)
h1 += h2
h3 ^= h1
h2 = rot64(h2, 54)
h2 += h3
h0 ^= h2
h3 = rot64(h3, 48)
h3 += h0
h1 ^= h3
h0 = rot64(h0, 38)
h0 += h1
h2 ^= h0
h1 = rot64(h1, 37)
h1 += h2
h3 ^= h1
h2 = rot64(h2, 62)
h2 += h3
h0 ^= h2
h3 = rot64(h3, 34)
h3 += h0
h1 ^= h3
h0 = rot64(h0, 5)
h0 += h1
h2 ^= h0
h1 = rot64(h1, 36)
h1 += h2
h3 ^= h1
return h0, h1, h2, h3
}
func shortEnd(h0, h1, h2, h3 uint64) (uint64, uint64, uint64, uint64) {
h3 ^= h2
h2 = rot64(h2, 15)
h3 += h2
h0 ^= h3
h3 = rot64(h3, 52)
h0 += h3
h1 ^= h0
h0 = rot64(h0, 26)
h1 += h0
h2 ^= h1
h1 = rot64(h1, 51)
h2 += h1
h3 ^= h2
h2 = rot64(h2, 28)
h3 += h2
h0 ^= h3
h3 = rot64(h3, 9)
h0 += h3
h1 ^= h0
h0 = rot64(h0, 47)
h1 += h0
h2 ^= h1
h1 = rot64(h1, 54)
h2 += h1
h3 ^= h2
h2 = rot64(h2, 32)
h3 += h2
h0 ^= h3
h3 = rot64(h3, 25)
h0 += h3
h1 ^= h0
h0 = rot64(h0, 63)
h1 += h0
return h0, h1, h2, h3
}
// spookyHash is a port of Bon Jenkins' SpookyHash::Short but
// instead of just returning just two, it returns four uint64 values.
func spookyHash(message []byte, seed1, seed2 uint64) (uint64, uint64, uint64, uint64) {
u8 := message
length := len(u8)
var u64 []uint64
var u32 []uint32
if length >= 8 {
u64 = uint64SliceFromByteSlice(u8)
}
if length >= 4 {
u32 = uint32SliceFromByteSlice(u8)
}
remainder := length & 31
a := seed1
b := seed2
c := sc
d := sc
if length > 15 {
// handle all complete sets of 32 bytes
for len(u64) >= 4 {
c += u64[0]
d += u64[1]
a, b, c, d = shortMix(a, b, c, d)
a += u64[2]
b += u64[3]
u64 = u64[4:]
u32 = u32[8:]
u8 = u8[32:]
}
//Handle the case of 16+ remaining bytes.
if remainder >= 16 {
c += u64[0]
d += u64[1]
a, b, c, d = shortMix(a, b, c, d)
u64 = u64[2:]
u32 = u32[4:]
u8 = u8[16:]
remainder -= 16
}
}
// Handle the last 0..15 bytes, and its length
d += uint64(length) << 56
switch remainder {
case 15:
d += uint64(u8[14]) << 48
fallthrough
case 14:
d += uint64(u8[13]) << 40
fallthrough
case 13:
d += uint64(u8[12]) << 32
fallthrough
case 12:
d += uint64(u32[2])
c += u64[0]
break
case 11:
d += uint64(u8[10]) << 16
fallthrough
case 10:
d += uint64(u8[9]) << 8
fallthrough
case 9:
d += uint64(u8[8])
fallthrough
case 8:
c += u64[0]
break
case 7:
c += uint64(u8[6]) << 48
fallthrough
case 6:
c += uint64(u8[5]) << 40
fallthrough
case 5:
c += uint64(u8[4]) << 32
fallthrough
case 4:
c += uint64(u32[0])
break
case 3:
c += uint64(u8[2]) << 16
fallthrough
case 2:
c += uint64(u8[1]) << 8
fallthrough
case 1:
c += uint64(u8[0])
break
case 0:
c += sc
d += sc
}
return shortEnd(a, b, c, d)
}
func uint64SliceFromByteSlice(bytes []byte) []uint64 {
sh := &reflect.SliceHeader{}
sh.Cap = cap(bytes) / 8
sh.Len = len(bytes) / 8
sh.Data = (uintptr)(unsafe.Pointer(&bytes[0]))
data := *(*[]uint64)(unsafe.Pointer(sh))
return data
}
func uint32SliceFromByteSlice(bytes []byte) []uint32 {
sh := &reflect.SliceHeader{}
sh.Cap = cap(bytes) / 4
sh.Len = len(bytes) / 4
sh.Data = (uintptr)(unsafe.Pointer(&bytes[0]))
data := *(*[]uint32)(unsafe.Pointer(sh))
return data
}