Vla*_*lad 3 debugging optimization gcc sse
我试图使用SSE2计算两个4d浮点矢量之间的平方欧几里德距离.我的操作系统是Mac OS X 10.7 Lion.
当我在XCode 4.5.2中使用Apple LLVM编译器时,一切都很好.但是当我在项目设置中切换到GCC 4.2时,我在_mm_mul_ps操作时出现错误EXC_BAD_ACCESS .
当我从命令行(g ++ main.cpp)编译代码而没有其他参数时,我有"分段错误".但是当我启用除O0之外的任何优化级别(O1,O2,O3,Os)时,一切正常.
我无法在使用GCC 4.6.3的Ubuntu 12.04上重现此问题.
#include <stdio.h>
#include <emmintrin.h>
typedef float SPPixel[4];
float sp_squared_color_diff(const SPPixel px1, const SPPixel px2) {
SPPixel d;
__m128 sse_px1 = _mm_load_ps(px1);
__m128 sse_px2 = _mm_load_ps(px2);
sse_px1 = _mm_sub_ps(sse_px1, sse_px2);
sse_px2 = _mm_mul_ps(sse_px1, sse_px1); // EXC_BAD_ACCESS
_mm_store_ps(d, sse_px2);
return d[0] + d[1] + d[2] + d[3];
}
int main(int argc, const char * argv[]) {
SPPixel a __attribute__ ((aligned (16))) = {1, 2, 3, 4};
SPPixel b __attribute__ ((aligned (16))) = {2, 4, 6, 8};
float result = sp_squared_color_diff(a, b);
printf("result = %f\n", result);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
局部变量d未对齐.修复typedef中的对齐,SPPixel而不是必须在每个定义上记住它.
更改:
typedef float SPPixel[4];
Run Code Online (Sandbox Code Playgroud)
至:
typedef float SPPixel[4] __attribute__ ((aligned(16)));
Run Code Online (Sandbox Code Playgroud)
然后你也可以删除__attribute__ ((aligned(16)))main 中的限定符.