I don't know if it would end up being faster, though.
I don't know if it would end up being faster, though.
The general rule is to only use cmov if your test condition is mostly random.
In which case cmov would be relatively cheap since it isn't blocking execution of other instructions.
This used to be a problem on Pentium4 where cmov had high latency (4 cycles or more), but today, IIRC, a register-to-register cmov is only one cycle, so it is safe to use whenever a branch could have a non-trivial misprediction rate.
If you decrement your array index you don't even need the cmp instruction. The compiler could probably gen so good code.
if (__builtin_expect(index >= cap,0)) { ...
Ad was pointed out in the thread, recent cpus have reduced the latency of cmov to a cycle. So your result could also depend on your architecture.