nvcc compiler: auto convert clamp to branchless select?


Conditional statements such as:

int  i;

if( i < 0 ) i = 0;

can be written as branchless code:

int  i, sel;

sel  =  - (i > 0 );

      i    =  i & sel;

The same is true for other conditional statements such as:

int    i, i_max;

      float  f, f_max;

if( i > i_max ) i = i_max;

if( f < 0.0f )  f = 0.0f;

if( f > f_max ) f = f_max;

Question: Does the nvcc compiler automatically convert such conditionals into branchless statements?


Yes it does. It uses a SETP and then a predicated MOV, which actually has a higher throughput than the bitwise AND.