From: Jacob Lifshay Date: Wed, 7 Oct 2020 02:36:11 +0000 (-0700) Subject: add overflow detection to DIVS X-Git-Tag: 24jan2021_ls180~11 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=9e5f483bedd63f79c647dd43d809621c4c9b99cf;p=nmutil.git add overflow detection to DIVS overflow shouldn't occur when calling DIVS since it should have been detected in the code calling DIVS to properly handle edge cases. --- diff --git a/src/nmutil/divmod.py b/src/nmutil/divmod.py index af5d214..357fdcb 100644 --- a/src/nmutil/divmod.py +++ b/src/nmutil/divmod.py @@ -17,6 +17,9 @@ def trunc_divs(n, d): res = copy(n) if (sign_n == sign_d): res.value = abs_q + if res.value & (1 << (res.bits - 1)) != 0: + # result should be positive but is negative + raise OverflowError() return res mask = (1 << res.bits) - 1 res.value = (-abs_q) & mask