gitwtfhub

wtf is branch-predictor-simulator?

codeyaruj/branch-predictor-simulator — explained in plain English

Analysis updated 2026-05-18

1C++Audience · researcherComplexity · 2/5Setup · easy

TL;DR

An educational C++ simulator that compares five different CPU branch prediction strategies against text based instruction traces.

Mindmap

mindmap
  root((Branch Predictor Sim))
    What it does
      Simulate branch prediction
      Compare predictor strategies
      Show prediction traces
    Tech stack
      C++17
      Make
    Use cases
      Learn CPU branch prediction
      Compare predictor accuracy
      Study GShare algorithm
    Audience
      Students
      Computer architecture learners

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Why would anyone build with this?

REASON 1

Simulate and compare five different branch prediction algorithms on a trace file.

REASON 2

Study how one bit, two bit, and GShare predictors differ in behavior.

REASON 3

Run verbose mode to inspect every prediction and state change step by step.

REASON 4

Write custom trace files to test predictor behavior on specific branch patterns.

What's in the stack?

C++Make

How it stacks up

codeyaruj/branch-predictor-simulatorallentdan/shape_based_matchingamu2mod/radeonmon
Stars111
LanguageC++C++C++
Last pushed2019-03-01
MaintenanceDormant
Setup difficultyeasymoderatemoderate
Complexity2/53/53/5
Audienceresearcherdevelopervibe coder

Figures from each repo's GitHub metadata at analysis time.

How do you spin it up?

Difficulty · easy Time to first run · 5min

Requires a C++17 compiler and Make, only intended for small learning traces since the whole trace loads into memory.

The README does not state a license for this project.

Wtf does this do

This is a small educational tool the author built to understand how CPUs guess the outcome of branching instructions before they actually run, a technique real processors use to keep their pipelines full and run faster. The simulator implements five different prediction strategies: always predicting taken, always predicting not taken, a simple one bit predictor, a more forgiving two bit predictor, and a more advanced GShare predictor that factors in recent branch history. To build it you need a C++17 compiler and the Make build tool, and building is a single command. You run the resulting program against a trace file, which is a plain text list of program counter addresses paired with whether that branch was actually taken or not. By default it uses the two bit predictor, but you can pick any of the five by name, tune GShare's internal table and history sizes, compare all five predictors against each other on the same trace at once, or turn on verbose mode to see every individual prediction and state change as it happens. The project includes its own test suite you can run with a single command. In the README, the author explains in plain terms what they learned: a one bit predictor flips its guess the moment it sees one different result, while a two bit predictor needs to be wrong more than once in a row before it changes its mind, and GShare improves on both by mixing the branch's address with a short history of recent branch outcomes to pick which prediction table entry to use. The author is upfront about its limits. It only models the direction of a branch, not real CPU timing or pipeline behavior, so it does not aim to be cycle accurate. It loads an entire trace into memory before running, so it is meant for small traces used for learning rather than large real world workloads, and its indexing into the prediction table uses the raw address bits without first removing the bits that come from instruction alignment.

Yoink these prompts

Prompt 1
Explain how the GShare branch predictor in this simulator works.
Prompt 2
Help me build and run Branch-Predictor-Simulator on a sample trace file.
Prompt 3
Walk me through the difference between the one-bit and two-bit predictors here.
Prompt 4
What does the --compare flag show me when I run this simulator?

Frequently asked questions

wtf is branch-predictor-simulator?

An educational C++ simulator that compares five different CPU branch prediction strategies against text based instruction traces.

What language is branch-predictor-simulator written in?

Mainly C++. The stack also includes C++, Make.

What license does branch-predictor-simulator use?

The README does not state a license for this project.

How hard is branch-predictor-simulator to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is branch-predictor-simulator for?

Mainly researcher.

View the repo → Decode another repo

This repo across BitVibe Labs

Don't trust strangers blindly. Verify against the repo.