How to swap two __m128i variables in C++03 given its an opaque type and an array?

前端 未结 2 1853
没有蜡笔的小新
没有蜡笔的小新 2021-01-14 11:19

What is the best practice for swapping __m128i variables?

The background is a compile error under Sun Studio 12.2, which is a C++03 compiler. __m1

2条回答
  •  醉梦人生
    2021-01-14 11:48

    swap via memcpy?

    #include 
    #include 
    
    template
    void memswap(T& a, T& b)
    {
        T t;
        std::memcpy(&t, &a, sizeof(t));
        std::memcpy(&a, &b, sizeof(t));
        std::memcpy(&b, &t, sizeof(t));
    }
    
    int main() {
        __m128i x;
        __m128i y;
        memswap(x, y);
        return 0;
    }
    

提交回复
热议问题