4.1.21 · D3Computer Architecture (Deep)

Worked examples — Branch prediction — static, dynamic (2-bit predictor, BTB)

4,064 words18 min readBack to topic

This page is the drill ground for the parent note. There we built the tools: the misprediction-penalty formula , the BTFNT static rule, the 1-bit and 2-bit predictors, and the BTB. Here we throw every kind of input at those tools — loops that always run, loops that alternate, nested loops, cold-start tables, degenerate zero-branch code, and an exam twist — and solve each fully.


The scenario matrix

Think of every branch-prediction question as landing in one of these cells. The worked examples below are labelled with the cell(s) they cover, and together they fill every cell.

# Cell (case class) What makes it special Example
A Static, backward branch loop bottom, BTFNT predicts taken Ex 1
B Static, forward branch if-skip, BTFNT predicts not-taken Ex 2
C 1-bit vs 2-bit on a long loop hysteresis pays off (2 misses → 1) Ex 3
D 2-bit on an alternating branch T,N,T,N… — worst case for 2-bit Ex 4
E Cold start / degenerate table predictor starts in wrong state Ex 5
F Zero-branch / degenerate code : prediction is irrelevant Ex 6
G Limiting behaviour of CPI , , deep Ex 7
H BTB hit vs miss direction right but target unknown Ex 8
I Real-world word problem full CPI budget of a program Ex 9
J Exam twist (nested loops) inner loop re-entry confuses 1-bit Ex 10

Example 1 — Cell A: static BTFNT on a backward branch

The figure below shows the two branch directions BTFNT keys on — this backward branch (Ex 1) and the forward branch of Ex 2 — so you can see why direction alone leaks the likely outcome.

Figure — Branch prediction — static, dynamic (2-bit predictor, BTB)

Reading the figure (Ex 1 side): the teal backward arrow curves from the loop bottom up to the loop top — that upward jump is what BTFNT reads as "loop, so predict Taken." It is right on all loop-backs and wrong only at the single downward exit.


Example 2 — Cell B: static BTFNT on a forward if

Reading the figure (Ex 2 side): the orange forward arrow points down to the handle() block — that jump only happens on the rare error. BTFNT predicts Not-taken (N), staying on the straight fall-through path, correct on the common case.


Example 3 — Cell C: 1-bit vs 2-bit on a long, repeatedly-entered loop

The figure below walks one full entry (T,T,T,N) step by step, plotting both predictors on the same state axis so you can watch the 1-bit line collapse to Not-taken on the exit while the 2-bit line only slips one notch.

Figure — Branch prediction — static, dynamic (2-bit predictor, BTB)

Reading the figure: follow the teal line (2-bit state ). It rides along (top region = "predict Taken", shaded teal), and the exit N only nudges it still inside the Taken region, so re-entry is safe. The orange dashed line (1-bit, drawn scaled) drops all the way into the Not-taken region on the exit, so its very next re-entry T is a fresh miss. The two purple "outcome" labels along the top mark where each predictor is tested.


Example 4 — Cell D: the 2-bit counter's worst case (alternating branch)

The figure shows the counter trapped in the two middle states, never reaching either "Strongly" corner — the visual signature of "wrong every step."

Figure — Branch prediction — static, dynamic (2-bit predictor, BTB)

Reading the figure: the four states are drawn as boxes left-to-right (). The plum arrows show the actual trajectory: it ping-pongs only between the two middle boxes and , never touching the shaded "Strongly" ends. Because the prediction (top bit) flips exactly out of phase with the outcomes, every test is a red ✗.


Example 5 — Cell E: cold start, table in the wrong state

The figure plots the counter climbing out of : two wrong guesses while it warms up, then correct, then the unavoidable exit miss.

Figure — Branch prediction — static, dynamic (2-bit predictor, BTB)

Reading the figure: the teal staircase starts in the bottom (Not-taken) region and climbs one step per Taken outcome. The first two markers sit in the orange "predict Not-taken" band → both are ✗ (wrong direction). Only once the line crosses into the teal "predict Taken" band do guesses turn ✓; the final drop is the exit miss.


Example 6 — Cell F: degenerate code with no branches ()


Example 7 — Cell G: limiting behaviour of

The figure draws CPI as a straight line against for the deep () and a shallow () pipeline, so the steeper slope of the deep pipeline visually equals "misses hurt more."

Figure — Branch prediction — static, dynamic (2-bit predictor, BTB)

Reading the figure: two straight lines both start at CPI when (perfect predictor, left edge). The orange deep-pipeline line () climbs steeply to at ; the teal shallow line () barely rises to . The three plum dots mark our computed points . Steeper line = deeper pipeline = prediction matters more.


Example 8 — Cell H: correct direction, but BTB miss

The figure contrasts the two fetch timelines: a BTB miss (bubble of cycles before the target is known) versus a BTB hit (target ready in IF, zero bubble).

Figure — Branch prediction — static, dynamic (2-bit predictor, BTB)

Reading the figure: the top timeline (orange, BTB miss) shows two hatched "bubble" slots where fetch has no target yet — that is the penalty. The bottom timeline (teal, BTB hit) jumps straight from the branch's IF to the target's IF with no gap. Same correct direction, wildly different cost — the BTB is what closes the gap.


Example 9 — Cell I: real-world CPI budget (word problem)


Example 10 — Cell J: exam twist — nested loops fool the 1-bit predictor

The figure stacks the two predictors' miss patterns across all four activations, so the extra orange (1-bit re-entry) misses stand out against the steady single teal (2-bit exit) miss per activation.

Figure — Branch prediction — static, dynamic (2-bit predictor, BTB)

Reading the figure: each of the four activation blocks shows the T,T,N outcomes as ticks. On the 1-bit row, misses (✗) land on both the exit N and the following re-entry T (except the very first block). On the 2-bit row, only the single exit N of each block is a miss. Count the ✗ marks: vs .


Recall Quick self-test — which cell is this?

"A branch alternates T,N,T,N forever; the 2-bit counter guesses wrong every time." Which matrix cell, and what's the fix? ::: Cell D (alternating worst case); fix = correlating/history predictor, not more local bits. "A fully unrolled kernel has no conditional branches; predictor accuracy is irrelevant." Which cell? ::: Cell F (, degenerate). "First visit to a taken branch: direction right, but fetch still stalls." Which cell and why? ::: Cell H — BTB miss; the target wasn't cached yet.