sbstndbs
20 cyclesper misprediction
← Back to archive · Field note 01

Finding the predictor in my own CPU

The branch machinery of my laptop's two core designs, measured with a clock and the hardware counters: a misprediction costs about 20 cycles, yet on random data no predictor beats 4 KiB of tables.

How big are the branch structures of a real core, and how far are they from the ones I modelled?

The last three articles built branch prediction into a model of a small RISC-V core. There, a misprediction costs 2 cycles, the branch target buffer has 16 entries, the return stack 8, and the direction tables 4 KiB. This time I measured the real thing: the laptop I write on. Its Intel Core Ultra 7 258V (Lunar Lake) has two core designs on one chip, four Lion Cove P-cores and four Skymont E-cores. They run the same instructions and the same binary, and answer the same design questions differently.

The short answer:

  • A misprediction costs about 20 cycles on both cores, ten times the model's 2, plus most of any time the branch waits for its operand.
  • The structures are far bigger. One branch can replay a random pattern of 5,800 outcomes on Skymont and about 13,000 on Lion Cove. The branch target buffers hold about 8,000 and 12,000 branches, the return stacks 128 and 17 levels.
  • Yet on the model's own programs, the real predictors mispredict about as often as the model's 4 KiB tables. Those branches follow random data, and no predictor learns data it has not seen.

I measured both cores from user space, with small loops in assembly, a clock and the hardware's event counters. A measurement like this sees a curve, not a circuit. A step at 8,000 jumps says that something holds about 8,000 jumps, not how it is built. Intel publishes few of these numbers. Where others have measured them, I compare.

Setup

Each probe is a loop in assembly, generated by a Python script, built with gcc and pinned to one core: cpu 3 is a Lion Cove core, cpu 7 a Skymont core. The loop runs once untimed, which also trains the predictors, then five more times. Every number below is the median of those five.

The clock is the time-stamp counter, which ticks at a fixed rate whatever the core's frequency. To turn ticks into core cycles, I run a chain of dependent additions next to the loop, one cycle per addition. Each run is cut into slices of about 65,000 branches, and each slice is converted with a reference chain measured just before and just after it, so a change of frequency between slices cancels out. The laptop ran in its performance profile: Lion Cove at 4.3 to 4.8 GHz, Skymont at 3.6 to 3.7 GHz.

The hardware counters count, around each slice of the loop and nothing else: cycles, instructions, conditional branches, mispredicted conditional branches, mispredicted returns, and a few front-end events I name where I use them. They need kernel.perf_event_paranoid at 2, which I set for the runs.

Checks

  • Two clocks agree. Over 3,810 measurements, the cycles converted from the time-stamp counter differ from the hardware's own cycle count by a median of 0.41 %, and by less than 2.4 % in 99 measurements out of 100.
  • A known latency. A chain of dependent 64-bit multiplications takes 3.02 cycles per multiplication on Lion Cove, where the documented latency is 3, and 4.00 on Skymont. A 32-bit multiplication takes 3.0 on both: it is the unit of delay below.
  • A known miss rate. On random outcomes, the counters see 50.01 % of the branches mispredicted on both cores, as any predictor must.
  • Spread. The slowest of the five runs is a median 2.0 % slower than the fastest, and within 7.5 % for nine measurements in ten.

What a misprediction costs

The first probe tests 64 branches per 64-bit word. A shift moves the next bit into the carry flag and a jump tests it, so the condition is ready one cycle after the previous shift, with no load in its path. With random words, half the branches are mispredicted; with words of all ones or all zeros, none are.

Lion Cove (P-core)Skymont (E-core)
Never taken (cycles per branch)1.001.00
Always taken1.022.35
Random10.9610.88
Mispredicted, counted50.01 %50.01 %
Cost of one misprediction19.9 cycles18.4 cycles

A misprediction costs about 20 cycles on both cores, ten times the 2 cycles of the five-stage model. The small core has as long a road from fetch to the resolved branch as the big one.

The cost grows when the branch waits for its operand. In the next figure, the bit passes through 1 to 10 dependent multiplications before the branch tests it. Each cycle of waiting adds about 0.8 cycles to every misprediction: the work after the branch is thrown away and done again, and the core hides only part of the wait.

Fig. 01Cycles lost per counted misprediction against the cycles the branch waits for its operand, on both cores: about 20 cycles with no wait, about 44 after 30 cycles of waiting.

