SIMD and dynamic memory allocation [duplicate]

蹲街弑〆低调 提交于 2019-12-01 07:41:32

It dies because the structure is mis-aligned. The CRT allocator only promises alignment to 8, 16 is required here. You'll need to use _aligned_malloc() on MSVC to get properly aligned heap allocated memory.

Two ways to go about it. Since this is a POD struct, you could just cast:

#include <malloc.h>
...
    SimdTest* test = (SimdTest*)_aligned_malloc(sizeof SimdTest, 16);
    test->setZero();
    _aligned_free(test);

Or you could override the new/delete operators for the struct:

struct SimdTest
{
    void* operator new(size_t size) { return _aligned_malloc(size, 16); }
    void operator delete(void* mem) { return _aligned_free(mem); }
    // etc..
};

MSDN states that the _m128 are automaticly aligned by 16 bytes, not __m128, but _m128. But anyway i guess the others right, as i recall there are two kind of move instructions, one for aligned movAps and one for unaligned - movUps. First requires 16b aligment and other don't. Don't know if compiler are capable of using both, but i'd tryed this _m128 type.

Actually there are special type for that: _M128A.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!