// 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

import (
	
	
	
	
)

const (
	c0 = uintptr((8-goarch.PtrSize)/4*2860486313 + (goarch.PtrSize-4)/4*33054211828000289)
	c1 = uintptr((8-goarch.PtrSize)/4*3267000013 + (goarch.PtrSize-4)/4*23344194077549503)
)

func memhash0( unsafe.Pointer,  uintptr) uintptr {
	return 
}

func memhash8( unsafe.Pointer,  uintptr) uintptr {
	return memhash(, , 1)
}

func memhash16( unsafe.Pointer,  uintptr) uintptr {
	return memhash(, , 2)
}

func memhash128( unsafe.Pointer,  uintptr) uintptr {
	return memhash(, , 16)
}

//go:nosplit
func memhash_varlen( unsafe.Pointer,  uintptr) uintptr {
	 := getclosureptr()
	 := *(*uintptr)(unsafe.Pointer( + unsafe.Sizeof()))
	return memhash(, , )
}

// runtime variable to check if the processor we're running on
// actually supports the instructions used by the AES-based
// hash implementation.
var useAeshash bool

// in asm_*.s
func memhash( unsafe.Pointer, ,  uintptr) uintptr
func memhash32( unsafe.Pointer,  uintptr) uintptr
func memhash64( unsafe.Pointer,  uintptr) uintptr
func strhash( unsafe.Pointer,  uintptr) uintptr

func strhashFallback( unsafe.Pointer,  uintptr) uintptr {
	 := (*stringStruct)()
	return memhashFallback(.str, , uintptr(.len))
}

// 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.

func f32hash( unsafe.Pointer,  uintptr) uintptr {
	 := *(*float32)()
	switch {
	case  == 0:
		return c1 * (c0 ^ ) // +0, -0
	case  != :
		return c1 * (c0 ^  ^ uintptr(rand())) // any kind of NaN
	default:
		return memhash(, , 4)
	}
}

func f64hash( unsafe.Pointer,  uintptr) uintptr {
	 := *(*float64)()
	switch {
	case  == 0:
		return c1 * (c0 ^ ) // +0, -0
	case  != :
		return c1 * (c0 ^  ^ uintptr(rand())) // any kind of NaN
	default:
		return memhash(, , 8)
	}
}

func c64hash( unsafe.Pointer,  uintptr) uintptr {
	 := (*[2]float32)()
	return f32hash(unsafe.Pointer(&[1]), f32hash(unsafe.Pointer(&[0]), ))
}

func c128hash( unsafe.Pointer,  uintptr) uintptr {
	 := (*[2]float64)()
	return f64hash(unsafe.Pointer(&[1]), f64hash(unsafe.Pointer(&[0]), ))
}

func interhash( unsafe.Pointer,  uintptr) uintptr {
	 := (*iface)()
	 := .tab
	if  == nil {
		return 
	}
	 := ._type
	if .Equal == nil {
		// Check hashability here. We could do this check inside
		// typehash, but we want to report the topmost type in
		// the error text (e.g. in a struct with a field of slice type
		// we want to report the struct, not the slice).
		panic(errorString("hash of unhashable type " + toRType().string()))
	}
	if isDirectIface() {
		return c1 * typehash(, unsafe.Pointer(&.data), ^c0)
	} else {
		return c1 * typehash(, .data, ^c0)
	}
}

func nilinterhash( unsafe.Pointer,  uintptr) uintptr {
	 := (*eface)()
	 := ._type
	if  == nil {
		return 
	}
	if .Equal == nil {
		// See comment in interhash above.
		panic(errorString("hash of unhashable type " + toRType().string()))
	}
	if isDirectIface() {
		return c1 * typehash(, unsafe.Pointer(&.data), ^c0)
	} else {
		return c1 * typehash(, .data, ^c0)
	}
}

