Aligned types and passing arguments by value

主宰稳场 提交于 2019-12-20 09:59:47

问题


Passing aligned types or structures with aligned types by value doesn't work with some implementations. This breaks STL containers, because some of the methods (such as resize) take their arguments by value.

I run some tests with Visual Studio 2008 and not entirely sure when and how pass by value fails. My main concern is function foo. It seems to work fine, but could it be a result of inlining or some other coincidence? What if I change its signature to void foo(const __m128&)?

Your input is greatly appreciated. Thank you.

struct A
{
    __m128 x;
    int n;
};

void foo(__m128);
void bar(A);

void f1()
{
    // won't compile
    // std::vector<A> vec1(3);

    // compiles, but fails at runtime when elements are accessed
    std::vector<__m128> vec2(3);

    // this seems to work. WHY???
    std::vector<__m128, some_16_byte_aligned_allocator<__m128> > vec3(3);

    __m128 x;
    A a;

    // passed by value, is it OK?
    foo(x);

    // won't compile
    //bar(a);
}

EDIT. STL fails even with aligned allocator, because pass by value problem remains.

Found this link pass __m128 by value


回答1:


I think the only safe way to do this in general is to pass by reference. Some platforms (e.g. Xbox 360) support passing vector arguments in registers, but I don't think this is possible on x86.

For the std::vector case, you'll need to make sure that the memory allocated is aligned to 16 bytes; otherwise you'll get crashes when you try to perform most operations on unaligned vectors.

If you're supporting multiple platforms, a safe plan is to use a typedef e.g.

typedef const MyVector& MyVectorParameter;

You can then change the typedef on platforms that support vector pass-by-value.




回答2:


the oftenly used resize() function is causing all the alignment and perhaps you can try to specialise vector template for __m128 ?



来源:https://stackoverflow.com/questions/4424741/aligned-types-and-passing-arguments-by-value

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