From c975ee8e488908e63a23b72a67a588b2d81836ee Mon Sep 17 00:00:00 2001 From: lkcl Date: Tue, 26 Apr 2022 14:10:47 +0100 Subject: [PATCH] --- openpower/sv/biginteger/analysis.mdwn | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/openpower/sv/biginteger/analysis.mdwn b/openpower/sv/biginteger/analysis.mdwn index dc9d4ac87..a29aa6a6b 100644 --- a/openpower/sv/biginteger/analysis.mdwn +++ b/openpower/sv/biginteger/analysis.mdwn @@ -320,15 +320,17 @@ the digits are 32 bit and, special-casing the overflow, a 64/32 divide is suffic ``` // Compute estimate qhat of q[j] from top 2 digits uint64_t dig2 = ((uint64_t)un[j + n] << 32) | un[j + n - 1]; - qhat = dig2 / vn[n - 1]; // 64/32 divide - rhat = dig2 % vn[n - 1]; // 64/32 modulo - again: + if (un[j+n] >= vn[n-1]) { + // rhat can be bigger than 32-bit when the division overflows, + rhat = dig2 - UINT32_MAX * vn[n - 1]; + } else { + qhat = dig2 / vn[n - 1]; // 64/32 divide + rhat = dig2 % vn[n - 1]; // 64/32 modulo + } // use 3rd-from-top digit to obtain better accuracy - if (qhat >= b || qhat * vn[n - 2] > b * rhat + un[j + n - 2]) { + while (rhat < b || qhat * vn[n - 2] > b * rhat + un[j + n - 2]) { qhat = qhat - 1; rhat = rhat + vn[n - 1]; - if (rhat < b) - goto again; } ``` -- 2.30.2