In Python, raising a number to some power is very concise - x ** p. In C++, there is no special operator, but std::pow(x, p) does the job as well. Unfortunately, even with -O3, a seemingly trivial snippet
results in a somewhat suboptimal assembly
std::pow overloads operate on floating point types, so our integer is converted to long double before calling pow function. But what if we don’t need floating point support? We can write our own power function. It can look something like
and produce a much nicer
that uses a logarithmic number of multiplications to raise x to the power of N. In fact, with constexpr it’s so easy and fun to do, that it’s hard to find any excuse not to :)