github.com/aacfactory/avro@v1.2.12/internal/hashs/mmhash/hash.go (about) 1 // Package mmhash export runtime.memhash 2 package mmhash 3 4 import "unsafe" 5 6 //go:noescape 7 //go:linkname memhash runtime.memhash 8 func memhash(p unsafe.Pointer, h, s uintptr) uintptr 9 10 type stringStruct struct { 11 str unsafe.Pointer 12 len int 13 } 14 15 // Sum64 sum bytes to uint64. 16 func Sum64(data []byte) uint64 { 17 ss := (*stringStruct)(unsafe.Pointer(&data)) 18 return uint64(memhash(ss.str, 0, uintptr(ss.len))) 19 } 20 21 // Hash sum bytes to uint32. 22 func Hash(b []byte) uint32 { 23 const ( 24 seed = 0xbc9f1d34 25 m = 0xc6a4a793 26 ) 27 h := uint32(seed) ^ uint32(len(b))*m 28 for ; len(b) >= 4; b = b[4:] { 29 h += uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 30 h *= m 31 h ^= h >> 16 32 } 33 switch len(b) { 34 case 3: 35 h += uint32(b[2]) << 16 36 fallthrough 37 case 2: 38 h += uint32(b[1]) << 8 39 fallthrough 40 case 1: 41 h += uint32(b[0]) 42 h *= m 43 h ^= h >> 24 44 } 45 return h 46 }