fix signed long shift right

This commit is contained in:
Irmen de Jong
2025-09-27 23:07:49 +02:00
parent 045c4909a8
commit cd1862dd9f
2 changed files with 13 additions and 83 deletions

View File

@@ -605,7 +605,9 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
">>" -> { ">>" -> {
asmgen.out(""" asmgen.out("""
ldy $sourceVar ldy $sourceVar
- lsr $targetVar+3 - lda $targetVar+3
asl a ; save sign bit
ror $targetVar+3
ror $targetVar+2 ror $targetVar+2
ror $targetVar+1 ror $targetVar+1
ror $targetVar ror $targetVar
@@ -780,100 +782,27 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
value in 0..2 -> { value in 0..2 -> {
repeat(value) { repeat(value) {
asmgen.out(""" asmgen.out("""
lsr $variable+3 lda $variable+3
asl a ; save sign bit
ror $variable+3
ror $variable+2 ror $variable+2
ror $variable+1 ror $variable+1
ror $variable""") ror $variable""")
} }
} }
value in 3..7 -> { // TODO optimize for more cases 8, 16, 24 etc but don't forget to take the sign bit into account!
value <= 31 -> {
asmgen.out(""" asmgen.out("""
ldy #$value ldy #$value
- lsr $variable+3 - lda $variable+3
asl a ; save sign bit
ror $variable+3
ror $variable+2 ror $variable+2
ror $variable+1 ror $variable+1
ror $variable ror $variable
dey dey
bne -""") bne -""")
} }
value == 8 -> {
asmgen.out("""
lda $variable+1
sta $variable
lda $variable+2
sta $variable+1
lda $variable+3
sta $variable+2
lda #0
sta $variable+3""")
}
value in 9..15 -> {
val shift = value - 8
asmgen.out("""
lda $variable+1
sta $variable
lda $variable+2
sta $variable+1
lda $variable+3
sta $variable+2
lda #0
sta $variable+3
ldy #$shift
- lsr $variable+2
ror $variable+1
ror $variable
dey
bne -""")
}
value == 16 -> {
asmgen.out("""
lda $variable+3
sta $variable+1
lda $variable+2
sta $variable
lda #0
sta $variable+2
sta $variable+3""")
}
value in 17..23 -> {
val shift = value-16
asmgen.out("""
lda $variable+3
sta $variable+1
lda $variable+2
sta $variable
lda #0
sta $variable+2
sta $variable+3
ldy #$shift
- lsr $variable+1
ror $variable
dey
bne -""")
}
value == 24 -> {
asmgen.out("""
lda $variable+3
sta $variable
lda #0
sta $variable+1
sta $variable+2
sta $variable+3""")
}
value <= 31 -> {
val shift = value-24
asmgen.out("""
lda $variable+3
ldy #$shift
- lsr a
dey
bne -
sta $variable
lda #0
sta $variable+1
sta $variable+2
sta $variable+3""")
}
else -> { else -> {
asmgen.out(""" asmgen.out("""
lda #0 lda #0

View File

@@ -1,7 +1,6 @@
TODO TODO
==== ====
CHECK THAT >> and << work correctly for negative longs
implement the bitwise & | ^ operations as expressions on longs (all types args) implement the bitwise & | ^ operations as expressions on longs (all types args)
@@ -177,6 +176,8 @@ Optimizations
- more optimized operator handling of different types, for example uword a ^ byte b now does a type cast of b to word first - more optimized operator handling of different types, for example uword a ^ byte b now does a type cast of b to word first
- optimize longEqualsValue() for const and variable operands to not assign needlessly to R0-R3. - optimize longEqualsValue() for const and variable operands to not assign needlessly to R0-R3.
- optimize inplacemodificationLongWithLiteralval() for more shift values such as 8, 16, 24 etc but take sign bit into account!
- optimize inplacemodificationLongWithLiteralval() for more shift values such as 8, 16, 24 etc but take sign bit into account!
- Port benchmarks from https://thred.github.io/c-bench-64/ to prog8 and see how it stacks up. - Port benchmarks from https://thred.github.io/c-bench-64/ to prog8 and see how it stacks up.
- Since fixing the missing zp-var initialization, programs grew in size again because STZ's reappeared. Can we add more intelligent (and correct!) optimizations to remove those STZs that might be redundant again? - Since fixing the missing zp-var initialization, programs grew in size again because STZ's reappeared. Can we add more intelligent (and correct!) optimizations to remove those STZs that might be redundant again?
- in Identifier: use typedarray of strings instead of listOf? Other places? - in Identifier: use typedarray of strings instead of listOf? Other places?