github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/runtime/alg.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package runtime 6 7 import ( 8 "internal/cpu" 9 "runtime/internal/sys" 10 "unsafe" 11 ) 12 13 const ( 14 c0 = uintptr((8-sys.PtrSize)/4*2860486313 + (sys.PtrSize-4)/4*33054211828000289) 15 c1 = uintptr((8-sys.PtrSize)/4*3267000013 + (sys.PtrSize-4)/4*23344194077549503) 16 ) 17 18 func memhash0(p unsafe.Pointer, h uintptr) uintptr { 19 return h 20 } 21 22 func memhash8(p unsafe.Pointer, h uintptr) uintptr { 23 return memhash(p, h, 1) 24 } 25 26 func memhash16(p unsafe.Pointer, h uintptr) uintptr { 27 return memhash(p, h, 2) 28 } 29 30 func memhash128(p unsafe.Pointer, h uintptr) uintptr { 31 return memhash(p, h, 16) 32 } 33 34 //go:nosplit 35 func memhash_varlen(p unsafe.Pointer, h uintptr) uintptr { 36 ptr := getclosureptr() 37 size := *(*uintptr)(unsafe.Pointer(ptr + unsafe.Sizeof(h))) 38 return memhash(p, h, size) 39 } 40 41 // runtime variable to check if the processor we're running on 42 // actually supports the instructions used by the AES-based 43 // hash implementation. 44 var useAeshash bool 45 46 // in asm_*.s 47 func memhash(p unsafe.Pointer, h, s uintptr) uintptr 48 func memhash32(p unsafe.Pointer, h uintptr) uintptr 49 func memhash64(p unsafe.Pointer, h uintptr) uintptr 50 func strhash(p unsafe.Pointer, h uintptr) uintptr 51 52 func strhashFallback(a unsafe.Pointer, h uintptr) uintptr { 53 x := (*stringStruct)(a) 54 return memhashFallback(x.str, h, uintptr(x.len)) 55 } 56 57 // NOTE: Because NaN != NaN, a map can contain any 58 // number of (mostly useless) entries keyed with NaNs. 59 // To avoid long hash chains, we assign a random number 60 // as the hash value for a NaN. 61 62 func f32hash(p unsafe.Pointer, h uintptr) uintptr { 63 f := *(*float32)(p) 64 switch { 65 case f == 0: 66 return c1 * (c0 ^ h) // +0, -0 67 case f != f: 68 return c1 * (c0 ^ h ^ uintptr(fastrand())) // any kind of NaN 69 default: 70 return memhash(p, h, 4) 71 } 72 } 73 74 func f64hash(p unsafe.Pointer, h uintptr) uintptr { 75 f := *(*float64)(p) 76 switch { 77 case f == 0: 78 return c1 * (c0 ^ h) // +0, -0 79 case f != f: 80 return c1 * (c0 ^ h ^ uintptr(fastrand())) // any kind of NaN 81 default: 82 return memhash(p, h, 8) 83 } 84 } 85 86 func c64hash(p unsafe.Pointer, h uintptr) uintptr { 87 x := (*[2]float32)(p) 88 return f32hash(unsafe.Pointer(&x[1]), f32hash(unsafe.Pointer(&x[0]), h)) 89 } 90 91 func c128hash(p unsafe.Pointer, h uintptr) uintptr { 92 x := (*[2]float64)(p) 93 return f64hash(unsafe.Pointer(&x[1]), f64hash(unsafe.Pointer(&x[0]), h)) 94 } 95 96 func interhash(p unsafe.Pointer, h uintptr) uintptr { 97 a := (*iface)(p) 98 tab := a.tab 99 if tab == nil { 100 return h 101 } 102 t := tab._type 103 if t.equal == nil { 104 // Check hashability here. We could do this check inside 105 // typehash, but we want to report the topmost type in 106 // the error text (e.g. in a struct with a field of slice type 107 // we want to report the struct, not the slice). 108 panic(errorString("hash of unhashable type " + t.string())) 109 } 110 if isDirectIface(t) { 111 return c1 * typehash(t, unsafe.Pointer(&a.data), h^c0) 112 } else { 113 return c1 * typehash(t, a.data, h^c0) 114 } 115 } 116 117 func nilinterhash(p unsafe.Pointer, h uintptr) uintptr { 118 a := (*eface)(p) 119 t := a._type 120 if t == nil { 121 return h 122 } 123 if t.equal == nil { 124 // See comment in interhash above. 125 panic(errorString("hash of unhashable type " + t.string())) 126 } 127 if isDirectIface(t) { 128 return c1 * typehash(t, unsafe.Pointer(&a.data), h^c0) 129 } else { 130 return c1 * typehash(t, a.data, h^c0) 131 } 132 } 133 134 // typehash computes the hash of the object of type t at address p. 135 // h is the seed. 136 // This function is seldom used. Most maps use for hashing either 137 // fixed functions (e.g. f32hash) or compiler-generated functions 138 // (e.g. for a type like struct { x, y string }). This implementation 139 // is slower but more general and is used for hashing interface types 140 // (called from interhash or nilinterhash, above) or for hashing in 141 // maps generated by reflect.MapOf (reflect_typehash, below). 142 // Note: this function must match the compiler generated 143 // functions exactly. See issue 37716. 144 func typehash(t *_type, p unsafe.Pointer, h uintptr) uintptr { 145 if t.tflag&tflagRegularMemory != 0 { 146 // Handle ptr sizes specially, see issue 37086. 147 switch t.size { 148 case 4: 149 return memhash32(p, h) 150 case 8: 151 return memhash64(p, h) 152 default: 153 return memhash(p, h, t.size) 154 } 155 } 156 switch t.kind & kindMask { 157 case kindFloat32: 158 return f32hash(p, h) 159 case kindFloat64: 160 return f64hash(p, h) 161 case kindComplex64: 162 return c64hash(p, h) 163 case kindComplex128: 164 return c128hash(p, h) 165 case kindString: 166 return strhash(p, h) 167 case kindInterface: 168 i := (*interfacetype)(unsafe.Pointer(t)) 169 if len(i.mhdr) == 0 { 170 return nilinterhash(p, h) 171 } 172 return interhash(p, h) 173 case kindArray: 174 a := (*arraytype)(unsafe.Pointer(t)) 175 for i := uintptr(0); i < a.len; i++ { 176 h = typehash(a.elem, add(p, i*a.elem.size), h) 177 } 178 return h 179 case kindStruct: 180 s := (*structtype)(unsafe.Pointer(t)) 181 memStart := uintptr(0) 182 memEnd := uintptr(0) 183 for _, f := range s.fields { 184 if memEnd > memStart && (f.name.isBlank() || f.offset() != memEnd || f.typ.tflag&tflagRegularMemory == 0) { 185 // flush any pending regular memory hashing 186 h = memhash(add(p, memStart), h, memEnd-memStart) 187 memStart = memEnd 188 } 189 if f.name.isBlank() { 190 continue 191 } 192 if f.typ.tflag&tflagRegularMemory == 0 { 193 h = typehash(f.typ, add(p, f.offset()), h) 194 continue 195 } 196 if memStart == memEnd { 197 memStart = f.offset() 198 } 199 memEnd = f.offset() + f.typ.size 200 } 201 if memEnd > memStart { 202 h = memhash(add(p, memStart), h, memEnd-memStart) 203 } 204 return h 205 default: 206 // Should never happen, as typehash should only be called 207 // with comparable types. 208 panic(errorString("hash of unhashable type " + t.string())) 209 } 210 } 211 212 //go:linkname reflect_typehash reflect.typehash 213 func reflect_typehash(t *_type, p unsafe.Pointer, h uintptr) uintptr { 214 return typehash(t, p, h) 215 } 216 217 func memequal0(p, q unsafe.Pointer) bool { 218 return true 219 } 220 func memequal8(p, q unsafe.Pointer) bool { 221 return *(*int8)(p) == *(*int8)(q) 222 } 223 func memequal16(p, q unsafe.Pointer) bool { 224 return *(*int16)(p) == *(*int16)(q) 225 } 226 func memequal32(p, q unsafe.Pointer) bool { 227 return *(*int32)(p) == *(*int32)(q) 228 } 229 func memequal64(p, q unsafe.Pointer) bool { 230 return *(*int64)(p) == *(*int64)(q) 231 } 232 func memequal128(p, q unsafe.Pointer) bool { 233 return *(*[2]int64)(p) == *(*[2]int64)(q) 234 } 235 func f32equal(p, q unsafe.Pointer) bool { 236 return *(*float32)(p) == *(*float32)(q) 237 } 238 func f64equal(p, q unsafe.Pointer) bool { 239 return *(*float64)(p) == *(*float64)(q) 240 } 241 func c64equal(p, q unsafe.Pointer) bool { 242 return *(*complex64)(p) == *(*complex64)(q) 243 } 244 func c128equal(p, q unsafe.Pointer) bool { 245 return *(*complex128)(p) == *(*complex128)(q) 246 } 247 func strequal(p, q unsafe.Pointer) bool { 248 return *(*string)(p) == *(*string)(q) 249 } 250 func interequal(p, q unsafe.Pointer) bool { 251 x := *(*iface)(p) 252 y := *(*iface)(q) 253 return x.tab == y.tab && ifaceeq(x.tab, x.data, y.data) 254 } 255 func nilinterequal(p, q unsafe.Pointer) bool { 256 x := *(*eface)(p) 257 y := *(*eface)(q) 258 return x._type == y._type && efaceeq(x._type, x.data, y.data) 259 } 260 func efaceeq(t *_type, x, y unsafe.Pointer) bool { 261 if t == nil { 262 return true 263 } 264 eq := t.equal 265 if eq == nil { 266 panic(errorString("comparing uncomparable type " + t.string())) 267 } 268 if isDirectIface(t) { 269 // Direct interface types are ptr, chan, map, func, and single-element structs/arrays thereof. 270 // Maps and funcs are not comparable, so they can't reach here. 271 // Ptrs, chans, and single-element items can be compared directly using ==. 272 return x == y 273 } 274 return eq(x, y) 275 } 276 func ifaceeq(tab *itab, x, y unsafe.Pointer) bool { 277 if tab == nil { 278 return true 279 } 280 t := tab._type 281 eq := t.equal 282 if eq == nil { 283 panic(errorString("comparing uncomparable type " + t.string())) 284 } 285 if isDirectIface(t) { 286 // See comment in efaceeq. 287 return x == y 288 } 289 return eq(x, y) 290 } 291 292 // Testing adapters for hash quality tests (see hash_test.go) 293 func stringHash(s string, seed uintptr) uintptr { 294 return strhash(noescape(unsafe.Pointer(&s)), seed) 295 } 296 297 func bytesHash(b []byte, seed uintptr) uintptr { 298 s := (*slice)(unsafe.Pointer(&b)) 299 return memhash(s.array, seed, uintptr(s.len)) 300 } 301 302 func int32Hash(i uint32, seed uintptr) uintptr { 303 return memhash32(noescape(unsafe.Pointer(&i)), seed) 304 } 305 306 func int64Hash(i uint64, seed uintptr) uintptr { 307 return memhash64(noescape(unsafe.Pointer(&i)), seed) 308 } 309 310 func efaceHash(i interface{}, seed uintptr) uintptr { 311 return nilinterhash(noescape(unsafe.Pointer(&i)), seed) 312 } 313 314 func ifaceHash(i interface { 315 F() 316 }, seed uintptr) uintptr { 317 return interhash(noescape(unsafe.Pointer(&i)), seed) 318 } 319 320 const hashRandomBytes = sys.PtrSize / 4 * 64 321 322 // used in asm_{386,amd64,arm64}.s to seed the hash function 323 var aeskeysched [hashRandomBytes]byte 324 325 // used in hash{32,64}.go to seed the hash function 326 var hashkey [4]uintptr 327 328 func alginit() { 329 // Install AES hash algorithms if the instructions needed are present. 330 if (GOARCH == "386" || GOARCH == "amd64") && 331 cpu.X86.HasAES && // AESENC 332 cpu.X86.HasSSSE3 && // PSHUFB 333 cpu.X86.HasSSE41 { // PINSR{D,Q} 334 initAlgAES() 335 return 336 } 337 if GOARCH == "arm64" && cpu.ARM64.HasAES { 338 initAlgAES() 339 return 340 } 341 getRandomData((*[len(hashkey) * sys.PtrSize]byte)(unsafe.Pointer(&hashkey))[:]) 342 hashkey[0] |= 1 // make sure these numbers are odd 343 hashkey[1] |= 1 344 hashkey[2] |= 1 345 hashkey[3] |= 1 346 } 347 348 func initAlgAES() { 349 useAeshash = true 350 // Initialize with random data so hash collisions will be hard to engineer. 351 getRandomData(aeskeysched[:]) 352 } 353 354 // Note: These routines perform the read with a native endianness. 355 func readUnaligned32(p unsafe.Pointer) uint32 { 356 q := (*[4]byte)(p) 357 if sys.BigEndian { 358 return uint32(q[3]) | uint32(q[2])<<8 | uint32(q[1])<<16 | uint32(q[0])<<24 359 } 360 return uint32(q[0]) | uint32(q[1])<<8 | uint32(q[2])<<16 | uint32(q[3])<<24 361 } 362 363 func readUnaligned64(p unsafe.Pointer) uint64 { 364 q := (*[8]byte)(p) 365 if sys.BigEndian { 366 return uint64(q[7]) | uint64(q[6])<<8 | uint64(q[5])<<16 | uint64(q[4])<<24 | 367 uint64(q[3])<<32 | uint64(q[2])<<40 | uint64(q[1])<<48 | uint64(q[0])<<56 368 } 369 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 370 }