-
Notifications
You must be signed in to change notification settings - Fork 0
/
predictor.hpp
93 lines (83 loc) · 2.38 KB
/
predictor.hpp
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
#ifndef PREDICTOR_HPP
#define PREDICTOR_HPP
#include <cstring>
using u32 = unsigned int;
using u8 = unsigned char;
//#define BHT_VALUE (BHT[iniPC & 0b111111111f])
#define BTB_VALUE (BTB[iniPC & 0b111111111])
#define OPcode (code & 0b1111111)
#define BHT_VALUE (BHT[iniPC & 0b111111111])
#define PHT_VALUE (PHT[iniPC & 0b111111111][BHT_VALUE])
extern double rate;
class predictor{
u32 suc,cnt;
u8 BHT[512];
u8 PHT[512][64];
u32 BTB[512];
public:
predictor(){
suc = 0;cnt = 0;
memset(BHT,0,sizeof(BHT));
memset(PHT,0,sizeof(PHT));
memset(BTB,0,sizeof(BTB));
}
~predictor(){
//if(cnt == 0)printf("no Branch");
//else printf("%lf ",((double) (suc)/cnt))
//,rate += (double)suc / (double)cnt;
//printf("suc : %d cnt %d \n\n",suc,cnt);
}
int predict(u32 code,u32 iniPC){
if(OPcode == 0b1100011 && (PHT_VALUE & 0b10))return BTB_VALUE;
return iniPC + 4;
}
void update(bool BranchTaken,bool isSuc,u32 iniPC,u32 jumpAddr){//针对分支命令
if(isSuc)++suc;
++cnt;
if(BranchTaken){
if(PHT_VALUE < 0b11u)++PHT_VALUE;
BHT_VALUE = (BHT_VALUE >> 1);
BTB_VALUE = jumpAddr;
} else {
if(PHT_VALUE > 0b00u)--PHT_VALUE;
BHT_VALUE = (BHT_VALUE >> 1) | 0b100000;
}
}
};
/* 二位饱和计数器预测
//only for b
class predictor{
u32 suc,cnt;
u8 BHT[4097]; //branch history table
u32 BTB[65]; //branch target buffer
public:
predictor(){
suc = 0;cnt = 0;
memset(BHT,0,sizeof(BHT));
memset(BTB,0,sizeof(BTB));
}
~predictor(){
if(cnt == 0)printf("no Branch");
else printf("%lf\n",((double) (suc)/cnt));
}
int predict(u32 code,u32 iniPC){//都进
if(OPcode == 0b1100011 && (BHT_VALUE & 0b10))return BTB_VALUE;
return iniPC + 4;
}
//只有分支指令才调用此函数
void update(bool BranchTaken,bool isSuc,u32 iniPC,u32 jumpAddr){//针对分支命令分支是否被采纳
if(isSuc)++suc;
//printf("%d\n",isSuc);
++cnt;
//不管是否成功,用isBrT和jumpAddr来更新
//iniPC
if(BranchTaken){
if(BHT_VALUE < 0b11)++BHT_VALUE;
BTB_VALUE = jumpAddr;
} else {
if(BHT_VALUE > 0b00)--BHT_VALUE;
}
}
};
*/
#endif