Bug 18315 – wrong code for `i > 0`

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-01-27T14:42:08Z
Last change time
2018-02-28T20:13:43Z
Keywords
pull, wrong-code
Assigned to
No Owner
Creator
ag0aep6g

Comments

Comment #0 by ag0aep6g — 2018-01-27T14:42:08Z
Found by kdevel who posted to D.learn: https://forum.dlang.org/post/[email protected] ---- void main () { int i = int.min; bool b = i > 0; assert(!b); /* fails */ } ----
Comment #1 by ag0aep6g — 2018-01-27T14:47:33Z
Whoops. Accidentally hit "submit". This is what dmd generates for `i > 0`: ---- neg EAX shr EAX,01Fh ---- In D: `(-i) >> 31`. That code assumes that negation always flips the sign bit. But it doesn't for `int.min`. `-int.min` is `int.min` again.
Comment #2 by schveiguy — 2018-01-27T14:56:23Z
Wow, this is really old. Tested all the way back to 2.040, still fails.
Comment #3 by schveiguy — 2018-01-27T14:56:45Z
Also fails on MacosX.
Comment #4 by code — 2018-01-28T19:24:02Z
Comment #5 by ag0aep6g — 2018-02-04T22:25:49Z
Comment #6 by schveiguy — 2018-02-07T14:29:37Z
Martin, the wrong code is for i > 0, not int.min > 0. int.min > 0 is folded to false: https://run.dlang.io/is/dvHi9a
Comment #7 by github-bugzilla — 2018-02-09T09:36:03Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/74904b6e43d0934f7fbcd2f966b04aaa58d4cde9 fix issue 18315 - wrong code for `i > 0` `SBB reg,0` is needed to make this work. There's nothing platform specific about it. Also add comments explaining how this works. https://github.com/dlang/dmd/commit/b4a86786619bcbbd52c9ecf2db4a75a5c3f52911 fixup! fix issue 18315 - wrong code for `i > 0` https://github.com/dlang/dmd/commit/4ccde911c8131dc170212f9a2f6d926ba7bdbe80 Merge pull request #7841 from aG0aep6G/18315 fix issue 18315 - wrong code for `i > 0`
Comment #8 by ag0aep6g — 2018-02-28T20:13:43Z
*** Issue 18001 has been marked as a duplicate of this issue. ***