title: Segmentation fault while working with SSE intrinsics due to incorrect memory alignment
body: <p>I am working with SSE intrinsics for the first time and I am encountering a segmentation fault even after ensuring 16byte memory alignment. This post is an extension to my earlier question:</p>
  
  <p><a href="https://stackoverflow.com/questions/11084468/how-to-allocate-16byte-memory-aligned-data">How to allocate 16byte memory aligned data</a></p>
  
  <p>This is how I have declared my array:</p>
  
  <pre><code>  float *V = (float*) memalign(16,dx*sizeof(float));
  </code></pre>
  
  <p>When I try to do this:</p>
  
  <pre><code>  __m128 v_i = _mm_load_ps(&amp;V[i]); //It works
  </code></pre>
  
  <p>But when I do this:</p>
  
  <pre><code>  __m128 u1 = _mm_load_ps(&amp;V[(i-1)]); //There is a segmentation fault
  </code></pre>
  
  <p>But if I do :</p>
  
  <pre><code>  __m128 u1 = _mm_loadu_ps(&amp;V[(i-1)]); //It works again
  </code></pre>
  
  <p>However I want to eliminate using <code>_mm_loadu_ps</code> and want to make it work using <code>_mm_load_ps</code> only.</p>
  
  <p>I am working with the Intel icc compiler.</p>
  
  <p>How do I resolve this issue?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>using both operations in the following code:</p>
  
  <pre><code>  void FDTD_base (float *V, float *U, int dx, float c0, float c1, float c2, float c3,     float c4)
      {
         int i, j, k;
                      for (i = 4; i &lt; dx-4; i++)
                      {
  
                              U[i] = (c0 * (V[i]) //center
                                      + c1 * (V[(i-1)] + V[(i+1)] )
                                      + c2 * (V[(i-2)] + V[(i+2)] )
                                      + c3 * (V[(i-3)] + V[(i+3)] )
                                      + c4 * (V[(i-4)] + V[(i+4)] ));
                      }
  
         }
  </code></pre>
  
  <p>SSE version:</p>
  
  <pre><code>         for (i=4; i &lt; dx-4; i+=4)
          {
              v_i = _mm_load_ps(&amp;V[i]);
              __m128 center = _mm_mul_ps(v_i,c0_i);
  
              __m128 u1 = _mm_loadu_ps(&amp;V[(i-1)]);
              u2 = _mm_loadu_ps(&amp;V[(i+1)]);
  
              u3 = _mm_loadu_ps(&amp;V[(i-2)]);
              u4 = _mm_loadu_ps(&amp;V[(i+2)]);
  
              u5 = _mm_loadu_ps(&amp;V[(i-3)]);
              u6 = _mm_loadu_ps(&amp;V[(i+3)]);
  
              u7 = _mm_load_ps(&amp;V[(i-4)]);
              u8 = _mm_load_ps(&amp;V[(i+4)]);
  
              __m128 tmp1 = _mm_add_ps(u1,u2);
              __m128 tmp2 = _mm_add_ps(u3,u4);
              __m128 tmp3 = _mm_add_ps(u5,u6);
              __m128 tmp4 = _mm_add_ps(u7,u8);
  
              __m128 tmp5 = _mm_mul_ps(tmp1,c1_i);
              __m128 tmp6 = _mm_mul_ps(tmp2,c2_i);
              __m128 tmp7 = _mm_mul_ps(tmp3,c3_i);
              __m128 tmp8 = _mm_mul_ps(tmp4,c4_i);
  
              __m128 tmp9 = _mm_add_ps(tmp5,tmp6);
              __m128 tmp10 = _mm_add_ps(tmp7,tmp8);
  
              __m128 tmp11 = _mm_add_ps(tmp9,tmp10);
              __m128 tmp12 = _mm_add_ps(center,tmp11);
  
              _mm_store_ps(&amp;U[i], tmp12);
      }
  </code></pre>
  
  <p>Is there a more efficient way of doing this using only <code>_mm_load_ps()</code> ?</p>