-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.cpp
More file actions
49 lines (49 loc) · 1.15 KB
/
command.cpp
File metadata and controls
49 lines (49 loc) · 1.15 KB
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
#include <iostream>
#include <cstring>
#include "parser.cpp"
enum counter{stronglyNotTaken,weaklyNotTaken,weaklyTaken,stronglyTaken};
class BranchPredictor
{
const static int N=5000,MOD=307;
int freq[N];
int getHash(int pc){return 1ll*pc*MOD%N;}
public:
BranchPredictor()
{
for(int i=0;i<N;i++) freq[i]=weaklyNotTaken;
}
bool getPredict(int pc)
{
int id=getHash(pc);
if(freq[id]==weaklyTaken||freq[id]==stronglyTaken) return true;
else return false;
}
void changeFreq(bool is,int pc)
{
int id=getHash(pc);
if(is&&freq[id]!=stronglyTaken) freq[id]++;
if(!is&&freq[id]!=stronglyNotTaken) freq[id]--;
}
} bP;
class CommandQueue
{
bool used;
int pc,pc2;
public:
CommandQueue(){pc=pc2=0;}
void nextClock(){pc=pc2;used=false;}
void modify(int v){pc2=v;used=true;}
int getpc(){return pc;}
int getCommand()
{
int ret=pc;
if(used||getType1(pc)=='?') return -1;
if(getType1(pc)=='B')
{
if(bP.getPredict(pc)) pc2+=getImm(pc);
else pc2+=4;
}
else pc2+=4;
return ret;
}
} cQ;