Use inline assemblers SSE2 and gcc inline

I tried to combine SSE2 inline assemblers and gcc inline assembler. But if I specify the variable as xmm0 / register as input, then in some cases I get a compiler error. Example:

#include <emmintrin.h> int main() { __m128i test = _mm_setzero_si128(); asm ("pxor %%xmm0, %%xmm0" : : "xmm0" (test) : ); } 

When compiling with gcc version 4.6.1, I get:

 >gcc asm_xmm.c asm_xmm.c: In function 'main': asm_xmm.c:10:3: error: matching constraint references invalid operand number asm_xmm.c:7:5: error: matching constraint references invalid operand number 

The strange thing is that in the same cases, when I have other input variables / registers, it suddenly works with xmm0 as input, but not xmm1, etc. And in another case, I was able to specify xmm0-xmm4, but not higher. A bit confused / upset: S

Thanks:)

+7
source share
1 answer

You must let the compiler do the register assignment. Here is an example of pshufb (for gcc too old to have tmmintrin for SSSE3):

 static inline __m128i __attribute__((always_inline)) _mm_shuffle_epi8(__m128i xmm, __m128i xmm_shuf) { __asm__("pshufb %1, %0" : "+x" (xmm) : "xm" (xmm_shuf)); return xmm; } 

Pay attention to the classifier "x" for arguments and just %0 in the assembly itself, where the compiler will be replaced in the selected register.

Observe the correct modifiers. "+x" means xmm - both input and output parameter. If you are messy with these modifiers (for example, using "=x" , meaning output only when you need "+x" ), you will encounter situations where it sometimes works, and sometimes not.

+11
source

All Articles