These functions return bit-masks that can be and'd with results in other calculations, in order to perform operations without conditionals, and thus without introducing branches.
For example:
nz2mask
returns0
if the argument is0
, and0xffffffff
otherwise.msb2mask
returns0
if the top bit of the argument is0
, and0xffffffff
if it is1
.
So if you have code like (with x86 instructions for reference):
if(a != 0) x += y;
// test ebx,ebx
// je skip
// add dword ptr [x],eax
// skip:
You can replace it with:
x += y & (nz2mask(a));
// mov ecx,ebx
// neg ecx
// or ecx,ebx
// sar ecx,1Fh
// and ecx,eax
// add ecx,dword ptr [x]
It produces more instructions (at least on x86), but it avoids a branch.
Then there are additional functions like iselmask()
which allow the selection of either input based on the mask provided, so you could replace:
x = (a != 0) ? r1 : r2;
with
x = iselmask(nz2mask(a), r1, r2);
Again, these functions should inline and compile down to relatively efficient assembler, trading off a bit of extra maths for no branching.