gem5  v21.1.0.2
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 
50 namespace gem5
51 {
52 
59 template <class T>
61 {
62  public:
64  GenericSatCounter() = delete;
65 
77  explicit GenericSatCounter(unsigned bits, T initial_val = 0)
78  : initialVal(initial_val), maxVal((1ULL << bits) - 1),
79  counter(initial_val)
80  {
81  fatal_if(bits > 8*sizeof(T),
82  "Number of bits exceeds counter size");
83  fatal_if(initial_val > maxVal,
84  "Saturating counter's initial value exceeds max value.");
85  }
86 
93  : initialVal(other.initialVal), maxVal(other.maxVal),
94  counter(other.counter)
95  {
96  }
97 
104  if (this != &other) {
105  GenericSatCounter temp(other);
106  this->swap(temp);
107  }
108  return *this;
109  }
110 
117  {
118  initialVal = other.initialVal;
119  maxVal = other.maxVal;
120  counter = other.counter;
121  GenericSatCounter temp(0);
122  other.swap(temp);
123  }
124 
131  if (this != &other) {
132  initialVal = other.initialVal;
133  maxVal = other.maxVal;
134  counter = other.counter;
135  GenericSatCounter temp(0);
136  other.swap(temp);
137  }
138  return *this;
139  }
140 
149  void
151  {
152  std::swap(initialVal, other.initialVal);
153  std::swap(maxVal, other.maxVal);
154  std::swap(counter, other.counter);
155  }
156 
164  {
165  if (counter < maxVal) {
166  ++counter;
167  }
168  return *this;
169  }
170 
178  {
179  GenericSatCounter old_counter = *this;
180  ++*this;
181  return old_counter;
182  }
183 
191  {
192  if (counter > 0) {
193  --counter;
194  }
195  return *this;
196  }
197 
205  {
206  GenericSatCounter old_counter = *this;
207  --*this;
208  return old_counter;
209  }
210 
217  operator>>=(const int& shift)
218  {
219  assert(shift >= 0);
220  this->counter >>= shift;
221  return *this;
222  }
223 
230  operator<<=(const int& shift)
231  {
232  assert(shift >= 0);
233  this->counter <<= shift;
234  if (this->counter > maxVal) {
235  this->counter = maxVal;
236  }
237  return *this;
238  }
239 
246  operator+=(const long long& value)
247  {
248  if (value >= 0) {
249  if (maxVal - this->counter >= value) {
250  this->counter += value;
251  } else {
252  this->counter = maxVal;
253  }
254  } else {
255  *this -= -value;
256  }
257  return *this;
258  }
259 
266  operator-=(const long long& value)
267  {
268  if (value >= 0) {
269  if (this->counter > value) {
270  this->counter -= value;
271  } else {
272  this->counter = 0;
273  }
274  } else {
275  *this += -value;
276  }
277  return *this;
278  }
279 
285  operator T() const { return counter; }
286 
292  void reset() { counter = initialVal; }
293 
303  double calcSaturation() const { return (double) counter / maxVal; }
304 
312  bool isSaturated() const { return counter == maxVal; }
313 
321  uint8_t saturate()
322  {
323  const uint8_t diff = maxVal - counter;
324  counter = maxVal;
325  return diff;
326  }
327 
328  private:
332 };
333 
343 [[deprecated("Use SatCounter8 (or variants) instead")]]
345 
346 } // namespace gem5
347 
348 #endif // __BASE_SAT_COUNTER_HH__
gem5::GenericSatCounter::swap
void swap(GenericSatCounter &other)
Swap the contents of every member of the class.
Definition: sat_counter.hh:150
gem5::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:303
gem5::SatCounter8
GenericSatCounter< uint8_t > SatCounter8
Definition: sat_counter.hh:337
gem5::GenericSatCounter::operator+=
GenericSatCounter & operator+=(const long long &value)
Add-assignment.
Definition: sat_counter.hh:246
gem5::GenericSatCounter::operator-=
GenericSatCounter & operator-=(const long long &value)
Subtract-assignment.
Definition: sat_counter.hh:266
gem5::GenericSatCounter::operator--
GenericSatCounter operator--(int)
Post-decrement operator.
Definition: sat_counter.hh:204
gem5::GenericSatCounter::operator=
GenericSatCounter & operator=(GenericSatCounter &&other)
Move assignment.
Definition: sat_counter.hh:130
gem5::GenericSatCounter::GenericSatCounter
GenericSatCounter()=delete
The default constructor should never be used.
gem5::GenericSatCounter::isSaturated
bool isSaturated() const
Whether the counter has achieved its maximum value or not.
Definition: sat_counter.hh:312
gem5::GenericSatCounter::operator--
GenericSatCounter & operator--()
Pre-decrement operator.
Definition: sat_counter.hh:190
gem5::SatCounter16
GenericSatCounter< uint16_t > SatCounter16
Definition: sat_counter.hh:338
gem5::GenericSatCounter::counter
T counter
Definition: sat_counter.hh:331
gem5::ArmISA::shift
Bitfield< 6, 5 > shift
Definition: types.hh:117
gem5::GenericSatCounter
Implements an n bit saturating counter and provides methods to increment, decrement,...
Definition: sat_counter.hh:60
gem5::GenericSatCounter::initialVal
T initialVal
Definition: sat_counter.hh:329
gem5::GenericSatCounter::GenericSatCounter
GenericSatCounter(unsigned bits, T initial_val=0)
Constructor for the counter.
Definition: sat_counter.hh:77
gem5::GenericSatCounter::operator<<=
GenericSatCounter & operator<<=(const int &shift)
Shift-left-assignment.
Definition: sat_counter.hh:230
gem5::GenericSatCounter::reset
void reset()
Reset the counter to its initial value.
Definition: sat_counter.hh:292
gem5::GenericSatCounter::operator++
GenericSatCounter & operator++()
Pre-increment operator.
Definition: sat_counter.hh:163
gem5::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:76
gem5::GenericSatCounter::saturate
uint8_t saturate()
Saturate the counter.
Definition: sat_counter.hh:321
gem5::GenericSatCounter::operator++
GenericSatCounter operator++(int)
Post-increment operator.
Definition: sat_counter.hh:177
gem5::GenericSatCounter::maxVal
T maxVal
Definition: sat_counter.hh:330
gem5::GenericSatCounter::GenericSatCounter
GenericSatCounter(const GenericSatCounter &other)
Copy constructor.
Definition: sat_counter.hh:92
gem5::GenericSatCounter::operator=
GenericSatCounter & operator=(const GenericSatCounter &other)
Copy assignment.
Definition: sat_counter.hh:103
gem5::SatCounter64
GenericSatCounter< uint64_t > SatCounter64
Definition: sat_counter.hh:340
types.hh
gem5::SatCounter
SatCounter8 SatCounter
Definition: sat_counter.hh:344
gem5::SatCounter32
GenericSatCounter< uint32_t > SatCounter32
Definition: sat_counter.hh:339
logging.hh
gem5::GenericSatCounter::GenericSatCounter
GenericSatCounter(GenericSatCounter &&other)
Move constructor.
Definition: sat_counter.hh:116
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:225
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::GenericSatCounter::operator>>=
GenericSatCounter & operator>>=(const int &shift)
Shift-right-assignment.
Definition: sat_counter.hh:217

Generated on Tue Sep 21 2021 12:24:57 for gem5 by doxygen 1.8.17