2016-05-11 65 views
0

我有以下試圖在vs2013中運行的SIMD代碼。它可以很好的編譯但不能運行。有人知道爲什麼VS2013中SSE2代碼的運行錯誤

#include <cstdio> 
#include <xmmintrin.h> 

int main() 
{ 
    const size_t num = 7; 
    float a[num] = { 1, 2, 3, 4, 5, 6, 7 }; 
    float b[num] = { 1, -1, -2, 1, -3, -2, 5 }; 
    float c[num]; 
    __m128 A, B, C; 
    A = _mm_load_ps(&a[0]); // <== crash here. 
    B = _mm_load_ps(&b[0]); 
    C = _mm_add_ps(A, B); 
    _mm_store_ps(&c[0], C); 

    return 0; 
} 
+0

和錯誤消息是什麼? – MSalters

+1

您的數據可能未對齊。 – Mysticial

+0

請對齊您的數據或使用'_mm_loadu_ps' /'_mm_storeu_ps'。 –

回答

1

使用這些內在函數加載或存儲的地址需要16字節對齊(可被16整除)。見 https://msdn.microsoft.com/en-us/library/zzd50xxt(v=vs.90).aspx

你應該聲明變量a,b和c是這樣的:

__declspec(align(16)) float a[num] = { 1, 2, 3, 4, 5, 6, 7 };

+0

如果使用g ++會怎麼樣?是__declspec(align(16))仍然是必要的嗎? –

+1

關於gcc,它是__attribute __((aligned(16))) - 請參閱http://stackoverflow.com/questions/841433/gcc-attribute-alignedx-explanation –