| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
. "github.com/mmcloughlin/avo/build" |
| 7 |
. "github.com/mmcloughlin/avo/operand" |
| 8 |
) |
| 9 |
|
| 10 |
func main() { |
| 11 |
TEXT("Sum", NOSPLIT, "func(xs []uint64) uint64") |
| 12 |
Doc("Sum returns the sum of the elements in xs.") |
| 13 |
ptr := Load(Param("xs").Base(), GP64()) |
| 14 |
n := Load(Param("xs").Len(), GP64()) |
| 15 |
|
| 16 |
Comment("Initialize sum register to zero.") |
| 17 |
s := GP64() |
| 18 |
XORQ(s, s) |
| 19 |
|
| 20 |
Label("loop") |
| 21 |
Comment("Loop until zero bytes remain.") |
| 22 |
CMPQ(n, Imm(0)) |
| 23 |
JE(LabelRef("done")) |
| 24 |
|
| 25 |
Comment("Load from pointer and add to running sum.") |
| 26 |
ADDQ(Mem{Base: ptr}, s) |
| 27 |
|
| 28 |
Comment("Advance pointer, decrement byte count.") |
| 29 |
ADDQ(Imm(8), ptr) |
| 30 |
DECQ(n) |
| 31 |
JMP(LabelRef("loop")) |
| 32 |
|
| 33 |
Label("done") |
| 34 |
Comment("Store sum to return value.") |
| 35 |
Store(s, ReturnIndex(0)) |
| 36 |
RET() |
| 37 |
Generate() |
| 38 |
} |