Optimization of X div(mod) 2^N is hindered by automatic upcasting to signed type.
Original Reporter info from Mantis: runewalsh
-
Reporter name:
Original Reporter info from Mantis: runewalsh
- Reporter name:
Description:
Instead of single SHR (div) / AND (mod), code below is compiled (with full optimization) into several shifts, ands, adds, and/or subs. But why it have to?
Same thing with x: uint32 on 64-bit.
In particular, expressions like a[x mod length(a)] where 'a' is a static array with power-of-two length and 'x' has unsigned type narrower than native are compiled as if 'x' could ever be negative.
Steps to reproduce:
var
x: uint16;
begin
readln(x);
writeln(x div 128);
writeln(x mod 128);
end.
Mantis conversion info:
- Mantis ID: 38180
- Build: 63950
- Fixed in version: 3.3.1
- Fixed in revision: 47797 (#9af9f140)