7

我试图在 gcc 中混合 SSE2 内在函数和内联汇编程序。但是,如果我将变量指定为 xmm0/register 作为输入,那么在某些情况下会出现编译器错误。例子:

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

当使用 gcc 版本 4.6.1 编译时,我得到:

>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

奇怪的是,在我有其他输入变量/寄存器的相同情况下,它突然使用 xmm0 作为输入而不是 xmm1 等。在另一种情况下,我能够指定 xmm0-xmm4 但不能在上面指定。对此有点困惑/沮丧:S

谢谢 :)

4

1 回答 1

11

You should let the compiler do the register assignment. Here's 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;
}

Note the "x" qualifier on the arguments and simply %0 in the assembly itself, where the compiler will substitute in the register it selected.

Be careful to use the right modifiers. "+x" means xmm is both an input and an output parameter. If you are sloppy with these modifiers (eg using "=x" meaning output only when you needed "+x") you will run into cases where it sometimes works and sometimes doesn't.

于 2012-01-27T21:39:49.053 回答