android ndk The cpufeatures Library

The cpufeatures Library

The NDK provides a small library named cpufeatures that your app can use at runtime to detect检测 the target device's CPU family and the optional features it supports. It is designed to work as-is on all official官方的 Android platform versions.

Usage用法


The cpufeatures library is available as an import module. To use it, follow the procedure过程 below:

  1. List cpufeatures in your list of static library dependencies. For example:
    LOCAL_STATIC_LIBRARIES := cpufeatures
    
  2. In your source code, include the <cpu-features.h> header file.
  3. At the end of your Android.mk file, insert an instruction to import the android/cpufeatures module. For example:
    $(call import-module,android/cpufeatures)
    

    Here is a simple example of an Android.mk file that imports the cpufeatures library:

    <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)
    

Functions


The cpufeatures library provides two functions. The first function returns the family to which the device's CPU belongs. Declare it as follows:

AndroidCpuFamily android_getCpuFamily();

The function returns one of the following enums, representing the CPU family/architecture that the device supports.

  • 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

For a 32-bit executable on a 64-bit system, this function returns only the 32-bit value.

The second function returns the set of optional features that the device's CPU supports. Declare it as follows:

uint64_t android_getCpuFeatures();

The return value takes the form of a set of bit flags位标志, each flag representing代表 one CPU-family-specific feature. The rest of this section provides information on features for the respective各自的 families.

32-bit ARM CPU family

The following flags are available for the 32-bit ARM CPU family:

ANDROID_CPU_ARM_FEATURE_VFPv2
Indicates表明 that the device's CPU supports the VFPv2 instruction set. Most ARMv6 CPUs support this instruction set.
ANDROID_CPU_ARM_FEATURE_ARMv7
Indicates that the device's CPU supports the ARMv7-A instruction set as supported by the  armeabi-v7aABI. This instruction set supports both Thumb-2 and VFPv3-D16 instructions. This return value also indicates support for the VFPv3 hardware FPU instruction-set extension.
ANDROID_CPU_ARM_FEATURE_VFPv3
Indicates that the device's CPU supports the VFPv3 hardware FPU instruction-set extension.

This value is equivalent等价 to the VFPv3-D16 instruction set, which provides provides only 16 hardware double-precision FP registers.

ANDROID_CPU_ARM_FEATURE_VFP_D32
Indicates that the device's CPU supports 32 hardware double-precision精度 FP registers instead of 16. Even when there are 32 hardware double-precision FP registers 浮点寄存器 , there are still only 32 single-precision registers mapped to the same register banks.
ANDROID_CPU_ARM_FEATURE_NEON
Indicates that the device's CPU supports the ARM Advanced SIMD (NEON) vector instruction set extension扩展. Note that ARM mandates(授权;命令) that such CPUs also implement VFPv3-D32, which provides 32 hardware FP registers (shared with the NEON unit).
ANDROID_CPU_ARM_FEATURE_VFP_FP16
Indicates that the device's CPU supports instructions to perform floating-point operations on 16-bit registers. This feature is part of the VFPv4 specification.
ANDROID_CPU_ARM_FEATURE_VFP_FMA
Indicates that the device's CPU supports the fused混合;熔解 multiply-accumulate 乘积累加 extension for the VFP instruction set. Also part of the VFPv4 specification.
ANDROID_CPU_ARM_FEATURE_NEON_FMA
Indicates that the device's CPU supports the fused multiply-accumulate extension for the NEON instruction set. Also part of the VFPv4 specification.
ANDROID_CPU_ARM_FEATURE_IDIV_ARM
Indicates that the device's CPU supports integer division in ARM mode. Only available on later- model CPUs, such as Cortex-A15.
ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2
Indicates that the device's CPU supports Integer division in Thumb-2 mode. Only available on later-model CPUs, such as Cortex-A15.
ANDROID_CPU_ARM_FEATURE_iWMMXt
Indicates that the device's CPU supports an instruction-set extension that adds MMX registers and instructions. This feature is only available on a few XScale- based CPUs.
ANDROID_CPU_ARM_FEATURE_LDREX_STREX
Indicates that the device's CPU supports LDREX and STREX instructions available since ARMv6. Together, these instructions provide atomic updates on memory with the help of exclusive monitor.
64-bit ARM CPU family

The following flags are available for the 64-bit ARM CPU family:

ANDROID_CPU_ARM64_FEATURE_FP
Indicates that the device's CPU has a Floating Point Unit (FPU). All Android ARM64 devices must support this feature.
ANDROID_CPU_ARM64_FEATURE_ASIMD
Indicates that the device's CPU has an Advanced SIMD (ASIMD) unit. All Android ARM64 devices must support this feature.
ANDROID_CPU_ARM64_FEATURE_AES
Indicates that the device's CPU supports  AES instructions.
ANDROID_CPU_ARM64_FEATURE_CRC32
Indicates that the device's CPU supports  CRC32 instructions.
ANDROID_CPU_ARM64_FEATURE_SHA1
Indicates that the device's CPU supports  SHA1 instructions.
ANDROID_CPU_ARM64_FEATURE_SHA2
Indicates that the device's CPU supports  SHA2 instructions.
ANDROID_CPU_ARM64_FEATURE_PMULL
Indicates that the device's CPU supports 64-bit  PMULL and  PMULL2 instructions.
32-bit x86 CPU family

The following flags are available for the 32-bit x86 CPU family.

ANDROID_CPU_X86_FEATURE_SSSE3Indicates that the device's CPU supports the SSSE3 instruction extension set. ANDROID_CPU_X86_FEATURE_POPCNT
Indicates that the device's CPU supports the  POPCNT instruction.
ANDROID_CPU_X86_FEATURE_MOVBE
Indicates that the device's CPU supports the  MOVBE instruction. This instruction is specific to some Intel IA-32 CPUs, such as Atom.

android_getCpuFeatures() returns 0 for CPU families for which there are no listed extensions.

The following function returns the maximum number of CPU cores on the target device:

int  android_getCpuCount(void);
MIPS CPU family
ANDROID_CPU_MIPS_FEATURE_R6
Indicates that the CPU executes MIPS Release 6 instructions natively, and supports obsoleted R1..R5 instructions only via kernel traps.
ANDROID_CPU_MIPS_FEATURE_MSA
Indicates that the CPU supports MIPS SIMD Architecture instructions.

Change History


For the complete change history of this library, see the comments in $NDK/sources/android/cpufeatures/cpu-features.c, where $NDK is the root of your NDK installation.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值