• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: why use pow(x, 2)?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: why use pow(x, 2)?


  • Subject: Re: why use pow(x, 2)?
  • From: Greg Parker <email@hidden>
  • Date: Mon, 2 Nov 2009 12:22:34 -0800

On Nov 2, 2009, at 12:03 PM, Stephen J. Butler wrote:
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.

The Libc implementations don't check, but the compiler does. It has special knowledge of some functions including pow(), and will substitute it's own code if it will be faster.


This is easy to test empirically. In this simple case, the compiler does optimize pow(x, 2) directly to a single-instruction x*x.

% cat test.c
#include <math.h>
int main(int argc, char **argv) {
    return pow(argc, 2);
}
% cc -O3 test.c -o - -S
[...]
_main:
LFB17:
	pushq	%rbp				// build stack frame
LCFI0:
	movq	%rsp, %rbp			// build stack frame
LCFI1:
	cvtsi2sd	íi, %xmm0		// convert int argc to float
	mulsd	%xmm0, %xmm0		// pow(argc, 2)
	cvttsd2si	%xmm0, êx		// convert float->int for return
	leave
	ret


-- Greg Parker email@hidden Runtime Wrangler


_______________________________________________

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


  • Follow-Ups:
    • Re: why use pow(x, 2)?
      • From: Chris Williams <email@hidden>
References: 
 >why use pow(x, 2)? (From: Chunk 1978 <email@hidden>)
 >Re: why use pow(x, 2)? (From: Jens Alfke <email@hidden>)
 >Re: why use pow(x, 2)? (From: Chunk 1978 <email@hidden>)
 >Re: why use pow(x, 2)? (From: Ed Wynne <email@hidden>)
 >Re: why use pow(x, 2)? (From: Luke the Hiesterman <email@hidden>)
 >Re: why use pow(x, 2)? (From: "Stephen J. Butler" <email@hidden>)

  • Prev by Date: Re: why use pow(x, 2)?
  • Next by Date: Re: Core Data not fulfill a fault (now: observer) after Save As
  • Previous by thread: Re: why use pow(x, 2)?
  • Next by thread: Re: why use pow(x, 2)?
  • Index(es):
    • Date
    • Thread