@@ -2,24 +2,33 @@
3 Contributed to the GNU project by Niels Möller
3 Contributed to the GNU project by Niels Möller
5−Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
6−2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
7−Free Software Foundation, Inc.
5+Copyright 1991-1997, 1999-2016 Free Software Foundation, Inc.
9This file is part of the GNU MP Library.
7This file is part of the GNU MP Library.
11The GNU MP Library is free software; you can redistribute it and/or modify
9The GNU MP Library is free software; you can redistribute it and/or modify
10+it under the terms of either:
12−it under the terms of the GNU Lesser General Public License as published by
12+ * the GNU Lesser General Public License as published by the Free
13−the Free Software Foundation; either version 3 of the License, or (at your
13+ Software Foundation; either version 3 of the License, or (at your
14−option) any later version.
14+ option) any later version.
18+ * the GNU General Public License as published by the Free Software
19+ Foundation; either version 2 of the License, or (at your option) any
22+or both in parallel, as here.
16The GNU MP Library is distributed in the hope that it will be useful, but
24The GNU MP Library is distributed in the hope that it will be useful, but
17WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18−or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
26+or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19−License for more details.
27+for more details.
21−You should have received a copy of the GNU Lesser General Public License
29+You should have received copies of the GNU General Public License and the
22−along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
30+GNU Lesser General Public License along with the GNU MP Library. If not,
31+see https://www.gnu.org/licenses/. */
24/* NOTE: All functions in this file which are not declared in
33/* NOTE: All functions in this file which are not declared in
25 mini-gmp.h are internal, and are not intended to be compatible
34 mini-gmp.h are internal, and are not intended to be compatible
@@ -60,8 +69,10 @@ along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
60#define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
69#define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
61#define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
70#define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
72+#define GMP_CMP(a,b) (((a) > (b)) - ((a) < (b)))
63#define gmp_assert_nocarry(x) do { \
74#define gmp_assert_nocarry(x) do { \
64− mp_limb_t __cy = x; \
75+ mp_limb_t __cy = (x); \
65 assert (__cy == 0); \
76 assert (__cy == 0); \
66 } while (0)
77 } while (0)
@@ -227,6 +238,8 @@ along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
227 (y) = __mpz_srcptr_swap__tmp; \
238 (y) = __mpz_srcptr_swap__tmp; \
228 } while (0)
239 } while (0)
241+const int mp_bits_per_limb = GMP_LIMB_BITS;
231/* Memory allocation and other helper functions. */
244/* Memory allocation and other helper functions. */
232static void
245static void
@@ -253,12 +266,12 @@ gmp_default_alloc (size_t size)
253static void *
266static void *
254gmp_default_realloc (void *old, size_t old_size, size_t new_size)
267gmp_default_realloc (void *old, size_t old_size, size_t new_size)
256− mp_ptr p;
269+ void * p;
258 p = realloc (old, new_size);
271 p = realloc (old, new_size);
261− gmp_die("gmp_default_realoc: Virtual memory exhausted.");
274+ gmp_die("gmp_default_realloc: Virtual memory exhausted.");
263 return p;
276 return p;
@@ -311,14 +324,14 @@ mp_set_memory_functions (void *(*alloc_func) (size_t),
311static mp_ptr
324static mp_ptr
312gmp_xalloc_limbs (mp_size_t size)
325gmp_xalloc_limbs (mp_size_t size)
314− return gmp_xalloc (size * sizeof (mp_limb_t));
327+ return (mp_ptr) gmp_xalloc (size * sizeof (mp_limb_t));
317static mp_ptr
330static mp_ptr
318gmp_xrealloc_limbs (mp_ptr old, mp_size_t size)
331gmp_xrealloc_limbs (mp_ptr old, mp_size_t size)
320 assert (size > 0);
333 assert (size > 0);
321− return (*gmp_reallocate_func) (old, 0, size * sizeof (mp_limb_t));
334+ return (mp_ptr) (*gmp_reallocate_func) (old, 0, size * sizeof (mp_limb_t));
@@ -335,7 +348,7 @@ mpn_copyi (mp_ptr d, mp_srcptr s, mp_size_t n)
336mpn_copyd (mp_ptr d, mp_srcptr s, mp_size_t n)
349mpn_copyd (mp_ptr d, mp_srcptr s, mp_size_t n)
338− while (n-- > 0)
351+ while (--n >= 0)
339 d[n] = s[n];
352 d[n] = s[n];
@@ -362,12 +375,23 @@ mpn_cmp4 (mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
362static mp_size_t
375static mp_size_t
363mpn_normalized_size (mp_srcptr xp, mp_size_t n)
376mpn_normalized_size (mp_srcptr xp, mp_size_t n)
365− for (; n > 0 && xp[n-1] == 0; n--)
378+ while (n > 0 && xp[n-1] == 0)
367 return n;
380 return n;
384+mpn_zero_p(mp_srcptr rp, mp_size_t n)
370−#define mpn_zero_p(xp, n) (mpn_normalized_size ((xp), (n)) == 0)
386+ return mpn_normalized_size (rp, n) == 0;
390+mpn_zero (mp_ptr rp, mp_size_t n)
373mpn_add_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
397mpn_add_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
@@ -432,7 +456,7 @@ mpn_sub_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
433 mp_limb_t a = ap[i];
457 mp_limb_t a = ap[i];
434 /* Carry out */
458 /* Carry out */
435− mp_limb_t cy = a < b;;
459+ mp_limb_t cy = a < b;
436 rp[i] = a - b;
460 rp[i] = a - b;
@@ -558,17 +582,16 @@ mpn_mul (mp_ptr rp, mp_srcptr up, mp_size_t un, mp_srcptr vp, mp_size_t vn)
560 rp[un] = mpn_mul_1 (rp, up, un, vp[0]);
584 rp[un] = mpn_mul_1 (rp, up, un, vp[0]);
561− rp += 1, vp += 1, vn -= 1;
563 /* Now accumulate the product of up[] and the next higher limb from
586 /* Now accumulate the product of up[] and the next higher limb from
566− while (vn >= 1)
589+ while (--vn >= 1)
568 rp[un] = mpn_addmul_1 (rp, up, un, vp[0]);
592 rp[un] = mpn_addmul_1 (rp, up, un, vp[0]);
569− rp += 1, vp += 1, vn -= 1;
571− return rp[un - 1];
594+ return rp[un];
@@ -588,7 +611,6 @@ mpn_lshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
589 mp_limb_t high_limb, low_limb;
612 mp_limb_t high_limb, low_limb;
590 unsigned int tnc;
613 unsigned int tnc;
592 mp_limb_t retval;
614 mp_limb_t retval;
594 assert (n >= 1);
616 assert (n >= 1);
@@ -603,7 +625,7 @@ mpn_lshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
603 retval = low_limb >> tnc;
625 retval = low_limb >> tnc;
604 high_limb = (low_limb << cnt);
626 high_limb = (low_limb << cnt);
606− for (i = n; --i != 0;)
628+ while (--n != 0)
608 low_limb = *--up;
630 low_limb = *--up;
609 *--rp = high_limb | (low_limb >> tnc);
631 *--rp = high_limb | (low_limb >> tnc);
@@ -619,7 +641,6 @@ mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
620 mp_limb_t high_limb, low_limb;
642 mp_limb_t high_limb, low_limb;
621 unsigned int tnc;
643 unsigned int tnc;
623 mp_limb_t retval;
644 mp_limb_t retval;
625 assert (n >= 1);
646 assert (n >= 1);
@@ -631,7 +652,7 @@ mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
631 retval = (high_limb << tnc);
652 retval = (high_limb << tnc);
632 low_limb = high_limb >> cnt;
653 low_limb = high_limb >> cnt;
634− for (i = n; --i != 0;)
655+ while (--n != 0)
636 high_limb = *up++;
657 high_limb = *up++;
637 *rp++ = low_limb | (high_limb << tnc);
658 *rp++ = low_limb | (high_limb << tnc);
@@ -682,24 +703,68 @@ mpn_scan0 (mp_srcptr ptr, mp_bitcnt_t bit)
682 i, ptr, i, GMP_LIMB_MAX);
703 i, ptr, i, GMP_LIMB_MAX);
707+mpn_com (mp_ptr rp, mp_srcptr up, mp_size_t n)
714+mpn_neg (mp_ptr rp, mp_srcptr up, mp_size_t n)
724+ mpn_com (++rp, ++up, --n);
686/* MPN division interface. */
729/* MPN division interface. */
731+/* The 3/2 inverse is defined as
733+ m = floor( (B^3-1) / (B u1 + u0)) - B
688mpn_invert_3by2 (mp_limb_t u1, mp_limb_t u0)
736mpn_invert_3by2 (mp_limb_t u1, mp_limb_t u0)
690− mp_limb_t r, p, m;
738+ mp_limb_t r, p, m, ql;
691− unsigned ul, uh;
739+ unsigned ul, uh, qh;
694− /* First, do a 2/1 inverse. */
695− /* The inverse m is defined as floor( (B^2 - 1 - u1)/u1 ), so that 0 <
696− * B^2 - (B + m) u1 <= u1 */
697 assert (u1 >= GMP_LIMB_HIGHBIT);
741 assert (u1 >= GMP_LIMB_HIGHBIT);
743+ /* For notation, let b denote the half-limb base, so that B = b^2.
744+ Split u1 = b uh + ul. */
699 ul = u1 & GMP_LLIMB_MASK;
745 ul = u1 & GMP_LLIMB_MASK;
700 uh = u1 >> (GMP_LIMB_BITS / 2);
746 uh = u1 >> (GMP_LIMB_BITS / 2);
748+ /* Approximation of the high half of quotient. Differs from the 2/1
749+ inverse of the half limb uh, since we have already subtracted
702 qh = ~u1 / uh;
751 qh = ~u1 / uh;
753+ /* Adjust to get a half-limb 3/2 inverse, i.e., we want
755+ qh' = floor( (b^3 - 1) / u) - b = floor ((b^3 - b u - 1) / u
756+ = floor( (b (~u) + b-1) / u),
760+ r = b (~u) + b-1 - qh (b uh + ul)
761+ = b (~u - qh uh) + b-1 - qh ul
763+ Subtraction of qh ul may underflow, which implies adjustments.
764+ But by normalization, 2 u >= B > qh ul, so we need to adjust by
703 r = ((~u1 - (mp_limb_t) qh * uh) << (GMP_LIMB_BITS / 2)) | GMP_LLIMB_MASK;
768 r = ((~u1 - (mp_limb_t) qh * uh) << (GMP_LIMB_BITS / 2)) | GMP_LLIMB_MASK;
705 p = (mp_limb_t) qh * ul;
770 p = (mp_limb_t) qh * ul;
@@ -717,11 +782,19 @@ mpn_invert_3by2 (mp_limb_t u1, mp_limb_t u0)
785+ /* Low half of the quotient is
787+ ql = floor ( (b r + b-1) / u1).
720− /* Do a 3/2 division (with half limb size) */
789+ This is a 3/2 division (on half-limbs), for which qh is a
790+ suitable inverse. */
721 p = (r >> (GMP_LIMB_BITS / 2)) * qh + r;
792 p = (r >> (GMP_LIMB_BITS / 2)) * qh + r;
793+ /* Unlike full-limb 3/2, we can add 1 without overflow. For this to
794+ work, it is essential that ql is a full mp_limb_t. */
722 ql = (p >> (GMP_LIMB_BITS / 2)) + 1;
795 ql = (p >> (GMP_LIMB_BITS / 2)) + 1;
724− /* By the 3/2 method, we don't need the high half limb. */
797+ /* By the 3/2 trick, we don't need the high half limb. */
725 r = (r << (GMP_LIMB_BITS / 2)) + GMP_LLIMB_MASK - ql * u1;
798 r = (r << (GMP_LIMB_BITS / 2)) + GMP_LLIMB_MASK - ql * u1;
727 if (r >= (p << (GMP_LIMB_BITS / 2)))
800 if (r >= (p << (GMP_LIMB_BITS / 2)))
@@ -736,6 +809,8 @@ mpn_invert_3by2 (mp_limb_t u1, mp_limb_t u0)
812+ /* Now m is the 2/1 invers of u1. If u0 > 0, adjust it to become a
739 if (u0 > 0)
814 if (u0 > 0)
741 mp_limb_t th, tl;
816 mp_limb_t th, tl;
@@ -847,7 +922,7 @@ mpn_div_qr_1_preinv (mp_ptr qp, mp_srcptr np, mp_size_t nn,
848 if (inv->shift > 0)
923 if (inv->shift > 0)
850− tp = gmp_xalloc_limbs (nn);
925+ tp = qp ? qp : gmp_xalloc_limbs (nn);
851 r = mpn_lshift (tp, np, nn, inv->shift);
926 r = mpn_lshift (tp, np, nn, inv->shift);
@@ -856,7 +931,7 @@ mpn_div_qr_1_preinv (mp_ptr qp, mp_srcptr np, mp_size_t nn,
857 d = inv->d1;
932 d = inv->d1;
858 di = inv->di;
933 di = inv->di;
859− while (nn-- > 0)
934+ while (--nn >= 0)
861 mp_limb_t q;
936 mp_limb_t q;
@@ -864,7 +939,7 @@ mpn_div_qr_1_preinv (mp_ptr qp, mp_srcptr np, mp_size_t nn,
865 qp[nn] = q;
940 qp[nn] = q;
867− if (inv->shift > 0)
942+ if (inv->shift > 0 && !qp)
868 gmp_free (tp);
943 gmp_free (tp);
870 return r >> inv->shift;
945 return r >> inv->shift;
@@ -1140,7 +1215,7 @@ mpn_get_str_bits (unsigned char *sp, unsigned bits, mp_srcptr up, mp_size_t un)
1140 unsigned char mask;
1215 unsigned char mask;
1141 size_t sn, j;
1216 size_t sn, j;
1142 mp_size_t i;
1217 mp_size_t i;
1143− int shift;
1218+ unsigned shift;
1145 sn = ((un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1])
1220 sn = ((un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1])
1146 + bits - 1) / bits;
1221 + bits - 1) / bits;
@@ -1281,6 +1356,8 @@ mpn_set_str_bits (mp_ptr rp, const unsigned char *sp, size_t sn,
1281 return rn;
1356 return rn;
1359+/* Result is usually normalized, except for all-zero input, in which
1360+ case a single zero limb is written at *RP, and 1 is returned. */
1284static mp_size_t
1361static mp_size_t
1285mpn_set_str_other (mp_ptr rp, const unsigned char *sp, size_t sn,
1362mpn_set_str_other (mp_ptr rp, const unsigned char *sp, size_t sn,
1286 mp_limb_t b, const struct mpn_base_info *info)
1363 mp_limb_t b, const struct mpn_base_info *info)
@@ -1290,16 +1367,18 @@ mpn_set_str_other (mp_ptr rp, const unsigned char *sp, size_t sn,
1290 unsigned k;
1367 unsigned k;
1291 size_t j;
1368 size_t j;
1293 k = 1 + (sn - 1) % info->exp;
1372 k = 1 + (sn - 1) % info->exp;
1296 w = sp[j++];
1375 w = sp[j++];
1297− for (; --k > 0; )
1376+ while (--k != 0)
1298 w = w * b + sp[j++];
1377 w = w * b + sp[j++];
1300 rp[0] = w;
1379 rp[0] = w;
1302− for (rn = (w > 0); j < sn;)
1381+ for (rn = 1; j < sn;)
1304 mp_limb_t cy;
1383 mp_limb_t cy;
@@ -1342,9 +1421,11 @@ mpn_set_str (mp_ptr rp, const unsigned char *sp, size_t sn, int base)
1343mpz_init (mpz_t r)
1422mpz_init (mpz_t r)
1424+ static const mp_limb_t dummy_limb = 0xc1a0;
1345− r->_mp_alloc = 1;
1426+ r->_mp_alloc = 0;
1346 r->_mp_size = 0;
1427 r->_mp_size = 0;
1347− r->_mp_d = gmp_xalloc_limbs (1);
1428+ r->_mp_d = (mp_ptr) &dummy_limb;
1350/* The utility of this function is a bit limited, since many functions
1431/* The utility of this function is a bit limited, since many functions
@@ -1365,15 +1446,19 @@ mpz_init2 (mpz_t r, mp_bitcnt_t bits)
1366mpz_clear (mpz_t r)
1447mpz_clear (mpz_t r)
1368− gmp_free (r->_mp_d);
1450+ gmp_free (r->_mp_d);
1371−static void *
1453+static mp_ptr
1372mpz_realloc (mpz_t r, mp_size_t size)
1454mpz_realloc (mpz_t r, mp_size_t size)
1374 size = GMP_MAX (size, 1);
1456 size = GMP_MAX (size, 1);
1376− r->_mp_d = gmp_xrealloc_limbs (r->_mp_d, size);
1459+ r->_mp_d = gmp_xrealloc_limbs (r->_mp_d, size);
1461+ r->_mp_d = gmp_xalloc_limbs (size);
1377 r->_mp_alloc = size;
1462 r->_mp_alloc = size;
1379 if (GMP_ABS (r->_mp_size) > size)
1464 if (GMP_ABS (r->_mp_size) > size)
@@ -1396,7 +1481,7 @@ mpz_set_si (mpz_t r, signed long int x)
1396 else /* (x < 0) */
1481 else /* (x < 0) */
1398 r->_mp_size = -1;
1483 r->_mp_size = -1;
1399− r->_mp_d[0] = GMP_NEG_CAST (unsigned long int, x);
1484+ MPZ_REALLOC (r, 1)[0] = GMP_NEG_CAST (unsigned long int, x);
@@ -1406,7 +1491,7 @@ mpz_set_ui (mpz_t r, unsigned long int x)
1406 if (x > 0)
1491 if (x > 0)
1408 r->_mp_size = 1;
1493 r->_mp_size = 1;
1409− r->_mp_d[0] = x;
1494+ MPZ_REALLOC (r, 1)[0] = x;
1412 r->_mp_size = 0;
1497 r->_mp_size = 0;
@@ -1455,14 +1540,12 @@ mpz_fits_slong_p (const mpz_t u)
1456 mp_size_t us = u->_mp_size;
1541 mp_size_t us = u->_mp_size;
1458− if (us == 0)
1543+ if (us == 1)
1461 return u->_mp_d[0] < GMP_LIMB_HIGHBIT;
1544 return u->_mp_d[0] < GMP_LIMB_HIGHBIT;
1462 else if (us == -1)
1545 else if (us == -1)
1463 return u->_mp_d[0] <= GMP_LIMB_HIGHBIT;
1546 return u->_mp_d[0] <= GMP_LIMB_HIGHBIT;
1465− return 0;
1548+ return (us == 0);
@@ -1476,14 +1559,11 @@ mpz_fits_ulong_p (const mpz_t u)
1477mpz_get_si (const mpz_t u)
1560mpz_get_si (const mpz_t u)
1479− mp_size_t us = u->_mp_size;
1562+ if (u->_mp_size < 0)
1480−
1563+ /* This expression is necessary to properly handle 0x80000000 */
1482− return (long) (u->_mp_d[0] & ~GMP_LIMB_HIGHBIT);
1564+ return -1 - (long) ((u->_mp_d[0] - 1) & ~GMP_LIMB_HIGHBIT);
1484− return (long) (- u->_mp_d[0] | GMP_LIMB_HIGHBIT);
1486− return 0;
1566+ return (long) (mpz_get_ui (u) & ~GMP_LIMB_HIGHBIT);
1489unsigned long int
1569unsigned long int
@@ -1507,6 +1587,48 @@ mpz_getlimbn (const mpz_t u, mp_size_t n)
1507 return 0;
1587 return 0;
1591+mpz_realloc2 (mpz_t x, mp_bitcnt_t n)
1593+ mpz_realloc (x, 1 + (n - (n != 0)) / GMP_LIMB_BITS);
1597+mpz_limbs_read (mpz_srcptr x)
1603+mpz_limbs_modify (mpz_t x, mp_size_t n)
1606+ return MPZ_REALLOC (x, n);
1610+mpz_limbs_write (mpz_t x, mp_size_t n)
1612+ return mpz_limbs_modify (x, n);
1616+mpz_limbs_finish (mpz_t x, mp_size_t xs)
1619+ xn = mpn_normalized_size (x->_mp_d, GMP_ABS (xs));
1620+ x->_mp_size = xs < 0 ? -xn : xn;
1624+mpz_roinit_n (mpz_t x, mp_srcptr xp, mp_size_t xs)
1627+ x->_mp_d = (mp_ptr) xp;
1628+ mpz_limbs_finish (x, xs);
1511/* Conversions and comparison to double. */
1633/* Conversions and comparison to double. */
@@ -1654,9 +1776,7 @@ mpz_cmp_d (const mpz_t x, double d)
1655mpz_sgn (const mpz_t u)
1777mpz_sgn (const mpz_t u)
1657− mp_size_t usize = u->_mp_size;
1779+ return GMP_CMP (u->_mp_size, 0);
1659− return (usize > 0) - (usize < 0);
@@ -1671,13 +1791,7 @@ mpz_cmp_si (const mpz_t u, long v)
1671 else if (usize >= 0)
1791 else if (usize >= 0)
1672 return 1;
1792 return 1;
1673 else /* usize == -1 */
1793 else /* usize == -1 */
1675− mp_limb_t ul = u->_mp_d[0];
1794+ return GMP_CMP (GMP_NEG_CAST (mp_limb_t, v), u->_mp_d[0]);
1676− if ((mp_limb_t)GMP_NEG_CAST (unsigned long int, v) < ul)
1679− return (mp_limb_t)GMP_NEG_CAST (unsigned long int, v) > ul;
@@ -1690,10 +1804,7 @@ mpz_cmp_ui (const mpz_t u, unsigned long v)
1690 else if (usize < 0)
1804 else if (usize < 0)
1691 return -1;
1805 return -1;
1693− {
1807+ return GMP_CMP (mpz_get_ui (u), v);
1694− mp_limb_t ul = (usize > 0) ? u->_mp_d[0] : 0;
1695− return (ul > v) - (ul < v);
@@ -1713,15 +1824,10 @@ mpz_cmp (const mpz_t a, const mpz_t b)
1714mpz_cmpabs_ui (const mpz_t u, unsigned long v)
1825mpz_cmpabs_ui (const mpz_t u, unsigned long v)
1716− mp_size_t un = GMP_ABS (u->_mp_size);
1827+ if (GMP_ABS (u->_mp_size) > 1)
1720 return 1;
1828 return 1;
1722− ul = (un == 1) ? u->_mp_d[0] : 0;
1830+ return GMP_CMP (mpz_get_ui (u), v);
1724− return (ul > v) - (ul < v);
@@ -1734,18 +1840,14 @@ mpz_cmpabs (const mpz_t u, const mpz_t v)
1735mpz_abs (mpz_t r, const mpz_t u)
1841mpz_abs (mpz_t r, const mpz_t u)
1738− mpz_set (r, u);
1843+ mpz_set (r, u);
1740 r->_mp_size = GMP_ABS (r->_mp_size);
1844 r->_mp_size = GMP_ABS (r->_mp_size);
1744mpz_neg (mpz_t r, const mpz_t u)
1848mpz_neg (mpz_t r, const mpz_t u)
1747− mpz_set (r, u);
1850+ mpz_set (r, u);
1749 r->_mp_size = -r->_mp_size;
1851 r->_mp_size = -r->_mp_size;
@@ -1771,7 +1873,7 @@ mpz_abs_add_ui (mpz_t r, const mpz_t a, unsigned long b)
1771 an = GMP_ABS (a->_mp_size);
1873 an = GMP_ABS (a->_mp_size);
1772 if (an == 0)
1874 if (an == 0)
1774− r->_mp_d[0] = b;
1876+ MPZ_REALLOC (r, 1)[0] = b;
1775 return b > 0;
1877 return b > 0;
@@ -1790,14 +1892,15 @@ static mp_size_t
1790mpz_abs_sub_ui (mpz_t r, const mpz_t a, unsigned long b)
1892mpz_abs_sub_ui (mpz_t r, const mpz_t a, unsigned long b)
1792 mp_size_t an = GMP_ABS (a->_mp_size);
1894 mp_size_t an = GMP_ABS (a->_mp_size);
1793− mp_ptr rp = MPZ_REALLOC (r, an);
1895+ mp_ptr rp;
1795 if (an == 0)
1897 if (an == 0)
1797− rp[0] = b;
1899+ MPZ_REALLOC (r, 1)[0] = b;
1798 return -(b > 0);
1900 return -(b > 0);
1902+ rp = MPZ_REALLOC (r, an);
1800− else if (an == 1 && a->_mp_d[0] < b)
1903+ if (an == 1 && a->_mp_d[0] < b)
1802 rp[0] = b - a->_mp_d[0];
1905 rp[0] = b - a->_mp_d[0];
1803 return -1;
1906 return -1;
@@ -2015,12 +2118,51 @@ mpz_mul_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bits)
2016 mpn_copyd (rp + limbs, u->_mp_d, un);
2119 mpn_copyd (rp + limbs, u->_mp_d, un);
2019− rp[--limbs] = 0;
2121+ mpn_zero (rp, limbs);
2021 r->_mp_size = (u->_mp_size < 0) ? - rn : rn;
2123 r->_mp_size = (u->_mp_size < 0) ? - rn : rn;
2127+mpz_addmul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2131+ mpz_mul_ui (t, u, v);
2137+mpz_submul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2141+ mpz_mul_ui (t, u, v);
2147+mpz_addmul (mpz_t r, const mpz_t u, const mpz_t v)
2157+mpz_submul (mpz_t r, const mpz_t u, const mpz_t v)
2025/* MPZ division */
2167/* MPZ division */
2026enum mpz_div_round_mode { GMP_DIV_FLOOR, GMP_DIV_CEIL, GMP_DIV_TRUNC };
2168enum mpz_div_round_mode { GMP_DIV_FLOOR, GMP_DIV_CEIL, GMP_DIV_TRUNC };
@@ -2085,8 +2227,7 @@ mpz_div_qr (mpz_t q, mpz_t r,
2085 mp_size_t qn, rn;
2227 mp_size_t qn, rn;
2086 mpz_t tq, tr;
2228 mpz_t tq, tr;
2088− mpz_init (tr);
2230+ mpz_init_set (tr, n);
2090 np = tr->_mp_d;
2231 np = tr->_mp_d;
2092 qn = nn - dn + 1;
2233 qn = nn - dn + 1;
@@ -2230,7 +2371,6 @@ mpz_div_q_2exp (mpz_t q, const mpz_t u, mp_bitcnt_t bit_index,
2231 if (qn <= 0)
2372 if (qn <= 0)
2236 qp = MPZ_REALLOC (q, qn);
2376 qp = MPZ_REALLOC (q, qn);
@@ -2284,16 +2424,9 @@ mpz_div_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bit_index,
2285 /* Have to negate and sign extend. */
2425 /* Have to negate and sign extend. */
2286 mp_size_t i;
2426 mp_size_t i;
2289− for (cy = 1, i = 0; i < un; i++)
2428+ gmp_assert_nocarry (! mpn_neg (rp, u->_mp_d, un));
2291− mp_limb_t s = ~u->_mp_d[i] + cy;
2296− for (; i < rn - 1; i++)
2429+ for (i = un; i < rn - 1; i++)
2297 rp[i] = GMP_LIMB_MAX;
2430 rp[i] = GMP_LIMB_MAX;
2299 rp[rn-1] = mask;
2432 rp[rn-1] = mask;
@@ -2318,23 +2451,13 @@ mpz_div_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bit_index,
2318 if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
2451 if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
2320 /* If r != 0, compute 2^{bit_count} - r. */
2453 /* If r != 0, compute 2^{bit_count} - r. */
2321− mp_size_t i;
2454+ mpn_neg (rp, rp, rn);
2323− for (i = 0; i < rn && rp[i] == 0; i++)
2327− /* r > 0, need to flip sign. */
2328− rp[i] = ~rp[i] + 1;
2332− rp[rn-1] &= mask;
2456+ rp[rn-1] &= mask;
2334− /* us is not used for anything else, so we can modify it
2458+ /* us is not used for anything else, so we can modify it
2335− here to indicate flipped sign. */
2459+ here to indicate flipped sign. */
2336− us = -us;
2460+ us = -us;
2340 rn = mpn_normalized_size (rp, rn);
2463 rn = mpn_normalized_size (rp, rn);
@@ -2389,6 +2512,24 @@ mpz_divisible_p (const mpz_t n, const mpz_t d)
2389 return mpz_div_qr (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
2512 return mpz_div_qr (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
2516+mpz_congruent_p (const mpz_t a, const mpz_t b, const mpz_t m)
2521+ /* a == b (mod 0) iff a == b */
2522+ if (mpz_sgn (m) == 0)
2523+ return (mpz_cmp (a, b) == 0);
2527+ res = mpz_divisible_p (t, m);
2392static unsigned long
2533static unsigned long
2393mpz_div_qr_ui (mpz_t q, mpz_t r,
2534mpz_div_qr_ui (mpz_t q, mpz_t r,
2394 const mpz_t n, unsigned long d, enum mpz_div_round_mode mode)
2535 const mpz_t n, unsigned long d, enum mpz_div_round_mode mode)
@@ -2431,7 +2572,7 @@ mpz_div_qr_ui (mpz_t q, mpz_t r,
2434− r->_mp_d[0] = rl;
2575+ MPZ_REALLOC (r, 1)[0] = rl;
2435 r->_mp_size = rs;
2576 r->_mp_size = rs;
@@ -2955,9 +3096,7 @@ void
2955mpz_ui_pow_ui (mpz_t r, unsigned long blimb, unsigned long e)
3096mpz_ui_pow_ui (mpz_t r, unsigned long blimb, unsigned long e)
2957 mpz_t b;
3098 mpz_t b;
2958− mpz_init_set_ui (b, blimb);
2959− mpz_pow_ui (r, b, e);
3099+ mpz_pow_ui (r, mpz_roinit_n (b, &blimb, 1), e);
@@ -3029,7 +3168,7 @@ mpz_powm (mpz_t r, const mpz_t b, const mpz_t e, const mpz_t m)
3030 mpz_init_set_ui (tr, 1);
3169 mpz_init_set_ui (tr, 1);
3032− while (en-- > 0)
3171+ while (--en >= 0)
3034 mp_limb_t w = e->_mp_d[en];
3173 mp_limb_t w = e->_mp_d[en];
3035 mp_limb_t bit;
3174 mp_limb_t bit;
@@ -3069,9 +3208,7 @@ void
3069mpz_powm_ui (mpz_t r, const mpz_t b, unsigned long elimb, const mpz_t m)
3208mpz_powm_ui (mpz_t r, const mpz_t b, unsigned long elimb, const mpz_t m)
3071 mpz_t e;
3210 mpz_t e;
3072− mpz_init_set_ui (e, elimb);
3073− mpz_powm (r, b, e, m);
3211+ mpz_powm (r, b, mpz_roinit_n (e, &elimb, 1), m);
3077/* x=trunc(y^(1/z)), r=y-x^z */
3214/* x=trunc(y^(1/z)), r=y-x^z */
@@ -3088,19 +3225,16 @@ mpz_rootrem (mpz_t x, mpz_t r, const mpz_t y, unsigned long z)
3088 gmp_die ("mpz_rootrem: Zeroth root.");
3225 gmp_die ("mpz_rootrem: Zeroth root.");
3090 if (mpz_cmpabs_ui (y, 1) <= 0) {
3227 if (mpz_cmpabs_ui (y, 1) <= 0) {
3091− mpz_set (x, y);
3229+ mpz_set (x, y);
3093 r->_mp_size = 0;
3231 r->_mp_size = 0;
3097 mpz_init (u);
3235 mpz_init (u);
3098− {
3236+ mpz_init (t);
3100− tb = mpz_sizeinbase (y, 2) / z + 1;
3237+ mpz_setbit (t, mpz_sizeinbase (y, 2) / z + 1);
3102− mpz_setbit (t, tb);
3105 if (z == 2) /* simplify sqrt loop: z-1 == 1 */
3239 if (z == 2) /* simplify sqrt loop: z-1 == 1 */
@@ -3132,7 +3266,8 @@ mpz_rootrem (mpz_t x, mpz_t r, const mpz_t y, unsigned long z)
3132 mpz_pow_ui (t, u, z);
3266 mpz_pow_ui (t, u, z);
3133 mpz_sub (r, y, t);
3267 mpz_sub (r, y, t);
3135− mpz_swap (x, u);
3270+ mpz_swap (x, u);
3136 mpz_clear (u);
3271 mpz_clear (u);
3137 mpz_clear (t);
3272 mpz_clear (t);
@@ -3164,6 +3299,47 @@ mpz_sqrt (mpz_t s, const mpz_t u)
3164 mpz_rootrem (s, NULL, u, 2);
3299 mpz_rootrem (s, NULL, u, 2);
3303+mpz_perfect_square_p (const mpz_t u)
3305+ if (u->_mp_size <= 0)
3306+ return (u->_mp_size == 0);
3308+ return mpz_root (NULL, u, 2);
3312+mpn_perfect_square_p (mp_srcptr p, mp_size_t n)
3317+ assert (p [n-1] != 0);
3318+ return mpz_root (NULL, mpz_roinit_n (t, p, n), 2);
3322+mpn_sqrtrem (mp_ptr sp, mp_ptr rp, mp_srcptr p, mp_size_t n)
3328+ assert (p [n-1] != 0);
3332+ mpz_rootrem (s, r, mpz_roinit_n (u, p, n), 2);
3334+ assert (s->_mp_size == (n+1)/2);
3335+ mpn_copyd (sp, s->_mp_d, s->_mp_size);
3339+ mpn_copyd (rp, r->_mp_d, res);
3168/* Combinatorics */
3344/* Combinatorics */
@@ -3171,7 +3347,7 @@ void
3171mpz_fac_ui (mpz_t x, unsigned long n)
3347mpz_fac_ui (mpz_t x, unsigned long n)
3173 mpz_set_ui (x, n + (n == 0));
3349 mpz_set_ui (x, n + (n == 0));
3174− for (;n > 2;)
3350+ while (n > 2)
3175 mpz_mul_ui (x, x, --n);
3351 mpz_mul_ui (x, x, --n);
@@ -3195,6 +3371,102 @@ mpz_bin_uiui (mpz_t r, unsigned long n, unsigned long k)
3195 mpz_clear (t);
3371 mpz_clear (t);
3375+/* Primality testing */
3377+gmp_millerrabin (const mpz_t n, const mpz_t nm1, mpz_t y,
3378+ const mpz_t q, mp_bitcnt_t k)
3382+ /* Caller must initialize y to the base. */
3383+ mpz_powm (y, y, q, n);
3385+ if (mpz_cmp_ui (y, 1) == 0 || mpz_cmp (y, nm1) == 0)
3390+ mpz_powm_ui (y, y, 2, n);
3391+ if (mpz_cmp (y, nm1) == 0)
3393+ /* y == 1 means that the previous y was a non-trivial square root
3394+ of 1 (mod n). y == 0 means that n is a power of the base.
3395+ In either case, n is not prime. */
3396+ if (mpz_cmp_ui (y, 1) <= 0)
3402+/* This product is 0xc0cfd797, and fits in 32 bits. */
3403+#define GMP_PRIME_PRODUCT \
3404+ (3UL*5UL*7UL*11UL*13UL*17UL*19UL*23UL*29UL)
3406+/* Bit (p+1)/2 is set, for each odd prime <= 61 */
3407+#define GMP_PRIME_MASK 0xc96996dcUL
3410+mpz_probab_prime_p (const mpz_t n, int reps)
3419+ /* Note that we use the absolute value of n only, for compatibility
3420+ with the real GMP. */
3421+ if (mpz_even_p (n))
3422+ return (mpz_cmpabs_ui (n, 2) == 0) ? 2 : 0;
3424+ /* Above test excludes n == 0 */
3425+ assert (n->_mp_size != 0);
3427+ if (mpz_cmpabs_ui (n, 64) < 0)
3428+ return (GMP_PRIME_MASK >> (n->_mp_d[0] >> 1)) & 2;
3430+ if (mpz_gcd_ui (NULL, n, GMP_PRIME_PRODUCT) != 1)
3433+ /* All prime factors are >= 31. */
3434+ if (mpz_cmpabs_ui (n, 31*31) < 0)
3437+ /* Use Miller-Rabin, with a deterministic sequence of bases, a[j] =
3438+ j^2 + j + 41 using Euler's polynomial. We potentially stop early,
3439+ if a[j] >= n - 1. Since n >= 31*31, this can happen only if reps >
3440+ 30 (a[30] == 971 > 31*31 == 961). */
3446+ /* Find q and k, where q is odd and n = 1 + 2**k * q. */
3447+ nm1->_mp_size = mpz_abs_sub_ui (nm1, n, 1);
3448+ k = mpz_scan1 (nm1, 0);
3449+ mpz_tdiv_q_2exp (q, nm1, k);
3451+ for (j = 0, is_prime = 1; is_prime & (j < reps); j++)
3453+ mpz_set_ui (y, (unsigned long) j*j+j+41);
3454+ if (mpz_cmp (y, nm1) >= 0)
3456+ /* Don't try any further bases. This "early" break does not affect
3457+ the result for any reasonable reps value (<=5000 was tested) */
3461+ is_prime = gmp_millerrabin (n, nm1, y, q, k);
3199/* Logical operations and bit manipulation. */
3471/* Logical operations and bit manipulation. */
@@ -3246,7 +3518,7 @@ mpz_tstbit (const mpz_t d, mp_bitcnt_t bit_index)
3246 must be complemented. */
3518 must be complemented. */
3247 if (shift > 0 && (w << (GMP_LIMB_BITS - shift)) > 0)
3519 if (shift > 0 && (w << (GMP_LIMB_BITS - shift)) > 0)
3248 return bit ^ 1;
3520 return bit ^ 1;
3249− while (limb_index-- > 0)
3521+ while (--limb_index >= 0)
3250 if (d->_mp_d[limb_index] > 0)
3522 if (d->_mp_d[limb_index] > 0)
3251 return bit ^ 1;
3523 return bit ^ 1;
@@ -3311,7 +3583,7 @@ mpz_abs_sub_bit (mpz_t d, mp_bitcnt_t bit_index)
3312 gmp_assert_nocarry (mpn_sub_1 (dp + limb_index, dp + limb_index,
3584 gmp_assert_nocarry (mpn_sub_1 (dp + limb_index, dp + limb_index,
3313 dn - limb_index, bit));
3585 dn - limb_index, bit));
3314− dn -= (dp[dn-1] == 0);
3586+ dn = mpn_normalized_size (dp, dn);
3315 d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
3587 d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
@@ -3389,7 +3661,7 @@ mpz_and (mpz_t r, const mpz_t u, const mpz_t v)
3389 /* If the smaller input is positive, higher limbs don't matter. */
3661 /* If the smaller input is positive, higher limbs don't matter. */
3390 rn = vx ? un : vn;
3662 rn = vx ? un : vn;
3392− rp = MPZ_REALLOC (r, rn + rc);
3664+ rp = MPZ_REALLOC (r, rn + (mp_size_t) rc);
3394 up = u->_mp_d;
3666 up = u->_mp_d;
3395 vp = v->_mp_d;
3667 vp = v->_mp_d;
@@ -3462,7 +3734,7 @@ mpz_ior (mpz_t r, const mpz_t u, const mpz_t v)
3462 don't matter. */
3734 don't matter. */
3463 rn = vx ? vn : un;
3735 rn = vx ? vn : un;
3465− rp = MPZ_REALLOC (r, rn + rc);
3737+ rp = MPZ_REALLOC (r, rn + (mp_size_t) rc);
3467 up = u->_mp_d;
3739 up = u->_mp_d;
3468 vp = v->_mp_d;
3740 vp = v->_mp_d;
@@ -3531,7 +3803,7 @@ mpz_xor (mpz_t r, const mpz_t u, const mpz_t v)
3531 vx = -vc;
3803 vx = -vc;
3532 rx = -rc;
3804 rx = -rc;
3534− rp = MPZ_REALLOC (r, un + rc);
3806+ rp = MPZ_REALLOC (r, un + (mp_size_t) rc);
3536 up = u->_mp_d;
3808 up = u->_mp_d;
3537 vp = v->_mp_d;
3809 vp = v->_mp_d;
@@ -3587,20 +3859,28 @@ gmp_popcount_limb (mp_limb_t x)
3589mp_bitcnt_t
3861mp_bitcnt_t
3590−mpz_popcount (const mpz_t u)
3862+mpn_popcount (mp_srcptr p, mp_size_t n)
3592− mp_size_t un, i;
3864+ mp_size_t i;
3593 mp_bitcnt_t c;
3865 mp_bitcnt_t c;
3867+ for (c = 0, i = 0; i < n; i++)
3868+ c += gmp_popcount_limb (p[i]);
3874+mpz_popcount (const mpz_t u)
3595 un = u->_mp_size;
3878 un = u->_mp_size;
3597 if (un < 0)
3880 if (un < 0)
3598 return ~(mp_bitcnt_t) 0;
3881 return ~(mp_bitcnt_t) 0;
3600− for (c = 0, i = 0; i < un; i++)
3883+ return mpn_popcount (u->_mp_d, un);
3601− c += gmp_popcount_limb (u->_mp_d[i]);
3606mp_bitcnt_t
3886mp_bitcnt_t
@@ -3799,7 +4079,7 @@ mpz_get_str (char *sp, int base, const mpz_t u)
3800 sn = 1 + mpz_sizeinbase (u, base);
4080 sn = 1 + mpz_sizeinbase (u, base);
3801 if (!sp)
4081 if (!sp)
3802− sp = gmp_xalloc (1 + sn);
4082+ sp = (char *) gmp_xalloc (1 + sn);
3804 un = GMP_ABS (u->_mp_size);
4084 un = GMP_ABS (u->_mp_size);
@@ -3846,7 +4126,7 @@ mpz_set_str (mpz_t r, const char *sp, int base)
3846 unsigned bits;
4126 unsigned bits;
3847 mp_size_t rn, alloc;
4127 mp_size_t rn, alloc;
3848 mp_ptr rp;
4128 mp_ptr rp;
3849− size_t sn;
4129+ size_t dn;
3850 int sign;
4130 int sign;
3851 unsigned char *dp;
4131 unsigned char *dp;
@@ -3860,18 +4140,17 @@ mpz_set_str (mpz_t r, const char *sp, int base)
3861 if (base == 0)
4141 if (base == 0)
3863− if (*sp == '0')
4143+ if (sp[0] == '0')
3866− if (*sp == 'x' || *sp == 'X')
4145+ if (sp[1] == 'x' || sp[1] == 'X')
3868 base = 16;
4147 base = 16;
3869− sp++;
4148+ sp += 2;
3871− else if (*sp == 'b' || *sp == 'B')
4150+ else if (sp[1] == 'b' || sp[1] == 'B')
3873 base = 2;
4152 base = 2;
3874− sp++;
4153+ sp += 2;
3877 base = 8;
4156 base = 8;
@@ -3880,16 +4159,20 @@ mpz_set_str (mpz_t r, const char *sp, int base)
3880 base = 10;
4159 base = 10;
3883− sn = strlen (sp);
4162+ if (!*sp)
3884− dp = gmp_xalloc (sn + (sn == 0));
4167+ dp = (unsigned char *) gmp_xalloc (strlen (sp));
3886− for (sn = 0; *sp; sp++)
4169+ for (dn = 0; *sp; sp++)
3888 unsigned digit;
4171 unsigned digit;
3890 if (isspace ((unsigned char) *sp))
4173 if (isspace ((unsigned char) *sp))
3891 continue;
4174 continue;
3892− if (*sp >= '0' && *sp <= '9')
4175+ else if (*sp >= '0' && *sp <= '9')
3893 digit = *sp - '0';
4176 digit = *sp - '0';
3894 else if (*sp >= 'a' && *sp <= 'z')
4177 else if (*sp >= 'a' && *sp <= 'z')
3895 digit = *sp - 'a' + 10;
4178 digit = *sp - 'a' + 10;
@@ -3898,31 +4181,40 @@ mpz_set_str (mpz_t r, const char *sp, int base)
3899 digit = base; /* fail */
4182 digit = base; /* fail */
3901− if (digit >= base)
4184+ if (digit >= (unsigned) base)
3903 gmp_free (dp);
4186 gmp_free (dp);
3904 r->_mp_size = 0;
4187 r->_mp_size = 0;
3905 return -1;
4188 return -1;
3908− dp[sn++] = digit;
4191+ dp[dn++] = digit;
3911 bits = mpn_base_power_of_two_p (base);
4200 bits = mpn_base_power_of_two_p (base);
3913 if (bits > 0)
4202 if (bits > 0)
3915− alloc = (sn * bits + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
4204+ alloc = (dn * bits + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
3916 rp = MPZ_REALLOC (r, alloc);
4205 rp = MPZ_REALLOC (r, alloc);
3917− rn = mpn_set_str_bits (rp, dp, sn, bits);
4206+ rn = mpn_set_str_bits (rp, dp, dn, bits);
3921 struct mpn_base_info info;
4210 struct mpn_base_info info;
3922 mpn_get_base_info (&info, base);
4211 mpn_get_base_info (&info, base);
3923− alloc = (sn + info.exp - 1) / info.exp;
4212+ alloc = (dn + info.exp - 1) / info.exp;
3924 rp = MPZ_REALLOC (r, alloc);
4213 rp = MPZ_REALLOC (r, alloc);
3925− rn = mpn_set_str_other (rp, dp, sn, base, &info);
4214+ rn = mpn_set_str_other (rp, dp, dn, base, &info);
4215+ /* Normalization, needed for all-zero input. */
4217+ rn -= rp[rn-1] == 0;
3927 assert (rn <= alloc);
4219 assert (rn <= alloc);
3928 gmp_free (dp);
4220 gmp_free (dp);