summaryrefslogtreecommitdiff
path: root/src/base/circular_queue.hh
diff options
context:
space:
mode:
authorGiacomo Travaglini <giacomo.travaglini@arm.com>2019-03-08 16:01:20 +0000
committerGiacomo Travaglini <giacomo.travaglini@arm.com>2019-03-22 10:01:10 +0000
commitbbcbde7a924ca1ca58df997cfe9f750c285e5564 (patch)
treeb185621387765b3749fc939bbc4112e7b7a9852a /src/base/circular_queue.hh
parent487ea069be405844e1fcf4aa1ed274b74f601c39 (diff)
downloadgem5-bbcbde7a924ca1ca58df997cfe9f750c285e5564.tar.xz
base: Fix CircularQueue when diffing iterators
This patch is fixing CircularQueue iterators' subtraction, in particular the behaviour when head and tail round multiple times. Change-Id: Ie79ac8accd30a10cf039cf4def87675b01375d6b Signed-off-by: Giacomo Travaglini <giacomo.travaglini@arm.com> Reviewed-by: Gabor Dozsa <gabor.dozsa@arm.com> Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/17188 Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br> Reviewed-by: Jason Lowe-Power <jason@lowepower.com> Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
Diffstat (limited to 'src/base/circular_queue.hh')
-rw-r--r--src/base/circular_queue.hh17
1 files changed, 13 insertions, 4 deletions
diff --git a/src/base/circular_queue.hh b/src/base/circular_queue.hh
index 5023661b9..f368ed0d1 100644
--- a/src/base/circular_queue.hh
+++ b/src/base/circular_queue.hh
@@ -106,10 +106,19 @@ class CircularQueue : private std::vector<T>
static uint32_t
moduloSub(uint32_t op1, uint32_t op2, uint32_t size)
{
- int ret = (uint32_t)(op1 - op2) % size;
+ int32_t ret = sub(op1, op2, size);
return ret >= 0 ? ret : ret + size;
}
+ static int32_t
+ sub(uint32_t op1, uint32_t op2, uint32_t size)
+ {
+ if (op1 > op2)
+ return (op1 - op2) % size;
+ else
+ return -((op2 - op1) % size);
+ }
+
void increase(uint32_t& v, size_t delta = 1)
{
v = moduloAdd(v, delta, _capacity);
@@ -355,10 +364,10 @@ class CircularQueue : private std::vector<T>
difference_type operator-(const iterator& that)
{
/* If a is already at the end, we can safely return 0. */
- auto ret = _cq->moduloSub(this->_idx, that._idx);
+ auto ret = _cq->sub(this->_idx, that._idx, _cq->capacity());
- if (ret == 0 && this->_round != that._round) {
- ret += this->_round * _cq->capacity();
+ if (this->_round != that._round) {
+ ret += ((this->_round - that._round) * _cq->capacity());
}
return ret;
}