From e3cfa564452bfc8d099f000a4a8f90d2ce3a3425 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Jul 2026 05:39:38 +0000 Subject: [PATCH] Polish MSVC overflow fallback implementation --- src/util/mpz.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util/mpz.cpp b/src/util/mpz.cpp index 38beee43a5..f26a0f98b4 100644 --- a/src/util/mpz.cpp +++ b/src/util/mpz.cpp @@ -39,11 +39,11 @@ static bool mul_overflows(int64_t a, int64_t b, int64_t & result) { __int64 high; result = _mul128(a, b, &high); // Overflow if high bits are not the sign extension of result - return high != (result < 0 ? -1 : 0); + return high != (result >> 63); #elif defined(_MSC_VER) - // `-(v + 1) + 1` computes |v| even for INT64_MIN, where direct negation would overflow. - 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); + // Unsigned negation preserves the magnitude of INT64_MIN without signed overflow. + uint64_t x = a < 0 ? -static_cast(a) : static_cast(a); + uint64_t y = b < 0 ? -static_cast(b) : 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) @@ -2027,7 +2027,7 @@ void mpz_manager::mul2k(mpz & a, unsigned k) { return; int64_t result; - if (is_small(a) && k < 63 && !mul_overflows(a.value(), 1ULL << k, result)) { + if (is_small(a) && k < 63 && !mul_overflows(a.value(), static_cast(1ULL << k), result)) { set(a, result); TRACE(mpz_mul2k, tout << "mul2k result:\n" << to_string(a) << '\n';); return;