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