Source file src/runtime/alg.go

     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/abi"
     9  	"internal/cpu"
    10  	"internal/goarch"
    11  	"internal/runtime/sys"
    12  	"unsafe"
    13  )
    14  
    15  const (
    16  	c0 = uintptr((8-goarch.PtrSize)/4*2860486313 + (goarch.PtrSize-4)/4*33054211828000289)
    17  	c1 = uintptr((8-goarch.PtrSize)/4*3267000013 + (goarch.PtrSize-4)/4*23344194077549503)
    18  )
    19  
    20  func memhash0(p unsafe.Pointer, h uintptr) uintptr {
    21  	return h
    22  }
    23  
    24  func memhash8(p unsafe.Pointer, h uintptr) uintptr {
    25  	return memhash(p, h, 1)
    26  }
    27  
    28  func memhash16(p unsafe.Pointer, h uintptr) uintptr {
    29  	return memhash(p, h, 2)
    30  }
    31  
    32  func memhash128(p unsafe.Pointer, h uintptr) uintptr {
    33  	return memhash(p, h, 16)
    34  }
    35  
    36  //go:nosplit
    37  func memhash_varlen(p unsafe.Pointer, h uintptr) uintptr {
    38  	ptr := sys.GetClosurePtr()
    39  	size := *(*uintptr)(unsafe.Pointer(ptr + unsafe.Sizeof(h)))
    40  	return memhash(p, h, size)
    41  }
    42  
    43  // runtime variable to check if the processor we're running on
    44  // actually supports the instructions used by the AES-based
    45  // hash implementation.
    46  var useAeshash bool
    47  
    48  // in asm_*.s
    49  
    50  // memhash should be an internal detail,
    51  // but widely used packages access it using linkname.
    52  // Notable members of the hall of shame include:
    53  //   - github.com/aacfactory/fns
    54  //   - github.com/dgraph-io/ristretto
    55  //   - github.com/minio/simdjson-go
    56  //   - github.com/nbd-wtf/go-nostr
    57  //   - github.com/outcaste-io/ristretto
    58  //   - github.com/puzpuzpuz/xsync/v2
    59  //   - github.com/puzpuzpuz/xsync/v3
    60  //   - github.com/authzed/spicedb
    61  //   - github.com/pingcap/badger
    62  //
    63  // Do not remove or change the type signature.
    64  // See go.dev/issue/67401.
    65  //
    66  //go:linkname memhash
    67  func memhash(p unsafe.Pointer, h, s uintptr) uintptr
    68  
    69  func memhash32(p unsafe.Pointer, h uintptr) uintptr
    70  
    71  func memhash64(p unsafe.Pointer, h uintptr) uintptr
    72  
    73  // strhash should be an internal detail,
    74  // but widely used packages access it using linkname.
    75  // Notable members of the hall of shame include:
    76  //   - github.com/aristanetworks/goarista
    77  //   - github.com/bytedance/sonic
    78  //   - github.com/bytedance/go-tagexpr/v2
    79  //   - github.com/cloudwego/dynamicgo
    80  //   - github.com/v2fly/v2ray-core/v5
    81  //
    82  // Do not remove or change the type signature.
    83  // See go.dev/issue/67401.
    84  //
    85  //go:linkname strhash
    86  func strhash(p unsafe.Pointer, h uintptr) uintptr
    87  
    88  func strhashFallback(a unsafe.Pointer, h uintptr) uintptr {
    89  	x := (*stringStruct)(a)
    90  	return memhashFallback(x.str, h, uintptr(x.len))
    91  }
    92  
    93  // NOTE: Because NaN != NaN, a map can contain any
    94  // number of (mostly useless) entries keyed with NaNs.
    95  // To avoid long hash chains, we assign a random number
    96  // as the hash value for a NaN.
    97  
    98  func f32hash(p unsafe.Pointer, h uintptr) uintptr {
    99  	f := *(*float32)(p)
   100  	switch {
   101  	case f == 0:
   102  		return c1 * (c0 ^ h) // +0, -0
   103  	case f != f:
   104  		return c1 * (c0 ^ h ^ uintptr(rand())) // any kind of NaN
   105  	default:
   106  		return memhash(p, h, 4)
   107  	}
   108  }
   109  
   110  func f64hash(p unsafe.Pointer, h uintptr) uintptr {
   111  	f := *(*float64)(p)
   112  	switch {
   113  	case f == 0:
   114  		return c1 * (c0 ^ h) // +0, -0
   115  	case f != f:
   116  		return c1 * (c0 ^ h ^ uintptr(rand())) // any kind of NaN
   117  	default:
   118  		return memhash(p, h, 8)
   119  	}
   120  }
   121  
   122  func c64hash(p unsafe.Pointer, h uintptr) uintptr {
   123  	x := (*[2]float32)(p)
   124  	return f32hash(unsafe.Pointer(&x[1]), f32hash(unsafe.Pointer(&x[0]), h))
   125  }
   126  
   127  func c128hash(p unsafe.Pointer, h uintptr) uintptr {
   128  	x := (*[2]float64)(p)
   129  	return f64hash(unsafe.Pointer(&x[1]), f64hash(unsafe.Pointer(&x[0]), h))
   130  }
   131  
   132  func interhash(p unsafe.Pointer, h uintptr) uintptr {
   133  	a := (*iface)(p)
   134  	tab := a.tab
   135  	if tab == nil {
   136  		return h
   137  	}
   138  	t := tab.Type
   139  	if t.Equal == nil {
   140  		// Check hashability here. We could do this check inside
   141  		// typehash, but we want to report the topmost type in
   142  		// the error text (e.g. in a struct with a field of slice type
   143  		// we want to report the struct, not the slice).
   144  		panic(errorString("hash of unhashable type " + toRType(t).string()))
   145  	}
   146  	if isDirectIface(t) {
   147  		return c1 * typehash(t, unsafe.Pointer(&a.data), h^c0)
   148  	} else {
   149  		return c1 * typehash(t, a.data, h^c0)
   150  	}
   151  }
   152  
   153  // nilinterhash should be an internal detail,
   154  // but widely used packages access it using linkname.
   155  // Notable members of the hall of shame include:
   156  //   - github.com/anacrolix/stm
   157  //   - github.com/aristanetworks/goarista
   158  //
   159  // Do not remove or change the type signature.
   160  // See go.dev/issue/67401.
   161  //
   162  //go:linkname nilinterhash
   163  func nilinterhash(p unsafe.Pointer, h uintptr) uintptr {
   164  	a := (*eface)(p)
   165  	t := a._type
   166  	if t == nil {
   167  		return h
   168  	}
   169  	if t.Equal == nil {
   170  		// See comment in interhash above.
   171  		panic(errorString("hash of unhashable type " + toRType(t).string()))
   172  	}
   173  	if isDirectIface(t) {
   174  		return c1 * typehash(t, unsafe.Pointer(&a.data), h^c0)
   175  	} else {
   176  		return c1 * typehash(t, a.data, h^c0)
   177  	}
   178  }
   179  
   180  // typehash computes the hash of the object of type t at address p.
   181  // h is the seed.
   182  // This function is seldom used. Most maps use for hashing either
   183  // fixed functions (e.g. f32hash) or compiler-generated functions
   184  // (e.g. for a type like struct { x, y string }). This implementation
   185  // is slower but more general and is used for hashing interface types
   186  // (called from interhash or nilinterhash, above) or for hashing in
   187  // maps generated by reflect.MapOf (reflect_typehash, below).
   188  // Note: this function must match the compiler generated
   189  // functions exactly. See issue 37716.
   190  //
   191  // typehash should be an internal detail,
   192  // but widely used packages access it using linkname.
   193  // Notable members of the hall of shame include:
   194  //   - github.com/puzpuzpuz/xsync/v2
   195  //   - github.com/puzpuzpuz/xsync/v3
   196  //
   197  // Do not remove or change the type signature.
   198  // See go.dev/issue/67401.
   199  //
   200  //go:linkname typehash
   201  func typehash(t *_type, p unsafe.Pointer, h uintptr) uintptr {
   202  	if t.TFlag&abi.TFlagRegularMemory != 0 {
   203  		// Handle ptr sizes specially, see issue 37086.
   204  		switch t.Size_ {
   205  		case 4:
   206  			return memhash32(p, h)
   207  		case 8:
   208  			return memhash64(p, h)
   209  		default:
   210  			return memhash(p, h, t.Size_)
   211  		}
   212  	}
   213  	switch t.Kind_ & abi.KindMask {
   214  	case abi.Float32:
   215  		return f32hash(p, h)
   216  	case abi.Float64:
   217  		return f64hash(p, h)
   218  	case abi.Complex64:
   219  		return c64hash(p, h)
   220  	case abi.Complex128:
   221  		return c128hash(p, h)
   222  	case abi.String:
   223  		return strhash(p, h)
   224  	case abi.Interface:
   225  		i := (*interfacetype)(unsafe.Pointer(t))
   226  		if len(i.Methods) == 0 {
   227  			return nilinterhash(p, h)
   228  		}
   229  		return interhash(p, h)
   230  	case abi.Array:
   231  		a := (*arraytype)(unsafe.Pointer(t))
   232  		for i := uintptr(0); i < a.Len; i++ {
   233  			h = typehash(a.Elem, add(p, i*a.Elem.Size_), h)
   234  		}
   235  		return h
   236  	case abi.Struct:
   237  		s := (*structtype)(unsafe.Pointer(t))
   238  		for _, f := range s.Fields {
   239  			if f.Name.IsBlank() {
   240  				continue
   241  			}
   242  			h = typehash(f.Typ, add(p, f.Offset), h)
   243  		}
   244  		return h
   245  	default:
   246  		// Should never happen, as typehash should only be called
   247  		// with comparable types.
   248  		panic(errorString("hash of unhashable type " + toRType(t).string()))
   249  	}
   250  }
   251  
   252  func mapKeyError(t *maptype, p unsafe.Pointer) error {
   253  	if !t.HashMightPanic() {
   254  		return nil
   255  	}
   256  	return mapKeyError2(t.Key, p)
   257  }
   258  
   259  func mapKeyError2(t *_type, p unsafe.Pointer) error {
   260  	if t.TFlag&abi.TFlagRegularMemory != 0 {
   261  		return nil
   262  	}
   263  	switch t.Kind_ & abi.KindMask {
   264  	case abi.Float32, abi.Float64, abi.Complex64, abi.Complex128, abi.String:
   265  		return nil
   266  	case abi.Interface:
   267  		i := (*interfacetype)(unsafe.Pointer(t))
   268  		var t *_type
   269  		var pdata *unsafe.Pointer
   270  		if len(i.Methods) == 0 {
   271  			a := (*eface)(p)
   272  			t = a._type
   273  			if t == nil {
   274  				return nil
   275  			}
   276  			pdata = &a.data
   277  		} else {
   278  			a := (*iface)(p)
   279  			if a.tab == nil {
   280  				return nil
   281  			}
   282  			t = a.tab.Type
   283  			pdata = &a.data
   284  		}
   285  
   286  		if t.Equal == nil {
   287  			return errorString("hash of unhashable type " + toRType(t).string())
   288  		}
   289  
   290  		if isDirectIface(t) {
   291  			return mapKeyError2(t, unsafe.Pointer(pdata))
   292  		} else {
   293  			return mapKeyError2(t, *pdata)
   294  		}
   295  	case abi.Array:
   296  		a := (*arraytype)(unsafe.Pointer(t))
   297  		for i := uintptr(0); i < a.Len; i++ {
   298  			if err := mapKeyError2(a.Elem, add(p, i*a.Elem.Size_)); err != nil {
   299  				return err
   300  			}
   301  		}
   302  		return nil
   303  	case abi.Struct:
   304  		s := (*structtype)(unsafe.Pointer(t))
   305  		for _, f := range s.Fields {
   306  			if f.Name.IsBlank() {
   307  				continue
   308  			}
   309  			if err := mapKeyError2(f.Typ, add(p, f.Offset)); err != nil {
   310  				return err
   311  			}
   312  		}
   313  		return nil
   314  	default:
   315  		// Should never happen, keep this case for robustness.
   316  		return errorString("hash of unhashable type " + toRType(t).string())
   317  	}
   318  }
   319  
   320  //go:linkname reflect_typehash reflect.typehash
   321  func reflect_typehash(t *_type, p unsafe.Pointer, h uintptr) uintptr {
   322  	return typehash(t, p, h)
   323  }
   324  
   325  func memequal0(p, q unsafe.Pointer) bool {
   326  	return true
   327  }
   328  func memequal8(p, q unsafe.Pointer) bool {
   329  	return *(*int8)(p) == *(*int8)(q)
   330  }
   331  func memequal16(p, q unsafe.Pointer) bool {
   332  	return *(*int16)(p) == *(*int16)(q)
   333  }
   334  func memequal32(p, q unsafe.Pointer) bool {
   335  	return *(*int32)(p) == *(*int32)(q)
   336  }
   337  func memequal64(p, q unsafe.Pointer) bool {
   338  	return *(*int64)(p) == *(*int64)(q)
   339  }
   340  func memequal128(p, q unsafe.Pointer) bool {
   341  	return *(*[2]int64)(p) == *(*[2]int64)(q)
   342  }
   343  func f32equal(p, q unsafe.Pointer) bool {
   344  	return *(*float32)(p) == *(*float32)(q)
   345  }
   346  func f64equal(p, q unsafe.Pointer) bool {
   347  	return *(*float64)(p) == *(*float64)(q)
   348  }
   349  func c64equal(p, q unsafe.Pointer) bool {
   350  	return *(*complex64)(p) == *(*complex64)(q)
   351  }
   352  func c128equal(p, q unsafe.Pointer) bool {
   353  	return *(*complex128)(p) == *(*complex128)(q)
   354  }
   355  func strequal(p, q unsafe.Pointer) bool {
   356  	return *(*string)(p) == *(*string)(q)
   357  }
   358  func interequal(p, q unsafe.Pointer) bool {
   359  	x := *(*iface)(p)
   360  	y := *(*iface)(q)
   361  	return x.tab == y.tab && ifaceeq(x.tab, x.data, y.data)
   362  }
   363  func nilinterequal(p, q unsafe.Pointer) bool {
   364  	x := *(*eface)(p)
   365  	y := *(*eface)(q)
   366  	return x._type == y._type && efaceeq(x._type, x.data, y.data)
   367  }
   368  func efaceeq(t *_type, x, y unsafe.Pointer) bool {
   369  	if t == nil {
   370  		return true
   371  	}
   372  	eq := t.Equal
   373  	if eq == nil {
   374  		panic(errorString("comparing uncomparable type " + toRType(t).string()))
   375  	}
   376  	if isDirectIface(t) {
   377  		// Direct interface types are ptr, chan, map, func, and single-element structs/arrays thereof.
   378  		// Maps and funcs are not comparable, so they can't reach here.
   379  		// Ptrs, chans, and single-element items can be compared directly using ==.
   380  		return x == y
   381  	}
   382  	return eq(x, y)
   383  }
   384  func ifaceeq(tab *itab, x, y unsafe.Pointer) bool {
   385  	if tab == nil {
   386  		return true
   387  	}
   388  	t := tab.Type
   389  	eq := t.Equal
   390  	if eq == nil {
   391  		panic(errorString("comparing uncomparable type " + toRType(t).string()))
   392  	}
   393  	if isDirectIface(t) {
   394  		// See comment in efaceeq.
   395  		return x == y
   396  	}
   397  	return eq(x, y)
   398  }
   399  
   400  // Testing adapters for hash quality tests (see hash_test.go)
   401  //
   402  // stringHash should be an internal detail,
   403  // but widely used packages access it using linkname.
   404  // Notable members of the hall of shame include:
   405  //   - github.com/k14s/starlark-go
   406  //
   407  // Do not remove or change the type signature.
   408  // See go.dev/issue/67401.
   409  //
   410  //go:linkname stringHash
   411  func stringHash(s string, seed uintptr) uintptr {
   412  	return strhash(noescape(unsafe.Pointer(&s)), seed)
   413  }
   414  
   415  func bytesHash(b []byte, seed uintptr) uintptr {
   416  	s := (*slice)(unsafe.Pointer(&b))
   417  	return memhash(s.array, seed, uintptr(s.len))
   418  }
   419  
   420  func int32Hash(i uint32, seed uintptr) uintptr {
   421  	return memhash32(noescape(unsafe.Pointer(&i)), seed)
   422  }
   423  
   424  func int64Hash(i uint64, seed uintptr) uintptr {
   425  	return memhash64(noescape(unsafe.Pointer(&i)), seed)
   426  }
   427  
   428  func efaceHash(i any, seed uintptr) uintptr {
   429  	return nilinterhash(noescape(unsafe.Pointer(&i)), seed)
   430  }
   431  
   432  func ifaceHash(i interface {
   433  	F()
   434  }, seed uintptr) uintptr {
   435  	return interhash(noescape(unsafe.Pointer(&i)), seed)
   436  }
   437  
   438  const hashRandomBytes = goarch.PtrSize / 4 * 64
   439  
   440  // used in asm_{386,amd64,arm64}.s to seed the hash function
   441  var aeskeysched [hashRandomBytes]byte
   442  
   443  // used in hash{32,64}.go to seed the hash function
   444  var hashkey [4]uintptr
   445  
   446  func alginit() {
   447  	// Install AES hash algorithms if the instructions needed are present.
   448  	if (GOARCH == "386" || GOARCH == "amd64") &&
   449  		cpu.X86.HasAES && // AESENC
   450  		cpu.X86.HasSSSE3 && // PSHUFB
   451  		cpu.X86.HasSSE41 { // PINSR{D,Q}
   452  		initAlgAES()
   453  		return
   454  	}
   455  	if GOARCH == "arm64" && cpu.ARM64.HasAES {
   456  		initAlgAES()
   457  		return
   458  	}
   459  	for i := range hashkey {
   460  		hashkey[i] = uintptr(bootstrapRand())
   461  	}
   462  }
   463  
   464  func initAlgAES() {
   465  	useAeshash = true
   466  	// Initialize with random data so hash collisions will be hard to engineer.
   467  	key := (*[hashRandomBytes / 8]uint64)(unsafe.Pointer(&aeskeysched))
   468  	for i := range key {
   469  		key[i] = bootstrapRand()
   470  	}
   471  }
   472  
   473  // Note: These routines perform the read with a native endianness.
   474  func readUnaligned32(p unsafe.Pointer) uint32 {
   475  	q := (*[4]byte)(p)
   476  	if goarch.BigEndian {
   477  		return uint32(q[3]) | uint32(q[2])<<8 | uint32(q[1])<<16 | uint32(q[0])<<24
   478  	}
   479  	return uint32(q[0]) | uint32(q[1])<<8 | uint32(q[2])<<16 | uint32(q[3])<<24
   480  }
   481  
   482  func readUnaligned64(p unsafe.Pointer) uint64 {
   483  	q := (*[8]byte)(p)
   484  	if goarch.BigEndian {
   485  		return uint64(q[7]) | uint64(q[6])<<8 | uint64(q[5])<<16 | uint64(q[4])<<24 |
   486  			uint64(q[3])<<32 | uint64(q[2])<<40 | uint64(q[1])<<48 | uint64(q[0])<<56
   487  	}
   488  	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
   489  }
   490  

View as plain text