Typically, to use math functions such as sqrt() , pow() , etc., you need to link to the math library, libm (on gcc, use the -lm option).
However, the compiler can apply optimization, especially when function arguments are constants. Consider the following two simplified functions:
double f1(int pressure) { return pow((float) pressure / PRESSURE0, 1/5.255F); } double f2(int pressure) { return pow((float) pressure / PRESSURE0, 1.0F); }
This compiles to i386 code as follows (unnecessary assembler directives removed):
f1: pushl %ebp movl %esp, %ebp subl $24, %esp ; (float) pressure / PRESSURE0 fildl 8(%ebp) fldl .LC0 fdivrp %st, %st(1) ; pow() fldl .LC1 fstpl 8(%esp) fstpl (%esp) call pow leave ret
f2: pushl %ebp movl %esp, %ebp ; (float) pressure / PRESSURE0 fildl 8(%ebp) fldl .LC0 fdivrp %st, %st(1) ; NO call to pow(x, 1.0), since it would not change the value of x popl %ebp ret
In f2() , since the exponent of pow() is 1.0 , there is no need to call the pow() function - the compiler detects this and removes the function call. Therefore, if there are no other calls to any mathematical functions in your code, there is no need to refer to libm in this particular case.
see also
source share