public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/101479] New: vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3)
@ 2021-07-16 20:48 Simon.Thornington at tssecurities dot com
  2021-07-16 20:52 ` [Bug c/101479] " Simon.Thornington at tssecurities dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Simon.Thornington at tssecurities dot com @ 2021-07-16 20:48 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101479
           Summary: vectorized impossible conditional floating point
                    operations still cause traps (-ffast-math, -O3)
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Simon.Thornington at tssecurities dot com
  Target Milestone: ---

On all versions of gcc I could test, a vectorized operation of the form 

y = could_be_zero ? 1.0 : (1.0 / x);

in a loop will still cause an FE_DIVBYZERO, even if x_not_zero is correctly set
to true whenever x could be 0.  

This is with -ffast-math and -O3.  With -fno-tree-vectorize it does not occur. 
If I make could_be_zero volatile, it does not occur.  If I insert a "compiler
fence" in the loop it also does not occur.  At -O2 it doesn't happen either.

Reproduction:

#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <fenv.h>

#define FPCHECK_FORMAT(fmt, ...) \
  do { \
    int flags = fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW); \
    if (flags) { \
        printf("Floating point exception(s) detected:%s%s%s: %s (%s:%d) " fmt,
\
               (flags & FE_INVALID) ? " FE_INVALID" : "", \
               (flags & FE_DIVBYZERO) ? " FE_DIVBYZERO" : "", \
               (flags & FE_OVERFLOW) ? " FE_OVERFLOW" : "", \
               __func__, \
               __FILE__, \
               __LINE__, ## __VA_ARGS__); \
        abort(); \
     } \
  } while(false) \


#define FPCHECK() FPCHECK_FORMAT("")
#define fpcheck(where) FPCHECK_FORMAT("%s", where)
#define compiler_fence() __asm__ __volatile__ ("" : : : "memory")

static bool close_to_zero(double x) {
    return fabs(x) < 0.5;
}

void f(double *x, double *y, int n) {
    fpcheck("before");
    for (int i=0; i<n; i++) {
        // making this volatile fixes it
        bool smol = close_to_zero(x[i]);
        // adding this compiler fence will fix it
        //compiler_fence();
        y[i] = smol ? 1.0 : (-1.0 / x[i]);
    }
    fpcheck("after");
}

int main(int argc, char *argv[]) {
    double x[8] = { atof(argv[1]), atof(argv[2]), atof(argv[3]), atof(argv[4]),
atof(argv[5]), atof(argv[6]), atof(argv[7]), atof(argv[8]) };
    double y[8];

    printf("before\n");
    f(x, y, 8);
    printf("after\n");
    return 0;
}

$ gcc -ffast-math -O3 -lm y.c && ./a.out 0 2 3 4 5 6 7 8
before
Floating point exception(s) detected: FE_DIVBYZERO: f (y.c:40) afterAborted

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

* [Bug c/101479] vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3)
  2021-07-16 20:48 [Bug c/101479] New: vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3) Simon.Thornington at tssecurities dot com
@ 2021-07-16 20:52 ` Simon.Thornington at tssecurities dot com
  2021-07-16 21:08 ` [Bug tree-optimization/101479] " pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Simon.Thornington at tssecurities dot com @ 2021-07-16 20:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Simon Thornington <Simon.Thornington at tssecurities dot com> ---
x_not_zero -> could_be_zero sorry

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

* [Bug tree-optimization/101479] vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3)
  2021-07-16 20:48 [Bug c/101479] New: vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3) Simon.Thornington at tssecurities dot com
  2021-07-16 20:52 ` [Bug c/101479] " Simon.Thornington at tssecurities dot com
@ 2021-07-16 21:08 ` pinskia at gcc dot gnu.org
  2021-07-16 21:09 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-16 21:08 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
-ffast-math enables -fno-trapping-math which means floating point will not
introduce any traps and assumes fetestexcept will not matter.

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

* [Bug tree-optimization/101479] vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3)
  2021-07-16 20:48 [Bug c/101479] New: vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3) Simon.Thornington at tssecurities dot com
  2021-07-16 20:52 ` [Bug c/101479] " Simon.Thornington at tssecurities dot com
  2021-07-16 21:08 ` [Bug tree-optimization/101479] " pinskia at gcc dot gnu.org
@ 2021-07-16 21:09 ` pinskia at gcc dot gnu.org
  2021-07-17  0:37 ` Simon.Thornington at tssecurities dot com
  2021-07-17 17:55 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-16 21:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
If you want -ffast-math and fetestexcept still, add -ftrapping-math to the end
of the command line.

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

* [Bug tree-optimization/101479] vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3)
  2021-07-16 20:48 [Bug c/101479] New: vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3) Simon.Thornington at tssecurities dot com
                   ` (2 preceding siblings ...)
  2021-07-16 21:09 ` pinskia at gcc dot gnu.org
@ 2021-07-17  0:37 ` Simon.Thornington at tssecurities dot com
  2021-07-17 17:55 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: Simon.Thornington at tssecurities dot com @ 2021-07-17  0:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Simon Thornington <Simon.Thornington at tssecurities dot com> ---
I'll add that changing close_to_zero from 

fabs(x) < 0.5

to

x == 0.0 || fabs(x) < 0.5

everything starts to work as I'd expect again...

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

* [Bug tree-optimization/101479] vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3)
  2021-07-16 20:48 [Bug c/101479] New: vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3) Simon.Thornington at tssecurities dot com
                   ` (3 preceding siblings ...)
  2021-07-17  0:37 ` Simon.Thornington at tssecurities dot com
@ 2021-07-17 17:55 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-17 17:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Simon Thornington from comment #4)
> I'll add that changing close_to_zero from 
> 
> fabs(x) < 0.5
> 
> to
> 
> x == 0.0 || fabs(x) < 0.5
> 
> everything starts to work as I'd expect again...

Yes because it is not vectorized.
Again if you want keep traps/exceptions use -ftrapping-math if you use -Ofast
or -ffast-math.

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

end of thread, other threads:[~2021-07-17 17:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-16 20:48 [Bug c/101479] New: vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3) Simon.Thornington at tssecurities dot com
2021-07-16 20:52 ` [Bug c/101479] " Simon.Thornington at tssecurities dot com
2021-07-16 21:08 ` [Bug tree-optimization/101479] " pinskia at gcc dot gnu.org
2021-07-16 21:09 ` pinskia at gcc dot gnu.org
2021-07-17  0:37 ` Simon.Thornington at tssecurities dot com
2021-07-17 17:55 ` pinskia at gcc dot gnu.org

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