I'm trying to do some adding with SSE and I'm using to this C with assembly. Why something like this doesn't work?
struct vector {
float x1, x2, x3, x4;
};
struct vector *dodawanie(const struct vector v1[], const struct vector v2[], int size) {
struct vector vec[size];
int i;
for(i = 0; i < size; i++) {
asm(
"MOV %1, %%rax \n"
"MOV %2, %%rdx \n"
"MOVUPS (%%rax), %%xmm0 \n"
"MOVUPS (%%rdx), %%xmm1 \n"
"ADDPS %%xmm0, %%xmm1 \n"
"MOVUPS %%xmm1, %0 \n"
:"=g"(vec[i]) //wyjscie
:"g"(v1[i]), "g"(v2[i]) //wejscie
:"%rax", "%rdx"
);
}
return vec;
}
I got error: Thread 1: EXC_BAD_ACCESS (CODE = EXC_I386_GPFLT)
But when instead of v1[i], v2[i] I put v1, v2 etc. this work correctly but of course only with the first element of array.
What's wrong in my code?
rax/rdx. 2. try to pass %1 and %2 directly byraxandrdx. Look here: gcc.gnu.org/onlinedocs/gcc/… for x86 specific constraintsaddps. If you are intent on using inline assembler I'd follow Michal's advice of looking at getting the assembler template to do most of the work. Assuming v1, v2, and vec are all arrays of vectors (__m128) then something like this may workasm( "MOVAPS %[v1], %[out]\n\t" "ADDPS %[v2], %[out]\n\t" :[out]"=&x,m"(vec[i]) :[v1]"mx,x"(v1[i]), [v2]"mx,x"(v2[i]) );