bit selection to replace if-then-else

Hi,

Does CUDA have a bit selection operator (or something similar) that enables you to implement an if-then-else structure, but doesn’t cause branch divergence?

I am looking for something similar to the Cell Broadband Engine’s bit-selection operator to implement something like:

if ( a>b)
c += 1
else
d = a+b

using code that has no branching:

select = spu_cmpgt(a,b)
c = spu_sel(c, c_plus_1, select) // Set “c” to either “c” or “c_plus_1”, based on value of “select”
d = spu_sel(d, a_plus_b, select) // Set “d” to either “d” or “a_plus_b”, based on value of “select”

cheers
steve

You can try using ‘res = expr ? aaa : bbb’ construct for which compiler seem to prefer using predicates instead of branching

I’ve tried following code

[codebox]

c = a>b ? c + 1 : c;

d = a > b ? d : a + b;

[/codebox]

and compiler generated code using selp instruction (selects one or another result based on the value in the predicate register - see ptx manual)

while your code seem to use branching.

Theoretically, compiler should automatically use predicates for small if-then-else constructs, but seems that it doesn’t want to do it either for a reason I don’t know about, or just it is not that mature yet.

Although for the following code

[codebox]

if (a>b)

c += 1;

if (a <=B)

d = a + b;

[/codebox]

compiler also generated predicated code. In your case I suppose compiler decides to use branching because it thinks it is faster than doing 2 predicated code paths. In general you should rely on compiler to bother about such small things, but as you can see, shuffling code around you can achieve the behavior you want.