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