top of page

2 bit Static Branch Predictor

In last tutorial, we already saw 1 bit static branch predictor. Now let's go ahead and enhance it a little bit. It is no way as good as the dynamic branch predictor. But we will get there one step at a time

​

To explain the concept of a 2-bit static branch predictor, lets again use an example of traffic lights, let's consider a scenario where we have a traffic light intersection that can be in two states: "Green" and "Red". The goal is to predict the state of the traffic light at the next cycle based on the previous states.

1. 2-Bit Static Branch Predictor:
   - We can model a 2-bit static branch predictor using a two-bit saturating counter, which can take four possible states: "Strongly Taken" (ST), "Weakly Taken" (WT), "Weakly Not Taken" (WNT), and "Strongly Not Taken" (SNT).
   - Initially, let's assume the traffic light starts in the "Green" state, and our predictor assumes a weakly taken state (WT).
   - At the first cycle, the traffic light is observed to be in the "Red" state. Since the prediction was weakly taken, the predictor predicts a taken state for the next cycle, assuming a WT state remains.
   - At the second cycle, if the traffic light remains in the "Red" state, the predictor's prediction is correct, and it remains in the WT state.
   - At the third cycle, if the traffic light switches to the "Green" state, the prediction is incorrect, and the predictor updates its state to weakly not taken (WNT).
   - At the fourth cycle, if the traffic light remains in the "Green" state, the predictor's prediction is again incorrect, and it updates its state to strongly not taken (SNT).
   - At this point, the predictor has learned that the traffic light stays in the "Red" state for a longer duration and updates its state to SNT, predicting a not taken state for the next cycle.
   - If the traffic light continues to remain in the "Red" state for subsequent cycles, the prediction remains correct, and the predictor stays in the SNT state.
   - However, if the traffic light switches back to the "Green" state, the prediction becomes incorrect, and the predictor adjusts its state to weakly taken (WT) or strongly taken (ST) based on the specific behavior observed.
   - The predictor continues to adapt and update its state based on the actual outcomes, gradually improving its prediction accuracy as it encounters different patterns of traffic light behavior.

The 2-bit static branch predictor utilizes the concept of hysteresis to reduce unnecessary state changes and make predictions based on historical behavior. It provides a more sophisticated prediction mechanism compared to the 1-bit predictor, allowing it to adapt to changes in the branch behavior over time.

In the context of traffic lights, the 2-bit static branch predictor would try to learn the patterns of switching between "Green" and "Red" states and make predictions based on the historical information. This helps to optimize traffic flow by reducing the number of unnecessary stops or delays at the intersection.

 

Code logic for a 2 bit static branch predictor would be something like this: 

Test bench for the above code:

Bonus Challenge: 

Can you modify this testbench to provide further scenarios? and predict the output of the design code? 

Also, 

super bonus challenge: 

Static Branch Predictors are simplest of predictors and there are so many more predictors out there. Checkout wikipedia : https://en.wikipedia.org/wiki/Branch_predictor

Creating your own specialized predictor can be a great weekend project? 

bottom of page