devutils/ATFQ/book ATFQ git

Part I · How it works

2.1 What a CPU does: instructions and architectures

alsoinstruction setisaarchitecturecpu architecturex86-64x86_64amd64arm64aarch64armrisc-vriscvavxavx2avx-512neoncpu flagsx86-64-v3microarchitecture level

TL;DR

  • A CPU runs a stream of machine instructions: load, add, compare, jump, store. Which instructions exist, and how they're encoded, is its architecture: x86-64, ARM64 (aarch64) or RISC-V.
  • A binary is built for one architecture: an ARM64 program can't run on an x86-64 CPU, unless an emulator translates it.
  • Within one architecture, newer CPUs add extensions (AVX2, AVX-512 on x86; SVE on ARM). x86-64 groups them into levels v1 to v4.
uname -m                          # your architecture
grep -m1 -wo -E 'avx2|avx512f|sve' /proc/cpuinfo

On your machine

alice@laptopexample
$ atfq 2.1
  Arch      x86_64, made by Intel
  Level     x86-64-v4: programs built for this level or below run here
  Has       SSE 4.2, AVX2 (256-bit vectors), AVX-512, AES instructions, SHA
            instructions, virtualization (Intel VT-x)
  Flags     145 in all, in /proc/cpuinfo

An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.

Pitfalls

Deep dive 2 questions

How does a CPU run instructions so fast?

It works like an assembly line. Each instruction is fetched, decoded, executed and its result written back, and many instructions are at different stages at the same time: that's the pipeline. Modern cores go further: they decode several instructions per cycle, run them out of order as soon as their inputs are ready, and guess the outcome of branches to keep the pipeline full (2.5).

Why are there x86-64 levels?

x86-64 dates from 2003. Compilers target the original instruction set by default, so every program runs on every 64-bit PC, but most ignore 20 years of additions. The levels name common sets: v2 adds SSE4.2 and POPCNT (about 2009), v3 adds AVX2 and FMA (about 2013 to 2015), v4 adds AVX-512. Building for v3 makes some code noticeably faster, at the cost of older machines.

Commands
uname lscpu grep
Paths
/proc/cpuinfo
See also
18. CPU
2.2 Cores, threads and sockets
23.2 ELF and "command not found"