SSE and C++ containers

前端 未结 4 1245
情书的邮戳
情书的邮戳 2020-12-09 20:26

Is there an obvious reason why the following code segfaults ?

#include 
#include 

struct point {
    __m128i v;

  point()          


        
4条回答
  •  盖世英雄少女心
    2020-12-09 20:48

    There is a possibility that the memory that is allocated by the default allocator in your compiler's STL implementation is not aligned. This will be dependent on the specific platform and compiler vendor.

    Usually the default allocator uses operator new, which usually does not guarantee alignment beyond the word size (32-bit or 64-bit). To solve the problem, it may be necessary to implement a custom allocator which uses _aligned_malloc.

    Also, a simple fix (although not a satisfactory one) would be to assign the value to a local __m128i variable, then copy that variable to the struct using unaligned instruction. Example:

    struct point {
        __m128i v;
        point() {
            __m128i temp = _mm_setr_epi32(0, 0, 0, 0);
            _mm_storeu_si128(&v, temp);
        }
    };
    

提交回复
热议问题