comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: GNAT Optimization of Constant Expressions
Date: 18 May 2007 15:51:27 -0700
Date: 2007-05-18T15:51:27-07:00	[thread overview]
Message-ID: <1179528686.946797.189500@w5g2000hsg.googlegroups.com> (raw)
In-Reply-To: <f2koa6$beh$1@jacob-sparre.dk>

On May 18, 10:40 am, "Randy Brukardt" <r...@rrsoftware.com> wrote:

> I'm not a numerical analysist, I only play at one when implementing
> compilers... ;-) but let me give my understanding.
>
> If the machine does not keep extra precision between operations (that is,
> the floating point registers are the same size as the in-memory
> representation), and abs X is < 1.0, then
>     (X - 0.5) - 0.5
> will have one more bit of precision than
>     X - 1.0
> will. That occurs because in order to add or subtract, the smaller value has
> to be shifted to match the larger value, which will cause bits to fall off
> the end of the smaller value. 1.0 requires more shifting than 0.5 does.

I tried it with an imaginary machine that supports only an 8-bit
mantissa, so that floating-point numbers have the form 1.abcdefgh *
(2**exponent).  Suppose X has an exponent of -4, so that the value of
X is essentially 0.0001abcdefgh.  Now, when we subtract 1.0, and we
line things up, the efgh bits will fall off, giving

       0.0001abcd
     - 1.00000000

with the result being negative (0.1110a'b'c'd' + .00000001); I'm using
' to indicate the complement of a bit.  To normalize the result, it
needs to be shifted one bit to the left; the question is, what bit
gets shifted in on the right?  Is it related to "e", or is it always
0, or what?  I don't know that much about the details of floating-
point hardware, so I don't know the answer to this.

If we subtract 0.5, then only fgh will fall off:

       0.0001abcde
     - 0.100000000  (since there will be 8 bits of mantissa after the
first 1)

with the result being negative (0.0110a'b'c'd'e' + 0.000000001).  As
before, when we normalize this result, some other bit will be shifted
in on the right.  Then, since this is negative, when we subtract 0.5
again we're really adding 0.5; when we line things up, whatever bit
just got shifted in, will be shifted out again:

      (0.0110a'b'c'd'e' + 0.000000001)
     + 0.10000 0 0 0 0

giving 0.1110a'b'c'd'e' + 0.000000001.

So I suspect Randy is right, but I'm not sure.  If the hardware keeps
just one extra mantissa bit internally when it does the subtraction,
and uses the extra bit when it shifts left to normalize the result,
then I think the two expressions will always produce the same result.

I could have easily screwed up the math in the above.  Hopefully I
didn't.

                      -- Adam




  reply	other threads:[~2007-05-18 22:51 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-16 22:37 GNAT Optimization of Constant Expressions David Smith
2007-05-17  4:50 ` Randy Brukardt
2007-05-17 20:03   ` Gautier
2007-05-17 20:46     ` Randy Brukardt
2007-05-17 22:49       ` David Smith
2007-05-17 23:38         ` Randy Brukardt
2007-05-18  7:58           ` Dmitry A. Kazakov
2007-05-18 11:27           ` Georg Bauhaus
2007-05-18 17:28             ` Randy Brukardt
2007-05-18 16:25       ` Ray Blaak
2007-05-18 17:40         ` Randy Brukardt
2007-05-18 22:51           ` Adam Beneschan [this message]
2007-05-19  2:44             ` Randy Brukardt
2007-05-18  9:05   ` Markus E Leypold
2007-05-18  9:47   ` Florian Weimer
2007-05-18 11:32     ` Duncan Sands
2007-05-18 17:20     ` Randy Brukardt
2007-05-18 17:35       ` Duncan Sands
     [not found]       ` <200705181935.23877.baldrick@free.fr>
2007-05-18 17:49         ` Duncan Sands
2007-05-17  5:30 ` Martin Krischik
2007-05-18  9:56 ` Duncan Sands
2007-05-18 15:39   ` David Smith
2007-05-18 17:08     ` Duncan Sands
     [not found]     ` <200705181908.54920.baldrick@free.fr>
2007-05-18 17:32       ` Duncan Sands
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox