Re: why use pow(x, 2)?
Re: why use pow(x, 2)?
- Subject: Re: why use pow(x, 2)?
- From: "Stephen J. Butler" <email@hidden>
- Date: Mon, 2 Nov 2009 14:15:04 -0600
On Mon, Nov 2, 2009 at 2:03 PM, Stephen J. Butler
<email@hidden> wrote:
> On Mon, Nov 2, 2009 at 1:37 PM, Luke the Hiesterman <email@hidden> wrote:
>> Would it really be that much faster? I don't know exactly how pow() is
>> implemented, but I assume it's basically just a loop of multiplications, in
>> which case it would basically be the same as x*x in this case, since it
>> would exit after the first iteration....
>
> I scanned through, and while there are some simple cases these
> implementations check for, it doesn't appear x^2 is one of them. So
> pow(x,2) is likely to be much slower than x*x.
Oops... I didn't look far enough! Down a ways is this code:
//if y is an integer, less than 2**16, do iPow
if( 0 == yFracBits && fabsy <= 0x1.0p16f )
{
int32_t iy = y; //should be exact
int32_t yIsNeg = iy >> 31;
iy = abs( iy );
double dx = x;
double result = iy & 1 ? dx : 1.0;
while( iy >>= 1 )
{
dx *= dx;
if( iy & 1 )
result *= dx;
}
//We are using double precision here, so we don't need to
worry about range differences between tiny vs huge numbers for
negative Y
if( yIsNeg )
return (float) (1.0 / result);
return (float) result;
}
So it won't be much slower, but just a little slower (function call
overhead, plus all the work to get to this point).
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden