From 35488ac254c992b4e7789828d469da3f18a2aa00 Mon Sep 17 00:00:00 2001 From: Cary R Date: Sun, 22 May 2011 21:35:45 -0700 Subject: [PATCH] Fix bug in queue average with a very large number of entries. Icarus is not actually fast enough to run into this issue in a reasonable amount of time. I discovered this by thinking about the algorithm and verified the fix with custom code. --- vpi/sys_queue.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vpi/sys_queue.c b/vpi/sys_queue.c index 13b35e220..f7312c0fc 100644 --- a/vpi/sys_queue.c +++ b/vpi/sys_queue.c @@ -84,9 +84,12 @@ uint64_t calc_average_wait_time(uint64_t high, uint64_t low, uint64_t total) /* It's a big value so calculate the average the long way. */ do { + unsigned carry = 0U; /* Copy bits from low to high until we have a bit to place * in the result or there are no bits left. */ - while ((bit >= 0) && (high < total)) { + while ((bit >= 0) && (high < total) && !carry) { + /* If the MSB is set then we will have a carry. */ + if (high > (UINT64_MAX >> 1)) carry = 1U; high <<= 1; high |= (low & 0x8000000000000000) != 0; low <<= 1;