gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sat_counter.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Inria
3  * All rights reserved.
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2005-2006 The Regents of The University of Michigan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are
19  * met: redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer;
21  * redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution;
24  * neither the name of the copyright holders nor the names of its
25  * contributors may be used to endorse or promote products derived from
26  * this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * Authors: Kevin Lim
41  * Daniel Carvalho
42  */
43 
44 #ifndef __BASE_SAT_COUNTER_HH__
45 #define __BASE_SAT_COUNTER_HH__
46 
47 #include <cassert>
48 #include <cstdint>
49 
50 #include "base/logging.hh"
51 #include "base/types.hh"
52 
58 {
59  public:
61  SatCounter() = delete;
62 
72  explicit SatCounter(unsigned bits, uint8_t initial_val = 0)
73  : initialVal(initial_val), maxVal((1 << bits) - 1),
74  counter(initial_val)
75  {
76  fatal_if(bits > 8*sizeof(uint8_t),
77  "Number of bits exceeds counter size");
78  fatal_if(initial_val > maxVal,
79  "Saturating counter's Initial value exceeds max value.");
80  }
81 
83  SatCounter(const SatCounter& other)
84  : initialVal(other.initialVal), maxVal(other.maxVal),
85  counter(other.counter)
86  {
87  }
88 
90  SatCounter& operator=(const SatCounter& other) {
91  if (this != &other) {
92  SatCounter temp(other);
93  this->swap(temp);
94  }
95  return *this;
96  }
97 
100  {
101  initialVal = other.initialVal;
102  maxVal = other.maxVal;
103  counter = other.counter;
104  SatCounter temp(0);
105  other.swap(temp);
106  }
107 
110  if (this != &other) {
111  initialVal = other.initialVal;
112  maxVal = other.maxVal;
113  counter = other.counter;
114  SatCounter temp(0);
115  other.swap(temp);
116  }
117  return *this;
118  }
119 
126  void
127  swap(SatCounter& other)
128  {
129  std::swap(initialVal, other.initialVal);
130  std::swap(maxVal, other.maxVal);
131  std::swap(counter, other.counter);
132  }
133 
135  SatCounter&
137  {
138  if (counter < maxVal) {
139  ++counter;
140  }
141  return *this;
142  }
143 
145  SatCounter
147  {
148  SatCounter old_counter = *this;
149  ++*this;
150  return old_counter;
151  }
152 
154  SatCounter&
156  {
157  if (counter > 0) {
158  --counter;
159  }
160  return *this;
161  }
162 
164  SatCounter
166  {
167  SatCounter old_counter = *this;
168  --*this;
169  return old_counter;
170  }
171 
173  SatCounter&
174  operator>>=(const int& shift)
175  {
176  assert(shift >= 0);
177  this->counter >>= shift;
178  return *this;
179  }
180 
182  SatCounter&
183  operator<<=(const int& shift)
184  {
185  assert(shift >= 0);
186  this->counter <<= shift;
187  if (this->counter > maxVal) {
188  this->counter = maxVal;
189  }
190  return *this;
191  }
192 
194  SatCounter&
195  operator+=(const int& value)
196  {
197  if (value >= 0) {
198  if (maxVal - this->counter >= value) {
199  this->counter += value;
200  } else {
201  this->counter = maxVal;
202  }
203  } else {
204  *this -= -value;
205  }
206  return *this;
207  }
208 
210  SatCounter&
211  operator-=(const int& value)
212  {
213  if (value >= 0) {
214  if (this->counter > value) {
215  this->counter -= value;
216  } else {
217  this->counter = 0;
218  }
219  } else {
220  *this += -value;
221  }
222  return *this;
223  }
224 
228  operator uint8_t() const { return counter; }
229 
231  void reset() { counter = initialVal; }
232 
240  double calcSaturation() const { return (double) counter / maxVal; }
241 
247  bool isSaturated() const { return counter == maxVal; }
248 
254  uint8_t saturate()
255  {
256  const uint8_t diff = maxVal - counter;
257  counter = maxVal;
258  return diff;
259  }
260 
261  private:
262  uint8_t initialVal;
263  uint8_t maxVal;
264  uint8_t counter;
265 };
266 
267 #endif // __BASE_SAT_COUNTER_HH__
SatCounter & operator+=(const int &value)
Add-assignment.
Definition: sat_counter.hh:195
double calcSaturation() const
Calculate saturation percentile of the current counter&#39;s value with regard to its maximum possible va...
Definition: sat_counter.hh:240
SatCounter & operator-=(const int &value)
Subtract-assignment.
Definition: sat_counter.hh:211
SatCounter(unsigned bits, uint8_t initial_val=0)
Constructor for the counter.
Definition: sat_counter.hh:72
SatCounter & operator++()
Pre-increment operator.
Definition: sat_counter.hh:136
uint8_t counter
Definition: sat_counter.hh:264
uint8_t initialVal
Definition: sat_counter.hh:262
SatCounter & operator--()
Pre-decrement operator.
Definition: sat_counter.hh:155
Implements an n bit saturating counter and provides methods to increment, decrement, and read it.
Definition: sat_counter.hh:57
Bitfield< 6, 5 > shift
Definition: types.hh:127
SatCounter operator--(int)
Post-decrement operator.
Definition: sat_counter.hh:165
void reset()
Reset the counter to its initial value.
Definition: sat_counter.hh:231
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:203
SatCounter & operator=(SatCounter &&other)
Move assignment.
Definition: sat_counter.hh:109
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
SatCounter operator++(int)
Post-increment operator.
Definition: sat_counter.hh:146
uint8_t saturate()
Saturate the counter.
Definition: sat_counter.hh:254
bool isSaturated() const
Whether the counter has achieved its maximum value or not.
Definition: sat_counter.hh:247
void swap(SatCounter &other)
Swap the contents of every member of the class.
Definition: sat_counter.hh:127
SatCounter(const SatCounter &other)
Copy constructor.
Definition: sat_counter.hh:83
SatCounter(SatCounter &&other)
Move constructor.
Definition: sat_counter.hh:99
SatCounter & operator<<=(const int &shift)
Shift-left-assignment.
Definition: sat_counter.hh:183
SatCounter & operator=(const SatCounter &other)
Copy assignment.
Definition: sat_counter.hh:90
SatCounter & operator>>=(const int &shift)
Shift-right-assignment.
Definition: sat_counter.hh:174
SatCounter()=delete
The default constructor should never be used.
T bits(T val, int first, int last)
Extract the bitfield from position &#39;first&#39; to &#39;last&#39; (inclusive) from &#39;val&#39; and right justify it...
Definition: bitfield.hh:72
uint8_t maxVal
Definition: sat_counter.hh:263

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13