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