title: How can I speed-up this loop (in C)?
body: <p>I'm trying to parallelize a convolution function in C. Here's the original function which convolves two arrays of 64-bit floats:</p>
  
  <pre><code>void convolve(const Float64 *in1,
                UInt32 in1Len,
                const Float64 *in2,
                UInt32 in2Len,
                Float64 *results)
  {
      UInt32 i, j;
  
      for (i = 0; i &lt; in1Len; i++) {
          for (j = 0; j &lt; in2Len; j++) {
              results[i+j] += in1[i] * in2[j];
          }
      }
  }
  </code></pre>
  
  <p>In order to allow for concurrency (without semaphores), I created a function that computes the result for a particular position in the <code>results</code> array:</p>
  
  <pre><code>void convolveHelper(const Float64 *in1,
                      UInt32 in1Len,
                      const Float64 *in2,
                      UInt32 in2Len,
                      Float64 *result,
                      UInt32 outPosition)
  {
      UInt32 i, j;
  
      for (i = 0; i &lt; in1Len; i++) {
          if (i &gt; outPosition)
              break;
          j = outPosition - i;
          if (j &gt;= in2Len)
              continue;
          *result += in1[i] * in2[j];
      }
  }
  </code></pre>
  
  <p>The problem is, using <code>convolveHelper</code> slows down the code about 3.5 times (when running on a single thread).</p>
  
  <p>Any ideas on how I can speed-up <code>convolveHelper</code>, while maintaining thread safety?</p>