gem5  v20.0.0.3
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 
69  explicit SatCounter(unsigned bits, uint8_t initial_val = 0)
70  : initialVal(initial_val), maxVal((1 << bits) - 1),
71  counter(initial_val)
72  {
73  fatal_if(bits > 8*sizeof(uint8_t),
74  "Number of bits exceeds counter size");
75  fatal_if(initial_val > maxVal,
76  "Saturating counter's Initial value exceeds max value.");
77  }
78 
80  SatCounter(const SatCounter& other)
81  : initialVal(other.initialVal), maxVal(other.maxVal),
82  counter(other.counter)
83  {
84  }
85 
87  SatCounter& operator=(const SatCounter& other) {
88  if (this != &other) {
89  SatCounter temp(other);
90  this->swap(temp);
91  }
92  return *this;
93  }
94 
97  {
98  initialVal = other.initialVal;
99  maxVal = other.maxVal;
100  counter = other.counter;
101  SatCounter temp(0);
102  other.swap(temp);
103  }
104 
107  if (this != &other) {
108  initialVal = other.initialVal;
109  maxVal = other.maxVal;
110  counter = other.counter;
111  SatCounter temp(0);
112  other.swap(temp);
113  }
114  return *this;
115  }
116 
123  void
124  swap(SatCounter& other)
125  {
126  std::swap(initialVal, other.initialVal);
127  std::swap(maxVal, other.maxVal);
128  std::swap(counter, other.counter);
129  }
130 
132  SatCounter&
134  {
135  if (counter < maxVal) {
136  ++counter;
137  }
138  return *this;
139  }
140 
142  SatCounter
144  {
145  SatCounter old_counter = *this;
146  ++*this;
147  return old_counter;
148  }
149 
151  SatCounter&
153  {
154  if (counter > 0) {
155  --counter;
156  }
157  return *this;
158  }
159 
161  SatCounter
163  {
164  SatCounter old_counter = *this;
165  --*this;
166  return old_counter;
167  }
168 
170  SatCounter&
171  operator>>=(const int& shift)
172  {
173  assert(shift >= 0);
174  this->counter >>= shift;
175  return *this;
176  }
177 
179  SatCounter&
180  operator<<=(const int& shift)
181  {
182  assert(shift >= 0);
183  this->counter <<= shift;
184  if (this->counter > maxVal) {
185  this->counter = maxVal;
186  }
187  return *this;
188  }
189 
191  SatCounter&
192  operator+=(const int& value)
193  {
194  if (value >= 0) {
195  if (maxVal - this->counter >= value) {
196  this->counter += value;
197  } else {
198  this->counter = maxVal;
199  }
200  } else {
201  *this -= -value;
202  }
203  return *this;
204  }
205 
207  SatCounter&
208  operator-=(const int& value)
209  {
210  if (value >= 0) {
211  if (this->counter > value) {
212  this->counter -= value;
213  } else {
214  this->counter = 0;
215  }
216  } else {
217  *this += -value;
218  }
219  return *this;
220  }
221 
225  operator uint8_t() const { return counter; }
226 
228  void reset() { counter = initialVal; }
229 
237  double calcSaturation() const { return (double) counter / maxVal; }
238 
244  bool isSaturated() const { return counter == maxVal; }
245 
251  uint8_t saturate()
252  {
253  const uint8_t diff = maxVal - counter;
254  counter = maxVal;
255  return diff;
256  }
257 
258  private:
259  uint8_t initialVal;
260  uint8_t maxVal;
261  uint8_t counter;
262 };
263 
264 #endif // __BASE_SAT_COUNTER_HH__
SatCounter & operator+=(const int &value)
Add-assignment.
Definition: sat_counter.hh:192
double calcSaturation() const
Calculate saturation percentile of the current counter&#39;s value with regard to its maximum possible va...
Definition: sat_counter.hh:237
SatCounter & operator-=(const int &value)
Subtract-assignment.
Definition: sat_counter.hh:208
SatCounter(unsigned bits, uint8_t initial_val=0)
Constructor for the counter.
Definition: sat_counter.hh:69
SatCounter & operator++()
Pre-increment operator.
Definition: sat_counter.hh:133
uint8_t counter
Definition: sat_counter.hh:261
uint8_t initialVal
Definition: sat_counter.hh:259
SatCounter & operator--()
Pre-decrement operator.
Definition: sat_counter.hh:152
Implements an n bit saturating counter and provides methods to increment, decrement, and read it.
Definition: sat_counter.hh:54
Bitfield< 6, 5 > shift
Definition: types.hh:125
SatCounter operator--(int)
Post-decrement operator.
Definition: sat_counter.hh:162
void reset()
Reset the counter to its initial value.
Definition: sat_counter.hh:228
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:199
SatCounter & operator=(SatCounter &&other)
Move assignment.
Definition: sat_counter.hh:106
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:143
uint8_t saturate()
Saturate the counter.
Definition: sat_counter.hh:251
bool isSaturated() const
Whether the counter has achieved its maximum value or not.
Definition: sat_counter.hh:244
void swap(SatCounter &other)
Swap the contents of every member of the class.
Definition: sat_counter.hh:124
SatCounter(const SatCounter &other)
Copy constructor.
Definition: sat_counter.hh:80
SatCounter(SatCounter &&other)
Move constructor.
Definition: sat_counter.hh:96
SatCounter & operator<<=(const int &shift)
Shift-left-assignment.
Definition: sat_counter.hh:180
SatCounter & operator=(const SatCounter &other)
Copy assignment.
Definition: sat_counter.hh:87
SatCounter & operator>>=(const int &shift)
Shift-right-assignment.
Definition: sat_counter.hh:171
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:71
uint8_t maxVal
Definition: sat_counter.hh:260

Generated on Fri Jul 3 2020 15:52:59 for gem5 by doxygen 1.8.13