title: Efficient Neon Implementation Of Clipping
body: <p>Within a loop i have to implement a sort of <strong>clipping</strong></p>
  <pre><code>if ( isLast )
  {
      val = ( val &lt; 0 ) ? 0 : val;
      val = ( val &gt; 255 ) ? 255 : val;        
  }
  </code></pre>
  <p>However this &quot;clipping&quot; takes up almost <strong>half the time of execution</strong> of the loop in Neon .
  This is what the whole <strong>loop</strong> looks like-</p>
  <pre><code>for (row = 0; row &lt; height; row++)
  {
    for (col = 0; col &lt; width; col++)
    {
        Int sum;
        //...Calculate the sum
     
        Short val = ( sum + offset ) &gt;&gt; shift;
        if ( isLast )
        {
             val = ( val &lt; 0 ) ? 0 : val;
             val = ( val &gt; 255 ) ? 255 : val;        
        }
        dst[col] = val;
     }
  
  }
  </code></pre>
  <p>This is how the <strong>clipping</strong> has been implemented in <strong>Neon</strong></p>
  <pre><code>     cmp       %10,#1                           //if(isLast)         
       bne       3f                                         
       vmov.i32   %4, d4[0]                       //put val in %4
       cmp       %4,#0                            //if( val &lt; 0 )
       blt       4f                               
       b         5f                               
       4:                                         
       mov       %4,#0                             
       vmov.i32   d4[0],%4                        
       5:                                         
       cmp       %4,%11                          //if( val &gt; maxVal )
       bgt       6f                               
       b         3f                               
       6:                                         
       mov       %4,%11                            
       vmov.i32   d4[0],%4                       
       3:  
  
       
  </code></pre>
  <p>This is the mapping of variables to registers-</p>
  <pre><code>isLast-    %10
  maxVal-    %11
  </code></pre>
  <p>Any suggestions to make it faster ?
  Thanks</p>
  <h1>EDIT-</h1>
  <p>The <strong>clipping now</strong> looks like-</p>
  <pre><code>     &quot;cmp       %10,#1                            \n\t&quot;//if(isLast)      
       &quot;bne       3f                                \n\t&quot;          
       &quot;vmin.s32   d4,d4,d13                        \n\t&quot;
       &quot;vmax.s32   d4,d4,d12                        \n\t&quot;
       &quot;3:                                          \n\t&quot; 
  
  //d13 contains maxVal(255)
  //d12 contains 0
  </code></pre>
  <p>Time consumed by this portion of the code has dropped from <strong>223ms to 18ms</strong></p>