Daniel Carvalho has submitted this change. (
https://gem5-review.googlesource.com/c/public/gem5/+/23664 )
Change subject: base: Fix negative op-assign of SatCounter
..
base: Fix negative op-assign of SatCounter
The value of the add and subtract assignment operations can be negative,
and this was not being handled properly previously. Regarding shift
assignment, the standard says it is undefined behaviour if a negative
number is given, so add assertions for these cases.
Change-Id: I2f1e4143c6385caa80fb25f84ca8edb0ca7e62b7
Signed-off-by: Daniel R. Carvalho
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/23664
Reviewed-by: Bobby R. Bruce
Reviewed-by: Jason Lowe-Power
Maintainer: Jason Lowe-Power
Tested-by: kokoro
---
M src/base/sat_counter.hh
M src/base/sat_counter.test.cc
2 files changed, 56 insertions(+), 6 deletions(-)
Approvals:
Jason Lowe-Power: Looks good to me, approved; Looks good to me, approved
Bobby R. Bruce: Looks good to me, but someone else must approve
kokoro: Regressions pass
diff --git a/src/base/sat_counter.hh b/src/base/sat_counter.hh
index 6aeca68..946da4f 100644
--- a/src/base/sat_counter.hh
+++ b/src/base/sat_counter.hh
@@ -44,6 +44,7 @@
#ifndef __BASE_SAT_COUNTER_HH__
#define __BASE_SAT_COUNTER_HH__
+#include
#include
#include "base/logging.hh"
@@ -172,6 +173,7 @@
SatCounter&
operator>>=(const int& shift)
{
+assert(shift >= 0);
this->counter >>= shift;
return *this;
}
@@ -180,6 +182,7 @@
SatCounter&
operator<<=(const int& shift)
{
+assert(shift >= 0);
this->counter <<= shift;
if (this->counter > maxVal) {
this->counter = maxVal;
@@ -191,10 +194,14 @@
SatCounter&
operator+=(const int& value)
{
-if (maxVal - this->counter >= value) {
-this->counter += value;
+if (value >= 0) {
+if (maxVal - this->counter >= value) {
+this->counter += value;
+} else {
+this->counter = maxVal;
+}
} else {
-this->counter = maxVal;
+*this -= -value;
}
return *this;
}
@@ -203,10 +210,14 @@
SatCounter&
operator-=(const int& value)
{
-if (this->counter > value) {
-this->counter -= value;
+if (value >= 0) {
+if (this->counter > value) {
+this->counter -= value;
+} else {
+this->counter = 0;
+}
} else {
-this->counter = 0;
+*this += -value;
}
return *this;
}
diff --git a/src/base/sat_counter.test.cc b/src/base/sat_counter.test.cc
index 817f2c7..d9a377f 100644
--- a/src/base/sat_counter.test.cc
+++ b/src/base/sat_counter.test.cc
@@ -28,6 +28,7 @@
* Authors: Daniel Carvalho
*/
+#include
#include
#include
@@ -184,6 +185,11 @@
ASSERT_EQ(counter, value);
counter >>= saturated_counter;
ASSERT_EQ(counter, 0);
+
+// Make sure the counters cannot be shifted by negative numbers, since
+// that is undefined behaviour
+ASSERT_DEATH(counter >>= -1, "");
+ASSERT_DEATH(counter <<= -1, "");
}
/**
@@ -319,3 +325,36 @@
ASSERT_EQ(counter, 0);
}
+/**
+ * Test add-assignment and subtract assignment using negative numbers.
+ */
+TEST(SatCounterTest, NegativeAddSubAssignment)
+{
+const unsigned bits = 3;
+const unsigned max_value = (1 << bits) - 1;
+SatCounter counter(bits, max_value);
+int value = max_value;
+
+// Test add-assignment for a few negative values until zero is reached
+counter += -2;
+value += -2;
+ASSERT_EQ(counter, value);
+counter += -3;
+value += -3;
+ASSERT_EQ(counter, value);
+counter += (int)-max_value;
+value = 0;
+ASSERT_EQ(counter, value);
+
+// Test subtract-assignment for a few negative values until saturation
+counter -= -2;
+value -= -2;
+ASSERT_EQ(counter, value);
+counter -= -3;
+value -= -3;
+ASSERT_EQ(counter, value);
+counter -= (int)-max_value;
+value = max_value;
+ASSERT_EQ(counter, value);
+}
+
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/23664
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings
Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: I2f1e4143c6385caa80fb25f84ca8edb0ca7e62b7
Gerrit-Change-Number: 23664
Gerrit-PatchSet: 2
Gerrit-Owner: Daniel Carvalho
Gerrit-Reviewer: Bobby R. Bruce
Gerrit-Reviewer: Daniel Carvalho
Gerrit-Reviewer: Jason Lowe-Power
Gerrit-Reviewer: kokoro
Gerrit-MessageType: merged
___
gem5-dev mailing list
gem5-dev@gem5.org
http://m5sim.org/mailman/listinfo/gem5-dev