Skip to content

Instantly share code, notes, and snippets.

@mikedotexe
Created November 25, 2026 19:50
Show Gist options
  • Select an option

  • Save mikedotexe/fd5aceeedaf0ef4cb9fc0379fea8a30d to your computer and use it in GitHub Desktop.

Select an option

Save mikedotexe/fd5aceeedaf0ef4cb9fc0379fea8a30d to your computer and use it in GitHub Desktop.
GMP qxn patch
--- a/mpf/div_ui.c
+++ b/mpf/div_ui.c
@@ -36,9 +36,9 @@
mpf_div_ui (mpf_ptr r, mpf_srcptr u, unsigned long int v)
{
mp_srcptr up;
- mp_ptr rp, tp, rtp;
+ mp_ptr rp;
mp_size_t usize;
- mp_size_t rsize, tsize;
+ mp_size_t rsize, tsize, fn;
mp_size_t sign_quotient;
mp_size_t prec;
mp_limb_t q_limb;
@@ -82,24 +82,25 @@
up = u->_mp_d;
tsize = 1 + prec;
- tp = TMP_ALLOC_LIMBS (tsize + 1);
-
if (usize > tsize)
{
up += usize - tsize;
usize = tsize;
- rtp = tp;
}
- else
+ fn = tsize - usize;
+
+ /* When dividing in place, the quotient can overwrite unread source
+ limbs. Compare the original limb pointers before using the possibly
+ truncated input. Distinct mpf allocations need no temporary. */
+ if (rp == u->_mp_d)
{
- MPN_ZERO (tp, tsize - usize);
- rtp = tp + (tsize - usize);
+ mp_ptr tp = TMP_ALLOC_LIMBS (usize);
+ MPN_COPY (tp, up, usize);
+ up = tp;
}
- /* Move the dividend to the remainder. */
- MPN_COPY (rtp, up, usize);
-
- mpn_divmod_1 (rp, tp, tsize, (mp_limb_t) v);
+ /* Supply the fractional limbs through qxn without padding the source. */
+ mpn_divrem_1 (rp, fn, up, usize, (mp_limb_t) v);
q_limb = rp[tsize - 1];
rsize = tsize - (q_limb == 0);
--- a/tests/mpf/t-muldiv.c
+++ b/tests/mpf/t-muldiv.c
@@ -26,6 +26,90 @@
#ifndef SIZE
#define SIZE 16
#endif
+
+/* Check the signed size, exponent and every live quotient limb with a
+ disjoint or in-place dividend. Vary the input size independently of
+ the result precision, including truncation after mpf_set_prec_raw. */
+static void
+check_div_ui_limbs (void)
+{
+ static const unsigned long divisors[] = {
+ 1, 2, 3, 7, 8, 10, 16, 17,
+ 1UL << (MIN (BITS_PER_ULONG, GMP_NUMB_BITS) - 1), ULONG_MAX
+ };
+ static const unsigned int precs[] = { 2, 3, 4, 24 };
+ mpf_t u, got, want;
+ mpz_t n, q;
+ mp_size_t fn;
+ unsigned long v, restore;
+ int width, limb, i, j, alias;
+
+ mpf_init2 (u, 6 * GMP_NUMB_BITS);
+ mpf_init (got);
+ mpf_init (want);
+ mpz_init (n);
+ mpz_init (q);
+ for (width = 0; width <= 5; width++)
+ {
+ mpz_set_ui (n, 0);
+ for (limb = 0; limb < width; limb++)
+ {
+ mpz_mul_2exp (n, n, GMP_NUMB_BITS);
+ mpz_add_ui (n, n, 2 * limb + 3);
+ }
+ if (width % 2)
+ mpz_neg (n, n);
+ mpf_set_z (u, n);
+ if (width != 0)
+ EXP (u) -= 3;
+ for (i = 0; i < numberof (precs); i++)
+ for (j = 0; j < numberof (divisors); j++)
+ {
+ refmpf_set_prec_limbs (want, precs[i]);
+ v = divisors[j];
+#if BITS_PER_ULONG > GMP_NUMB_BITS
+ if (v > GMP_NUMB_MAX)
+ {
+ mpf_t vf;
+ mpf_init_set_ui (vf, v);
+ mpf_div (want, u, vf);
+ mpf_clear (vf);
+ }
+ else
+#endif
+ {
+ fn = PREC (want) + 1 - width;
+ if (fn < 0)
+ mpz_tdiv_q_2exp (q, n, -fn * GMP_NUMB_BITS);
+ else
+ mpz_mul_2exp (q, n, fn * GMP_NUMB_BITS);
+ mpz_tdiv_q_ui (q, q, v);
+ mpf_set_z (want, q);
+ if (SIZ (want))
+ EXP (want) += EXP (u) - (PREC (want) + 1);
+ }
+
+ for (alias = 0; alias <= 1; alias++)
+ {
+ refmpf_set_prec_limbs (got, precs[i]);
+ if (alias)
+ restore = refmpf_set_overlap (got, u);
+ mpf_div_ui (got, alias ? got : u, v);
+ ASSERT_ALWAYS (SIZ (got) == SIZ (want));
+ ASSERT_ALWAYS (EXP (got) == EXP (want));
+ ASSERT_ALWAYS (!SIZ (got) ||
+ mpn_cmp (PTR (got), PTR (want), ABSIZ (got)) == 0);
+ if (alias)
+ mpf_set_prec_raw (got, restore);
+ }
+ }
+ }
+ mpz_clear (q);
+ mpz_clear (n);
+ mpf_clear (want);
+ mpf_clear (got);
+ mpf_clear (u);
+}
int
main (int argc, char **argv)
@@ -42,6 +126,8 @@
tests_start ();
+ check_div_ui_limbs ();
+
if (argc > 1)
{
reps = strtol (argv[1], 0, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment