gem5  v20.1.0.0
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 
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 
55 {
56  public:
58  SatCounter() = delete;
59 
71  explicit SatCounter(unsigned bits, uint8_t initial_val = 0)
72  : initialVal(initial_val), maxVal((1 << bits) - 1),
73  counter(initial_val)
74  {
75  fatal_if(bits > 8*sizeof(uint8_t),
76  "Number of bits exceeds counter size");
77  fatal_if(initial_val > maxVal,
78  "Saturating counter's Initial value exceeds max value.");
79  }
80 
86  SatCounter(const SatCounter& other)
87  : initialVal(other.initialVal), maxVal(other.maxVal),
88  counter(other.counter)
89  {
90  }
91 
97  SatCounter& operator=(const SatCounter& other) {
98  if (this != &other) {
99  SatCounter temp(other);
100  this->swap(temp);
101  }
102  return *this;
103  }
104 
111  {
112  initialVal = other.initialVal;
113  maxVal = other.maxVal;
114  counter = other.counter;
115  SatCounter temp(0);
116  other.swap(temp);
117  }
118 
125  if (this != &other) {
126  initialVal = other.initialVal;
127  maxVal = other.maxVal;
128  counter = other.counter;
129  SatCounter temp(0);
130  other.swap(temp);
131  }
132  return *this;
133  }
134 
143  void
144  swap(SatCounter& other)
145  {
146  std::swap(initialVal, other.initialVal);
147  std::swap(maxVal, other.maxVal);
148  std::swap(counter, other.counter);
149  }
150 
156  SatCounter&
158  {
159  if (counter < maxVal) {
160  ++counter;
161  }
162  return *this;
163  }
164 
170  SatCounter
172  {
173  SatCounter old_counter = *this;
174  ++*this;
175  return old_counter;
176  }
177 
183  SatCounter&
185  {
186  if (counter > 0) {
187  --counter;
188  }
189  return *this;
190  }
191 
197  SatCounter
199  {
200  SatCounter old_counter = *this;
201  --*this;
202  return old_counter;
203  }
204 
210  SatCounter&
211  operator>>=(const int& shift)
212  {
213  assert(shift >= 0);
214  this->counter >>= shift;
215  return *this;
216  }
217 
223  SatCounter&
224  operator<<=(const int& shift)
225  {
226  assert(shift >= 0);
227  this->counter <<= shift;
228  if (this->counter > maxVal) {
229  this->counter = maxVal;
230  }
231  return *this;
232  }
233 
239  SatCounter&
240  operator+=(const int& value)
241  {
242  if (value >= 0) {
243  if (maxVal - this->counter >= value) {
244  this->counter += value;
245  } else {
246  this->counter = maxVal;
247  }
248  } else {
249  *this -= -value;
250  }
251  return *this;
252  }
253 
259  SatCounter&
260  operator-=(const int& value)
261  {
262  if (value >= 0) {
263  if (this->counter > value) {
264  this->counter -= value;
265  } else {
266  this->counter = 0;
267  }
268  } else {
269  *this += -value;
270  }
271  return *this;
272  }
273 
279  operator uint8_t() const { return counter; }
280 
286  void reset() { counter = initialVal; }
287 
297  double calcSaturation() const { return (double) counter / maxVal; }
298 
306  bool isSaturated() const { return counter == maxVal; }
307 
315  uint8_t saturate()
316  {
317  const uint8_t diff = maxVal - counter;
318  counter = maxVal;
319  return diff;
320  }
321 
322  private:
323  uint8_t initialVal;
324  uint8_t maxVal;
325  uint8_t counter;
326 };
327 
328 #endif // __BASE_SAT_COUNTER_HH__
SatCounter::reset
void reset()
Reset the counter to its initial value.
Definition: sat_counter.hh:286
SatCounter::operator>>=
SatCounter & operator>>=(const int &shift)
Shift-right-assignment.
Definition: sat_counter.hh:211
SatCounter::SatCounter
SatCounter()=delete
The default constructor should never be used.
SatCounter::operator++
SatCounter operator++(int)
Post-increment operator.
Definition: sat_counter.hh:171
SatCounter::operator=
SatCounter & operator=(const SatCounter &other)
Copy assignment.
Definition: sat_counter.hh:97
SatCounter::saturate
uint8_t saturate()
Saturate the counter.
Definition: sat_counter.hh:315
ArmISA::shift
Bitfield< 6, 5 > shift
Definition: types.hh:126
SatCounter::calcSaturation
double calcSaturation() const
Calculate saturation percentile of the current counter's value with regard to its maximum possible va...
Definition: sat_counter.hh:297
SatCounter::operator<<=
SatCounter & operator<<=(const int &shift)
Shift-left-assignment.
Definition: sat_counter.hh:224
SatCounter::operator--
SatCounter & operator--()
Pre-decrement operator.
Definition: sat_counter.hh:184
SatCounter::operator+=
SatCounter & operator+=(const int &value)
Add-assignment.
Definition: sat_counter.hh:240
SatCounter::isSaturated
bool isSaturated() const
Whether the counter has achieved its maximum value or not.
Definition: sat_counter.hh:306
SatCounter::swap
void swap(SatCounter &other)
Swap the contents of every member of the class.
Definition: sat_counter.hh:144
SatCounter::SatCounter
SatCounter(SatCounter &&other)
Move constructor.
Definition: sat_counter.hh:110
SatCounter::operator-=
SatCounter & operator-=(const int &value)
Subtract-assignment.
Definition: sat_counter.hh:260
SatCounter::SatCounter
SatCounter(unsigned bits, uint8_t initial_val=0)
Constructor for the counter.
Definition: sat_counter.hh:71
SatCounter::maxVal
uint8_t maxVal
Definition: sat_counter.hh:324
SatCounter::operator++
SatCounter & operator++()
Pre-increment operator.
Definition: sat_counter.hh:157
SatCounter
Implements an n bit saturating counter and provides methods to increment, decrement,...
Definition: sat_counter.hh:54
SatCounter::operator--
SatCounter operator--(int)
Post-decrement operator.
Definition: sat_counter.hh:198
SatCounter::counter
uint8_t counter
Definition: sat_counter.hh:325
SatCounter::initialVal
uint8_t initialVal
Definition: sat_counter.hh:323
types.hh
logging.hh
SatCounter::SatCounter
SatCounter(const SatCounter &other)
Copy constructor.
Definition: sat_counter.hh:86
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
SatCounter::operator=
SatCounter & operator=(SatCounter &&other)
Move assignment.
Definition: sat_counter.hh:124
bits
T bits(T val, int first, int last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition: bitfield.hh:75

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17