4 ms·
Note, if the three comparisons are sufficiently predicable, then your transformation would be _less_ efficient than the simple branches. A predicable branch is
by FullyFunctional 7y ago
Note, if the three comparisons are sufficiently predicable, then your transformation would be _less_ efficient than the simple branches.
A predicable branch is almost free in a modern processor.
- ncmncm 7y agoThree comparison/branch pairs are not free. Clang will optimize bool swap_if(bool c, int& a, int& b) { int ta = a, tb = b; a = (-c & tb)|((c-1) & ta); b = (-c & ta)|((c-1) & tb); return c; } into cmov instructions. Used in a quicksort partition loop as l += swap_if( *r <= pv, *l, *r); It makes quicksort fully 2x as fast as the usual branch.
- FullyFunctional 7y agoQuicksort comparisons are obviously not predictable