Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
[apple scitech] Problem with Optimization
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[apple scitech] Problem with Optimization



Hi,

I'm working for physical simulation with Xcode 2.4.1 on Tiger (10.4.11/PPC).

I put a famous code by Chris Lomont in my application. The code is for 1.0/sqrt(x). Actually it is:

template<class TYPE> TYPE InvSqrt( TYPE x0 ) {
float x = (float)x0;
float xhalf = 0.5*x;
unsigned long i = *(unsigned long*)&x; // get bits for floating value
printf("InvSqrt: %d / %e half is %e\n",i,x,xhalf);
i = 0x5f375a86L - (i>>1); // gives initial guess y0
x = *(float*)&i; // convert bits back to float
printf("InvSqrt: %d / %e\n",i,x);
x = x*(1.5-xhalf*x*x); // Newton step, repeating increases accuracy
x = x*(1.5-xhalf*x*x); // Newton step, repeating increases accuracy
printf("InvSqrt: %d / %e half is %e\n",i,x,xhalf);
return (TYPE)x;
}


The code works fine with -O1 but with -O2,-O3 and -Os it gives odd results.

The output of the code with -O1 is like
InvSqrt: 1083432276 / 4.620768e+00 half is 2.310384e+00
InvSqrt: 1055747036 / 4.637135e-01
InvSqrt: 1055747036 / 4.652035e-01 half is 2.310384e+00

But with -Os it gives,
InvSqrt: -266853528 / 3.263210e+01 half is 1.631605e+01
InvSqrt: -416593710 / -1.883196e+29
InvSqrt: -416593710 / -inf half is 1.631605e+01

My application works fine with -O1 but it would be better performance with higher optimization.
I have been searching compiler directive such as,
#pragma optimize("",off)
but I can't find such a directive for g++ so far.


I understand sometimes the optimization gives very strange result. But it would be nice for me there is a way to trick the compiler.

Any suggestion are really welcome.

Thank you so much.

ABE Hiroshi
 from Tokorozawa, JAPAN

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Scitech mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.