Description
A mibbit user noted in #rust-beginners
that the integer pow
method with a base of two generates far worse code than a shift: https://godbolt.org/g/mE1wjH (the shift is not equivalent for n >= 64, but you can see that it would still be better code if you accounted for that)
Perhaps we can special case the base 2 and use a shift in that case? One complication is that pow
is subject to overflow checks: Whether it panics or returns 0 for excessive exponents depends on whether overflow checks are enabled, and I don't know of a way to branch depending on whether they are enabled (which is required for this optimization to not change behavior).
Alternatively, better LLVM optimizations could help, but this might be a bit too much to ask.