inorder: enforce 78-character rule
[gem5.git] / src / base / sat_counter.hh
1 /*
2 * Copyright (c) 2001-2005 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 * Authors: Steve Raasch
29 * Steve Reinhardt
30 */
31
32 #ifndef __SAT_COUNTER_HH__
33 #define __SAT_COUNTER_HH__
34
35 #include <string>
36
37 #include "base/predictor.hh"
38
39 #include "base/statistics.hh"
40 #include "sim/stats.hh"
41
42 //
43 //
44 // A simple saturating counter predictor
45 //
46 //
47 class SaturatingCounterPred : public GenericPredictor
48 {
49 private:
50 std::string pred_name;
51 std::string zero_name;
52 std::string one_name;
53
54 unsigned index_bits;
55 unsigned counter_bits;
56 unsigned zero_change;
57 unsigned one_change;
58 unsigned thresh;
59 unsigned init_value;
60
61 unsigned max_value; // maximum counter value
62
63 unsigned long max_index; // also the index mask value
64 unsigned *table;
65
66 // Statistics
67 Stats::Scalar predicted_one; // Total predictions of one, preds_one
68 Stats::Scalar predicted_zero; // Total predictions of zero, preds_zero
69 Stats::Scalar correct_pred_one; // Total correct predictions of one, correct_one
70 Stats::Scalar correct_pred_zero; // Total correct predictions of zero, correct_zero
71
72 Stats::Scalar record_zero; //updates_zero
73 Stats::Scalar record_one; //updates_one
74
75 Stats::Formula preds_total;
76 Stats::Formula pred_frac_zero;
77 Stats::Formula pred_frac_one;
78 Stats::Formula correct_total;
79 Stats::Formula updates_total;
80 Stats::Formula pred_rate;
81 Stats::Formula frac_correct_zero;
82 Stats::Formula frac_correct_one;
83 Stats::Formula coverage_zero;
84 Stats::Formula coverage_one;
85
86 private:
87 bool pred_one(unsigned &counter) { return counter > thresh; }
88 bool pred_zero(unsigned &counter) { return counter <= thresh; }
89
90 void update_one(unsigned &counter) {
91
92 if (one_change)
93 counter += one_change;
94 else
95 counter = 0;
96
97 // check for wrap
98 if (counter > max_value)
99 counter = max_value;
100 }
101
102 void update_zero(unsigned &counter) {
103 if (zero_change) {
104 // check for wrap
105 if (counter < zero_change)
106 counter = 0;
107 else
108 counter -= zero_change;
109 } else
110 counter = 0;
111 }
112
113
114 public:
115
116 SaturatingCounterPred(std::string p_name,
117 std::string z_name, std::string o_name,
118 unsigned _index_bits, unsigned _counter_bits = 2,
119 unsigned _zero_change = 1, unsigned _one_change = 1,
120 unsigned _thresh = 1, unsigned _init_value = 0);
121
122 void
123 clear()
124 {
125 for (unsigned long i = 0; i <= max_index; ++i)
126 table[i] = init_value;
127 }
128
129 // Record the ACTUAL result... and indicate whether the prediction
130 // corresponding to this event was correct
131 void record(unsigned long _index, unsigned _val, unsigned _predicted,
132 unsigned _pdata)
133 {
134 record(_index, _val, _predicted);
135 }
136
137 void record(unsigned long _index, unsigned _val, unsigned _predicted) {
138 unsigned long index = _index & max_index;
139
140 if (_val) {
141 update_one(table[index]);
142 ++record_one;
143
144 if (_predicted)
145 ++correct_pred_one;
146 } else {
147 update_zero(table[index]);
148 ++record_zero;
149
150 if (!_predicted)
151 ++correct_pred_zero;
152 }
153 }
154
155 unsigned value(unsigned long _index) {
156 unsigned long index = _index & max_index;
157
158 return table[index];
159 }
160
161
162 unsigned predict(unsigned long _index, unsigned &pdata) {
163 return predict(_index);
164 }
165
166 unsigned predict(unsigned long _index) {
167 unsigned long index = _index & max_index;
168
169 if (pred_one(table[index])) {
170 ++predicted_one;
171 return 1;
172 }
173
174 ++predicted_zero;
175 return 0;
176 }
177
178 // No internal state is changed here
179 unsigned peek(unsigned long _index) {
180 unsigned long index = _index & max_index;
181
182 if (pred_one(table[index]))
183 return 1;
184
185 return 0;
186 }
187
188
189 //=======================================================
190 void regStats();
191 void regFormulas();
192 };
193
194
195 #endif // __SAT_COUNTER_HH__