union __m128 float* Error accesing memmory

I'm trying to learn SSE instructions and I aspire to multiply two matices. I have my data storage with an union. However, after i call _mm_mul_ps(__m128, __m128) i try to acces to the float* in the union causing an error. Here`s the code with the problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
typedef union{
	__m128 vec;
	float *afloat;
}u_float;

int main(){

	__declspec(align(16)) u_float *mat1;
	__declspec(align(16)) u_float *mat2;
        __declspec(align(16)) u_float aux;
        __declspec(align(16)) u_float *res;

        // Memmory allocation
        mat1 = (u_float*)malloc(sizeof(u_float)*4);
	    for(int i = 0; i < 4; i++)
		mat1[i].afloat = (float*)malloc(sizeof(float)*4);

        mat2 = (u_float*)malloc(sizeof(u_float)*4);
	    for(int i = 0; i < 4; i++)
		mat2[i].afloat = (float*)malloc(sizeof(float)*4);

        res = (u_float*)malloc(sizeof(u_float)*4);
	    for(int i = 0; i < 4; i++)
		res[i].afloat = (float*)malloc(sizeof(float)*4);

       // Fill mat1 and mat2 matrices

       // Here comes the problem
       aux.afloat = (float*)malloc(sizeof(float)*4);
       for(int i = 0; i < 4; i++){
	  aux.vec = _mm_mul_ps(mat1[i].vec, mat2[i].vec);
	  for(int j = 0; j < 4; i++){
	     res[i].afloat[j] = aux.afloat[j]; // Error. Acces memmory
	 }
}
Last edited on
If we ignore the fact that writing to vec then reading from afloat results in undefined behavior, the value written to vec will be the address used when afloat is dereferenced. Clearly that isn't what you want.
Topic archived. No new replies allowed.