Related to this answer.
In the above answer, it's mentioned how you can avoid branch prediction fails by avoiding branches.
The user demonstrates this by replacing:
if (data[c] >= 128)
{
sum += data[c];
}
With:
int t = (data[c] - 128) >> 31;
sum += ~t & data[c];
How are these two equivalent (for the specific data set, not strictly equivalent)?
What are some general ways I can do similar things in similar situations? Would it always be by using >> and ~?
The trick here is that if
data[c] >= 128, thendata[c] - 128is nonnegative, otherwise it is negative. The highest bit in anint, the sign bit, is 1 if and only if that number is negative.>>is a shift that extends the sign bit, so shifting right by 31 makes the whole result 0 if it used to be nonnegative, and all 1 bits (which represents -1) if it used to be negative. Sotis0ifdata[c] >= 128, and-1otherwise.~tswitches these possibilities, so~tis-1ifdata[c] >= 128, and0otherwise.x & (-1)is always equal tox, andx & 0is always equal to0. Sosum += ~t & data[c]increasessumby0ifdata[c] < 128, and bydata[c]otherwise.Many of these tricks can be applied elsewhere. This trick can certainly be generally applied to have a number be
0if and only if one value is greater than or equal to another value, and-1otherwise, and you can mess with it some more to get<=,<, and so on. Bit twiddling like this is a common approach to making mathematical operations branch-free, though it's certainly not always going to be built out of the same operations;^(xor) and|(or) also come into play sometimes.