Files
avo/examples/fnv1a/README.md
Michael McLoughlin 602bb5197c build: unify Label function signatures
The Context.Label method and LABEL global function did not agree. Also
breaks the convention I'd like to set that capitalized functions must
agree with existing Go assembly syntax.

To help avoid a conflict with `avo.Label`, attributes were moved to
their own package.

Fixes #35
2019-01-05 18:18:49 -08:00

40 lines
717 B
Markdown

# fnv1a
[FNV-1a](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash) in `avo`.
[embedmd]:# (asm.go /const/ $)
```go
const (
OffsetBasis = 0xcbf29ce484222325
Prime = 0x100000001b3
)
func main() {
TEXT("Hash64", "func(data []byte) uint64")
Doc("Hash64 computes the FNV-1a hash of data.")
ptr := Load(Param("data").Base(), GP64())
n := Load(Param("data").Len(), GP64())
h := RAX
MOVQ(Imm(OffsetBasis), h)
p := GP64()
MOVQ(Imm(Prime), p)
Label("loop")
CMPQ(n, Imm(0))
JE(LabelRef("done"))
b := GP64()
MOVBQZX(Mem{Base: ptr}, b)
XORQ(b, h)
MULQ(p)
INCQ(ptr)
DECQ(n)
JMP(LabelRef("loop"))
Label("done")
Store(h, ReturnIndex(0))
RET()
Generate()
}
```