optimize x % p where p=power-of-2, into just x & (p-1)

This commit is contained in:
Irmen de Jong 2021-04-08 22:21:16 +02:00
parent c853afe769
commit 91e421d961
3 changed files with 11 additions and 6 deletions

View File

@ -490,9 +490,9 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker()
if(!idt.isKnown)
throw FatalAstException("unknown dt")
return NumericLiteralValue(idt.typeOrElse(DataType.STRUCT), 0, expr.position)
} else if (cv == 2.0) {
} else if (cv in powersOfTwo) {
expr.operator = "&"
expr.right = NumericLiteralValue.optimalInteger(1, expr.position)
expr.right = NumericLiteralValue.optimalInteger(cv!!.toInt()-1, expr.position)
return null
}
}

View File

@ -2,8 +2,6 @@
TODO
====
- optimize %32 to & 31
- optimize several inner loops in gfx2
- hoist all variable declarations up to the subroutine scope *before* even the constant folding takes place (to avoid undefined symbol errors when referring to a variable from another nested scope in the subroutine)
- optimize swap of two memread values with index, using the same pointer expression/variable, like swap(@(ptr+1), @(ptr+2))

View File

@ -1,10 +1,17 @@
%import textio
main {
sub start() {
uword width
uword width2 = 12345
ubyte ub1
ubyte ub2 = 123
; TODO fix compiler crash with noopt:
uword x1 = (width-1 as uword) + 2
ub1 = ub2 % 32
txt.print_ub(ub1)
txt.nl()
width = width2 % 32
txt.print_uw(width)
}
}