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

Reply via email to