public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "Simon.Thornington at tssecurities dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/101479] New: vectorized impossible conditional floating point operations still cause traps (-ffast-math, -O3)
Date: Fri, 16 Jul 2021 20:48:56 +0000	[thread overview]
Message-ID: <bug-101479-4@http.gcc.gnu.org/bugzilla/> (raw)

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	other threads:[~2021-07-16 20:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-16 20:48 Simon.Thornington at tssecurities dot com [this message]
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

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