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)
Comment #2 by bugzilla — 2014-12-15T09:56:25Z
Comment #3 by github-bugzilla — 2014-12-15T11:26:26Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/61dd9d1fac47855a2fb6c1c79cd54c49a47db306 fix Issue 13784 - Wrong code with modulo operation and optimisations enabled https://github.com/D-Programming-Language/dmd/commit/1b8db262262352b00d027c93aee00b669b290931 Merge pull request #4214 from WalterBright/fix13784 fix Issue 13784 - Wrong code with modulo operation and optimisations ena...
Comment #4 by public — 2014-12-15T11:34:59Z
Reopening as D1-only until fix is merged into dmd-1.x :P
Comment #5 by github-bugzilla — 2014-12-15T20:20:01Z
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/012695720948b8633e054e63485ccb091b3847d5 fix Issue 13784 - Wrong code with modulo operation and optimisations enabled
Comment #6 by github-bugzilla — 2015-02-18T03:41:20Z
Commits pushed to 2.067 at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/61dd9d1fac47855a2fb6c1c79cd54c49a47db306 fix Issue 13784 - Wrong code with modulo operation and optimisations enabled https://github.com/D-Programming-Language/dmd/commit/1b8db262262352b00d027c93aee00b669b290931 Merge pull request #4214 from WalterBright/fix13784