检测预处理器中的 ARM NEON 可用性? [英] Detect ARM NEON availability in the preprocessor?
问题描述
根据ARM ARM、__ARM_NEON__
在 Neon SIMD 指令可用时定义.我无法让 GCC 提供它.
According to the ARM ARM, __ARM_NEON__
is defined when Neon SIMD instructions are available. I'm having trouble getting GCC to provide it.
运行 Debian 8.2 的 BananaPi Pro 开发板提供了 Neon:
Neon available on this BananaPi Pro dev board running Debian 8.2:
$ cat /proc/cpuinfo | grep neon
Features : swp half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt
我使用的是 GCC 4.9:
I'm using GCC 4.9:
$ gcc --version
gcc (Debian 4.9.2-10) 4.9.2
试试 GCC 和 -march=native
:
$ g++ -march=native -dM -E - </dev/null | grep -i neon
#define __ARM_NEON_FP 4
好的,试试 Google 在为 Neon 构建时为 Android 使用的东西:
OK, try what Google uses for Android when building for Neon:
$ g++ -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -dM -E - </dev/null | grep -i neon
#define __ARM_NEON_FP 4
也许是带有硬浮点的 ARMv7-a:
Maybe a ARMv7-a with a hard float:
$ g++ -march=armv7-a -mfloat-abi=hard -dM -E - </dev/null | grep -i neon
#define __ARM_NEON_FP 4
我的问题是:
- 为什么我没有看到
__ARM_NEON__
? - 如何检测预处理器中的 Neon 可用性?
也许:
- 我应该使用哪些 GCC 开关来启用 Neon SIMD 指令?
相关,在 LeMaker HiKey 上,它是 AARCH64/ARM64 运行 Linaro 和 GCC4.9.2,这是预处理器的输出:
Related, on a LeMaker HiKey, which is AARCH64/ARM64 running Linaro with GCC 4.9.2, here's the output from the preprocessor:
$ cpp -dM </dev/null | grep -i neon
#define __ARM_NEON 1
根据 ARM,该板确实具有高级 SIMD 指令,即使:
According to ARM, this board does have Advanced SIMD instructions even though:
$ cat /proc/cpuinfo
Processor : AArch64 Processor rev 3 (aarch64)
...
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
推荐答案
这里隐藏了很多问题,我会尝试依次提取...
There are a number of questions hidden in here, I'll try to extract them in turn...
根据 ARM ARM,__ARM_NEON__
是在 Neon SIMD 指令可用时定义的.我无法让 GCC 提供它.
According to the ARM ARM,
__ARM_NEON__
is defined when Neon SIMD instructions are available. I'm having trouble getting GCC to provide it.
这是[旧版本] ARM 编译器的编译器文档,而不是 ARM 架构参考手册.__ARM_NEON
是一个更好的检查高级 SIMD 指令存在的宏,它在 ARM C 语言扩展.
That is compiler documentation for [an old version of] the ARM Compiler rather than the ARM Architceture Reference Manual. A better macro to check for the presence of the Advanced SIMD instructions would be __ARM_NEON
, which is defined in the ARM C Language Extensions.
试试 GCC 和 -march=native
:
正如您可能发现的.用于 ARM 目标的 GCC 分离出 -march
(对于 GCC 应该为其生成代码的架构版本)、-mfpu
(对于可用的浮点/高级 SIMD 单元)和-mfloat-abi
(关于如何传递浮点参数,以及是否存在浮点单元).最后是 -mtune
(要求 GCC 尝试针对特定处理器进行优化)和 -mcpu
(作为 -mtune
和 -march
).
As you may have found. GCC for the ARM target separates out -march
(For the architecture revision for which GCC should generate code), -mfpu
(For the floating point/Advanced SIMD unit available) and -mfloat-abi
(For how floating point arguments should be passed, and for the presence or absence of a floating point unit). Finally there is -mtune
(Which asks GCC to try to optimise for a particular processor) and -mcpu
(which acts as a combination of -mtune
and -march
).
通过要求 -march=native
您要求 GCC 生成适用于您正在运行的处理器的检测架构的代码.这对 -mfpu
设置没有影响,因此不一定启用高级 SIMD 指令生成.
By asking for -march=native
You're asking GCC to generate code appropriate for the detected architecture of the processor on which you are running. This has no impact on the -mfpu
setting, and so does not necessarily enable Advanced SIMD instruction generation.
请注意,以上内容仅适用于针对 AArch32 的编译器.AArch64 GCC 不支持 -mfpu
并将通过 -march=native
检测是否存在高级 SIMD 支持.
Note that the above only applies to a compiler targeting AArch32. The AArch64 GCC does not support -mfpu
and will detect presence of Advanced SIMD support through -march=native
.
好的,试试 Google 在为 Neon 构建时为 Android 使用的东西:
OK, try what Google uses for Android when building for Neon:
$ g++ -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -dM -E
这些构建标志不足以支持高级 SIMD 指令,您的注释可能不完整.-mfpu
标志支持 由 GCC 4.9.2 我期望:
These build flags are not sufficient to enable support for Advanced SIMD instructions, your notes may be incomplete. Of the -mfpu
flags supported by GCC 4.9.2 I'd expect any of:
neon
、neon-fp16
、neon-vfpv4
、neon-fp-armv8
、crypto-neon-fp-armv8
给你你想要的.
根据 ARM,该板确实具有高级 SIMD 指令,即使:
According to ARM, this board does have Advanced SIMD instructions even though:
看起来您在 AArch64 内核上运行,该内核通过 asimd
功能公开了对高级 SIMD 的支持 - 如您的示例输出所示.
Looks like you're running on an AArch64 kernel, which exposes support for Advanced SIMD through the asimd
feature - as in your example output.
这篇关于检测预处理器中的 ARM NEON 可用性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!