// typehash computes the hash of the object of type t at address p.
// h is the seed.
// This function is seldom used. Most maps use for hashing either
// fixed functions (e.g. f32hash) or compiler-generated functions
// (e.g. for a type like struct { x, y string }). This implementation
// is slower but more general and is used for hashing interface types
// (called from interhash or nilinterhash, above) or for hashing in
// maps generated by reflect.MapOf (reflect_typehash, below).
// Note: this function must match the compiler generated
// functions exactly. See issue 37716.
func typehash( *_type,  unsafe.Pointer,  uintptr) uintptr {
	if .TFlag&abi.TFlagRegularMemory != 0 {
		// Handle ptr sizes specially, see issue 37086.
		switch .Size_ {
		case 4:
			return memhash32(, )
		case 8:
			return memhash64(, )
		default:
			return memhash(, , .Size_)
		}
	}
	switch .Kind_ & kindMask {
	case kindFloat32:
		return f32hash(, )
	case kindFloat64:
		return f64hash(, )
	case kindComplex64:
		return c64hash(, )
	case kindComplex128:
		return c128hash(, )
	case kindString:
		return strhash(, )
	case kindInterface:
		 := (*interfacetype)(unsafe.Pointer())
		if len(.Methods) == 0 {
			return nilinterhash(, )
		}
		return interhash(, )
	case kindArray:
		 := (*arraytype)(unsafe.Pointer())
		for  := uintptr(0);  < .Len; ++ {
			 = (.Elem, add(, *.Elem.Size_), )
		}
		return 
	case kindStruct:
		 := (*structtype)(unsafe.Pointer())
		for ,  := range .Fields {
			if .Name.IsBlank() {
				continue
			}
			 = (.Typ, add(, .Offset), )
		}
		return 
	default:
		// Should never happen, as typehash should only be called
		// with comparable types.
		panic(errorString("hash of unhashable type " + toRType().string()))
	}
}

func mapKeyError( *maptype,  unsafe.Pointer) error {
	if !.HashMightPanic() {
		return nil
	}
	return mapKeyError2(.Key, )
}

func mapKeyError2( *_type,  unsafe.Pointer) error {
	if .TFlag&abi.TFlagRegularMemory != 0 {
		return nil
	}
	switch .Kind_ & kindMask {
	case kindFloat32, kindFloat64, kindComplex64, kindComplex128, kindString:
		return nil
	case kindInterface:
		 := (*interfacetype)(unsafe.Pointer())
		var  *_type
		var  *unsafe.Pointer
		if len(.Methods) == 0 {
			 := (*eface)()
			 = ._type
			if  == nil {
				return nil
			}
			 = &.data
		} else {
			 := (*iface)()
			if .tab == nil {
				return nil
			}
			 = .tab._type
			 = &.data
		}

		if .Equal == nil {
			return errorString("hash of unhashable type " + toRType().string())
		}

		if isDirectIface() {
			return (, unsafe.Pointer())
		} else {
			return (, *)
		}
	case kindArray:
		 := (*arraytype)(unsafe.Pointer())
		for  := uintptr(0);  < .Len; ++ {
			if  := (.Elem, add(, *.Elem.Size_));  != nil {
				return 
			}
		}
		return nil
	case kindStruct:
		 := (*structtype)(unsafe.Pointer())
		for ,  := range .Fields {
			if .Name.IsBlank() {
				continue
			}
			if  := (.Typ, add(, .Offset));  != nil {
				return 
			}
		}
		return nil
	default:
		// Should never happen, keep this case for robustness.
		return errorString("hash of unhashable type " + toRType().string())
	}
}

//go:linkname reflect_typehash reflect.typehash
func reflect_typehash( *_type,  unsafe.Pointer,  uintptr) uintptr {
	return typehash(, , )
}

