public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug math/29193] New: sincos produces a different output than sin/cos
@ 2022-05-27 16:53 dangelog at gmail dot com
  2022-05-27 19:33 ` [Bug math/29193] " schwab@linux-m68k.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: dangelog at gmail dot com @ 2022-05-27 16:53 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=29193

            Bug ID: 29193
           Summary: sincos produces a different output than sin/cos
           Product: glibc
           Version: 2.31
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: math
          Assignee: unassigned at sourceware dot org
          Reporter: dangelog at gmail dot com
  Target Milestone: ---

Hello,

Please consider this reduced testcase:

    #define _GNU_SOURCE
    #include <math.h>
    #include <stdio.h>

    double degreesToRadians(double degrees)
    {
        return degrees * (M_PI / 180);
    }

    double __attribute__ ((noinline)) a(double rad)
    {
        return sin(rad);
    }

    double __attribute__ ((noinline)) b(double rad)
    {
        double s;
        double c;
        sincos(rad, &s, &c);
        if (c > 100)
            return 0.0;
        return s;
    }

    int main() 
    {
        double d = 297;
        double rad = degreesToRadians(d);

        printf("%a\n%a\n", a(rad), b(rad));
    }


Godbolt: https://c.godbolt.org/z/z45GGWEEK

Under GCC 12.1 -O2 -fno-builtin, the output is:

-0x1.c83201d3d2c6ep-1
-0x1.c83201d3d2c6dp-1


In other words, sincos seems to be introducing ~1ULP of error when compared to
calling sin/cos separately. 

This is annoying, because GCC has a specific optimization where it replaces
nearby sin/cos calls with a call to sincos (slightly modified example here:
https://c.godbolt.org/z/rYEhrcPcx ). However, by doing so, it actually changes
the results. As sincos is not documented to be any different from sin+cos, the
optimization is correct, in principle. (If, on the other hand, sincos could
generate different results than sin/cos, then GCC must stop employing such
optimization without -ffast-math or similar flags).

-- 
You are receiving this mail because:
You are on the CC list for the bug.

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

* [Bug math/29193] sincos produces a different output than sin/cos
  2022-05-27 16:53 [Bug math/29193] New: sincos produces a different output than sin/cos dangelog at gmail dot com
@ 2022-05-27 19:33 ` schwab@linux-m68k.org
  2022-05-27 20:05 ` schwab@linux-m68k.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: schwab@linux-m68k.org @ 2022-05-27 19:33 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=29193

--- Comment #1 from Andreas Schwab <schwab@linux-m68k.org> ---
Which specific optimized sin function are you using?  With __sin_avx I cannot
reproduce that.  Note that sincos always uses a non-optimized sin/cos
computation.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

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

* [Bug math/29193] sincos produces a different output than sin/cos
  2022-05-27 16:53 [Bug math/29193] New: sincos produces a different output than sin/cos dangelog at gmail dot com
  2022-05-27 19:33 ` [Bug math/29193] " schwab@linux-m68k.org
@ 2022-05-27 20:05 ` schwab@linux-m68k.org
  2022-05-27 22:50 ` dangelog at gmail dot com
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: schwab@linux-m68k.org @ 2022-05-27 20:05 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=29193

--- Comment #2 from Andreas Schwab <schwab@linux-m68k.org> ---
I cannot reproduce that with __sin_sse2 either.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

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

* [Bug math/29193] sincos produces a different output than sin/cos
  2022-05-27 16:53 [Bug math/29193] New: sincos produces a different output than sin/cos dangelog at gmail dot com
  2022-05-27 19:33 ` [Bug math/29193] " schwab@linux-m68k.org
  2022-05-27 20:05 ` schwab@linux-m68k.org
@ 2022-05-27 22:50 ` dangelog at gmail dot com
  2022-05-28  9:03 ` dangelog at gmail dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: dangelog at gmail dot com @ 2022-05-27 22:50 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=29193

--- Comment #3 from Giuseppe D'Angelo <dangelog at gmail dot com> ---
Hello,

I'm actually not sure; running gdb on the binary and stepping through
instructions leads me to __sin_fma:

(gdb)  bt
#0  __sin_fma (x=5.1836278784231586) at ../sysdeps/ieee754/dbl-64/s_sin.c:202
#1  0x00005555555550d9 in main () at test.c:30

This is on Ubuntu 20.04 on a Broadwell CPU.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

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

* [Bug math/29193] sincos produces a different output than sin/cos
  2022-05-27 16:53 [Bug math/29193] New: sincos produces a different output than sin/cos dangelog at gmail dot com
                   ` (2 preceding siblings ...)
  2022-05-27 22:50 ` dangelog at gmail dot com
@ 2022-05-28  9:03 ` dangelog at gmail dot com
  2022-05-31 11:37 ` dangelog at gmail dot com
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: dangelog at gmail dot com @ 2022-05-28  9:03 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=29193

