v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/hash/memhash.go (about)

     1  package hash
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  func memhash(p unsafe.Pointer, h, s uintptr) uintptr
     9  
    10  //go:linkname memhash runtime.memhash
    11  
    12  func MemHash(b []byte, seed uintptr) uintptr {
    13  	if len(b) == 0 {
    14  		return seed
    15  	}
    16  	return memhash(unsafe.Pointer(&b[0]), seed, uintptr(len(b)))
    17  }
    18  
    19  func Memhash64(b []byte) uint64 {
    20  	return uint64(MemHash(b, uintptr(42)))
    21  }
    22  
    23  func MemHashString(str string, seed uintptr) uintptr {
    24  	if len(str) == 0 {
    25  		return 0
    26  	}
    27  	return memhash(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&str)).Data), seed, uintptr(len(str)))
    28  }
    29  
    30  func MemHashString64(str string) uint64 {
    31  	return uint64(MemHashString(str, 42))
    32  }