title: Fastest way to do horizontal vector sum with AVX instructions
body: <p>I have a packed vector of four 64-bit floating-point values.<br>
  I would like to get the sum of the vector's elements.</p>
  
  <p>With SSE (and using 32-bit floats) I could just do the following:</p>
  
  <pre><code>v_sum = _mm_hadd_ps(v_sum, v_sum);
  v_sum = _mm_hadd_ps(v_sum, v_sum);
  </code></pre>
  
  <p>Unfortunately, even though AVX features a _mm256_hadd_pd instruction, it differs in the result from the SSE version. I believe this is due to the fact that most AVX instructions work as SSE instructions for each low and high 128-bits separately, without ever crossing the 128-bit boundary.</p>
  
  <p>Ideally, the solution I am looking for should follow these guidelines:<br>
  1) only use AVX/AVX2 instructions. (no SSE)<br>
  2) do it in no more than 2-3 instructions.</p>
  
  <p>However, any efficient/elegant way to do it (even without following the above guidelines) is always well accepted.</p>
  
  <p>Thanks a lot for any help.</p>
  
  <p>-Luigi Castelli</p>