Branch Predictor Simulator
Prerequisites: The CPU Pipeline and Branch Prediction, Branches, Indirect Calls, and Inlining
The AAPL update loop tests if (update.price > threshold) on every update. The CPU Pipeline and Branch Prediction described a predictor that guesses each branch's direction from its history; every wrong guess flushes the pipeline.
Build two such predictors and run them on a branch trace.
- Branch trace: a record of every conditional branch a program executed, in order.
pc(program counter): the address of the branch instruction. Everyifin the source has its ownpc, and each trace entry names its branch bypc.- Outcome:
Tif the branch was taken,Nif it was not.
What to implement
simulate_predictor(predictor, index_bits, history_bits, trace)returns the printed lines, in order.predictorisBIMODAL,GSHARE, orBOTH.index_bitsisk.history_bitsish, the width of the history register from Part 2.- Each line of
traceis one entry, in one of the forms below. - On stdin, the first three lines are
predictor,index_bits, andhistory_bits. The fourth is the number of trace entries, and each remaining line is one entry. - There are three parts, each adding behavior on top of the previous one. One solution must pass all ten tests.
| Trace line | Meaning |
|---|---|
pc outcome | one branch at pc with outcome T or N |
LOOP pc n | shorthand for n branches at pc (Part 3) |
2-bit saturating counters
Both predictors are built from 2-bit saturating counters. Each holds an integer from 0 to 3, and its value is a state that decides the prediction:
| Value | State | Predicts |
|---|---|---|
| 0 | strongly not-taken | N |
| 1 | weakly not-taken | N |
| 2 | weakly taken | T |
| 3 | strongly taken | T |
- Once the real outcome is known, the counter trains: a taken branch adds 1 unless it is already 3, and a not-taken branch subtracts 1 unless it is already 0.
- The counter saturates at both ends instead of wrapping, so a branch must surprise a strong state twice in a row before the prediction flips.
- A predictor keeps a table of
2^kcounters, numbered0to2^k - 1. Every counter starts at 1, weakly not-taken.
For each branch, the predictor:
- computes an index into its table;
- reads the counter there;
- predicts
Tif the counter is 2 or 3,Nif it is 0 or 1; - compares the prediction with the outcome and trains the counter.
Part 1: The bimodal predictor
- The bimodal predictor indexes its table by the branch's address alone:
index = pc mod 2^k, the lowkbits ofpc, orpc & (2^k - 1). - Each branch therefore trains the counter at its own index. A branch that is usually taken pushes its counter toward 3.
- Two branches whose
pcvalues share their lowkbits read and train the same counter. This is aliasing, and the simulator does not separate them: if one is usually taken and the other usually not, each undoes the other's training.
Output (BIMODAL and GSHARE modes): one line per branch, then a TOTAL line after the last branch.
pc index counter prediction outcome result TOTAL branches MISSES misses
counteris the value read before training.resultisHITwhen the prediction matched the outcome andMISSwhen it did not.branchesis the number of branches;missesis the number of mispredictions.
Limits
kis between 1 and 12.his between 0 andk, and it is 0 wheneverpredictorisBIMODAL.- Every
pcis between 0 and 109, sopcand every value computed from it fit in a 32-bitint. - The trace has between 1 and 104 lines, and every outcome is exactly
TorN. - The whole trace expands to at most 105 branches.
Tests 1–3 cover this part. They use BIMODAL with history_bits equal to 0.
Part 2: Gshare and the global history register
A bimodal counter learns only a branch's usual direction. It mispredicts at least half of an alternating run T, N, T, N, which is what the price check produces when the price hovers around threshold.
Gshare adds a global history register: an h-bit integer whose bits are the outcomes of the last h branches executed, from any pc (1 for taken, 0 for not taken).
- The newest outcome is bit 0. The register starts at 0, as if the last
hbranches were not taken. - The index is
index = (pc ^ history) mod 2^k, where^is bitwise XOR: each bit of the result is 1 where exactly one of the two input bits is 1. - The index uses the history from before the current branch, because the predictor must guess before the outcome is known.
- After the counter trains, the outcome enters the register:
history = ((history << 1) | bit) mod 2^h, wherebitis 1 forTand 0 forN. The shift makes room for the new bit;mod 2^hdrops the oldest. - The same branch can now train different counters, depending on what happened just before it.
- With
hequal to 0 the history is always 0, and gshare behaves exactly like the bimodal predictor.
GSHARE mode prints the same per-branch line and TOTAL line as BIMODAL mode.
Tests 4–6 cover this part.
Part 3: Loops and a side-by-side comparison
The branch that closes a loop jumps back to the top on every iteration except the last, where the loop exits.
LOOP pc nstands forn - 1entriespc Tfollowed by one entrypc N.- It prints exactly what those
nentries would print and counts asnbranches.LOOP pc 1is a singlepc N. nis between 1 and 104.LOOPworks in every mode.BOTHmode runs a bimodal predictor and a gshare predictor, withkindex bits andhhistory bits, side by side on the same trace.- Each has its own table of
2^kcounters starting at 1. Only the gshare predictor has a history register.
Output (BOTH mode): one line per branch, then a TOTAL line after the last branch.
pc outcome bimodal_prediction bimodal_result gshare_prediction gshare_result TOTAL branches BIMODAL bimodal_misses GSHARE gshare_misses
Tests 7–10 cover this part. Test 7 uses LOOP in GSHARE mode, and tests 8–10 use BOTH.
BIMODAL 2 0 7 1203 N 1203 T 1203 T 1203 T 1203 N 1203 T 1201 T
1203 3 1 N N HIT 1203 3 0 N T MISS 1203 3 1 N T MISS 1203 3 2 T T HIT 1203 3 3 T N MISS 1203 3 2 T T HIT 1201 1 1 N T MISS TOTAL 7 MISSES 4
The first three lines are the predictor, k = 2, and h = 0, so the table has four counters, each starting at 1.
Branch 1203 reads counter 1203 mod 4 = 3. Its first outcome N matches the prediction, and the counter drops to 0.
The next two T outcomes are both mispredicted while the counter climbs from 0 to 2. The third T is predicted correctly and pushes the counter to 3, strongly taken.
The single N that follows is a miss, but it only lowers the counter to 2, so the prediction stays T and the next T is a hit.
Branch 1201 reads counter 1, which no other branch has trained, so it still predicts N and misses.