comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: GNAT Optimization of Constant Expressions
Date: Fri, 18 May 2007 12:40:16 -0500
Date: 2007-05-18T12:40:16-05:00	[thread overview]
Message-ID: <f2koa6$beh$1@jacob-sparre.dk> (raw)
In-Reply-To: u1whep09x.fsf@STRIPCAPStelus.net

"Ray Blaak" <rAYblaaK@STRIPCAPStelus.net> wrote in message
news:u1whep09x.fsf@STRIPCAPStelus.net...
> "Randy Brukardt" <randy@rrsoftware.com> writes:
> > One example is:
> >      Y := (X - 0.5) - 0.5;
> >
> > You might think that replacing this with
> >     Y := X - 1.0;
> > would be a good idea, but actually it reduces the accuracy of the result
>
> I don't understand this. Both 0.5 and 1.0 have "exact" binary
representations
> right?
>
> Can you give a value of X where this would hold?

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. This
sort of expression is common in trig. libraries (for instance, in a software
implementation of Sin). And a trig. library is the last place that you would
want a hair-brained optimizer losing accuracy!

It should be pointed out that on Intel hardware, the original condition does
not hold (extra precision *may* be kept between operations). However, it
still might hold if common subexpressions are preevaluated into memory (as
in the OP's program with the unused constants). Thus, the optimizer still
has to be wary of reordering (and most optimizers are designed to work on a
variety of hardware, not just Intel chips.).

                           Randy.







  reply	other threads:[~2007-05-18 17:40 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 [this message]
2007-05-18 22:51           ` Adam Beneschan
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