optimized codegen for word*128 (word << 7): no longer do 7 shifts

This commit is contained in:
Irmen de Jong 2025-01-04 23:05:22 +01:00
parent 407773bda2
commit a56ae7539a
4 changed files with 67 additions and 13 deletions

View File

@ -1161,6 +1161,23 @@ internal class AssignmentAsmGen(
}
}
} else if(dt.isWord) {
if(shifts==7 && expr.operator == "<<") {
// optimized shift left 7 (*128) by first swapping the lsb/msb and then doing just one final shift
assignExpressionToRegister(expr.left, RegisterOrPair.AY, signed)
asmgen.out("""
; shift left 7
sty P8ZP_SCRATCH_REG ; msb
lsr P8ZP_SCRATCH_REG
php ; save carry
sta P8ZP_SCRATCH_REG
lda #0
plp ; restore carry
ror P8ZP_SCRATCH_REG
ror a
ldy P8ZP_SCRATCH_REG""")
return true
}
assignExpressionToRegister(expr.left, RegisterOrPair.AY, signed)
when (shifts) {
in 0..7 -> {

View File

@ -1901,6 +1901,20 @@ $shortcutLabel:""")
else
asmgen.out(" lda #0 | sta $lsb")
}
value==7 -> {
// optimized shift left 7 (*128) by first swapping the lsb/msb and then doing just one final shift
asmgen.out("""
; shift left 7
lsr $msb
php ; save carry
lda $lsb
sta $msb
lda #0
sta $lsb
plp ; restore carry
ror $msb
ror $lsb""")
}
value>3 -> asmgen.out("""
ldy #$value
- asl $lsb

View File

@ -79,10 +79,8 @@ Libraries
Optimizations
-------------
- optimize word*$0080 (also *$0040); don't shift 7 times, but swap lsb/msb (rockrunner)
- if abs(dx) < cave.VISIBLE_CELLS_H looks like it treats abs(dx) as signed word still in codegen? (rockrunner)
- word offset = (row as uword) * 128 + col*2 inefficient code for col*2 (rockrunner)
- is @(cells+offset) same code as cells[offset] ?
- if magicwall_enabled and (jiffy_counter & 3 == 1) sounds.magicwall() -> generates shortcut jump to another jump, why not immediately after the if
- explode(x, y+1) pushes x on the stack and pops it, could simply load it in reverse order and not use the stack.normal
- return mkword(attrs[cx16.r2L], object[cx16.r2L]) same as the explode() above

View File

@ -4,17 +4,42 @@
main {
sub start() {
uword @nozp pointer
pointer++
cx16.r0L = @(pointer)
cx16.r1L = @(pointer+100)
cx16.r1 = peekw(pointer)
cx16.r2 = peekw(pointer+100)
cx16.r3L = peek(pointer+100)
@(pointer) = 99
@(pointer+100) = 99
poke(pointer, 99)
pokew(pointer, 99)
cx16.r0 = cx16.r1 = cx16.r2 = cx16.r3 = %0001111_11000011
cx16.r0 *= $0080
txt.print("goede antwoord:\n")
txt.print_uwbin(cx16.r0, true)
txt.spc()
txt.print_uw(cx16.r0)
txt.nl()
%asm {{
lda cx16.r1H
lsr a
php ; save carry
lda cx16.r1L
sta cx16.r1H
lda #0
sta cx16.r1L
plp ; restore carry
ror cx16.r1H
ror cx16.r1L
}}
txt.print("antwoord 2:\n")
txt.print_uwbin(cx16.r1, true)
txt.spc()
txt.print_uw(cx16.r1)
txt.nl()
txt.nl()
txt.print("antwoord 3:\n")
txt.print_uwbin(cx16.r2 << 7, true)
txt.spc()
txt.print_uw(cx16.r2 << 7)
txt.nl()
txt.nl()
}
}