func memequal0(,  unsafe.Pointer) bool {
	return true
}
func memequal8(,  unsafe.Pointer) bool {
	return *(*int8)() == *(*int8)()
}
func memequal16(,  unsafe.Pointer) bool {
	return *(*int16)() == *(*int16)()
}
func memequal32(,  unsafe.Pointer) bool {
	return *(*int32)() == *(*int32)()
}
func memequal64(,  unsafe.Pointer) bool {
	return *(*int64)() == *(*int64)()
}
func memequal128(,  unsafe.Pointer) bool {
	return *(*[2]int64)() == *(*[2]int64)()
}
func f32equal(,  unsafe.Pointer) bool {
	return *(*float32)() == *(*float32)()
}
func f64equal(,  unsafe.Pointer) bool {
	return *(*float64)() == *(*float64)()
}
func c64equal(,  unsafe.Pointer) bool {
	return *(*complex64)() == *(*complex64)()
}
func c128equal(,  unsafe.Pointer) bool {
	return *(*complex128)() == *(*complex128)()
}
func strequal(,  unsafe.Pointer) bool {
	return *(*string)() == *(*string)()
}
func interequal(,  unsafe.Pointer) bool {
	 := *(*iface)()
	 := *(*iface)()
	return .tab == .tab && ifaceeq(.tab, .data, .data)
}
func nilinterequal(,  unsafe.Pointer) bool {
	 := *(*eface)()
	 := *(*eface)()
	return ._type == ._type && efaceeq(._type, .data, .data)
}
func efaceeq( *_type, ,  unsafe.Pointer) bool {
	if  == nil {
		return true
	}
	 := .Equal
	if  == nil {
		panic(errorString("comparing uncomparable type " + toRType().string()))
	}
	if isDirectIface() {
		// Direct interface types are ptr, chan, map, func, and single-element structs/arrays thereof.
		// Maps and funcs are not comparable, so they can't reach here.
		// Ptrs, chans, and single-element items can be compared directly using ==.
		return  == 
	}
	return (, )
}
func ifaceeq( *itab, ,  unsafe.Pointer) bool {
	if  == nil {
		return true
	}
	 := ._type
	 := .Equal
	if  == nil {
		panic(errorString("comparing uncomparable type " + toRType().string()))
	}
	if isDirectIface() {
		// See comment in efaceeq.
		return  == 
	}
	return (, )
}

// Testing adapters for hash quality tests (see hash_test.go)
func stringHash( string,  uintptr) uintptr {
	return strhash(noescape(unsafe.Pointer(&)), )
}

func bytesHash( []byte,  uintptr) uintptr {
	 := (*slice)(unsafe.Pointer(&))
	return memhash(.array, , uintptr(.len))
}

func int32Hash( uint32,  uintptr) uintptr {
	return memhash32(noescape(unsafe.Pointer(&)), )
}

func int64Hash( uint64,  uintptr) uintptr {
	return memhash64(noescape(unsafe.Pointer(&)), )
}

func efaceHash( any,  uintptr) uintptr {
	return nilinterhash(noescape(unsafe.Pointer(&)), )
}

func ifaceHash( interface {
	()
},  uintptr) uintptr {
	return interhash(noescape(unsafe.Pointer(&)), )
}

const hashRandomBytes = goarch.PtrSize / 4 * 64

// used in asm_{386,amd64,arm64}.s to seed the hash function
var aeskeysched [hashRandomBytes]byte

// used in hash{32,64}.go to seed the hash function
var hashkey [4]uintptr

func alginit() {
	// Install AES hash algorithms if the instructions needed are present.
	if (GOARCH == "386" || GOARCH == "amd64") &&
		cpu.X86.HasAES && // AESENC
		cpu.X86.HasSSSE3 && // PSHUFB
		cpu.X86.HasSSE41 { // PINSR{D,Q}
		initAlgAES()
		return
	}
	if GOARCH == "arm64" && cpu.ARM64.HasAES {
		initAlgAES()
		return
	}
	for  := range hashkey {
		hashkey[] = uintptr(rand()) | 1 // make sure these numbers are odd
	}
}

func initAlgAES() {
	useAeshash = true
	// Initialize with random data so hash collisions will be hard to engineer.
	 := (*[hashRandomBytes / 8]uint64)(unsafe.Pointer(&aeskeysched))
	for  := range  {
		[] = bootstrapRand()
	}
}

// Note: These routines perform the read with a native endianness.
func readUnaligned32( unsafe.Pointer) uint32 {
	 := (*[4]byte)()
	if goarch.BigEndian {
		return uint32([3]) | uint32([2])<<8 | uint32([1])<<16 | uint32([0])<<24
	}
	return uint32([0]) | uint32([1])<<8 | uint32([2])<<16 | uint32([3])<<24
}

func readUnaligned64( unsafe.Pointer) uint64 {
	 := (*[8]byte)()
	if goarch.BigEndian {
		return uint64([7]) | uint64([6])<<8 | uint64([5])<<16 | uint64([4])<<24 |
			uint64([3])<<32 | uint64([2])<<40 | uint64([1])<<48 | uint64([0])<<56
	}
	return uint64([0]) | uint64([1])<<8 | uint64([2])<<16 | uint64([3])<<24 | uint64([4])<<32 | uint64([5])<<40 | uint64([6])<<48 | uint64([7])<<56
}