-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBranch.cpp
132 lines (115 loc) · 3.56 KB
/
Branch.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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <iomanip>
#include <stdlib.h>
#include <cmath>
#include <bitset>
using namespace std;
struct config
{
int size1;
};
int main(int argc, char* argv[])
{
config branchconfig;
ifstream branch_params;
branch_params.open(argv[1]);
while(!branch_params.eof()) // read config file
{
branch_params>>branchconfig.size1;
}
long x = pow(2,branchconfig.size1);
int A[x-1];
for(int i =0;i<x;i++)
{
A[i] = 3;
}
int misprediction =0;
int total =0;
int correct =0;
ifstream traces;
ofstream tracesout;
string outname;
outname = string(argv[2]) + ".out";
int j = 0;
traces.open(argv[2]);
tracesout.open(outname.c_str());
int m = 32-branchconfig.size1;
int bit;
string line;
string hexa;
unsigned int integeraddress;
bitset<32> bitaddress;
bitset<32> index;
if (traces.is_open()&&tracesout.is_open()){
while (getline (traces,line)){ // read mem access file and access Cache
istringstream iss(line);
if (!(iss >> hexa >> bit)){
break;
}
stringstream saddr(hexa);
saddr >> std::hex >> integeraddress;
total ++;
bitaddress = bitset <32> (integeraddress);
index = (bitset<32>((bitaddress.to_string().substr(m,31))).to_ulong());
int arr_index = index.to_ulong();
if (bit == 0){
if(A[arr_index]==0){
A[arr_index]=0;
correct++;
j = 0;
}
else if(A[arr_index]==1){
A[arr_index]=0;
correct++;
j = 0;
}
else if(A[arr_index]==2){
A[arr_index]=0;
misprediction++;
j = 1;
}
else if (A[arr_index]==3)
A[arr_index]=2;
misprediction++;
j = 1;
}
}
else{
if(A[arr_index]==3){
A[arr_index]=3;
correct++;
j = 1;
}
else if(A[arr_index]==2){
A[arr_index]=3;
correct++;
j = 1;
}
else if(A[arr_index]==1){
A[arr_index]=3;
misprediction++;
j = 0;
}
else if (A[arr_index]==0){
A[arr_index]=1;
misprediction++;
j = 0;
}
}
tracesout<<j<<endl;
}
cout<<"Mispredicted is " <<misprediction <<"\t";
cout<<"correct is "<< correct <<"\n";
cout<<"total" << total <<"\n";
float xx = (float(misprediction) / float(total)) * 100 ;
cout<<"misprediction rate " << xx <<" \n";
traces.close();
tracesout.close();
}
else cout<< "Unable to open trace or traceout file ";
return 0;
}