wraparound_counter & operator ++() {
m_count = (m_count + 1) % upper_limit;
/*if (upper == m_count)
m_count = lower;
++m_count;*/
return *this;
}
It is my understanding that on some systems using the remainder operator trick will be faster, but on others the conditional will be faster. Is there any way to figure out which will be faster at compile-time (or run-time)?
For most platforms, you can assume that the conditional will be faster. This is because most modern architecture where branch mispredictions are expensive have some form of conditional move instruction, which the compiler will utilize to perform the requested check & assignment. For example, my gcc translates this:
into this for x86_64 (and x86):
(cmove is available since PentiumPro.)
This comes out for ARM-thumb:
All ARMs have conditional execution.
And so. To wrap it up: You’d need an architecture with
to end up with the modulo trick as being the fastest. **If* there is such an architecture, then please tell me about it. I’m always interested in such things.