CPU features
Stay organized with collections
Save and categorize content based on your preferences.
There are several ways to check for CPU features in your code, each with a
different set of trade-offs.
ABI: Use the preprocessor's pre-defined macros
It's usually most convenient to determine the ABI at build time using #ifdef
in conjunction with:
__arm__
for 32-bit ARM
__aarch64__
for 64-bit ARM
__i386__
for 32-bit X86
__x86_64__
for 64-bit X86
Note that 32-bit X86 is called __i386__
, not __x86__
as you might expect!
CPU core counts: Use libc's sysconf(3)
sysconf(3) lets you
query both _SC_NPROCESSORS_CONF
(the number of CPU cores in the system)
and _SC_NPROCESSORS_ONLN
(the number of CPU cores currently online).
Features: Use libc's getauxval(3)
In API level 18 and newer,
getauxval(3)
is available in Android's C library. The AT_HWCAP
and AT_HWCAP2
arguments
return bitmasks listing CPU-specific features. See the various hwcap.h
headers in the NDK for the constants to compare against, such as HWCAP_SHA512
for arm64's SHA512 instructions, or HWCAP_IDIVT
for arm's Thumb integer
division instructions.
The Google cpu_features library
One problem with AT_HWCAP
is that sometimes devices are mistaken. Some old
devices, for example, claim to have integer division instructions but do not.
Google's cpu_features library works
around such issues by applying its own knowledge of specific SoCs (by parsing
/proc/cpuinfo
to work out the specific SoC in question).
This library is maintained for use by Google's first-party app teams, and
has workarounds for every problematic device they've encountered in the wild.
The NDK cpufeatures library (deprecated)
The NDK still provides a deprecated library named cpufeatures
for source
compatibility with apps that already use it. Unlike the newer and more complete
cpu_features library, this historical
library does not have workarounds for as many specific SoCs.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2021-11-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2021-11-16 UTC."],[],[],null,["# CPU features\n\nThere are several ways to check for CPU features in your code, each with a\ndifferent set of trade-offs.\n\nABI: Use the preprocessor's pre-defined macros\n----------------------------------------------\n\nIt's usually most convenient to determine the ABI at build time using `#ifdef`\nin conjunction with:\n\n- `__arm__` for 32-bit ARM\n- `__aarch64__` for 64-bit ARM\n- `__i386__` for 32-bit X86\n- `__x86_64__` for 64-bit X86\n\nNote that 32-bit X86 is called `__i386__`, not `__x86__` as you might expect!\n\nCPU core counts: Use libc's sysconf(3)\n--------------------------------------\n\n[sysconf(3)](http://man7.org/linux/man-pages/man3/sysconf.3.html) lets you\nquery both `_SC_NPROCESSORS_CONF` (the number of CPU cores in the system)\nand `_SC_NPROCESSORS_ONLN` (the number of CPU cores currently online).\n\nFeatures: Use libc's getauxval(3)\n---------------------------------\n\nIn API level 18 and newer,\n[getauxval(3)](http://man7.org/linux/man-pages/man3/getauxval.3.html)\nis available in Android's C library. The `AT_HWCAP` and `AT_HWCAP2` arguments\nreturn bitmasks listing CPU-specific features. See the various `hwcap.h`\nheaders in the NDK for the constants to compare against, such as `HWCAP_SHA512`\nfor arm64's SHA512 instructions, or `HWCAP_IDIVT` for arm's Thumb integer\ndivision instructions.\n\nThe Google cpu_features library\n-------------------------------\n\nOne problem with `AT_HWCAP` is that sometimes devices are mistaken. Some old\ndevices, for example, claim to have integer division instructions but do not.\n\n[Google's cpu_features](https://github.com/google/cpu_features) library works\naround such issues by applying its own knowledge of specific SoCs (by parsing\n`/proc/cpuinfo` to work out the specific SoC in question).\n\nThis library is maintained for use by Google's first-party app teams, and\nhas workarounds for every problematic device they've encountered in the wild.\n\nThe NDK cpufeatures library (deprecated)\n----------------------------------------\n\nThe NDK still provides a deprecated library named `cpufeatures` for source\ncompatibility with apps that already use it. Unlike the newer and more complete\n[cpu_features](https://github.com/google/cpu_features) library, this historical\nlibrary does not have workarounds for as many specific SoCs."]]