// 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 ( unsafe.Pointer,  uintptr) uintptr {
	return 
}

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

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

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

//go:nosplit
func ( 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 ( unsafe.Pointer, ,  uintptr) uintptr
func ( unsafe.Pointer,  uintptr) uintptr
func ( unsafe.Pointer,  uintptr) uintptr
func ( unsafe.Pointer,  uintptr) uintptr

func ( 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 ( unsafe.Pointer,  uintptr) uintptr {
	 := *(*float32)()
	switch {
	case  == 0:
		return c1 * (c0 ^ ) // +0, -0
	case  != :
		return c1 * (c0 ^  ^ uintptr(fastrand())) // any kind of NaN
	default:
		return memhash(, , 4)
	}
}

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

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

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

func ( 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 ( 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 ( *_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()))
	}
}

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

func (,  unsafe.Pointer) bool {
	return true
}
func (,  unsafe.Pointer) bool {
	return *(*int8)() == *(*int8)()
}
func (,  unsafe.Pointer) bool {
	return *(*int16)() == *(*int16)()
}
func (,  unsafe.Pointer) bool {
	return *(*int32)() == *(*int32)()
}
func (,  unsafe.Pointer) bool {
	return *(*int64)() == *(*int64)()
}
func (,  unsafe.Pointer) bool {
	return *(*[2]int64)() == *(*[2]int64)()
}
func (,  unsafe.Pointer) bool {
	return *(*float32)() == *(*float32)()
}
func (,  unsafe.Pointer) bool {
	return *(*float64)() == *(*float64)()
}
func (,  unsafe.Pointer) bool {
	return *(*complex64)() == *(*complex64)()
}
func (,  unsafe.Pointer) bool {
	return *(*complex128)() == *(*complex128)()
}
func (,  unsafe.Pointer) bool {
	return *(*string)() == *(*string)()
}
func (,  unsafe.Pointer) bool {
	 := *(*iface)()
	 := *(*iface)()
	return .tab == .tab && ifaceeq(.tab, .data, .data)
}
func (,  unsafe.Pointer) bool {
	 := *(*eface)()
	 := *(*eface)()
	return ._type == ._type && efaceeq(._type, .data, .data)
}
func ( *_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 ( *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 ( string,  uintptr) uintptr {
	return strhash(noescape(unsafe.Pointer(&)), )
}

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

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

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

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

func ( 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 () {
	// 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
	}
	getRandomData((*[len(hashkey) * goarch.PtrSize]byte)(unsafe.Pointer(&hashkey))[:])
	hashkey[0] |= 1 // make sure these numbers are odd
	hashkey[1] |= 1
	hashkey[2] |= 1
	hashkey[3] |= 1
}

func () {
	useAeshash = true
	// Initialize with random data so hash collisions will be hard to engineer.
	getRandomData(aeskeysched[:])
}

// Note: These routines perform the read with a native endianness.
func ( 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 ( 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
}