【翻译】(8)CPU ARM Neon

----------------- 英文文档见android-ndk-r5b的documentation.html 属于Android Native Development Kit (NDK)的一部分

见http://developer.android.com/sdk/ndk/(需要代理) 翻译仅个人见解

----------------- Android NDK & ARM NEON instruction set extension support

Android NDK 和 ARM NEON指令集扩展支持

-------------------------------------------------------- Introduction: 介绍:-------------

Android NDK r3 added support for the new 'armeabi-v7a' ARM-based ABI that allows native code to use two useful instruction set extensions:

Android NDK r3添加对新的基于ARM的armeabi-v7a ABI的支持,允许原生代码使用两个有用指令集扩展:

- Thumb-2, which provides performance comparable to 32-bit ARM instructions with similar compactness to Thumb-1 - Thumb-2,

它提供与32位ARM指令可比的性能,也提供与Thumb-1相似的紧凑性 - VFPv3,

which provides hardware FPU registers and computations, to boost floating point performance significantly. - VFPv3,

它提供硬件浮点处理单元寄存器和计算能力,显著提升浮点性能。

More specifically, by default 'armeabi-v7a' only supports VFPv3-D16 which only uses/requires 16 hardware FPU 64-bit registers.

更显著的是,默认armeabi-v7a只支持VFPv3-D16,VFPv3-D16只使用/需要16个硬件浮点处理单元64位寄存器。

More information about this can be read in docs/CPU-ARCH-ABIS.html

更多相关信息可以阅读docs/CPU-ARCH-ABIS.html。

The ARMv7 Architecture Reference Manual also defines another optional instruction set extension known as "ARM Advanced SIMD", nick-named "NEON". It provides:

ARMv7架构参考手册还定义另一个可选的指令集合,即ARM高级SIMD,昵称为NEON。它提供:

- A set of interesting scalar/vector instructions and registers (the latter are mapped to the same chip area as the FPU ones), comparable to MMX/SSE/3DNow! in the x86 world.

- 一组有趣的标量/矢量指令(注:标量指令是指处理器每次处理一条数据,而矢量指令则相反,允许并行处理多条数据)和寄存器(后来被映射为相同的芯片领域如浮点运算单元寄存器),可以和x86世界的MMX/SSE/3DNow!相比。

- VFPv3-D32 as a requirement (i.e. 32 hardware FPU 64-bit registers, instead of the minimum of 16).

- VFPv3-D32作为一种最低需要(即32个硬件浮点单元64位寄存器,而非至少16个)。

Not all ARMv7-based Android devices will support NEON, but those that do may benefit in significant ways from the scalar/vector instructions.

不是所有基于ARMv7的Android设备会支持NEON,而那些支持NEON的设备可以从标矢量指令中很有意义地获得好处。

The NDK supports the compilation of modules or even specific source files with support for NEON. What this means is that a specific compiler flag will be used to enable the use of GCC ARM Neon intrinsics and VFPv3-D32 at the same time. The intrinsics are described here:

NDK支持模块的编译或甚至是特定的源代码,拥有对NEON的支持。这意味着将使用一个特定的编译器开关同时打开对GCC ARM Neon内建和VFPv3-D32的使用。内建功能在这里描述:

http://gcc.gnu.org/onlinedocs/gcc/ARM-NEON-Intrinsics.html LOCAL_ARM_NEON: LOCAL_ARM_NEON

--------------- Define LOCAL_ARM_NEON to 'true' in your module definition, and the NDK will build all its source files with NEON support. This can be useful if you want to build a static or shared library that specifically contains NEON code paths.

在你的模块定义中把LOCAL_ARM_NEON定义为true,NDK则会用NEON支持构建所有源文件。如果你想构建一个特定地包含NEON代码路径的静态或动态库,这可能有用。

Using the .neon suffix:

使用.neon后缀:

----------------------- When listing sources files in your LOCAL_SRC_FILES variable, you now have the option of using the .neon suffix to indicate that you want to corresponding source(s) to be built with Neon support. For example:

当在你的LOCAL_SRC_FILES变量中列出源文件时,你现在拥有使用.neon的选择,以指出你想把源代码相应地用Neon支持进行构建。例如:

LOCAL_SRC_FILES := foo.c.neon bar.c

Will only build 'foo.c' with NEON support. 将只对foo.c用NEON支持构建。

Note that the .neon suffix can be used with the .arm suffix too (used to specify the 32-bit ARM instruction set for non-NEON instructions), but must appear after it.

注意.neon后缀可以同时使用.arm后缀(用于指明对非NEON指令的32位ARM指令集),但必须出现在后面。

In other words, 'foo.c.arm.neon' works, but 'foo.c.neon.arm' does NOT.

换句话说,foo.c.arm.neon可以,但foo.c.neon.arm不可以。

Build Requirements:

构建需要:

------------------ Neon support only works when targetting the 'armeabi-v7a' ABI, otherwise the NDK build scripts will complain and abort. It is important to use checks like the following in your Android.mk:

Neon支持仅在目标是armeabi-v7a ABI时才可工作,否则NDK构建脚本将解释和中止。在你的Android.mk中使用类似如下方式的检查是很重要的。

# define a static library containing our NEON code

# 定义一个静态库,包含我们的NEON代码

ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)

include $(CLEAR_VARS)

LOCAL_MODULE := mylib-neon

LOCAL_SRC_FILES := mylib-neon.c

LOCAL_ARM_NEON := true

include $(BUILD_STATIC_LIBRARY)

endif #

TARGET_ARCH_ABI == armeabi-v7a

Runtime Detection:

运行时检测:

------------------ As said previously, NOT ALL ARMv7-BASED ANDROID DEVICES WILL SUPPORT NEON ! It is thus crucial to perform runtime detection to know if the NEON-capable machine code can be run on the target device.

正如前面所说的,不是所有基于ARMv7的Android设备支持NEON!因此最重要的是执行运行时检测以知道NEON能力的机器代码是否能运行在目标设备上。

To do that, use the 'cpufeatures' library that comes with this NDK. To lean more about it, see docs/CPU-FEATURES.html.

为了做到那一点,可以使用NDK提供的cpufeatures库。想知道更多相关信息,请参考docs/CPU-FEATURES.html。

You should explicitly check that android_getCpuFamily() returns ANDROID_CPU_FAMILY_ARM, and that android_getCpuFeatures() returns a value that has the ANDROID_CPU_ARM_FEATURE_NEON flag set, as in:

你应该显式地检查android_getCpuFamily()返回ANDROID_CPU_FAMILY_ARM,并且android_getCpuFeatures()返回一个拥有ANDROID_CPU_ARM_FEATURE_NEON标记位设置的值,正如这样:

#include 

...

...

if (android_getCpuFamily() == ANDROID_CPU_FAMILY_ARM && (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0)

{ // use NEON-optimized routines // 使用NEON优化例程 ...

} else { // use non-NEON fallback routines instead // 改为使用非NEON倒退例程

... } ...

Sample code:

示例代码:

------------ Look at the source code for the "hello-neon" sample in this NDK for an example on how to use the 'cpufeatures' library and Neon intrinsics at the same time.

查看这份NDK中hello-neon例子的源代码以获得关于如何同时使用cpufeatures库和Neon内建的例子。

This implements a tiny benchmark for a FIR filter loop using a C version, and a NEON-optimized one for devices that support it.

它实现了一个使用C版本的FIR(注:有限脉冲响应)滤波器循环,以及针对支持硬件NEON的设备的经过NEON优化的小型性能比较测试。

你可能感兴趣的:(cpu)