-
Notifications
You must be signed in to change notification settings - Fork 2
/
RP_PLRU.cpp
115 lines (95 loc) · 2.48 KB
/
RP_PLRU.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
#include "RP_PLRU.h"
unsigned RP_PLRU::insertionPosition(unsigned set, unsigned slice)
{
// check for invalid lines
for (unsigned i = 0; i < ways_; i++)
{
if (!memory_[slice * lines_ + set * ways_ + i].valid)
{
return i;
}
}
unsigned way = 0;
int bit = 1 << (ways_ - 2); //start at top level decision bit
for (unsigned i = 0; i < way_bits_; i++)
{
bool plru_bit = (plru_sets_[slice * sets_ + set] & bit) != 0;
way <<= 1;
way |= plru_bit;
bit >>= plru_bit << i;
bit >>= 1 << i;
}
return way;
}
InsertionPosition RP_PLRU::insertionPosition(CacheSet* set, unsigned slice)
{
unsigned subset, way;
for (subset = 0; subset < subsets_; subset++)
{
for (way = 0; way < subset_ways_; way++)
{
if (!(*c_memory_)(slice, subset, set, way).valid)
{
return { subset, way };
}
}
}
subset = 0;
if (subsets_ != 1)
subset = rand() % subsets_;
way = 0;
int bit = 1 << (subset_ways_ - 2); //start at top level decision bit
for (unsigned i = 0; i < subset_way_bits_; i++)
{
bool plru_bit = (plru_sets_[slice * subsets_ * sets_ + subset * sets_ + set->index[subset]] & bit) != 0;
way <<= 1;
way |= plru_bit;
bit >>= plru_bit << i;
bit >>= 1 << i;
}
return { subset, way };
}
void RP_PLRU::updateSet(unsigned set, unsigned slice, unsigned way, bool replacement)
{
int bit = 1 << (ways_ - 2);
for (unsigned i = 0; i < way_bits_; i++)
{
bool plru_bit = (way & (1 << (way_bits_ - (i + 1)))) != 0;
//change bit away from path to current way
if (plru_bit)
{
plru_sets_[slice * sets_ + set] &= ~bit;
bit >>= 1 << i;
}
else
{
plru_sets_[slice * sets_ + set] |= bit;
}
bit >>= 1 << i;
}
}
void RP_PLRU::updateSet(CacheLine& line, CacheSet *set, unsigned slice, unsigned subset, unsigned way, bool replacement)
{
int bit = 1 << (subset_ways_ - 2);
for (unsigned i = 0; i < subset_way_bits_; i++)
{
bool plru_bit = (way & (1 << (subset_way_bits_ - (i + 1)))) != 0;
//change bit away from path to current way
if (plru_bit)
{
plru_sets_[slice * subsets_ * sets_ + subset * sets_ + set->index[subset]] &= ~bit;
bit >>= 1 << i;
}
else
{
plru_sets_[slice * subsets_ * sets_ + subset * sets_ + set->index[subset]] |= bit;
}
bit >>= 1 << i;
}
}
RPolicy* RP_PLRU::getCopy()
{
RPolicy* copy = static_cast<RPolicy*>(new RP_PLRU(*this));
((RP_PLRU*)copy)->initCopy();
return copy;
}