[Bug tree-optimization/101479] vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3)

2021-07-16 Thread Simon.Thornington at tssecurities dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101479

--- Comment #4 from Simon Thornington  ---
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...

[Bug c/101479] vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3)

2021-07-16 Thread Simon.Thornington at tssecurities dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101479

--- Comment #1 from Simon Thornington  ---
x_not_zero -> could_be_zero sorry

[Bug c/101479] New: vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3)

2021-07-16 Thread Simon.Thornington at tssecurities dot com via 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 
#include 
#include 
#include 
#include 

#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