gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
50namespace gem5
51{
52
59template <class T>
61{
62 public:
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
293
303 double calcSaturation() const { return (double) counter / maxVal; }
304
312 bool isSaturated() const { return counter == maxVal; }
313
322 {
323 const T diff = maxVal - counter;
324 counter = maxVal;
325 return diff;
326 }
327
328 private:
332};
333
343} // namespace gem5
344
345#endif // __BASE_SAT_COUNTER_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
Implements an n bit saturating counter and provides methods to increment, decrement,...
GenericSatCounter()=delete
The default constructor should never be used.
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:79
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
GenericSatCounter & operator+=(const long long &value)
Add-assignment.
GenericSatCounter< uint32_t > SatCounter32
bool isSaturated() const
Whether the counter has achieved its maximum value or not.
GenericSatCounter & operator<<=(const int &shift)
Shift-left-assignment.
GenericSatCounter operator--(int)
Post-decrement operator.
GenericSatCounter(unsigned bits, T initial_val=0)
Constructor for the counter.
GenericSatCounter(const GenericSatCounter &other)
Copy constructor.
GenericSatCounter & operator=(const GenericSatCounter &other)
Copy assignment.
GenericSatCounter & operator=(GenericSatCounter &&other)
Move assignment.
GenericSatCounter operator++(int)
Post-increment operator.
double calcSaturation() const
Calculate saturation percentile of the current counter's value with regard to its maximum possible va...
GenericSatCounter(GenericSatCounter &&other)
Move constructor.
T saturate()
Saturate the counter.
void swap(GenericSatCounter &other)
Swap the contents of every member of the class.
GenericSatCounter & operator>>=(const int &shift)
Shift-right-assignment.
GenericSatCounter< uint16_t > SatCounter16
GenericSatCounter< uint8_t > SatCounter8
GenericSatCounter< uint64_t > SatCounter64
void reset()
Reset the counter to its initial value.
GenericSatCounter & operator++()
Pre-increment operator.
GenericSatCounter & operator-=(const long long &value)
Subtract-assignment.
GenericSatCounter & operator--()
Pre-decrement operator.
Bitfield< 6, 5 > shift
Definition types.hh:117
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0