-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_pred.h
241 lines (219 loc) · 6.71 KB
/
all_pred.h
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <fstream>
#include <iostream>
// uop type encoding
#define IS_BR_CONDITIONAL (1 << 0) // conditional branch
#define IS_BR_INDIRECT (1 << 1) // indirect branch
#define IS_BR_CALL (1 << 2) // call
#define BR_SIZE 8192
//#define BR_SIZE 4
class my_predictor_1
{
//private:
public:
// Prediction table for 1 bit and 2 bit Bimodal Predictor
// Third least significant bit is for 1 bit Bimodal Predictor
// First and second least significant bit for 2 bit Bimodal Predictor
int *table1024;
int globalReg;
int **gtable;
int alwaysT;
int alwaysNT;
int bimodal1bit1024;
int bimodal2bit1024;
int gCounter[9];
int tCounter;
int index;
int iCount;
char branchTaken;
my_predictor_1(void)
{
table1024 = new int[BR_SIZE];
// Gshare Predictor
// Use third and fourth least significant bits of gtable[8] for tournament predictor
globalReg = 0;
gtable = new int*[9]; // Prediction table for 2 to 10 bit global register
for (int i = 0; i < 9; i++) {
gtable[i] = new int[BR_SIZE];
}
// Initialize prediction table
// 1 bit Bimodal Prediction Table set to Taken (1)
// 2 bit Bimodal Prediction Table set to Strongly Taken (11)
// Gshare Prediction Table also set to Strongly Taken (11)
// Tournament Prediction Table set to Prefer Gshare (00)
for (int i = 0; i < BR_SIZE; i++) {
table1024[i] = 7;
gtable[0][i] = gtable[1][i] = gtable[2][i] = gtable[3][i] = gtable[4][i] = gtable[5][i] = gtable[6][i] = gtable[7][i] = gtable[8][i] = 3;//gtable[9][i] = gtable[10][i] = gtable[11][i] = gtable[12][i]= 3;
}
// Create Counters
alwaysT = 0;
alwaysNT = 0;
bimodal1bit1024 = 0;
bimodal2bit1024 = 0;
gCounter[9] = {0};
tCounter = 0;
iCount = 0;
// int index = address % 1024;
// Tournament Predictor
// tCounter += tournamentPred(gtable[8][index], table1024[index] % 4, gtable[8][index ^ globalReg & 1023] % 4, branchTaken);
// Gshare Predictor (2 - 10 bits global register)
// for (int i = 2; i <= 10; i++) {
// int cutOff = 1 << i; //Use to obtain bits from global register
// gCounter[i - 2] += predictor2bit(gtable[i - 2], (index) ^ (globalReg % cutOff), branchTaken);
// }
// globalreg = globalreg << 1;
// if (branchtaken == 't') {
// alwayst++;
// globalreg++;
// } else {
// alwaysnt++;
// }
// globalreg &= (br_size-1); //keep global register to 10 bits
//1 bit Bimodal Predictor with various table size
// bimodal1bit1024 += predictor1bit(table1024, address % 1024, branchTaken);
//2 bit Bimodal Predictor with various table size
// bimodal2bit1024 += predictor2bit(table1024, address % 1024, branchTaken);
iCount++; //Instruction Count
}
/**
* Predictor using 1 bit saturating counters
* Update the entry in table base on the actual branch direction
* (1 - Taken, 0 - Non-Taken)
*
* @param table prediction table
* @param index locate the entry in prediction table
* @param taken actual branch direction
* @return whether entry in the table matches actual branch direction
*/
bool predictor1bit(int * table, int index, char taken) {
bool correct = 0;
if (taken == 'T') {
if (table[index] >= 4) {
correct = 1;
} else {
table[index] += 4;
}
} else {
if (table[index] < 4) {
correct = 1;
} else {
table[index] -= 4;
}
}
return correct;
}
/**
* Predictor using 2 bit saturating counters
* Update the entry in table base on the actual branch direction
* (11 - Strongly Taken, 10 - Weakly Taken, 01 - Weakly Non-Taken, 00 - Strongly Non-Taken)
*
* @param table prediction table
* @param index locate the entry in prediction table
* @param taken actual branch direction
* @return whether entry in the table matches actual branch direction
*/
bool predictor2bit(int * table, int index, char taken) {
bool correct = 0;
int state = table[index] % 4;
if (taken == 'T') {
switch (state) {
case 0:
case 1:
table[index]++;
break;
case 2:
table[index]++;
case 3:
correct = 1;
break;
}
} else {
switch (state) {
case 0:
correct = 1;
break;
case 1:
correct = 1;
case 2:
case 3:
table[index]--;
break;
}
}
return correct;
}
/**
* Tournament Predictor
* Update the entry in selector base on the actual branch direction
* (11 - Strongly Prefer 2 bits Bimodal, 10 - Weakly Prefer 2 bits Bimodal,
* 01 - Weakly Prefer Gshare, 00 - Strongly Prefer Gshare)
*
* @param entry entry of the selector
* @param bPred prediction made by 2 bits Bimodal
* @param gPred prediction made by Gshare
* @return whether entry in the prefered table matches actual branch direction
*/
bool tournamentPred(int &entry, int bPred, int gPred, char taken) {
bool correct = 0;
int state = entry & 12;
if (taken == 'T') {
switch (state) {
case 0:
case 4:
if (gPred >= 2) {
if (bPred <= 1) {
entry = entry % 4;
}
correct = 1;
} else if (bPred > 1) {
entry += 4;
}
break;
case 8:
case 12:
if (bPred >= 2) {
if (gPred <= 1) {
entry = 12 + entry % 4;
}
correct = 1;
} else if (gPred >= 2) {
entry -= 4;
}
break;
}
} else {
switch (state) {
case 0:
case 4:
if (gPred <= 1) {
if (bPred >= 2) {
entry = entry % 4;
}
correct = 1;
} else if (bPred <= 1) {
entry += 4;
}
break;
case 8:
case 12:
if (bPred <= 1) {
if (gPred >= 2) {
entry = 12 + entry % 4;
}
correct = 1;
} else if (bPred > 1 && gPred <= 1) {
entry -= 4;
}
break;
}
}
return correct;
}
~my_predictor_1()
{
delete [] table1024;
for (int i = 0; i < 12; i++) {
delete [] gtable[i];
}
delete [] gtable;
}
};