tinygo/util.go
2018-08-24 00:56:20 +02:00

26 строки
692 Б
Go

package main
// This file contains functions that are also used by the runtime. These must be
// kept in sync.
// Get FNV-1a hash of this string.
//
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash
func stringhash(s *string) uint32 {
var result uint32 = 2166136261 // FNV offset basis
for i := 0; i < len(*s); i++ {
result ^= uint32((*s)[i])
result *= 16777619 // FNV prime
}
return result
}
// Get the topmost 8 bits of the hash, without using a special value (like 0).
func hashmapTopHash(hash uint32) uint8 {
tophash := uint8(hash >> 24)
if tophash < 1 {
// 0 means empty slot, so make it bigger.
tophash += 1
}
return tophash
}