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.

Functionx86_64aarch64
cpu_vendorthe 12-character cpuid stringthe designer from MIDR_EL1.Implementer (ARM, Qualcomm, Apple, ...)
cpu_brandthe 48-character brand stringthe core name for ARM cores (ARM Cortex-A72), otherwise the part number (Qualcomm part 0x804)
cpu_familyextended familythe implementer code (65 = ARM)
cpu_modelextended modelthe part number (3336 = 0xd08, Cortex-A72)
cpu_steppingsteppingrevision rNpM as N * 16 + M
cpu_cache_line_sizecpuid leaf 1smallest data cache line, CTR_EL0.DminLine
physical_address_bits, virtual_address_bitscpuid leaf 0x80000008-1: Linux hides those fields from user space
is_hypervisorcpuid leaf 1, ECX bit 31always 0: no equivalent bit
has_popcnt, has_fma, has_aes, has_rdrandtheir cpuid bitsCNT (NEON), FMADD (base FP), AES, RNDR

Constants

NameValueMeaning
SYS_SCHED_GETAFFINITYx86_64 204, aarch64 123
CPUID_LEAF_VENDOR0x86_64 only.
CPUID_LEAF_FEATURES1x86_64 only.
CPUID_LEAF_EXT_FEATURES7x86_64 only.
CPUID_LEAF_EXT_MAX2147483648x86_64 only. 0x80000000
CPUID_LEAF_BRAND_12147483650x86_64 only. 0x80000002
CPUID_LEAF_BRAND_22147483651x86_64 only. 0x80000003
CPUID_LEAF_BRAND_32147483652x86_64 only. 0x80000004
CPUID_LEAF_ADDRESS_SIZES2147483656x86_64 only. 0x80000008
CPUID_EDX_FPU1x86_64 only.
CPUID_EDX_MMX8388608x86_64 only.
CPUID_EDX_SSE33554432x86_64 only.
CPUID_EDX_SSE267108864x86_64 only.
CPUID_EDX_HTT268435456x86_64 only.
CPUID_ECX_SSE31x86_64 only.
CPUID_ECX_SSSE3512x86_64 only.
CPUID_ECX_FMA4096x86_64 only.
CPUID_ECX_SSE41524288x86_64 only.
CPUID_ECX_SSE421048576x86_64 only.
CPUID_ECX_POPCNT8388608x86_64 only.
CPUID_ECX_AES33554432x86_64 only.
CPUID_ECX_AVX268435456x86_64 only.
CPUID_ECX_RDRAND1073741824x86_64 only.
CPUID_ECX_HYPERVISOR2147483648x86_64 only.
CPUID_LEAF7_EBX_AVX232x86_64 only.
CPUID_LEAF7_EBX_AVX512F65536x86_64 only.
ID_AA64PFR0_FP16aarch64 only.
ID_AA64PFR0_ADVSIMD20aarch64 only.
ID_AA64PFR0_SVE32aarch64 only.
ID_AA64ISAR0_AES4aarch64 only.
ID_AA64ISAR0_RNDR60aarch64 only.
ID_AA64ZFR0_SVEVER0aarch64 only.
ID_FIELD_ABSENT15aarch64 only.

Classes

class
CpuidResult

x86_64 only. The four registers returned by cpuid.

    eax int
    ebx int
    ecx int
    edx int

Functions

def cpuid(leaf: int, subleaf: int, out: CpuidResult) -> None

x86_64 only.

Executes cpuid(leaf, subleaf) and stores EAX/EBX/ECX/EDX in out.

def cpu_vendor() -> str

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).

def cpu_brand() -> str

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.

def cpu_family() -> int

Extended family on x86_64, the implementer code on aarch64.

def cpu_model() -> int

Extended model on x86_64, the part number on aarch64.

def cpu_stepping() -> int

Stepping on x86_64; on aarch64 the revision rNpM as N * 16 + M.

def cpu_cache_line_size() -> int

Cache line size in bytes (the smallest data cache line on aarch64).

def physical_address_bits() -> int

Bits of physical addressing; -1 on aarch64, where Linux does not show the field to user space.

def virtual_address_bits() -> int

Bits of virtual addressing; -1 on aarch64.

def logical_cpu_count() -> int

Logical CPUs available to this process (sched_getaffinity).

def has_sse2() -> int

SSE2 support (0 on aarch64).

def has_sse3() -> int

SSE3 support (0 on aarch64).

def has_ssse3() -> int

SSSE3 support (0 on aarch64).

def has_sse41() -> int

SSE4.1 support (0 on aarch64).

def has_sse42() -> int

SSE4.2 support (0 on aarch64).

def has_avx() -> int

AVX support (the OS may still have it disabled; 0 on aarch64).

def has_avx2() -> int

AVX2 support (0 on aarch64).

def has_avx512f() -> int

AVX-512 Foundation support (0 on aarch64).

def has_aes() -> int

AES instructions: AES-NI on x86_64, the AES extension on aarch64.

def has_rdrand() -> int

A hardware random number instruction: RDRAND on x86_64, RNDR on aarch64.

def has_popcnt() -> int

Bit count instruction: POPCNT on x86_64, CNT (part of NEON) on aarch64.

def has_fma() -> int

Fused multiply-add: FMA3 on x86_64, FMADD (base floating point) on aarch64.

def has_mmx() -> int

MMX support (0 on aarch64).

def is_hypervisor() -> int

1 when running under a hypervisor (x86_64); always 0 on aarch64, which has no such bit.

def rdmsr(msr: int) -> int

x86_64 only.

Reads a model-specific register (ring 0 only: for kernel code).

def wrmsr(msr: int, value: int) -> None

x86_64 only.

Writes a model-specific register (ring 0 only).

def has_neon() -> int

NEON (AdvSIMD), the 128-bit vector registers of aarch64 (0 on x86_64).

def has_sve() -> int

SVE, the variable-length vectors of aarch64 (0 on x86_64).

def has_sve2() -> int

SVE2 (0 on x86_64).

def read_midr_el1() -> long

aarch64 only.

MIDR_EL1: implementer, variant, architecture, part number and revision of the core.
def read_ctr_el0() -> long

aarch64 only.

CTR_EL0: cache geometry.
def read_id_aa64pfr0_el1() -> long

aarch64 only.

ID_AA64PFR0_EL1: floating point, NEON, SVE.
def read_id_aa64isar0_el1() -> long

aarch64 only.

ID_AA64ISAR0_EL1: optional instructions (AES, SHA, CRC32, atomics, RNDR, ...).
def read_id_aa64zfr0_el1() -> long

aarch64 only.

ID_AA64ZFR0_EL1: the SVE version.
def id_reg_field(reg: long, shift: int, width: int) -> int

aarch64 only.

The width bits of an ID register starting at bit shift.

def arm_core_name(part: int) -> str

aarch64 only.

The name of a core designed by ARM from its part number, "" if not in the list.