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