1 Android NDK & ARM NEON instruction set extension support 2 3 Introduction: 4 ==== 5 6 Android NDK r3 added support for the new 'armeabi-v7a' ARM-based ABI 7 that allows native code to use two useful instruction set extensions: 8 9 - Thumb-2, which provides performance comparable to 32-bit ARM 10 instructions with similar compactness to Thumb-1 11 12 - VFPv3, which provides hardware FPU registers and computations, 13 to boost floating point performance significantly. 14 15 More specifically, by default 'armeabi-v7a' only supports 16 VFPv3-D16 which only uses/requires 16 hardware FPU 64-bit registers. 17 18 More information about this can be read in docs/CPU-ARCH-ABIS.html 19 20 The ARMv7 Architecture Reference Manual also defines another optional 21 instruction set extension known as "ARM Advanced SIMD", nick-named 22 "NEON". It provides: 23 24 - A set of interesting scalar/vector instructions and registers 25 (the latter are mapped to the same chip area as the FPU ones), 26 comparable to MMX/SSE/3DNow! in the x86 world. 27 28 - VFPv3-D32 as a requirement (i.e. 32 hardware FPU 64-bit registers, 29 instead of the minimum of 16). 30 31 Not all ARMv7-based Android devices will support NEON, but those that 32 do may benefit in significant ways from the scalar/vector instructions. 33 34 The NDK supports the compilation of modules or even specific source 35 files with support for NEON. What this means is that a specific compiler 36 flag will be used to enable the use of GCC ARM Neon intrinsics and 37 VFPv3-D32 at the same time. The intrinsics are described here: 38 39 > http://gcc.gnu.org/onlinedocs/gcc/ARM-NEON-Intrinsics.html 40 41 42 Using LOCAL_ARM_NEON: 43 --------------------- 44 45 Define LOCAL_ARM_NEON to 'true' in your module definition, and the NDK 46 will build all its source files with NEON support. This can be useful if 47 you want to build a static or shared library that specifically contains 48 NEON code paths. 49 50 51 Using the .neon suffix: 52 ----------------------- 53 54 When listing sources files in your LOCAL_SRC_FILES variable, you now have 55 the option of using the .neon suffix to indicate that you want to 56 corresponding source(s) to be built with Neon support. For example: 57 58 LOCAL_SRC_FILES := foo.c.neon bar.c 59 60 Will only build 'foo.c' with NEON support. 61 62 Note that the .neon suffix can be used with the .arm suffix too (used to 63 specify the 32-bit ARM instruction set for non-NEON instructions), but must 64 appear after it. 65 66 In other words, 'foo.c.arm.neon' works, but 'foo.c.neon.arm' does NOT. 67 68 69 Build Requirements: 70 ------------------ 71 72 Neon support only works when targeting the 'armeabi-v7a' ABI, otherwise the 73 NDK build scripts will complain and abort. It is important to use checks like 74 the following in your Android.mk: 75 76 # define a static library containing our NEON code 77 ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) 78 include $(CLEAR_VARS) 79 LOCAL_MODULE := mylib-neon 80 LOCAL_SRC_FILES := mylib-neon.c 81 LOCAL_ARM_NEON := true 82 include $(BUILD_STATIC_LIBRARY) 83 endif # TARGET_ARCH_ABI == armeabi-v7a 84 85 86 Runtime Detection: 87 ------------------ 88 89 As said previously, NOT ALL ARMv7-BASED ANDROID DEVICES WILL SUPPORT NEON ! 90 It is thus crucial to perform runtime detection to know if the NEON-capable 91 machine code can be run on the target device. 92 93 To do that, use the 'cpufeatures' library that comes with this NDK. To learn 94 more about it, see docs/CPU-FEATURES.html. 95 96 You should explicitly check that android_getCpuFamily() returns 97 ANDROID_CPU_FAMILY_ARM, and that android_getCpuFeatures() returns a value 98 that has the ANDROID_CPU_ARM_FEATURE_NEON flag set, as in: 99 100 #include <cpu-features.h> 101 102 ... 103 ... 104 105 if (android_getCpuFamily() == ANDROID_CPU_FAMILY_ARM && 106 (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0) 107 { 108 // use NEON-optimized routines 109 ... 110 } 111 else 112 { 113 // use non-NEON fallback routines instead 114 ... 115 } 116 117 ... 118 119 Sample code: 120 ------------ 121 122 Look at the source code for the "hello-neon" sample in this NDK for an example 123 on how to use the 'cpufeatures' library and Neon intrinsics at the same time. 124 125 This implements a tiny benchmark for a FIR filter loop using a C version, and 126 a NEON-optimized one for devices that support it. 127