问题描述
我有一些适用于 Android 的 C 代码,用于处理大量低级数字运算.我想知道我应该使用哪些设置(例如,对于我的 Android.mk 和 Application.mk)文件,以便生成的代码可以在所有当前的 Android 设备上运行,而且还可以利用针对特定芯片组的优化.我正在寻找好的默认 Android.mk 和 Application.mk 设置来使用,并且我想避免在我的 C 代码中乱扔#ifdef 分支.
I have some C code for Android that does lots of low-level number crunching. I'd like to know what settings I should use (e.g. for my Android.mk and Application.mk) files so that the code produced will run on all current Android devices but also takes advantage of optimisations for specific chipsets. I'm looking for good default Android.mk and Application.mk settings to use and I want to avoid having to litter my C code with #ifdef branches.
例如,我知道 ARMv7 具有浮点指令,并且一些 ARMv7 芯片支持 NEON 指令,而默认的 ARM 都不支持这些指令.是否可以设置标志以便我可以使用 NEON 构建 ARMv7、没有 NEON 的 ARMv7 以及默认的 ARM 构建?我知道如何做后两个,但不是全部 3.我对我使用的设置持谨慎态度,因为我认为当前的默认设置是最安全的设置以及其他选项有什么风险.
For example, I'm aware that ARMv7 has floating point instructions and some ARMv7 chips support NEON instructions and that the default ARM supports neither of these. Is it possible to set flags so that I can build ARMv7 with NEON, ARMv7 without NEON and the default ARM build? I'm know how to do the latter two but not all 3. I'm cautious about what settings I use as I assume the current defaults are the safest settings and what risks other options have.
对于 GCC 特定的优化,我使用以下标志:
For GCC specific optimisation, I'm using the following flags:
LOCAL_CFLAGS=-ffast-math -O3 -funroll-loops
我已经检查了所有这 3 个可以加快我的代码的速度.还有其他常见的我可以添加吗?
I've checked all 3 of these speed up my code. Are there any other common ones I could add?
我的另一个技巧是在 Android.mk 中添加LOCAL_ARM_MODE := arm",以加快较新的 arm 芯片的速度(尽管我对这到底是做什么的以及在旧芯片上会发生什么感到困惑).
Another tip I have is to add "LOCAL_ARM_MODE := arm" to Android.mk to enable a speed up on newer arm chips (although I'm confused at exactly what this does and what happens on older chips).
推荐答案
ARM 处理器有 2 个它们支持的通用指令集:ARM"和Thumb".尽管两者都有不同的风格,但 ARM 指令是 32 位的,而 Thumb 指令是 16 位的.两者的主要区别在于 ARM 指令在一条指令中可以做的事情比 Thumb 做的更多.例如,一条 ARM 指令可以将一个寄存器添加到另一个寄存器,同时对第二个寄存器执行左移.在 Thumb 中,一条指令必须进行移位,然后第二条指令将进行加法.
ARM processors have 2 general instruction sets that they support: "ARM" and "Thumb". Though there are different flavors of both, ARM instructions are 32 bits each and Thumb instructions are 16 bits. The main difference between the two is that ARM instructions have the possibility to do more in a single instruction than Thumb can. For example a single ARM instruction can add one register to another register, while performing a left shift on the second register. In Thumb one instruction would have to do the shift, then a second instruction would do the addition.
ARM 指令没有两倍好,但在某些情况下它们可以更快.在手卷 ARM 组件中尤其如此,可以以新颖的方式对其进行调整,以充分利用免费换班".拇指指令有其自身的优势和大小:它们消耗的电池更少.
ARM instructions are not twice as good, but in certain cases they can be faster. This is especially true in hand-rolled ARM assembly, which can be tuned in novel ways to make the best use of "shifts for free". Thumb instructions have their own advantage as well as size: they drain the battery less.
无论如何,这就是 LOCAL_ARM_MODE 所做的 - 这意味着您将代码编译为 ARM 指令而不是 Thumb 指令.编译到 Thumb 是 NDK 中的默认设置,因为它倾向于创建更小的二进制文件,并且对于大多数代码来说速度差异并不明显.编译器不能总是利用 ARM 可以提供的额外魅力",因此您最终还是需要或多或少相同数量的指令.
Anyway, this is what LOCAL_ARM_MODE does - it means you compile your code as ARM instructions instead of Thumb instructions. Compiling to Thumb is the default in the NDK as it tends to create a smaller binary and the speed difference is not that noticeable for most code. The compiler can't always take advantage of the extra "oomph" that ARM can provide, so you end up needing more or less the same number of instructions anyway.
您从编译到 ARM 或 Thumb 的 C/C++ 代码中看到的结果将是相同的(除了编译器错误).
The result of what you see from C/C++ code compiled to ARM or Thumb will be identical (barring compiler bugs).
这本身就兼容当今所有 Android 手机的新旧 ARM 处理器.这是因为默认情况下,NDK 编译为支持 ARMv5TE 指令集的基于 ARM 的 CPU 的应用程序二进制接口".这个 ABI 被称为armeabi",可以通过放置 APP_ABI := armeabi
在 Application.mk 中显式设置.
This by itself is compatible between new and old ARM processors for all Android phones available today. This is because by default the NDK compiles to an "Application Binary Interface" for ARM-based CPUs that support the ARMv5TE instruction set. This ABI is known as "armeabi" and can be explicitly set in the Application.mk by putting APP_ABI := armeabi
.
较新的处理器还支持称为 armeabi-v7a
的 Android 特定 ABI,它扩展了 armeabi 以添加 Thumb-2 指令集 和称为 VFPv3-D16 的硬件浮点指令集.armeabi-v7a 兼容的 CPU 还可以选择支持 NEON 指令集,您必须在运行时检查它并提供代码路径,以确定它何时可用,何时不可用.NDK/samples 目录中有一个执行此操作的示例(hello-neon).从本质上讲,Thumb-2 更像ARM",因为它的指令可以在一条指令中执行更多操作,同时还具有占用更少空间的优势.
Newer processors also support the Android-specific ABI known as armeabi-v7a
, which extends armeabi to add the Thumb-2 instruction set and a hardware floating point instruction set called VFPv3-D16. armeabi-v7a compatible CPUs can also optionally support the NEON instruction set, which you have to check for at run time and provide code paths for when it is available and when it is not. There's an example in the NDK/samples directory that does this (hello-neon). Under the hood, Thumb-2 is more "ARM-like" in that its instructions can do more in a single instruction, while having the advantage of still taking up less space.
为了编译包含 armeabi 和 armeabi-v7a 库的胖二进制文件",您需要将以下内容添加到 Application.mk:
In order to compile a "fat binary" that contains both armeabi and armeabi-v7a libraries you would add the following to Application.mk:
APP_ABI := armeabi armeabi-v7a
安装 .apk 文件后,Android 包管理器会为设备安装最佳库.因此,在较旧的平台上,它将安装 armeabi 库,在较新的设备上安装 armeabi-v7a 库.
When the .apk file is installed, the Android package manager installs the best library for the device. So on older platforms it would install the armeabi library, and on newer devices the armeabi-v7a one.
如果你想在运行时测试 CPU 特性,那么你可以使用 NDK 函数 uint64_t android_getCpuFeatures()
来获取处理器支持的特性.这将在 v7a 处理器上返回 ANDROID_CPU_ARM_FEATURE_ARMv7
的位标志,如果支持硬件浮点,则返回 ANDROID_CPU_ARM_FEATURE_VFPv3
,如果支持高级 SIMD 指令,则返回 ANDROID_CPU_ARM_FEATURE_NEON
.如果没有 VFPv3,ARM 就无法拥有 NEON.
If you want to test for CPU features at run time then you can use the NDK function uint64_t android_getCpuFeatures()
to get the features supported by the processor. This returns a bit-flag of ANDROID_CPU_ARM_FEATURE_ARMv7
on v7a processors, ANDROID_CPU_ARM_FEATURE_VFPv3
if hardware floating points are supported and ANDROID_CPU_ARM_FEATURE_NEON
if advanced SIMD instructions are supported. ARM can't have NEON without VFPv3.
总而言之:默认情况下,您的程序是最兼容的.由于使用 ARM 指令,使用 LOCAL_ARM_MODE 可能会以牺牲电池寿命为代价使事情变得稍微快一些 - 它与默认设置一样兼容.通过添加 APP_ABI := armeabi armeabi-v7a
行,您将在新设备上提高性能,保持与旧设备的兼容性,但您的 .apk 文件会更大(由于有 2 个库).为了使用 NEON 指令,您需要编写特殊代码来在运行时检测 CPU 的能力,这仅适用于可以运行 armeabi-v7a 的较新设备.
In summary: by default, your programs are the most compatible. Using LOCAL_ARM_MODE may make things slightly faster at the expense of battery life due to the use of ARM instructions - and it is as compatible as the default set-up. By adding the APP_ABI := armeabi armeabi-v7a
line you will have improved performance on newer devices, remain compatible with older ones, but your .apk file will be larger (due to having 2 libraries). In order to use NEON instructions, you will need to write special code that detects the capabilities of the CPU at run time, and this only applies to newer devices that can run armeabi-v7a.
这篇关于为多种架构生成优化的 NDK 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!