When looping over an array with inline assembly should I use the register modifier "r" or he memory modifier "m"?
Let's consider an example which adds two float arrays x
, and y
and writes the results to z
. Normally I would use intrinsics to do this like this
for(int i=0; i<n/4; i++) {
__m128 x4 = _mm_load_ps(&x[4*i]);
__m128 y4 = _mm_load_ps(&y[4*i]);
__m128 s = _mm_add_ps(x4,y4);
_mm_store_ps(&z[4*i], s);
}
Here is the inline assembly solution I have come up with using the register modifier "r"
void add_asm1(float *x, float *y, float *z, unsigned n) {
for(int i=0; i<n; i+=4) {
__asm__ __volatile__ (
"movaps (%1,%%rax,4), %%xmm0\n"
"addps (%2,%%rax,4), %%xmm0\n"
"movaps %%xmm0, (%0,%%rax,4)\n"
:
: "r" (z), "r" (y), "r" (x), "a" (i)
:
);
}
}
This generates similar assembly to GCC. The main difference is that GCC adds 16 to the index register and uses a scale of 1 whereas the inline-assembly solution adds 4 to the index register and uses a scale of 4.
I was not able to use a general register for the iterator. I had to specify one which in this case was rax
. Is there a reason for this?
Here is the solution I came up with using the memory modifer "m"
void add_asm2(float *x, float *y, float *z, unsigned n) {
for(int i=0; i<n; i+=4) {
__asm__ __volatile__ (
"movaps %1, %%xmm0\n"
"addps %2, %%xmm0\n"
"movaps %%xmm0, %0\n"
: "=m" (z[i])
: "m" (y[i]), "m" (x[i])
:
);
}
}
This is less efficient as it does not use an index register and instead has to add 16 to the base register of each array. The generated assembly is (gcc (Ubuntu 5.2.1-22ubuntu2) with gcc -O3 -S asmtest.c
):
.L22
movaps (%rsi), %xmm0
addps (%rdi), %xmm0
movaps %xmm0, (%rdx)
addl $4, %eax
addq $16, %rdx
addq $16, %rsi
addq $16, %rdi
cmpl %eax, %ecx
ja .L22
Is there a better solution using the memory modifier "m"? Is there some way to get it to use an index register? The reason I asked is that it seemed more logical to me to use the memory modifer "m" since I am reading and writing memory. Additionally, with the register modifier "r" I never use an output operand list which seemed odd to me at first.
Maybe there is a better solution than using "r" or "m"?
Here is the full code I used to test this
#include <stdio.h>
#include <x86intrin.h>
#define N 64
void add_intrin(float *x, float *y, float *z, unsigned n) {
for(int i=0; i<n; i+=4) {
__m128 x4 = _mm_load_ps(&x[i]);
__m128 y4 = _mm_load_ps(&y[i]);
__m128 s = _mm_add_ps(x4,y4);
_mm_store_ps(&z[i], s);
}
}
void add_intrin2(float *x, float *y, float *z, unsigned n) {
for(int i=0; i<n/4; i++) {
__m128 x4 = _mm_load_ps(&x[4*i]);
__m128 y4 = _mm_load_ps(&y[4*i]);
__m128 s = _mm_add_ps(x4,y4);
_mm_store_ps(&z[4*i], s);
}
}
void add_asm1(float *x, float *y, float *z, unsigned n) {
for(int i=0; i<n; i+=4) {
__asm__ __volatile__ (
"movaps (%1,%%rax,4), %%xmm0\n"
"addps (%2,%%rax,4), %%xmm0\n"
"movaps %%xmm0, (%0,%%rax,4)\n"
:
: "r" (z), "r" (y), "r" (x), "a" (i)
:
);
}
}
void add_asm2(float *x, float *y, float *z, unsigned n) {
for(int i=0; i<n; i+=4) {
__asm__ __volatile__ (
"movaps %1, %%xmm0\n"
"addps %2, %%xmm0\n"
"movaps %%xmm0, %0\n"
: "=m" (z[i])
: "m" (y[i]), "m" (x[i])
:
);
}
}
int main(void) {
float x[N], y[N], z1[N], z2[N], z3[N];
for(int i=0; i<N; i++) x[i] = 1.0f, y[i] = 2.0f;
add_intrin2(x,y,z1,N);
add_asm1(x,y,z2,N);
add_asm2(x,y,z3,N);
for(int i=0; i<N; i++) printf("%.0f ", z1[i]); puts("");
for(int i=0; i<N; i++) printf("%.0f ", z2[i]); puts("");
for(int i=0; i<N; i++) printf("%.0f ", z3[i]); puts("");
}
long long
or use%q3
to force the full register. BTW, since add_asm1 modifies memory, it should use the memory clobber. – Woods(%1,%4,4)
instead of instead of(%1,%%rax,4)
where%4
is whatever register gcc decides rarther than forcing it to berax
. – Episcopalint i=0
tolong long i=0
, then you can use "r" along with %3. Alternately, you can leave i an int, and use %q3 (also changing from "a" to "r"). – Woods%3
. I tried this and it works I did not even have to switch tolong lont i=0
. Looking at the assembly I see that gcc uses%eax
. That's a better solution anyways as there is no reason to use%rax
for the index. If you want to write up an answer I will upvote you. – Episcopalmemory
clobber: They mean usememory
if you can't tell the compiler which memory was clobbered. In this case it is predictable, so you can use the statement-expression trick suggested at the end of the Clobbers section:{"m"( ({ struct { char x[16]; } *p = (void *)(z+i*4) ; *p; }) )}
. I modified the example to fit your code: clobber 16 bytes at&z[i*4]
. Also note that using a memory output operand would mean you don't need__volatile__
on your asm, since it knows it can't hoist a store toz[i]
. – Golightly{"m"( ({ struct { char x[16]; } *p = (void *)(z+i*4) ; *p; }) )}
I will upvote it. – Episcopal