CPU 機能
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
CPU 機能をコード内で確認するには、いくつかの方法がありますが、それぞれに異なるトレードオフがあります。
ABI: プリプロセッサの定義済みマクロを使用する
通常は、#ifdef
と以下を併用して、ビルド時に ABI を指定しておくことをおすすめします。
__arm__
(32 ビット ARM の場合)
__aarch64__
(64 ビット ARM の場合)
__i386__
(32 ビット x86 の場合)
__x86_64__
(64 ビット x86 の場合)
32 ビット x86 は、予想される「__x86__
」ではなく「__i386__
」になることにご注意ください。
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
の問題の 1 つとして、デバイスが誤って解釈されることがあります。たとえば、古いデバイスの場合に「整数除算命令をサポートしている」と示されても、実際にはサポートしていないことがあります。
Google の cpu_features ライブラリは、各 SoC に関する独自のナレッジを適用することで(/proc/cpuinfo
を解析して、対象の SoC を判別することで)、このような問題を回避します。
このライブラリは Google のファーストパーティ アプリチーム用に保持されており、これまでに同チームが実際に遭遇した、問題のある各デバイスについての回避策を備えています。
NDK cpufeatures ライブラリ(サポート終了)
NDK では cpufeatures
ライブラリ(サポート終了)を引き続き提供しています。これは、すでにこのライブラリを使用しているアプリとのソースの互換性を維持するためです。より完全な新しい cpu_features ライブラリとは異なり、この古いライブラリには多くの特定の SoC に対する回避策は含まれていません。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-27 UTC。
[[["わかりやすい","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 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."]]