This is the mail archive of the gcc-help@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: Floating point optimizations


The general concept of optimizations is to preserve the answer while reducing the time needed to compute the answer. There are some cases in which an optimization fails to preserve the answer, but those usually occur in complex situations where the designer of the optimization had no practical way to avoid the difference.

As you noted c-(c-a) is routinely different from a. Anyone designing optimizations would understand that. So making that optimization would violate the general principle of optimization.

I don't know of any gcc option that might turn on that questionable "optimization". I doubt that there is one, but I'm not certain there isn't.


Christian Keil wrote:


Hi,

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.

Cheers,
Christian

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

int
main(void)
{
 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);
}
------8<--------





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