sat_counter.hh revision 13960
113960Sodanrc@yahoo.com.br/* 213960Sodanrc@yahoo.com.br * Copyright (c) 2019 Inria 313960Sodanrc@yahoo.com.br * All rights reserved. 413960Sodanrc@yahoo.com.br * 513960Sodanrc@yahoo.com.br * The license below extends only to copyright in the software and shall 613960Sodanrc@yahoo.com.br * not be construed as granting a license to any other intellectual 713960Sodanrc@yahoo.com.br * property including but not limited to intellectual property relating 813960Sodanrc@yahoo.com.br * to a hardware implementation of the functionality of the software 913960Sodanrc@yahoo.com.br * licensed hereunder. You may use the software subject to the license 1013960Sodanrc@yahoo.com.br * terms below provided that you ensure that this notice is replicated 1113960Sodanrc@yahoo.com.br * unmodified and in its entirety in all distributions of the software, 1213960Sodanrc@yahoo.com.br * modified or unmodified, in source code or in binary form. 1313960Sodanrc@yahoo.com.br * 1413960Sodanrc@yahoo.com.br * Copyright (c) 2005-2006 The Regents of The University of Michigan 1513960Sodanrc@yahoo.com.br * All rights reserved. 1613960Sodanrc@yahoo.com.br * 1713960Sodanrc@yahoo.com.br * Redistribution and use in source and binary forms, with or without 1813960Sodanrc@yahoo.com.br * modification, are permitted provided that the following conditions are 1913960Sodanrc@yahoo.com.br * met: redistributions of source code must retain the above copyright 2013960Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer; 2113960Sodanrc@yahoo.com.br * redistributions in binary form must reproduce the above copyright 2213960Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer in the 2313960Sodanrc@yahoo.com.br * documentation and/or other materials provided with the distribution; 2413960Sodanrc@yahoo.com.br * neither the name of the copyright holders nor the names of its 2513960Sodanrc@yahoo.com.br * contributors may be used to endorse or promote products derived from 2613960Sodanrc@yahoo.com.br * this software without specific prior written permission. 2713960Sodanrc@yahoo.com.br * 2813960Sodanrc@yahoo.com.br * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2913960Sodanrc@yahoo.com.br * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3013960Sodanrc@yahoo.com.br * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 3113960Sodanrc@yahoo.com.br * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3213960Sodanrc@yahoo.com.br * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3313960Sodanrc@yahoo.com.br * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3413960Sodanrc@yahoo.com.br * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3513960Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3613960Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3713960Sodanrc@yahoo.com.br * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3813960Sodanrc@yahoo.com.br * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3913960Sodanrc@yahoo.com.br * 4013960Sodanrc@yahoo.com.br * Authors: Kevin Lim 4113960Sodanrc@yahoo.com.br * Daniel Carvalho 4213960Sodanrc@yahoo.com.br */ 4313960Sodanrc@yahoo.com.br 4413960Sodanrc@yahoo.com.br#ifndef __BASE_SAT_COUNTER_HH__ 4513960Sodanrc@yahoo.com.br#define __BASE_SAT_COUNTER_HH__ 4613960Sodanrc@yahoo.com.br 4713960Sodanrc@yahoo.com.br#include <cstdint> 4813960Sodanrc@yahoo.com.br 4913960Sodanrc@yahoo.com.br#include "base/logging.hh" 5013960Sodanrc@yahoo.com.br#include "base/types.hh" 5113960Sodanrc@yahoo.com.br 5213960Sodanrc@yahoo.com.br/** 5313960Sodanrc@yahoo.com.br * Implements an n bit saturating counter and provides methods to 5413960Sodanrc@yahoo.com.br * increment, decrement, and read it. 5513960Sodanrc@yahoo.com.br */ 5613960Sodanrc@yahoo.com.brclass SatCounter 5713960Sodanrc@yahoo.com.br{ 5813960Sodanrc@yahoo.com.br public: 5913960Sodanrc@yahoo.com.br /** 6013960Sodanrc@yahoo.com.br * Constructor for the counter. The explicit keyword is used to make 6113960Sodanrc@yahoo.com.br * sure the user does not assign a number to the counter thinking it 6213960Sodanrc@yahoo.com.br * will be used as a counter value when it is in fact used as the number 6313960Sodanrc@yahoo.com.br * of bits. 6413960Sodanrc@yahoo.com.br * 6513960Sodanrc@yahoo.com.br * @param bits How many bits the counter will have. 6613960Sodanrc@yahoo.com.br * @param initial_val Starting value for the counter. 6713960Sodanrc@yahoo.com.br */ 6813960Sodanrc@yahoo.com.br explicit SatCounter(unsigned bits, uint8_t initial_val = 0) 6913960Sodanrc@yahoo.com.br : initialVal(initial_val), maxVal((1 << bits) - 1), 7013960Sodanrc@yahoo.com.br counter(initial_val) 7113960Sodanrc@yahoo.com.br { 7213960Sodanrc@yahoo.com.br fatal_if(bits > 8*sizeof(uint8_t), 7313960Sodanrc@yahoo.com.br "Number of bits exceeds counter size"); 7413960Sodanrc@yahoo.com.br fatal_if(initial_val > maxVal, 7513960Sodanrc@yahoo.com.br "Saturating counter's Initial value exceeds max value."); 7613960Sodanrc@yahoo.com.br } 7713960Sodanrc@yahoo.com.br 7813960Sodanrc@yahoo.com.br /** Pre-increment operator. */ 7913960Sodanrc@yahoo.com.br SatCounter& 8013960Sodanrc@yahoo.com.br operator++() 8113960Sodanrc@yahoo.com.br { 8213960Sodanrc@yahoo.com.br if (counter < maxVal) { 8313960Sodanrc@yahoo.com.br ++counter; 8413960Sodanrc@yahoo.com.br } 8513960Sodanrc@yahoo.com.br return *this; 8613960Sodanrc@yahoo.com.br } 8713960Sodanrc@yahoo.com.br 8813960Sodanrc@yahoo.com.br /** Post-increment operator. */ 8913960Sodanrc@yahoo.com.br SatCounter 9013960Sodanrc@yahoo.com.br operator++(int) 9113960Sodanrc@yahoo.com.br { 9213960Sodanrc@yahoo.com.br SatCounter old_counter = *this; 9313960Sodanrc@yahoo.com.br ++*this; 9413960Sodanrc@yahoo.com.br return old_counter; 9513960Sodanrc@yahoo.com.br } 9613960Sodanrc@yahoo.com.br 9713960Sodanrc@yahoo.com.br /** Pre-decrement operator. */ 9813960Sodanrc@yahoo.com.br SatCounter& 9913960Sodanrc@yahoo.com.br operator--() 10013960Sodanrc@yahoo.com.br { 10113960Sodanrc@yahoo.com.br if (counter > 0) { 10213960Sodanrc@yahoo.com.br --counter; 10313960Sodanrc@yahoo.com.br } 10413960Sodanrc@yahoo.com.br return *this; 10513960Sodanrc@yahoo.com.br } 10613960Sodanrc@yahoo.com.br 10713960Sodanrc@yahoo.com.br /** Post-decrement operator. */ 10813960Sodanrc@yahoo.com.br SatCounter 10913960Sodanrc@yahoo.com.br operator--(int) 11013960Sodanrc@yahoo.com.br { 11113960Sodanrc@yahoo.com.br SatCounter old_counter = *this; 11213960Sodanrc@yahoo.com.br --*this; 11313960Sodanrc@yahoo.com.br return old_counter; 11413960Sodanrc@yahoo.com.br } 11513960Sodanrc@yahoo.com.br 11613960Sodanrc@yahoo.com.br /** 11713960Sodanrc@yahoo.com.br * Read the counter's value. 11813960Sodanrc@yahoo.com.br */ 11913960Sodanrc@yahoo.com.br operator uint8_t() const { return counter; } 12013960Sodanrc@yahoo.com.br 12113960Sodanrc@yahoo.com.br /** Reset the counter to its initial value. */ 12213960Sodanrc@yahoo.com.br void reset() { counter = initialVal; } 12313960Sodanrc@yahoo.com.br 12413960Sodanrc@yahoo.com.br private: 12513960Sodanrc@yahoo.com.br uint8_t initialVal; 12613960Sodanrc@yahoo.com.br uint8_t maxVal; 12713960Sodanrc@yahoo.com.br uint8_t counter; 12813960Sodanrc@yahoo.com.br}; 12913960Sodanrc@yahoo.com.br 13013960Sodanrc@yahoo.com.br#endif // __BASE_SAT_COUNTER_HH__ 131