为什么编译器没有定义 __ARM_FEATURE_CRC32?

2022-01-17 00:00:00 arm neon c++ intrinsics c-preprocessor

我已经研究这个问题一段时间了,我希望有人能指出我的错误.我想我再也看不到森林了.

I've been working on this issue for some time now, and I hope someone can point out my mistake. I guess I can no longer see the forest through the trees.

我有一个用于测试的 LeMaker HiKey 开发板.它的 AArch64,因此它具有 NEON 和其他 cpu 功能,如 AES、SHA 和 CRC32:

I have a LeMaker HiKey dev board I use for testing. Its AArch64, so its has NEON and the other cpu features like AES, SHA and CRC32:

$ cat /proc/cpuinfo 
Processor   : AArch64 Processor rev 3 (aarch64)
...
Features    : fp asimd evtstrm aes pmull sha1 sha2 crc32 
...

当我尝试编译程序时:

$ cat test.cxx
#if (defined(__ARM_NEON__) || defined(__ARM_NEON))
# define NEON_INTRINSICS_AVAILABLE 1
#else
# define NEON_INTRINSICS_AVAILABLE 0
#endif

#if BOOL_NEON_INTRINSICS_AVAILABLE
# include <arm_neon.h>
# if defined(__ARM_FEATURE_CRC32) || (__ARM_ACLE >= 200)
#  include <arm_acle.h>
# endif
#endif

#include <stdint.h>

int main(int argc, char* argv[])
{
  uint32_t crc = 0;

  crc = __crc32b(crc, (uint8_t)0);
  return 0
}

结果如下:

$ g++ test.cxx -o test.exe
test.cxx: In function ‘int main(int, char**)’:
test.cxx:20:33: error: ‘__crc32b’ was not declared in this scope
   crc = __crc32b(crc, (uint8_t)0);
                                 ^
test.cxx:22:1: error: expected ‘;’ before ‘}’ token
 }
 ^

$ clang++ test.cxx -o test.exe
test.cxx:20:9: error: use of undeclared identifier '__crc32b'
  crc = __crc32b(crc, (uint8_t)0);
        ^
test.cxx:21:11: error: expected ';' after return statement
  return 0
          ^
          ;
2 errors generated.

文件系统的 grep 显示 arm_acle.h 实际上是标题:

A grep of the file system reveals arm_acle.h is in fact the header:

$ grep -IR '__crc32' /usr/lib
/usr/lib/gcc/.../include/arm_acle.h:__crc32b (uint32_t __a, uint8_t __b)
...

根据 ARM? C Language Extensions,第 9.7 节 CRC32 Intrinsics,假设在定义 __ARM_FEATURE_CRC32 时会出现缺失的符号.检查 arm_acle.h 即可确认.

And according to ARM? C Language Extensions, Section 9.7 CRC32 Intrinsics, the missing symbols are suppose be present when __ARM_FEATURE_CRC32 is defined. Inspecting arm_acle.h confirms it.

为了完整起见,我尝试使用 -march=native 进行编译,但编译器拒绝了它.

For completeness, I tried compiling with -march=native, but the compiler rejected it.

为什么编译器没有定义__ARM_FEATURE_CRC32?

Why is __ARM_FEATURE_CRC32 not being defined by the compiler?

我可以做些什么来让程序与板上可用的本机功能一起编译?

What can I do to get the program to compile with the native features available on the board?

$ gcc --version
gcc (Debian/Linaro 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ clang --version
Debian clang version 3.5.0-10 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: aarch64-unknown-linux-gnu
Thread model: posix

<小时>

$ g++ -dM -E - </dev/null | egrep -i '(arm|neon|acle)'
#define __ARM_NEON 1

$ clang++ -dM -E - </dev/null | egrep -i '(arm|neon|acle)'
#define __ARM_64BIT_STATE 1
#define __ARM_ACLE 200
#define __ARM_ALIGN_MAX_STACK_PWR 4
#define __ARM_ARCH 8
#define __ARM_ARCH_ISA_A64 1
#define __ARM_ARCH_PROFILE 'A'
#define __ARM_FEATURE_CLZ 1
#define __ARM_FEATURE_DIV 1
#define __ARM_FEATURE_FMA 1
#define __ARM_FEATURE_UNALIGNED 1
#define __ARM_FP 0xe
#define __ARM_FP16_FORMAT_IEEE 1
#define __ARM_FP_FENV_ROUNDING 1
#define __ARM_NEON 1
#define __ARM_NEON_FP 0xe
#define __ARM_PCS_AAPCS64 1
#define __ARM_SIZEOF_MINIMAL_ENUM 4
#define __ARM_SIZEOF_WCHAR_T 4

推荐答案

至于为什么默认不开启这个功能;这是您的编译器所针对的基线 ABI 中不存在的可选功能,即您的编译器生成的二进制文件预计能够在缺少 CRC 功能的设备上运行.

As for why this feature isn't enabled by default; this is an optional feature not present in the baseline ABI that your compiler targets, i.e. the binaries that your compiler produces are expected to be able to run on devices lacking the CRC feature.

至少对于 gcc,你可以使用 -march 修饰符 crc 启用这个功能,像这样:

At least for gcc, you can enable this feature with the -march modifier crc, like this:

$ gcc -dM -E - -march=armv8-a+crc < /dev/null | egrep -i '(arm|neon|acle|crc)'
#define __ARM_FEATURE_CRC32 1
#define __ARM_NEON 1

参见 https://gcc.gnu.org/onlinedocs/gcc-6.1.0/gcc/AArch64-Options.html(或旧 gcc 版本的同一页面)以获取有关如何设置的更多文档.

See https://gcc.gnu.org/onlinedocs/gcc-6.1.0/gcc/AArch64-Options.html (or the same page for older gcc versions) for more docs on how to set this.

我想人们可能希望 -march=native 做同样的事情,但该选项目前似乎只针对 x86 架构实现.

I guess one could expect -march=native to do the same, but that option currently only seems to be implemented for x86 architectures.

相关文章