-
Notifications
You must be signed in to change notification settings - Fork 0
/
branchPredictor.cpp
55 lines (51 loc) · 1.97 KB
/
branchPredictor.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
48
49
50
51
52
53
54
#include "branchPredictor.h"
int BranchPredictor::getBranchPrediction() {
switch(this->currentState) {
case BranchPredictionType::STRONG_NOT_TAKEN: return 0;
case BranchPredictionType::WEAK_NOT_TAKEN: return 0;
case BranchPredictionType::STRONG_TAKEN: return 1;
case BranchPredictionType::WEAK_TAKEN: return 1;
}
}
void BranchPredictor::updateState(bool inpIsCorrect) {
if (inpIsCorrect) {
// prediction was correct
switch(this->currentState) {
case BranchPredictionType::STRONG_NOT_TAKEN:
break;
case BranchPredictionType::WEAK_NOT_TAKEN:
this->currentState = BranchPredictionType::STRONG_NOT_TAKEN;
break;
case BranchPredictionType::STRONG_TAKEN:
break;
case BranchPredictionType::WEAK_TAKEN:
this->currentState = BranchPredictionType::STRONG_TAKEN;
break;
}
} else {
// prediction was wrong
switch(this->currentState) {
case BranchPredictionType::STRONG_NOT_TAKEN:
this->currentState = BranchPredictionType::WEAK_NOT_TAKEN;
break;
case BranchPredictionType::WEAK_NOT_TAKEN:
this->currentState = BranchPredictionType::WEAK_TAKEN;
break;
case BranchPredictionType::STRONG_TAKEN:
this->currentState = BranchPredictionType::WEAK_TAKEN;
break;
case BranchPredictionType::WEAK_TAKEN:
this->currentState = BranchPredictionType::WEAK_NOT_TAKEN;
break;
}
}
if (debugMode) cout << "\n UPDATED BRANCH PREDICTOR STATE= " << this->currentState << "\n";
}
BranchPredictor::BranchPredictor(
const bool debugMode
) :
debugMode(debugMode)
{
this->moduleType = ModuleType::BRANCH_PREDICTOR;
if (debugMode) cout << "\nINITIAL BRANCH PREDICTOR STATE= " << this->currentState << "\n";
};