Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Mac > Mac Programmer Help > Re: float vs. d...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 10 Topic 3779 of 3882
Post > Topic >>

Re: float vs. double

by MacRules <dodeca001@[EMAIL PROTECTED] > Jun 22, 2008 at 07:55 PM

<snip>

>
> // --- do float testing ---
>
> =A0 =A0 =A0 =A0 a_f =3D 1.6;
> =A0 =A0 =A0 =A0 b_f =3D -0.33;
> =A0 =A0 =A0 =A0 for (n=3D0; n<num_iter; n++) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 c_f =3D a_f + b_f;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 d_f =3D a_f - b_f;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 e_f =3D a_f * b_f;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 f_f =3D a_f / b_f;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 a_f +=3D 3.2;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 b_f -=3D 1.05;
>

<snip>

> // --- do double testing ---
>
> =A0 =A0 =A0 =A0 a_d =3D 1.6;
> =A0 =A0 =A0 =A0 b_d =3D -0.33;
> =A0 =A0 =A0 =A0 for (n=3D0; n<num_iter; n++) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 c_d =3D a_d + b_d;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 d_d =3D a_d - b_d;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 e_d =3D a_d * b_d;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 f_d =3D a_d / b_d;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 a_d +=3D 3.2;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 b_d -=3D 1.05; }
>

Sorry - stupid mistake. I took a look at the ASM code - the compiler
optimizer was smart enough to figure out that the variables inside
these loops aren't used for anything, and deleted them. However, the
optimizer wasn't smart enough to figure out that the resulting empty
FOR loops aren't doing anything, either, so it left these in.

I added some printf() calls to be bottom of the function, making all
of the variables above 'relevant' and thus forcing the optimizer to
leave the FOR loop contents intact:

printf("a_f b_f c_f d_f e_f f_f =3D %f %f %f %f %f %f
\n",a_f,b_f,c_f,d_f,e_f,f_f);
printf("a_d b_d c_d d_d e_d f_d =3D %lf %lf %lf %lf %lf %lf
\n",a_d,b_d,c_d,d_d,e_d,f_d);

I then verified that the compiler is now doing the requested
mathematical operations with floats and doubles.

The new results:

Number of iterations? 50000000

Duration float, double =3D 643 361 ms
a_f b_f c_f d_f e_f f_f =3D 67108864.000000 -33554432.000000
33554432.000000 100663296.000000 -2251799813685248.000000 -2.000000
a_d b_d c_d d_d e_d f_d =3D 160000001.592189 -52500000.293033
107499999.149156 212499997.635222 -8399999794475229.000000 -3.047619

I don't know why double would be twice as fast as float, considering
that the ASM code shows a separate instruction for +,-,*,/ for both
floats and doubles. Perhaps the double math instructions take few
internal processor cycles than the float instructions. In any case, my
original conclusion still holds: I should upgrade to double.

-Steve
 




 10 Posts in Topic:
float vs. double
MacRules <dodeca001@[E  2008-06-22 09:37:54 
Re: float vs. double
MacRules <dodeca001@[E  2008-06-22 19:55:39 
Re: float vs. double
Gregory Weston <uce@[E  2008-06-22 23:42:28 
Re: float vs. double
Reinder Verlinde <rein  2008-06-23 18:56:28 
Re: float vs. double
MacRules <dodeca001@[E  2008-06-23 05:31:21 
Re: float vs. double
MacRules <dodeca001@[E  2008-06-23 18:32:47 
Re: float vs. double
Korchkidu <korchkidu@[  2008-06-24 07:55:57 
Re: float vs. double
MacRules <dodeca001@[E  2008-06-24 09:39:46 
Re: float vs. double
Korchkidu <korchkidu@[  2008-06-29 08:14:36 
Re: float vs. double
glenn andreas <gandrea  2008-06-29 10:35:42 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Dec 5 10:29:01 CST 2008.