-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbit_branch_predictor.h
76 lines (66 loc) · 1.91 KB
/
nbit_branch_predictor.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
/*
* nbit_branch_predictor.h
*
* Branch Predictor Class: nbit Predictor
* for Sniper Simulation
*
* < Chris Mark >
*
*/
#ifndef NBIT_BRANCH_PREDICTOR_H
#define NBIT_BRANCH_PREDICTOR_H
#include "branch_predictor.h"
#include <cstring>
#include <iostream>
class NbitBranchPredictor : public BranchPredictor
{
public:
NbitBranchPredictor(String name, core_id_t core_id,
unsigned int index_bits_, unsigned int cntr_bits_)
: BranchPredictor(name, core_id)
, index_bits(index_bits_)
, cntr_bits(cntr_bits_)
{
table_entries = 1 << index_bits;
TABLE = new unsigned long long[table_entries];
memset(TABLE, 0, table_entries * sizeof(*TABLE));
COUNTER_MAX = (1 << cntr_bits) - 1;
};
~NbitBranchPredictor()
{
delete TABLE;
};
/**
* Called from the Simulator to predict the result of branch instruction
* in address 'ip' and target 'target'.
**/
bool predict(IntPtr ip, IntPtr target)
{
unsigned int ip_table_index = ip % table_entries;
unsigned long long ip_table_value = TABLE[ip_table_index];
unsigned long long prediction = ip_table_value >> (cntr_bits - 1);
return (prediction != 0);
};
/**
* Called from the Simulator to update the internal state of the predictor.
**/
void update(bool predicted, bool actual, IntPtr ip, IntPtr target)
{
unsigned int ip_table_index = ip % table_entries;
if (actual) {
if (TABLE[ip_table_index] < COUNTER_MAX)
TABLE[ip_table_index]++;
} else {
if (TABLE[ip_table_index] > 0)
TABLE[ip_table_index]--;
}
updateCounters(predicted, actual);
};
private:
unsigned int index_bits, cntr_bits;
unsigned int COUNTER_MAX;
/* Make this unsigned long long so as to support big numbers of cntr_bits. */
unsigned long long *TABLE;
unsigned int table_entries;
};
#endif