Floating point optimizations

Tim Prince TimothyPrince@sbcglobal.net
Sat Apr 5 19:05:00 GMT 2008

NightStrike wrote:
> On 4/5/08, Andrew Haley <aph@redhat.com> wrote:
>> Tim Prince wrote:
>>> 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.
>> There may well be such an expectation, but gcc only prevents re-association
>> across explicit parenthesis in FORTRAN, and it's only been doing that for
>> a few weeks.
> Wait, so are things like "c - (c - a)" optimzed down to "a" or not?  I
> use a lot of that in very time-intensive c++ code.
Yes, for C and C++ with -ffast-math.

More information about the Gcc-help mailing list