Do all 3 computations. Stick the result in a 3 element array. Use non branching arithmetic to find the correct array index. Return that result.
Ie,
bool icheck = i > 10;
bool zero = icheck & (a > b);
bool one = !icheck & (b > a);
bool two = !zero & !one;
int idx = one | (two << 1);
return val[idx];
Where val
holds the result of the three computations. The use of &
instead of &&
is important.
This removes your branch prediction problems. Finally, make sure the looping code can see the implementation, so the call overhead can be eliminated.