Here\'s some code (full program follows later in the question):
template
T fizzbuzz(T n) {
T count(0);
#if CONST
const T d
Is there a known way of wrapping an int such that the compiler can discard the wrapping when optimising?
Try passing WrappedInt
by value. Then WrappedInt
s can be passed in registers. Pass-by-const-reference sometimes forces gcc to fall back to the stack for argument passing.
About the int
vs const int
slowdown, I can only speculate that GCC is trying to play it safe in the face of aliasing. Basically, if it cannot prove that div
is not an alias for another, more accessible variable, it cannot turn it into a constant. If you declare it const, GCC assumes it's not aliased and performs the conversion into a constant. Apart from the idivl
, you should also see a memory fetch, once, when entering the function, as opposed to immediate values being used for the const
case.