-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbc2.cpp
139 lines (115 loc) · 2.8 KB
/
bc2.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "bc2.h"
class BC2_BHT{
// unordered map
unordered_map<uint16_t, int> BHT_cache;
//max_size = 1024 Utilising the 10 bits of pc
public:
int BHT_check(uint16_t,int);
void BHT_update(uint16_t,int,int);
};
BC2_BHT BHT;
int BC2_BHT::BHT_check(uint16_t index, int code)
{
unordered_map<uint16_t, int> :: iterator element = BHT_cache.find(index);
if(element == BHT_cache.end())
{
BHT_update(index,N,code);
cout<<" "<<" Prelim Prediction:"<<"N"<<" ";
return N;
}
else
{
if(element->second == 0)
{
cout<<" "<<"Prelim Prediction:"<<"N"<<" ";
}
else if(element->second == 1)
{
cout<<" "<<"Prelim Prediction:"<<"n"<<" ";
}
else if(element->second == 2)
{
cout<<" "<<"Prelim Prediction:"<<"t"<<" ";
}
else if(element->second == 3)
{
cout<<" "<<"Prelim Prediction:"<<"T"<<" ";
}
return element->second;
}
}
void BC2_BHT::BHT_update(uint16_t index, int value, int code)
{
if(code == 1)
{
//cout<<" In BHT Update Code 1 ";
BHT_cache[index] = value;
}
else if(code == 2)
{
unordered_map<uint16_t, int> :: const_iterator element1 = BHT_cache.find(index);
if( element1 != BHT_cache.end())
{
if(element1->second == N )
{
BHT_cache[index] = n;
}
else if (element1->second == n )
{
BHT_cache[index] = t;
}
else if (element1->second == t )
{
BHT_cache[index] = T;
}
}
else{
cout<<" In BHT Update - Element Not Found ";
}
}
else if(code == 3)
{
unordered_map<uint16_t, int> :: const_iterator element2 = BHT_cache.find(index);
if( element2 != BHT_cache.end())
{
if(element2->second == T)
{
BHT_cache[index] = t;
}
else if (element2->second == t)
{
BHT_cache[index] = n;
}
else if (element2->second == n)
{
BHT_cache[index] = N;
}
}
else{
cout<<" In BHT Update - Element Not Found ";
}
}
}
int bc2_predict(uint16_t pc, int code)
{
uint16_t index;
index = pc & 4092;
int prediction;
cout<<" "<<"Index: "<<index;
if(code == 1){
prediction = BHT.BHT_check(index,code);
if(prediction == N || prediction == n)
{
return 0;
}
else if(prediction == T || prediction == t)
{
return 1;
}
}
else if(code == 2 || code == 3)
{
BHT.BHT_update(index,0,code);
}
return -1;
}