2

这个问题让我有点发疯。该代码似乎无缘无故地出现分段错误:

#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。

4

1 回答 1

4

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

这是属性两侧的两个下划线并连接到它,顺便说一句。

于 2009-11-13T07:31:53.643 回答