Efficient min() function in SSE
<p>I have the following loop that takes the square root of each entry in an array:</p>

<pre><code>#include &lt;mmintrin.h&gt;

float array[SIZE];
for (int i = 0; i &lt; SIZE; i += 4)
{
    __m128 fourFloats, fourRoots;
    fourFloats = _mm_load_ps(&amp;array[i]);
    fourRoots = _mm_sqrt_ps(fourFloats);
    float results[4];
    _mm_store_ps(results, fourRoots);
    // This is bottleneck
    array[i]   = results[0] &gt; 63.0F ? 63.0F : floor(results[0]);
    array[i+1] = results[1] &gt; 63.0F ? 63.0F : floor(results[1]);
    array[i+2] = results[2] &gt; 63.0F ? 63.0F : floor(results[2]);
    array[i+3] = results[3] &gt; 63.0F ? 63.0F : floor(results[3]);
    // This is slower
//  array[i] = (int) std::min(floor(results[0]), 63.0F);
}
</code></pre>

<p>According to my profiler (<a href="http://www.rotateright.com/" rel="nofollow">Zoom</a>) the square roots take no significant amount of time, but each of the four clipping of the results take about 20% of the time each, even with <code>-O2</code> optimisation on. Is there a more efficient way to implement the loop? Note that <code>_mm_store_ps()</code> gets optimised out by <code>gcc</code>.</p>

<p>I tried an optimised table lookup of the square root as 97% of the input <code>array</code> values are under 512, but that didn't help. Note that this routine takes just under a quarter of the total processor time for my complete application, a constantly-running image recognition application.</p>