libstd/cpuinfo
CPU identification, with the same functions on x86_64 and aarch64: vendor, brand, family/model/stepping, cache line size, address widths, feature flags, and the number of logical CPUs available to the process. On x86_64 the answers come from the cpuid instruction. On aarch64 (Linux) they come from the system ID registers read with mrs, which Linux emulates for user space: MIDR_EL1 and the feature fields are real, the other fields read as 0.
#include <libstd/cpuinfo>
Includes libstd/type.
Example
#include <libstd/stdio>
#include <libstd/cpuinfo>
print(cpu_vendor(), " / ", cpu_brand(), "\n")
print("AVX2: ", has_avx2(), " SVE2: ", has_sve2(), " logical CPUs: ", logical_cpu_count(), "\n")
if is_hypervisor():
print("running in a VM\n")
The flags of one architecture's extensions are 0 on the other: has_avx2() on aarch64, has_sve2() on x86_64. A program checks what it needs on both without #ifdef.
| Function | x86_64 | aarch64 |
|---|---|---|
cpu_vendor | the 12-character cpuid string | the designer from MIDR_EL1.Implementer (ARM, Qualcomm, Apple, ...) |
cpu_brand | the 48-character brand string | the core name for ARM cores (ARM Cortex-A72), otherwise the part number (Qualcomm part 0x804) |
cpu_family | extended family | the implementer code (65 = ARM) |
cpu_model | extended model | the part number (3336 = 0xd08, Cortex-A72) |
cpu_stepping | stepping | revision rNpM as N * 16 + M |
cpu_cache_line_size | cpuid leaf 1 | smallest data cache line, CTR_EL0.DminLine |
physical_address_bits, virtual_address_bits | cpuid leaf 0x80000008 | -1: Linux hides those fields from user space |
is_hypervisor | cpuid leaf 1, ECX bit 31 | always 0: no equivalent bit |
has_popcnt, has_fma, has_aes, has_rdrand | their cpuid bits | CNT (NEON), FMADD (base FP), AES, RNDR |
Constants
| Name | Value | Meaning |
|---|---|---|
SYS_SCHED_GETAFFINITY | x86_64 204, aarch64 123 | |
CPUID_LEAF_VENDOR | 0 | x86_64 only. |
CPUID_LEAF_FEATURES | 1 | x86_64 only. |
CPUID_LEAF_EXT_FEATURES | 7 | x86_64 only. |
CPUID_LEAF_EXT_MAX | 2147483648 | x86_64 only. 0x80000000 |
CPUID_LEAF_BRAND_1 | 2147483650 | x86_64 only. 0x80000002 |
CPUID_LEAF_BRAND_2 | 2147483651 | x86_64 only. 0x80000003 |
CPUID_LEAF_BRAND_3 | 2147483652 | x86_64 only. 0x80000004 |
CPUID_LEAF_ADDRESS_SIZES | 2147483656 | x86_64 only. 0x80000008 |
CPUID_EDX_FPU | 1 | x86_64 only. |
CPUID_EDX_MMX | 8388608 | x86_64 only. |
CPUID_EDX_SSE | 33554432 | x86_64 only. |
CPUID_EDX_SSE2 | 67108864 | x86_64 only. |
CPUID_EDX_HTT | 268435456 | x86_64 only. |
CPUID_ECX_SSE3 | 1 | x86_64 only. |
CPUID_ECX_SSSE3 | 512 | x86_64 only. |
CPUID_ECX_FMA | 4096 | x86_64 only. |
CPUID_ECX_SSE41 | 524288 | x86_64 only. |
CPUID_ECX_SSE42 | 1048576 | x86_64 only. |
CPUID_ECX_POPCNT | 8388608 | x86_64 only. |
CPUID_ECX_AES | 33554432 | x86_64 only. |
CPUID_ECX_AVX | 268435456 | x86_64 only. |
CPUID_ECX_RDRAND | 1073741824 | x86_64 only. |
CPUID_ECX_HYPERVISOR | 2147483648 | x86_64 only. |
CPUID_LEAF7_EBX_AVX2 | 32 | x86_64 only. |
CPUID_LEAF7_EBX_AVX512F | 65536 | x86_64 only. |
ID_AA64PFR0_FP | 16 | aarch64 only. |
ID_AA64PFR0_ADVSIMD | 20 | aarch64 only. |
ID_AA64PFR0_SVE | 32 | aarch64 only. |
ID_AA64ISAR0_AES | 4 | aarch64 only. |
ID_AA64ISAR0_RNDR | 60 | aarch64 only. |
ID_AA64ZFR0_SVEVER | 0 | aarch64 only. |
ID_FIELD_ABSENT | 15 | aarch64 only. |
Classes
x86_64 only. The four registers returned by cpuid.
eax int
ebx int
ecx int
edx intFunctions
x86_64 only.
Executes cpuid(leaf, subleaf) and stores EAX/EBX/ECX/EDX in out.
Who designed the CPU: the cpuid vendor string (GenuineIntel, AuthenticAMD) on x86_64, the implementer name on aarch64 (Unknown for a code not in the list).
The brand string on x86_64, e.g. Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz (may have trailing spaces). aarch64 has no brand string: the core name for ARM cores, the part number in hex for the others.
Extended family on x86_64, the implementer code on aarch64.
Extended model on x86_64, the part number on aarch64.
Stepping on x86_64; on aarch64 the revision rNpM as N * 16 + M.
Cache line size in bytes (the smallest data cache line on aarch64).
Bits of physical addressing; -1 on aarch64, where Linux does not show the field to user space.
Bits of virtual addressing; -1 on aarch64.
Logical CPUs available to this process (sched_getaffinity).
SSE2 support (0 on aarch64).
SSE3 support (0 on aarch64).
SSSE3 support (0 on aarch64).
SSE4.1 support (0 on aarch64).
SSE4.2 support (0 on aarch64).
AVX support (the OS may still have it disabled; 0 on aarch64).
AVX2 support (0 on aarch64).
AVX-512 Foundation support (0 on aarch64).
AES instructions: AES-NI on x86_64, the AES extension on aarch64.
A hardware random number instruction: RDRAND on x86_64, RNDR on aarch64.
Bit count instruction: POPCNT on x86_64, CNT (part of NEON) on aarch64.
Fused multiply-add: FMA3 on x86_64, FMADD (base floating point) on aarch64.
MMX support (0 on aarch64).
1 when running under a hypervisor (x86_64); always 0 on aarch64, which has no such bit.
x86_64 only.
Reads a model-specific register (ring 0 only: for kernel code).
x86_64 only.
Writes a model-specific register (ring 0 only).
NEON (AdvSIMD), the 128-bit vector registers of aarch64 (0 on x86_64).
SVE, the variable-length vectors of aarch64 (0 on x86_64).
SVE2 (0 on x86_64).
aarch64 only.
MIDR_EL1: implementer, variant, architecture, part number and revision of the core.aarch64 only.
CTR_EL0: cache geometry.aarch64 only.
ID_AA64PFR0_EL1: floating point, NEON, SVE.aarch64 only.
ID_AA64ISAR0_EL1: optional instructions (AES, SHA, CRC32, atomics, RNDR, ...).aarch64 only.
ID_AA64ZFR0_EL1: the SVE version.aarch64 only.
The width bits of an ID register starting at bit shift.
aarch64 only.
The name of a core designed by ARM from its part number, "" if not in the list.