github.com/amp-space/amp-sdk-go@v0.7.6/stdlib/bufs/hash.go (about)

     1  package bufs
     2  
     3  import "unsafe"
     4  
     5  // HashBuf is the hash function used by go map, it uses available hardware instructions.
     6  // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
     7  func HashBuf(data []byte) uint64 {
     8  	ss := (*stringStruct)(unsafe.Pointer(&data))
     9  	return uint64(memhash(ss.str, 0, uintptr(ss.len)))
    10  }
    11  
    12  // HashStr is the hash function used by go map, it utilizes available hardware instructions.
    13  // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
    14  func HashStr(str string) uint64 {
    15  	ss := (*stringStruct)(unsafe.Pointer(&str))
    16  	return uint64(memhash(ss.str, 0, uintptr(ss.len)))
    17  }
    18  
    19  // AP Hash Function -- deprecated in place of HashBuf.
    20  // https://www.partow.net/programming/hashfunctions/#AvailableHashFunctions
    21  func APHash64(buf []byte) uint64 {
    22  	var hash uint64 = 0xaaaaaaaaaaaaaaaa
    23  	for i, b := range buf {
    24  		if (i & 1) == 0 {
    25  			hash ^= ((hash << 7) ^ uint64(b) ^ (hash >> 3))
    26  		} else {
    27  			hash ^= (^((hash << 11) ^ uint64(b) ^ (hash >> 5)) + 1)
    28  		}
    29  	}
    30  	return hash
    31  }
    32  
    33  type stringStruct struct {
    34  	str unsafe.Pointer
    35  	len int
    36  }
    37  
    38  //go:noescape
    39  //go:linkname memhash runtime.memhash
    40  func memhash(p unsafe.Pointer, h, s uintptr) uintptr