From 32d894d6b662340070b1758fc16e5c9248019fdb Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 28 Feb 2021 21:22:46 +0100 Subject: [PATCH] optimized repeat loop for word counts --- .../compiler/target/cpu6502/codegen/AsmGen.kt | 12 ++----- examples/test.p8 | 33 ++++++++++++++++++- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt index 4d4fa38a8..a8e28e79f 100644 --- a/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt @@ -1011,14 +1011,8 @@ internal class AsmGen(private val program: Program, // note: A/Y must have been loaded with the number of iterations! if(constIterations==0) return - if(constIterations==null) { - out(""" - cmp #0 - bne + - cpy #0 - beq $endLabel ; skip loop if zero iters -+""") - } + // no need to explicitly test for 0 iterations as this is done in the count down logic below + val counterVar = makeLabel("repeatcounter") out(""" sta $counterVar @@ -1027,7 +1021,7 @@ $repeatLabel lda $counterVar bne + lda $counterVar+1 beq $endLabel -+ lda $counterVar + lda $counterVar bne + dec $counterVar+1 + dec $counterVar diff --git a/examples/test.p8 b/examples/test.p8 index f7ef2344a..4fe9069d4 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -2,6 +2,37 @@ %zeropage basicsafe main { + sub start() { + bench() + txt.nl() + + uword total + ubyte bb + uword ww + + for bb in 0 to 255 { + total = 0 + repeat bb + total++ + txt.print_uw(total) + txt.spc() + txt.spc() + } + + txt.nl() + txt.nl() + + for ww in 0 to 600 { + total = 0 + repeat ww + total++ + txt.print_uw(total) + txt.spc() + txt.spc() + } + + } + sub iter(uword iterations) -> uword { uword total = 0 @@ -26,7 +57,7 @@ main { return total } - sub start() { + sub bench() { uword xx1 uword xx2 uword xx3