| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | // Copyright 2014 The Go Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package runtime | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-11 12:39:30 -05:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"runtime/internal/sys" | 
					
						
							|  |  |  | 	"unsafe" | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2015-11-11 12:39:30 -05:00
										 |  |  | 	c0 = uintptr((8-sys.PtrSize)/4*2860486313 + (sys.PtrSize-4)/4*33054211828000289) | 
					
						
							|  |  |  | 	c1 = uintptr((8-sys.PtrSize)/4*3267000013 + (sys.PtrSize-4)/4*23344194077549503) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | // type algorithms - known to compiler | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	alg_MEM = iota | 
					
						
							|  |  |  | 	alg_MEM0 | 
					
						
							|  |  |  | 	alg_MEM8 | 
					
						
							|  |  |  | 	alg_MEM16 | 
					
						
							|  |  |  | 	alg_MEM32 | 
					
						
							|  |  |  | 	alg_MEM64 | 
					
						
							|  |  |  | 	alg_MEM128 | 
					
						
							|  |  |  | 	alg_NOEQ | 
					
						
							|  |  |  | 	alg_NOEQ0 | 
					
						
							|  |  |  | 	alg_NOEQ8 | 
					
						
							|  |  |  | 	alg_NOEQ16 | 
					
						
							|  |  |  | 	alg_NOEQ32 | 
					
						
							|  |  |  | 	alg_NOEQ64 | 
					
						
							|  |  |  | 	alg_NOEQ128 | 
					
						
							|  |  |  | 	alg_STRING | 
					
						
							|  |  |  | 	alg_INTER | 
					
						
							|  |  |  | 	alg_NILINTER | 
					
						
							|  |  |  | 	alg_SLICE | 
					
						
							|  |  |  | 	alg_FLOAT32 | 
					
						
							|  |  |  | 	alg_FLOAT64 | 
					
						
							|  |  |  | 	alg_CPLX64 | 
					
						
							|  |  |  | 	alg_CPLX128 | 
					
						
							|  |  |  | 	alg_max | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-27 10:04:11 +01:00
										 |  |  | // typeAlg is also copied/used in reflect/type.go. | 
					
						
							|  |  |  | // keep them in sync. | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | type typeAlg struct { | 
					
						
							|  |  |  | 	// function for hashing objects of this type | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	// (ptr to object, seed) -> hash | 
					
						
							|  |  |  | 	hash func(unsafe.Pointer, uintptr) uintptr | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	// function for comparing objects of this type | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	// (ptr to object A, ptr to object B) -> ==? | 
					
						
							|  |  |  | 	equal func(unsafe.Pointer, unsafe.Pointer) bool | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func memhash0(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							|  |  |  | 	return h | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func memhash8(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							|  |  |  | 	return memhash(p, h, 1) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func memhash16(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							|  |  |  | 	return memhash(p, h, 2) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func memhash32(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							|  |  |  | 	return memhash(p, h, 4) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func memhash64(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							|  |  |  | 	return memhash(p, h, 8) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func memhash128(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							|  |  |  | 	return memhash(p, h, 16) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // memhash_varlen is defined in assembly because it needs access | 
					
						
							|  |  |  | // to the closure.  It appears here to provide an argument | 
					
						
							|  |  |  | // signature for the assembly routine. | 
					
						
							|  |  |  | func memhash_varlen(p unsafe.Pointer, h uintptr) uintptr | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | var algarray = [alg_max]typeAlg{ | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	alg_MEM:      {nil, nil}, // not used | 
					
						
							|  |  |  | 	alg_MEM0:     {memhash0, memequal0}, | 
					
						
							|  |  |  | 	alg_MEM8:     {memhash8, memequal8}, | 
					
						
							|  |  |  | 	alg_MEM16:    {memhash16, memequal16}, | 
					
						
							|  |  |  | 	alg_MEM32:    {memhash32, memequal32}, | 
					
						
							|  |  |  | 	alg_MEM64:    {memhash64, memequal64}, | 
					
						
							|  |  |  | 	alg_MEM128:   {memhash128, memequal128}, | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	alg_NOEQ:     {nil, nil}, | 
					
						
							|  |  |  | 	alg_NOEQ0:    {nil, nil}, | 
					
						
							|  |  |  | 	alg_NOEQ8:    {nil, nil}, | 
					
						
							|  |  |  | 	alg_NOEQ16:   {nil, nil}, | 
					
						
							|  |  |  | 	alg_NOEQ32:   {nil, nil}, | 
					
						
							|  |  |  | 	alg_NOEQ64:   {nil, nil}, | 
					
						
							|  |  |  | 	alg_NOEQ128:  {nil, nil}, | 
					
						
							|  |  |  | 	alg_STRING:   {strhash, strequal}, | 
					
						
							|  |  |  | 	alg_INTER:    {interhash, interequal}, | 
					
						
							|  |  |  | 	alg_NILINTER: {nilinterhash, nilinterequal}, | 
					
						
							|  |  |  | 	alg_SLICE:    {nil, nil}, | 
					
						
							|  |  |  | 	alg_FLOAT32:  {f32hash, f32equal}, | 
					
						
							|  |  |  | 	alg_FLOAT64:  {f64hash, f64equal}, | 
					
						
							|  |  |  | 	alg_CPLX64:   {c64hash, c64equal}, | 
					
						
							|  |  |  | 	alg_CPLX128:  {c128hash, c128equal}, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var useAeshash bool | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // in asm_*.s | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func aeshash(p unsafe.Pointer, h, s uintptr) uintptr | 
					
						
							|  |  |  | func aeshash32(p unsafe.Pointer, h uintptr) uintptr | 
					
						
							|  |  |  | func aeshash64(p unsafe.Pointer, h uintptr) uintptr | 
					
						
							|  |  |  | func aeshashstr(p unsafe.Pointer, h uintptr) uintptr | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func strhash(a unsafe.Pointer, h uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2014-12-10 17:02:58 -08:00
										 |  |  | 	x := (*stringStruct)(a) | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	return memhash(x.str, h, uintptr(x.len)) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NOTE: Because NaN != NaN, a map can contain any | 
					
						
							|  |  |  | // number of (mostly useless) entries keyed with NaNs. | 
					
						
							|  |  |  | // To avoid long hash chains, we assign a random number | 
					
						
							|  |  |  | // as the hash value for a NaN. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func f32hash(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	f := *(*float32)(p) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 	switch { | 
					
						
							|  |  |  | 	case f == 0: | 
					
						
							|  |  |  | 		return c1 * (c0 ^ h) // +0, -0 | 
					
						
							|  |  |  | 	case f != f: | 
					
						
							| 
									
										
										
										
											2014-09-02 14:33:33 -07:00
										 |  |  | 		return c1 * (c0 ^ h ^ uintptr(fastrand1())) // any kind of NaN | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 		return memhash(p, h, 4) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func f64hash(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	f := *(*float64)(p) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 	switch { | 
					
						
							|  |  |  | 	case f == 0: | 
					
						
							|  |  |  | 		return c1 * (c0 ^ h) // +0, -0 | 
					
						
							|  |  |  | 	case f != f: | 
					
						
							| 
									
										
										
										
											2014-09-02 14:33:33 -07:00
										 |  |  | 		return c1 * (c0 ^ h ^ uintptr(fastrand1())) // any kind of NaN | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 		return memhash(p, h, 8) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func c64hash(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	x := (*[2]float32)(p) | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	return f32hash(unsafe.Pointer(&x[1]), f32hash(unsafe.Pointer(&x[0]), h)) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func c128hash(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	x := (*[2]float64)(p) | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	return f64hash(unsafe.Pointer(&x[1]), f64hash(unsafe.Pointer(&x[0]), h)) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func interhash(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	a := (*iface)(p) | 
					
						
							| 
									
										
										
										
											2014-08-06 13:42:00 -07:00
										 |  |  | 	tab := a.tab | 
					
						
							| 
									
										
										
										
											2014-08-06 16:22:52 -04:00
										 |  |  | 	if tab == nil { | 
					
						
							|  |  |  | 		return h | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	t := tab._type | 
					
						
							| 
									
										
										
										
											2014-12-27 20:32:11 -08:00
										 |  |  | 	fn := t.alg.hash | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	if fn == nil { | 
					
						
							| 
									
										
										
										
											2014-08-06 16:22:52 -04:00
										 |  |  | 		panic(errorString("hash of unhashable type " + *t._string)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-18 21:13:11 -04:00
										 |  |  | 	if isDirectIface(t) { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 		return c1 * fn(unsafe.Pointer(&a.data), h^c0) | 
					
						
							| 
									
										
										
										
											2014-08-06 16:22:52 -04:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 		return c1 * fn(a.data, h^c0) | 
					
						
							| 
									
										
										
										
											2014-08-06 16:22:52 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func nilinterhash(p unsafe.Pointer, h uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	a := (*eface)(p) | 
					
						
							| 
									
										
										
										
											2014-08-06 13:42:00 -07:00
										 |  |  | 	t := a._type | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 	if t == nil { | 
					
						
							|  |  |  | 		return h | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-27 20:32:11 -08:00
										 |  |  | 	fn := t.alg.hash | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	if fn == nil { | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 		panic(errorString("hash of unhashable type " + *t._string)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-18 21:13:11 -04:00
										 |  |  | 	if isDirectIface(t) { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 		return c1 * fn(unsafe.Pointer(&a.data), h^c0) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 		return c1 * fn(a.data, h^c0) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | func memequal(p, q unsafe.Pointer, size uintptr) bool { | 
					
						
							|  |  |  | 	if p == q { | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return memeq(p, q, size) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func memequal0(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func memequal8(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	return *(*int8)(p) == *(*int8)(q) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func memequal16(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	return *(*int16)(p) == *(*int16)(q) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func memequal32(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	return *(*int32)(p) == *(*int32)(q) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func memequal64(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	return *(*int64)(p) == *(*int64)(q) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func memequal128(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	return *(*[2]int64)(p) == *(*[2]int64)(q) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func f32equal(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	return *(*float32)(p) == *(*float32)(q) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func f64equal(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	return *(*float64)(p) == *(*float64)(q) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func c64equal(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	return *(*complex64)(p) == *(*complex64)(q) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func c128equal(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	return *(*complex128)(p) == *(*complex128)(q) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func strequal(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	return *(*string)(p) == *(*string)(q) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func interequal(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2015-10-21 12:12:25 -07:00
										 |  |  | 	return ifaceeq(*(*iface)(p), *(*iface)(q)) | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | func nilinterequal(p, q unsafe.Pointer) bool { | 
					
						
							| 
									
										
										
										
											2015-10-21 12:12:25 -07:00
										 |  |  | 	return efaceeq(*(*eface)(p), *(*eface)(q)) | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-10-21 12:12:25 -07:00
										 |  |  | func efaceeq(x, y eface) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	t := x._type | 
					
						
							|  |  |  | 	if t != y._type { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if t == nil { | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-27 20:32:11 -08:00
										 |  |  | 	eq := t.alg.equal | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	if eq == nil { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 		panic(errorString("comparing uncomparable type " + *t._string)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-18 21:13:11 -04:00
										 |  |  | 	if isDirectIface(t) { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 		return eq(noescape(unsafe.Pointer(&x.data)), noescape(unsafe.Pointer(&y.data))) | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	return eq(x.data, y.data) | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-10-21 12:12:25 -07:00
										 |  |  | func ifaceeq(x, y iface) bool { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	xtab := x.tab | 
					
						
							|  |  |  | 	if xtab != y.tab { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if xtab == nil { | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	t := xtab._type | 
					
						
							| 
									
										
										
										
											2014-12-27 20:32:11 -08:00
										 |  |  | 	eq := t.alg.equal | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	if eq == nil { | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 		panic(errorString("comparing uncomparable type " + *t._string)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-18 21:13:11 -04:00
										 |  |  | 	if isDirectIface(t) { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 		return eq(noescape(unsafe.Pointer(&x.data)), noescape(unsafe.Pointer(&y.data))) | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	return eq(x.data, y.data) | 
					
						
							| 
									
										
										
										
											2014-08-07 14:52:55 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | // Testing adapters for hash quality tests (see hash_test.go) | 
					
						
							|  |  |  | func stringHash(s string, seed uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	return algarray[alg_STRING].hash(noescape(unsafe.Pointer(&s)), seed) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func bytesHash(b []byte, seed uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2015-04-11 10:01:54 +12:00
										 |  |  | 	s := (*slice)(unsafe.Pointer(&b)) | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	return memhash(s.array, seed, uintptr(s.len)) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func int32Hash(i uint32, seed uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	return algarray[alg_MEM32].hash(noescape(unsafe.Pointer(&i)), seed) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func int64Hash(i uint64, seed uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	return algarray[alg_MEM64].hash(noescape(unsafe.Pointer(&i)), seed) | 
					
						
							| 
									
										
										
										
											2014-07-31 15:12:53 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-08-07 12:33:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func efaceHash(i interface{}, seed uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	return algarray[alg_NILINTER].hash(noescape(unsafe.Pointer(&i)), seed) | 
					
						
							| 
									
										
										
										
											2014-08-07 12:33:20 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ifaceHash(i interface { | 
					
						
							|  |  |  | 	F() | 
					
						
							|  |  |  | }, seed uintptr) uintptr { | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:48 -08:00
										 |  |  | 	return algarray[alg_INTER].hash(noescape(unsafe.Pointer(&i)), seed) | 
					
						
							| 
									
										
										
										
											2014-08-07 12:33:20 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-08-20 14:02:11 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Testing adapter for memclr | 
					
						
							|  |  |  | func memclrBytes(b []byte) { | 
					
						
							| 
									
										
										
										
											2015-04-11 10:01:54 +12:00
										 |  |  | 	s := (*slice)(unsafe.Pointer(&b)) | 
					
						
							| 
									
										
										
										
											2014-08-20 14:02:11 -07:00
										 |  |  | 	memclr(s.array, uintptr(s.len)) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-11 12:39:30 -05:00
										 |  |  | const hashRandomBytes = sys.PtrSize / 4 * 64 | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 09:06:44 -08:00
										 |  |  | // used in asm_{386,amd64}.s to seed the hash function | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | var aeskeysched [hashRandomBytes]byte | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 09:06:44 -08:00
										 |  |  | // used in hash{32,64}.go to seed the hash function | 
					
						
							|  |  |  | var hashkey [4]uintptr | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 09:06:44 -08:00
										 |  |  | func init() { | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	// Install aes hash algorithm if we have the instructions we need | 
					
						
							| 
									
										
										
										
											2015-01-06 09:06:44 -08:00
										 |  |  | 	if (GOARCH == "386" || GOARCH == "amd64") && | 
					
						
							|  |  |  | 		GOOS != "nacl" && | 
					
						
							|  |  |  | 		cpuid_ecx&(1<<25) != 0 && // aes (aesenc) | 
					
						
							|  |  |  | 		cpuid_ecx&(1<<9) != 0 && // sse3 (pshufb) | 
					
						
							|  |  |  | 		cpuid_ecx&(1<<19) != 0 { // sse4.1 (pinsr{d,q}) | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 		useAeshash = true | 
					
						
							|  |  |  | 		algarray[alg_MEM32].hash = aeshash32 | 
					
						
							|  |  |  | 		algarray[alg_MEM64].hash = aeshash64 | 
					
						
							|  |  |  | 		algarray[alg_STRING].hash = aeshashstr | 
					
						
							|  |  |  | 		// Initialize with random data so hash collisions will be hard to engineer. | 
					
						
							| 
									
										
										
										
											2014-12-09 14:40:40 -08:00
										 |  |  | 		getRandomData(aeskeysched[:]) | 
					
						
							| 
									
										
										
										
											2015-01-06 09:06:44 -08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-11 12:39:30 -05:00
										 |  |  | 	getRandomData((*[len(hashkey) * sys.PtrSize]byte)(unsafe.Pointer(&hashkey))[:]) | 
					
						
							| 
									
										
										
										
											2015-08-31 16:26:12 -07:00
										 |  |  | 	hashkey[0] |= 1 // make sure these numbers are odd | 
					
						
							|  |  |  | 	hashkey[1] |= 1 | 
					
						
							|  |  |  | 	hashkey[2] |= 1 | 
					
						
							|  |  |  | 	hashkey[3] |= 1 | 
					
						
							| 
									
										
										
										
											2014-08-30 08:40:56 +04:00
										 |  |  | } |