Branchless Ternary Operator

/
0 Comments
Branches, while faster every day, can be slow in some systems, especially in GPUs. By slow I mean slower than some arithmetic operations. If the branch is operating with numeric values, and assuming that boolean operations are convertible to 0 and 1, then it can be written as:
            a * Cond + b * (1-Cond) .
With a,b numbers and Cond a 1 or 0 representing true or false

I implemented two C++ versions of it, one that takes a boolean, and other that takes a function(can be a lambda, praised be C++11!)

Also, in HLSL or similar it can be
Take into account that that implementation is intended to mimic the logic of the C ternary operator : (cond)? a : b
The speed gain is really system-dependent, I have seen gains from 2 % to over 60 %, so see for yourself!


You may also like

No comments:

Pages

Powered by Blogger.