// 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 (
	// We use 32-bit hash on Wasm, see hash32.go.
	hashSize = (1-goarch.IsWasm)*goarch.PtrSize + goarch.IsWasm*4
	c0       = uintptr((8-hashSize)/4*2860486313 + (hashSize-4)/4*33054211828000289)
	c1       = uintptr((8-hashSize)/4*3267000013 + (hashSize-4)/4*23344194077549503)
)

func trimHash( uintptr) uintptr {
	if goarch.IsWasm != 0 {
		// On Wasm, we use 32-bit hash, despite that uintptr is 64-bit.
		// memhash* always returns a uintptr with high 32-bit being 0
		// (see hash32.go). We trim the hash in other places where we
		// compute the hash manually, e.g. in interhash.
		return uintptr(uint32())
	}
	return 
}

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 {
	 := sys.GetClosurePtr()
	 := *(*uintptr)(unsafe.Pointer( + unsafe.Sizeof()))
	return memhash(, , )
}

// This is simple wrappers.
// It's better to use maps.MemHash functions directly,
// but we have reflection code that still calls hashing from runtime via LookupRuntime,
// so we have to try to minimize overhead of an extra call.
// For this add nosplit for performance

// memhash should be an internal detail,
// but widely used packages access it using linkname.
// Notable members of the hall of shame include:
//   - github.com/aacfactory/fns
//   - github.com/dgraph-io/ristretto
//   - github.com/minio/simdjson-go
//   - github.com/nbd-wtf/go-nostr
//   - github.com/outcaste-io/ristretto
//   - github.com/puzpuzpuz/xsync/v2
//   - github.com/puzpuzpuz/xsync/v3
//   - github.com/authzed/spicedb
//   - github.com/pingcap/badger
//
// Do not remove or change the type signature.
// See go.dev/issue/67401.
//
//go:nosplit
//go:linkname memhash
func memhash( unsafe.Pointer, ,  uintptr) uintptr {
	return maps.MemHash(, , )
}

//go:nosplit
func memhash64( unsafe.Pointer,  uintptr) uintptr {
	return maps.MemHash64(readUnaligned64(), )
}

//go:nosplit
func memhash32( unsafe.Pointer,  uintptr) uintptr {
	return maps.MemHash32(readUnaligned32(), )
}

// strhash should be an internal detail,
// but widely used packages access it using linkname.
// Notable members of the hall of shame include:
//   - github.com/aristanetworks/goarista
//   - github.com/bytedance/sonic
//   - github.com/bytedance/go-tagexpr/v2
//   - github.com/cloudwego/dynamicgo
//   - github.com/v2fly/v2ray-core/v5
//
// Do not remove or change the type signature.
// See go.dev/issue/67401.
//
//go:nosplit
//go:linkname strhash
func strhash( unsafe.Pointer,  uintptr) uintptr {
	return maps.StrHash(*(*string)(), )
}

// 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 trimHash(c1 * (c0 ^ )) // +0, -0
	case  != :
		return trimHash(c1 * (c0 ^  ^ uintptr(rand()))) // any kind of NaN
	default:
		return memhash(, , 4)
	}
}

func f64hash( unsafe.Pointer,  uintptr) uintptr {
	 := *(*float64)()
	switch {
	case  == 0:
		return trimHash(c1 * (c0 ^ )) // +0, -0
	case  != :
		return trimHash(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 trimHash(c1 * typehash(, unsafe.Pointer(&.data), ^c0))
	} else {
		return trimHash(c1 * typehash(, .data, ^c0))
	}
}

// nilinterhash should be an internal detail,
// but widely used packages access it using linkname.
// Notable members of the hall of shame include:
//   - github.com/anacrolix/stm
//   - github.com/aristanetworks/goarista
//
// Do not remove or change the type signature.
// See go.dev/issue/67401.
//
//go:linkname nilinterhash
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 trimHash(c1 * typehash(, unsafe.Pointer(&.data), ^c0))
	} else {
		return trimHash(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).
//
// typehash should be an internal detail,
// but widely used packages access it using linkname.
// Notable members of the hall of shame include:
//   - github.com/puzpuzpuz/xsync/v2
//   - github.com/puzpuzpuz/xsync/v3
//
// Do not remove or change the type signature.
// See go.dev/issue/67401.
//
//go:linkname typehash
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() {
	case abi.Float32:
		return f32hash(, )
	case abi.Float64:
		return f64hash(, )
	case abi.Complex64:
		return c64hash(, )
	case abi.Complex128:
		return c128hash(, )
	case abi.String:
		return strhash(, )
	case abi.Interface:
		 := (*interfacetype)(unsafe.Pointer())
		if len(.Methods) == 0 {
			return nilinterhash(, )
		}
		return interhash(, )
	case abi.Array:
		 := (*arraytype)(unsafe.Pointer())
		for  := uintptr(0);  < .Len; ++ {
			 = (.Elem, add(, *.Elem.Size_), )
		}
		return 
	case abi.Struct:
		 := (*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 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)
//
// stringHash should be an internal detail,
// but widely used packages access it using linkname.
// Notable members of the hall of shame include:
//   - github.com/k14s/starlark-go
//
// Do not remove or change the type signature.
// See go.dev/issue/67401.
//
//go:linkname stringHash
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(&)), )
}

func readUnaligned32( unsafe.Pointer) uint32 {
	 := (*[4]byte)()
	if goarch.BigEndian {
		return byteorder.BEUint32([:])
	}
	return byteorder.LEUint32([:])
}

func readUnaligned64( unsafe.Pointer) uint64 {
	 := (*[8]byte)()
	if goarch.BigEndian {
		return byteorder.BEUint64([:])
	}
	return byteorder.LEUint64([:])
}