From c1c01b418f67534c659b22a84cb845d41ec1b4bc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Jul 2026 05:14:52 +0000 Subject: [PATCH] Refine MSVC ARM64 multiplication overflow fallback --- src/util/mpz.cpp | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/util/mpz.cpp b/src/util/mpz.cpp index f2e6f20604..00fecdc59c 100644 --- a/src/util/mpz.cpp +++ b/src/util/mpz.cpp @@ -38,25 +38,17 @@ static bool mul_overflows(int64_t a, int64_t b, int64_t & result) { // Overflow if high bits are not the sign extension of result return high != (result >> 63); #elif defined(_MSC_VER) - if (a > 0) { - if (b > 0) { - if (a > INT64_MAX / b) - return true; - } - else if (b < INT64_MIN / a) { - return true; - } - } - else if (a < 0) { - if (b > 0) { - if (a < INT64_MIN / b) - return true; - } - else if (b < 0 && b < INT64_MAX / a) { - return true; - } - } - result = a * b; + uint64_t x = a < 0 ? static_cast(-(a + 1)) + 1 : static_cast(a); + uint64_t y = b < 0 ? static_cast(-(b + 1)) + 1 : static_cast(b); + bool is_neg = (a < 0) != (b < 0); + uint64_t limit = is_neg ? (uint64_t{1} << 63) : static_cast(INT64_MAX); + if (x != 0 && y > limit / x) + return true; + uint64_t magnitude = x * y; + if (is_neg) + result = magnitude == (uint64_t{1} << 63) ? INT64_MIN : -static_cast(magnitude); + else + result = static_cast(magnitude); return false; #else static_assert(false);