public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "roger at nextmovesoftware dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/103406] gcc -O0 behaves differently on "DBL_MAX related operations" than gcc -O1 and above
Date: Thu, 25 Nov 2021 19:40:16 +0000	[thread overview]
Message-ID: <bug-103406-4-CYHVdr6e9v@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-103406-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103406

Roger Sayle <roger at nextmovesoftware dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |NEW
           Assignee|roger at nextmovesoftware dot com  |unassigned at gcc dot gnu.org
            Summary|[12 Regression] gcc -O0     |gcc -O0 behaves differently
                   |behaves differently on      |on "DBL_MAX related
                   |"DBL_MAX related            |operations" than gcc -O1
                   |operations" than gcc -O1    |and above
                   |and above                   |
             Target|                            |x86_64

--- Comment #13 from Roger Sayle <roger at nextmovesoftware dot com> ---
The Inf - Inf => 0.0 regression should now be fixed on mainline.

Hmm.  As hinted by Richard Beiner's investigation, the underlying problem is
even more pervasive.  It turns out that on x86/IA64 chips, floating point
addition is not commutative, i.e. x+y is not the same as y+x, as demonstrated
by the test program below:

#include <stdio.h>

const double pn = __builtin_nan("");
const double mn = -__builtin_nan("");

__attribute__ ((noinline, noclone))
double plus(double x, double y)
{
  return x + y;
}

int main()
{
  printf("%lf\n",plus(pn,mn));
  printf("%lf\n",plus(mn,pn));
  return 0;
}

Output:
nan
-nan

Unfortunately, GCC assumes almost everywhere the FP addition is commutative
and (as per comments #8 and #9) associative with negation/minus.  This appears
to be target property, c.f. libgcc's _FP_CHOOSENAN, but could in theory be
resolved by a -fstrict-math mode (that implies -ftrapping-math) that disables
commutativity (swapping of operands) throughout the compiler, including
reload/fold-const etc., on affected Intel-like targets.
Perhaps this PR is a duplicate now that the regression has been fixed?

  parent reply	other threads:[~2021-11-25 19:40 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-24 11:09 [Bug c/103406] New: " shaohua.li at inf dot ethz.ch
2021-11-24 11:21 ` [Bug c/103406] " pinskia at gcc dot gnu.org
2021-11-24 11:23 ` ubizjak at gmail dot com
2021-11-24 11:29 ` pinskia at gcc dot gnu.org
2021-11-24 11:54 ` rguenth at gcc dot gnu.org
2021-11-24 12:01 ` rguenth at gcc dot gnu.org
2021-11-24 12:02 ` rguenth at gcc dot gnu.org
2021-11-24 12:03 ` [Bug middle-end/103406] [12 Regression] " rguenth at gcc dot gnu.org
2021-11-24 12:29 ` roger at nextmovesoftware dot com
2021-11-24 13:02 ` rguenth at gcc dot gnu.org
2021-11-24 13:04 ` rguenth at gcc dot gnu.org
2021-11-24 13:05 ` rguenth at gcc dot gnu.org
2021-11-24 22:22 ` joseph at codesourcery dot com
2021-11-25 19:04 ` cvs-commit at gcc dot gnu.org
2021-11-25 19:40 ` roger at nextmovesoftware dot com [this message]
2021-11-25 20:29 ` [Bug middle-end/103406] " joseph at codesourcery dot com
2021-11-26 17:58 ` roger at nextmovesoftware 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-103406-4-CYHVdr6e9v@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.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).