ฟีเจอร์ของ CPU
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
การตรวจสอบฟีเจอร์ CPU ในโค้ดทำได้หลายวิธี โดยแต่ละวิธีมีข้อดีข้อเสียแตกต่างกัน
ABI: ใช้มาโครที่กำหนดไว้ล่วงหน้าของตัวประมวลผลล่วงหน้า
โดยปกติแล้ว การกำหนด ABI ในเวลาบิลด์โดยใช้ #ifdef
ร่วมกับรายการต่อไปนี้จะสะดวกที่สุด
__arm__
สำหรับ ARM 32 บิต
__aarch64__
สำหรับ ARM 64 บิต
__i386__
สำหรับ X86 32 บิต
__x86_64__
สำหรับ X86 64 บิต
โปรดทราบว่า X86 32 บิตเรียกว่า __i386__
ไม่ใช่ __x86__
อย่างที่คุณอาจคาดไว้
จำนวนแกนของ CPU: ใช้ sysconf(3) ของ libc
sysconf(3) ช่วยให้คุณ
ค้นหาทั้ง _SC_NPROCESSORS_CONF
(จำนวนแกน CPU ในระบบ)
และ _SC_NPROCESSORS_ONLN
(จำนวนแกน CPU ที่ออนไลน์อยู่ในปัจจุบัน)
ฟีเจอร์: ใช้ getauxval(3) ของ libc
ใน API ระดับ 18 ขึ้นไป
getauxval(3)
จะพร้อมใช้งานในไลบรารี C ของ Android อาร์กิวเมนต์ AT_HWCAP
และ AT_HWCAP2
จะแสดงผลบิตมาสก์ที่แสดงฟีเจอร์เฉพาะของ CPU ดูส่วนหัวต่างๆ hwcap.h
ใน NDK เพื่อดูค่าคงที่ที่จะใช้เปรียบเทียบ เช่น HWCAP_SHA512
สำหรับคำสั่ง SHA512 ของ arm64 หรือ HWCAP_IDIVT
สำหรับคำสั่งหารจำนวนเต็ม Thumb ของ arm
ไลบรารี cpu_features ของ Google
ปัญหาอย่างหนึ่งของ AT_HWCAP
คือบางครั้งระบบอาจระบุอุปกรณ์ผิด อุปกรณ์รุ่นเก่าบางรุ่น เช่น อ้างว่ามีคำสั่งการหารจำนวนเต็ม แต่จริงๆ แล้วไม่มี
ไลบรารี cpu_features ของ Google แก้ปัญหาดังกล่าวโดยใช้ความรู้ของตนเองเกี่ยวกับ SoC ที่เฉพาะเจาะจง (โดยการแยกวิเคราะห์ /proc/cpuinfo
เพื่อหา SoC ที่เฉพาะเจาะจงที่เป็นปัญหา)
ไลบรารีนี้ได้รับการดูแลรักษาเพื่อให้ทีมแอปบุคคลที่หนึ่งของ Google ใช้งาน และ
มีวิธีแก้ปัญหาสำหรับอุปกรณ์ทุกเครื่องที่พบปัญหา
ไลบรารี cpufeatures ของ NDK (เลิกใช้งานแล้ว)
NDK ยังคงมีไลบรารีที่เลิกใช้งานแล้วชื่อ cpufeatures
สำหรับแหล่งที่มา
ที่เข้ากันได้กับแอปที่ใช้ไลบรารีนี้อยู่แล้ว ไลบรารีนี้เป็นไลบรารีเก่า
จึงไม่มีวิธีแก้ปัญหาสำหรับ SoC บางรุ่นมากเท่ากับไลบรารี cpu_features รุ่นใหม่กว่าและสมบูรณ์กว่า
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา 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."]]