base: Fix CircularQueue's operator-= when negative subtraction
authorGiacomo Travaglini <giacomo.travaglini@arm.com>
Thu, 21 Mar 2019 15:41:04 +0000 (15:41 +0000)
committerGiacomo Travaglini <giacomo.travaglini@arm.com>
Fri, 22 Mar 2019 10:01:10 +0000 (10:01 +0000)
Using operator-= when the rhs is a negative value is equivalent
to using += on -rhs. This is fixing rounding in that scenario.

Change-Id: Ia22e51f81a6805d27fd6b2115d288bb23421d00f
Signed-off-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/17528
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>

src/base/circular_queue.hh
src/base/circular_queue.test.cc

index f368ed0d1efe4d51220e1466f5c7fea5c0ddbbf9..da3da8b28b2569abc7f371573e1c4bc949ceb133 100644 (file)
@@ -323,12 +323,12 @@ class CircularQueue : private std::vector<T>
             assert(_cq);
 
             /* C does not do euclidean division, so we have to adjust */
-            if (t >= 0)
+            if (t >= 0) {
                 _round += (-t + _idx) / _cq->capacity();
-            else
-                _round += (-t + _idx - _cq->capacity() + 1) / _cq->capacity();
-
-            _idx = _cq->moduloSub(_idx, t);
+                _idx = _cq->moduloSub(_idx, t);
+            } else {
+                *this += -t;
+            }
             return *this;
         }
 
index cce6cb0b6eae1779aad6706d07206b687cf93ad8..fd63f7203e56d67d3b883bf1618009a409995062 100644 (file)
@@ -198,8 +198,10 @@ TEST(CircularQueueTest, IteratorsOp)
     cq.push_back(first_value);
     cq.push_back(second_value);
 
+    auto negative_offset = -(cq_size + 1);
     auto it_1 = cq.begin();
     auto it_2 = cq.begin() + 1;
+    auto it_3 = cq.begin() - negative_offset;
 
     // Operators test
     ASSERT_TRUE(it_1 != it_2);
@@ -213,6 +215,7 @@ TEST(CircularQueueTest, IteratorsOp)
     ASSERT_EQ(it_1, it_2 - 1);
     ASSERT_EQ(it_2 - it_1, 1);
     ASSERT_EQ(it_1 - it_2, -1);
+    ASSERT_EQ(it_3._round, 1);
 
     auto temp_it = it_1;
     ASSERT_EQ(++temp_it, it_2);