1
2
3
4
5 package runtime
6
7 import (
8 "internal/abi"
9 "internal/byteorder"
10 "internal/cpu"
11 "internal/goarch"
12 "internal/runtime/sys"
13 "unsafe"
14 )
15
16 const (
17
18 hashSize = (1-goarch.IsWasm)*goarch.PtrSize + goarch.IsWasm*4
19 c0 = uintptr((8-hashSize)/4*2860486313 + (hashSize-4)/4*33054211828000289)
20 c1 = uintptr((8-hashSize)/4*3267000013 + (hashSize-4)/4*23344194077549503)
21 )
22
23 func trimHash(h uintptr) uintptr {
24 if goarch.IsWasm != 0 {
25
26
27
28
29 return uintptr(uint32(h))
30 }
31 return h
32 }
33
34 func memhash0(p unsafe.Pointer, h uintptr) uintptr {
35 return h
36 }
37
38 func memhash8(p unsafe.Pointer, h uintptr) uintptr {
39 return memhash(p, h, 1)
40 }
41
42 func memhash16(p unsafe.Pointer, h uintptr) uintptr {
43 return memhash(p, h, 2)
44 }
45
46 func memhash128(p unsafe.Pointer, h uintptr) uintptr {
47 return memhash(p, h, 16)
48 }
49
50
51 func memhash_varlen(p unsafe.Pointer, h uintptr) uintptr {
52 ptr := sys.GetClosurePtr()
53 size := *(*uintptr)(unsafe.Pointer(ptr + unsafe.Sizeof(h)))
54 return memhash(p, h, size)
55 }
56
57
58
59
60 var useAeshash bool
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 func memhash(p unsafe.Pointer, h, s uintptr) uintptr
82
83 func memhash32(p unsafe.Pointer, h uintptr) uintptr
84
85 func memhash64(p unsafe.Pointer, h uintptr) uintptr
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 func strhash(p unsafe.Pointer, h uintptr) uintptr
101
102 func strhashFallback(a unsafe.Pointer, h uintptr) uintptr {
103 x := (*stringStruct)(a)
104 return memhashFallback(x.str, h, uintptr(x.len))
105 }
106
107
108
109
110
111
112 func f32hash(p unsafe.Pointer, h uintptr) uintptr {
113 f := *(*float32)(p)
114 switch {
115 case f == 0:
116 return trimHash(c1 * (c0 ^ h))
117 case f != f:
118 return trimHash(c1 * (c0 ^ h ^ uintptr(rand())))
119 default:
120 return memhash(p, h, 4)
121 }
122 }
123
124 func f64hash(p unsafe.Pointer, h uintptr) uintptr {
125 f := *(*float64)(p)
126 switch {
127 case f == 0:
128 return trimHash(c1 * (c0 ^ h))
129 case f != f:
130 return trimHash(c1 * (c0 ^ h ^ uintptr(rand())))
131 default:
132 return memhash(p, h, 8)
133 }
134 }
135
136 func c64hash(p unsafe.Pointer, h uintptr) uintptr {
137 x := (*[2]float32)(p)
138 return f32hash(unsafe.Pointer(&x[1]), f32hash(unsafe.Pointer(&x[0]), h))
139 }
140
141 func c128hash(p unsafe.Pointer, h uintptr) uintptr {
142 x := (*[2]float64)(p)
143 return f64hash(unsafe.Pointer(&x[1]), f64hash(unsafe.Pointer(&x[0]), h))
144 }
145
146 func interhash(p unsafe.Pointer, h uintptr) uintptr {
147 a := (*iface)(p)
148 tab := a.tab
149 if tab == nil {
150 return h
151 }
152 t := tab.Type
153 if t.Equal == nil {
154
155
156
157
158 panic(errorString("hash of unhashable type " + toRType(t).string()))
159 }
160 if t.IsDirectIface() {
161 return trimHash(c1 * typehash(t, unsafe.Pointer(&a.data), h^c0))
162 } else {
163 return trimHash(c1 * typehash(t, a.data, h^c0))
164 }
165 }
166
167
168
169
170
171
172
173
174
175
176
177 func nilinterhash(p unsafe.Pointer, h uintptr) uintptr {
178 a := (*eface)(p)
179 t := a._type
180 if t == nil {
181 return h
182 }
183 if t.Equal == nil {
184
185 panic(errorString("hash of unhashable type " + toRType(t).string()))
186 }
187 if t.IsDirectIface() {
188 return trimHash(c1 * typehash(t, unsafe.Pointer(&a.data), h^c0))
189 } else {
190 return trimHash(c1 * typehash(t, a.data, h^c0))
191 }
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 func typehash(t *_type, p unsafe.Pointer, h uintptr) uintptr {
214 if t.TFlag&abi.TFlagRegularMemory != 0 {
215
216 switch t.Size_ {
217 case 4:
218 return memhash32(p, h)
219 case 8:
220 return memhash64(p, h)
221 default:
222 return memhash(p, h, t.Size_)
223 }
224 }
225 switch t.Kind() {
226 case abi.Float32:
227 return f32hash(p, h)
228 case abi.Float64:
229 return f64hash(p, h)
230 case abi.Complex64:
231 return c64hash(p, h)
232 case abi.Complex128:
233 return c128hash(p, h)
234 case abi.String:
235 return strhash(p, h)
236 case abi.Interface:
237 i := (*interfacetype)(unsafe.Pointer(t))
238 if len(i.Methods) == 0 {
239 return nilinterhash(p, h)
240 }
241 return interhash(p, h)
242 case abi.Array:
243 a := (*arraytype)(unsafe.Pointer(t))
244 for i := uintptr(0); i < a.Len; i++ {
245 h = typehash(a.Elem, add(p, i*a.Elem.Size_), h)
246 }
247 return h
248 case abi.Struct:
249 s := (*structtype)(unsafe.Pointer(t))
250 for _, f := range s.Fields {
251 if f.Name.IsBlank() {
252 continue
253 }
254 h = typehash(f.Typ, add(p, f.Offset), h)
255 }
256 return h
257 default:
258
259
260 panic(errorString("hash of unhashable type " + toRType(t).string()))
261 }
262 }
263
264
265 func reflect_typehash(t *_type, p unsafe.Pointer, h uintptr) uintptr {
266 return typehash(t, p, h)
267 }
268
269 func memequal0(p, q unsafe.Pointer) bool {
270 return true
271 }
272 func memequal8(p, q unsafe.Pointer) bool {
273 return *(*int8)(p) == *(*int8)(q)
274 }
275 func memequal16(p, q unsafe.Pointer) bool {
276 return *(*int16)(p) == *(*int16)(q)
277 }
278 func memequal32(p, q unsafe.Pointer) bool {
279 return *(*int32)(p) == *(*int32)(q)
280 }
281 func memequal64(p, q unsafe.Pointer) bool {
282 return *(*int64)(p) == *(*int64)(q)
283 }
284 func memequal128(p, q unsafe.Pointer) bool {
285 return *(*[2]int64)(p) == *(*[2]int64)(q)
286 }
287 func f32equal(p, q unsafe.Pointer) bool {
288 return *(*float32)(p) == *(*float32)(q)
289 }
290 func f64equal(p, q unsafe.Pointer) bool {
291 return *(*float64)(p) == *(*float64)(q)
292 }
293 func c64equal(p, q unsafe.Pointer) bool {
294 return *(*complex64)(p) == *(*complex64)(q)
295 }
296 func c128equal(p, q unsafe.Pointer) bool {
297 return *(*complex128)(p) == *(*complex128)(q)
298 }
299 func strequal(p, q unsafe.Pointer) bool {
300 return *(*string)(p) == *(*string)(q)
301 }
302 func interequal(p, q unsafe.Pointer) bool {
303 x := *(*iface)(p)
304 y := *(*iface)(q)
305 return x.tab == y.tab && ifaceeq(x.tab, x.data, y.data)
306 }
307 func nilinterequal(p, q unsafe.Pointer) bool {
308 x := *(*eface)(p)
309 y := *(*eface)(q)
310 return x._type == y._type && efaceeq(x._type, x.data, y.data)
311 }
312 func efaceeq(t *_type, x, y unsafe.Pointer) bool {
313 if t == nil {
314 return true
315 }
316 eq := t.Equal
317 if eq == nil {
318 panic(errorString("comparing uncomparable type " + toRType(t).string()))
319 }
320 if t.IsDirectIface() {
321
322
323
324 return x == y
325 }
326 return eq(x, y)
327 }
328 func ifaceeq(tab *itab, x, y unsafe.Pointer) bool {
329 if tab == nil {
330 return true
331 }
332 t := tab.Type
333 eq := t.Equal
334 if eq == nil {
335 panic(errorString("comparing uncomparable type " + toRType(t).string()))
336 }
337 if t.IsDirectIface() {
338
339 return x == y
340 }
341 return eq(x, y)
342 }
343
344
345
346
347
348
349
350
351
352
353
354
355 func stringHash(s string, seed uintptr) uintptr {
356 return strhash(noescape(unsafe.Pointer(&s)), seed)
357 }
358
359 func bytesHash(b []byte, seed uintptr) uintptr {
360 s := (*slice)(unsafe.Pointer(&b))
361 return memhash(s.array, seed, uintptr(s.len))
362 }
363
364 func int32Hash(i uint32, seed uintptr) uintptr {
365 return memhash32(noescape(unsafe.Pointer(&i)), seed)
366 }
367
368 func int64Hash(i uint64, seed uintptr) uintptr {
369 return memhash64(noescape(unsafe.Pointer(&i)), seed)
370 }
371
372 func efaceHash(i any, seed uintptr) uintptr {
373 return nilinterhash(noescape(unsafe.Pointer(&i)), seed)
374 }
375
376 func ifaceHash(i interface {
377 F()
378 }, seed uintptr) uintptr {
379 return interhash(noescape(unsafe.Pointer(&i)), seed)
380 }
381
382 const hashRandomBytes = goarch.PtrSize / 4 * 64
383
384
385 var aeskeysched [hashRandomBytes]byte
386
387
388 var hashkey [4]uintptr
389
390 func alginit() {
391
392 if (GOARCH == "386" || GOARCH == "amd64") &&
393 cpu.X86.HasAES &&
394 cpu.X86.HasSSSE3 &&
395 cpu.X86.HasSSE41 {
396 initAlgAES()
397 return
398 }
399 if GOARCH == "arm64" && cpu.ARM64.HasAES {
400 initAlgAES()
401 return
402 }
403 for i := range hashkey {
404 hashkey[i] = uintptr(bootstrapRand())
405 }
406 }
407
408 func initAlgAES() {
409 useAeshash = true
410
411 key := (*[hashRandomBytes / 8]uint64)(unsafe.Pointer(&aeskeysched))
412 for i := range key {
413 key[i] = bootstrapRand()
414 }
415 }
416
417
418 func readUnaligned32(p unsafe.Pointer) uint32 {
419 q := (*[4]byte)(p)
420 if goarch.BigEndian {
421 return byteorder.BEUint32(q[:])
422 }
423 return byteorder.LEUint32(q[:])
424 }
425
426 func readUnaligned64(p unsafe.Pointer) uint64 {
427 q := (*[8]byte)(p)
428 if goarch.BigEndian {
429 return byteorder.BEUint64(q[:])
430 }
431 return byteorder.LEUint64(q[:])
432 }
433
View as plain text