使用 SSE 指令时是什么导致了此分段错误?
这个问题让我有点抓狂。该代码似乎是分段错误,没有充分的理由:
#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。
This problem is driving me a bit crazy. The code seems to be segmentation faulting for no good reason:
#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;
}
Note that so long as I have defined both the 'before' and 'after' variables it segmentation faults. If I just have 'before' or just have 'after' then it works fine.
I'm on Ubuntu Hardy (8.04), GCC version 4.2.4 (Ubuntu 4.2.4-1ubuntu4). Linux kernel: 2.6.24-16-generic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查a和b的地址。我怀疑您会发现它们必须与 16 字节边界对齐以避免段错误。在声明后添加
__ attribute __((aligned(16)))
应该可以解决问题。顺便说一句,属性的每一侧都有两个下划线并与之相连。
Check the address of a and b. I suspect you'll find that they must be aligned to a 16 byte boundary in order to avoid a segfault. Adding
__ attribute __((aligned(16)))
after their declarations should do the trick.That's two underscores on each side of attribute and connected to it, BTW.