Remove a ton of old simplescalar crap that we no longer need.
[gem5.git] / sim / sat_counter.hh
1 /*
2 * Copyright (c) 2003 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef __SAT_COUNTER_HH__
30 #define __SAT_COUNTER_HH__
31
32 #include <string>
33
34 #include "predictor.hh"
35
36 #include "statistics.hh"
37 #include "sim_stats.hh"
38
39 struct stat_sdb_t;
40
41 //
42 //
43 // A simple saturating counter predictor
44 //
45 //
46 class SaturatingCounterPred : public GenericPredictor
47 {
48 private:
49 std::string pred_name;
50 std::string zero_name;
51 std::string one_name;
52
53 unsigned index_bits;
54 unsigned counter_bits;
55 unsigned zero_change;
56 unsigned one_change;
57 unsigned thresh;
58 unsigned init_value;
59
60 unsigned max_value; // maximum counter value
61
62 unsigned long max_index; // also the index mask value
63 unsigned *table;
64
65 // Statistics
66 Statistics::Scalar<> predicted_one; // Total predictions of one, preds_one
67 Statistics::Scalar<> predicted_zero; // Total predictions of zero, preds_zero
68 Statistics::Scalar<> correct_pred_one; // Total correct predictions of one, correct_one
69 Statistics::Scalar<> correct_pred_zero; // Total correct predictions of zero, correct_zero
70
71 Statistics::Scalar<> record_zero; //updates_zero
72 Statistics::Scalar<> record_one; //updates_one
73
74 Statistics::Formula preds_total;
75 Statistics::Formula pred_frac_zero;
76 Statistics::Formula pred_frac_one;
77 Statistics::Formula correct_total;
78 Statistics::Formula updates_total;
79 Statistics::Formula pred_rate;
80 Statistics::Formula frac_correct_zero;
81 Statistics::Formula frac_correct_one;
82 Statistics::Formula coverage_zero;
83 Statistics::Formula coverage_one;
84
85 private:
86 bool pred_one(unsigned &counter) { return counter > thresh; }
87 bool pred_zero(unsigned &counter) { return counter <= thresh; }
88
89 void update_one(unsigned &counter) {
90
91 if (one_change)
92 counter += one_change;
93 else
94 counter = 0;
95
96 // check for wrap
97 if (counter > max_value)
98 counter = max_value;
99 }
100
101 void update_zero(unsigned &counter) {
102 if (zero_change) {
103 // check for wrap
104 if (counter < zero_change)
105 counter = 0;
106 else
107 counter -= zero_change;
108 } else
109 counter = 0;
110 }
111
112
113 public:
114
115 SaturatingCounterPred(std::string p_name,
116 std::string z_name, std::string o_name,
117 unsigned _index_bits, unsigned _counter_bits = 2,
118 unsigned _zero_change = 1, unsigned _one_change = 1,
119 unsigned _thresh = 1, unsigned _init_value = 0);
120
121 void clear() {
122 for (int i = 0; i <= max_index; ++i)
123 table[i] = init_value;
124 }
125
126 // Record the ACTUAL result... and indicate whether the prediction
127 // corresponding to this event was correct
128 void record(unsigned long _index, unsigned _val, unsigned _predicted,
129 unsigned _pdata)
130 {
131 record(_index, _val, _predicted);
132 }
133
134 void record(unsigned long _index, unsigned _val, unsigned _predicted) {
135 unsigned long index = _index & max_index;
136
137 if (_val) {
138 update_one(table[index]);
139 ++record_one;
140
141 if (_predicted)
142 ++correct_pred_one;
143 } else {
144 update_zero(table[index]);
145 ++record_zero;
146
147 if (!_predicted)
148 ++correct_pred_zero;
149 }
150 }
151
152 unsigned value(unsigned long _index) {
153 unsigned long index = _index & max_index;
154
155 return table[index];
156 }
157
158
159 unsigned predict(unsigned long _index, unsigned &pdata) {
160 return predict(_index);
161 }
162
163 unsigned predict(unsigned long _index) {
164 unsigned long index = _index & max_index;
165
166 if (pred_one(table[index])) {
167 ++predicted_one;
168 return 1;
169 }
170
171 ++predicted_zero;
172 return 0;
173 }
174
175 // No internal state is changed here
176 unsigned peek(unsigned long _index) {
177 unsigned long index = _index & max_index;
178
179 if (pred_one(table[index]))
180 return 1;
181
182 return 0;
183 }
184
185
186 //=======================================================
187 void regStats();
188 void regFormulas();
189 };
190
191
192 #endif // __SAT_COUNTER_HH__