title: CUDA /openCL; rewriting branches as non-branching expression
body: <p>Most of the time a branch is required in a CUDA or OpenCL program, like:</p>
  
  <pre><code>for (int i=0; i&lt;width; i++)
  {
     if( i % threadIdx.x == 0)
       quantity += i*i;
  }
  </code></pre>
  
  <p>the code can always (or at least, most of the time) be rewritten in non-branching style:</p>
  
  <pre><code>for (int i=0; i&lt;width; i++)
  {
     quantity += i*i* (i % threadIdx.x != 0);
  }
  </code></pre>
  
  <p>the tradeoff seems to be either running in a single warp slot versus doing more computations on all the threads (in the second case, the sum is executed always, just that sometimes the value is zero)</p>
  
  <p>Assuming that branching operations will take multiple warp slots for each possible branch, one would expect the second be consistently better than the first, Now my question is; Can i rely on the compiler to optimize 1) into 2) whenever it makes sense, or there is not a broadly-applicable criteria, which implies that it cannot be decided in general which one is better without trying and profiling?</p>