base: Add function to saturate SatCounter

Create a saturation function for the SatCounter that makes its
internal counter reach its maximum value.

Change-Id: Ibd30fa27c4ae2714dd48e3eba85addd035fb737c
Signed-off-by: Daniel R. Carvalho <odanrc@yahoo.com.br>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/20451
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Daniel R. Carvalho
2019-05-29 21:15:06 +02:00
committed by Daniel Carvalho
parent 51d38a4b79
commit fc528bb0c0
2 changed files with 29 additions and 0 deletions

View File

@@ -235,6 +235,18 @@ class SatCounter
*/
bool isSaturated() const { return counter == maxVal; }
/**
* Saturate the counter.
*
* @return The value added to the counter to reach saturation.
*/
uint8_t saturate()
{
const uint8_t diff = maxVal - counter;
counter = maxVal;
return diff;
}
private:
uint8_t initialVal;
uint8_t maxVal;

View File

@@ -96,6 +96,23 @@ TEST(SatCounterTest, SaturationPercentile)
ASSERT_TRUE(counter.isSaturated());
}
/**
* Test abrupt saturation.
*/
TEST(SatCounterTest, Saturate)
{
const unsigned bits = 3;
const unsigned max_value = (1 << bits) - 1;
SatCounter counter(bits);
counter++;
ASSERT_FALSE(counter.isSaturated());
// Make sure the value added is what was missing to saturate
const unsigned diff = counter.saturate();
ASSERT_EQ(diff, max_value - 1);
ASSERT_TRUE(counter.isSaturated());
}
/**
* Test back and forth against an int.
*/