nanochess/IntyBASIC

optimizing "and 255" on bytes

artrag opened this issue · 1 comments

In my code I have two_pi = 255 defined as constant. The values is used in cos/sin tables and in other formulas about movements of the main ship and enemies.
Those formulas are general and have been written when I was using less than 256 angles
One of them is this:
;[687] ms_p=(ms_p+ms_speed) and two_pi
SRCFILE "dzgorf.bas",687
5824 0280 0160 MVI V81,R0
5826 03F8 0080 XORI #128,R0
5828 0338 0080 SUBI #128,R0
582A 02C0 015F ADD V4,R0
582C 03B8 00FF ANDI #255,R0
582E 0240 015F MVO R0,V4

As ms_p is a byte, I was expecting that the compiler was removing the "and 255" part, cutting away this line
582C 03B8 00FF ANDI #255,R0

But maybe, the compiler is not aware that V4 is in the 8 bit RAM, so cannot perform this kind of optimizations.
On a different side, just for my understanding, ms_speed is signed, ms_p is unsigned.
What is the reason for XORI #128,R0 and SUBI #128,R0 on V81 (alias ms_speed )?
The fact is that, being the result stored in a 8 bit location, the compiler could avoid sign extension for ms_speed

The ANDI #255,R0 is now optimized. The compiler currently cannot avoid expanding sign for ms_speed because the optimizer is a peephole one.