Skymont pays 2.35 cycles for each taken branch when they are packed five bytes apart, where Lion Cove pays 1. Its front-end counters show why: each of those taken branches costs 3.1 issue slots lost to a redirect of the fetch. I come back to dense branches below.

How much one branch remembers

The second probe has one branch whose outcome is read from memory and follows a random pattern that repeats every P branches. To predict it, the predictor must store the whole pattern, one outcome per position, and find its position from the recent history. A 2-bit counter learns nothing from a random pattern. Each pattern length is measured with three different patterns. The cycles are compared with a pattern that alternates taken and not taken, just as often taken but easy to learn.

Fig. 02Mispredictions (top, counted) and extra cycles (bottom) per branch against the period of a repeated random pattern. Skymont learns patterns up to 5,800 outcomes, Lion Cove up to about 13,000. From a period of 19, Lion Cove also pays up to 0.8 cycles per branch without mispredicting.
  • Skymont learns patterns of up to 5,793 outcomes and fails at 6,317, with all three patterns. Past that, the misses climb towards half the branches.
  • Lion Cove learns patterns of up to 12,634 to 13,777 outcomes, depending on the pattern, and fails from 13,777 to 15,024.
  • From a period of 19, Lion Cove pays extra cycles without mispredicting. Its branches cost 0.2 to 0.8 cycles more than the alternating pattern, while the counters see 0.04 to 0.12 % of them mispredicted.

That extra cost is a delay in the front end. The counter of issue slots the front end leaves empty while the back end could take more (IDQ_BUBBLES.CORE) sees 17 empty slots per branch on these patterns, against 0.4 on the alternating one. To test it, I delayed the branch: 24 cycles of dependent multiplications by one before the branch reads its byte. A misprediction is paid when the branch resolves, so the delay makes it more expensive. A bubble in the front end costs nothing once the back end is the bottleneck, and the multiplications make it so.

Lion Cove, per branchperiod 128period 1,024period 4,096
Mispredicted, branch resolves at once0.07 %0.05 %0.07 %
Extra cycles, branch resolves at once0.650.700.80
Empty front-end slots, branch resolves at once17.017.318.1
Extra cycles, branch resolves 24 cycles later0.040.040.11
Empty front-end slots, branch resolves 24 cycles later0.10.20.9

With the delay, a full misprediction costs 49 cycles instead of 27, but the extra cost of the long patterns vanishes, and so do the empty slots. Lion Cove predicts short patterns at once and long ones a little late. That is what the overriding predictor of the previous article does: a fast predictor redirects fetch at once, and a slower, more accurate one corrects it when they disagree. The counters show that the answer arrives late, not which structure gives it.

Branches sharing the predictor

The third probe has B branches, each with its own random pattern of the same period, and takes the longest period learned with under 5 % mispredicted. B times that period is the number of outcomes held at once.

With 128 to 512 branches, Skymont holds about 23,000 outcomes in all, and grows smoothly towards it as the branches multiply. With 512 branches, it learns patterns of 45 and fails at 54. Chips and Cheese, with the same kind of test, reports that Skymont fails beyond 48. Lion Cove holds 16,000 to 19,500 outcomes with 128 to 512 branches. With fewer branches, its total jumps between 4,900 and 19,500 from one count to the next, as if this regular test sometimes defeated the way its tables are indexed.

For scale, the 4 KiB tables of the previous article hold 16,384 two-bit counters. A counter keeps one guess per branch, and a random pattern needs one per position.

Taken jumps and the branch target buffer

A taken branch has to redirect fetch before the branch is even decoded, so the core keeps a cache of branch targets: the branch target buffer, or BTB. The fourth probe chains n unconditional jumps, each to the next, 4, 16 or 64 bytes apart, and runs them in a loop. While the BTB holds every jump, the jumps cost little; past each of its levels, they cost more. Past the last, the core finds each jump only when it decodes it, and a counter sees that: it counts the clears for branches the predictor did not know (BACLEARS).

Fig. 03Cycles per taken jump against the number of jumps in a loop, for jumps 4, 16 and 64 bytes apart, on Lion Cove (top) and Skymont (bottom).

On Lion Cove, with jumps 16 bytes apart:

  • Up to about 23 jumps, the loop runs at three jumps per cycle (0.29 cycles each). It probably runs from a loop buffer, which replays it without fetching it again.
  • Up to about 200 jumps, one jump per cycle. The level ends at about 400 jumps when they are 4 or 8 bytes apart, and at about 45 when they are 64 bytes apart.
  • Up to about 4,900 jumps, two cycles per jump; then three cycles up to about 11,600.
  • Beyond, about 13 cycles per jump, and the clears climb from 0.02 per jump at 11,585 jumps to 0.97 at 32,768: the BTB holds about 12,000 branches.

