Floating point optimizations

Christian Keil c.keil@tu-harburg.de
Thu Apr 3 17:00:00 GMT 2008


we are currently investigating some numerical algorithms and the claim
appeared that a C statement like
  x = c - (c - a);
would be easily transformed into
  x = a;
by the compiler. Now investigating this with a vanilla GCC 4.1.2 failed
to support the claim. Compiling the below program with -O3 -ffast-math
keeps the computation of x. The output shows x is different from a. The
question is, is there some compiler switch or the like to get GCC to
make the above transformation? I searched the docs but had the
impression that all relevant flags should be included in the above two
(especially ffast-math).

Please let me know if you need any additional information or if this is
the wrong list for this kind of question.


#include <stdio.h>
#include <stdlib.h>

  double a, x;
  double c;

  scanf("%lf", &a);
  c = ((1 << 27) + 1) * a;
  x = c - (c - a);

  printf("a = %e(%a)\n", a, a);
  printf("x = %e(%a)\n", x, x);

Christian Keil                        /"\
Institute for Reliable Computing      \ /    ASCII Ribbon Campaign
Hamburg University of Technology       X  against HTML email & vCards
mail:c.keil@tu-harburg.de             / \

More information about the Gcc-help mailing list