Path: blob/master/3rdparty/cpufeatures/cpu-features.h
16337 views
/*1* Copyright (C) 2010 The Android Open Source Project2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* * Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* * Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in11* the documentation and/or other materials provided with the12* distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS15* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT16* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS17* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE18* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,20* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS21* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED22* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,23* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT24* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/27#ifndef CPU_FEATURES_H28#define CPU_FEATURES_H2930#include <sys/cdefs.h>31#include <stdint.h>3233__BEGIN_DECLS3435/* A list of valid values returned by android_getCpuFamily().36* They describe the CPU Architecture of the current process.37*/38typedef enum {39ANDROID_CPU_FAMILY_UNKNOWN = 0,40ANDROID_CPU_FAMILY_ARM,41ANDROID_CPU_FAMILY_X86,42ANDROID_CPU_FAMILY_MIPS,43ANDROID_CPU_FAMILY_ARM64,44ANDROID_CPU_FAMILY_X86_64,45ANDROID_CPU_FAMILY_MIPS64,4647ANDROID_CPU_FAMILY_MAX /* do not remove */4849} AndroidCpuFamily;5051/* Return the CPU family of the current process.52*53* Note that this matches the bitness of the current process. I.e. when54* running a 32-bit binary on a 64-bit capable CPU, this will return the55* 32-bit CPU family value.56*/57extern AndroidCpuFamily android_getCpuFamily(void);5859/* Return a bitmap describing a set of optional CPU features that are60* supported by the current device's CPU. The exact bit-flags returned61* depend on the value returned by android_getCpuFamily(). See the62* documentation for the ANDROID_CPU_*_FEATURE_* flags below for details.63*/64extern uint64_t android_getCpuFeatures(void);6566/* The list of feature flags for ANDROID_CPU_FAMILY_ARM that can be67* recognized by the library (see note below for 64-bit ARM). Value details68* are:69*70* VFPv2:71* CPU supports the VFPv2 instruction set. Many, but not all, ARMv6 CPUs72* support these instructions. VFPv2 is a subset of VFPv3 so this will73* be set whenever VFPv3 is set too.74*75* ARMv7:76* CPU supports the ARMv7-A basic instruction set.77* This feature is mandated by the 'armeabi-v7a' ABI.78*79* VFPv3:80* CPU supports the VFPv3-D16 instruction set, providing hardware FPU81* support for single and double precision floating point registers.82* Note that only 16 FPU registers are available by default, unless83* the D32 bit is set too. This feature is also mandated by the84* 'armeabi-v7a' ABI.85*86* VFP_D32:87* CPU VFP optional extension that provides 32 FPU registers,88* instead of 16. Note that ARM mandates this feature is the 'NEON'89* feature is implemented by the CPU.90*91* NEON:92* CPU FPU supports "ARM Advanced SIMD" instructions, also known as93* NEON. Note that this mandates the VFP_D32 feature as well, per the94* ARM Architecture specification.95*96* VFP_FP16:97* Half-width floating precision VFP extension. If set, the CPU98* supports instructions to perform floating-point operations on99* 16-bit registers. This is part of the VFPv4 specification, but100* not mandated by any Android ABI.101*102* VFP_FMA:103* Fused multiply-accumulate VFP instructions extension. Also part of104* the VFPv4 specification, but not mandated by any Android ABI.105*106* NEON_FMA:107* Fused multiply-accumulate NEON instructions extension. Optional108* extension from the VFPv4 specification, but not mandated by any109* Android ABI.110*111* IDIV_ARM:112* Integer division available in ARM mode. Only available113* on recent CPUs (e.g. Cortex-A15).114*115* IDIV_THUMB2:116* Integer division available in Thumb-2 mode. Only available117* on recent CPUs (e.g. Cortex-A15).118*119* iWMMXt:120* Optional extension that adds MMX registers and operations to an121* ARM CPU. This is only available on a few XScale-based CPU designs122* sold by Marvell. Pretty rare in practice.123*124* AES:125* CPU supports AES instructions. These instructions are only126* available for 32-bit applications running on ARMv8 CPU.127*128* CRC32:129* CPU supports CRC32 instructions. These instructions are only130* available for 32-bit applications running on ARMv8 CPU.131*132* SHA2:133* CPU supports SHA2 instructions. These instructions are only134* available for 32-bit applications running on ARMv8 CPU.135*136* SHA1:137* CPU supports SHA1 instructions. These instructions are only138* available for 32-bit applications running on ARMv8 CPU.139*140* PMULL:141* CPU supports 64-bit PMULL and PMULL2 instructions. These142* instructions are only available for 32-bit applications143* running on ARMv8 CPU.144*145* If you want to tell the compiler to generate code that targets one of146* the feature set above, you should probably use one of the following147* flags (for more details, see technical note at the end of this file):148*149* -mfpu=vfp150* -mfpu=vfpv2151* These are equivalent and tell GCC to use VFPv2 instructions for152* floating-point operations. Use this if you want your code to153* run on *some* ARMv6 devices, and any ARMv7-A device supported154* by Android.155*156* Generated code requires VFPv2 feature.157*158* -mfpu=vfpv3-d16159* Tell GCC to use VFPv3 instructions (using only 16 FPU registers).160* This should be generic code that runs on any CPU that supports the161* 'armeabi-v7a' Android ABI. Note that no ARMv6 CPU supports this.162*163* Generated code requires VFPv3 feature.164*165* -mfpu=vfpv3166* Tell GCC to use VFPv3 instructions with 32 FPU registers.167* Generated code requires VFPv3|VFP_D32 features.168*169* -mfpu=neon170* Tell GCC to use VFPv3 instructions with 32 FPU registers, and171* also support NEON intrinsics (see <arm_neon.h>).172* Generated code requires VFPv3|VFP_D32|NEON features.173*174* -mfpu=vfpv4-d16175* Generated code requires VFPv3|VFP_FP16|VFP_FMA features.176*177* -mfpu=vfpv4178* Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32 features.179*180* -mfpu=neon-vfpv4181* Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32|NEON|NEON_FMA182* features.183*184* -mcpu=cortex-a7185* -mcpu=cortex-a15186* Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32|187* NEON|NEON_FMA|IDIV_ARM|IDIV_THUMB2188* This flag implies -mfpu=neon-vfpv4.189*190* -mcpu=iwmmxt191* Allows the use of iWMMXt instrinsics with GCC.192*193* IMPORTANT NOTE: These flags should only be tested when194* android_getCpuFamily() returns ANDROID_CPU_FAMILY_ARM, i.e. this is a195* 32-bit process.196*197* When running a 64-bit ARM process on an ARMv8 CPU,198* android_getCpuFeatures() will return a different set of bitflags199*/200enum {201ANDROID_CPU_ARM_FEATURE_ARMv7 = (1 << 0),202ANDROID_CPU_ARM_FEATURE_VFPv3 = (1 << 1),203ANDROID_CPU_ARM_FEATURE_NEON = (1 << 2),204ANDROID_CPU_ARM_FEATURE_LDREX_STREX = (1 << 3),205ANDROID_CPU_ARM_FEATURE_VFPv2 = (1 << 4),206ANDROID_CPU_ARM_FEATURE_VFP_D32 = (1 << 5),207ANDROID_CPU_ARM_FEATURE_VFP_FP16 = (1 << 6),208ANDROID_CPU_ARM_FEATURE_VFP_FMA = (1 << 7),209ANDROID_CPU_ARM_FEATURE_NEON_FMA = (1 << 8),210ANDROID_CPU_ARM_FEATURE_IDIV_ARM = (1 << 9),211ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 = (1 << 10),212ANDROID_CPU_ARM_FEATURE_iWMMXt = (1 << 11),213ANDROID_CPU_ARM_FEATURE_AES = (1 << 12),214ANDROID_CPU_ARM_FEATURE_PMULL = (1 << 13),215ANDROID_CPU_ARM_FEATURE_SHA1 = (1 << 14),216ANDROID_CPU_ARM_FEATURE_SHA2 = (1 << 15),217ANDROID_CPU_ARM_FEATURE_CRC32 = (1 << 16),218};219220/* The bit flags corresponding to the output of android_getCpuFeatures()221* when android_getCpuFamily() returns ANDROID_CPU_FAMILY_ARM64. Value details222* are:223*224* FP:225* CPU has Floating-point unit.226*227* ASIMD:228* CPU has Advanced SIMD unit.229*230* AES:231* CPU supports AES instructions.232*233* CRC32:234* CPU supports CRC32 instructions.235*236* SHA2:237* CPU supports SHA2 instructions.238*239* SHA1:240* CPU supports SHA1 instructions.241*242* PMULL:243* CPU supports 64-bit PMULL and PMULL2 instructions.244*/245enum {246ANDROID_CPU_ARM64_FEATURE_FP = (1 << 0),247ANDROID_CPU_ARM64_FEATURE_ASIMD = (1 << 1),248ANDROID_CPU_ARM64_FEATURE_AES = (1 << 2),249ANDROID_CPU_ARM64_FEATURE_PMULL = (1 << 3),250ANDROID_CPU_ARM64_FEATURE_SHA1 = (1 << 4),251ANDROID_CPU_ARM64_FEATURE_SHA2 = (1 << 5),252ANDROID_CPU_ARM64_FEATURE_CRC32 = (1 << 6),253};254255/* The bit flags corresponding to the output of android_getCpuFeatures()256* when android_getCpuFamily() returns ANDROID_CPU_FAMILY_X86 or257* ANDROID_CPU_FAMILY_X86_64.258*/259enum {260ANDROID_CPU_X86_FEATURE_SSSE3 = (1 << 0),261ANDROID_CPU_X86_FEATURE_POPCNT = (1 << 1),262ANDROID_CPU_X86_FEATURE_MOVBE = (1 << 2),263ANDROID_CPU_X86_FEATURE_SSE4_1 = (1 << 3),264ANDROID_CPU_X86_FEATURE_SSE4_2 = (1 << 4),265ANDROID_CPU_X86_FEATURE_AES_NI = (1 << 5),266ANDROID_CPU_X86_FEATURE_AVX = (1 << 6),267ANDROID_CPU_X86_FEATURE_RDRAND = (1 << 7),268ANDROID_CPU_X86_FEATURE_AVX2 = (1 << 8),269ANDROID_CPU_X86_FEATURE_SHA_NI = (1 << 9),270};271272/* The bit flags corresponding to the output of android_getCpuFeatures()273* when android_getCpuFamily() returns ANDROID_CPU_FAMILY_MIPS274* or ANDROID_CPU_FAMILY_MIPS64. Values are:275*276* R6:277* CPU executes MIPS Release 6 instructions natively, and278* supports obsoleted R1..R5 instructions only via kernel traps.279*280* MSA:281* CPU supports Mips SIMD Architecture instructions.282*/283enum {284ANDROID_CPU_MIPS_FEATURE_R6 = (1 << 0),285ANDROID_CPU_MIPS_FEATURE_MSA = (1 << 1),286};287288289/* Return the number of CPU cores detected on this device. */290extern int android_getCpuCount(void);291292/* The following is used to force the CPU count and features293* mask in sandboxed processes. Under 4.1 and higher, these processes294* cannot access /proc, which is the only way to get information from295* the kernel about the current hardware (at least on ARM).296*297* It _must_ be called only once, and before any android_getCpuXXX298* function, any other case will fail.299*300* This function return 1 on success, and 0 on failure.301*/302extern int android_setCpu(int cpu_count,303uint64_t cpu_features);304305#ifdef __arm__306/* Retrieve the ARM 32-bit CPUID value from the kernel.307* Note that this cannot work on sandboxed processes under 4.1 and308* higher, unless you called android_setCpuArm() before.309*/310extern uint32_t android_getCpuIdArm(void);311312/* An ARM-specific variant of android_setCpu() that also allows you313* to set the ARM CPUID field.314*/315extern int android_setCpuArm(int cpu_count,316uint64_t cpu_features,317uint32_t cpu_id);318#endif319320__END_DECLS321322#endif /* CPU_FEATURES_H */323324325