You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1738 lines
42 KiB

  1. From 57700c26d73cf7fa6f5cfaec1145eccf388acab9 Mon Sep 17 00:00:00 2001
  2. From: Nikos Mavrogiannopoulos <nmav@gnutls.org>
  3. Date: Sun, 9 Mar 2014 11:27:42 +0100
  4. Subject: [PATCH 1/5] Updated mini-gmp
  5. ---
  6. mini-gmp.c | 890 +++++++++++++++++++++++++++++++++++++++----------------------
  7. mini-gmp.h | 55 +++-
  8. 2 files changed, 618 insertions(+), 327 deletions(-)
  9. diff --git a/mini-gmp.c b/mini-gmp.c
  10. index 8b6f070..766df30 100644
  11. --- a/mini-gmp.c
  12. +++ b/mini-gmp.c
  13. @@ -2,24 +2,33 @@
  14. Contributed to the GNU project by Niels Möller
  15. -Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
  16. -2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
  17. -Free Software Foundation, Inc.
  18. +Copyright 1991-1997, 1999-2014 Free Software Foundation, Inc.
  19. This file is part of the GNU MP Library.
  20. The GNU MP Library is free software; you can redistribute it and/or modify
  21. -it under the terms of the GNU Lesser General Public License as published by
  22. -the Free Software Foundation; either version 3 of the License, or (at your
  23. -option) any later version.
  24. +it under the terms of either:
  25. +
  26. + * the GNU Lesser General Public License as published by the Free
  27. + Software Foundation; either version 3 of the License, or (at your
  28. + option) any later version.
  29. +
  30. +or
  31. +
  32. + * the GNU General Public License as published by the Free Software
  33. + Foundation; either version 2 of the License, or (at your option) any
  34. + later version.
  35. +
  36. +or both in parallel, as here.
  37. The GNU MP Library is distributed in the hope that it will be useful, but
  38. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  39. -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  40. -License for more details.
  41. +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  42. +for more details.
  43. -You should have received a copy of the GNU Lesser General Public License
  44. -along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
  45. +You should have received copies of the GNU General Public License and the
  46. +GNU Lesser General Public License along with the GNU MP Library. If not,
  47. +see https://www.gnu.org/licenses/. */
  48. /* NOTE: All functions in this file which are not declared in
  49. mini-gmp.h are internal, and are not intended to be compatible
  50. @@ -222,11 +231,13 @@ along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
  51. } while (0)
  52. #define MPZ_SRCPTR_SWAP(x, y) \
  53. do { \
  54. - mpz_srcptr __mpz_srcptr_swap__tmp = (x); \
  55. + mpz_srcptr __mpz_srcptr_swap__tmp = (x); \
  56. (x) = (y); \
  57. (y) = __mpz_srcptr_swap__tmp; \
  58. } while (0)
  59. +const int mp_bits_per_limb = GMP_LIMB_BITS;
  60. +
  61. /* Memory allocation and other helper functions. */
  62. static void
  63. @@ -342,12 +353,10 @@ mpn_copyd (mp_ptr d, mp_srcptr s, mp_size_t n)
  64. int
  65. mpn_cmp (mp_srcptr ap, mp_srcptr bp, mp_size_t n)
  66. {
  67. - for (; n > 0; n--)
  68. + while (--n >= 0)
  69. {
  70. - if (ap[n-1] < bp[n-1])
  71. - return -1;
  72. - else if (ap[n-1] > bp[n-1])
  73. - return 1;
  74. + if (ap[n] != bp[n])
  75. + return ap[n] > bp[n] ? 1 : -1;
  76. }
  77. return 0;
  78. }
  79. @@ -355,10 +364,8 @@ mpn_cmp (mp_srcptr ap, mp_srcptr bp, mp_size_t n)
  80. static int
  81. mpn_cmp4 (mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
  82. {
  83. - if (an > bn)
  84. - return 1;
  85. - else if (an < bn)
  86. - return -1;
  87. + if (an != bn)
  88. + return an < bn ? -1 : 1;
  89. else
  90. return mpn_cmp (ap, bp, an);
  91. }
  92. @@ -373,20 +380,31 @@ mpn_normalized_size (mp_srcptr xp, mp_size_t n)
  93. #define mpn_zero_p(xp, n) (mpn_normalized_size ((xp), (n)) == 0)
  94. +void
  95. +mpn_zero (mp_ptr rp, mp_size_t n)
  96. +{
  97. + mp_size_t i;
  98. +
  99. + for (i = 0; i < n; i++)
  100. + rp[i] = 0;
  101. +}
  102. +
  103. mp_limb_t
  104. mpn_add_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
  105. {
  106. mp_size_t i;
  107. assert (n > 0);
  108. -
  109. - for (i = 0; i < n; i++)
  110. + i = 0;
  111. + do
  112. {
  113. mp_limb_t r = ap[i] + b;
  114. /* Carry out */
  115. b = (r < b);
  116. rp[i] = r;
  117. }
  118. + while (++i < n);
  119. +
  120. return b;
  121. }
  122. @@ -429,7 +447,8 @@ mpn_sub_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
  123. assert (n > 0);
  124. - for (i = 0; i < n; i++)
  125. + i = 0;
  126. + do
  127. {
  128. mp_limb_t a = ap[i];
  129. /* Carry out */
  130. @@ -437,6 +456,8 @@ mpn_sub_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
  131. rp[i] = a - b;
  132. b = cy;
  133. }
  134. + while (++i < n);
  135. +
  136. return b;
  137. }
  138. @@ -602,7 +623,7 @@ mpn_lshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
  139. retval = low_limb >> tnc;
  140. high_limb = (low_limb << cnt);
  141. - for (i = n - 1; i != 0; i--)
  142. + for (i = n; --i != 0;)
  143. {
  144. low_limb = *--up;
  145. *--rp = high_limb | (low_limb >> tnc);
  146. @@ -630,7 +651,7 @@ mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
  147. retval = (high_limb << tnc);
  148. low_limb = high_limb >> cnt;
  149. - for (i = n - 1; i != 0; i--)
  150. + for (i = n; --i != 0;)
  151. {
  152. high_limb = *up++;
  153. *rp++ = low_limb | (high_limb << tnc);
  154. @@ -641,6 +662,46 @@ mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
  155. return retval;
  156. }
  157. +static mp_bitcnt_t
  158. +mpn_common_scan (mp_limb_t limb, mp_size_t i, mp_srcptr up, mp_size_t un,
  159. + mp_limb_t ux)
  160. +{
  161. + unsigned cnt;
  162. +
  163. + assert (ux == 0 || ux == GMP_LIMB_MAX);
  164. + assert (0 <= i && i <= un );
  165. +
  166. + while (limb == 0)
  167. + {
  168. + i++;
  169. + if (i == un)
  170. + return (ux == 0 ? ~(mp_bitcnt_t) 0 : un * GMP_LIMB_BITS);
  171. + limb = ux ^ up[i];
  172. + }
  173. + gmp_ctz (cnt, limb);
  174. + return (mp_bitcnt_t) i * GMP_LIMB_BITS + cnt;
  175. +}
  176. +
  177. +mp_bitcnt_t
  178. +mpn_scan1 (mp_srcptr ptr, mp_bitcnt_t bit)
  179. +{
  180. + mp_size_t i;
  181. + i = bit / GMP_LIMB_BITS;
  182. +
  183. + return mpn_common_scan ( ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
  184. + i, ptr, i, 0);
  185. +}
  186. +
  187. +mp_bitcnt_t
  188. +mpn_scan0 (mp_srcptr ptr, mp_bitcnt_t bit)
  189. +{
  190. + mp_size_t i;
  191. + i = bit / GMP_LIMB_BITS;
  192. +
  193. + return mpn_common_scan (~ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
  194. + i, ptr, i, GMP_LIMB_MAX);
  195. +}
  196. +
  197. /* MPN division interface. */
  198. mp_limb_t
  199. @@ -715,8 +776,7 @@ mpn_invert_3by2 (mp_limb_t u1, mp_limb_t u0)
  200. if (r < th)
  201. {
  202. m--;
  203. - if (r > u1 || (r == u1 && tl > u0))
  204. - m--;
  205. + m -= ((r > u1) | ((r == u1) & (tl > u0)));
  206. }
  207. }
  208. @@ -836,14 +896,20 @@ mpn_div_qr_1 (mp_ptr qp, mp_srcptr np, mp_size_t nn, mp_limb_t d)
  209. assert (d > 0);
  210. /* Special case for powers of two. */
  211. - if (d > 1 && (d & (d-1)) == 0)
  212. + if ((d & (d-1)) == 0)
  213. {
  214. - unsigned shift;
  215. mp_limb_t r = np[0] & (d-1);
  216. - gmp_ctz (shift, d);
  217. if (qp)
  218. - mpn_rshift (qp, np, nn, shift);
  219. -
  220. + {
  221. + if (d <= 1)
  222. + mpn_copyi (qp, np, nn);
  223. + else
  224. + {
  225. + unsigned shift;
  226. + gmp_ctz (shift, d);
  227. + mpn_rshift (qp, np, nn, shift);
  228. + }
  229. + }
  230. return r;
  231. }
  232. else
  233. @@ -880,7 +946,8 @@ mpn_div_qr_2_preinv (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
  234. r0 = np[nn - 1];
  235. - for (i = nn - 2; i >= 0; i--)
  236. + i = nn - 2;
  237. + do
  238. {
  239. mp_limb_t n0, q;
  240. n0 = np[i];
  241. @@ -889,6 +956,7 @@ mpn_div_qr_2_preinv (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
  242. if (qp)
  243. qp[i] = q;
  244. }
  245. + while (--i >= 0);
  246. if (shift > 0)
  247. {
  248. @@ -930,18 +998,19 @@ mpn_div_qr_pi1 (mp_ptr qp,
  249. assert (dn > 2);
  250. assert (nn >= dn);
  251. - assert ((dp[dn-1] & GMP_LIMB_HIGHBIT) != 0);
  252. d1 = dp[dn - 1];
  253. d0 = dp[dn - 2];
  254. + assert ((d1 & GMP_LIMB_HIGHBIT) != 0);
  255. /* Iteration variable is the index of the q limb.
  256. *
  257. * We divide <n1, np[dn-1+i], np[dn-2+i], np[dn-3+i],..., np[i]>
  258. * by <d1, d0, dp[dn-3], ..., dp[0] >
  259. */
  260. - for (i = nn - dn; i >= 0; i--)
  261. + i = nn - dn;
  262. + do
  263. {
  264. mp_limb_t n0 = np[dn-1+i];
  265. @@ -973,6 +1042,7 @@ mpn_div_qr_pi1 (mp_ptr qp,
  266. if (qp)
  267. qp[i] = q;
  268. }
  269. + while (--i >= 0);
  270. np[dn - 1] = n1;
  271. }
  272. @@ -994,7 +1064,9 @@ mpn_div_qr_preinv (mp_ptr qp, mp_ptr np, mp_size_t nn,
  273. mp_limb_t nh;
  274. unsigned shift;
  275. - assert (dp[dn-1] & GMP_LIMB_HIGHBIT);
  276. + assert (inv->d1 == dp[dn-1]);
  277. + assert (inv->d0 == dp[dn-2]);
  278. + assert ((inv->d1 & GMP_LIMB_HIGHBIT) != 0);
  279. shift = inv->shift;
  280. if (shift > 0)
  281. @@ -1002,9 +1074,6 @@ mpn_div_qr_preinv (mp_ptr qp, mp_ptr np, mp_size_t nn,
  282. else
  283. nh = 0;
  284. - assert (inv->d1 == dp[dn-1]);
  285. - assert (inv->d0 == dp[dn-2]);
  286. -
  287. mpn_div_qr_pi1 (qp, np, nn, nh, dp, dn, inv->di);
  288. if (shift > 0)
  289. @@ -1238,15 +1307,14 @@ mpn_set_str_other (mp_ptr rp, const unsigned char *sp, size_t sn,
  290. {
  291. mp_size_t rn;
  292. mp_limb_t w;
  293. - unsigned first;
  294. unsigned k;
  295. size_t j;
  296. - first = 1 + (sn - 1) % info->exp;
  297. + k = 1 + (sn - 1) % info->exp;
  298. j = 0;
  299. w = sp[j++];
  300. - for (k = 1; k < first; k++)
  301. + for (; --k > 0; )
  302. w = w * b + sp[j++];
  303. rp[0] = w;
  304. @@ -1300,7 +1368,7 @@ mpz_init (mpz_t r)
  305. }
  306. /* The utility of this function is a bit limited, since many functions
  307. - assings the result variable using mpz_swap. */
  308. + assigns the result variable using mpz_swap. */
  309. void
  310. mpz_init2 (mpz_t r, mp_bitcnt_t bits)
  311. {
  312. @@ -1422,7 +1490,7 @@ mpz_fits_ulong_p (const mpz_t u)
  313. {
  314. mp_size_t us = u->_mp_size;
  315. - return us == 0 || us == 1;
  316. + return (us == (us > 0));
  317. }
  318. long int
  319. @@ -1459,6 +1527,48 @@ mpz_getlimbn (const mpz_t u, mp_size_t n)
  320. return 0;
  321. }
  322. +void
  323. +mpz_realloc2 (mpz_t x, mp_bitcnt_t n)
  324. +{
  325. + mpz_realloc (x, 1 + (n - (n != 0)) / GMP_LIMB_BITS);
  326. +}
  327. +
  328. +mp_srcptr
  329. +mpz_limbs_read (mpz_srcptr x)
  330. +{
  331. + return x->_mp_d;;
  332. +}
  333. +
  334. +mp_ptr
  335. +mpz_limbs_modify (mpz_t x, mp_size_t n)
  336. +{
  337. + assert (n > 0);
  338. + return MPZ_REALLOC (x, n);
  339. +}
  340. +
  341. +mp_ptr
  342. +mpz_limbs_write (mpz_t x, mp_size_t n)
  343. +{
  344. + return mpz_limbs_modify (x, n);
  345. +}
  346. +
  347. +void
  348. +mpz_limbs_finish (mpz_t x, mp_size_t xs)
  349. +{
  350. + mp_size_t xn;
  351. + xn = mpn_normalized_size (x->_mp_d, GMP_ABS (xs));
  352. + x->_mp_size = xs < 0 ? -xn : xn;
  353. +}
  354. +
  355. +mpz_srcptr
  356. +mpz_roinit_n (mpz_t x, mp_srcptr xp, mp_size_t xs)
  357. +{
  358. + x->_mp_alloc = 0;
  359. + x->_mp_d = (mp_ptr) xp;
  360. + mpz_limbs_finish (x, xs);
  361. + return x;
  362. +}
  363. +
  364. /* Conversions and comparison to double. */
  365. void
  366. @@ -1473,19 +1583,15 @@ mpz_set_d (mpz_t r, double x)
  367. /* x != x is true when x is a NaN, and x == x * 0.5 is true when x is
  368. zero or infinity. */
  369. - if (x == 0.0 || x != x || x == x * 0.5)
  370. + if (x != x || x == x * 0.5)
  371. {
  372. r->_mp_size = 0;
  373. return;
  374. }
  375. - if (x < 0.0)
  376. - {
  377. - x = - x;
  378. - sign = 1;
  379. - }
  380. - else
  381. - sign = 0;
  382. + sign = x < 0.0 ;
  383. + if (sign)
  384. + x = - x;
  385. if (x < 1.0)
  386. {
  387. @@ -1502,8 +1608,9 @@ mpz_set_d (mpz_t r, double x)
  388. f = (mp_limb_t) x;
  389. x -= f;
  390. assert (x < 1.0);
  391. - rp[rn-1] = f;
  392. - for (i = rn-1; i-- > 0; )
  393. + i = rn-1;
  394. + rp[i] = f;
  395. + while (--i >= 0)
  396. {
  397. x = B * x;
  398. f = (mp_limb_t) x;
  399. @@ -1611,12 +1718,7 @@ mpz_sgn (const mpz_t u)
  400. {
  401. mp_size_t usize = u->_mp_size;
  402. - if (usize > 0)
  403. - return 1;
  404. - else if (usize < 0)
  405. - return -1;
  406. - else
  407. - return 0;
  408. + return (usize > 0) - (usize < 0);
  409. }
  410. int
  411. @@ -1635,10 +1737,9 @@ mpz_cmp_si (const mpz_t u, long v)
  412. mp_limb_t ul = u->_mp_d[0];
  413. if ((mp_limb_t)GMP_NEG_CAST (unsigned long int, v) < ul)
  414. return -1;
  415. - else if ( (mp_limb_t)GMP_NEG_CAST (unsigned long int, v) > ul)
  416. - return 1;
  417. + else
  418. + return (mp_limb_t)GMP_NEG_CAST (unsigned long int, v) > ul;
  419. }
  420. - return 0;
  421. }
  422. int
  423. @@ -1653,12 +1754,8 @@ mpz_cmp_ui (const mpz_t u, unsigned long v)
  424. else
  425. {
  426. mp_limb_t ul = (usize > 0) ? u->_mp_d[0] : 0;
  427. - if (ul > v)
  428. - return 1;
  429. - else if (ul < v)
  430. - return -1;
  431. + return (ul > v) - (ul < v);
  432. }
  433. - return 0;
  434. }
  435. int
  436. @@ -1667,16 +1764,12 @@ mpz_cmp (const mpz_t a, const mpz_t b)
  437. mp_size_t asize = a->_mp_size;
  438. mp_size_t bsize = b->_mp_size;
  439. - if (asize > bsize)
  440. - return 1;
  441. - else if (asize < bsize)
  442. - return -1;
  443. - else if (asize > 0)
  444. + if (asize != bsize)
  445. + return (asize < bsize) ? -1 : 1;
  446. + else if (asize >= 0)
  447. return mpn_cmp (a->_mp_d, b->_mp_d, asize);
  448. - else if (asize < 0)
  449. - return -mpn_cmp (a->_mp_d, b->_mp_d, -asize);
  450. else
  451. - return 0;
  452. + return mpn_cmp (b->_mp_d, a->_mp_d, -asize);
  453. }
  454. int
  455. @@ -1690,12 +1783,7 @@ mpz_cmpabs_ui (const mpz_t u, unsigned long v)
  456. ul = (un == 1) ? u->_mp_d[0] : 0;
  457. - if (ul > v)
  458. - return 1;
  459. - else if (ul < v)
  460. - return -1;
  461. - else
  462. - return 0;
  463. + return (ul > v) - (ul < v);
  464. }
  465. int
  466. @@ -1753,7 +1841,7 @@ mpz_abs_add_ui (mpz_t r, const mpz_t a, unsigned long b)
  467. cy = mpn_add_1 (rp, a->_mp_d, an, b);
  468. rp[an] = cy;
  469. - an += (cy > 0);
  470. + an += cy;
  471. return an;
  472. }
  473. @@ -1815,20 +1903,21 @@ mpz_abs_add (mpz_t r, const mpz_t a, const mpz_t b)
  474. {
  475. mp_size_t an = GMP_ABS (a->_mp_size);
  476. mp_size_t bn = GMP_ABS (b->_mp_size);
  477. - mp_size_t rn;
  478. mp_ptr rp;
  479. mp_limb_t cy;
  480. - rn = GMP_MAX (an, bn);
  481. - rp = MPZ_REALLOC (r, rn + 1);
  482. - if (an >= bn)
  483. - cy = mpn_add (rp, a->_mp_d, an, b->_mp_d, bn);
  484. - else
  485. - cy = mpn_add (rp, b->_mp_d, bn, a->_mp_d, an);
  486. + if (an < bn)
  487. + {
  488. + MPZ_SRCPTR_SWAP (a, b);
  489. + MP_SIZE_T_SWAP (an, bn);
  490. + }
  491. - rp[rn] = cy;
  492. + rp = MPZ_REALLOC (r, an + 1);
  493. + cy = mpn_add (rp, a->_mp_d, an, b->_mp_d, bn);
  494. +
  495. + rp[an] = cy;
  496. - return rn + (cy > 0);
  497. + return an + cy;
  498. }
  499. static mp_size_t
  500. @@ -1899,31 +1988,26 @@ mpz_mul_si (mpz_t r, const mpz_t u, long int v)
  501. void
  502. mpz_mul_ui (mpz_t r, const mpz_t u, unsigned long int v)
  503. {
  504. - mp_size_t un;
  505. - mpz_t t;
  506. + mp_size_t un, us;
  507. mp_ptr tp;
  508. mp_limb_t cy;
  509. - un = GMP_ABS (u->_mp_size);
  510. + us = u->_mp_size;
  511. - if (un == 0 || v == 0)
  512. + if (us == 0 || v == 0)
  513. {
  514. r->_mp_size = 0;
  515. return;
  516. }
  517. - mpz_init2 (t, (un + 1) * GMP_LIMB_BITS);
  518. + un = GMP_ABS (us);
  519. - tp = t->_mp_d;
  520. + tp = MPZ_REALLOC (r, un + 1);
  521. cy = mpn_mul_1 (tp, u->_mp_d, un, v);
  522. tp[un] = cy;
  523. - t->_mp_size = un + (cy > 0);
  524. - if (u->_mp_size < 0)
  525. - t->_mp_size = - t->_mp_size;
  526. -
  527. - mpz_swap (r, t);
  528. - mpz_clear (t);
  529. + un += (cy > 0);
  530. + r->_mp_size = (us < 0) ? - un : un;
  531. }
  532. void
  533. @@ -1934,8 +2018,8 @@ mpz_mul (mpz_t r, const mpz_t u, const mpz_t v)
  534. mpz_t t;
  535. mp_ptr tp;
  536. - un = GMP_ABS (u->_mp_size);
  537. - vn = GMP_ABS (v->_mp_size);
  538. + un = u->_mp_size;
  539. + vn = v->_mp_size;
  540. if (un == 0 || vn == 0)
  541. {
  542. @@ -1943,7 +2027,10 @@ mpz_mul (mpz_t r, const mpz_t u, const mpz_t v)
  543. return;
  544. }
  545. - sign = (u->_mp_size ^ v->_mp_size) < 0;
  546. + sign = (un ^ vn) < 0;
  547. +
  548. + un = GMP_ABS (un);
  549. + vn = GMP_ABS (vn);
  550. mpz_init2 (t, (un + vn) * GMP_LIMB_BITS);
  551. @@ -1996,6 +2083,46 @@ mpz_mul_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bits)
  552. r->_mp_size = (u->_mp_size < 0) ? - rn : rn;
  553. }
  554. +void
  555. +mpz_addmul_ui (mpz_t r, const mpz_t u, unsigned long int v)
  556. +{
  557. + mpz_t t;
  558. + mpz_init (t);
  559. + mpz_mul_ui (t, u, v);
  560. + mpz_add (r, r, t);
  561. + mpz_clear (t);
  562. +}
  563. +
  564. +void
  565. +mpz_submul_ui (mpz_t r, const mpz_t u, unsigned long int v)
  566. +{
  567. + mpz_t t;
  568. + mpz_init (t);
  569. + mpz_mul_ui (t, u, v);
  570. + mpz_sub (r, r, t);
  571. + mpz_clear (t);
  572. +}
  573. +
  574. +void
  575. +mpz_addmul (mpz_t r, const mpz_t u, const mpz_t v)
  576. +{
  577. + mpz_t t;
  578. + mpz_init (t);
  579. + mpz_mul (t, u, v);
  580. + mpz_add (r, r, t);
  581. + mpz_clear (t);
  582. +}
  583. +
  584. +void
  585. +mpz_submul (mpz_t r, const mpz_t u, const mpz_t v)
  586. +{
  587. + mpz_t t;
  588. + mpz_init (t);
  589. + mpz_mul (t, u, v);
  590. + mpz_sub (r, r, t);
  591. + mpz_clear (t);
  592. +}
  593. +
  594. /* MPZ division */
  595. enum mpz_div_round_mode { GMP_DIV_FLOOR, GMP_DIV_CEIL, GMP_DIV_TRUNC };
  596. @@ -2060,8 +2187,7 @@ mpz_div_qr (mpz_t q, mpz_t r,
  597. mp_size_t qn, rn;
  598. mpz_t tq, tr;
  599. - mpz_init (tr);
  600. - mpz_set (tr, n);
  601. + mpz_init_set (tr, n);
  602. np = tr->_mp_d;
  603. qn = nn - dn + 1;
  604. @@ -2171,10 +2297,7 @@ mpz_tdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
  605. void
  606. mpz_mod (mpz_t r, const mpz_t n, const mpz_t d)
  607. {
  608. - if (d->_mp_size >= 0)
  609. - mpz_div_qr (NULL, r, n, d, GMP_DIV_FLOOR);
  610. - else
  611. - mpz_div_qr (NULL, r, n, d, GMP_DIV_CEIL);
  612. + mpz_div_qr (NULL, r, n, d, d->_mp_size >= 0 ? GMP_DIV_FLOOR : GMP_DIV_CEIL);
  613. }
  614. static void
  615. @@ -2184,7 +2307,7 @@ mpz_div_q_2exp (mpz_t q, const mpz_t u, mp_bitcnt_t bit_index,
  616. mp_size_t un, qn;
  617. mp_size_t limb_cnt;
  618. mp_ptr qp;
  619. - mp_limb_t adjust;
  620. + int adjust;
  621. un = u->_mp_size;
  622. if (un == 0)
  623. @@ -2226,7 +2349,8 @@ mpz_div_q_2exp (mpz_t q, const mpz_t u, mp_bitcnt_t bit_index,
  624. q->_mp_size = qn;
  625. - mpz_add_ui (q, q, adjust);
  626. + if (adjust)
  627. + mpz_add_ui (q, q, 1);
  628. if (un < 0)
  629. mpz_neg (q, q);
  630. }
  631. @@ -2303,7 +2427,7 @@ mpz_div_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bit_index,
  632. {
  633. /* r > 0, need to flip sign. */
  634. rp[i] = ~rp[i] + 1;
  635. - for (i++; i < rn; i++)
  636. + while (++i < rn)
  637. rp[i] = ~rp[i];
  638. rp[rn-1] &= mask;
  639. @@ -2366,6 +2490,24 @@ mpz_divisible_p (const mpz_t n, const mpz_t d)
  640. return mpz_div_qr (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
  641. }
  642. +int
  643. +mpz_congruent_p (const mpz_t a, const mpz_t b, const mpz_t m)
  644. +{
  645. + mpz_t t;
  646. + int res;
  647. +
  648. + /* a == b (mod 0) iff a == b */
  649. + if (mpz_sgn (m) == 0)
  650. + return (mpz_cmp (a, b) == 0);
  651. +
  652. + mpz_init (t);
  653. + mpz_sub (t, a, b);
  654. + res = mpz_divisible_p (t, m);
  655. + mpz_clear (t);
  656. +
  657. + return res;
  658. +}
  659. +
  660. static unsigned long
  661. mpz_div_qr_ui (mpz_t q, mpz_t r,
  662. const mpz_t n, unsigned long d, enum mpz_div_round_mode mode)
  663. @@ -2579,32 +2721,16 @@ mpz_gcd_ui (mpz_t g, const mpz_t u, unsigned long v)
  664. }
  665. static mp_bitcnt_t
  666. -mpz_make_odd (mpz_t r, const mpz_t u)
  667. +mpz_make_odd (mpz_t r)
  668. {
  669. - mp_size_t un, rn, i;
  670. - mp_ptr rp;
  671. - unsigned shift;
  672. -
  673. - un = GMP_ABS (u->_mp_size);
  674. - assert (un > 0);
  675. + mp_bitcnt_t shift;
  676. - for (i = 0; u->_mp_d[i] == 0; i++)
  677. - ;
  678. -
  679. - gmp_ctz (shift, u->_mp_d[i]);
  680. -
  681. - rn = un - i;
  682. - rp = MPZ_REALLOC (r, rn);
  683. - if (shift > 0)
  684. - {
  685. - mpn_rshift (rp, u->_mp_d + i, rn, shift);
  686. - rn -= (rp[rn-1] == 0);
  687. - }
  688. - else
  689. - mpn_copyi (rp, u->_mp_d + i, rn);
  690. + assert (r->_mp_size > 0);
  691. + /* Count trailing zeros, equivalent to mpn_scan1, because we know that there is a 1 */
  692. + shift = mpn_common_scan (r->_mp_d[0], 0, r->_mp_d, 0, 0);
  693. + mpz_tdiv_q_2exp (r, r, shift);
  694. - r->_mp_size = rn;
  695. - return i * GMP_LIMB_BITS + shift;
  696. + return shift;
  697. }
  698. void
  699. @@ -2627,8 +2753,10 @@ mpz_gcd (mpz_t g, const mpz_t u, const mpz_t v)
  700. mpz_init (tu);
  701. mpz_init (tv);
  702. - uz = mpz_make_odd (tu, u);
  703. - vz = mpz_make_odd (tv, v);
  704. + mpz_abs (tu, u);
  705. + uz = mpz_make_odd (tu);
  706. + mpz_abs (tv, v);
  707. + vz = mpz_make_odd (tv);
  708. gz = GMP_MIN (uz, vz);
  709. if (tu->_mp_size < tv->_mp_size)
  710. @@ -2644,7 +2772,7 @@ mpz_gcd (mpz_t g, const mpz_t u, const mpz_t v)
  711. {
  712. int c;
  713. - mpz_make_odd (tu, tu);
  714. + mpz_make_odd (tu);
  715. c = mpz_cmp (tu, tv);
  716. if (c == 0)
  717. {
  718. @@ -2706,8 +2834,10 @@ mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, const mpz_t u, const mpz_t v)
  719. mpz_init (t0);
  720. mpz_init (t1);
  721. - uz = mpz_make_odd (tu, u);
  722. - vz = mpz_make_odd (tv, v);
  723. + mpz_abs (tu, u);
  724. + uz = mpz_make_odd (tu);
  725. + mpz_abs (tv, v);
  726. + vz = mpz_make_odd (tv);
  727. gz = GMP_MIN (uz, vz);
  728. uz -= gz;
  729. @@ -2755,7 +2885,7 @@ mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, const mpz_t u, const mpz_t v)
  730. if (tu->_mp_size > 0)
  731. {
  732. mp_bitcnt_t shift;
  733. - shift = mpz_make_odd (tu, tu);
  734. + shift = mpz_make_odd (tu);
  735. mpz_mul_2exp (t0, t0, shift);
  736. mpz_mul_2exp (s0, s0, shift);
  737. power += shift;
  738. @@ -2778,7 +2908,7 @@ mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, const mpz_t u, const mpz_t v)
  739. mpz_add (t0, t0, t1);
  740. mpz_add (s0, s0, s1);
  741. - shift = mpz_make_odd (tv, tv);
  742. + shift = mpz_make_odd (tv);
  743. mpz_mul_2exp (t1, t1, shift);
  744. mpz_mul_2exp (s1, s1, shift);
  745. }
  746. @@ -2788,7 +2918,7 @@ mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, const mpz_t u, const mpz_t v)
  747. mpz_add (t1, t0, t1);
  748. mpz_add (s1, s0, s1);
  749. - shift = mpz_make_odd (tu, tu);
  750. + shift = mpz_make_odd (tu);
  751. mpz_mul_2exp (t0, t0, shift);
  752. mpz_mul_2exp (s0, s0, shift);
  753. }
  754. @@ -2926,12 +3056,16 @@ mpz_pow_ui (mpz_t r, const mpz_t b, unsigned long e)
  755. mpz_t tr;
  756. mpz_init_set_ui (tr, 1);
  757. - for (bit = GMP_ULONG_HIGHBIT; bit > 0; bit >>= 1)
  758. + bit = GMP_ULONG_HIGHBIT;
  759. + do
  760. {
  761. mpz_mul (tr, tr, tr);
  762. if (e & bit)
  763. mpz_mul (tr, tr, b);
  764. + bit >>= 1;
  765. }
  766. + while (bit > 0);
  767. +
  768. mpz_swap (r, tr);
  769. mpz_clear (tr);
  770. }
  771. @@ -2987,7 +3121,7 @@ mpz_powm (mpz_t r, const mpz_t b, const mpz_t e, const mpz_t m)
  772. if (e->_mp_size < 0)
  773. {
  774. if (!mpz_invert (base, b, m))
  775. - gmp_die ("mpz_powm: Negative exponent and non-invertibe base.");
  776. + gmp_die ("mpz_powm: Negative exponent and non-invertible base.");
  777. }
  778. else
  779. {
  780. @@ -3019,7 +3153,8 @@ mpz_powm (mpz_t r, const mpz_t b, const mpz_t e, const mpz_t m)
  781. mp_limb_t w = e->_mp_d[en];
  782. mp_limb_t bit;
  783. - for (bit = GMP_LIMB_HIGHBIT; bit > 0; bit >>= 1)
  784. + bit = GMP_LIMB_HIGHBIT;
  785. + do
  786. {
  787. mpz_mul (tr, tr, tr);
  788. if (w & bit)
  789. @@ -3029,7 +3164,9 @@ mpz_powm (mpz_t r, const mpz_t b, const mpz_t e, const mpz_t m)
  790. mpn_div_qr_preinv (NULL, tr->_mp_d, tr->_mp_size, mp, mn, &minv);
  791. tr->_mp_size = mpn_normalized_size (tr->_mp_d, mn);
  792. }
  793. + bit >>= 1;
  794. }
  795. + while (bit > 0);
  796. }
  797. /* Final reduction */
  798. @@ -3064,21 +3201,26 @@ mpz_rootrem (mpz_t x, mpz_t r, const mpz_t y, unsigned long z)
  799. mpz_t t, u;
  800. sgn = y->_mp_size < 0;
  801. - if (sgn && (z & 1) == 0)
  802. + if ((~z & sgn) != 0)
  803. gmp_die ("mpz_rootrem: Negative argument, with even root.");
  804. if (z == 0)
  805. gmp_die ("mpz_rootrem: Zeroth root.");
  806. if (mpz_cmpabs_ui (y, 1) <= 0) {
  807. - mpz_set (x, y);
  808. + if (x)
  809. + mpz_set (x, y);
  810. if (r)
  811. r->_mp_size = 0;
  812. return;
  813. }
  814. - mpz_init (t);
  815. mpz_init (u);
  816. - mpz_setbit (t, mpz_sizeinbase (y, 2) / z + 1);
  817. + {
  818. + mp_bitcnt_t tb;
  819. + tb = mpz_sizeinbase (y, 2) / z + 1;
  820. + mpz_init2 (t, tb);
  821. + mpz_setbit (t, tb);
  822. + }
  823. if (z == 2) /* simplify sqrt loop: z-1 == 1 */
  824. do {
  825. @@ -3110,7 +3252,8 @@ mpz_rootrem (mpz_t x, mpz_t r, const mpz_t y, unsigned long z)
  826. mpz_pow_ui (t, u, z);
  827. mpz_sub (r, y, t);
  828. }
  829. - mpz_swap (x, u);
  830. + if (x)
  831. + mpz_swap (x, u);
  832. mpz_clear (u);
  833. mpz_clear (t);
  834. }
  835. @@ -3142,19 +3285,56 @@ mpz_sqrt (mpz_t s, const mpz_t u)
  836. mpz_rootrem (s, NULL, u, 2);
  837. }
  838. +int
  839. +mpz_perfect_square_p (const mpz_t u)
  840. +{
  841. + if (u->_mp_size <= 0)
  842. + return (u->_mp_size == 0);
  843. + else
  844. + return mpz_root (NULL, u, 2);
  845. +}
  846. +
  847. +int
  848. +mpn_perfect_square_p (mp_srcptr p, mp_size_t n)
  849. +{
  850. + mpz_t t;
  851. +
  852. + assert (n > 0);
  853. + assert (p [n-1] != 0);
  854. + return mpz_root (NULL, mpz_roinit_n (t, p, n), 2);
  855. +}
  856. +
  857. +mp_size_t
  858. +mpn_sqrtrem (mp_ptr sp, mp_ptr rp, mp_srcptr p, mp_size_t n)
  859. +{
  860. + mpz_t s, r, u;
  861. + mp_size_t res;
  862. +
  863. + assert (n > 0);
  864. + assert (p [n-1] != 0);
  865. +
  866. + mpz_init (r);
  867. + mpz_init (s);
  868. + mpz_rootrem (s, r, mpz_roinit_n (u, p, n), 2);
  869. +
  870. + assert (s->_mp_size == (n+1)/2);
  871. + mpn_copyd (sp, s->_mp_d, s->_mp_size);
  872. + mpz_clear (s);
  873. + res = r->_mp_size;
  874. + if (rp)
  875. + mpn_copyd (rp, r->_mp_d, res);
  876. + mpz_clear (r);
  877. + return res;
  878. +}
  879. /* Combinatorics */
  880. void
  881. mpz_fac_ui (mpz_t x, unsigned long n)
  882. {
  883. - if (n < 2) {
  884. - mpz_set_ui (x, 1);
  885. - return;
  886. - }
  887. - mpz_set_ui (x, n);
  888. - for (;--n > 1;)
  889. - mpz_mul_ui (x, x, n);
  890. + mpz_set_ui (x, n + (n == 0));
  891. + for (;n > 2;)
  892. + mpz_mul_ui (x, x, --n);
  893. }
  894. void
  895. @@ -3162,25 +3342,120 @@ mpz_bin_uiui (mpz_t r, unsigned long n, unsigned long k)
  896. {
  897. mpz_t t;
  898. - if (k > n) {
  899. - r->_mp_size = 0;
  900. - return;
  901. - }
  902. - mpz_fac_ui (r, n);
  903. + mpz_set_ui (r, k <= n);
  904. +
  905. + if (k > (n >> 1))
  906. + k = (k <= n) ? n - k : 0;
  907. +
  908. mpz_init (t);
  909. mpz_fac_ui (t, k);
  910. - mpz_divexact (r, r, t);
  911. - mpz_fac_ui (t, n - k);
  912. +
  913. + for (; k > 0; k--)
  914. + mpz_mul_ui (r, r, n--);
  915. +
  916. mpz_divexact (r, r, t);
  917. mpz_clear (t);
  918. }
  919. +/* Primality testing */
  920. +static int
  921. +gmp_millerrabin (const mpz_t n, const mpz_t nm1, mpz_t y,
  922. + const mpz_t q, mp_bitcnt_t k)
  923. +{
  924. + mp_bitcnt_t i;
  925. +
  926. + /* Caller must initialize y to the base. */
  927. + mpz_powm (y, y, q, n);
  928. +
  929. + if (mpz_cmp_ui (y, 1) == 0 || mpz_cmp (y, nm1) == 0)
  930. + return 1;
  931. +
  932. + for (i = 1; i < k; i++)
  933. + {
  934. + mpz_powm_ui (y, y, 2, n);
  935. + if (mpz_cmp (y, nm1) == 0)
  936. + return 1;
  937. + if (mpz_cmp_ui (y, 1) == 0)
  938. + return 0;
  939. + }
  940. + return 0;
  941. +}
  942. +
  943. +/* This product is 0xc0cfd797, and fits in 32 bits. */
  944. +#define GMP_PRIME_PRODUCT \
  945. + (3UL*5UL*7UL*11UL*13UL*17UL*19UL*23UL*29UL)
  946. +
  947. +/* Bit (p+1)/2 is set, for each odd prime <= 61 */
  948. +#define GMP_PRIME_MASK 0xc96996dcUL
  949. +
  950. +int
  951. +mpz_probab_prime_p (const mpz_t n, int reps)
  952. +{
  953. + mpz_t nm1;
  954. + mpz_t q;
  955. + mpz_t y;
  956. + mp_bitcnt_t k;
  957. + int is_prime;
  958. + int j;
  959. +
  960. + /* Note that we use the absolute value of n only, for compatibility
  961. + with the real GMP. */
  962. + if (mpz_even_p (n))
  963. + return (mpz_cmpabs_ui (n, 2) == 0) ? 2 : 0;
  964. +
  965. + /* Above test excludes n == 0 */
  966. + assert (n->_mp_size != 0);
  967. +
  968. + if (mpz_cmpabs_ui (n, 64) < 0)
  969. + return (GMP_PRIME_MASK >> (n->_mp_d[0] >> 1)) & 2;
  970. +
  971. + if (mpz_gcd_ui (NULL, n, GMP_PRIME_PRODUCT) != 1)
  972. + return 0;
  973. +
  974. + /* All prime factors are >= 31. */
  975. + if (mpz_cmpabs_ui (n, 31*31) < 0)
  976. + return 2;
  977. +
  978. + /* Use Miller-Rabin, with a deterministic sequence of bases, a[j] =
  979. + j^2 + j + 41 using Euler's polynomial. We potentially stop early,
  980. + if a[j] >= n - 1. Since n >= 31*31, this can happen only if reps >
  981. + 30 (a[30] == 971 > 31*31 == 961). */
  982. +
  983. + mpz_init (nm1);
  984. + mpz_init (q);
  985. + mpz_init (y);
  986. +
  987. + /* Find q and k, where q is odd and n = 1 + 2**k * q. */
  988. + mpz_abs (nm1, n);
  989. + mpz_sub_ui (nm1, nm1, 1);
  990. + k = mpz_scan1 (nm1, 0);
  991. + mpz_tdiv_q_2exp (q, nm1, k);
  992. +
  993. + for (j = 0, is_prime = 1; is_prime && j < reps; j++)
  994. + {
  995. + mpz_set_ui (y, (unsigned long) j*j+j+41);
  996. + if (mpz_cmp (y, nm1) >= 0)
  997. + {
  998. + /* Don't try any further bases. */
  999. + assert (j >= 30);
  1000. + break;
  1001. + }
  1002. + is_prime &= gmp_millerrabin (n, nm1, y, q, k);
  1003. + }
  1004. + mpz_clear (nm1);
  1005. + mpz_clear (q);
  1006. + mpz_clear (y);
  1007. +
  1008. + return is_prime;
  1009. +}
  1010. +
  1011. +
  1012. /* Logical operations and bit manipulation. */
  1013. /* Numbers are treated as if represented in two's complement (and
  1014. infinitely sign extended). For a negative values we get the two's
  1015. - complement from -x = ~x + 1, where ~ is bitwise complementt.
  1016. + complement from -x = ~x + 1, where ~ is bitwise complement.
  1017. Negation transforms
  1018. xxxx10...0
  1019. @@ -3374,7 +3649,8 @@ mpz_and (mpz_t r, const mpz_t u, const mpz_t v)
  1020. up = u->_mp_d;
  1021. vp = v->_mp_d;
  1022. - for (i = 0; i < vn; i++)
  1023. + i = 0;
  1024. + do
  1025. {
  1026. ul = (up[i] ^ ux) + uc;
  1027. uc = ul < uc;
  1028. @@ -3386,6 +3662,7 @@ mpz_and (mpz_t r, const mpz_t u, const mpz_t v)
  1029. rc = rl < rc;
  1030. rp[i] = rl;
  1031. }
  1032. + while (++i < vn);
  1033. assert (vc == 0);
  1034. for (; i < rn; i++)
  1035. @@ -3445,7 +3722,8 @@ mpz_ior (mpz_t r, const mpz_t u, const mpz_t v)
  1036. up = u->_mp_d;
  1037. vp = v->_mp_d;
  1038. - for (i = 0; i < vn; i++)
  1039. + i = 0;
  1040. + do
  1041. {
  1042. ul = (up[i] ^ ux) + uc;
  1043. uc = ul < uc;
  1044. @@ -3457,6 +3735,7 @@ mpz_ior (mpz_t r, const mpz_t u, const mpz_t v)
  1045. rc = rl < rc;
  1046. rp[i] = rl;
  1047. }
  1048. + while (++i < vn);
  1049. assert (vc == 0);
  1050. for (; i < rn; i++)
  1051. @@ -3512,7 +3791,8 @@ mpz_xor (mpz_t r, const mpz_t u, const mpz_t v)
  1052. up = u->_mp_d;
  1053. vp = v->_mp_d;
  1054. - for (i = 0; i < vn; i++)
  1055. + i = 0;
  1056. + do
  1057. {
  1058. ul = (up[i] ^ ux) + uc;
  1059. uc = ul < uc;
  1060. @@ -3524,6 +3804,7 @@ mpz_xor (mpz_t r, const mpz_t u, const mpz_t v)
  1061. rc = rl < rc;
  1062. rp[i] = rl;
  1063. }
  1064. + while (++i < vn);
  1065. assert (vc == 0);
  1066. for (; i < un; i++)
  1067. @@ -3561,20 +3842,28 @@ gmp_popcount_limb (mp_limb_t x)
  1068. }
  1069. mp_bitcnt_t
  1070. -mpz_popcount (const mpz_t u)
  1071. +mpn_popcount (mp_srcptr p, mp_size_t n)
  1072. {
  1073. - mp_size_t un, i;
  1074. + mp_size_t i;
  1075. mp_bitcnt_t c;
  1076. + for (c = 0, i = 0; i < n; i++)
  1077. + c += gmp_popcount_limb (p[i]);
  1078. +
  1079. + return c;
  1080. +}
  1081. +
  1082. +mp_bitcnt_t
  1083. +mpz_popcount (const mpz_t u)
  1084. +{
  1085. + mp_size_t un;
  1086. +
  1087. un = u->_mp_size;
  1088. if (un < 0)
  1089. return ~(mp_bitcnt_t) 0;
  1090. - for (c = 0, i = 0; i < un; i++)
  1091. - c += gmp_popcount_limb (u->_mp_d[i]);
  1092. -
  1093. - return c;
  1094. + return mpn_popcount (u->_mp_d, un);
  1095. }
  1096. mp_bitcnt_t
  1097. @@ -3591,16 +3880,13 @@ mpz_hamdist (const mpz_t u, const mpz_t v)
  1098. if ( (un ^ vn) < 0)
  1099. return ~(mp_bitcnt_t) 0;
  1100. - if (un < 0)
  1101. + comp = - (uc = vc = (un < 0));
  1102. + if (uc)
  1103. {
  1104. assert (vn < 0);
  1105. un = -un;
  1106. vn = -vn;
  1107. - uc = vc = 1;
  1108. - comp = - (mp_limb_t) 1;
  1109. }
  1110. - else
  1111. - uc = vc = comp = 0;
  1112. up = u->_mp_d;
  1113. vp = v->_mp_d;
  1114. @@ -3636,10 +3922,8 @@ mpz_scan1 (const mpz_t u, mp_bitcnt_t starting_bit)
  1115. {
  1116. mp_ptr up;
  1117. mp_size_t us, un, i;
  1118. - mp_limb_t limb, ux, uc;
  1119. - unsigned cnt;
  1120. + mp_limb_t limb, ux;
  1121. - up = u->_mp_d;
  1122. us = u->_mp_size;
  1123. un = GMP_ABS (us);
  1124. i = starting_bit / GMP_LIMB_BITS;
  1125. @@ -3649,36 +3933,24 @@ mpz_scan1 (const mpz_t u, mp_bitcnt_t starting_bit)
  1126. if (i >= un)
  1127. return (us >= 0 ? ~(mp_bitcnt_t) 0 : starting_bit);
  1128. - if (us < 0)
  1129. - {
  1130. - ux = GMP_LIMB_MAX;
  1131. - uc = mpn_zero_p (up, i);
  1132. - }
  1133. - else
  1134. - ux = uc = 0;
  1135. -
  1136. - limb = (ux ^ up[i]) + uc;
  1137. - uc = limb < uc;
  1138. -
  1139. - /* Mask to 0 all bits before starting_bit, thus ignoring them. */
  1140. - limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
  1141. + up = u->_mp_d;
  1142. + ux = 0;
  1143. + limb = up[i];
  1144. - while (limb == 0)
  1145. + if (starting_bit != 0)
  1146. {
  1147. - i++;
  1148. - if (i == un)
  1149. + if (us < 0)
  1150. {
  1151. - assert (uc == 0);
  1152. - /* For the u > 0 case, this can happen only for the first
  1153. - masked limb. For the u < 0 case, it happens when the
  1154. - highest limbs of the absolute value are all ones. */
  1155. - return (us >= 0 ? ~(mp_bitcnt_t) 0 : un * GMP_LIMB_BITS);
  1156. + ux = mpn_zero_p (up, i);
  1157. + limb = ~ limb + ux;
  1158. + ux = - (mp_limb_t) (limb >= ux);
  1159. }
  1160. - limb = (ux ^ up[i]) + uc;
  1161. - uc = limb < uc;
  1162. +
  1163. + /* Mask to 0 all bits before starting_bit, thus ignoring them. */
  1164. + limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
  1165. }
  1166. - gmp_ctz (cnt, limb);
  1167. - return (mp_bitcnt_t) i * GMP_LIMB_BITS + cnt;
  1168. +
  1169. + return mpn_common_scan (limb, i, up, un, ux);
  1170. }
  1171. mp_bitcnt_t
  1172. @@ -3686,46 +3958,28 @@ mpz_scan0 (const mpz_t u, mp_bitcnt_t starting_bit)
  1173. {
  1174. mp_ptr up;
  1175. mp_size_t us, un, i;
  1176. - mp_limb_t limb, ux, uc;
  1177. - unsigned cnt;
  1178. + mp_limb_t limb, ux;
  1179. - up = u->_mp_d;
  1180. us = u->_mp_size;
  1181. + ux = - (mp_limb_t) (us >= 0);
  1182. un = GMP_ABS (us);
  1183. i = starting_bit / GMP_LIMB_BITS;
  1184. /* When past end, there's an immediate 0 bit for u>=0, or no 0 bits for
  1185. u<0. Notice this test picks up all cases of u==0 too. */
  1186. if (i >= un)
  1187. - return (us >= 0 ? starting_bit : ~(mp_bitcnt_t) 0);
  1188. + return (ux ? starting_bit : ~(mp_bitcnt_t) 0);
  1189. - if (us < 0)
  1190. - {
  1191. - ux = GMP_LIMB_MAX;
  1192. - uc = mpn_zero_p (up, i);
  1193. - }
  1194. - else
  1195. - ux = uc = 0;
  1196. + up = u->_mp_d;
  1197. + limb = up[i] ^ ux;
  1198. - limb = (ux ^ up[i]) + uc;
  1199. - uc = limb < uc;
  1200. + if (ux == 0)
  1201. + limb -= mpn_zero_p (up, i); /* limb = ~(~limb + zero_p) */
  1202. - /* Mask to 1 all bits before starting_bit, thus ignoring them. */
  1203. - limb |= ((mp_limb_t) 1 << (starting_bit % GMP_LIMB_BITS)) - 1;
  1204. + /* Mask all bits before starting_bit, thus ignoring them. */
  1205. + limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
  1206. - while (limb == GMP_LIMB_MAX)
  1207. - {
  1208. - i++;
  1209. - if (i == un)
  1210. - {
  1211. - assert (uc == 0);
  1212. - return (us >= 0 ? un * GMP_LIMB_BITS : ~(mp_bitcnt_t) 0);
  1213. - }
  1214. - limb = (ux ^ up[i]) + uc;
  1215. - uc = limb < uc;
  1216. - }
  1217. - gmp_ctz (cnt, ~limb);
  1218. - return (mp_bitcnt_t) i * GMP_LIMB_BITS + cnt;
  1219. + return mpn_common_scan (limb, i, up, un, ux);
  1220. }
  1221. @@ -3771,11 +4025,15 @@ mpz_sizeinbase (const mpz_t u, int base)
  1222. mpn_copyi (tp, up, un);
  1223. mpn_div_qr_1_invert (&bi, base);
  1224. - for (ndigits = 0; un > 0; ndigits++)
  1225. + ndigits = 0;
  1226. + do
  1227. {
  1228. + ndigits++;
  1229. mpn_div_qr_1_preinv (tp, tp, un, &bi);
  1230. un -= (tp[un-1] == 0);
  1231. }
  1232. + while (un > 0);
  1233. +
  1234. gmp_free (tp);
  1235. return ndigits;
  1236. }
  1237. @@ -3852,7 +4110,6 @@ mpz_set_str (mpz_t r, const char *sp, int base)
  1238. mp_size_t rn, alloc;
  1239. mp_ptr rp;
  1240. size_t sn;
  1241. - size_t dn;
  1242. int sign;
  1243. unsigned char *dp;
  1244. @@ -3861,13 +4118,8 @@ mpz_set_str (mpz_t r, const char *sp, int base)
  1245. while (isspace( (unsigned char) *sp))
  1246. sp++;
  1247. - if (*sp == '-')
  1248. - {
  1249. - sign = 1;
  1250. - sp++;
  1251. - }
  1252. - else
  1253. - sign = 0;
  1254. + sign = (*sp == '-');
  1255. + sp += sign;
  1256. if (base == 0)
  1257. {
  1258. @@ -3894,7 +4146,7 @@ mpz_set_str (mpz_t r, const char *sp, int base)
  1259. sn = strlen (sp);
  1260. dp = gmp_xalloc (sn + (sn == 0));
  1261. - for (dn = 0; *sp; sp++)
  1262. + for (sn = 0; *sp; sp++)
  1263. {
  1264. unsigned digit;
  1265. @@ -3916,7 +4168,7 @@ mpz_set_str (mpz_t r, const char *sp, int base)
  1266. return -1;
  1267. }
  1268. - dp[dn++] = digit;
  1269. + dp[sn++] = digit;
  1270. }
  1271. bits = mpn_base_power_of_two_p (base);
  1272. @@ -3925,7 +4177,7 @@ mpz_set_str (mpz_t r, const char *sp, int base)
  1273. {
  1274. alloc = (sn * bits + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
  1275. rp = MPZ_REALLOC (r, alloc);
  1276. - rn = mpn_set_str_bits (rp, dp, dn, bits);
  1277. + rn = mpn_set_str_bits (rp, dp, sn, bits);
  1278. }
  1279. else
  1280. {
  1281. @@ -3933,7 +4185,7 @@ mpz_set_str (mpz_t r, const char *sp, int base)
  1282. mpn_get_base_info (&info, base);
  1283. alloc = (sn + info.exp - 1) / info.exp;
  1284. rp = MPZ_REALLOC (r, alloc);
  1285. - rn = mpn_set_str_other (rp, dp, dn, base, &info);
  1286. + rn = mpn_set_str_other (rp, dp, sn, base, &info);
  1287. }
  1288. assert (rn <= alloc);
  1289. gmp_free (dp);
  1290. @@ -3967,14 +4219,9 @@ mpz_out_str (FILE *stream, int base, const mpz_t x)
  1291. static int
  1292. gmp_detect_endian (void)
  1293. {
  1294. - static const int i = 1;
  1295. + static const int i = 2;
  1296. const unsigned char *p = (const unsigned char *) &i;
  1297. - if (*p == 1)
  1298. - /* Little endian */
  1299. - return -1;
  1300. - else
  1301. - /* Big endian */
  1302. - return 1;
  1303. + return 1 - *p;
  1304. }
  1305. /* Import and export. Does not support nails. */
  1306. @@ -4037,29 +4284,22 @@ mpz_import (mpz_t r, size_t count, int order, size_t size, int endian,
  1307. }
  1308. }
  1309. }
  1310. - if (bytes > 0)
  1311. + assert (i + (bytes > 0) == rn);
  1312. + if (limb != 0)
  1313. rp[i++] = limb;
  1314. - assert (i == rn);
  1315. + else
  1316. + i = mpn_normalized_size (rp, i);
  1317. - r->_mp_size = mpn_normalized_size (rp, i);
  1318. + r->_mp_size = i;
  1319. }
  1320. void *
  1321. mpz_export (void *r, size_t *countp, int order, size_t size, int endian,
  1322. size_t nails, const mpz_t u)
  1323. {
  1324. - unsigned char *p;
  1325. - ptrdiff_t word_step;
  1326. - size_t count, k;
  1327. + size_t count;
  1328. mp_size_t un;
  1329. - /* The current (partial) limb. */
  1330. - mp_limb_t limb;
  1331. - /* The number of bytes left to to in this limb. */
  1332. - size_t bytes;
  1333. - /* The index where the limb was read. */
  1334. - mp_size_t i;
  1335. -
  1336. if (nails != 0)
  1337. gmp_die ("mpz_import: Nails not supported.");
  1338. @@ -4067,62 +4307,74 @@ mpz_export (void *r, size_t *countp, int order, size_t size, int endian,
  1339. assert (endian >= -1 && endian <= 1);
  1340. assert (size > 0 || u->_mp_size == 0);
  1341. - un = GMP_ABS (u->_mp_size);
  1342. - if (un == 0)
  1343. - {
  1344. - if (countp)
  1345. - *countp = 0;
  1346. - return r;
  1347. - }
  1348. + un = u->_mp_size;
  1349. + count = 0;
  1350. + if (un != 0)
  1351. + {
  1352. + size_t k;
  1353. + unsigned char *p;
  1354. + ptrdiff_t word_step;
  1355. + /* The current (partial) limb. */
  1356. + mp_limb_t limb;
  1357. + /* The number of bytes left to to in this limb. */
  1358. + size_t bytes;
  1359. + /* The index where the limb was read. */
  1360. + mp_size_t i;
  1361. - /* Count bytes in top limb. */
  1362. - for (limb = u->_mp_d[un-1], k = 0; limb > 0; k++, limb >>= CHAR_BIT)
  1363. - ;
  1364. + un = GMP_ABS (un);
  1365. - assert (k > 0);
  1366. + /* Count bytes in top limb. */
  1367. + limb = u->_mp_d[un-1];
  1368. + assert (limb != 0);
  1369. - count = (k + (un-1) * sizeof (mp_limb_t) + size - 1) / size;
  1370. + k = 0;
  1371. + do {
  1372. + k++; limb >>= CHAR_BIT;
  1373. + } while (limb != 0);
  1374. - if (!r)
  1375. - r = gmp_xalloc (count * size);
  1376. + count = (k + (un-1) * sizeof (mp_limb_t) + size - 1) / size;
  1377. - if (endian == 0)
  1378. - endian = gmp_detect_endian ();
  1379. + if (!r)
  1380. + r = gmp_xalloc (count * size);
  1381. - p = (unsigned char *) r;
  1382. + if (endian == 0)
  1383. + endian = gmp_detect_endian ();
  1384. - word_step = (order != endian) ? 2 * size : 0;
  1385. + p = (unsigned char *) r;
  1386. - /* Process bytes from the least significant end, so point p at the
  1387. - least significant word. */
  1388. - if (order == 1)
  1389. - {
  1390. - p += size * (count - 1);
  1391. - word_step = - word_step;
  1392. - }
  1393. + word_step = (order != endian) ? 2 * size : 0;
  1394. - /* And at least significant byte of that word. */
  1395. - if (endian == 1)
  1396. - p += (size - 1);
  1397. + /* Process bytes from the least significant end, so point p at the
  1398. + least significant word. */
  1399. + if (order == 1)
  1400. + {
  1401. + p += size * (count - 1);
  1402. + word_step = - word_step;
  1403. + }
  1404. - for (bytes = 0, i = 0, k = 0; k < count; k++, p += word_step)
  1405. - {
  1406. - size_t j;
  1407. - for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
  1408. - {
  1409. - if (bytes == 0)
  1410. - {
  1411. - if (i < un)
  1412. - limb = u->_mp_d[i++];
  1413. - bytes = sizeof (mp_limb_t);
  1414. - }
  1415. - *p = limb;
  1416. - limb >>= CHAR_BIT;
  1417. - bytes--;
  1418. - }
  1419. - }
  1420. - assert (i == un);
  1421. - assert (k == count);
  1422. + /* And at least significant byte of that word. */
  1423. + if (endian == 1)
  1424. + p += (size - 1);
  1425. +
  1426. + for (bytes = 0, i = 0, k = 0; k < count; k++, p += word_step)
  1427. + {
  1428. + size_t j;
  1429. + for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
  1430. + {
  1431. + if (bytes == 0)
  1432. + {
  1433. + if (i < un)
  1434. + limb = u->_mp_d[i++];
  1435. + bytes = sizeof (mp_limb_t);
  1436. + }
  1437. + *p = limb;
  1438. + limb >>= CHAR_BIT;
  1439. + bytes--;
  1440. + }
  1441. + }
  1442. + assert (i == un);
  1443. + assert (k == count);
  1444. + }
  1445. if (countp)
  1446. *countp = count;
  1447. diff --git a/mini-gmp.h b/mini-gmp.h
  1448. index 8c94ca2..d8f691f 100644
  1449. --- a/mini-gmp.h
  1450. +++ b/mini-gmp.h
  1451. @@ -1,21 +1,32 @@
  1452. /* mini-gmp, a minimalistic implementation of a GNU GMP subset.
  1453. -Copyright 2011, 2012, 2013 Free Software Foundation, Inc.
  1454. +Copyright 2011-2014 Free Software Foundation, Inc.
  1455. This file is part of the GNU MP Library.
  1456. The GNU MP Library is free software; you can redistribute it and/or modify
  1457. -it under the terms of the GNU Lesser General Public License as published by
  1458. -the Free Software Foundation; either version 3 of the License, or (at your
  1459. -option) any later version.
  1460. +it under the terms of either:
  1461. +
  1462. + * the GNU Lesser General Public License as published by the Free
  1463. + Software Foundation; either version 3 of the License, or (at your
  1464. + option) any later version.
  1465. +
  1466. +or
  1467. +
  1468. + * the GNU General Public License as published by the Free Software
  1469. + Foundation; either version 2 of the License, or (at your option) any
  1470. + later version.
  1471. +
  1472. +or both in parallel, as here.
  1473. The GNU MP Library is distributed in the hope that it will be useful, but
  1474. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  1475. -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  1476. -License for more details.
  1477. +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  1478. +for more details.
  1479. -You should have received a copy of the GNU Lesser General Public License
  1480. -along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
  1481. +You should have received copies of the GNU General Public License and the
  1482. +GNU Lesser General Public License along with the GNU MP Library. If not,
  1483. +see https://www.gnu.org/licenses/. */
  1484. /* About mini-gmp: This is a minimal implementation of a subset of the
  1485. GMP interface. It is intended for inclusion into applications which
  1486. @@ -64,8 +75,11 @@ typedef __mpz_struct mpz_t[1];
  1487. typedef __mpz_struct *mpz_ptr;
  1488. typedef const __mpz_struct *mpz_srcptr;
  1489. +extern const int mp_bits_per_limb;
  1490. +
  1491. void mpn_copyi (mp_ptr, mp_srcptr, mp_size_t);
  1492. void mpn_copyd (mp_ptr, mp_srcptr, mp_size_t);
  1493. +void mpn_zero (mp_ptr, mp_size_t);
  1494. int mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t);
  1495. @@ -84,10 +98,17 @@ mp_limb_t mpn_submul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
  1496. mp_limb_t mpn_mul (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
  1497. void mpn_mul_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
  1498. void mpn_sqr (mp_ptr, mp_srcptr, mp_size_t);
  1499. +int mpn_perfect_square_p (mp_srcptr, mp_size_t);
  1500. +mp_size_t mpn_sqrtrem (mp_ptr, mp_ptr, mp_srcptr, mp_size_t);
  1501. mp_limb_t mpn_lshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
  1502. mp_limb_t mpn_rshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
  1503. +mp_bitcnt_t mpn_scan0 (mp_srcptr, mp_bitcnt_t);
  1504. +mp_bitcnt_t mpn_scan1 (mp_srcptr, mp_bitcnt_t);
  1505. +
  1506. +mp_bitcnt_t mpn_popcount (mp_srcptr, mp_size_t);
  1507. +
  1508. mp_limb_t mpn_invert_3by2 (mp_limb_t, mp_limb_t);
  1509. #define mpn_invert_limb(x) mpn_invert_3by2 ((x), 0)
  1510. @@ -124,6 +145,10 @@ void mpz_mul_si (mpz_t, const mpz_t, long int);
  1511. void mpz_mul_ui (mpz_t, const mpz_t, unsigned long int);
  1512. void mpz_mul (mpz_t, const mpz_t, const mpz_t);
  1513. void mpz_mul_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
  1514. +void mpz_addmul_ui (mpz_t, const mpz_t, unsigned long int);
  1515. +void mpz_addmul (mpz_t, const mpz_t, const mpz_t);
  1516. +void mpz_submul_ui (mpz_t, const mpz_t, unsigned long int);
  1517. +void mpz_submul (mpz_t, const mpz_t, const mpz_t);
  1518. void mpz_cdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
  1519. void mpz_fdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
  1520. @@ -147,6 +172,7 @@ void mpz_mod (mpz_t, const mpz_t, const mpz_t);
  1521. void mpz_divexact (mpz_t, const mpz_t, const mpz_t);
  1522. int mpz_divisible_p (const mpz_t, const mpz_t);
  1523. +int mpz_congruent_p (const mpz_t, const mpz_t, const mpz_t);
  1524. unsigned long mpz_cdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
  1525. unsigned long mpz_fdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
  1526. @@ -176,6 +202,7 @@ int mpz_invert (mpz_t, const mpz_t, const mpz_t);
  1527. void mpz_sqrtrem (mpz_t, mpz_t, const mpz_t);
  1528. void mpz_sqrt (mpz_t, const mpz_t);
  1529. +int mpz_perfect_square_p (const mpz_t);
  1530. void mpz_pow_ui (mpz_t, const mpz_t, unsigned long);
  1531. void mpz_ui_pow_ui (mpz_t, unsigned long, unsigned long);
  1532. @@ -188,6 +215,9 @@ int mpz_root (mpz_t, const mpz_t, unsigned long);
  1533. void mpz_fac_ui (mpz_t, unsigned long);
  1534. void mpz_bin_uiui (mpz_t, unsigned long, unsigned long);
  1535. +int
  1536. +mpz_probab_prime_p (const mpz_t, int);
  1537. +
  1538. int mpz_tstbit (const mpz_t, mp_bitcnt_t);
  1539. void mpz_setbit (mpz_t, mp_bitcnt_t);
  1540. void mpz_clrbit (mpz_t, mp_bitcnt_t);
  1541. @@ -211,6 +241,15 @@ double mpz_get_d (const mpz_t);
  1542. size_t mpz_size (const mpz_t);
  1543. mp_limb_t mpz_getlimbn (const mpz_t, mp_size_t);
  1544. +void mpz_realloc2 (mpz_t, mp_bitcnt_t);
  1545. +mp_srcptr mpz_limbs_read (mpz_srcptr);
  1546. +mp_ptr mpz_limbs_modify (mpz_t, mp_size_t);
  1547. +mp_ptr mpz_limbs_write (mpz_t, mp_size_t);
  1548. +void mpz_limbs_finish (mpz_t, mp_size_t);
  1549. +mpz_srcptr mpz_roinit_n (mpz_t, mp_srcptr, mp_size_t);
  1550. +
  1551. +#define MPZ_ROINIT_N(xp, xs) {{0, (xs),(xp) }}
  1552. +
  1553. void mpz_set_si (mpz_t, signed long int);
  1554. void mpz_set_ui (mpz_t, unsigned long int);
  1555. void mpz_set (mpz_t, const mpz_t);
  1556. --
  1557. 1.9.2