Files
tinygo/src/runtime/memhash_fnv.go
Randy Reddig 8e8ad9004f all: replace target=wasi with target=wasip1
This eliminates the 'wasi' build tag in favor of 'GOOS=wasip1', introduced in Go 1.21.

For backwards compatablity, -target=wasi is a synonym for -target=wasip1.
2024-03-27 16:01:40 +01:00

35 lines
1023 B
Go

//go:build (!wasip1 && !runtime_memhash_tsip && !runtime_memhash_leveldb) || (wasip1 && runtime_memhash_fnv)
// This is the default for all targets except WASI, unless a more specific build
// tag is set.
package runtime
import "unsafe"
// Get FNV-1a hash of the given memory buffer.
//
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash
func hash32(ptr unsafe.Pointer, n, seed uintptr) uint32 {
var result uint32 = 2166136261 // FNV offset basis
result *= uint32(seed)
for i := uintptr(0); i < n; i++ {
c := *(*uint8)(unsafe.Add(ptr, i))
result ^= uint32(c) // XOR with byte
result *= 16777619 // FNV prime
}
return result
}
// Also a FNV-1a hash.
func hash64(ptr unsafe.Pointer, n, seed uintptr) uint64 {
var result uint64 = 14695981039346656037 // FNV offset basis
result *= uint64(seed)
for i := uintptr(0); i < n; i++ {
c := *(*uint8)(unsafe.Add(ptr, i))
result ^= uint64(c) // XOR with byte
result *= 1099511628211 // FNV prime
}
return result
}