github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/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  // memhash_varlen is defined in assembly because it needs access
    67  // to the closure. It appears here to provide an argument
    68  // signature for the assembly routine.
    69  func memhash_varlen(p unsafe.Pointer, h uintptr) uintptr
    70  
    71  var algarray = [alg_max]typeAlg{
    72  	alg_NOEQ:     {nil, nil},
    73  	alg_MEM0:     {memhash0, memequal0},
    74  	alg_MEM8:     {memhash8, memequal8},
    75  	alg_MEM16:    {memhash16, memequal16},
    76  	alg_MEM32:    {memhash32, memequal32},
    77  	alg_MEM64:    {memhash64, memequal64},
    78  	alg_MEM128:   {memhash128, memequal128},
    79  	alg_STRING:   {strhash, strequal},
    80  	alg_INTER:    {interhash, interequal},
    81  	alg_NILINTER: {nilinterhash, nilinterequal},
    82  	alg_FLOAT32:  {f32hash, f32equal},
    83  	alg_FLOAT64:  {f64hash, f64equal},
    84  	alg_CPLX64:   {c64hash, c64equal},
    85  	alg_CPLX128:  {c128hash, c128equal},
    86  }
    87  
    88  var useAeshash bool
    89  
    90  // in asm_*.s
    91  func aeshash(p unsafe.Pointer, h, s uintptr) uintptr
    92  func aeshash32(p unsafe.Pointer, h uintptr) uintptr
    93  func aeshash64(p unsafe.Pointer, h uintptr) uintptr
    94  func aeshashstr(p unsafe.Pointer, h uintptr) uintptr
    95  
    96  func strhash(a unsafe.Pointer, h uintptr) uintptr {
    97  	x := (*stringStruct)(a)
    98  	return memhash(x.str, h, uintptr(x.len))
    99  }
   100  
   101  // NOTE: Because NaN != NaN, a map can contain any
   102  // number of (mostly useless) entries keyed with NaNs.
   103  // To avoid long hash chains, we assign a random number
   104  // as the hash value for a NaN.
   105  
   106  func f32hash(p unsafe.Pointer, h uintptr) uintptr {
   107  	f := *(*float32)(p)
   108  	switch {
   109  	case f == 0:
   110  		return c1 * (c0 ^ h) // +0, -0
   111  	case f != f:
   112  		return c1 * (c0 ^ h ^ uintptr(fastrand())) // any kind of NaN
   113  	default:
   114  		return memhash(p, h, 4)
   115  	}
   116  }
   117  
   118  func f64hash(p unsafe.Pointer, h uintptr) uintptr {
   119  	f := *(*float64)(p)
   120  	switch {
   121  	case f == 0:
   122  		return c1 * (c0 ^ h) // +0, -0
   123  	case f != f:
   124  		return c1 * (c0 ^ h ^ uintptr(fastrand())) // any kind of NaN
   125  	default:
   126  		return memhash(p, h, 8)
   127  	}
   128  }
   129  
   130  func c64hash(p unsafe.Pointer, h uintptr) uintptr {
   131  	x := (*[2]float32)(p)
   132  	return f32hash(unsafe.Pointer(&x[1]), f32hash(unsafe.Pointer(&x[0]), h))
   133  }
   134  
   135  func c128hash(p unsafe.Pointer, h uintptr) uintptr {
   136  	x := (*[2]float64)(p)
   137  	return f64hash(unsafe.Pointer(&x[1]), f64hash(unsafe.Pointer(&x[0]), h))
   138  }
   139  
   140  func interhash(p unsafe.Pointer, h uintptr) uintptr {
   141  	a := (*iface)(p)
   142  	tab := a.tab
   143  	if tab == nil {
   144  		return h
   145  	}
   146  	t := tab._type
   147  	fn := t.alg.hash
   148  	if fn == nil {
   149  		panic(errorString("hash of unhashable type " + t.string()))
   150  	}
   151  	if isDirectIface(t) {
   152  		return c1 * fn(unsafe.Pointer(&a.data), h^c0)
   153  	} else {
   154  		return c1 * fn(a.data, h^c0)
   155  	}
   156  }
   157  
   158  func nilinterhash(p unsafe.Pointer, h uintptr) uintptr {
   159  	a := (*eface)(p)
   160  	t := a._type
   161  	if t == nil {
   162  		return h
   163  	}
   164  	fn := t.alg.hash
   165  	if fn == nil {
   166  		panic(errorString("hash of unhashable type " + t.string()))
   167  	}
   168  	if isDirectIface(t) {
   169  		return c1 * fn(unsafe.Pointer(&a.data), h^c0)
   170  	} else {
   171  		return c1 * fn(a.data, h^c0)
   172  	}
   173  }
   174  
   175  func memequal0(p, q unsafe.Pointer) bool {
   176  	return true
   177  }
   178  func memequal8(p, q unsafe.Pointer) bool {
   179  	return *(*int8)(p) == *(*int8)(q)
   180  }
   181  func memequal16(p, q unsafe.Pointer) bool {
   182  	return *(*int16)(p) == *(*int16)(q)
   183  }
   184  func memequal32(p, q unsafe.Pointer) bool {
   185  	return *(*int32)(p) == *(*int32)(q)
   186  }
   187  func memequal64(p, q unsafe.Pointer) bool {
   188  	return *(*int64)(p) == *(*int64)(q)
   189  }
   190  func memequal128(p, q unsafe.Pointer) bool {
   191  	return *(*[2]int64)(p) == *(*[2]int64)(q)
   192  }
   193  func f32equal(p, q unsafe.Pointer) bool {
   194  	return *(*float32)(p) == *(*float32)(q)
   195  }
   196  func f64equal(p, q unsafe.Pointer) bool {
   197  	return *(*float64)(p) == *(*float64)(q)
   198  }
   199  func c64equal(p, q unsafe.Pointer) bool {
   200  	return *(*complex64)(p) == *(*complex64)(q)
   201  }
   202  func c128equal(p, q unsafe.Pointer) bool {
   203  	return *(*complex128)(p) == *(*complex128)(q)
   204  }
   205  func strequal(p, q unsafe.Pointer) bool {
   206  	return *(*string)(p) == *(*string)(q)
   207  }
   208  func interequal(p, q unsafe.Pointer) bool {
   209  	x := *(*iface)(p)
   210  	y := *(*iface)(q)
   211  	return x.tab == y.tab && ifaceeq(x.tab, x.data, y.data)
   212  }
   213  func nilinterequal(p, q unsafe.Pointer) bool {
   214  	x := *(*eface)(p)
   215  	y := *(*eface)(q)
   216  	return x._type == y._type && efaceeq(x._type, x.data, y.data)
   217  }
   218  func efaceeq(t *_type, x, y unsafe.Pointer) bool {
   219  	if t == nil {
   220  		return true
   221  	}
   222  	eq := t.alg.equal
   223  	if eq == nil {
   224  		panic(errorString("comparing uncomparable type " + t.string()))
   225  	}
   226  	if isDirectIface(t) {
   227  		return eq(noescape(unsafe.Pointer(&x)), noescape(unsafe.Pointer(&y)))
   228  	}
   229  	return eq(x, y)
   230  }
   231  func ifaceeq(tab *itab, x, y unsafe.Pointer) bool {
   232  	if tab == nil {
   233  		return true
   234  	}
   235  	t := tab._type
   236  	eq := t.alg.equal
   237  	if eq == nil {
   238  		panic(errorString("comparing uncomparable type " + t.string()))
   239  	}
   240  	if isDirectIface(t) {
   241  		return eq(noescape(unsafe.Pointer(&x)), noescape(unsafe.Pointer(&y)))
   242  	}
   243  	return eq(x, y)
   244  }
   245  
   246  // Testing adapters for hash quality tests (see hash_test.go)
   247  func stringHash(s string, seed uintptr) uintptr {
   248  	return algarray[alg_STRING].hash(noescape(unsafe.Pointer(&s)), seed)
   249  }
   250  
   251  func bytesHash(b []byte, seed uintptr) uintptr {
   252  	s := (*slice)(unsafe.Pointer(&b))
   253  	return memhash(s.array, seed, uintptr(s.len))
   254  }
   255  
   256  func int32Hash(i uint32, seed uintptr) uintptr {
   257  	return algarray[alg_MEM32].hash(noescape(unsafe.Pointer(&i)), seed)
   258  }
   259  
   260  func int64Hash(i uint64, seed uintptr) uintptr {
   261  	return algarray[alg_MEM64].hash(noescape(unsafe.Pointer(&i)), seed)
   262  }
   263  
   264  func efaceHash(i interface{}, seed uintptr) uintptr {
   265  	return algarray[alg_NILINTER].hash(noescape(unsafe.Pointer(&i)), seed)
   266  }
   267  
   268  func ifaceHash(i interface {
   269  	F()
   270  }, seed uintptr) uintptr {
   271  	return algarray[alg_INTER].hash(noescape(unsafe.Pointer(&i)), seed)
   272  }
   273  
   274  const hashRandomBytes = sys.PtrSize / 4 * 64
   275  
   276  // used in asm_{386,amd64}.s to seed the hash function
   277  var aeskeysched [hashRandomBytes]byte
   278  
   279  // used in hash{32,64}.go to seed the hash function
   280  var hashkey [4]uintptr
   281  
   282  func alginit() {
   283  	// Install aes hash algorithm if we have the instructions we need
   284  	if (GOARCH == "386" || GOARCH == "amd64") &&
   285  		GOOS != "nacl" &&
   286  		cpuid_ecx&(1<<25) != 0 && // aes (aesenc)
   287  		cpuid_ecx&(1<<9) != 0 && // sse3 (pshufb)
   288  		cpuid_ecx&(1<<19) != 0 { // sse4.1 (pinsr{d,q})
   289  		useAeshash = true
   290  		algarray[alg_MEM32].hash = aeshash32
   291  		algarray[alg_MEM64].hash = aeshash64
   292  		algarray[alg_STRING].hash = aeshashstr
   293  		// Initialize with random data so hash collisions will be hard to engineer.
   294  		getRandomData(aeskeysched[:])
   295  		return
   296  	}
   297  	getRandomData((*[len(hashkey) * sys.PtrSize]byte)(unsafe.Pointer(&hashkey))[:])
   298  	hashkey[0] |= 1 // make sure these numbers are odd
   299  	hashkey[1] |= 1
   300  	hashkey[2] |= 1
   301  	hashkey[3] |= 1
   302  }