Files

43 lines
820 B
Go
Raw Permalink Normal View History

//go:build ignore
package main
import (
2026-03-06 20:14:02 +00:00
. "sources.truenas.cloud/code/avo/build"
. "sources.truenas.cloud/code/avo/operand"
)
func main() {
TEXT("Triangle", NOSPLIT, "func(n uint64) uint64")
Doc("Triangle computes the nth triangle number.")
n := Load(Param("n"), GP64())
Comment("Initialize sum register to zero.")
s := GP64()
XORQ(s, s)
// Use two labels for the top of the loop.
Label("loop_even")
Label("loop_odd")
Comment("Loop until n is zero.")
CMPQ(n, Imm(0))
JE(LabelRef("done"))
Comment("Add n to sum.")
ADDQ(n, s)
Comment("Decrement n.")
DECQ(n)
Comment("Jump to one of the loop labels depending on parity.")
TESTQ(U32(1), n)
JZ(LabelRef("loop_even"))
JMP(LabelRef("loop_odd"))
Label("done")
Comment("Store sum to return value.")
Store(s, ReturnIndex(0))
RET()
Generate()
}