CPU 功能
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以通过多种方式检查代码中的 CPU 功能,但每种方式都需要做出不同的取舍。
ABI:使用预处理器的预定义宏
通常,在构建时使用 #ifdef
及以下各项确定 ABI 最为方便:
- 对于 32 位 ARM,使用
__arm__
- 对于 64 位 ARM,使用
__aarch64__
- 对于 32 位 X86,使用
__i386__
- 对于 64 位 X86,使用
__x86_64__
请注意:32 位 X86 称为 __i386__
,而不是 __x86__
,这可能与您预想的有所不同!
CPU 核心计数:使用 libc 的 sysconf(3)
sysconf(3) 既可以查询 _SC_NPROCESSORS_CONF
(系统中的 CPU 核心数),又可以查询 _SC_NPROCESSORS_ONLN
(当前可供使用的 CPU 核心数)。
功能:使用 libc 的 getauxval(3)
在 API 级别 18 及更高版本中,Android 的 C 库开始支持 getauxval(3)。AT_HWCAP
和 AT_HWCAP2
参数会返回列出特定 CPU 功能的位掩码。请参阅 NDK 中的各种 hwcap.h
头文件以获取要进行比较的常量,如用于 arm64 SHA512 指令的 HWCAP_SHA512
,或用于 arm Thumb 整数除法指令的 HWCAP_IDIVT
。
Google cpu_features 库
AT_HWCAP
的一个问题是有时设备会出错。例如,一些旧设备宣称拥有整数除法指令,但实际上并没有。
Google 的 cpu_features 库凭借其对特定 SoC 的了解(通过解析 /proc/cpuinfo
掌握相应的 SoC),解决了此类问题。
维护此库是为了供 Google 的第一方应用团队使用,并且此库中包含针对这些团队实际遇到的所有问题设备的解决方法。
NDK cpufeatures 库(已弃用)
NDK 仍然提供了一个名为 cpufeatures
的已废弃库,以便与已经使用此库的应用实现源代码兼容。与更新且更完善的 cpu_features 库不同,此旧库没有针对很多特定 SoC 设置权宜解决方法。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],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."]]