This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: GCC and Floating-Point


On 2005-05-26 20:49:13 +0200, Allan Sandfeld Jensen wrote:
> I can't speak of all compilers, only the ones I've tried. ICC
> enables it always, Sun CC, Dec CXX, and HP CC at certain levels of
> optimizations (equivalent to -O2).

I've tried with "cc: Sun WorkShop 6 2000/04/07 C 5.1", and there
doesn't seem to be unsafe optimizations. Here's an example:

#include <stdio.h>

int main (void)
{
  int i;

  for (i = 2; i >= 0; i--)
    {
      double x = (double) i;
      x = -x;
      printf ("%2g %2g\n", x, x + 0.0);
    }

  for (i = 0; i < 100; i += 30)
    {
      long double x = (long double) i;
      printf ("%.20Lg\n", x / 30.0);
    }

  return 0;
}

craffe:~/src/fp> cc -xO5 -o tst tst.c -lsunmath
craffe:~/src/fp> ./tst
-2 -2
-1 -1
-0  0
0
1
2
3

But with gcc, using -ffast-math:

craffe:~/src/fp> gcc -O -ffast-math -o tst tst.c
craffe:~/src/fp> ./tst
-2 -2
-1 -1
-0 -0
0
1
2
3

See the third line: -0 -0.

And indeed, icc does some unsafe optimizations by default, when
assuming IEEE-754, but nothing buggy...

latour:~/src/fp> /localdisk/icc/opt/intel_cc_80/bin/icc -o tst tst.c
latour:~/src/fp> ./tst
-2 -2
-1 -1
-0 -0
0
1
2
3

contrary to gcc:

latour:~/src/fp> gcc -O -ffast-math -o tst tst.c
latour:~/src/fp> ./tst
-2 -2
-1 -1
-0 -0
0
1
2
3.0000000000000000002

So, yes, -ffast-math by default would really be a bad idea and would
make gcc much worse than other compilers.

> > Standard should be the default.
> >
> > (Is this a troll or what?)
> 
> So why isn't -ansi or -pendantic default?

-ansi is broken.

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / SPACES project at LORIA


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]