public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dangelog at gmail dot com" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug math/29193] New: sincos produces a different output than sin/cos
Date: Fri, 27 May 2022 16:53:51 +0000	[thread overview]
Message-ID: <bug-29193-131@http.sourceware.org/bugzilla/> (raw)

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.

             reply	other threads:[~2022-05-27 16:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-27 16:53 dangelog at gmail dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-29193-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).