base: Fix signature of SatCounter::saturate()

The variants that use more than 8 bits were broken,
since the size of the difference in those cases
could be larger than 8 bits, and the return value
was only 8-bits long.

Change-Id: I8b75be48f924cc33ebf5e5aeff6d4045fac66bcc
Signed-off-by: Daniel R. Carvalho <odanrc@yahoo.com.br>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/66791
Maintainer: Matt Sinclair <mattdsinclair@gmail.com>
Reviewed-by: Matt Sinclair <mattdsinclair@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Daniel R. Carvalho
2022-12-18 10:33:10 -03:00
committed by Daniel Carvalho
parent 06f18242fe
commit 7fb2fda841
2 changed files with 16 additions and 2 deletions

View File

@@ -318,9 +318,9 @@ class GenericSatCounter
*
* @ingroup api_sat_counter
*/
uint8_t saturate()
T saturate()
{
const uint8_t diff = maxVal - counter;
const T diff = maxVal - counter;
counter = maxVal;
return diff;
}

View File

@@ -149,6 +149,20 @@ TEST(SatCounterTest, Saturate)
ASSERT_TRUE(counter.isSaturated());
}
TEST(SatCounterTest, Saturate16)
{
const unsigned bits = 14;
const unsigned max_value = (1 << bits) - 1;
SatCounter16 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.
*/