compiler: refactor compiler into separate package

This commit is contained in:
Ayke van Laethem
2018-09-22 20:30:46 +02:00
parent b75a02e66d
commit b2cbfa78ca
5 changed files with 12 additions and 7 deletions
+26
View File
@@ -0,0 +1,26 @@
package compiler
// 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 hashmapHash(data []byte) uint32 {
var result uint32 = 2166136261 // FNV offset basis
for _, c := range data {
result ^= uint32(c)
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
}