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.

Intermediate 4 min readLesson 5 of 15 Interview Q&A

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

FetchPC · I-cache · branch predictorDecodeto micro-opsRename / schedulereorder bufferExecution unitsALUs · FPU · load/store · branchRegister fileL1 D-cache → L2 → L3 → RAM
ComponentRole
RegistersA 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 unitDecodes instructions and steers data through the datapath.
ALU / FPU / vector unitsDo integer, floating-point and SIMD arithmetic.
Load/store unitMoves data between registers and memory through the caches.
CachesSmall SRAM memories close to the core (Cache memory).

The performance equation

CPU time=instructions×cyclesinstruction×secondscycle\text{CPU time} = \text{instructions} \times \frac{\text{cycles}}{\text{instruction}} \times \frac{\text{seconds}}{\text{cycle}}

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

  1. Pipelining — overlap the stages of consecutive instructions (Pipelining).
  2. Superscalar — fetch, decode and execute several instructions per cycle (4–8 wide in modern cores).
  3. 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.
  4. 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).
  5. Branch prediction — guess branch outcomes (over 95% accuracy) and execute ahead speculatively; discard the work on a misprediction.
  6. 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.

The famous sorted-array effect
// 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);

CISC vs RISC

CISC (x86)RISC (ARM, RISC-V)
InstructionsMany, complex, variable length (1–15 bytes)Fewer, simple, fixed length (4 bytes)
Memory operandsArithmetic can read memory directlyOnly loads and stores touch memory
DecodingComplex; translated internally into RISC-like micro-opsSimple
TodayDesktops 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.

Key takeaways
  • 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.
Interview questions
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.