View Issue Details

IDProjectCategoryView StatusLast Update
0038180FPCCompilerpublic2020-12-16 22:55
Reporterrunewalsh Assigned ToFlorian  
PrioritylowSeveritytweakReproducibilityalways
Status resolvedResolutionfixed 
Fixed in Version3.3.1 
Summary0038180: Optimization of X div(mod) 2^N is hindered by automatic upcasting to signed type.
DescriptionInstead 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 Reproducevar
    x: uint16;

begin
    readln(x);
    writeln(x div 128);
    writeln(x mod 128);
end.
TagsNo tags attached.
Fixed in Revision47797
FPCOldBugId
FPCTarget-
Attached Files

Activities

J. Gareth Moreton

2020-12-07 14:24

developer   ~0127417

I'll have a play around to see what can be safely improved.

Thaddy de Koning

2020-12-07 20:17

reporter   ~0127431

If you are playing around you might want to check against C and Delphi. Arithmetic Modulo can never be negative, but compute modulo can...

Issue History

Date Modified Username Field Change
2020-12-07 11:05 runewalsh New Issue
2020-12-07 14:24 J. Gareth Moreton Priority normal => low
2020-12-07 14:24 J. Gareth Moreton Severity minor => tweak
2020-12-07 14:24 J. Gareth Moreton FPCTarget => -
2020-12-07 14:24 J. Gareth Moreton Note Added: 0127417
2020-12-07 20:17 Thaddy de Koning Note Added: 0127431
2020-12-16 22:55 Florian Assigned To => Florian
2020-12-16 22:55 Florian Status new => resolved
2020-12-16 22:55 Florian Resolution open => fixed
2020-12-16 22:55 Florian Fixed in Version => 3.3.1
2020-12-16 22:55 Florian Fixed in Revision => 47797