Skip to content

Upgrade internal Lehmer GCD to 2-digit (64-bit) approximation#8877

Closed
Copilot wants to merge 7 commits intomasterfrom
copilot/add-gcd-algorithms-multivariate-polynomials
Closed

Upgrade internal Lehmer GCD to 2-digit (64-bit) approximation#8877
Copilot wants to merge 7 commits intomasterfrom
copilot/add-gcd-algorithms-multivariate-polynomials

Conversation

Copy link
Contributor

Copilot AI commented Mar 5, 2026

Z3's internal big-integer GCD (used when building without libgmp) used a 1-digit (32-bit) Lehmer approximation, limiting each outer matrix application to ~30 simulated Euclidean steps. This upgrades to 2-digit (64-bit) approximations, roughly doubling the simulation depth per iteration and halving the number of expensive big-integer operations.

Changes (src/util/mpz.cpp)

  • 2-digit approximation: Build â/ from the top two 32-bit digits combined into a 62-bit int64_t (shifted right by 2 to preserve sign bit headroom), vs. the previous single leading digit:

    // Before: 32-bit approximation
    a_hat = a1.m_ptr->m_digits[a_sz - 1];
    
    // After: 62-bit approximation from top two digits
    uint64_t ah = (uint64_t)a1.m_ptr->m_digits[a_sz - 1];
    uint64_t al = (a_sz >= 2) ? (uint64_t)a1.m_ptr->m_digits[a_sz - 2] : 0;
    a_hat = (int64_t)(((ah << 32) | al) >> 2);
  • Overflow safety via __int128: Cofactor products (q·C, q·D) can overflow int64_t with 62-bit approximations. Uses __int128 arithmetic with an explicit overflow check on GCC/Clang; exits the inner loop safely if overflow is detected.

  • b_sz == a_sz - 1 alignment: When b1 has one fewer digit than a1, its leading digit is placed in the lower 32 bits of the 64-bit window to maintain correct relative scale.

  • <= 0 break condition: The denominator check is upgraded from == 0 to <= 0; with signed 64-bit cofactors, a negative denominator is equally invalid.

  • 1-digit fallback: The original 32-bit path is preserved under #else for MSVC and other compilers without __int128.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits March 6, 2026 00:42
… tests

Co-authored-by: levnach <5377127+levnach@users.noreply.github.com>
… tests

Co-authored-by: levnach <5377127+levnach@users.noreply.github.com>
Copilot AI changed the title [WIP] Add GCD algorithms for multivariate polynomials in Z3 Add Heuristic GCD (GCDheu) fast-path for multivariate polynomial GCD Mar 6, 2026
@levnach
Copy link
Contributor

levnach commented Mar 6, 2026

According to the test at http://mtzguido.tplinkdns.com:8081/z3/compare_stats.html the new algorithm does not improve, but keep performance the same. Try to improve performance by improving the implementation of the new GCD algorithm, or try another method.

…limit

Co-authored-by: levnach <5377127+levnach@users.noreply.github.com>
Copilot AI changed the title Add Heuristic GCD (GCDheu) fast-path for multivariate polynomial GCD Fix GCDheu performance: use L∞ norm for evaluation base xi Mar 6, 2026
Copilot AI and others added 2 commits March 7, 2026 00:00
…overflow safety

Co-authored-by: levnach <5377127+levnach@users.noreply.github.com>
…ition

Co-authored-by: levnach <5377127+levnach@users.noreply.github.com>
Copilot AI changed the title Fix GCDheu performance: use L∞ norm for evaluation base xi Improve GCD performance: 2-digit Lehmer integer GCD + corrected GCDheu polynomial evaluation base Mar 7, 2026
…D improvement

Co-authored-by: levnach <5377127+levnach@users.noreply.github.com>
Copilot AI changed the title Improve GCD performance: 2-digit Lehmer integer GCD + corrected GCDheu polynomial evaluation base Upgrade internal Lehmer GCD to 2-digit (64-bit) approximation Mar 7, 2026
@levnach levnach closed this Mar 7, 2026
@levnach levnach deleted the copilot/add-gcd-algorithms-multivariate-polynomials branch March 7, 2026 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants