c - 错误: invalid use of vector register at operand 1

标签 c gcc inline-assembly arm64

我正在 64 位 Aarch64 设备上的 ARM 下学习 GCC 内联汇编器。我看到一条我不太明白的错误消息。来自 GCC 内联汇编器的错误消息:

$ gcc -DNDEBUG -g3 -O1 -march=armv8-a+crc+crypto test.cc -o test.exe
/tmp/ccCHOWrn.s: Assembler messages:
/tmp/ccCHOWrn.s:19: Error: invalid use of vector register at operand 1 -- `pmull v0,v0,v0'

示例程序只是尝试执行多项式乘法:

$ cat test.cc 
#include <arm_neon.h>
int main(int argc, char* argv[])
{
    uint64x2_t r = {0,0}, a = {2,4};
    __asm__ __volatile__
    (
        "pmull %0, %1, %1;"
        : "=w" (r)
        : "w" (a), "w" (a)
        : "cc"
    );

    return (int)r[0];
}

“w”Aarch64 machine constraint 。在本例中,它被描述为“浮点或 SIMD vector 寄存器”,这似乎就是我想要的。

uint64x2_t 类型通常与 ARM 内在函数一起使用。但它是 128 位类型并针对 SIMD 协处理器进行了调整,因此对于示例来说它似乎是一个不错的选择。

该设备是 LeMaker HiKey使用 Linaro 镜像和 GCC 4.9.2 编译器。看起来这个错误几年前就被修复了,但我不确定它是否相关:fbb ftbfs on arm64 .

我有两个问题:

  • 该错误是什么意思?我该如何修复它?
  • pmullpmull2 是否有内在函数?

我尝试添加排列说明符,但我并不惊讶它不起作用,因为我不知道语法:

$ gcc -DNDEBUG -g3 -O1 -march=armv8-a+crc+crypto test.cc -o test.exe
test.cc: In function ‘int main(int, char**)’:
test.cc:8:15: error: expected ‘)’ before numeric constant
      : "=w" (r.1q)
               ^
test.cc:8:15: error: expected ‘)’ before numeric constant
test.cc:9:6: error: expected ‘;’ before ‘:’ token
      : "w" (a.1d), "w" (a.1d)
      ^
test.cc:9:6: error: expected primary-expression before ‘:’ token

我还尝试添加双百分号(即 %%0%%1),因为汇编器在使用 .att_stntax 时遇到问题> 和 .intel_syntax:

$ gcc -DNDEBUG -g3 -O1 -march=armv8-a+crc+crypto test.cc -o test.exe
/tmp/ccPpnvUP.s: Assembler messages:
/tmp/ccPpnvUP.s:19: Error: operand 1 should be a SIMD vector register -- `pmull %0,%1,%1'

最佳答案

What does the error mean, and how can I fix it?

我猜这个错误意味着发生了约束冲突。看起来下面的方法可以解决问题:

$ cat test.cc
#include <arm_neon.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
  uint64x2_t r = {0,0}, a = {2,4};
  __asm__ __volatile__
    (
     "pmull %0.1q, %1.1d, %1.1d;"
     : "=w" (r)
     : "w" (a[0]), "w" (a[1])
     : "cc"
     );

  fprintf(stdout, "%d, %d\n", r[0], r[1]);
  return 0;
}

还有:

$ gcc -march=armv8-a+crc+crypto test.cc -o test.exe 
$ ./test.exe 
4, 0

And :

