Jump to page: 1 2
Thread overview
[Issue 13485] FP wrong-code with -O
Sep 17, 2014
Iain Buclaw
Sep 17, 2014
Iain Buclaw
Sep 17, 2014
Iain Buclaw
Feb 08, 2015
yebblies
September 16, 2014
https://issues.dlang.org/show_bug.cgi?id=13485

Илья Ярошенко <ilyayaroshenko@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code

--
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13485

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw@gdcproject.org

--- Comment #1 from Iain Buclaw <ibuclaw@gdcproject.org> ---
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");
}
---

--
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13485

--- Comment #2 from Iain Buclaw <ibuclaw@gdcproject.org> ---
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

--
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13485

--- Comment #3 from Илья Ярошенко <ilyayaroshenko@gmail.com> ---
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.

--
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13485

--- Comment #4 from Илья Ярошенко <ilyayaroshenko@gmail.com> ---
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.

--
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13485

--- Comment #5 from Илья Ярошенко <ilyayaroshenko@gmail.com> ---
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.

--
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13485

--- Comment #6 from Iain Buclaw <ibuclaw@gdcproject.org> ---
(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)

--
September 17, 2014
https://issues.dlang.org/show_bug.cgi?id=13485

--- Comment #7 from Илья Ярошенко <ilyayaroshenko@gmail.com> ---

> 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

--
September 18, 2014
https://issues.dlang.org/show_bug.cgi?id=13485

Илья Ярошенко <ilyayaroshenko@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=13474

--
February 08, 2015
https://issues.dlang.org/show_bug.cgi?id=13485

yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
                 CC|                            |yebblies@gmail.com

--- Comment #8 from yebblies <yebblies@gmail.com> ---
Please test your original code against this pull.

https://github.com/D-Programming-Language/dmd/pull/4394

--
« First   ‹ Prev
1 2