Chips and Cheese measured a 12K-entry BTB on Lion Cove, and Wikipedia's table lists levels of 256, 6K and 12K entries, without a source. The steps fall close to those sizes.

On Skymont, with jumps 16 bytes apart, a jump costs one cycle up to about 1,000 jumps and three cycles up to 8,192. The clears start at 9,742 jumps (0.39 per jump), and every jump clears from about 19,000. Chips and Cheese reports a 1,024-entry first level with no bubble, a second level at 3 to 4 cycles and 8,192 entries in the last. The match is close. Dense jumps are Skymont's weak point: four bytes apart, they cost 7.5 cycles each beyond a dozen, and half of them clear from 16 jumps on.

Returns

A return jumps to wherever the matching call came from, so its target changes from one call to the next. A return stack predicts it: each call pushes its return address, and each return pops one. Two probes test its depth, and the counters count the mispredicted returns.

  • The chain. D distinct functions, each calling the next. Every return has a single target.
  • The random sites. One recursive function, called at each level from one of four call sites chosen at random. Only a return stack can know where each return goes; anything else guesses right one time in four.
Fig. 04Mispredicted returns per round against the depth of nested calls, counted, for chains of functions and for random call sites. Lion Cove mispredicts from 18 levels with random call sites and never with the chains; Skymont from 129 levels in both tests.

Skymont's stack holds 128 levels, as Chips and Cheese reports. Past it, every extra level costs a mispredicted return, in both tests: when the stack runs out, nothing else predicts the return.

Lion Cove's stack holds 17 levels. With random call sites, no return is mispredicted up to 17 levels; past it, each extra level adds 0.76 mispredicted returns per round, close to the three in four of a guess. With the chains, no return is mispredicted at any depth, up to 512. Past the stack, a return with a fixed target is still predicted, by another predictor. Intel documents this: on some of its cores, a return that finds the return stack empty is predicted by other predictors, which the Linux kernel's documentation names as the branch target buffer. That fallback is slower: in time, the chains slow down past 17 levels, or past 25 when four functions share each 64-byte line, without a single misprediction. Chips and Cheese reports 24 entries; I count 17 levels before the first mispredicted return.

Two checks hold that limit in place. Twelve more frames below the test change nothing, so the stack overwrites its oldest entries, as it should. Delaying the choice of call site by 24 cycles, so that a mispredicted choice runs further down the wrong path before it is undone, changes nothing either.

The model's programs on the real cores

The previous article measured its predictors on ten small C programs and a stress program, compiled for RISC-V with clang at -O2. I compiled the same programs for this machine with the same compiler and level, and counted their conditional branches and mispredictions on both cores. To give the predictor a first contact with the program, as in the model, each program is built at five code offsets: a predictor indexes its tables by branch address, so a new offset brings branches it has never seen. A million random branches elsewhere run before each measurement, and every result is the median over the five builds.

The comparison only makes sense where both builds run the same branches. In seven programs, the x86 build executes within 15 % of the RISC-V build's conditional branches. In the other four, the x86 compiler removed branches: it turned the stress program's thousand branches into conditional moves (402 conditional branches left out of 398,800), did the same to collatz's odd-even test, vectorised strsearch, and took a fifth of tree's branches away. The RISC-V target of the model has no conditional move.

Fig. 05Share of conditional branches mispredicted in the seven programs whose two builds run about as many branches: the model's bimodal and perceptron tables of 4 KiB against Lion Cove and Skymont at first contact.
Mean of the seven programsMispredicted
Model, bimodal, 4 KiB14.10 %
Model, gshare, 4 KiB15.12 %
Model, tournament, 4 KiB13.93 %
Model, perceptron, 4 KiB13.27 %
Lion Cove, first contact14.29 %
Lion Cove, third run13.04 %
Skymont, first contact15.37 %
Skymont, third run15.25 %

At first contact, Lion Cove mispredicts as often as the model's bimodal table, and a little more than its perceptron; Skymont a little more than both. The branches that miss in these programs follow random data: qsort's partition test, bsearch's comparisons, the hash probes, the lexer's character classes. No history predicts them the first time. The model also has an easier task: its predictors are updated as soon as a branch resolves and never see a wrong path, which favours it slightly.

