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
|
// 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 runtime
import (
"runtime/internal/sys"
"unsafe"
)
// For gccgo, use go:linkname to rename compiler-called functions to
// themselves, so that the compiler will export them.
//
//go:linkname interhash runtime.interhash
//go:linkname nilinterhash runtime.nilinterhash
//go:linkname interequal runtime.interequal
//go:linkname nilinterequal runtime.nilinterequal
//go:linkname efaceeq runtime.efaceeq
//go:linkname ifaceeq runtime.ifaceeq
//go:linkname ifacevaleq runtime.ifacevaleq
//go:linkname ifaceefaceeq runtime.ifaceefaceeq
//go:linkname efacevaleq runtime.efacevaleq
//go:linkname eqstring runtime.eqstring
//go:linkname cmpstring runtime.cmpstring
//
// Temporary to be called from C code.
//go:linkname alginit runtime.alginit
const (
c0 = uintptr((8-sys.PtrSize)/4*2860486313 + (sys.PtrSize-4)/4*33054211828000289)
c1 = uintptr((8-sys.PtrSize)/4*3267000013 + (sys.PtrSize-4)/4*23344194077549503)
)
var useAeshash bool
// in C code
func aeshashbody(p unsafe.Pointer, h, s uintptr, sched []byte) uintptr
func aeshash(p unsafe.Pointer, h, s uintptr) uintptr {
return aeshashbody(p, h, s, aeskeysched[:])
}
func aeshashstr(p unsafe.Pointer, h uintptr) uintptr {
ps := (*stringStruct)(p)
return aeshashbody(unsafe.Pointer(ps.str), h, uintptr(ps.len), aeskeysched[:])
}
func interhash(p unsafe.Pointer, h uintptr, size uintptr) uintptr {
a := (*iface)(p)
tab := a.tab
if tab == nil {
return h
}
t := *(**_type)(tab)
fn := t.hashfn
if fn == nil {
panic(errorString("hash of unhashable type " + *t.string))
}
if isDirectIface(t) {
return c1 * fn(unsafe.Pointer(&a.data), h^c0, t.size)
} else {
return c1 * fn(a.data, h^c0, t.size)
}
}
func nilinterhash(p unsafe.Pointer, h uintptr, size uintptr) uintptr {
a := (*eface)(p)
t := a._type
if t == nil {
return h
}
fn := t.hashfn
if fn == nil {
panic(errorString("hash of unhashable type " + *t.string))
}
if isDirectIface(t) {
return c1 * fn(unsafe.Pointer(&a.data), h^c0, t.size)
} else {
return c1 * fn(a.data, h^c0, t.size)
}
}
func interequal(p, q unsafe.Pointer, size uintptr) bool {
return ifaceeq(*(*iface)(p), *(*iface)(q))
}
func nilinterequal(p, q unsafe.Pointer, size uintptr) bool {
return efaceeq(*(*eface)(p), *(*eface)(q))
}
func efaceeq(x, y eface) bool {
t := x._type
if !eqtype(t, y._type) {
return false
}
if t == nil {
return true
}
eq := t.equalfn
if eq == nil {
panic(errorString("comparing uncomparable type " + *t.string))
}
if isDirectIface(t) {
return x.data == y.data
}
return eq(x.data, y.data, t.size)
}
func ifaceeq(x, y iface) bool {
xtab := x.tab
if xtab == nil && y.tab == nil {
return true
}
if xtab == nil || y.tab == nil {
return false
}
t := *(**_type)(xtab)
if !eqtype(t, *(**_type)(y.tab)) {
return false
}
eq := t.equalfn
if eq == nil {
panic(errorString("comparing uncomparable type " + *t.string))
}
if isDirectIface(t) {
return x.data == y.data
}
return eq(x.data, y.data, t.size)
}
func ifacevaleq(x iface, t *_type, p unsafe.Pointer) bool {
if x.tab == nil {
return false
}
xt := *(**_type)(x.tab)
if !eqtype(xt, t) {
return false
}
eq := t.equalfn
if eq == nil {
panic(errorString("comparing uncomparable type " + *t.string))
}
if isDirectIface(t) {
return x.data == p
}
return eq(x.data, p, t.size)
}
func ifaceefaceeq(x iface, y eface) bool {
if x.tab == nil && y._type == nil {
return true
}
if x.tab == nil || y._type == nil {
return false
}
xt := *(**_type)(x.tab)
if !eqtype(xt, y._type) {
return false
}
eq := xt.equalfn
if eq == nil {
panic(errorString("comparing uncomparable type " + *xt.string))
}
if isDirectIface(xt) {
return x.data == y.data
}
return eq(x.data, y.data, xt.size)
}
func efacevaleq(x eface, t *_type, p unsafe.Pointer) bool {
if x._type == nil {
return false
}
if !eqtype(x._type, t) {
return false
}
eq := t.equalfn
if eq == nil {
panic(errorString("comparing uncomparable type " + *t.string))
}
if isDirectIface(t) {
return x.data == p
}
return eq(x.data, p, t.size)
}
func eqstring(x, y string) bool {
a := stringStructOf(&x)
b := stringStructOf(&y)
if a.len != b.len {
return false
}
return memcmp(unsafe.Pointer(a.str), unsafe.Pointer(b.str), uintptr(a.len)) == 0
}
func cmpstring(x, y string) int {
a := stringStructOf(&x)
b := stringStructOf(&y)
l := a.len
if l > b.len {
l = b.len
}
i := memcmp(unsafe.Pointer(a.str), unsafe.Pointer(b.str), uintptr(l))
if i != 0 {
return int(i)
}
if a.len < b.len {
return -1
} else if a.len > b.len {
return 1
}
return 0
}
// Force the creation of function descriptors for equality and hash
// functions. These will be referenced directly by the compiler.
var _ = memhash
var _ = interhash
var _ = interequal
var _ = nilinterhash
var _ = nilinterequal
const hashRandomBytes = sys.PtrSize / 4 * 64
// used in asm_{386,amd64}.s to seed the hash function
var aeskeysched [hashRandomBytes]byte
// used in hash{32,64}.go to seed the hash function
var hashkey [4]uintptr
func alginit() {
// Install aes hash algorithm if we have the instructions we need
if (GOARCH == "386" || GOARCH == "amd64") &&
GOOS != "nacl" &&
cpuid_ecx&(1<<25) != 0 && // aes (aesenc)
cpuid_ecx&(1<<9) != 0 && // sse3 (pshufb)
cpuid_ecx&(1<<19) != 0 { // sse4.1 (pinsr{d,q})
useAeshash = true
// Initialize with random data so hash collisions will be hard to engineer.
getRandomData(aeskeysched[:])
return
}
getRandomData((*[len(hashkey) * sys.PtrSize]byte)(unsafe.Pointer(&hashkey))[:])
hashkey[0] |= 1 // make sure these numbers are odd
hashkey[1] |= 1
hashkey[2] |= 1
hashkey[3] |= 1
}
|