/* TESTED WITH GCC 12.2.0* 1) By default the compiler assumes exceptions can occur. Compile with:* gcc -O3 -lm test_fpexcept.c* run ./a.out prints "0 0"* * 2) Now tell the compiler no exceptions will occur, to enable further optimizations. Compile with:* gcc -O3 -fno-trapping-math -lm test_fpexcept.c* run ./a.out generates a spurious exception.*/#define _GNU_SOURCE#include<stdio.h>#include<fenv.h>#define N 1000intl[N];floatl_2[N];voidinit(int*x,intn){for(inti=0;i<n;i++)x[i]=i*(i+1);x[100]=0;// add some zeros at random placesx[30]=0;}intmain(){init(l,N);// -fno-trapping-math basically tells the compiler (among other things) we will NOT call the following line:feenableexcept(FE_DIVBYZERO|FE_INVALID|FE_OVERFLOW);// turn this on to track NaNs or other floating-point exceptions (requires gcc, #define _GNU_SOURCE and #include <fenv.h>)// This loop can be vectorized when compiled with -fno-trapping-mathfor(inti=0;i<N;i++){intii=l[i];l_2[i]=(ii==0)?0.0f:1.0f/ii;// avoid division by 0 !}printf("%g %g\n",l_2[30],l_2[100]);// do something with data to avoid eliminating all code.}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.