Bug 13784 – Wrong code with modulo operation and optimisations enabled
Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2014-11-27T14:08:00Z
Last change time
2015-02-18T03:41:20Z
Keywords
industry, wrong-code
Assigned to
nobody
Creator
public
Comments
Comment #0 by public — 2014-11-27T14:08:56Z
assertions passes when compiled with no flags and fails when compiled with -O
```
long modulo24 (long ticks)
{
const TicksPerDay = 864000000000;
ticks %= TicksPerDay;
if (ticks < 0)
ticks += TicksPerDay;
return ticks;
}
unittest
{
assert (modulo24(-141600000000) == 722400000000);
}
```
Checking assembly reveals wrong optimisation via replacing modulo with multiplication.
Issue present in both D1 and D2 compilers. Workaround is to avoid modulo operation with negative numbers.
Comment #1 by public — 2014-11-28T13:48:08Z
Extra issue is that even with workaround it sometimes does broken optimisation at if both -O and -inline are present (at the call site for modulo24)