且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C - 如何使用GCC SSE向量扩展访问向量的元素

更新时间:2023-11-09 23:48:04

The safe and recommended way to access the elements is with a union, instead of pointer type punning, which fools the aliasing detection mechanisms of the compiler and may lead to unstable code.

union Vec4 {
    v4sf v;
    float e[4];
};

Vec4 vec;
vec.v = (v4sf){0.1f,0.2f,0.3f,0.4f};
printf("%f %f %f %f\n", vec.e[0], vec.e[1], vec.e[2], vec.e[3]);