Boost logo

Boost :

From: Anatoli Tubman (anatoli_at_[hidden])
Date: 2001-09-01 07:09:22


On Thursday 30 August 2001 21:25, George Heintzelman wrote:
> So, I tested this code (which would look very similar to what a
> compiler would need to do for an implicit conversion between inches and
> centimeters) with the two compilers readily available to me: g++-2.95
> and Sun CC 6u1, both on a Solaris 2.8 machine:
>
> #include <iostream>
>
> int main() {
> const double x = 0.0254;
> const double y = 0.01;
>
> double inches;
> std::cin >> inches;
>
> std::cout << inches*(x/y) << std::endl;
> }
>
> g++ (-O3 optimization) successfully hoisted the floating-point division
> x/y out into a predefined constant, and removed all reference to the
> space for x and y proper. CC (-xO3 optimization), on the other hand,
> did not manage to optimize that out, instead carrying out the division
> at runtime, although it allocated x and y in a global space rather than
> on the stack (not really a relevant optimization here, but nice to
> know).

Whoa! I must have been testing something else. Nice to see it works.

> The same results were obtained when x and y were taken out and
> implemented as inline functions instead (both compilers generated
> isomorphic code to their versions with the const double variables, as
> far as I could tell, though I'm not an assembler expert).

I'm not an expert either, but if you use -fsimple=[1|2] with Sun CC,
there will be no fdiv(d) instruction in the generated code.
This option breaks IEEE 754 conformance. (man(1) is your friend.)

> When the parentheses around (x/y) were removed, g++ did *not* optimize
> out the multiplication -- correctly, I believe, as IEEE
> multiplication/division are not associative, unlike the pure
> mathematical form. So we would have to take care that all floating
> point arithmetic with constants was carried out before applying it to
> the user's variable, if at all possible, to enable this optimization.
> That is probably also necessary to avoid unnecessary overflows, anyway.

Aha. I see. I was testing a non-parenthesed equivalent of your code.

-- 
anatoli at ptc dot com -- opinions are mine

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk