尝试初始化 __m128 类成员变量时出现 EXC_BAD_ACCESS 信号
我正在使用 Apple GCC 4.2.1,并且我偶然发现了以下代码的一个奇怪问题...在尝试初始化 EXC_BAD_ACCESS 时,我总是遇到 EXC_BAD_ACCESS 异常>__m128 类成员变量。不幸的是,下面的简化代码可以在测试应用程序中运行,但也许您仍然可以帮助我找到这个问题的根源?
我无法理解 EXC_BAD_ACCESS 异常背后的原因 - __m128 类型不是指针,所有其他 MyClass 成员都已初始化和访问,没有任何问题,没有堆栈/堆损坏的迹象,如果我使用局部变量,一切正常,并且在 MSVC 下没有问题...也许对齐有问题?
请帮忙!
class MyClass
{
public:
// lots of members
__m128 vect;
MyClass()
{
vect = _mm_setr_ps (0.f, 0.f, 0.f, 10.0f); // Program received signal: “EXC_BAD_ACCESS”.
}
void iniialize()
{
__m128 localVector = _mm_setr_ps (0.f, 0.f, 0.f, 10.0f); // No problems
vect = localVector; // Program received signal: “EXC_BAD_ACCESS”.
}
};
I'm using Apple GCC 4.2.1 and I've stumbled upon a strange problem with the following code... I always get EXC_BAD_ACCESS exception when trying to initialize __m128 class member variable. Unfortunately the following simplified code works in a test application, but maybe you can still help me locate the root of this problem?
I fail to understand the reason behind EXC_BAD_ACCESS exception - __m128 type is not a pointer and all other MyClass members are initialized and accessed without any problems, there are no signs of stack / heap corruption, everything works if I use local variables and there are no problems under MSVC... Maybe something is wrong with alignment?
Please help!
class MyClass
{
public:
// lots of members
__m128 vect;
MyClass()
{
vect = _mm_setr_ps (0.f, 0.f, 0.f, 10.0f); // Program received signal: “EXC_BAD_ACCESS”.
}
void iniialize()
{
__m128 localVector = _mm_setr_ps (0.f, 0.f, 0.f, 10.0f); // No problems
vect = localVector; // Program received signal: “EXC_BAD_ACCESS”.
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从我的想法中:我会说对齐问题
特别是它说“很多成员”的部分
看看
__attribute__
aligned
From the top of my mind: i'd say alignment issues
Especially, the part where it says 'lot's of members'
Look at
__attribute__
aligned
如果对象是在堆栈上创建的,gcc 将自动正确对齐 __m128 成员,但是对于通过 new 分配的对象,您将受到内存分配器的支配,这通常只是Linux 上为 8 字节对齐。您可能需要为您的类重写运算符
new
,以便它调用 posix_memalign< /a> 这样你总是能得到 16 字节对齐的对象。话虽如此,如果您要进行 SSE 代码优化,那么您可能需要重新评估编码方式 - 因为性能通常是 SIMD 优化的动机,您可能希望在比 C++ 类稍低的级别上工作 -通常,您只想对大块连续数据(即一维或二维数组)进行同质操作。
gcc will automatically align
__m128
members correctly if the object is created on the stack, but for objects allocated vianew
you are at the mercy of the memory allocator, which is typically only 8 byte aligned on Linux. You may need to override operatornew
for your class so that it calls posix_memalign so that you always get 16 byte aligned objects.Having said that, if you're getting into SSE code optimisation then you may want to re-evaluate how you do the coding - since performance is usually the motivation for SIMD optimisation you may want to work at a somewhat lower level than C++ classes - typically you just want to operate homogeneously on big chunks of contiguous data, i.e. 1D or 2D arrays.
如果堆栈未对齐是问题所在,则应检查
-mstackrealign
命令行选项,请参阅GCC 文档。这解决了我在 MinGW 目标上的问题。另请参阅有关堆栈对齐的讨论。最后,您可能想要将 GCC 更新到较新的版本。另一方面,如果动态分配对象,则必须确保内存对齐,正如 Paul 指出的那样。有像
_mm_malloc
和_mm_free
这样的方法可以帮助您。If stack misalignment is the problem, you should check the
-mstackrealign
command line option, see GCC documentation. This solved my issues on MinGW target. See also the discussion on stack alignment. Finally, you might want to update GCC to a newer version.On the other hand, if you allocate objects dynamically, you must make sure that the memory is aligned, as Paul noted. There are methods like
_mm_malloc
and_mm_free
that can help you with that.