2 * Copyright (c) 2019 Inria
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.
14 * Copyright (c) 2005-2006 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
41 #ifndef __BASE_SAT_COUNTER_HH__
42 #define __BASE_SAT_COUNTER_HH__
47 #include "base/logging.hh"
48 #include "base/types.hh"
51 * Implements an n bit saturating counter and provides methods to
52 * increment, decrement, and read it.
57 /** The default constructor should never be used. */
58 SatCounter() = delete;
61 * Constructor for the counter. The explicit keyword is used to make
62 * sure the user does not assign a number to the counter thinking it
63 * will be used as a counter value when it is in fact used as the number
66 * @param bits How many bits the counter will have.
67 * @param initial_val Starting value for the counter.
69 * @ingroup api_sat_counter
71 explicit SatCounter(unsigned bits, uint8_t initial_val = 0)
72 : initialVal(initial_val), maxVal((1 << bits) - 1),
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.");
84 * @ingroup api_sat_counter
86 SatCounter(const SatCounter& other)
87 : initialVal(other.initialVal), maxVal(other.maxVal),
88 counter(other.counter)
95 * @ingroup api_sat_counter
97 SatCounter& operator=(const SatCounter& other) {
99 SatCounter temp(other);
108 * @ingroup api_sat_counter
110 SatCounter(SatCounter&& other)
112 initialVal = other.initialVal;
113 maxVal = other.maxVal;
114 counter = other.counter;
122 * @ingroup api_sat_counter
124 SatCounter& operator=(SatCounter&& other) {
125 if (this != &other) {
126 initialVal = other.initialVal;
127 maxVal = other.maxVal;
128 counter = other.counter;
136 * Swap the contents of every member of the class. Used for the default
137 * copy-assignment created by the compiler.
139 * @param other The other object to swap contents with.
141 * @ingroup api_sat_counter
144 swap(SatCounter& other)
146 std::swap(initialVal, other.initialVal);
147 std::swap(maxVal, other.maxVal);
148 std::swap(counter, other.counter);
152 * Pre-increment operator.
154 * @ingroup api_sat_counter
159 if (counter < maxVal) {
166 * Post-increment operator.
168 * @ingroup api_sat_counter
173 SatCounter old_counter = *this;
179 * Pre-decrement operator.
181 * @ingroup api_sat_counter
193 * Post-decrement operator.
195 * @ingroup api_sat_counter
200 SatCounter old_counter = *this;
206 * Shift-right-assignment.
208 * @ingroup api_sat_counter
211 operator>>=(const int& shift)
214 this->counter >>= shift;
219 * Shift-left-assignment.
221 * @ingroup api_sat_counter
224 operator<<=(const int& shift)
227 this->counter <<= shift;
228 if (this->counter > maxVal) {
229 this->counter = maxVal;
237 * @ingroup api_sat_counter
240 operator+=(const int& value)
243 if (maxVal - this->counter >= value) {
244 this->counter += value;
246 this->counter = maxVal;
255 * Subtract-assignment.
257 * @ingroup api_sat_counter
260 operator-=(const int& value)
263 if (this->counter > value) {
264 this->counter -= value;
275 * Read the counter's value.
277 * @ingroup api_sat_counter
279 operator uint8_t() const { return counter; }
282 * Reset the counter to its initial value.
284 * @ingroup api_sat_counter
286 void reset() { counter = initialVal; }
289 * Calculate saturation percentile of the current counter's value
290 * with regard to its maximum possible value.
292 * @return A value between 0.0 and 1.0 to indicate which percentile of
293 * the maximum value the current value is.
295 * @ingroup api_sat_counter
297 double calcSaturation() const { return (double) counter / maxVal; }
300 * Whether the counter has achieved its maximum value or not.
302 * @return True if the counter saturated.
304 * @ingroup api_sat_counter
306 bool isSaturated() const { return counter == maxVal; }
309 * Saturate the counter.
311 * @return The value added to the counter to reach saturation.
313 * @ingroup api_sat_counter
317 const uint8_t diff = maxVal - counter;
328 #endif // __BASE_SAT_COUNTER_HH__