public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/102126] New: Wrong optimization of multiplication by 1 and -1 with -ftrapping-math when an underflow is possible
@ 2021-08-30  9:11 vincent-gcc at vinc17 dot net
  2021-08-30  9:22 ` [Bug middle-end/102126] Wrong optimization of FP multiplication and division " vincent-gcc at vinc17 dot net
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2021-08-30  9:11 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102126
           Summary: Wrong optimization of multiplication by 1 and -1 with
                    -ftrapping-math when an underflow is possible
           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: ---

Without -fsignaling-nans, GCC optimizes the floating-point multiplication by 1
as a no-op, even when it may generate a trap due to underflow. This is wrong.

Testcase:

#define _GNU_SOURCE
#include <stdio.h>
#include <float.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON

double f (double x)
{
  return x * 1.0;
}

double g (double x)
{
  volatile double one = 1.0;
  return x * one;
}

int main (void)
{
  volatile double x = DBL_MIN / 2, y;

  feenableexcept (FE_UNDERFLOW);
  y = f(x);
  printf ("f: %d\n", y != 0);
  y = g(x);
  printf ("g: %d\n", y != 0);

  return 0;
}

Here, x is a subnormal, so that its multiplication by 1 signals an underflow.
Note that since the result is exact, the underflow flag is not raised under the
default exception handling, so that without -ftrapping-math and
-fsignaling-nans, f() may be optimized to no-op. With -fsignaling-nans, GCC
disables the optimization as expected. But with -ftrapping-math, it doesn't
(the optimization is done at any optimization level, even -O0); this can be
seen in the generated asm code, with -S.

When executing the code (with -O3 -ftrapping-math -lm):

f: 1
Floating point exception (core dumped)

showing that f() was incorrectly optimized, and that g(), which is not
optimized due to volatile, gives a floating-point exception as expected.

Similar issue with the multiplication by -1 (at least on x86_64), where GCC
just negates the value.

Bug reproducible from at least GCC 4.6 to GCC 11.2.0.

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

end of thread, other threads:[~2021-08-31 17:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-30  9:11 [Bug middle-end/102126] New: Wrong optimization of multiplication by 1 and -1 with -ftrapping-math when an underflow is possible vincent-gcc at vinc17 dot net
2021-08-30  9:22 ` [Bug middle-end/102126] Wrong optimization of FP multiplication and division " vincent-gcc at vinc17 dot net
2021-08-30  9:37 ` vincent-gcc at vinc17 dot net
2021-08-30 11:28 ` rguenth at gcc dot gnu.org
2021-08-30 13:17 ` vincent-gcc at vinc17 dot net
2021-08-30 19:38 ` joseph at codesourcery dot com
2021-08-30 19:55 ` joseph at codesourcery dot com
2021-08-31  0:51 ` vincent-gcc at vinc17 dot net
2021-08-31 17:58 ` joseph at codesourcery 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).