Avoiding+Branches

toc
 * Home * Programming * Optimization * Avoiding Branches**

Miss-predicted [|branches] causes huge penalties on todays super [|pipelined] processors. While processors become smarter to predict branches with several heuristics, branches on random data should be avoided.

The techniques shown here often use arithmetical shift right (by bit-width - 1, that is 31 for 32-bit double words as integers) to determine a mask of sign-bits, either all bits set (-1) or all bits clear 0. x86 compiler may emit an cdq (Convert Double to Quad) instruction, which sign extends a 32 bit register to two 32 bit registers. Since arithmetical shift right is not strictly specified in C, it might be not portable through all compilers and architectures. Note that in C, a comparison or a boolean expression with the result {false, true} might be treated as numerical value {0, 1}.

=Abs, Max, Min= It is recommend to use functions provided by the programming language. In C or C++ one should use appropriate compiler intrinsics and/or template functions provided by the C Runtime Library or Standard Template Library.

The tricks shown here, might be useful if compiler don't support those functions or don't generate the intended branchless assembly and the input is quite random, so that the branch prediction heuristics will fail often. 

Absolute value of an Integer
Abs as C intrinsic is likely implemented based on following code snippet ... code format="cpp" int abs(int a) { int s = a >> 31; // cdq, signed shift, -1 if negative, else 0 a ^= s; // ones' complement if negative a -= s; // plus one if negative -> two's complement if negative return a; } code ... by compilers, on x86 a sequence three instructions: {cdq, xor, sub} or {cdq, add, xor}.



By CRT
Microsoft Visual C Run-Time Library provides a _max macro.

By Sign-Mask
Following trick only works for a reduced integer range of effectively one bit less, which is most often no problem for 32-bit integers in chess programs, like scores and that like: INT_MIN <= a - b <= INT_MAX : If a is greater b, a - 0 is returned, otherwise a - (a - b) == +b code format="cpp" int max(int a, int b) { int diff = a - b; int dsgn = diff >> 31; return a - (diff & dsgn); } code



By CRT
Microsoft Visual C Run-Time Library provides a _min macro.

By Sign-Mask
Following trick only works for a reduced integer range of effectively one bit less, which is most often no problem for 32-bit integers in chess programs, like scores and that like: INT_MIN <= a - b <= INT_MAX : If a is greater b, b + 0 is returned, otherwise b + (a - b) == +a code format="cpp" int min(int a, int b) { int diff = a - b; int dsgn = diff >> 31; return b + (diff & dsgn); } code

=Conditional Expressions=

Conditional Assignment
A conditional assignment in C or C++ may be implemented by compilers as x86 conditional move (cmovCC) instruction. code format="cpp" x = ( a > b ) ? C : D; code otherwise it might be reformulated with conditional increment: code format="cpp" x = D; if ( a > b ) x += C - D; code

Conditional Increment
If a > b is hard to predict, code format="cpp" if ( a > b ) x += C; code it might be reformulated branch-less in C, which likely emits a x86 setCC instruction: code format="cpp" x += -( a > b ) & C; // with any boolean expression code With a reduced value range and INT_MIN <= b - a <= INT_MAX, greater and less relations might be implemented using a sign mask: code format="cpp" x += (( b - a ) >> 31) & C; code 

Conditional Write
During list generation, while conditionally writing data to an array with post-incrementing a pointer or index, one may try to avoid the conditional branch by storing always and to increment the pointer by the condition, which is either 0 or 1. code format="cpp" if (a > b) *ptr++ = value; code might be rewritten by code format="cpp" *ptr = value; ptr += (a > b); code

=Indirect Branch= Robert Hyatt on x86 [|Branch predictor], [|Branch target predictor], and [|Indirect branch] in CCC :

=See also=
 * Bit-Twiddling
 * DirGolem
 * Profiling
 * Table-driven Move Generation

=Forum Posts=
 * [|branch misprediction] by Eric Oldre, CCC, July 14, 2004
 * [|Re: Fruit 2.0 Toga : Recapture extension] by Gerd Isenberg, CCC, March 19, 2005
 * [|Re: Function pointers hurt performance?] by Robert Hyatt, CCC, September 22, 2011
 * [|Branch-poor looping] by Harm Geert Muller, CCC, September 02, 2015
 * [|Mispredicted branch VS cache miss] by Natale Galioto, CCC, September 09, 2015
 * [|Tipical cache and branch misses for a chess engine] by Nicu Ionita, CCC, September 14, 2016 » Memory, Profiling

=External Links= > lineup: Joe Bowie, [|Ronny Drayton], [|Bill Bickford], Kim Clarke, [|John Mulkerin], [|Kenny Martin] > media type="youtube" key="NmPUCNyCtpU"
 * [|Compute the integer absolute value (abs) without branching] by [|Sean Eron Anderson]
 * [|Compute the minimum (min) or maximum (max) of two integers without branching] by [|Sean Eron Anderson]
 * [|Programming Optimization] by Paul Hsieh
 * [|Avoiding the Cost of Branch Misprediction - Intel® Software Network] by [|Rajiv Kapoor], February 20, 2009
 * [|Branch (computer science) from Wikipedia]
 * [|Branch table]
 * [|Indirect branch]
 * [|Conditional (programming)]
 * [|Branch predictor]
 * [|Branch target predictor]
 * Defunkt - [|Avoid The Funk], Live at [|Drom], April 14, 2010, [|YouTube] Video

=References= =What links here?= include page="Avoiding Branches" component="backlinks" limit="80"
 * Up one Level**