examples/sum: use Comment (#41)

This commit is contained in:
Michael McLoughlin
2019-01-11 10:57:38 -08:00
parent 85432688f1
commit 3ca82be16c
4 changed files with 31 additions and 15 deletions

View File

@@ -102,24 +102,24 @@ func main() {
ptr := Load(Param("xs").Base(), GP64()) ptr := Load(Param("xs").Base(), GP64())
n := Load(Param("xs").Len(), GP64()) n := Load(Param("xs").Len(), GP64())
// Initialize sum register to zero. Comment("Initialize sum register to zero.")
s := GP64() s := GP64()
XORQ(s, s) XORQ(s, s)
// Loop until zero bytes remain. Comment("Loop until zero bytes remain.")
Label("loop") Label("loop")
CMPQ(n, Imm(0)) CMPQ(n, Imm(0))
JE(LabelRef("done")) JE(LabelRef("done"))
// Load from pointer and add to running sum. Comment("Load from pointer and add to running sum.")
ADDQ(Mem{Base: ptr}, s) ADDQ(Mem{Base: ptr}, s)
// Advance pointer, decrement byte count. Comment("Advance pointer, decrement byte count.")
ADDQ(Imm(8), ptr) ADDQ(Imm(8), ptr)
DECQ(n) DECQ(n)
JMP(LabelRef("loop")) JMP(LabelRef("loop"))
// Store sum to return value. Comment("Store sum to return value.")
Label("done") Label("done")
Store(s, ReturnIndex(0)) Store(s, ReturnIndex(0))
RET() RET()
@@ -139,16 +139,24 @@ The result from this code generator is:
TEXT ·Sum(SB), NOSPLIT, $0-32 TEXT ·Sum(SB), NOSPLIT, $0-32
MOVQ xs_base(FP), AX MOVQ xs_base(FP), AX
MOVQ xs_len+8(FP), CX MOVQ xs_len+8(FP), CX
// Initialize sum register to zero.
XORQ DX, DX XORQ DX, DX
// Loop until zero bytes remain.
loop: loop:
CMPQ CX, $0x00 CMPQ CX, $0x00
JE done JE done
// Load from pointer and add to running sum.
ADDQ (AX), DX ADDQ (AX), DX
// Advance pointer, decrement byte count.
ADDQ $0x08, AX ADDQ $0x08, AX
DECQ CX DECQ CX
JMP loop JMP loop
// Store sum to return value.
done: done:
MOVQ DX, ret+24(FP) MOVQ DX, ret+24(FP)
RET RET

View File

@@ -10,24 +10,24 @@ func main() {
ptr := Load(Param("xs").Base(), GP64()) ptr := Load(Param("xs").Base(), GP64())
n := Load(Param("xs").Len(), GP64()) n := Load(Param("xs").Len(), GP64())
// Initialize sum register to zero. Comment("Initialize sum register to zero.")
s := GP64() s := GP64()
XORQ(s, s) XORQ(s, s)
// Loop until zero bytes remain. Comment("Loop until zero bytes remain.")
Label("loop") Label("loop")
CMPQ(n, Imm(0)) CMPQ(n, Imm(0))
JE(LabelRef("done")) JE(LabelRef("done"))
// Load from pointer and add to running sum. Comment("Load from pointer and add to running sum.")
ADDQ(Mem{Base: ptr}, s) ADDQ(Mem{Base: ptr}, s)
// Advance pointer, decrement byte count. Comment("Advance pointer, decrement byte count.")
ADDQ(Imm(8), ptr) ADDQ(Imm(8), ptr)
DECQ(n) DECQ(n)
JMP(LabelRef("loop")) JMP(LabelRef("loop"))
// Store sum to return value. Comment("Store sum to return value.")
Label("done") Label("done")
Store(s, ReturnIndex(0)) Store(s, ReturnIndex(0))
RET() RET()

View File

@@ -13,24 +13,24 @@ func main() {
ptr := Load(Param("xs").Base(), GP64()) ptr := Load(Param("xs").Base(), GP64())
n := Load(Param("xs").Len(), GP64()) n := Load(Param("xs").Len(), GP64())
// Initialize sum register to zero. Comment("Initialize sum register to zero.")
s := GP64() s := GP64()
XORQ(s, s) XORQ(s, s)
// Loop until zero bytes remain. Comment("Loop until zero bytes remain.")
Label("loop") Label("loop")
CMPQ(n, Imm(0)) CMPQ(n, Imm(0))
JE(LabelRef("done")) JE(LabelRef("done"))
// Load from pointer and add to running sum. Comment("Load from pointer and add to running sum.")
ADDQ(Mem{Base: ptr}, s) ADDQ(Mem{Base: ptr}, s)
// Advance pointer, decrement byte count. Comment("Advance pointer, decrement byte count.")
ADDQ(Imm(8), ptr) ADDQ(Imm(8), ptr)
DECQ(n) DECQ(n)
JMP(LabelRef("loop")) JMP(LabelRef("loop"))
// Store sum to return value. Comment("Store sum to return value.")
Label("done") Label("done")
Store(s, ReturnIndex(0)) Store(s, ReturnIndex(0))
RET() RET()

View File

@@ -6,16 +6,24 @@
TEXT ·Sum(SB), NOSPLIT, $0-32 TEXT ·Sum(SB), NOSPLIT, $0-32
MOVQ xs_base(FP), AX MOVQ xs_base(FP), AX
MOVQ xs_len+8(FP), CX MOVQ xs_len+8(FP), CX
// Initialize sum register to zero.
XORQ DX, DX XORQ DX, DX
// Loop until zero bytes remain.
loop: loop:
CMPQ CX, $0x00 CMPQ CX, $0x00
JE done JE done
// Load from pointer and add to running sum.
ADDQ (AX), DX ADDQ (AX), DX
// Advance pointer, decrement byte count.
ADDQ $0x08, AX ADDQ $0x08, AX
DECQ CX DECQ CX
JMP loop JMP loop
// Store sum to return value.
done: done:
MOVQ DX, ret+24(FP) MOVQ DX, ret+24(FP)
RET RET