public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "vincent-gcc at vinc17 dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/102129] New: -ftrapping-math is broken or badly documented
Date: Mon, 30 Aug 2021 14:32:45 +0000	[thread overview]
Message-ID: <bug-102129-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 102129
           Summary: -ftrapping-math is broken or badly documented
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent-gcc at vinc17 dot net
  Target Milestone: ---

Testcase:

void g (void);
double f (double x)
{
  double r = x * 5.0;
  g ();
  return r;
}

In the generated code with -O1 and above, one can see that the multiplication
is done after g is called, even if -ftrapping-math is used (which is the
default). This is incorrect as x * 5.0 may trap, terminating the program with a
floating-point exception, in which case g must not be executed.

There is a similar discussion in PR29186 (which is actually about flag
reading), and -ftrapping-math is also mentioned in PR34678 ("I suspect that if
-ftrapping-math actually implemented everything required for the floating-point
exceptions aspects of FENV_ACCESS, [...]").

The STDC FENV_ACCESS pragma is not implemented, but by definition of
-ftrapping-math, it should imply some behavior similar to this pragma being on.
This bug is different from PR34678 because not everything from this pragma
needs to be implemented, just a subset of the "when a program might [...] run
under non-default floating-point control modes." part. This seems to be the
whole purpose of this option.

Note also that https://gcc.gnu.org/wiki/FloatingPointMath doesn't mention a
limitation for traps (only for the change of the rounding mode).

For the reference, the current documentation:

  -fno-trapping-math

    Compile code assuming that floating-point operations cannot generate
user-visible traps. These traps include division by zero, overflow, underflow,
inexact result and invalid operation. This option requires that
-fno-signaling-nans be in effect. Setting this option may allow faster code if
one relies on “non-stop” IEEE arithmetic, for example.

    This option should never be turned on by any -O option since it can result
in incorrect output for programs that depend on an exact implementation of IEEE
or ISO rules/specifications for math functions.

    The default is -ftrapping-math.

(from https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html).

             reply	other threads:[~2021-08-30 14:32 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-30 14:32 vincent-gcc at vinc17 dot net [this message]
2021-08-30 15:00 ` [Bug middle-end/102129] " vincent-gcc at vinc17 dot net
2021-08-30 19:49 ` joseph at codesourcery dot com
2021-08-31  0:37 ` vincent-gcc at vinc17 dot net
2021-08-31  7:12 ` rguenth at gcc dot gnu.org
2021-08-31  7:18 ` rguenth at gcc dot gnu.org
2021-08-31  8:25 ` rguenth at gcc dot gnu.org
2021-08-31  8:26 ` rguenth at gcc dot gnu.org
2021-08-31  8:49 ` rguenth at gcc dot gnu.org
2021-08-31  9:38 ` cvs-commit at gcc dot gnu.org
2021-08-31  9:38 ` rguenth at gcc dot gnu.org

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-102129-4@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).