Bug 13485 – FP wrong-code with -O

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-09-16T16:55:00Z
Last change time
2015-02-21T09:11:30Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
ilyayaroshenko
See also
https://issues.dlang.org/show_bug.cgi?id=13474

Comments

Comment #0 by ilyayaroshenko — 2014-09-16T16:55:16Z
Comment #1 by ibuclaw — 2014-09-17T08:03:23Z
Pasting in code here: --- //Optimization check F foo(F)(F c, F d) { c += d; c += d; return c; } void test0() { alias F = float; enum F d = (cast(F)(2)) ^^ (F.max_exp - 1); assert(foo(-d, d) == d); } void test1() { alias F = double; enum F d = (cast(F)(2)) ^^ (F.max_exp - 1); assert(foo(-d, d) == d); } void test2() { alias F = real; enum F d = (cast(F)(2)) ^^ (F.max_exp - 1); assert(foo(-d, d) == d); } void main() { test0(); test1(); test2(); import core.stdc.stdio; printf("Success\n"); } ---
Comment #2 by ibuclaw — 2014-09-17T08:15:03Z
FYI, this looks more like an x87 problem, rather than compiler. And it's not just D that is affected by this. For instance, this bug in gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=323 Though they did eventually fix it in implementing C99-conformant (kinda) excess precision. http://marc.info/?l=gcc-patches&m=122576450613005&w=2
Comment #3 by ilyayaroshenko — 2014-09-17T08:21:57Z
No, (In reply to Iain Buclaw from comment #2) > FYI, this looks more like an x87 problem, rather than compiler. And it's > not just D that is affected by this. > > For instance, this bug in gcc: > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=323 > > > Though they did eventually fix it in implementing C99-conformant (kinda) > excess precision. > > http://marc.info/?l=gcc-patches&m=122576450613005&w=2 I have looked into disassembler code. It is Not problem with x87 0. dmd do this optimization d+=d and c} 1. float and double on 64bit are using xmm registers even in dmd. 2. It works fine with LDC.
Comment #4 by ilyayaroshenko — 2014-09-17T08:25:55Z
No, (In reply to Iain Buclaw from comment #2) > FYI, this looks more like an x87 problem, rather than compiler. And it's > not just D that is affected by this. > > For instance, this bug in gcc: > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=323 > > > Though they did eventually fix it in implementing C99-conformant (kinda) > excess precision. > > http://marc.info/?l=gcc-patches&m=122576450613005&w=2 I am sorry for previous not finished comment. I have looked into disassembler code. It is Not problem with x87 0. dmd do this optimization {d+=d; c+=d} But this is correct only when flags like -ffast-math was enabled. 1. float and double on 64bit are using xmm registers even in dmd. 2. It works fine with LDC.
Comment #5 by ilyayaroshenko — 2014-09-17T08:41:49Z
For example current std.math.log implementation (from CEPHES library if I am not wrong) needs IEEE754 standard. If there are bugs like current then log function can be incorrect. log2 contains code: z = x - 0.5; z -= 0.5; This should not be optimized. Am sorry for my English.
Comment #6 by ibuclaw — 2014-09-17T12:18:09Z
(In reply to Илья Ярошенко from comment #5) > For example current std.math.log implementation (from CEPHES library if I am > not wrong) needs IEEE754 standard. > > If there are bugs like current then log function can be incorrect. > > log2 contains code: > > z = x - 0.5; > z -= 0.5; > > This should not be optimized. > DMD does this: return yl2x(x, LN2); Which translates to: fyl2x ST1(x), ST(LN2)
Comment #7 by ilyayaroshenko — 2014-09-17T12:40:42Z
> DMD does this: > > return yl2x(x, LN2); > > Which translates to: > > fyl2x ST1(x), ST(LN2) My dump: dmd -O -m64 -c test7.d (fails with -run) objdump -d test7.o > test7.asm 0000000000000000 <_D5test710__T3fooTfZ3fooFNaNbNiNfffZf>: 0: 55 push %rbp 1: 48 8b ec mov %rsp,%rbp 4: f3 0f 10 e0 movss %xmm0,%xmm4 //xmm4 = xmm0 = d 8: f3 0f 10 d9 movss %xmm1,%xmm3 //xmm3 = xmm1 = c c: f3 0f 58 c4 addss %xmm4,%xmm0 //d+=d (incorrect) 10: f3 0f 58 d8 addss %xmm0,%xmm3 //c+=d 14: f3 0f 10 c3 movss %xmm3,%xmm0 // 18: 5d pop %rbp 19: c3 retq 1a: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1) And without optimization: dmd -m64 -c test7.d (Success with -run) objdump -d test7.o > test7.asm 0000000000000000 <_D5test710__T3fooTfZ3fooFNaNbNiNfffZf>: 0: 55 push %rbp 1: 48 8b ec mov %rsp,%rbp 4: 48 83 ec 10 sub $0x10,%rsp 8: f3 0f 11 4d f8 movss %xmm1,-0x8(%rbp) d: f3 0f 10 4d f8 movss -0x8(%rbp),%xmm1 12: f3 0f 58 c8 addss %xmm0,%xmm1 //c+=d 16: f3 0f 11 4d f8 movss %xmm1,-0x8(%rbp) 1b: f3 0f 10 55 f8 movss -0x8(%rbp),%xmm2 20: f3 0f 58 d0 addss %xmm0,%xmm2 //c+=d 24: f3 0f 11 55 f8 movss %xmm2,-0x8(%rbp) 29: f3 0f 10 45 f8 movss -0x8(%rbp),%xmm0 2e: c9 leaveq 2f: c3 retq
Comment #8 by yebblies — 2015-02-08T11:17:08Z
Please test your original code against this pull. https://github.com/D-Programming-Language/dmd/pull/4394
Comment #9 by github-bugzilla — 2015-02-14T04:23:53Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/5b10f22e044a89b66bbceebb00dd342987b9d29b Fix Issue 13485 - FP wrong-code with -O https://github.com/D-Programming-Language/dmd/commit/527b3cfc1b018feecd4ee8c037dda35f105ed853 Merge pull request #4394 from yebblies/issue13485 Issue 13485 - FP wrong-code with -O
Comment #10 by github-bugzilla — 2015-02-21T09:11:30Z