使用SSE指令时导致此分段错误的原因是什么?

时间:2009-11-13 07:21:55

标签: gcc assembly segmentation-fault inline-assembly

这个问题让我有点疯狂。代码似乎是segmentation faulting没有充分的理由:

#define MULT_FLOAT4(X, Y) ({ \
    asm volatile ( \
        "movups (%0), %%xmm0\n\t" \
        "mulps (%1), %%xmm0\n\t" \
        "movups %%xmm0, (%1)" \
        :: "r" (X), "r" (Y)); })

int main(void)
{
    int before;
    float a[4] = { 10, 20, 30, 40 };
    float b[4] = { 0.1, 0.1, 0.1, 0.1 };

    /* Segmentation faults if I uncomment this line below, otherwise works fine. Why?? */
    int after;

    MULT_FLOAT4(a, b);

    return 0;
}

请注意,只要我定义了“之前”和“之后”变量,它就会出现分段错误。如果我只是'之前'或只是'之后'那么它就可以了。

我使用的是Ubuntu Hardy(8.04),GCC版本4.2.4(Ubuntu 4.2.4-1ubuntu4)。 Linux内核:2.6.24-16-generic。

1 个答案:

答案 0 :(得分:4)

检查a和b的地址。我怀疑你会发现它们必须与16字节边界对齐以避免段错误。在声明之后添加__ attribute __((aligned(16)))应该可以解决问题。

这是属性的每一侧的两个下划线并连接到它,BTW。