public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded
@ 2012-03-15  6:00 bugdal at aerifal dot cx
  2012-03-15  9:58 ` [Bug target/52593] " rguenth at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: bugdal at aerifal dot cx @ 2012-03-15  6:00 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52593

             Bug #: 52593
           Summary: Builtin sqrt on x86 is not correctly rounded
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: bugdal@aerifal.cx


The builtin sqrt() on x86 (i387) should be disabled except with -ffast-math
because it is not correctly rounded. For example, sqrt(0x1.fffffffffffffp-1)
yields 1 instead of 0x1.fffffffffffffp-1. Using -fno-builtin-sqrt will give the
correct value assuming your C library/libm is correctly rounded.

Unfortunately bugs like this seem endemic in gcc. I would really like to see
all dubious builtins and other dubious floating point optimizations disabled
except with -ffast-math until somebody takes the time to rigorously test them
and prove their correctness.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/52593] Builtin sqrt on x86 is not correctly rounded
  2012-03-15  6:00 [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded bugdal at aerifal dot cx
@ 2012-03-15  9:58 ` rguenth at gcc dot gnu.org
  2012-03-15 17:25 ` bugdal at aerifal dot cx
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-03-15  9:58 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52593

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING
   Last reconfirmed|                            |2012-03-15
     Ever Confirmed|0                           |1

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-03-15 09:51:25 UTC ---
Which GCC version did you test?  Please provide a compilable testcase that can
be executed and shows the error.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/52593] Builtin sqrt on x86 is not correctly rounded
  2012-03-15  6:00 [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded bugdal at aerifal dot cx
  2012-03-15  9:58 ` [Bug target/52593] " rguenth at gcc dot gnu.org
@ 2012-03-15 17:25 ` bugdal at aerifal dot cx
  2012-03-15 20:21 ` dominiq at lps dot ens.fr
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bugdal at aerifal dot cx @ 2012-03-15 17:25 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52593

--- Comment #2 from Rich Felker <bugdal at aerifal dot cx> 2012-03-15 16:37:25 UTC ---
Tested with gcc 4.6.2.

#include <stdio.h>
#include <math.h>
int main()
{
    volatile double x = 0x1.fffffffffffffp-1;
    volatile double y = sqrt(x);
    printf("%a\n", y);
}

