I have just been doing some benchmarks with std::pow for double
s. For the case that the exponent is an integer value (although coded as a double) (std::pow(a, 2.0)
, std::pow(a, 3.0)...
I found that std::pow
is much slower than just doing the multiplications when compiling with -O3
std::pow(a, 2.0) // a*a
std::pow(a, 3.0) // a*a*a
std::pow(a, 4.0) // a*a*a*a
...
except for the case of 2.0, which behaves exactly the same and produces the same binary code when compiling with -O3.
Why is the compiler optimizing the 2.0 case but not being able to compile the 3.0 case as aaa?
When compiling with -O3 -ffast-math I get exactly the same binary code for 2.0 and 3.0 (havent tested above). How is this?
Edit: this question has been marked as duplicate, which I dont agrede. First of all, this is about c++, and the related question is about c. I am not asking why pow(a, 3.0) is slower than aaa, but about the differences between pow and multiplications and why in power of 2 they produce exactly the same code, but not for other exponents.