branchless code..
Branchless code, is much more efficient than otherwise, even on modern processors, despite branch prediction logic, simply because they still try and fetch instructions before they execute. that said, below is a branchless code to compute maximum and minimum of two nos.
int x; int y; /*the two nos*/*/
int r; /* the result goes here */
r = y + ((x - y) & -(x < y)); /* min(x, y) */
r = x - ((x - y) & -(x < y)); /* max(x, y)
ALU full throttle!





but you cant alwayz convert a branch(ed) code to a branchless one can you?
zakimirza
April 1, 2007
but you cant alwayz convert a branch(ed) code to a branchless one can you? hey and this theme of yours..isnt rlly showing up well on my IE6..:s
zakimirza
April 1, 2007
@zaki- yes of course, converting a branched code to branchless may not always be possible. true that.
thanks for the theme feedback…since i’ve not used any kind of CSS here, all i can do is buzz the guys at wordpress. it looks messy in IE 7 as well. I never realized this, since im better off with firefox
Nirmal Thacker
April 1, 2007
This is not branchless code. The expression (x>8*sizeof(int)-1 in place of -(x<y). In principle the compiler could do this for you but I know of no compiler that does.
One very nice application of this kind of branchless logic is to make a branchless binary search. If the number of iterations is constant you can even unroll the loop into a tiny fragment that still fits happily in L1 cache.
Rich
September 27, 2008