From 4e484ca33e7d751a76b00f4c27f55fd589962607 Mon Sep 17 00:00:00 2001 From: Hoa Nguyen Date: Thu, 28 May 2020 17:20:06 -0700 Subject: [PATCH] python: Fix m5's tick rounding mechanism MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Partially reverts: https://gem5-review.googlesource.com/c/public/gem5/+/29372 where the `math.ceil` function was used to fix an issue where 0.5 was rounded to zero in python3. This has the side effect of giving incorrect clock frequences due to rounding errors. To demonstrate: ``` >>> import math >>> 1e-9*1000000000000 1000.0000000000001 >>> int(math.ceil(1e-9*1000000000000)) 1001 ``` The solution to this problem is to use Python's `decimal` module to round values. Issue-on: https://gem5.atlassian.net/browse/GEM5-616 Signed-off-by: Hoa Nguyen Change-Id: Ic76736ccb4b6b8c037103a34493aff7d9731d314 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/29577 Reviewed-by: Tiago Mück Maintainer: Jason Lowe-Power Tested-by: kokoro --- src/python/m5/ticks.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/python/m5/ticks.py b/src/python/m5/ticks.py index 047116ac7..cdba1e86a 100644 --- a/src/python/m5/ticks.py +++ b/src/python/m5/ticks.py @@ -25,7 +25,7 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from __future__ import print_function -from math import ceil +import decimal import six if six.PY3: long = int @@ -74,7 +74,8 @@ def fromSeconds(value): # convert the value from time to ticks value *= _m5.core.getClockFrequency() - int_value = int(ceil(value)) + int_value = int( + decimal.Decimal(value).to_integral_value( decimal.ROUND_HALF_UP)) err = (value - int_value) / value if err > frequency_tolerance: warn("rounding error > tolerance\n %f rounded to %d", value, -- 2.30.2