gpt4 book ai didi

debugging - SSE2 : _mm_mul_ps fails on OS X in case of GCC 4. 2 和 O0 优化

转载 作者:行者123 更新时间:2023-12-02 20:56:42 25 4
gpt4 key购买 nike

我正在尝试使用 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;
}

最佳答案

局部变量d未对齐。修复 SPPixel 的 typedef 中的对齐方式,而不必在每个定义中记住它。

更改:

typedef float SPPixel[4];

至:

typedef float SPPixel[4] __attribute__ ((aligned(16)));

然后您还可以删除 main 中的 __attribute__ ((aligned(16))) 限定符。

关于debugging - SSE2 : _mm_mul_ps fails on OS X in case of GCC 4. 2 和 O0 优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14024304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com