-
Notifications
You must be signed in to change notification settings - Fork 46
/
hash.go
312 lines (279 loc) · 8.33 KB
/
hash.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
package haxmap
/*
From https://github.com/cespare/xxhash
Copyright (c) 2016 Caleb Spare
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import (
"encoding/binary"
"math/bits"
"reflect"
"unsafe"
)
const (
// hash input allowed sizes
byteSize = 1 << iota
wordSize
dwordSize
qwordSize
owordSize
)
const (
prime1 uint64 = 11400714785074694791
prime2 uint64 = 14029467366897019727
prime3 uint64 = 1609587929392839161
prime4 uint64 = 9650029242287828579
prime5 uint64 = 2870177450012600261
)
var prime1v = prime1
func u64(b []byte) uint64 { return binary.LittleEndian.Uint64(b) }
func u32(b []byte) uint32 { return binary.LittleEndian.Uint32(b) }
func round(acc, input uint64) uint64 {
acc += input * prime2
acc = rol31(acc)
acc *= prime1
return acc
}
func mergeRound(acc, val uint64) uint64 {
val = round(0, val)
acc ^= val
acc = acc*prime1 + prime4
return acc
}
func rol1(x uint64) uint64 { return bits.RotateLeft64(x, 1) }
func rol7(x uint64) uint64 { return bits.RotateLeft64(x, 7) }
func rol11(x uint64) uint64 { return bits.RotateLeft64(x, 11) }
func rol12(x uint64) uint64 { return bits.RotateLeft64(x, 12) }
func rol18(x uint64) uint64 { return bits.RotateLeft64(x, 18) }
func rol23(x uint64) uint64 { return bits.RotateLeft64(x, 23) }
func rol27(x uint64) uint64 { return bits.RotateLeft64(x, 27) }
func rol31(x uint64) uint64 { return bits.RotateLeft64(x, 31) }
// xxHash implementation for known key type sizes, minimal with no branching
var (
// byte hasher, key size -> 1 byte
byteHasher = func(key uint8) uintptr {
h := prime5 + 1
h ^= uint64(key) * prime5
h = bits.RotateLeft64(h, 11) * prime1
h ^= h >> 33
h *= prime2
h ^= h >> 29
h *= prime3
h ^= h >> 32
return uintptr(h)
}
// word hasher, key size -> 2 bytes
wordHasher = func(key uint16) uintptr {
h := prime5 + 2
h ^= (uint64(key) & 0xff) * prime5
h = bits.RotateLeft64(h, 11) * prime1
h ^= ((uint64(key) >> 8) & 0xff) * prime5
h = bits.RotateLeft64(h, 11) * prime1
h ^= h >> 33
h *= prime2
h ^= h >> 29
h *= prime3
h ^= h >> 32
return uintptr(h)
}
// dword hasher, key size -> 4 bytes
dwordHasher = func(key uint32) uintptr {
h := prime5 + 4
h ^= uint64(key) * prime1
h = bits.RotateLeft64(h, 23)*prime2 + prime3
h ^= h >> 33
h *= prime2
h ^= h >> 29
h *= prime3
h ^= h >> 32
return uintptr(h)
}
// separate dword hasher for float32 type
// required for casting float32 to unsigned integer type without any loss of bits
// Example :- casting uint32(1.3) will drop off the 0.3 decimal part but using *(*uint32)(unsafe.Pointer(&key)) will retain all bits (both the integer as well as the decimal part)
// this will ensure correctness of the hash
float32Hasher = func(key float32) uintptr {
h := prime5 + 4
h ^= uint64(*(*uint32)(unsafe.Pointer(&key))) * prime1
h = bits.RotateLeft64(h, 23)*prime2 + prime3
h ^= h >> 33
h *= prime2
h ^= h >> 29
h *= prime3
h ^= h >> 32
return uintptr(h)
}
// qword hasher, key size -> 8 bytes
qwordHasher = func(key uint64) uintptr {
k1 := key * prime2
k1 = bits.RotateLeft64(k1, 31)
k1 *= prime1
h := (prime5 + 8) ^ k1
h = bits.RotateLeft64(h, 27)*prime1 + prime4
h ^= h >> 33
h *= prime2
h ^= h >> 29
h *= prime3
h ^= h >> 32
return uintptr(h)
}
// separate qword hasher for float64 type
// for reason see definition of float32Hasher on line 127
float64Hasher = func(key float64) uintptr {
k1 := *(*uint64)(unsafe.Pointer(&key)) * prime2
k1 = bits.RotateLeft64(k1, 31)
k1 *= prime1
h := (prime5 + 8) ^ k1
h = bits.RotateLeft64(h, 27)*prime1 + prime4
h ^= h >> 33
h *= prime2
h ^= h >> 29
h *= prime3
h ^= h >> 32
return uintptr(h)
}
// separate qword hasher for complex64 type
complex64Hasher = func(key complex64) uintptr {
k1 := *(*uint64)(unsafe.Pointer(&key)) * prime2
k1 = bits.RotateLeft64(k1, 31)
k1 *= prime1
h := (prime5 + 8) ^ k1
h = bits.RotateLeft64(h, 27)*prime1 + prime4
h ^= h >> 33
h *= prime2
h ^= h >> 29
h *= prime3
h ^= h >> 32
return uintptr(h)
}
)
func (m *Map[K, V]) setDefaultHasher() {
// default hash functions
switch reflect.TypeOf(*new(K)).Kind() {
case reflect.String:
// use default xxHash algorithm for key of any size for golang string data type
m.hasher = func(key K) uintptr {
sh := (*reflect.StringHeader)(unsafe.Pointer(&key))
b := unsafe.Slice((*byte)(unsafe.Pointer(sh.Data)), sh.Len)
n := sh.Len
var h uint64
if n >= 32 {
v1 := prime1v + prime2
v2 := prime2
v3 := uint64(0)
v4 := -prime1v
for len(b) >= 32 {
v1 = round(v1, u64(b[0:8:len(b)]))
v2 = round(v2, u64(b[8:16:len(b)]))
v3 = round(v3, u64(b[16:24:len(b)]))
v4 = round(v4, u64(b[24:32:len(b)]))
b = b[32:len(b):len(b)]
}
h = rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4)
h = mergeRound(h, v1)
h = mergeRound(h, v2)
h = mergeRound(h, v3)
h = mergeRound(h, v4)
} else {
h = prime5
}
h += uint64(n)
i, end := 0, len(b)
for ; i+8 <= end; i += 8 {
k1 := round(0, u64(b[i:i+8:len(b)]))
h ^= k1
h = rol27(h)*prime1 + prime4
}
if i+4 <= end {
h ^= uint64(u32(b[i:i+4:len(b)])) * prime1
h = rol23(h)*prime2 + prime3
i += 4
}
for ; i < end; i++ {
h ^= uint64(b[i]) * prime5
h = rol11(h) * prime1
}
h ^= h >> 33
h *= prime2
h ^= h >> 29
h *= prime3
h ^= h >> 32
return uintptr(h)
}
case reflect.Int, reflect.Uint, reflect.Uintptr, reflect.UnsafePointer:
switch intSizeBytes {
case 2:
// word hasher
m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&wordHasher))
case 4:
// dword hasher
m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&dwordHasher))
case 8:
// qword hasher
m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&qwordHasher))
}
case reflect.Int8, reflect.Uint8:
// byte hasher
m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&byteHasher))
case reflect.Int16, reflect.Uint16:
// word hasher
m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&wordHasher))
case reflect.Int32, reflect.Uint32:
// dword hasher
m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&dwordHasher))
case reflect.Float32:
// custom float32 dword hasher
m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&float32Hasher))
case reflect.Int64, reflect.Uint64:
// qword hasher
m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&qwordHasher))
case reflect.Float64:
// custom float64 qword hasher
m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&float64Hasher))
case reflect.Complex64:
// custom complex64 qword hasher
m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&complex64Hasher))
case reflect.Complex128:
// oword hasher, key size -> 16 bytes
m.hasher = func(key K) uintptr {
b := *(*[owordSize]byte)(unsafe.Pointer(&key))
h := prime5 + 16
val := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
k1 := val * prime2
k1 = bits.RotateLeft64(k1, 31)
k1 *= prime1
h ^= k1
h = bits.RotateLeft64(h, 27)*prime1 + prime4
val = uint64(b[8]) | uint64(b[9])<<8 | uint64(b[10])<<16 | uint64(b[11])<<24 |
uint64(b[12])<<32 | uint64(b[13])<<40 | uint64(b[14])<<48 | uint64(b[15])<<56
k1 = val * prime2
k1 = bits.RotateLeft64(k1, 31)
k1 *= prime1
h ^= k1
h = bits.RotateLeft64(h, 27)*prime1 + prime4
h ^= h >> 33
h *= prime2
h ^= h >> 29
h *= prime3
h ^= h >> 32
return uintptr(h)
}
}
}