Alexander Monakov wrote:
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).
This transformation is indeed included into -ffast-math. I checked with
$ gcc --version
gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52)
and it does eliminate the calculation. What does generated assembly code look
like in your case? Note you may as well check on this code:
double f(double a, double c)
{
Normally, when such expressions are written in source code, there is a
reasonable expectation that algebraic simplification will not be performed
across parentheses. That was one of the major changes between K&R style
and the C89 standard, which adopted a convention which has been in Fortran
since the f66 standard. -ffast-math, or similar aggressive options in
other compilers, are of diminished practical value without the ability to
separate violation of parentheses from other optimizations.