CPU 功能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如要檢查程式碼中的 CPU 功能,有多種方法可以選擇,但每種方法都各有優缺點。
ABI:使用前置處理器的預先定義巨集
如要確定 ABI,最方便的做法通常是在建構時使用 #ifdef
與以下項目:
- 針對 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 和/或其關係企業的商標或註冊商標。
上次更新時間: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"]],["上次更新時間: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."]]