github.com/karrick/go@v0.0.0-20170817181416-d5b0ec858b37/src/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  	"runtime/internal/sys"
     9  	"unsafe"
    10  )
    11  
    12  const (
    13  	c0 = uintptr((8-sys.PtrSize)/4*2860486313 + (sys.PtrSize-4)/4*33054211828000289)
    14  	c1 = uintptr((8-sys.PtrSize)/4*3267000013 + (sys.PtrSize-4)/4*23344194077549503)
    15  )
    16  
    17  // type algorithms - known to compiler
    18  const (
    19  	alg_NOEQ = iota
    20  	alg_MEM0
    21  	alg_MEM8
    22  	alg_MEM16
    23  	alg_MEM32
    24  	alg_MEM64
    25  	alg_MEM128
    26  	alg_STRING
    27  	alg_INTER
    28  	alg_NILINTER
    29  	alg_FLOAT32
    30  	alg_FLOAT64
    31  	alg_CPLX64
    32  	alg_CPLX128
    33  	alg_max
    34  )
    35  
    36  // typeAlg is also copied/used in reflect/type.go.
    37  // keep them in sync.
    38  type typeAlg struct {
    39  	// function for hashing objects of this type
    40  	// (ptr to object, seed) -> hash
    41  	hash func(unsafe.Pointer, uintptr) uintptr
    42  	// function for comparing objects of this type
    43  	// (ptr to object A, ptr to object B) -> ==?
    44  	equal func(unsafe.Pointer, unsafe.Pointer) bool
    45  }
    46  
    47  func memhash0(p unsafe.Pointer, h uintptr) uintptr {
    48  	return h
    49  }
    50  func memhash8(p unsafe.Pointer, h uintptr) uintptr {
    51  	return memhash(p, h, 1)
    52  }
    53  func memhash16(p unsafe.Pointer, h uintptr) uintptr {
    54  	return memhash(p, h, 2)
    55  }
    56  func memhash32(p unsafe.Pointer, h uintptr) uintptr {
    57  	return memhash(p, h, 4)
    58  }
    59  func memhash64(p unsafe.Pointer, h uintptr) uintptr {
    60  	return memhash(p, h, 8)
    61  }
    62  func memhash128(p unsafe.Pointer, h uintptr) uintptr {
    63  	return memhash(p, h, 16)
    64  }
    65  
    66  //go:nosplit
    67  func memhash_varlen(p unsafe.Pointer, h uintptr) uintptr {
    68  	ptr := getclosureptr()
    69  	size := *(*uintptr)(unsafe.Pointer(ptr + unsafe.Sizeof(h)))
    70  	return memhash(p, h, size)
    71  }
    72  
    73  var algarray = [alg_max]typeAlg{
    74  	alg_NOEQ:     {nil, nil},
    75  	alg_MEM0:     {memhash0, memequal0},
    76  	alg_MEM8:     {memhash8, memequal8},
    77  	alg_MEM16:    {memhash16, memequal16},
    78  	alg_MEM32:    {memhash32, memequal32},
    79  	alg_MEM64:    {memhash64, memequal64},
    80  	alg_MEM128:   {memhash128, memequal128},
    81  	alg_STRING:   {strhash, strequal},
    82  	alg_INTER:    {interhash, interequal},
    83  	alg_NILINTER: {nilinterhash, nilinterequal},
    84  	alg_FLOAT32:  {f32hash, f32equal},
    85  	alg_FLOAT64:  {f64hash, f64equal},
    86  	alg_CPLX64:   {c64hash, c64equal},
    87  	alg_CPLX128:  {c128hash, c128equal},
    88  }
    89  
    90  var useAeshash bool
    91  
    92  // in asm_*.s
    93  func aeshash(p unsafe.Pointer, h, s uintptr) uintptr
    94  func aeshash32(p unsafe.Pointer, h uintptr) uintptr
    95  func aeshash64(p unsafe.Pointer, h uintptr) uintptr
    96  func aeshashstr(p unsafe.Pointer, h uintptr) uintptr
    97  
    98  func strhash(a unsafe.Pointer, h uintptr) uintptr {
    99  	x := (*stringStruct)(a)
   100  	return memhash(x.str, h, uintptr(x.len))
   101  }
   102  
   103  // NOTE: Because NaN != NaN, a map can contain any
   104  // number of (mostly useless) entries keyed with NaNs.
   105  // To avoid long hash chains, we assign a random number
   106  // as the hash value for a NaN.
   107  
   108  func f32hash(p unsafe.Pointer, h uintptr) uintptr {
   109  	f := *(*float32)(p)
   110  	switch {
   111  	case f == 0:
   112  		return c1 * (c0 ^ h) // +0, -0
   113  	case f != f:
   114  		return c1 * (c0 ^ h ^ uintptr(fastrand())) // any kind of NaN
   115  	default:
   116  		return memhash(p, h, 4)
   117  	}
   118  }
   119  
   120  func f64hash(p unsafe.Pointer, h uintptr) uintptr {
   121  	f := *(*float64)(p)
   122  	switch {
   123  	case f == 0:
   124  		return c1 * (c0 ^ h) // +0, -0
   125  	case f != f:
   126  		return c1 * (c0 ^ h ^ uintptr(fastrand())) // any kind of NaN
   127  	default:
   128  		return memhash(p, h, 8)
   129  	}
   130  }
   131  
   132  func c64hash(p unsafe.Pointer, h uintptr) uintptr {
   133  	x := (*[2]float32)(p)
   134  	return f32hash(unsafe.Pointer(&x[1]), f32hash(unsafe.Pointer(&x[0]), h))
   135  }
   136  
   137  func c128hash(p unsafe.Pointer, h uintptr) uintptr {
   138  	x := (*[2]float64)(p)
   139  	return f64hash(unsafe.Pointer(&x[1]), f64hash(unsafe.Pointer(&x[0]), h))
   140  }
   141  
   142  func interhash(p unsafe.Pointer, h uintptr) uintptr {
   143  	a := (*iface)(p)
   144  	tab := a.tab
   145  	if tab == nil {
   146  		return h
   147  	}
   148  	t := tab._type
   149  	fn := t.alg.hash
   150  	if fn == nil {
   151  		panic(errorString("hash of unhashable type " + t.string()))
   152  	}
   153  	if isDirectIface(t) {
   154  		return c1 * fn(unsafe.Pointer(&a.data), h^c0)
   155  	} else {
   156  		return c1 * fn(a.data, h^c0)
   157  	}
   158  }
   159  
   160  func nilinterhash(p unsafe.Pointer, h uintptr) uintptr {
   161  	a := (*eface)(p)
   162  	t := a._type
   163  	if t == nil {
   164  		return h
   165  	}
   166  	fn := t.alg.hash
   167  	if fn == nil {
   168  		panic(errorString("hash of unhashable type " + t.string()))
   169  	}
   170  	if isDirectIface(t) {
   171  		return c1 * fn(unsafe.Pointer(&a.data), h^c0)
   172  	} else {
   173  		return c1 * fn(a.data, h^c0)
   174  	}
   175  }
   176  
   177  func memequal0(p, q unsafe.Pointer) bool {
   178  	return true
   179  }
   180  func memequal8(p, q unsafe.Pointer) bool {
   181  	return *(*int8)(p) == *(*int8)(q)
   182  }
   183  func memequal16(p, q unsafe.Pointer) bool {
   184  	return *(*int16)(p) == *(*int16)(q)
   185  }
   186  func memequal32(p, q unsafe.Pointer) bool {
   187  	return *(*int32)(p) == *(*int32)(q)
   188  }
   189  func memequal64(p, q unsafe.Pointer) bool {
   190  	return *(*int64)(p) == *(*int64)(q)
   191  }
   192  func memequal128(p, q unsafe.Pointer) bool {
   193  	return *(*[2]int64)(p) == *(*[2]int64)(q)
   194  }
   195  func f32equal(p, q unsafe.Pointer) bool {
   196  	return *(*float32)(p) == *(*float32)(q)
   197  }
   198  func f64equal(p, q unsafe.Pointer) bool {
   199  	return *(*float64)(p) == *(*float64)(q)
   200  }
   201  func c64equal(p, q unsafe.Pointer) bool {
   202  	return *(*complex64)(p) == *(*complex64)(q)
   203  }
   204  func c128equal(p, q unsafe.Pointer) bool {
   205  	return *(*complex128)(p) == *(*complex128)(q)
   206  }
   207  func strequal(p, q unsafe.Pointer) bool {
   208  	return *(*string)(p) == *(*string)(q)
   209  }
   210  func interequal(p, q unsafe.Pointer) bool {
   211  	x := *(*iface)(p)
   212  	y := *(*iface)(q)
   213  	return x.tab == y.tab && ifaceeq(x.tab, x.data, y.data)
   214  }
   215  func nilinterequal(p, q unsafe.Pointer) bool {
   216  	x := *(*eface)(p)
   217  	y := *(*eface)(q)
   218  	return x._type == y._type && efaceeq(x._type, x.data, y.data)
   219  }
   220  func efaceeq(t *_type, x, y unsafe.Pointer) bool {
   221  	if t == nil {
   222  		return true
   223  	}
   224  	eq := t.alg.equal
   225  	if eq == nil {
   226  		panic(errorString("comparing uncomparable type " + t.string()))
   227  	}
   228  	if isDirectIface(t) {
   229  		return eq(noescape(unsafe.Pointer(&x)), noescape(unsafe.Pointer(&y)))
   230  	}
   231  	return eq(x, y)
   232  }
   233  func ifaceeq(tab *itab, x, y unsafe.Pointer) bool {
   234  	if tab == nil {
   235  		return true
   236  	}
   237  	t := tab._type
   238  	eq := t.alg.equal
   239  	if eq == nil {
   240  		panic(errorString("comparing uncomparable type " + t.string()))
   241  	}
   242  	if isDirectIface(t) {
   243  		return eq(noescape(unsafe.Pointer(&x)), noescape(unsafe.Pointer(&y)))
   244  	}
   245  	return eq(x, y)
   246  }
   247  
   248  // Testing adapters for hash quality tests (see hash_test.go)
   249  func stringHash(s string, seed uintptr) uintptr {
   250  	return algarray[alg_STRING].hash(noescape(unsafe.Pointer(&s)), seed)
   251  }
   252  
   253  func bytesHash(b []byte, seed uintptr) uintptr {
   254  	s := (*slice)(unsafe.Pointer(&b))
   255  	return memhash(s.array, seed, uintptr(s.len))
   256  }
   257  
   258  func int32Hash(i uint32, seed uintptr) uintptr {
   259  	return algarray[alg_MEM32].hash(noescape(unsafe.Pointer(&i)), seed)
   260  }
   261  
   262  func int64Hash(i uint64, seed uintptr) uintptr {
   263  	return algarray[alg_MEM64].hash(noescape(unsafe.Pointer(&i)), seed)
   264  }
   265  
   266  func efaceHash(i interface{}, seed uintptr) uintptr {
   267  	return algarray[alg_NILINTER].hash(noescape(unsafe.Pointer(&i)), seed)
   268  }
   269  
   270  func ifaceHash(i interface {
   271  	F()
   272  }, seed uintptr) uintptr {
   273  	return algarray[alg_INTER].hash(noescape(unsafe.Pointer(&i)), seed)
   274  }
   275  
   276  const hashRandomBytes = sys.PtrSize / 4 * 64
   277  
   278  // used in asm_{386,amd64}.s to seed the hash function
   279  var aeskeysched [hashRandomBytes]byte
   280  
   281  // used in hash{32,64}.go to seed the hash function
   282  var hashkey [4]uintptr
   283  
   284  func alginit() {
   285  	// Install aes hash algorithm if we have the instructions we need
   286  	if (GOARCH == "386" || GOARCH == "amd64") &&
   287  		GOOS != "nacl" &&
   288  		support_aes && // AESENC
   289  		support_ssse3 && // PSHUFB
   290  		support_sse41 { // PINSR{D,Q}
   291  		useAeshash = true
   292  		algarray[alg_MEM32].hash = aeshash32
   293  		algarray[alg_MEM64].hash = aeshash64
   294  		algarray[alg_STRING].hash = aeshashstr
   295  		// Initialize with random data so hash collisions will be hard to engineer.
   296  		getRandomData(aeskeysched[:])
   297  		return
   298  	}
   299  	getRandomData((*[len(hashkey) * sys.PtrSize]byte)(unsafe.Pointer(&hashkey))[:])
   300  	hashkey[0] |= 1 // make sure these numbers are odd
   301  	hashkey[1] |= 1
   302  	hashkey[2] |= 1
   303  	hashkey[3] |= 1
   304  }