Subject: Re: gcc4 misoptimization
To: None <tech-toolchain@NetBSD.org>
From: Alan Barrett <apb@cequrux.com>
List: tech-toolchain
Date: 07/29/2006 20:05:17
[ Re compiler evaluating this using an extended precision:
		/* all variables are float */
                x += TWO52[s];
		x -= TWO52[s];
]
On Thu, 27 Jul 2006, Martin Husemann wrote:
> I think the compiler is allowed to do this optimization, and the volatile
> temporary you added is the right way to instruct it not to do so.

What part of the C99 standard gives the compiler permission to do
that?  Section 6.5 paragraph 8 seems to apply only to expressions, so
operations in different statements (separated by a sequence point) would
not appear to be covered there.  Section 5.2.4.2.2 paragraph 8 seems
unclear to me, but perhaps it can be read as allowing the compiler to
keep extended precision results across sequence points.

My experiments with gcc-4.1.2 in NetBSD/i386 seem to show that simply
casting the intermediate result to (float) is sufficient to prevent the
unwanted behaviour:

                x = (float)(x + TWO52[s]) - TWO52[s];

--apb (Alan Barrett)