Compile with -O2 -ffloat-store, and this program gives an output of 0x1p+0,
rather than the correct output of 0x1.fffffffffffffp-1. Unfortunately it's
impossible to get the correct output with glibc's -lm version of sqrt either
since it has the exact same bug. But you can look at the generated assembly
with and without -fno-builtin-sqrt and see that the version with builtin sqrt
is wrong (using the fsqrt opcode directly).


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/52593] Builtin sqrt on x86 is not correctly rounded
  2012-03-15  6:00 [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded bugdal at aerifal dot cx
  2012-03-15  9:58 ` [Bug target/52593] " rguenth at gcc dot gnu.org
  2012-03-15 17:25 ` bugdal at aerifal dot cx
@ 2012-03-15 20:21 ` dominiq at lps dot ens.fr
  2012-03-16  2:24 ` bugdal at aerifal dot cx
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: dominiq at lps dot ens.fr @ 2012-03-15 20:21 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52593

--- Comment #3 from Dominique d'Humieres <dominiq at lps dot ens.fr> 2012-03-15 19:24:53 UTC ---
On x86_64-apple-darwin10 (default '-mfpmath=sse'), I get '0x1.fffffffffffffp-1'
for all the revisions I have tested (from 4.4 to 4.8) unless I compile the test
with '-mfpmath=387'.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/52593] Builtin sqrt on x86 is not correctly rounded
  2012-03-15  6:00 [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded bugdal at aerifal dot cx
                   ` (2 preceding siblings ...)
  2012-03-15 20:21 ` dominiq at lps dot ens.fr
@ 2012-03-16  2:24 ` bugdal at aerifal dot cx
  2012-03-16 11:00 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bugdal at aerifal dot cx @ 2012-03-16  2:24 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52593

--- Comment #4 from Rich Felker <bugdal at aerifal dot cx> 2012-03-15 23:53:51 UTC ---
Of course. This bug is 387-math-specific.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/52593] Builtin sqrt on x86 is not correctly rounded
  2012-03-15  6:00 [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded bugdal at aerifal dot cx
                   ` (3 preceding siblings ...)
  2012-03-16  2:24 ` bugdal at aerifal dot cx
@ 2012-03-16 11:00 ` rguenth at gcc dot gnu.org
  2012-03-16 14:49 ` bugdal at aerifal dot cx
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-03-16 11:00 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52593

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
             Target|                            |i?86-*-*
             Status|WAITING                     |NEW
            Version|unknown                     |4.6.4

--- Comment #5 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-03-16 10:53:12 UTC ---
Confirmed.  I think sqrt is special (compared to sin, cos, etc.) because it's
one of the core IEEE arithmetic functions.  I suppose correct rounding
is only ensured for 80bit long double.

It will of course be an unexpected performance drop for most people with
no additional benefit as the libm implementation is wrong as well :/


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/52593] Builtin sqrt on x86 is not correctly rounded
  2012-03-15  6:00 [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded bugdal at aerifal dot cx
                   ` (4 preceding siblings ...)
  2012-03-16 11:00 ` rguenth at gcc dot gnu.org
@ 2012-03-16 14:49 ` bugdal at aerifal dot cx
  2012-04-28 23:22 ` bugdal at aerifal dot cx
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bugdal at aerifal dot cx @ 2012-03-16 14:49 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52593

--- Comment #6 from Rich Felker <bugdal at aerifal dot cx> 2012-03-16 14:23:09 UTC ---
The 387 FPU ensures correct rounding for the currently selected precision mode,
which per the ABI is always extended precision.

As for the usefulness of fixing this, I found the bug while working on my
correct sqrt implementation in musl libc, because despite the existence of a
correct version of the function, I was still getting wrong results. It turned
out gcc was replacing it with a buggy builtin. I don't think "glibc gets it
wrong anyway" is a reason not to fix the problem, especially now that glibc
seems to be under new maintainership and actually fixing longstanding WONTFIX
bugs.

Folks who just care about speed and want to throw correctness away should
already be using -ffast-math and similar.

Actually since this bug is rounding-related, perhaps it would suffice to make
-frounding-math turn off the builtin sqrt when using 387 math...?


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/52593] Builtin sqrt on x86 is not correctly rounded
  2012-03-15  6:00 [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded bugdal at aerifal dot cx
                   ` (5 preceding siblings ...)
  2012-03-16 14:49 ` bugdal at aerifal dot cx
@ 2012-04-28 23:22 ` bugdal at aerifal dot cx
  2012-04-29  0:17 ` joseph at codesourcery dot com
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bugdal at aerifal dot cx @ 2012-04-28 23:22 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52593

--- Comment #7 from Rich Felker <bugdal at aerifal dot cx> 2012-04-28 23:21:51 UTC ---
This bug seems to have been fixed with the addition of the
-fexcess-precision=standard feature, which is now set by default with -std=c99
or c11, and which disables the builtin sqrt based on 387 fsqrt. So apparently
it had already been fixed at the time I reported this, but I was unaware of the
right options to enable the fix and did not even think to try just using
-std=c99.

Note that for buggy libm (including glibc's), the fact that gcc has fixed the
issue will not fix the incorrect results, since the code in libm makes exactly
the same mistake gcc was making. But at least it's possible to fix it there.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/52593] Builtin sqrt on x86 is not correctly rounded
  2012-03-15  6:00 [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded bugdal at aerifal dot cx
                   ` (6 preceding siblings ...)
  2012-04-28 23:22 ` bugdal at aerifal dot cx
@ 2012-04-29  0:17 ` joseph at codesourcery dot com
  2012-04-29  1:22 ` bugdal at aerifal dot cx
  2014-03-10 22:36 ` david.heidelberger at ixit dot cz
  9 siblings, 0 replies; 11+ messages in thread
From: joseph at codesourcery dot com @ 2012-04-29  0:17 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52593

--- Comment #8 from joseph at codesourcery dot com <joseph at codesourcery dot com> 2012-04-29 00:16:38 UTC ---
If you have a bug in glibc's libm, please make sure there is an open bug 
report for it in glibc Bugzilla, component "math"; I don't see anything 
there about sqrt.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/52593] Builtin sqrt on x86 is not correctly rounded
  2012-03-15  6:00 [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded bugdal at aerifal dot cx
                   ` (7 preceding siblings ...)
  2012-04-29  0:17 ` joseph at codesourcery dot com
@ 2012-04-29  1:22 ` bugdal at aerifal dot cx
  2014-03-10 22:36 ` david.heidelberger at ixit dot cz
  9 siblings, 0 replies; 11+ messages in thread
From: bugdal at aerifal dot cx @ 2012-04-29  1:22 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52593

--- Comment #9 from Rich Felker <bugdal at aerifal dot cx> 2012-04-29 01:21:59 UTC ---
Reported to glibc bug tracker as bug #14032:

http://sourceware.org/bugzilla/show_bug.cgi?id=14032


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/52593] Builtin sqrt on x86 is not correctly rounded
  2012-03-15  6:00 [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded bugdal at aerifal dot cx
                   ` (8 preceding siblings ...)
  2012-04-29  1:22 ` bugdal at aerifal dot cx
@ 2014-03-10 22:36 ` david.heidelberger at ixit dot cz
  9 siblings, 0 replies; 11+ messages in thread
From: david.heidelberger at ixit dot cz @ 2014-03-10 22:36 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52593

David Heidelberger (okias) <david.heidelberger at ixit dot cz> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |david.heidelberger at ixit dot cz

--- Comment #10 from David Heidelberger (okias) <david.heidelberger at ixit dot cz> ---
fixed in glibc


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2014-03-10 22:36 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-15  6:00 [Bug target/52593] New: Builtin sqrt on x86 is not correctly rounded bugdal at aerifal dot cx
2012-03-15  9:58 ` [Bug target/52593] " rguenth at gcc dot gnu.org
2012-03-15 17:25 ` bugdal at aerifal dot cx
2012-03-15 20:21 ` dominiq at lps dot ens.fr
2012-03-16  2:24 ` bugdal at aerifal dot cx
2012-03-16 11:00 ` rguenth at gcc dot gnu.org
2012-03-16 14:49 ` bugdal at aerifal dot cx
2012-04-28 23:22 ` bugdal at aerifal dot cx
2012-04-29  0:17 ` joseph at codesourcery dot com
2012-04-29  1:22 ` bugdal at aerifal dot cx
2014-03-10 22:36 ` david.heidelberger at ixit dot cz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).