-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
221 lines (188 loc) · 4.71 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
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hash
import (
"math/rand"
"reflect"
"runtime"
"time"
"unsafe"
)
const (
c0 = uintptr((8-ptrSize)/4*2860486313 +
(ptrSize-4)/4*33054211828000289)
c1 = uintptr((8-ptrSize)/4*3267000013 +
(ptrSize-4)/4*23344194077549503)
)
var (
hashkey [4]uintptr
)
func init() {
hashkeyInit(time.Now().UnixNano())
}
func hashkeyInit(seed int64) {
rand.Seed(seed)
rand.Read((*[uintptr(len(hashkey)) *
ptrSize]byte)(unsafe.Pointer(&hashkey))[:])
hashkey[0] |= 1 // make sure these numbers are odd
hashkey[1] |= 1
hashkey[2] |= 1
hashkey[3] |= 1
}
type Hasher interface {
Hash() uintptr
}
type SeededHasher interface {
SeededHash(seed uintptr) uintptr
}
func Any(v interface{}, seed uintptr) uintptr {
return hash(v, seed)
}
func hash(v interface{}, seed uintptr) uintptr {
switch val := v.(type) {
case SeededHasher:
return val.SeededHash(seed)
case Hasher:
return val.Hash()
case string:
return String(val, seed)
case []byte:
return Bytes(val, seed)
case int8:
return Int8(val, seed)
case uint8:
return Uint8(val, seed)
case int16:
return Int16(val, seed)
case uint16:
return Uint16(val, seed)
case int32:
return Int32(val, seed)
case uint32:
return Uint32(val, seed)
case int:
return Int(val, seed)
case uint:
return Uint(val, seed)
case int64:
return Int64(val, seed)
case uint64:
return Uint64(val, seed)
case uintptr:
return memhash(unsafe.Pointer(&val), seed, ptrSize)
case float32:
return Float32(val, seed)
case float64:
return Float64(val, seed)
case complex64:
return Complex64(val, seed)
case complex128:
return Complex128(val, seed)
case struct{}:
return memhash0(unsafe.Pointer(&val), seed)
default:
return reflecthash(val, seed)
}
}
func Unsafe(ptr unsafe.Pointer, size uintptr, seed uintptr) uintptr {
return memhash(ptr, seed, size)
}
func Int8(val int8, seed uintptr) uintptr {
return memhash8(unsafe.Pointer(&val), seed)
}
func Uint8(val uint8, seed uintptr) uintptr {
return memhash8(unsafe.Pointer(&val), seed)
}
func Int16(val int16, seed uintptr) uintptr {
return memhash16(unsafe.Pointer(&val), seed)
}
func Uint16(val uint16, seed uintptr) uintptr {
return memhash16(unsafe.Pointer(&val), seed)
}
func Int32(val int32, seed uintptr) uintptr {
return memhash32(unsafe.Pointer(&val), seed)
}
func Uint32(val uint32, seed uintptr) uintptr {
return memhash32(unsafe.Pointer(&val), seed)
}
func Int(val int, seed uintptr) uintptr {
return memhash32(unsafe.Pointer(&val), seed)
}
func Uint(val uint, seed uintptr) uintptr {
return memhash32(unsafe.Pointer(&val), seed)
}
func Int64(val int64, seed uintptr) uintptr {
return memhash64(unsafe.Pointer(&val), seed)
}
func Uint64(val uint64, seed uintptr) uintptr {
return memhash64(unsafe.Pointer(&val), seed)
}
func String(s string, h uintptr) uintptr {
hdr := (*reflect.StringHeader)(unsafe.Pointer(&s))
out := memhash(unsafe.Pointer(hdr.Data), h, uintptr(hdr.Len))
runtime.KeepAlive(&s)
return out
}
func Bytes(b []byte, seed uintptr) uintptr {
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
out := memhash(unsafe.Pointer(hdr.Data), seed, uintptr(hdr.Len))
runtime.KeepAlive(&b)
return out
}
func Float32(f float32, h uintptr) uintptr {
switch {
case f == 0:
return c1 * (c0 ^ h) // +0, -0
case f != f:
return c1 * (c0 ^ h ^ uintptr(rand.Uint32())) //any kind of NaN
default:
return memhash(unsafe.Pointer(&f), h, 4)
}
}
func Float64(f float64, h uintptr) uintptr {
switch {
case f == 0:
return c1 * (c0 ^ h) // +0, -0
case f != f:
return c1 * (c0 ^ h ^ uintptr(rand.Uint32())) //any kind of NaN
default:
return memhash(unsafe.Pointer(&f), h, 8)
}
}
func Complex64(c complex64, h uintptr) uintptr {
p := unsafe.Pointer(&c)
x := (*[2]float32)(p)
return Float32(x[1], Float32(x[0], h))
}
func Complex128(c complex128, h uintptr) uintptr {
p := unsafe.Pointer(&c)
x := (*[2]float64)(p)
return Float64(x[1], Float64(x[0], h))
}
func reflecthash(v interface{}, seed uintptr) uintptr {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
vp := reflect.New(val.Type())
vp.Elem().Set(val)
val = vp
}
valType := val.Type().Elem()
ptr := unsafe.Pointer(val.Pointer())
return c1 * memhash(ptr, seed^c0, valType.Size())
}
func memhash0(p unsafe.Pointer, h uintptr) uintptr {
return h
}
func memhash8(p unsafe.Pointer, h uintptr) uintptr {
return memhash(p, h, 1)
}
func memhash16(p unsafe.Pointer, h uintptr) uintptr {
return memhash(p, h, 2)
}
func memhash128(p unsafe.Pointer, h uintptr) uintptr {
return memhash(p, h, 16)
}
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}