FP negation optimization
Geoff Keating
geoffk@geoffk.org
Mon Feb 2 01:13:00 GMT 2004
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.
--
- Geoffrey Keating <geoffk@geoffk.org>
More information about the Gcc
mailing list