CPU Architecture
Inside a modern processor — registers, control unit, ALU, the instruction cycle, and how superscalar, out-of-order, speculative cores run billions of instructions per second.
The CPU executes the instruction stream. Conceptually it runs one instruction after another (fetch–decode–execute). Under the hood, modern cores juggle hundreds of instructions at once and only pretend to be sequential.
The main components
| Component | Role |
|---|---|
| Registers | A few dozen named, very fast storage slots (x86-64: 16 general-purpose; ARM64: 31). Operands live here. |
| Program counter (PC) | Address of the next instruction. |
| Control unit | Decodes instructions and steers data through the datapath. |
| ALU / FPU / vector units | Do integer, floating-point and SIMD arithmetic. |
| Load/store unit | Moves data between registers and memory through the caches. |
| Caches | Small SRAM memories close to the core (Cache memory). |
The performance equation
There are three ways to go faster: execute fewer instructions (better algorithms, compilers, ISA), complete more instructions per cycle (IPC), or shorten the cycle (a higher clock frequency). They trade off against each other: a deeper pipeline raises the clock but makes mispredictions more expensive.
How modern cores get high IPC
- Pipelining — overlap the stages of consecutive instructions (Pipelining).
- Superscalar — fetch, decode and execute several instructions per cycle (4–8 wide in modern cores).
- Out-of-order execution — run instructions as soon as their inputs are ready, not in program order. A reorder buffer retires them in order, so the result looks sequential.
- Register renaming — map the 16–32 architectural registers onto hundreds of physical ones, removing false dependencies (two instructions that merely reuse the same register name).
- Branch prediction — guess branch outcomes (over 95% accuracy) and execute ahead speculatively; discard the work on a misprediction.
- Memory-level parallelism — keep many cache misses in flight at once, plus hardware prefetching.
Branch prediction in practice
A misprediction flushes the pipeline: around 15–20 wasted cycles. Predictors learn patterns from history, so predictable branches are nearly free and random ones are expensive.
// Summing elements >= 128 is ~3-6x faster on a sorted array:
// the branch becomes predictable (all false, then all true).
long sum = 0;
for (int x : data) if (x >= 128) sum += x;
// Branchless version: no misprediction regardless of order.
for (int x : data) sum += x & ~((x - 128) >> 31);# CPython's interpreter overhead hides this effect; NumPy shows the
# vectorised, branch-free style instead:
import numpy as np
total = data[data >= 128].sum()let sum = 0;
for (const x of data) if (x >= 128) sum += x; // fast if data is sortedCISC vs RISC
| CISC (x86) | RISC (ARM, RISC-V) | |
|---|---|---|
| Instructions | Many, complex, variable length (1–15 bytes) | Fewer, simple, fixed length (4 bytes) |
| Memory operands | Arithmetic can read memory directly | Only loads and stores touch memory |
| Decoding | Complex; translated internally into RISC-like micro-ops | Simple |
| Today | Desktops and servers (Intel, AMD) | Phones, Apple Silicon, AWS Graviton, embedded |
Today the distinction is mostly about the instruction encoding: high-end x86 cores decode into micro-ops and run them on a RISC-like out-of-order engine. Details in Instruction set architecture.
Multicore and SMT
- Multicore — several independent cores on one chip, sharing the last-level cache and memory controller.
- Simultaneous multithreading (SMT, Intel's Hyper-Threading) — one core runs two hardware threads, filling execution slots the other thread leaves idle (for example while it waits on a cache miss). It typically gives 10–30% more throughput, not 2×.
More in Parallelism & multicore.
- A CPU has registers, a PC, a control unit, execution units and caches.
- CPU time = instructions × CPI × cycle time.
- Modern cores are pipelined, superscalar, out-of-order and speculative, but retire in order.
- Branch mispredictions cost ~15–20 cycles; predictable branches are nearly free.
- Clock speeds stalled around 2005; performance now comes from IPC, cores and accelerators.
QWhat's the difference between a core and a thread?easy
A core is a physical processor with its own execution units. A hardware thread is an execution context (registers, PC); with SMT one core runs two threads that share its execution units. Software threads are scheduled by the OS onto hardware threads.
QWhy can't we just keep increasing clock speed?medium
Dynamic power grows with frequency × voltage², and higher frequencies need higher voltage. Beyond around 4–5 GHz the heat can't be removed cheaply, so designers spend transistors on parallelism instead.