GMP 6.3.0: cover mpf_div_ui fraction generation Exercise the same inputs at large precisions and check exact limb patterns around the fraction threshold, with both signs and aliasing. Apply after 0001 with patch -p1. For a release tarball, also apply tests-mpf-Makefile.in.patch before configure so make check runs the test. --- /dev/null +++ b/tests/mpf/t-div_ui.c @@ -0,0 +1,309 @@ +/* Test mpf_div_ui. + +Copyright 2026 Free Software Foundation, Inc. + +This file is part of the GNU MP Library test suite. + +The GNU MP Library test suite is free software; you can redistribute it +and/or modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 3 of the License, +or (at your option) any later version. + +The GNU MP Library test suite is distributed in the hope that it will be +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +Public License for more details. + +You should have received a copy of the GNU General Public License along with +the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */ + +#include +#include +#include "gmp-impl.h" +#include "tests.h" + + +/* mpf_div_ui makes the low limbs of a quotient differently when the + dividend is much shorter than the precision of the result. The sizes + here go well past that point on both sides. */ + +#ifndef SWEEP_PREC +#define SWEEP_PREC 300 +#endif + +static const unsigned long vdata[] = { + 1, 2, 3, 5, 7, 10, 255, 256, 257, 641, 1000, 65535, 65536, 65537, + ULONG_HIGHBIT - 1, ULONG_HIGHBIT, ULONG_HIGHBIT + 1, + ULONG_MAX / 3, ULONG_MAX / 3 + 1, ULONG_MAX - 2, ULONG_MAX - 1, ULONG_MAX, +}; + +void +check_one (const char *desc, mpf_ptr got, mpf_srcptr u, unsigned long v) +{ + mpf_t vf; + mp_limb_t vlimbs[2]; + mp_size_t vsize; + + vlimbs[0] = v & GMP_NUMB_MASK; + vsize = (v != 0); +#if BITS_PER_ULONG > GMP_NUMB_BITS + vlimbs[1] = v >> GMP_NUMB_BITS; + vsize += (vlimbs[1] != 0); +#endif + PTR(vf) = vlimbs; + SIZ(vf) = vsize; + EXP(vf) = vsize; + + if (! refmpf_validate_division ("mpf_div_ui", got, u, vf)) + { + mp_trace_base = -16; + mpf_trace (" u", u); + printf (" v 0x%lX (%lu)\n", v, v); + printf (" %s\n", desc); + abort (); + } +} + +/* Short dividends at every precision up to SWEEP_PREC, so every length of + fraction part is seen. */ +void +check_sizes (void) +{ + unsigned long min_prec = __GMPF_BITS_TO_PREC (1); + gmp_randstate_ptr rands = RANDS; + unsigned long prec, v; + mpf_t got, u; + size_t i; + + mpf_init (got); + mpf_init (u); + refmpf_set_prec_limbs (u, 3L); + + for (prec = min_prec; prec <= SWEEP_PREC; prec++) + { + refmpf_set_prec_limbs (got, prec); + + for (i = 0; i < numberof (vdata); i++) + { + v = vdata[i]; + + /* 1/v */ + mpf_set_ui (u, 1L); + mpf_div_ui (got, u, v); + check_one ("1/v", got, u, v); + + /* (v-1)/v, the largest remainder */ + if (v > 1) + { + mpf_set_ui (u, v - 1); + mpf_div_ui (got, u, v); + check_one ("(v-1)/v", got, u, v); + } + + /* -v/v, an exact quotient */ + mpf_set_ui (u, v); + mpf_neg (u, u); + mpf_div_ui (got, u, v); + check_one ("-v/v", got, u, v); + } + + /* one, two or three random limbs over a random v */ + do { + v = gmp_urandomb_ui (rands, gmp_urandomm_ui (rands, BITS_PER_ULONG+1)); + } while (v == 0); + mpf_random2 (u, (mp_size_t) (1 + prec % 3), (mp_exp_t) 20); + mpf_div_ui (got, u, v); + check_one ("sizes, random", got, u, v); + } + + mpf_clear (got); + mpf_clear (u); +} + +void +check_rand (int reps) +{ + unsigned long min_prec = __GMPF_BITS_TO_PREC (1); + gmp_randstate_ptr rands = RANDS; + unsigned long prec, uprec, v; + mpf_t got, u; + int i; + + mpf_init (got); + mpf_init (u); + + for (i = 0; i < reps; i++) + { + /* got precision, now and then a long one */ + if ((i % 16) == 0) + prec = min_prec + gmp_urandomm_ui (rands, 2000L); + else + prec = min_prec + gmp_urandomm_ui (rands, 150L); + refmpf_set_prec_limbs (got, prec); + + /* u precision, mostly short against got, sometimes longer than it */ + if ((i % 4) == 0) + uprec = min_prec + gmp_urandomm_ui (rands, prec + 5); + else + uprec = min_prec + gmp_urandomm_ui (rands, 6L); + refmpf_set_prec_limbs (u, uprec); + + /* u, non-zero, possibly negative */ + do { + mpf_random2 (u, PREC(u), (mp_exp_t) 20); + } while (SIZ(u) == 0); + if (gmp_urandomb_ui (rands, 1L)) + mpf_neg (u, u); + + /* v, non-zero */ + do { + v = gmp_urandomb_ui (rands, gmp_urandomm_ui (rands, BITS_PER_ULONG+1)); + } while (v == 0); + + if ((i % 2) == 0) + { + /* src != dst */ + mpf_div_ui (got, u, v); + check_one ("separate", got, u, v); + } + else + { + /* src == dst */ + prec = refmpf_set_overlap (got, u); + mpf_div_ui (got, got, v); + check_one ("overlap src==dst", got, u, v); + + mpf_set_prec_raw (got, prec); + } + } + + mpf_clear (got); + mpf_clear (u); +} + +void +check_various (void) +{ + mpf_t got, u; + + mpf_init (got); + mpf_init (u); + + /* 100/4 == 25 */ + mpf_set_prec (got, 20L); + mpf_set_ui (u, 100L); + mpf_div_ui (got, u, 4L); + MPF_CHECK_FORMAT (got); + ASSERT_ALWAYS (mpf_cmp_ui (got, 25L) == 0); + + /* 1/2^3 == 0.125, with a long run of zero limbs below it */ + refmpf_set_prec_limbs (got, 500L); + mpf_set_ui (u, 1L); + mpf_div_ui (got, u, 8L); + MPF_CHECK_FORMAT (got); + ASSERT_ALWAYS (mpf_cmp_d (got, 0.125) == 0); + + /* 1/3 is the same limb over and over, when a limb has an even number of + bits */ + if (GMP_NUMB_BITS % 2 == 0) + { + mp_size_t i; + + refmpf_set_prec_limbs (got, 500L); + mpf_set_ui (u, 1L); + mpf_div_ui (got, u, 3L); + MPF_CHECK_FORMAT (got); + ASSERT_ALWAYS (EXP(got) == 0); + ASSERT_ALWAYS (SIZ(got) == 500); + for (i = 0; i < 500; i++) + ASSERT_ALWAYS (PTR(got)[i] == GMP_NUMB_MAX / 3); + } + + mpf_clear (got); + mpf_clear (u); +} + +void +check_fixed_states (void) +{ +#if GMP_NUMB_BITS >= 3 + static const unsigned long divisors[] = { 8, 3 }; + static const unsigned long precs[] = { 23, 24, 25 }; + mpf_t got, u; + mp_limb_t limb; + mp_size_t n, j; + unsigned long v; + size_t vi, pi; + int neg, overlap; + + mpf_init (got); + mpf_init (u); + + for (vi = 0; vi < numberof (divisors); vi++) + { + v = divisors[vi]; + if (v == 3 && GMP_NUMB_BITS % 2 != 0) + continue; + + /* With B = 2^GMP_NUMB_BITS, B mod v is 0 for v=8 and 1 for + v=3. Use rem=v-1 > 1: filling in place must not overwrite + the reciprocal seed before multiplying it by rem. */ + limb = GMP_NUMB_MAX / v + (v == 8); + limb *= v - 1; + + for (pi = 0; pi < numberof (precs); pi++) + { + n = precs[pi]; + refmpf_set_prec_limbs (got, precs[pi]); + for (neg = 0; neg <= 1; neg++) + for (overlap = 0; overlap <= 1; overlap++) + { + mpf_set_ui (u, v - 1); + if (neg) + mpf_neg (u, u); + + /* A one-limb dividend gives exactly PREC(got) + fractional limbs, straddling the default threshold 24. */ + ASSERT_ALWAYS (ABS (SIZ(u)) == 1); + ASSERT_ALWAYS (PREC(got) == n); + if (overlap) + { + mpf_set (got, u); + mpf_div_ui (got, got, v); + } + else + mpf_div_ui (got, u, v); + + MPF_CHECK_FORMAT (got); + check_one (overlap ? "fixed state, overlap" : "fixed state", + got, u, v); + ASSERT_ALWAYS (EXP(got) == 0); + ASSERT_ALWAYS (SIZ(got) == (neg ? -n : n)); + for (j = 0; j < n; j++) + ASSERT_ALWAYS (PTR(got)[j] + == (v == 3 || j == n - 1 ? limb : 0)); + } + } + } + + mpf_clear (got); + mpf_clear (u); +#endif +} + +int +main (int argc, char **argv) +{ + int reps = 2000; + + tests_start (); + TESTS_REPS (reps, argv, argc); + + check_various (); + check_fixed_states (); + check_sizes (); + check_rand (reps); + + tests_end (); + exit (0); +} --- a/tests/mpf/Makefile.am +++ b/tests/mpf/Makefile.am @@ -23,7 +23,7 @@ LDADD = $(top_builddir)/tests/libtests.la $(top_builddir)/libgmp.la check_PROGRAMS = t-dm2exp t-conv t-add t-sub t-sqrt t-sqrt_ui t-muldiv reuse \ - t-cmp_d t-cmp_si t-div t-fits t-get_d t-get_d_2exp \ + t-cmp_d t-cmp_si t-div t-div_ui t-fits t-get_d t-get_d_2exp \ t-get_si t-get_ui t-gsprec t-inp_str t-int_p t-mul_ui \ t-set t-set_q t-set_si t-set_ui t-trunc t-ui_div t-eq t-pow_ui TESTS = $(check_PROGRAMS)