To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here

http://justlinux.com/   Fri, 03-Sep-2010 14:37:34 GMT
         internet.com

Go Back   JustLinux Forums > Community Help: Check the Help Files, then come here to ask! > Programming/Scripts

Programming/Scripts Perl, Python, C/C++, Java, CGI, shell, etc. Share your code!

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 02-05-2009, 12:23 AM
gamblor01 gamblor01 is offline
Sr. Muerto
 
Join Date: Jan 2003
Location: Austin, Texas
Posts: 676
gcc and pow in a C program

I found some interesting behavior when discussing the pow function from Math.h with someone. I am well aware of the need to compile with the -lm flag, but to my surprise this actually isn't necessary if the arguments to the pow function are literals. For example, this program compiles in gcc without using -lm:

Code:
#include <stdio.h>
#include <math.h>

int main()
{
  double b = 2.0;
  double e = 3.0;
  double bCubed = pow(2.0, 3.0);

  printf("%f cubed is %f\n", b, bCubed);
  return 0;
}
If I try to change the call to any of these:

Code:
  double bCubed = pow(2.0, e);
Code:
  double bCubed = pow(b, 3.0);
Code:
  double bCubed = pow(b, e);
Then the -lm option is required or else it can't compile.

So does anyone know why it's necessary to use -lm when the argument(s) is/are variable(s) but it works fine if both arguments are literal values?

As a side note, any of the above compile with g++ however. Also, if it helps, this is the version of gcc I'm using:

gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)

This doesn't happen on my Mac however that is running: i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5484)
__________________
"The author of that poem is either Homer or, if not Homer, somebody else of the same name."

Last edited by gamblor01; 02-05-2009 at 12:26 AM.
Reply With Quote
  #2  
Old 02-05-2009, 01:58 AM
bwkaz bwkaz is offline
Moderator
 
Join Date: Apr 2001
Location: SF Bay Area, CA
Posts: 14,950
This is a wild guess, but I would not be surprised if gcc was inlining the call to pow() when you gave it two literal values. (Alternately, if gcc wasn't inlining anything, but was just omitting the call to pow() and setting eCubed to 8.0 directly: doing the computation at compile time.) If there's no function call, then there's no need for the library.

If you know assembly, you can probably disassemble the output file and see whether there's a function call happening at that point in the code...
__________________
"Quando omni flunkus moritati" -- Possum Lodge motto -- Loose translation: "When all else fails, play dead."

Registered Linux User #219692
Please take a look at the Posting Guidelines, to help us help you!
Reply With Quote
  #3  
Old 02-06-2009, 04:36 PM
gamblor01 gamblor01 is offline
Sr. Muerto
 
Join Date: Jan 2003
Location: Austin, Texas
Posts: 676
I knew I could count on you bwkaz! You're right -- this particular version of GCC was being sneaky (also known as "optimizing" the code)!!

I simply put the code with literals inside of foo.c and the code with variables inside of foo2.c and then used gcc -S on both of them. Notice that there is no call to the pow function inside of foo.s while there is inside of foo2.s:

Code:
% grep 'call' foo*.s
foo2.s:	call	pow
foo2.s:	call	printf
foo.s:	call	printf
Thanks for the tip!
__________________
"The author of that poem is either Homer or, if not Homer, somebody else of the same name."
Reply With Quote
  #4  
Old 02-08-2009, 02:23 PM
bwkaz bwkaz is offline
Moderator
 
Join Date: Apr 2001
Location: SF Bay Area, CA
Posts: 14,950
Heh -- I was looking at a completely unrelated thing the other day (MPFR), and found a list of software using it: one of the programs listed was GCC. They linked to the GCC bug that tracked this optimization, and also the changelog entry talking about it:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29335
http://gcc.gnu.org/gcc-4.3/changes.html#mpfropts

The second link in particular should be relevant. Of course, you already figured this out, so whatever.
__________________
"Quando omni flunkus moritati" -- Possum Lodge motto -- Loose translation: "When all else fails, play dead."

Registered Linux User #219692
Please take a look at the Posting Guidelines, to help us help you!
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 10:37 AM.

Help File Library
Troubleshooting
Software
Modems
Games
Tools
More



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
Free Newsletter
Server Daily


More Free Newsletters




internet.commerce
Be a Commerce Partner












Linux is a trademark of Linus Torvalds.

Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.7.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.