--- Comment #4 from Giuseppe D'Angelo <dangelog at gmail dot com> ---
As a data point, if I run the testcase under 

GLIBC_TUNABLES=glibc.cpu.hwcaps=-AVX2_Usable 

then I get identical results:

-0x1.c83201d3d2c6dp-1
-0x1.c83201d3d2c6dp-1

With that environment variable, I end up into __sin_avx.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

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

* [Bug math/29193] sincos produces a different output than sin/cos
  2022-05-27 16:53 [Bug math/29193] New: sincos produces a different output than sin/cos dangelog at gmail dot com
                   ` (3 preceding siblings ...)
  2022-05-28  9:03 ` dangelog at gmail dot com
@ 2022-05-31 11:37 ` dangelog at gmail dot com
  2022-05-31 11:47 ` fweimer at redhat dot com
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: dangelog at gmail dot com @ 2022-05-31 11:37 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=29193

--- Comment #5 from Giuseppe D'Angelo <dangelog at gmail dot com> ---
Hello,

In a nutshell, there are a few things crossing each other:

1) `__sin_fma` returns slightly different results than `__sin_avx` or
`__sin_sse2`. I don't think that this is a problem at all, but I might be
wrong, I just lack the necessary knowledge about what glibc specifically
promises here.

2) `sincos` seems to be consistent with `__sin_sse2`, and therefore
inconsistent with `__sin_fma`. Is this inconsistency OK? Should it be
documented?

3) In general, this opens the question: within the very same "environment"
(same process, CPU, libm, fpenv, etc.), is there any guarantee that `sin(x)`
always  yields the very same result for identical values of `x`? Is `sin(x)`
meant to be perfectly reproducible? If yes, should GCC stop turning pairs of
`sin(x)` and `cos(x)` calls into one call to `sincos`?

-- 
You are receiving this mail because:
You are on the CC list for the bug.

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

* [Bug math/29193] sincos produces a different output than sin/cos
  2022-05-27 16:53 [Bug math/29193] New: sincos produces a different output than sin/cos dangelog at gmail dot com
                   ` (4 preceding siblings ...)
  2022-05-31 11:37 ` dangelog at gmail dot com
@ 2022-05-31 11:47 ` fweimer at redhat dot com
  2022-06-01  8:32 ` schwab@linux-m68k.org
  2022-06-02  9:29 ` dangelog at gmail dot com
  7 siblings, 0 replies; 9+ messages in thread
From: fweimer at redhat dot com @ 2022-05-31 11:47 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=29193

Florian Weimer <fweimer at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fweimer at redhat dot com

-- 
You are receiving this mail because:
You are on the CC list for the bug.

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

* [Bug math/29193] sincos produces a different output than sin/cos
  2022-05-27 16:53 [Bug math/29193] New: sincos produces a different output than sin/cos dangelog at gmail dot com
                   ` (5 preceding siblings ...)
  2022-05-31 11:47 ` fweimer at redhat dot com
@ 2022-06-01  8:32 ` schwab@linux-m68k.org
  2022-06-02  9:29 ` dangelog at gmail dot com
  7 siblings, 0 replies; 9+ messages in thread
From: schwab@linux-m68k.org @ 2022-06-01  8:32 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=29193

Andreas Schwab <schwab@linux-m68k.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|UNCONFIRMED                 |RESOLVED
   Target Milestone|---                         |2.36

--- Comment #6 from Andreas Schwab <schwab@linux-m68k.org> ---
Fixed in 2.36.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

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

* [Bug math/29193] sincos produces a different output than sin/cos
  2022-05-27 16:53 [Bug math/29193] New: sincos produces a different output than sin/cos dangelog at gmail dot com
                   ` (6 preceding siblings ...)
  2022-06-01  8:32 ` schwab@linux-m68k.org
@ 2022-06-02  9:29 ` dangelog at gmail dot com
  7 siblings, 0 replies; 9+ messages in thread
From: dangelog at gmail dot com @ 2022-06-02  9:29 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=29193

--- Comment #7 from Giuseppe D'Angelo <dangelog at gmail dot com> ---
Thank you very much!

-- 
You are receiving this mail because:
You are on the CC list for the bug.

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

end of thread, other threads:[~2022-06-02  9:29 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-27 16:53 [Bug math/29193] New: sincos produces a different output than sin/cos dangelog at gmail dot com
2022-05-27 19:33 ` [Bug math/29193] " schwab@linux-m68k.org
2022-05-27 20:05 ` schwab@linux-m68k.org
2022-05-27 22:50 ` dangelog at gmail dot com
2022-05-28  9:03 ` dangelog at gmail dot com
2022-05-31 11:37 ` dangelog at gmail dot com
2022-05-31 11:47 ` fweimer at redhat dot com
2022-06-01  8:32 ` schwab@linux-m68k.org
2022-06-02  9:29 ` dangelog at gmail dot com

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).