$ gdb -batch -ex 'disassemble main' ./test.exe 
Dump of assembler code for function main:
   0x00000000004005f0 <+0>: stp x29, x30, [sp,#-64]!
   0x00000000004005f4 <+4>: mov x29, sp
   0x00000000004005f8 <+8>: str w0, [x29,#28]
   0x00000000004005fc <+12>:    str x1, [x29,#16]
   0x0000000000400600 <+16>:    movi    v0.4s, #0x0
   0x0000000000400604 <+20>:    mov x0, v0.d[0]
   0x0000000000400608 <+24>:    mov x1, v0.d[1]
   0x000000000040060c <+28>:    fmov    d0, x0
   0x0000000000400610 <+32>:    mov v0.d[1], x1
   0x0000000000400614 <+36>:    str q0, [x29,#48]
   0x0000000000400618 <+40>:    ldr q0, 0x4006a0
   0x000000000040061c <+44>:    mov x0, v0.d[0]
   0x0000000000400620 <+48>:    mov x1, v0.d[1]
   0x0000000000400624 <+52>:    fmov    d0, x0
   0x0000000000400628 <+56>:    mov v0.d[1], x1
   0x000000000040062c <+60>:    str q0, [x29,#32]
   0x0000000000400630 <+64>:    ldr x0, [x29,#32]
   0x0000000000400634 <+68>:    ldr x1, [x29,#40]
   0x0000000000400638 <+72>:    fmov    d0, x0
   0x000000000040063c <+76>:    fmov    d1, x1
   0x0000000000400640 <+80>:    pmull   v0.1q, v0.1d, v0.1d
   0x0000000000400644 <+84>:    mov x0, v0.d[0]
   0x0000000000400648 <+88>:    mov x1, v0.d[1]
   0x000000000040064c <+92>:    fmov    d0, x0
   0x0000000000400650 <+96>:    mov v0.d[1], x1
   0x0000000000400654 <+100>:   str q0, [x29,#48]
   0x0000000000400658 <+104>:   adrp    x0, 0x410000
   0x000000000040065c <+108>:   add x0, x0, #0x9f0
   0x0000000000400660 <+112>:   ldr x4, [x0]
   0x0000000000400664 <+116>:   ldr x1, [x29,#48]
   0x0000000000400668 <+120>:   ldr x2, [x29,#56]
   0x000000000040066c <+124>:   adrp    x0, 0x400000
   0x0000000000400670 <+128>:   add x0, x0, #0x748
   0x0000000000400674 <+132>:   mov x3, x2
   0x0000000000400678 <+136>:   mov x2, x1
   0x000000000040067c <+140>:   mov x1, x0
   0x0000000000400680 <+144>:   mov x0, x4
   0x0000000000400684 <+148>:   bl  0x4004a0 <fprintf@plt>
   0x0000000000400688 <+152>:   mov w0, #0x0                    // #0
   0x000000000040068c <+156>:   ldp x29, x30, [sp],#64
   0x0000000000400690 <+160>:   ret
End of assembler dump.

Is there an intrinsic for pmull and pmull2?

看起来有一些内在的东西:

$ gcc -march=armv8-a+crc+crypto -E test.cc | grep -B 4 pmull
__extension__ static __inline poly16x8_t __attribute__ ((__always_inline__))
vmull_high_p8 (poly8x16_t a, poly8x16_t b)
{
  poly16x8_t result;
  __asm__ ("pmull2 %0.8h,%1.16b,%2.16b"
--
__extension__ static __inline poly16x8_t __attribute__ ((__always_inline__))
vmull_p8 (poly8x8_t a, poly8x8_t b)
{
  poly16x8_t result;
  __asm__ ("pmull %0.8h, %1.8b, %2.8b"
--
static __inline poly128_t
vmull_p64 (poly64_t a, poly64_t b)
{
  return
    __builtin_aarch64_crypto_pmulldi_ppp (a, b);
--

static __inline poly128_t
vmull_high_p64 (poly64x2_t a, poly64x2_t b)
{
  return __builtin_aarch64_crypto_pmullv2di_ppp (a, b);

关于c - 错误: invalid use of vector register at operand 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551637/

相关文章:

c - 双向链表指针混淆

c - 文件写入/读取速度有多快

C语言编程错误: expected specifier-qualifier-list before '{ '

c++ - 如何在 CMake 中指定链接类型?

c++ - 程序运行周期中代码块Ver.16.01崩溃

assembly - 如何在 x86 汇编编程中表示 FFFFFFBB 等十六进制值?

代码设计——状态机或程序代码

c - 枚举作为c函数中的参数

c++ - 如何克服 GCC 限制 "could not convert template argument ' 0' to ' Foo *'"?

c - C 中的内联汇编 : Dynamic registers