base: Add function to saturate SatCounter
authorDaniel R. Carvalho <odanrc@yahoo.com.br>
Wed, 29 May 2019 19:15:06 +0000 (21:15 +0200)
committerDaniel Carvalho <odanrc@yahoo.com.br>
Thu, 29 Aug 2019 20:19:05 +0000 (20:19 +0000)
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>
src/base/sat_counter.hh
src/base/sat_counter.test.cc

index 3de9486db28e4690f2343a1b199e82228ce9082f..6aeca681d6da1de3cb75b349530c5aa0518837f2 100644 (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;
index dbdaf0ae674351b8b78bb5c73e3c83e30800932c..817f2c7ac5887ddb8580f84cda2c5316c7aa68df 100644 (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.
  */