NDK Programmer's Guide
|
This NDK provides a small library named "`cpufeatures`" that can be used at runtime to detect the target device's CPU family and the optional features it supports.
The library is available as an import module. To use it, you must:
cpufeatures
' in your list of static library dependencies, as in: LOCAL_STATIC_LIBRARIES := cpufeatures
android/cpufeatures
' module, as in: $(call import-module,android/cpufeatures)
<cpu-features.h>
Here is a simple example:
<project-path>/jni/Android.mk: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := <your-module-name> LOCAL_SRC_FILES := <your-source-files> LOCAL_STATIC_LIBRARIES := cpufeatures include $(BUILD_SHARED_LIBRARY) $(call import-module,android/cpufeatures)
Two functions are provided for now:
AndroidCpuFamily android_getCpuFamily();
Returns a value matching the CPU family/architecture supported by the current process as an enum. Currently, the following families are defined:
ANDROID_CPU_FAMILY_ARM
ANDROID_CPU_FAMILY_X86
ANDROID_CPU_FAMILY_MIPS
ANDROID_CPU_FAMILY_ARM64
ANDROID_CPU_FAMILY_X86_64
ANDROID_CPU_FAMILY_MIPS64
Note that when running a 32-bit executable on a 64-bit system, this function will return the 32-bit family value only.
Secondly:
uint64_t android_getCpuFeatures();
Returns the set of optional features supported by the device's CPU. The result is a set of bit-flags, each corresponding to one CPU Family-specific optional feature.
Currently, only the following flags are defined, for the 32-bit ARM CPU Family:
ANDROID_CPU_ARM_FEATURE_VFPv2
ANDROID_CPU_ARM_FEATURE_ARMv7
ANDROID_CPU_ARM_FEATURE_VFPv3
Indicates that the device's CPU supports the VFPv3 hardware FPU instruction set extension. Due to the definition of 'armeabi-v7a', this will always be the case if ANDROID_CPU_ARM_FEATURE_ARMv7 is returned.
Note that this corresponds to the minimum profile VFPv3-D16 that only provides 16 hardware double-precision FP registers.
ANDROID_CPU_ARM_FEATURE_VFP_D32
ANDROID_CPU_ARM_FEATURE_NEON
ANDROID_CPU_ARM_FEATURE_VFP_FP16
ANDROID_CPU_ARM_FEATURE_VFP_FMA
ANDROID_CPU_ARM_FEATURE_NEON_FMA
ANDROID_CPU_ARM_FEATURE_IDIV_ARM
ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2
ANDROID_CPU_ARM_FEATURE_iWMMXt
ANDROID_CPU_ARM_FEATURE_LDREX_STREX
And the following flags for the 32-bit x86 CPU Family:
ANDROID_CPU_X86_FEATURE_SSSE3
ANDROID_CPU_X86_FEATURE_POPCNT
ANDROID_CPU_X86_FEATURE_MOVBE
And the following flags for the 64-bit ARM CPU Family:
ANDROID_CPU_ARM64_FEATURE_FP
ANDROID_CPU_ARM64_FEATURE_ASIMD
ANDROID_CPU_ARM64_FEATURE_AES
ANDROID_CPU_ARM64_FEATURE_CRC32
ANDROID_CPU_ARM64_FEATURE_SHA1
ANDROID_CPU_ARM64_FEATURE_SHA2
ANDROID_CPU_ARM64_FEATURE_PMULL
Other CPU families do not have extensions listed at the moment, which means that android_getCpuFeatures() will return 0 for them.
The following function is also defined to return the max number of CPU cores on the target device:
int android_getCpuCount(void);
The cpufeatures library will be updated to support more CPU families and optional features in the future. It is designed to work as-is on all official Android platform versions.
Please see the comments in $NDK/sources/android/cpufeatures/cpu-features.c
for the complete change history for this library.