我的問題讓我的g ++ 5.4使用矢量化進行比較。基本上我想比較使用矢量化的4個未簽名的整數。我的第一個方法是直截了當:自動矢量比較
bool compare(unsigned int const pX[4]) {
bool c1 = (temp[0] < 1);
bool c2 = (temp[1] < 2);
bool c3 = (temp[2] < 3);
bool c4 = (temp[3] < 4);
return c1 && c2 && c3 && c4;
}
與g++ -std=c++11 -Wall -O3 -funroll-loops -march=native -mtune=native -ftree-vectorize -msse -msse2 -ffast-math -fopt-info-vec-missed
編譯告訴是,它無法向量化的比較,由於未對齊的數據:
main.cpp:5:17: note: not vectorized: failed to find SLP opportunities in basic block.
main.cpp:5:17: note: misalign = 0 bytes of ref MEM[(const unsigned int *)&x]
main.cpp:5:17: note: misalign = 4 bytes of ref MEM[(const unsigned int *)&x + 4B]
main.cpp:5:17: note: misalign = 8 bytes of ref MEM[(const unsigned int *)&x + 8B]
main.cpp:5:17: note: misalign = 12 bytes of ref MEM[(const unsigned int *)&x + 12B]
因此,我的第二次嘗試,告訴G ++對齊數據並使用臨時陣列:
bool compare(unsigned int const pX[4]) {
unsigned int temp[4] __attribute__ ((aligned(16)));
temp[0] = pX[0];
temp[1] = pX[1];
temp[2] = pX[2];
temp[3] = pX[3];
bool c1 = (temp[0] < 1);
bool c2 = (temp[1] < 2);
bool c3 = (temp[2] < 3);
bool c4 = (temp[3] < 4);
return c1 && c2 && c3 && c4;
}
但是,輸出相同。我的CPU支持AVX2,英特爾固有指南告訴我, _mm256_cmpgt_epi8/16/32/64
作比較。任何想法如何告訴g ++使用它?
不知道如果有一個可移植的方式來做到這一點的結果結合起來,但如果你只是想看看是否所有的'bool's設置或者沒有[intrinsics](https://software.intel.com/sites/landingpage/IntrinsicsGuide/),它會告訴你它們是否通過位計數等都是錯誤的[intel甚至有一個例子](https:/ /software.intel.com/en-us/blogs/2013/05/17/processing-arrays-of-bits-with-intel-advanced-vector-extensions-2-intel-avx2) – Mgetz
沒有32位無符號比較在SSE/AVX - 嘗試與簽名。 –
AVX2需要32字節對齊 –