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