This is the mail archive of the gcc@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: FP negation optimization


On Sun, Feb 01, 2004 at 05:13:33PM -0800, Geoff Keating wrote:
> Chris Lattner <sabre@nondot.org> writes:
> 
> > On Sun, 1 Feb 2004, Richard Henderson wrote:
> > 
> > > On Sun, Feb 01, 2004 at 02:50:31AM -0600, Chris Lattner wrote:
> > > > Is '-0.0 - X' always guaranteed to be the same as '-X' with IEEE math?
> > >
> > > No.  X == +0.0 differs.
> > 
> > Are you sure?  It seems to work for me, at least on X86:
> > 
> > void test(double X) {
> >   printf("%f %f\n", -0.0 - X, -X);
> > }
> > int main() {
> >   test(+0.0);
> >   test(-0.0);
> > }
> 
> I looked this up.  '-0.0 - X' is the same as '-0.0 + (-X)'.  When -X
> != 0.0, the result will be -X, so that's OK.  Suppose X is +/- 0.0.
> Then the following rules apply:
> 
> - If both operands have the same sign, the sign of the result is the
>   same as the sign of the operands.  So if X is +0.0, you'll get -0.0,
>   the same as -X.
> 
> - If the sum of two operands with opposite sign is exactly zero, the
>   sign is positive in all rounding modes except round towards -Inf, in
>   which case the sign is negative.  So, if X is -0.0, you'll get
>   +0.0, the same as -X, *unless* we're rounding towards -Inf.
> 
> Thus, the answer to your question is "no".  The case that differs is
> when X is -0.0 and the rounding mode is towards -Inf.

Indeed, but doesn't constant folding assume that the rounding mode
is round to nearest (at least for C, I believe that Ada has different
rules) ?

At least a quick look at fold-const.c, real.c, real.h shows that 
while do_add and friends return a boolean telling whether the result
is inexact or not, the returned value is ignored. 

So the optimization suggested by Chris would be consistent with the
way constant folding works. Or am I opening a whole new can of worms?

	Regards,
	Gabriel


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