-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictors.cpp
48 lines (39 loc) · 1.52 KB
/
predictors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* File Name: predictors.cpp
* Description: Implements the functionality of 1-bit and 2-bit branch predictors.
* Key Functionality: Includes methods to calculate the prediction accuracy of 1-bit and 2-bit branch predictors based on
* a vector of boolean representing branch outcomes.
* Author: Wa'el Alkalbani
* Email: wael@pdx.edu
*/
#include "predictors.h"
// 1-bit branch predictor implementation
double OneBitPredictor::calculateAccuracy(const vector<bool>& branchOutcomes) {
int correctPredictions = 0;
int counter = 0; // Initial state is '00'
for (bool outcome : branchOutcomes) {
bool prediction = (counter >= 2); // Predict 'taken' if counter is 10 or 11
if (prediction == outcome) {
correctPredictions++;
}
// Update the counter based on the actual outcome
if (outcome) {
if (counter < 3) counter++;
} else {
if (counter > 0) counter--;
}
}
return static_cast<double>(correctPredictions) / branchOutcomes.size() * 100;
}
// 2-bit saturating counter branch predictor implementation
double TwoBitPredictor::calculateAccuracy(const vector<bool>& branchOutcomes) {
int correctPredictions = 0;
bool lastOutcome = false; // Initial state is 'not taken'
for (bool outcome : branchOutcomes) {
if (lastOutcome == outcome) {
correctPredictions++;
}
lastOutcome = outcome;
}
return static_cast<double>(correctPredictions) / branchOutcomes.size() * 100;
}