| 1 | # fnv1a |
| 2 | |
| 3 | [FNV-1a](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash) in `avo`. |
| 4 | |
| 5 | [embedmd]:# (asm.go /const/ $) |
| 6 | ```go |
| 7 | const ( |
| 8 | OffsetBasis = 0xcbf29ce484222325 |
| 9 | Prime = 0x100000001b3 |
| 10 | ) |
| 11 | |
| 12 | func main() { |
| 13 | TEXT("Hash64", NOSPLIT, "func(data []byte) uint64") |
| 14 | Doc("Hash64 computes the FNV-1a hash of data.") |
| 15 | ptr := Load(Param("data").Base(), GP64()) |
| 16 | n := Load(Param("data").Len(), GP64()) |
| 17 | |
| 18 | h := RAX |
| 19 | MOVQ(Imm(OffsetBasis), h) |
| 20 | p := GP64() |
| 21 | MOVQ(Imm(Prime), p) |
| 22 | |
| 23 | Label("loop") |
| 24 | CMPQ(n, Imm(0)) |
| 25 | JE(LabelRef("done")) |
| 26 | b := GP64() |
| 27 | MOVBQZX(Mem{Base: ptr}, b) |
| 28 | XORQ(b, h) |
| 29 | MULQ(p) |
| 30 | INCQ(ptr) |
| 31 | DECQ(n) |
| 32 | |
| 33 | JMP(LabelRef("loop")) |
| 34 | Label("done") |
| 35 | Store(h, ReturnIndex(0)) |
| 36 | RET() |
| 37 | Generate() |
| 38 | } |
| 39 | ``` |