-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhybrid.cc
120 lines (97 loc) · 3.13 KB
/
hybrid.cc
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
#include <bits/stdc++.h>
#include <fstream>
#include <vector>
using namespace std;
int* hybrid(int k, int m_1, int n, int m_2, char *tracefile,
vector<int> table_g, vector<int> table_b, vector<uint8_t> chooser_table) {
int bhr_largest_bit = 1 << (n - 1);
bool actual_taken;
bool pred_taken_g, pred_taken_b, pred_taken_h;
// File
ifstream InFile(tracefile);
string line;
// Stats
int predictions = 0;
int mispredictions = 0;
int bhr = 0;
int pc = 0;
// Find and predict at each branch
while (getline(InFile, line)) {
actual_taken = (line[7] == 't');
// Get PC (without last 2 bits)
pc = stoi(line.substr(0,6), 0, 16);
pc >>= 2;
// Gshare Prediction
// Explained in gshare.cc
int index_A = (((pc >> n) << n) % (1 << m_1));
int index_B = (pc % (1 << n)) ^ bhr;
int index_g = index_A + index_B;
int value_g = table_g[index_g];
pred_taken_g = (value_g >= 4);
// Update BHR
bhr >>= 1;
if (actual_taken)
bhr += bhr_largest_bit;
// Bimodal Prediction
int index_b = (pc) % (1 << m_2);
int value_b = table_b[index_b];
pred_taken_b = (value_b >= 4);
// Hybrid Prediction
int index_choose = pc % (1 << k);
int value_h = chooser_table[index_choose];
bool gshare_selected = false;
if (value_h >= 2) {
pred_taken_h = pred_taken_g;
gshare_selected = true;
} else {
pred_taken_h = pred_taken_b;
}
// UPDATE GSHARE
if (gshare_selected) {
if (actual_taken && value_g < 7)
table_g[index_g]++;
else if (!actual_taken && value_g > 0)
table_g[index_g]--;
}
// UPDATE BIMODAL
else {
if (actual_taken && value_b < 7)
table_b[index_b]++;
else if (!actual_taken && value_b > 0)
table_b[index_b]--;
}
// Gshare is correct, Bimodal is wrong
if (actual_taken == pred_taken_g && actual_taken != pred_taken_b) {
chooser_table[index_choose] = min(3, value_h + 1);
// Bimodal is correct, Gshare is wrong
} else if (actual_taken == pred_taken_b && actual_taken != pred_taken_g) {
chooser_table[index_choose] = max(0, value_h - 1);
}
predictions++;
if (actual_taken != pred_taken_h) mispredictions++;
}
InFile.close();
int size = chooser_table.size() + table_b.size() + table_g.size();
int b = 0, g = 0;
// Return results
int *ret = (int *)malloc(sizeof(int) * (size + 2));
ret[0] = predictions;
ret[1] = mispredictions;
for (long unsigned int i = 0; i < chooser_table.size(); i++)
{
ret[i+2] = chooser_table.at(i);
g = i+1;
}
for (long unsigned int i = 0; i < table_g.size(); i++)
{
ret[g+2] = table_g.at(i);
g++;
b = g;
}
for (long unsigned int i = 0; i < table_b.size(); i++)
{
ret[b+2] = table_b.at(i);
b++;
}
return ret;
}