gem5  v21.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, 2020 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 
41 #ifndef __BASE_SAT_COUNTER_HH__
42 #define __BASE_SAT_COUNTER_HH__
43 
44 #include <cassert>
45 #include <cstdint>
46 
47 #include "base/logging.hh"
48 #include "base/types.hh"
49 
56 template <class T>
58 {
59  public:
61  GenericSatCounter() = delete;
62 
74  explicit GenericSatCounter(unsigned bits, T initial_val = 0)
75  : initialVal(initial_val), maxVal((1ULL << bits) - 1),
76  counter(initial_val)
77  {
78  fatal_if(bits > 8*sizeof(T),
79  "Number of bits exceeds counter size");
80  fatal_if(initial_val > maxVal,
81  "Saturating counter's Initial value exceeds max value.");
82  }
83 
90  : initialVal(other.initialVal), maxVal(other.maxVal),
91  counter(other.counter)
92  {
93  }
94 
101  if (this != &other) {
102  GenericSatCounter temp(other);
103  this->swap(temp);
104  }
105  return *this;
106  }
107 
114  {
115  initialVal = other.initialVal;
116  maxVal = other.maxVal;
117  counter = other.counter;
118  GenericSatCounter temp(0);
119  other.swap(temp);
120  }
121 
128  if (this != &other) {
129  initialVal = other.initialVal;
130  maxVal = other.maxVal;
131  counter = other.counter;
132  GenericSatCounter temp(0);
133  other.swap(temp);
134  }
135  return *this;
136  }
137 
146  void
148  {
149  std::swap(initialVal, other.initialVal);
150  std::swap(maxVal, other.maxVal);
151  std::swap(counter, other.counter);
152  }
153 
161  {
162  if (counter < maxVal) {
163  ++counter;
164  }
165  return *this;
166  }
167 
175  {
176  GenericSatCounter old_counter = *this;
177  ++*this;
178  return old_counter;
179  }
180 
188  {
189  if (counter > 0) {
190  --counter;
191  }
192  return *this;
193  }
194 
202  {
203  GenericSatCounter old_counter = *this;
204  --*this;
205  return old_counter;
206  }
207 
214  operator>>=(const int& shift)
215  {
216  assert(shift >= 0);
217  this->counter >>= shift;
218  return *this;
219  }
220 
227  operator<<=(const int& shift)
228  {
229  assert(shift >= 0);
230  this->counter <<= shift;
231  if (this->counter > maxVal) {
232  this->counter = maxVal;
233  }
234  return *this;
235  }
236 
243  operator+=(const long long& value)
244  {
245  if (value >= 0) {
246  if (maxVal - this->counter >= value) {
247  this->counter += value;
248  } else {
249  this->counter = maxVal;
250  }
251  } else {
252  *this -= -value;
253  }
254  return *this;
255  }
256 
263  operator-=(const long long& value)
264  {
265  if (value >= 0) {
266  if (this->counter > value) {
267  this->counter -= value;
268  } else {
269  this->counter = 0;
270  }
271  } else {
272  *this += -value;
273  }
274  return *this;
275  }
276 
282  operator T() const { return counter; }
283 
289  void reset() { counter = initialVal; }
290 
300  double calcSaturation() const { return (double) counter / maxVal; }
301 
309  bool isSaturated() const { return counter == maxVal; }
310 
318  uint8_t saturate()
319  {
320  const uint8_t diff = maxVal - counter;
321  counter = maxVal;
322  return diff;
323  }
324 
325  private:
329 };
330 
340 [[deprecated("Use SatCounter8 (or variants) instead")]]
342 
343 #endif // __BASE_SAT_COUNTER_HH__
GenericSatCounter::operator--
GenericSatCounter & operator--()
Pre-decrement operator.
Definition: sat_counter.hh:187
GenericSatCounter::operator+=
GenericSatCounter & operator+=(const long long &value)
Add-assignment.
Definition: sat_counter.hh:243
GenericSatCounter::operator=
GenericSatCounter & operator=(GenericSatCounter &&other)
Move assignment.
Definition: sat_counter.hh:127
SatCounter32
GenericSatCounter< uint32_t > SatCounter32
Definition: sat_counter.hh:336
GenericSatCounter::GenericSatCounter
GenericSatCounter()=delete
The default constructor should never be used.
GenericSatCounter::swap
void swap(GenericSatCounter &other)
Swap the contents of every member of the class.
Definition: sat_counter.hh:147
GenericSatCounter::operator<<=
GenericSatCounter & operator<<=(const int &shift)
Shift-left-assignment.
Definition: sat_counter.hh:227
ArmISA::shift
Bitfield< 6, 5 > shift
Definition: types.hh:126
GenericSatCounter::initialVal
T initialVal
Definition: sat_counter.hh:326
GenericSatCounter::counter
T counter
Definition: sat_counter.hh:328
GenericSatCounter::operator++
GenericSatCounter operator++(int)
Post-increment operator.
Definition: sat_counter.hh:174
GenericSatCounter::GenericSatCounter
GenericSatCounter(const GenericSatCounter &other)
Copy constructor.
Definition: sat_counter.hh:89
GenericSatCounter::GenericSatCounter
GenericSatCounter(unsigned bits, T initial_val=0)
Constructor for the counter.
Definition: sat_counter.hh:74
GenericSatCounter::operator++
GenericSatCounter & operator++()
Pre-increment operator.
Definition: sat_counter.hh:160
GenericSatCounter
Implements an n bit saturating counter and provides methods to increment, decrement,...
Definition: sat_counter.hh:57
types.hh
GenericSatCounter::operator>>=
GenericSatCounter & operator>>=(const int &shift)
Shift-right-assignment.
Definition: sat_counter.hh:214
GenericSatCounter::GenericSatCounter
GenericSatCounter(GenericSatCounter &&other)
Move constructor.
Definition: sat_counter.hh:113
SatCounter64
GenericSatCounter< uint64_t > SatCounter64
Definition: sat_counter.hh:337
GenericSatCounter::maxVal
T maxVal
Definition: sat_counter.hh:327
logging.hh
SatCounter16
GenericSatCounter< uint16_t > SatCounter16
Definition: sat_counter.hh:335
bits
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition: bitfield.hh:73
GenericSatCounter::reset
void reset()
Reset the counter to its initial value.
Definition: sat_counter.hh:289
GenericSatCounter::saturate
uint8_t saturate()
Saturate the counter.
Definition: sat_counter.hh:318
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:219
GenericSatCounter::operator-=
GenericSatCounter & operator-=(const long long &value)
Subtract-assignment.
Definition: sat_counter.hh:263
GenericSatCounter::calcSaturation
double calcSaturation() const
Calculate saturation percentile of the current counter's value with regard to its maximum possible va...
Definition: sat_counter.hh:300
GenericSatCounter::operator--
GenericSatCounter operator--(int)
Post-decrement operator.
Definition: sat_counter.hh:201
GenericSatCounter::operator=
GenericSatCounter & operator=(const GenericSatCounter &other)
Copy assignment.
Definition: sat_counter.hh:100
SatCounter8
GenericSatCounter< uint8_t > SatCounter8
Definition: sat_counter.hh:334
ULL
#define ULL(N)
uint64_t constant
Definition: types.hh:46
GenericSatCounter::isSaturated
bool isSaturated() const
Whether the counter has achieved its maximum value or not.
Definition: sat_counter.hh:309
SatCounter
SatCounter8 SatCounter
Definition: sat_counter.hh:341

Generated on Tue Mar 23 2021 19:41:24 for gem5 by doxygen 1.8.17