Lion Cove learns from repetition, Skymont does not. On the third run of the same build, Lion Cove's mispredictions fall from 14.29 % to 13.04 %, through the million random branches between runs; Skymont's stay at 15.3 %. Lion Cove keeps part of what it learned about a program while other code runs.

Two answers to the same questions

The two cores agree on the cost of a misprediction and differ on almost everything else.

  • Skymont answers at once, and puts its storage where a narrow core needs it. Its first BTB level holds about 1,000 branches with no bubble, several times Lion Cove's 200 to 400, and its return stack 128 levels. It never answers late. When it runs out, it mispredicts: no second predictor catches the deep returns, and it pays for dense taken branches.
  • Lion Cove layers its predictors. A fast answer comes first, and a slower one corrects it on long patterns at the price of a front-end bubble. Its return stack is shallow, with the BTB behind it for returns that always go to the same place. It learns patterns twice as long for a single branch, replays small loops at three jumps per cycle, and keeps what it learned about a program while other code runs.

Against the model

The model (articles 2 and 3)Lion CoveSkymont
Cost of a misprediction2 cycles19.9 cycles18.4 cycles
Random pattern one branch can replaynone (2-bit counters)about 13,000 outcomes5,800 outcomes
Branch targets16 entriesabout 12,000about 8,000
Return stack8 entries17 levels, then the BTB128 levels
Model's programs mispredicted, at first contact13.3 to 15.1 %14.3 %15.4 %

The previous article concluded that, on a five-stage core, a fast predictor beats an accurate one, and its limitations said that a deeper pipeline would favour accuracy. Here is the deeper pipeline: at 20 cycles per misprediction, every miss avoided is worth ten times more, and Lion Cove does not choose between fast and accurate, it layers them. But the size of these structures earns its keep on code with patterns, calls and repetition. On a branch that follows fresh random data, a 2024 core does no better than 4 KiB of tables.

Limitations

  • One laptop. One chip, one core of each type, measured in user space. The slices and the counters handle the clock, not every interference from the rest of the system.
  • Random patterns. They measure how much a predictor can store, not how well it predicts real code. Real branches are mostly easier.
  • Counters name events, not structures. A counter says that a return was mispredicted or a slot left empty, not which table did it. Their meaning comes from Intel's event descriptions.
  • The model comparison. Two instruction sets and two builds of the same C, on seven small programs. The model's predictors are trace-driven, updated at once and free of wrong paths, which favours them.
  • Steps, not structures. A level found at about 4,900 jumps may be a table of 6,000 entries that loses some to collisions. The comparisons with published sizes are the closest I have to a check.
  • Other people's numbers. The published figures come from Chips and Cheese's measurements, partly on Arrow Lake, which uses the same Lion Cove core, and from a Wikipedia table that cites no source.

How far can this be trusted?

The method has checks I trust: the cycles from the time-stamp counter agree with the hardware's own count, a multiplication measures its documented latency, and random outcomes are mispredicted half the time. On Skymont, four results land on published ones: a 1,024-entry first BTB level, an 8,192-entry last one, a 128-level return stack, and patterns of under 48 outcomes across 512 branches. The counters confirm the inferences an earlier version of this article drew from time alone, and corrected one: the 25 levels I had found for Lion Cove's return stack were a slowdown, not mispredictions. What remains open is which structure delays Lion Cove's long patterns, and why its total storage looks irregular with a few dozen branches.

Reproduction

Everything runs from the cpu-systemc folder, on Linux and x86-64. The probes are in probe/:

sh
sudo sysctl kernel.perf_event_paranoid=2 # the hardware counters; without them, timing only python3 probe/run.py # every experiment on cpu 3 and cpu 7, about half an hour python3 probe/probe_numbers.py # the tables and figures of this article

run.py writes one JSON file per experiment to probe/results/. The core numbers and the hardware events are set at the top of run.py; on another machine, pick one core of each type, or one core, and the events of that part.

Updated on 14 October 2026: the measurements were run again with the hardware counters, in the performance profile. The first version inferred mispredictions from time alone.

Sources: Chips and Cheese, Skymont: Intel's E-Cores reach for the Sky and Intel's Lion Cove P-Core and Gaming; Wikipedia, Lion Cove; Intel, Return Stack Buffer Underflow; the Linux kernel, Return Stack Buffer.