2021-10-29 01:18:34 -07:00
|
|
|
//go:build ignore
|
2018-12-11 23:02:50 -08:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
. "github.com/mmcloughlin/avo/build"
|
2019-01-05 17:41:07 -08:00
|
|
|
. "github.com/mmcloughlin/avo/operand"
|
2018-12-11 23:02:50 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2019-01-06 20:04:51 -08:00
|
|
|
TEXT("Sum", NOSPLIT, "func(xs []uint64) uint64")
|
2018-12-27 23:09:44 -08:00
|
|
|
Doc("Sum returns the sum of the elements in xs.")
|
2019-01-04 18:23:44 -08:00
|
|
|
ptr := Load(Param("xs").Base(), GP64())
|
|
|
|
|
n := Load(Param("xs").Len(), GP64())
|
2019-01-06 18:06:29 -08:00
|
|
|
|
2019-01-11 10:57:38 -08:00
|
|
|
Comment("Initialize sum register to zero.")
|
2019-01-04 18:23:44 -08:00
|
|
|
s := GP64()
|
2018-12-11 23:02:50 -08:00
|
|
|
XORQ(s, s)
|
2019-01-06 18:06:29 -08:00
|
|
|
|
2019-01-05 18:18:49 -08:00
|
|
|
Label("loop")
|
2019-01-11 11:06:10 -08:00
|
|
|
Comment("Loop until zero bytes remain.")
|
2019-01-05 17:41:07 -08:00
|
|
|
CMPQ(n, Imm(0))
|
|
|
|
|
JE(LabelRef("done"))
|
2019-01-06 18:06:29 -08:00
|
|
|
|
2019-01-11 10:57:38 -08:00
|
|
|
Comment("Load from pointer and add to running sum.")
|
2019-01-05 17:41:07 -08:00
|
|
|
ADDQ(Mem{Base: ptr}, s)
|
2019-01-06 18:06:29 -08:00
|
|
|
|
2019-01-11 10:57:38 -08:00
|
|
|
Comment("Advance pointer, decrement byte count.")
|
2019-01-05 17:41:07 -08:00
|
|
|
ADDQ(Imm(8), ptr)
|
2018-12-11 23:02:50 -08:00
|
|
|
DECQ(n)
|
2019-01-05 17:41:07 -08:00
|
|
|
JMP(LabelRef("loop"))
|
2019-01-06 18:06:29 -08:00
|
|
|
|
2019-01-05 18:18:49 -08:00
|
|
|
Label("done")
|
2019-01-11 11:06:10 -08:00
|
|
|
Comment("Store sum to return value.")
|
2018-12-11 23:02:50 -08:00
|
|
|
Store(s, ReturnIndex(0))
|
|
|
|
|
RET()
|
|
|
|
|
Generate()
|
|
|
|
|
}
|