C - How to access elements of vector using GCC SSE vector extension

gcc, sse

Solution

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]);

Problem

Usually I work with 3D vectors using following types: ``` typedef vec3_t float[3]; ``` initializing vectors using smth. like: ``` vec3_t x_basis = {1.0, 0.0, 0.0}; vec3_t y_basis = {0.0, 1.0, 0.0}; vec3_t z_basis = {0.0, 0.0, 1.0}; ``` and accessing them using smth. like: ``` x_basis[X] * y_basis[X] + ... ``` Now I need a vector arithmetics using SSE instructions. I have following code: ``` typedef float v4sf __attribute__ ((mode(V4SF))) int main(void) { v4sf a,b,c; a = (v4sf){0.1f,0.2f,0.3f,0.4f}; b = (v4sf){0.1f,0.2f,0.3f,0.4f}; c = (v4sf){0.1f,0.2f,0.3f,0.4f}; a = b + c; printf("a=%f \n", a); return 0; } ``` GCC supports such way. But... First, it gives me 0.00000 as result. Second, I cannot access the elements of such vectors. My question is: how can I access elements of such vectors? I need smth. like a[0] to access X element, a[1] to access Y element, etc. PS: I compile this code using: ``` gcc -msse testgcc.c -o testgcc ```

Original source