remote_gdb.cc:
[gem5.git] / base / hybrid_pred.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 //==========================================================================
30 //
31 // This predictor takes the AND of a "local" and a "global" predictor
32 // in order to determine its prediction.
33 //
34 //
35 //
36 //
37
38 #ifndef __HYBRID_PRED_HH__
39 #define __HYBRID_PRED_HH__
40
41 #include <string>
42
43 #include "base/sat_counter.hh"
44
45 #include "base/statistics.hh"
46 #include "sim/sim_stats.hh"
47
48 class HybridPredictor : public GenericPredictor
49 {
50 private:
51 std::string pred_name;
52 std::string one_name;
53 std::string zero_name;
54 bool reg_individual_stats;
55
56 SaturatingCounterPred *local;
57 SaturatingCounterPred *global;
58
59 unsigned long max_index;
60
61 //
62 // Stats
63 //
64 Statistics::Scalar<> pred_one; //num_one_preds
65 Statistics::Scalar<> pred_zero; //num_zero_preds
66 Statistics::Scalar<> correct_pred_one; //num_one_correct
67 Statistics::Scalar<> correct_pred_zero; //num_zero_correct
68 Statistics::Scalar<> record_one; //num_one_updates
69 Statistics::Scalar<> record_zero; //num_zero_updates
70
71 Statistics::Formula total_preds;
72 Statistics::Formula frac_preds_zero;
73 Statistics::Formula frac_preds_one;
74 Statistics::Formula total_correct;
75 Statistics::Formula total_accuracy;
76 Statistics::Formula zero_accuracy;
77 Statistics::Formula one_accuracy;
78 Statistics::Formula zero_coverage;
79 Statistics::Formula one_coverage;
80
81 public:
82 HybridPredictor(const char *_p_name, const char *_z_name,
83 const char *_o_name,
84 unsigned _index_bits, unsigned _counter_bits,
85 unsigned _zero_change, unsigned _one_change,
86 unsigned _thresh,
87 unsigned _global_bits, unsigned _global_thresh,
88 bool _reg_individual_stats = false);
89
90 void clear() {
91 global->clear();
92 local->clear();
93 }
94
95 unsigned peek(unsigned long _index) {
96 unsigned l = local->peek(_index);
97 unsigned g = global->peek(_index);
98
99 if (l && g)
100 return 1;
101
102 return 0;
103 }
104
105 unsigned value(unsigned long _index) {
106 unsigned l = local->peek(_index);
107 unsigned g = global->peek(_index);
108
109 l = l & 0xFFFF;
110 g = g & 0xFFFF;
111
112 return (l << 16) | g;
113
114 }
115
116 unsigned predict(unsigned long _index) {
117 unsigned l = local->predict(_index);
118 unsigned g = global->predict(_index);
119
120 if (l && g) {
121 ++pred_one;
122 return 1;
123 }
124
125 ++pred_zero;
126 return 0;
127 }
128
129
130 //
131 // This version need only be used if local/global statistics
132 // will be maintained
133 //
134 unsigned predict(unsigned long _index, unsigned &_pdata) {
135 unsigned l = local->predict(_index);
136 unsigned g = global->predict(_index);
137
138 //
139 // bit 0 => local predictor result
140 // bit 1 => global predictor result
141 //
142 _pdata = 0;
143 if (l)
144 _pdata |= 1;
145 if (g)
146 _pdata |= 2;
147 if (l && g) {
148 ++pred_one;
149 return 1;
150 }
151
152 ++pred_zero;
153 return 0;
154 }
155
156 void record(unsigned long _index, unsigned _val, unsigned _predicted) {
157
158 if (_val) {
159 local->record(_index, _val, 0);
160 global->record(_index, _val, 0);
161 ++record_one;
162
163 if (_val == _predicted) {
164 ++correct_pred_one;
165 }
166 } else {
167 local->record(_index, _val, 0);
168 global->record(_index, _val, 0);
169 ++record_zero;
170
171 if (_val == _predicted)
172 ++correct_pred_zero;
173 }
174 }
175
176 void record(unsigned long _index, unsigned _val, unsigned _predicted,
177 unsigned _pdata)
178 {
179
180 local->record(_index, _val, (_pdata & 1));
181 global->record(_index, _val, ((_pdata & 2) ? 1 : 0));
182
183
184 if (_val) {
185 ++record_one;
186
187 if (_val == _predicted)
188 ++correct_pred_one;
189 } else {
190 ++record_zero;
191
192 if (_val == _predicted)
193 ++correct_pred_zero;
194 }
195 }
196
197 void regStats();
198 void regFormulas();
199 };
200
201
202 #endif // _HYBRID_PRED_HH__
203