From: Daniel R. Carvalho Date: Wed, 29 May 2019 19:15:06 +0000 (+0200) Subject: base: Add function to saturate SatCounter X-Git-Tag: v19.0.0.0~609 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=fc528bb0c0d2c6fe1ae55e9aa5be8652cc3fd456;p=gem5.git 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 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/20451 Reviewed-by: Andreas Sandberg Maintainer: Andreas Sandberg Tested-by: kokoro --- diff --git a/src/base/sat_counter.hh b/src/base/sat_counter.hh index 3de9486db..6aeca681d 100644 --- a/src/base/sat_counter.hh +++ b/src/base/sat_counter.hh @@ -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; diff --git a/src/base/sat_counter.test.cc b/src/base/sat_counter.test.cc index dbdaf0ae6..817f2c7ac 100644 --- a/src/base/sat_counter.test.cc +++ b/src/base/sat_counter.test.cc @@ -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. */