Files
avo/examples/sum
Michael McLoughlin 51931ee673 doc: README for sum example
Updates #14
2019-01-01 16:04:37 -08:00
..
2018-12-27 23:09:44 -08:00
2019-01-01 16:04:37 -08:00
2018-12-27 23:09:44 -08:00
2018-12-12 00:02:22 -08:00
2018-12-31 00:23:27 -08:00

sum

Sum an array of uint64s.

func main() {
	TEXT("Sum", "func(xs []uint64) uint64")
	Doc("Sum returns the sum of the elements in xs.")
	ptr := Load(Param("xs").Base(), GP64v())
	n := Load(Param("xs").Len(), GP64v())
	s := GP64v()
	XORQ(s, s)
	LABEL("loop")
	CMPQ(n, operand.Imm(0))
	JE(operand.LabelRef("done"))
	ADDQ(operand.Mem{Base: ptr}, s)
	ADDQ(operand.Imm(8), ptr)
	DECQ(n)
	JMP(operand.LabelRef("loop"))
	LABEL("done")
	Store(s, ReturnIndex(0))
	RET()
	Generate()
}