תכונות המעבד (CPU)
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
יש כמה דרכים לבדוק את התכונות של המעבד בקוד, ולכל אחת מהן יש יתרונות וחסרונות.
ABI: שימוש בפקודות מאקרו שהוגדרו מראש על ידי מעבד מקדים
בדרך כלל הכי נוח לקבוע את ה-ABI בזמן הבנייה באמצעות #ifdef
בצירוף:
-
__arm__
ל-ARM ב-32 ביט
-
__aarch64__
ל-ARM 64 ביט
-
__i386__
ל-32 ביט X86
-
__x86_64__
ל-X86 ב-64 ביט
שימו לב ש-32-bit X86 נקרא __i386__
ולא __x86__
, כפי שאולי ציפיתם.
מספר ליבות ה-CPU: משתמשים ב-sysconf(3) של libc
sysconf(3) מאפשרת לכם לשאול גם על _SC_NPROCESSORS_CONF
(מספר ליבות המעבד במערכת) וגם על _SC_NPROCESSORS_ONLN
(מספר ליבות המעבד שמחוברות כרגע).
תכונות: שימוש ב-getauxval(3) של libc
ב-API ברמה 18 ומעלה, הפונקציה getauxval(3) זמינה בספריית C של Android. הארגומנטים AT_HWCAP
ו-AT_HWCAP2
מחזירים מסכות ביטים שמפרטות תכונות ספציפיות למעבד. אפשר לעיין בכותרות השונות ב-NDK כדי לראות את הקבועים להשוואה, כמו HWCAP_SHA512
להוראות SHA512 של arm64, או HWCAP_IDIVT
להוראות חלוקת מספרים של Thumb של arm.hwcap.h
הספרייה cpu_features של Google
בעיה אחת ב-AT_HWCAP
היא שלפעמים יש טעות בזיהוי המכשירים. לדוגמה, חלק מהמכשירים הישנים טוענים שיש להם הוראות חלוקה של מספרים שלמים, אבל בפועל אין להם.
הספרייה cpu_features של Google פותרת בעיות כאלה על ידי שימוש בידע שלה לגבי מערכות SoC ספציפיות (על ידי ניתוח של /proc/cpuinfo
כדי לזהות את מערכת ה-SoC הספציפית הרלוונטית).
הספרייה הזו מתוחזקת לשימוש של צוותי האפליקציות מאינטראקציה ישירה של Google, ויש בה פתרונות לכל מכשיר בעייתי שהם נתקלו בו.
הספרייה cpufeatures של NDK (הוצאה משימוש)
ה-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."]]