This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 84633ab4b3 [Arith] Use native GCD for fitting BigInt operands (#20380)
84633ab4b3 is described below

commit 84633ab4b3af0d93a42159cf9b6558df4a867962
Author: Tianqi Chen <[email protected]>
AuthorDate: Thu Sep 17 13:02:51 2026 -0400

    [Arith] Use native GCD for fitting BigInt operands (#20380)
    
    Use the existing native GCD implementation when both BigInt operands fit
    int64 and neither is INT64_MIN. Try native dispatch before BigInt sign
    normalization, preserving the original BigInt fallback for wider values
    and signed-minimum inputs.
---
 src/arith/int_operator.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/arith/int_operator.h b/src/arith/int_operator.h
index 139284e408..70ef74f321 100644
--- a/src/arith/int_operator.h
+++ b/src/arith/int_operator.h
@@ -156,6 +156,14 @@ inline int64_t ZeroAwareGCD(int64_t a, int64_t b) {
 }
 
 inline ffi::BigInt ZeroAwareGCD(ffi::BigInt a, ffi::BigInt b) {
+  auto a_int64 = a.as<int64_t>();
+  auto b_int64 = b.as<int64_t>();
+  // The native overload cannot negate INT64_MIN.
+  if (a_int64.has_value() && b_int64.has_value() &&
+      *a_int64 != std::numeric_limits<int64_t>::min() &&
+      *b_int64 != std::numeric_limits<int64_t>::min()) {
+    return ZeroAwareGCD(*a_int64, *b_int64);
+  }
   if (a < 0) a = -a;
   if (b < 0) b = -b;
   if (a < b) std::swap(a, b);

Reply via email to