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