The pow () function is usually implemented in the math library, possibly using special instructions in the target processor, for x86 see How: pow (real, real) in x86 . However, instructions like fyl2x and f2xm1 are not fast, so all this can take 100 CPU cycles. For performance reasons, a compiler such as gcc provides “built-in” functions that provide a reduction in strength to enable faster calculations in special cases. When power N is an integer (as in your case) and small (as in your case), it’s faster to multiply N times than by calling a library function.
To detect cases where power is an integer, the math library provides overloaded functions, such as double pow(double,int) . You will see that gcc converts
double x = std::pow(y,4);
internally in 2 multiplications, which is much faster than calling the library, and gives the exact integer result you would expect when both operands are integers
double tmp = y * y; double x = tmp * tmp;
to get this kind of power reduction you have to
include <cmath>
- compile with -O2 optimization
- call the pow function in the library explicitly
std::pow() to make sure you get the version, not one of math.h
Then you will correspond to the pow overloaded function in <cmath>, which looks like this
inline double pow(double __x, int __i) { return __builtin_powi(__x, __i); }
Note that this function is implemented using __builtin_powi , which knows the decrease in the power of pow () to multiply when the power is a small integer.
source share