【问题标题】:SSE2: _mm_mul_ps fails on OS X in case of GCC 4.2 and O0 optimizationSSE2:在 GCC 4.2 和 O0 优化的情况下,_mm_mul_ps 在 OS X 上失败
【发布时间】:2023-03-12 12:40:01
【问题描述】:

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

【问题讨论】:

    标签: debugging optimization gcc sse


    【解决方案1】:

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

    变化:

    typedef float SPPixel[4];
    

    到:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-15
      • 2015-03-29
      • 2016-02-14
      • 1970-01-01
      • 2011-05-20
      • 2013-11-28
      • 2013-11-02
      • 1970-01-01
      相关资源
      最近更新 更多