7894779ee7c2b0190ec4a08a3499bfa50fe61d09
[openwrt/staging/chunkeey.git] / package / network / utils / nftables / patches / 102-build-allow-building-with-mini-gmp-instead-of-gmp.patch
1 From da4e4f5db6561923ad239aa653660250aaeb5873 Mon Sep 17 00:00:00 2001
2 From: Steven Barth <cyrus@openwrt.org>
3 Date: Mon, 6 Oct 2014 07:26:13 +0200
4 Subject: [PATCH 2/2] build: allow building with mini-gmp instead of gmp
5
6 This introduces --without-libgmp which includes mini-gmp into nft
7 (adding ~30k) but avoids linking libgmp which is >400k.
8
9 This is useful for embedded distributions not using gmp otherwise.
10
11 Note: currently --without-libgmp must be used with --disable-debug.
12
13 Signed-off-by: Steven Barth <cyrus@openwrt.org>
14 ---
15 configure.ac | 14 +-
16 include/bignum.h | 17 +
17 include/expression.h | 2 +-
18 include/gmputil.h | 2 +-
19 include/mini-gmp.h | 294 ++++
20 include/utils.h | 4 +-
21 src/Makefile.in | 5 +
22 src/datatype.c | 4 +-
23 src/erec.c | 4 +-
24 src/evaluate.c | 8 +-
25 src/gmputil.c | 1 -
26 src/mini-gmp-printf.c | 62 +
27 src/mini-gmp.c | 4386 +++++++++++++++++++++++++++++++++++++++++++++++++
28 13 files changed, 4790 insertions(+), 13 deletions(-)
29 create mode 100644 include/bignum.h
30 create mode 100644 include/mini-gmp.h
31 create mode 100644 src/mini-gmp-printf.c
32 create mode 100644 src/mini-gmp.c
33
34 --- a/configure.ac
35 +++ b/configure.ac
36 @@ -72,8 +72,19 @@ AM_CONDITIONAL([BUILD_PDF], [test "$DBLA
37 PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3])
38 PKG_CHECK_MODULES([LIBNFTNL], [libnftnl >= 1.0.2])
39
40 -AC_CHECK_LIB([gmp], [__gmpz_init], ,
41 - AC_MSG_ERROR([No suitable version of libgmp found]))
42 +AC_ARG_WITH([libgmp], [AS_HELP_STRING([--without-libgmp],
43 + [Disable libgmp support (use builtin mini-gmp)])], [],
44 + [with_libgmp=yes])
45 +AS_IF([test "x$with_libgmp" != xno], [
46 +AC_CHECK_LIB([gmp],[__gmpz_init], , AC_MSG_ERROR([No suitable version of libgmp found]))
47 +AC_DEFINE([HAVE_LIBGMP], [1], [])
48 +])
49 +AM_CONDITIONAL([BUILD_MINIGMP], [test "x$with_libgmp" == xno])
50 +
51 +
52 +AS_IF([test "x$with_libgmp" != xyes -a "x$CONFIG_DEBUG" = xy], [
53 +AC_MSG_ERROR([--without-libgmp MUST be used with --disable-debug])
54 +])
55
56 AC_ARG_WITH([cli], [AS_HELP_STRING([--without-cli],
57 [disable interactive CLI (libreadline support)])],
58 --- /dev/null
59 +++ b/include/bignum.h
60 @@ -0,0 +1,17 @@
61 +#ifndef NFTABLES_BIGNUM_H
62 +#define NFTABLES_BIGNUM_H
63 +
64 +#ifdef HAVE_LIBGMP
65 +#include <gmp.h>
66 +
67 +#else
68 +
69 +#include <mini-gmp.h>
70 +#include <stdio.h>
71 +#include <stdarg.h>
72 +
73 +int gmp_printf(const char *format, const mpz_t value);
74 +
75 +#endif
76 +
77 +#endif /* NFTABLES_BIGNUM_H */
78 --- a/include/expression.h
79 +++ b/include/expression.h
80 @@ -2,7 +2,7 @@
81 #define NFTABLES_EXPRESSION_H
82
83 #include <stdbool.h>
84 -#include <gmp.h>
85 +#include <bignum.h>
86 #include <linux/netfilter/nf_tables.h>
87
88 #include <nftables.h>
89 --- a/include/gmputil.h
90 +++ b/include/gmputil.h
91 @@ -1,7 +1,7 @@
92 #ifndef NFTABLES_GMPUTIL_H
93 #define NFTABLES_GMPUTIL_H
94
95 -#include <gmp.h>
96 +#include <bignum.h>
97 #include <asm/byteorder.h>
98
99 enum mpz_word_order {
100 --- /dev/null
101 +++ b/include/mini-gmp.h
102 @@ -0,0 +1,294 @@
103 +/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
104 +
105 +Copyright 2011-2014 Free Software Foundation, Inc.
106 +
107 +This file is part of the GNU MP Library.
108 +
109 +The GNU MP Library is free software; you can redistribute it and/or modify
110 +it under the terms of either:
111 +
112 + * the GNU Lesser General Public License as published by the Free
113 + Software Foundation; either version 3 of the License, or (at your
114 + option) any later version.
115 +
116 +or
117 +
118 + * the GNU General Public License as published by the Free Software
119 + Foundation; either version 2 of the License, or (at your option) any
120 + later version.
121 +
122 +or both in parallel, as here.
123 +
124 +The GNU MP Library is distributed in the hope that it will be useful, but
125 +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
126 +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
127 +for more details.
128 +
129 +You should have received copies of the GNU General Public License and the
130 +GNU Lesser General Public License along with the GNU MP Library. If not,
131 +see https://www.gnu.org/licenses/. */
132 +
133 +/* About mini-gmp: This is a minimal implementation of a subset of the
134 + GMP interface. It is intended for inclusion into applications which
135 + have modest bignums needs, as a fallback when the real GMP library
136 + is not installed.
137 +
138 + This file defines the public interface. */
139 +
140 +#ifndef __MINI_GMP_H__
141 +#define __MINI_GMP_H__
142 +
143 +/* For size_t */
144 +#include <stddef.h>
145 +
146 +#if defined (__cplusplus)
147 +extern "C" {
148 +#endif
149 +
150 +void mp_set_memory_functions (void *(*) (size_t),
151 + void *(*) (void *, size_t, size_t),
152 + void (*) (void *, size_t));
153 +
154 +void mp_get_memory_functions (void *(**) (size_t),
155 + void *(**) (void *, size_t, size_t),
156 + void (**) (void *, size_t));
157 +
158 +typedef unsigned long mp_limb_t;
159 +typedef long mp_size_t;
160 +typedef unsigned long mp_bitcnt_t;
161 +
162 +typedef mp_limb_t *mp_ptr;
163 +typedef const mp_limb_t *mp_srcptr;
164 +
165 +typedef struct
166 +{
167 + int _mp_alloc; /* Number of *limbs* allocated and pointed
168 + to by the _mp_d field. */
169 + int _mp_size; /* abs(_mp_size) is the number of limbs the
170 + last field points to. If _mp_size is
171 + negative this is a negative number. */
172 + mp_limb_t *_mp_d; /* Pointer to the limbs. */
173 +} __mpz_struct;
174 +
175 +typedef __mpz_struct mpz_t[1];
176 +
177 +typedef __mpz_struct *mpz_ptr;
178 +typedef const __mpz_struct *mpz_srcptr;
179 +
180 +extern const int mp_bits_per_limb;
181 +
182 +void mpn_copyi (mp_ptr, mp_srcptr, mp_size_t);
183 +void mpn_copyd (mp_ptr, mp_srcptr, mp_size_t);
184 +void mpn_zero (mp_ptr, mp_size_t);
185 +
186 +int mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t);
187 +
188 +mp_limb_t mpn_add_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
189 +mp_limb_t mpn_add_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
190 +mp_limb_t mpn_add (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
191 +
192 +mp_limb_t mpn_sub_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
193 +mp_limb_t mpn_sub_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
194 +mp_limb_t mpn_sub (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
195 +
196 +mp_limb_t mpn_mul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
197 +mp_limb_t mpn_addmul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
198 +mp_limb_t mpn_submul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
199 +
200 +mp_limb_t mpn_mul (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
201 +void mpn_mul_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
202 +void mpn_sqr (mp_ptr, mp_srcptr, mp_size_t);
203 +int mpn_perfect_square_p (mp_srcptr, mp_size_t);
204 +mp_size_t mpn_sqrtrem (mp_ptr, mp_ptr, mp_srcptr, mp_size_t);
205 +
206 +mp_limb_t mpn_lshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
207 +mp_limb_t mpn_rshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
208 +
209 +mp_bitcnt_t mpn_scan0 (mp_srcptr, mp_bitcnt_t);
210 +mp_bitcnt_t mpn_scan1 (mp_srcptr, mp_bitcnt_t);
211 +
212 +mp_bitcnt_t mpn_popcount (mp_srcptr, mp_size_t);
213 +
214 +mp_limb_t mpn_invert_3by2 (mp_limb_t, mp_limb_t);
215 +#define mpn_invert_limb(x) mpn_invert_3by2 ((x), 0)
216 +
217 +size_t mpn_get_str (unsigned char *, int, mp_ptr, mp_size_t);
218 +mp_size_t mpn_set_str (mp_ptr, const unsigned char *, size_t, int);
219 +
220 +void mpz_init (mpz_t);
221 +void mpz_init2 (mpz_t, mp_bitcnt_t);
222 +void mpz_clear (mpz_t);
223 +
224 +#define mpz_odd_p(z) (((z)->_mp_size != 0) & (int) (z)->_mp_d[0])
225 +#define mpz_even_p(z) (! mpz_odd_p (z))
226 +
227 +int mpz_sgn (const mpz_t);
228 +int mpz_cmp_si (const mpz_t, long);
229 +int mpz_cmp_ui (const mpz_t, unsigned long);
230 +int mpz_cmp (const mpz_t, const mpz_t);
231 +int mpz_cmpabs_ui (const mpz_t, unsigned long);
232 +int mpz_cmpabs (const mpz_t, const mpz_t);
233 +int mpz_cmp_d (const mpz_t, double);
234 +int mpz_cmpabs_d (const mpz_t, double);
235 +
236 +void mpz_abs (mpz_t, const mpz_t);
237 +void mpz_neg (mpz_t, const mpz_t);
238 +void mpz_swap (mpz_t, mpz_t);
239 +
240 +void mpz_add_ui (mpz_t, const mpz_t, unsigned long);
241 +void mpz_add (mpz_t, const mpz_t, const mpz_t);
242 +void mpz_sub_ui (mpz_t, const mpz_t, unsigned long);
243 +void mpz_ui_sub (mpz_t, unsigned long, const mpz_t);
244 +void mpz_sub (mpz_t, const mpz_t, const mpz_t);
245 +
246 +void mpz_mul_si (mpz_t, const mpz_t, long int);
247 +void mpz_mul_ui (mpz_t, const mpz_t, unsigned long int);
248 +void mpz_mul (mpz_t, const mpz_t, const mpz_t);
249 +void mpz_mul_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
250 +void mpz_addmul_ui (mpz_t, const mpz_t, unsigned long int);
251 +void mpz_addmul (mpz_t, const mpz_t, const mpz_t);
252 +void mpz_submul_ui (mpz_t, const mpz_t, unsigned long int);
253 +void mpz_submul (mpz_t, const mpz_t, const mpz_t);
254 +
255 +void mpz_cdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
256 +void mpz_fdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
257 +void mpz_tdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
258 +void mpz_cdiv_q (mpz_t, const mpz_t, const mpz_t);
259 +void mpz_fdiv_q (mpz_t, const mpz_t, const mpz_t);
260 +void mpz_tdiv_q (mpz_t, const mpz_t, const mpz_t);
261 +void mpz_cdiv_r (mpz_t, const mpz_t, const mpz_t);
262 +void mpz_fdiv_r (mpz_t, const mpz_t, const mpz_t);
263 +void mpz_tdiv_r (mpz_t, const mpz_t, const mpz_t);
264 +
265 +void mpz_cdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
266 +void mpz_fdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
267 +void mpz_tdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
268 +void mpz_cdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
269 +void mpz_fdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
270 +void mpz_tdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
271 +
272 +void mpz_mod (mpz_t, const mpz_t, const mpz_t);
273 +
274 +void mpz_divexact (mpz_t, const mpz_t, const mpz_t);
275 +
276 +int mpz_divisible_p (const mpz_t, const mpz_t);
277 +int mpz_congruent_p (const mpz_t, const mpz_t, const mpz_t);
278 +
279 +unsigned long mpz_cdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
280 +unsigned long mpz_fdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
281 +unsigned long mpz_tdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
282 +unsigned long mpz_cdiv_q_ui (mpz_t, const mpz_t, unsigned long);
283 +unsigned long mpz_fdiv_q_ui (mpz_t, const mpz_t, unsigned long);
284 +unsigned long mpz_tdiv_q_ui (mpz_t, const mpz_t, unsigned long);
285 +unsigned long mpz_cdiv_r_ui (mpz_t, const mpz_t, unsigned long);
286 +unsigned long mpz_fdiv_r_ui (mpz_t, const mpz_t, unsigned long);
287 +unsigned long mpz_tdiv_r_ui (mpz_t, const mpz_t, unsigned long);
288 +unsigned long mpz_cdiv_ui (const mpz_t, unsigned long);
289 +unsigned long mpz_fdiv_ui (const mpz_t, unsigned long);
290 +unsigned long mpz_tdiv_ui (const mpz_t, unsigned long);
291 +
292 +unsigned long mpz_mod_ui (mpz_t, const mpz_t, unsigned long);
293 +
294 +void mpz_divexact_ui (mpz_t, const mpz_t, unsigned long);
295 +
296 +int mpz_divisible_ui_p (const mpz_t, unsigned long);
297 +
298 +unsigned long mpz_gcd_ui (mpz_t, const mpz_t, unsigned long);
299 +void mpz_gcd (mpz_t, const mpz_t, const mpz_t);
300 +void mpz_gcdext (mpz_t, mpz_t, mpz_t, const mpz_t, const mpz_t);
301 +void mpz_lcm_ui (mpz_t, const mpz_t, unsigned long);
302 +void mpz_lcm (mpz_t, const mpz_t, const mpz_t);
303 +int mpz_invert (mpz_t, const mpz_t, const mpz_t);
304 +
305 +void mpz_sqrtrem (mpz_t, mpz_t, const mpz_t);
306 +void mpz_sqrt (mpz_t, const mpz_t);
307 +int mpz_perfect_square_p (const mpz_t);
308 +
309 +void mpz_pow_ui (mpz_t, const mpz_t, unsigned long);
310 +void mpz_ui_pow_ui (mpz_t, unsigned long, unsigned long);
311 +void mpz_powm (mpz_t, const mpz_t, const mpz_t, const mpz_t);
312 +void mpz_powm_ui (mpz_t, const mpz_t, unsigned long, const mpz_t);
313 +
314 +void mpz_rootrem (mpz_t, mpz_t, const mpz_t, unsigned long);
315 +int mpz_root (mpz_t, const mpz_t, unsigned long);
316 +
317 +void mpz_fac_ui (mpz_t, unsigned long);
318 +void mpz_bin_uiui (mpz_t, unsigned long, unsigned long);
319 +
320 +int mpz_probab_prime_p (const mpz_t, int);
321 +
322 +int mpz_tstbit (const mpz_t, mp_bitcnt_t);
323 +void mpz_setbit (mpz_t, mp_bitcnt_t);
324 +void mpz_clrbit (mpz_t, mp_bitcnt_t);
325 +void mpz_combit (mpz_t, mp_bitcnt_t);
326 +
327 +void mpz_com (mpz_t, const mpz_t);
328 +void mpz_and (mpz_t, const mpz_t, const mpz_t);
329 +void mpz_ior (mpz_t, const mpz_t, const mpz_t);
330 +void mpz_xor (mpz_t, const mpz_t, const mpz_t);
331 +
332 +mp_bitcnt_t mpz_popcount (const mpz_t);
333 +mp_bitcnt_t mpz_hamdist (const mpz_t, const mpz_t);
334 +mp_bitcnt_t mpz_scan0 (const mpz_t, mp_bitcnt_t);
335 +mp_bitcnt_t mpz_scan1 (const mpz_t, mp_bitcnt_t);
336 +
337 +int mpz_fits_slong_p (const mpz_t);
338 +int mpz_fits_ulong_p (const mpz_t);
339 +long int mpz_get_si (const mpz_t);
340 +unsigned long int mpz_get_ui (const mpz_t);
341 +double mpz_get_d (const mpz_t);
342 +size_t mpz_size (const mpz_t);
343 +mp_limb_t mpz_getlimbn (const mpz_t, mp_size_t);
344 +
345 +void mpz_realloc2 (mpz_t, mp_bitcnt_t);
346 +mp_srcptr mpz_limbs_read (mpz_srcptr);
347 +mp_ptr mpz_limbs_modify (mpz_t, mp_size_t);
348 +mp_ptr mpz_limbs_write (mpz_t, mp_size_t);
349 +void mpz_limbs_finish (mpz_t, mp_size_t);
350 +mpz_srcptr mpz_roinit_n (mpz_t, mp_srcptr, mp_size_t);
351 +
352 +#define MPZ_ROINIT_N(xp, xs) {{0, (xs),(xp) }}
353 +
354 +void mpz_set_si (mpz_t, signed long int);
355 +void mpz_set_ui (mpz_t, unsigned long int);
356 +void mpz_set (mpz_t, const mpz_t);
357 +void mpz_set_d (mpz_t, double);
358 +
359 +void mpz_init_set_si (mpz_t, signed long int);
360 +void mpz_init_set_ui (mpz_t, unsigned long int);
361 +void mpz_init_set (mpz_t, const mpz_t);
362 +void mpz_init_set_d (mpz_t, double);
363 +
364 +size_t mpz_sizeinbase (const mpz_t, int);
365 +char *mpz_get_str (char *, int, const mpz_t);
366 +int mpz_set_str (mpz_t, const char *, int);
367 +int mpz_init_set_str (mpz_t, const char *, int);
368 +
369 +/* This long list taken from gmp.h. */
370 +/* For reference, "defined(EOF)" cannot be used here. In g++ 2.95.4,
371 + <iostream> defines EOF but not FILE. */
372 +#if defined (FILE) \
373 + || defined (H_STDIO) \
374 + || defined (_H_STDIO) /* AIX */ \
375 + || defined (_STDIO_H) /* glibc, Sun, SCO */ \
376 + || defined (_STDIO_H_) /* BSD, OSF */ \
377 + || defined (__STDIO_H) /* Borland */ \
378 + || defined (__STDIO_H__) /* IRIX */ \
379 + || defined (_STDIO_INCLUDED) /* HPUX */ \
380 + || defined (__dj_include_stdio_h_) /* DJGPP */ \
381 + || defined (_FILE_DEFINED) /* Microsoft */ \
382 + || defined (__STDIO__) /* Apple MPW MrC */ \
383 + || defined (_MSL_STDIO_H) /* Metrowerks */ \
384 + || defined (_STDIO_H_INCLUDED) /* QNX4 */ \
385 + || defined (_ISO_STDIO_ISO_H) /* Sun C++ */ \
386 + || defined (__STDIO_LOADED) /* VMS */
387 +size_t mpz_out_str (FILE *, int, const mpz_t);
388 +#endif
389 +
390 +void mpz_import (mpz_t, size_t, int, size_t, int, size_t, const void *);
391 +void *mpz_export (void *, size_t *, int, size_t, int, size_t, const mpz_t);
392 +
393 +#if defined (__cplusplus)
394 +}
395 +#endif
396 +#endif /* __MINI_GMP_H__ */
397 --- a/include/utils.h
398 +++ b/include/utils.h
399 @@ -9,14 +9,14 @@
400 #include <unistd.h>
401 #include <assert.h>
402 #include <list.h>
403 -#include <gmp.h>
404 +#include <bignum.h>
405
406 #define BITS_PER_BYTE 8
407
408 #ifdef DEBUG
409 #define pr_debug(fmt, arg...) gmp_printf(fmt, ##arg)
410 #else
411 -#define pr_debug(fmt, arg...) ({ if (false) gmp_printf(fmt, ##arg); 0; })
412 +#define pr_debug(fmt, arg...)
413 #endif
414
415 #define __fmtstring(x, y) __attribute__((format(printf, x, y)))
416 --- a/src/datatype.c
417 +++ b/src/datatype.c
418 @@ -275,11 +275,9 @@ static struct error_record *integer_type
419 struct expr **res)
420 {
421 mpz_t v;
422 - int len;
423
424 mpz_init(v);
425 - if (gmp_sscanf(sym->identifier, "%Zu%n", v, &len) != 1 ||
426 - (int)strlen(sym->identifier) != len) {
427 + if (mpz_set_str(v, sym->identifier, 0)) {
428 mpz_clear(v);
429 return error(&sym->location, "Could not parse %s",
430 sym->dtype->desc);
431 --- a/src/erec.c
432 +++ b/src/erec.c
433 @@ -44,6 +44,7 @@ static void erec_destroy(struct error_re
434 xfree(erec);
435 }
436
437 +__attribute__((format(printf, 3, 0)))
438 struct error_record *erec_vcreate(enum error_record_types type,
439 const struct location *loc,
440 const char *fmt, va_list ap)
441 @@ -55,10 +56,11 @@ struct error_record *erec_vcreate(enum e
442 erec->num_locations = 0;
443 erec_add_location(erec, loc);
444
445 - gmp_vasprintf(&erec->msg, fmt, ap);
446 + if (vasprintf(&erec->msg, fmt, ap)) {}
447 return erec;
448 }
449
450 +__attribute__((format(printf, 3, 4)))
451 struct error_record *erec_create(enum error_record_types type,
452 const struct location *loc,
453 const char *fmt, ...)
454 --- a/src/evaluate.c
455 +++ b/src/evaluate.c
456 @@ -232,9 +232,13 @@ static int expr_evaluate_value(struct ev
457 case TYPE_INTEGER:
458 mpz_init_bitmask(mask, ctx->ectx.len);
459 if (mpz_cmp((*expr)->value, mask) > 0) {
460 + char *valstr = mpz_get_str(NULL, 10, (*expr)->value);
461 + char *rangestr = mpz_get_str(NULL, 10, mask);
462 expr_error(ctx->msgs, *expr,
463 - "Value %Zu exceeds valid range 0-%Zu",
464 - (*expr)->value, mask);
465 + "Value %s exceeds valid range 0-%s",
466 + valstr, rangestr);
467 + free(valstr);
468 + free(rangestr);
469 mpz_clear(mask);
470 return -1;
471 }
472 --- a/src/gmputil.c
473 +++ b/src/gmputil.c
474 @@ -14,7 +14,6 @@
475 #include <stdio.h>
476 #include <unistd.h>
477 #include <string.h>
478 -#include <gmp.h>
479
480 #include <nftables.h>
481 #include <datatype.h>
482 --- /dev/null
483 +++ b/src/mini-gmp-printf.c
484 @@ -0,0 +1,63 @@
485 +#include <stdarg.h>
486 +#include <stdbool.h>
487 +#include <stdio.h>
488 +#include <stdlib.h>
489 +#include <bignum.h>
490 +
491 +// nftables mostly uses gmp_printf as below so we build a minimalistic
492 +// version to avoid the awkwardness of wrapping printf.
493 +// This requires rewriting other occurences of gmp_printf or
494 +// variants which are rare (only 1 so far).
495 +// Also we exclude pr_debug here since this is a rathole
496 +// and if debugging is desired then libgmp can be used.
497 +
498 +int gmp_printf(const char *f, const mpz_t value)
499 +{
500 + int n = 0;
501 + while (*f) {
502 + if (*f != '%') {
503 + if (fputc(*f, stdout) != *f)
504 + return -1;
505 +
506 + ++n;
507 + } else {
508 + unsigned long prec = 0;
509 + int base;
510 + size_t len;
511 + char *str;
512 + bool ok;
513 +
514 + if (*++f == '.')
515 + prec = strtoul(++f, (char**)&f, 10);
516 +
517 + if (*f++ != 'Z')
518 + return -1;
519 +
520 + if (*f == 'u')
521 + base = 10;
522 + else if (*f == 'x')
523 + base = 16;
524 + else
525 + return -1;
526 +
527 + len = mpz_sizeinbase(value, base);
528 + while (prec-- > len) {
529 + if (fputc('0', stdout) != '0')
530 + return -1;
531 +
532 + ++n;
533 + }
534 +
535 + str = mpz_get_str(NULL, base, value);
536 + ok = str && fwrite(str, 1, len, stdout) == len;
537 + free(str);
538 +
539 + if (!ok)
540 + return -1;
541 +
542 + n += len;
543 + }
544 + ++f;
545 + }
546 + return n;
547 +}
548 --- /dev/null
549 +++ b/src/mini-gmp.c
550 @@ -0,0 +1,4386 @@
551 +/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
552 +
553 + Contributed to the GNU project by Niels Möller
554 +
555 +Copyright 1991-1997, 1999-2014 Free Software Foundation, Inc.
556 +
557 +This file is part of the GNU MP Library.
558 +
559 +The GNU MP Library is free software; you can redistribute it and/or modify
560 +it under the terms of either:
561 +
562 + * the GNU Lesser General Public License as published by the Free
563 + Software Foundation; either version 3 of the License, or (at your
564 + option) any later version.
565 +
566 +or
567 +
568 + * the GNU General Public License as published by the Free Software
569 + Foundation; either version 2 of the License, or (at your option) any
570 + later version.
571 +
572 +or both in parallel, as here.
573 +
574 +The GNU MP Library is distributed in the hope that it will be useful, but
575 +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
576 +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
577 +for more details.
578 +
579 +You should have received copies of the GNU General Public License and the
580 +GNU Lesser General Public License along with the GNU MP Library. If not,
581 +see https://www.gnu.org/licenses/. */
582 +
583 +/* NOTE: All functions in this file which are not declared in
584 + mini-gmp.h are internal, and are not intended to be compatible
585 + neither with GMP nor with future versions of mini-gmp. */
586 +
587 +/* Much of the material copied from GMP files, including: gmp-impl.h,
588 + longlong.h, mpn/generic/add_n.c, mpn/generic/addmul_1.c,
589 + mpn/generic/lshift.c, mpn/generic/mul_1.c,
590 + mpn/generic/mul_basecase.c, mpn/generic/rshift.c,
591 + mpn/generic/sbpi1_div_qr.c, mpn/generic/sub_n.c,
592 + mpn/generic/submul_1.c. */
593 +
594 +#include <assert.h>
595 +#include <ctype.h>
596 +#include <limits.h>
597 +#include <stdio.h>
598 +#include <stdlib.h>
599 +#include <string.h>
600 +
601 +#include "mini-gmp.h"
602 +
603 +\f
604 +/* Macros */
605 +#define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
606 +
607 +#define GMP_LIMB_MAX (~ (mp_limb_t) 0)
608 +#define GMP_LIMB_HIGHBIT ((mp_limb_t) 1 << (GMP_LIMB_BITS - 1))
609 +
610 +#define GMP_HLIMB_BIT ((mp_limb_t) 1 << (GMP_LIMB_BITS / 2))
611 +#define GMP_LLIMB_MASK (GMP_HLIMB_BIT - 1)
612 +
613 +#define GMP_ULONG_BITS (sizeof(unsigned long) * CHAR_BIT)
614 +#define GMP_ULONG_HIGHBIT ((unsigned long) 1 << (GMP_ULONG_BITS - 1))
615 +
616 +#define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
617 +#define GMP_NEG_CAST(T,x) (-((T)((x) + 1) - 1))
618 +
619 +#define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
620 +#define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
621 +
622 +#define gmp_assert_nocarry(x) do { \
623 + mp_limb_t __cy = x; \
624 + assert (__cy == 0); \
625 + } while (0)
626 +
627 +#define gmp_clz(count, x) do { \
628 + mp_limb_t __clz_x = (x); \
629 + unsigned __clz_c; \
630 + for (__clz_c = 0; \
631 + (__clz_x & ((mp_limb_t) 0xff << (GMP_LIMB_BITS - 8))) == 0; \
632 + __clz_c += 8) \
633 + __clz_x <<= 8; \
634 + for (; (__clz_x & GMP_LIMB_HIGHBIT) == 0; __clz_c++) \
635 + __clz_x <<= 1; \
636 + (count) = __clz_c; \
637 + } while (0)
638 +
639 +#define gmp_ctz(count, x) do { \
640 + mp_limb_t __ctz_x = (x); \
641 + unsigned __ctz_c = 0; \
642 + gmp_clz (__ctz_c, __ctz_x & - __ctz_x); \
643 + (count) = GMP_LIMB_BITS - 1 - __ctz_c; \
644 + } while (0)
645 +
646 +#define gmp_add_ssaaaa(sh, sl, ah, al, bh, bl) \
647 + do { \
648 + mp_limb_t __x; \
649 + __x = (al) + (bl); \
650 + (sh) = (ah) + (bh) + (__x < (al)); \
651 + (sl) = __x; \
652 + } while (0)
653 +
654 +#define gmp_sub_ddmmss(sh, sl, ah, al, bh, bl) \
655 + do { \
656 + mp_limb_t __x; \
657 + __x = (al) - (bl); \
658 + (sh) = (ah) - (bh) - ((al) < (bl)); \
659 + (sl) = __x; \
660 + } while (0)
661 +
662 +#define gmp_umul_ppmm(w1, w0, u, v) \
663 + do { \
664 + mp_limb_t __x0, __x1, __x2, __x3; \
665 + unsigned __ul, __vl, __uh, __vh; \
666 + mp_limb_t __u = (u), __v = (v); \
667 + \
668 + __ul = __u & GMP_LLIMB_MASK; \
669 + __uh = __u >> (GMP_LIMB_BITS / 2); \
670 + __vl = __v & GMP_LLIMB_MASK; \
671 + __vh = __v >> (GMP_LIMB_BITS / 2); \
672 + \
673 + __x0 = (mp_limb_t) __ul * __vl; \
674 + __x1 = (mp_limb_t) __ul * __vh; \
675 + __x2 = (mp_limb_t) __uh * __vl; \
676 + __x3 = (mp_limb_t) __uh * __vh; \
677 + \
678 + __x1 += __x0 >> (GMP_LIMB_BITS / 2);/* this can't give carry */ \
679 + __x1 += __x2; /* but this indeed can */ \
680 + if (__x1 < __x2) /* did we get it? */ \
681 + __x3 += GMP_HLIMB_BIT; /* yes, add it in the proper pos. */ \
682 + \
683 + (w1) = __x3 + (__x1 >> (GMP_LIMB_BITS / 2)); \
684 + (w0) = (__x1 << (GMP_LIMB_BITS / 2)) + (__x0 & GMP_LLIMB_MASK); \
685 + } while (0)
686 +
687 +#define gmp_udiv_qrnnd_preinv(q, r, nh, nl, d, di) \
688 + do { \
689 + mp_limb_t _qh, _ql, _r, _mask; \
690 + gmp_umul_ppmm (_qh, _ql, (nh), (di)); \
691 + gmp_add_ssaaaa (_qh, _ql, _qh, _ql, (nh) + 1, (nl)); \
692 + _r = (nl) - _qh * (d); \
693 + _mask = -(mp_limb_t) (_r > _ql); /* both > and >= are OK */ \
694 + _qh += _mask; \
695 + _r += _mask & (d); \
696 + if (_r >= (d)) \
697 + { \
698 + _r -= (d); \
699 + _qh++; \
700 + } \
701 + \
702 + (r) = _r; \
703 + (q) = _qh; \
704 + } while (0)
705 +
706 +#define gmp_udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv) \
707 + do { \
708 + mp_limb_t _q0, _t1, _t0, _mask; \
709 + gmp_umul_ppmm ((q), _q0, (n2), (dinv)); \
710 + gmp_add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1)); \
711 + \
712 + /* Compute the two most significant limbs of n - q'd */ \
713 + (r1) = (n1) - (d1) * (q); \
714 + gmp_sub_ddmmss ((r1), (r0), (r1), (n0), (d1), (d0)); \
715 + gmp_umul_ppmm (_t1, _t0, (d0), (q)); \
716 + gmp_sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0); \
717 + (q)++; \
718 + \
719 + /* Conditionally adjust q and the remainders */ \
720 + _mask = - (mp_limb_t) ((r1) >= _q0); \
721 + (q) += _mask; \
722 + gmp_add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0)); \
723 + if ((r1) >= (d1)) \
724 + { \
725 + if ((r1) > (d1) || (r0) >= (d0)) \
726 + { \
727 + (q)++; \
728 + gmp_sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0)); \
729 + } \
730 + } \
731 + } while (0)
732 +
733 +/* Swap macros. */
734 +#define MP_LIMB_T_SWAP(x, y) \
735 + do { \
736 + mp_limb_t __mp_limb_t_swap__tmp = (x); \
737 + (x) = (y); \
738 + (y) = __mp_limb_t_swap__tmp; \
739 + } while (0)
740 +#define MP_SIZE_T_SWAP(x, y) \
741 + do { \
742 + mp_size_t __mp_size_t_swap__tmp = (x); \
743 + (x) = (y); \
744 + (y) = __mp_size_t_swap__tmp; \
745 + } while (0)
746 +#define MP_BITCNT_T_SWAP(x,y) \
747 + do { \
748 + mp_bitcnt_t __mp_bitcnt_t_swap__tmp = (x); \
749 + (x) = (y); \
750 + (y) = __mp_bitcnt_t_swap__tmp; \
751 + } while (0)
752 +#define MP_PTR_SWAP(x, y) \
753 + do { \
754 + mp_ptr __mp_ptr_swap__tmp = (x); \
755 + (x) = (y); \
756 + (y) = __mp_ptr_swap__tmp; \
757 + } while (0)
758 +#define MP_SRCPTR_SWAP(x, y) \
759 + do { \
760 + mp_srcptr __mp_srcptr_swap__tmp = (x); \
761 + (x) = (y); \
762 + (y) = __mp_srcptr_swap__tmp; \
763 + } while (0)
764 +
765 +#define MPN_PTR_SWAP(xp,xs, yp,ys) \
766 + do { \
767 + MP_PTR_SWAP (xp, yp); \
768 + MP_SIZE_T_SWAP (xs, ys); \
769 + } while(0)
770 +#define MPN_SRCPTR_SWAP(xp,xs, yp,ys) \
771 + do { \
772 + MP_SRCPTR_SWAP (xp, yp); \
773 + MP_SIZE_T_SWAP (xs, ys); \
774 + } while(0)
775 +
776 +#define MPZ_PTR_SWAP(x, y) \
777 + do { \
778 + mpz_ptr __mpz_ptr_swap__tmp = (x); \
779 + (x) = (y); \
780 + (y) = __mpz_ptr_swap__tmp; \
781 + } while (0)
782 +#define MPZ_SRCPTR_SWAP(x, y) \
783 + do { \
784 + mpz_srcptr __mpz_srcptr_swap__tmp = (x); \
785 + (x) = (y); \
786 + (y) = __mpz_srcptr_swap__tmp; \
787 + } while (0)
788 +
789 +const int mp_bits_per_limb = GMP_LIMB_BITS;
790 +
791 +\f
792 +/* Memory allocation and other helper functions. */
793 +static void
794 +gmp_die (const char *msg)
795 +{
796 + fprintf (stderr, "%s\n", msg);
797 + abort();
798 +}
799 +
800 +static void *
801 +gmp_default_alloc (size_t size)
802 +{
803 + void *p;
804 +
805 + assert (size > 0);
806 +
807 + p = malloc (size);
808 + if (!p)
809 + gmp_die("gmp_default_alloc: Virtual memory exhausted.");
810 +
811 + return p;
812 +}
813 +
814 +static void *
815 +gmp_default_realloc (void *old, size_t old_size, size_t new_size)
816 +{
817 + mp_ptr p;
818 +
819 + p = realloc (old, new_size);
820 +
821 + if (!p)
822 + gmp_die("gmp_default_realoc: Virtual memory exhausted.");
823 +
824 + return p;
825 +}
826 +
827 +static void
828 +gmp_default_free (void *p, size_t size)
829 +{
830 + free (p);
831 +}
832 +
833 +static void * (*gmp_allocate_func) (size_t) = gmp_default_alloc;
834 +static void * (*gmp_reallocate_func) (void *, size_t, size_t) = gmp_default_realloc;
835 +static void (*gmp_free_func) (void *, size_t) = gmp_default_free;
836 +
837 +void
838 +mp_get_memory_functions (void *(**alloc_func) (size_t),
839 + void *(**realloc_func) (void *, size_t, size_t),
840 + void (**free_func) (void *, size_t))
841 +{
842 + if (alloc_func)
843 + *alloc_func = gmp_allocate_func;
844 +
845 + if (realloc_func)
846 + *realloc_func = gmp_reallocate_func;
847 +
848 + if (free_func)
849 + *free_func = gmp_free_func;
850 +}
851 +
852 +void
853 +mp_set_memory_functions (void *(*alloc_func) (size_t),
854 + void *(*realloc_func) (void *, size_t, size_t),
855 + void (*free_func) (void *, size_t))
856 +{
857 + if (!alloc_func)
858 + alloc_func = gmp_default_alloc;
859 + if (!realloc_func)
860 + realloc_func = gmp_default_realloc;
861 + if (!free_func)
862 + free_func = gmp_default_free;
863 +
864 + gmp_allocate_func = alloc_func;
865 + gmp_reallocate_func = realloc_func;
866 + gmp_free_func = free_func;
867 +}
868 +
869 +#define gmp_xalloc(size) ((*gmp_allocate_func)((size)))
870 +#define gmp_free(p) ((*gmp_free_func) ((p), 0))
871 +
872 +static mp_ptr
873 +gmp_xalloc_limbs (mp_size_t size)
874 +{
875 + return gmp_xalloc (size * sizeof (mp_limb_t));
876 +}
877 +
878 +static mp_ptr
879 +gmp_xrealloc_limbs (mp_ptr old, mp_size_t size)
880 +{
881 + assert (size > 0);
882 + return (*gmp_reallocate_func) (old, 0, size * sizeof (mp_limb_t));
883 +}
884 +
885 +\f
886 +/* MPN interface */
887 +
888 +void
889 +mpn_copyi (mp_ptr d, mp_srcptr s, mp_size_t n)
890 +{
891 + mp_size_t i;
892 + for (i = 0; i < n; i++)
893 + d[i] = s[i];
894 +}
895 +
896 +void
897 +mpn_copyd (mp_ptr d, mp_srcptr s, mp_size_t n)
898 +{
899 + while (n-- > 0)
900 + d[n] = s[n];
901 +}
902 +
903 +int
904 +mpn_cmp (mp_srcptr ap, mp_srcptr bp, mp_size_t n)
905 +{
906 + while (--n >= 0)
907 + {
908 + if (ap[n] != bp[n])
909 + return ap[n] > bp[n] ? 1 : -1;
910 + }
911 + return 0;
912 +}
913 +
914 +static int
915 +mpn_cmp4 (mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
916 +{
917 + if (an != bn)
918 + return an < bn ? -1 : 1;
919 + else
920 + return mpn_cmp (ap, bp, an);
921 +}
922 +
923 +static mp_size_t
924 +mpn_normalized_size (mp_srcptr xp, mp_size_t n)
925 +{
926 + for (; n > 0 && xp[n-1] == 0; n--)
927 + ;
928 + return n;
929 +}
930 +
931 +#define mpn_zero_p(xp, n) (mpn_normalized_size ((xp), (n)) == 0)
932 +
933 +void
934 +mpn_zero (mp_ptr rp, mp_size_t n)
935 +{
936 + mp_size_t i;
937 +
938 + for (i = 0; i < n; i++)
939 + rp[i] = 0;
940 +}
941 +
942 +mp_limb_t
943 +mpn_add_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
944 +{
945 + mp_size_t i;
946 +
947 + assert (n > 0);
948 + i = 0;
949 + do
950 + {
951 + mp_limb_t r = ap[i] + b;
952 + /* Carry out */
953 + b = (r < b);
954 + rp[i] = r;
955 + }
956 + while (++i < n);
957 +
958 + return b;
959 +}
960 +
961 +mp_limb_t
962 +mpn_add_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
963 +{
964 + mp_size_t i;
965 + mp_limb_t cy;
966 +
967 + for (i = 0, cy = 0; i < n; i++)
968 + {
969 + mp_limb_t a, b, r;
970 + a = ap[i]; b = bp[i];
971 + r = a + cy;
972 + cy = (r < cy);
973 + r += b;
974 + cy += (r < b);
975 + rp[i] = r;
976 + }
977 + return cy;
978 +}
979 +
980 +mp_limb_t
981 +mpn_add (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
982 +{
983 + mp_limb_t cy;
984 +
985 + assert (an >= bn);
986 +
987 + cy = mpn_add_n (rp, ap, bp, bn);
988 + if (an > bn)
989 + cy = mpn_add_1 (rp + bn, ap + bn, an - bn, cy);
990 + return cy;
991 +}
992 +
993 +mp_limb_t
994 +mpn_sub_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
995 +{
996 + mp_size_t i;
997 +
998 + assert (n > 0);
999 +
1000 + i = 0;
1001 + do
1002 + {
1003 + mp_limb_t a = ap[i];
1004 + /* Carry out */
1005 + mp_limb_t cy = a < b;;
1006 + rp[i] = a - b;
1007 + b = cy;
1008 + }
1009 + while (++i < n);
1010 +
1011 + return b;
1012 +}
1013 +
1014 +mp_limb_t
1015 +mpn_sub_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
1016 +{
1017 + mp_size_t i;
1018 + mp_limb_t cy;
1019 +
1020 + for (i = 0, cy = 0; i < n; i++)
1021 + {
1022 + mp_limb_t a, b;
1023 + a = ap[i]; b = bp[i];
1024 + b += cy;
1025 + cy = (b < cy);
1026 + cy += (a < b);
1027 + rp[i] = a - b;
1028 + }
1029 + return cy;
1030 +}
1031 +
1032 +mp_limb_t
1033 +mpn_sub (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
1034 +{
1035 + mp_limb_t cy;
1036 +
1037 + assert (an >= bn);
1038 +
1039 + cy = mpn_sub_n (rp, ap, bp, bn);
1040 + if (an > bn)
1041 + cy = mpn_sub_1 (rp + bn, ap + bn, an - bn, cy);
1042 + return cy;
1043 +}
1044 +
1045 +mp_limb_t
1046 +mpn_mul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
1047 +{
1048 + mp_limb_t ul, cl, hpl, lpl;
1049 +
1050 + assert (n >= 1);
1051 +
1052 + cl = 0;
1053 + do
1054 + {
1055 + ul = *up++;
1056 + gmp_umul_ppmm (hpl, lpl, ul, vl);
1057 +
1058 + lpl += cl;
1059 + cl = (lpl < cl) + hpl;
1060 +
1061 + *rp++ = lpl;
1062 + }
1063 + while (--n != 0);
1064 +
1065 + return cl;
1066 +}
1067 +
1068 +mp_limb_t
1069 +mpn_addmul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
1070 +{
1071 + mp_limb_t ul, cl, hpl, lpl, rl;
1072 +
1073 + assert (n >= 1);
1074 +
1075 + cl = 0;
1076 + do
1077 + {
1078 + ul = *up++;
1079 + gmp_umul_ppmm (hpl, lpl, ul, vl);
1080 +
1081 + lpl += cl;
1082 + cl = (lpl < cl) + hpl;
1083 +
1084 + rl = *rp;
1085 + lpl = rl + lpl;
1086 + cl += lpl < rl;
1087 + *rp++ = lpl;
1088 + }
1089 + while (--n != 0);
1090 +
1091 + return cl;
1092 +}
1093 +
1094 +mp_limb_t
1095 +mpn_submul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
1096 +{
1097 + mp_limb_t ul, cl, hpl, lpl, rl;
1098 +
1099 + assert (n >= 1);
1100 +
1101 + cl = 0;
1102 + do
1103 + {
1104 + ul = *up++;
1105 + gmp_umul_ppmm (hpl, lpl, ul, vl);
1106 +
1107 + lpl += cl;
1108 + cl = (lpl < cl) + hpl;
1109 +
1110 + rl = *rp;
1111 + lpl = rl - lpl;
1112 + cl += lpl > rl;
1113 + *rp++ = lpl;
1114 + }
1115 + while (--n != 0);
1116 +
1117 + return cl;
1118 +}
1119 +
1120 +mp_limb_t
1121 +mpn_mul (mp_ptr rp, mp_srcptr up, mp_size_t un, mp_srcptr vp, mp_size_t vn)
1122 +{
1123 + assert (un >= vn);
1124 + assert (vn >= 1);
1125 +
1126 + /* We first multiply by the low order limb. This result can be
1127 + stored, not added, to rp. We also avoid a loop for zeroing this
1128 + way. */
1129 +
1130 + rp[un] = mpn_mul_1 (rp, up, un, vp[0]);
1131 + rp += 1, vp += 1, vn -= 1;
1132 +
1133 + /* Now accumulate the product of up[] and the next higher limb from
1134 + vp[]. */
1135 +
1136 + while (vn >= 1)
1137 + {
1138 + rp[un] = mpn_addmul_1 (rp, up, un, vp[0]);
1139 + rp += 1, vp += 1, vn -= 1;
1140 + }
1141 + return rp[un - 1];
1142 +}
1143 +
1144 +void
1145 +mpn_mul_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
1146 +{
1147 + mpn_mul (rp, ap, n, bp, n);
1148 +}
1149 +
1150 +void
1151 +mpn_sqr (mp_ptr rp, mp_srcptr ap, mp_size_t n)
1152 +{
1153 + mpn_mul (rp, ap, n, ap, n);
1154 +}
1155 +
1156 +mp_limb_t
1157 +mpn_lshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
1158 +{
1159 + mp_limb_t high_limb, low_limb;
1160 + unsigned int tnc;
1161 + mp_size_t i;
1162 + mp_limb_t retval;
1163 +
1164 + assert (n >= 1);
1165 + assert (cnt >= 1);
1166 + assert (cnt < GMP_LIMB_BITS);
1167 +
1168 + up += n;
1169 + rp += n;
1170 +
1171 + tnc = GMP_LIMB_BITS - cnt;
1172 + low_limb = *--up;
1173 + retval = low_limb >> tnc;
1174 + high_limb = (low_limb << cnt);
1175 +
1176 + for (i = n; --i != 0;)
1177 + {
1178 + low_limb = *--up;
1179 + *--rp = high_limb | (low_limb >> tnc);
1180 + high_limb = (low_limb << cnt);
1181 + }
1182 + *--rp = high_limb;
1183 +
1184 + return retval;
1185 +}
1186 +
1187 +mp_limb_t
1188 +mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
1189 +{
1190 + mp_limb_t high_limb, low_limb;
1191 + unsigned int tnc;
1192 + mp_size_t i;
1193 + mp_limb_t retval;
1194 +
1195 + assert (n >= 1);
1196 + assert (cnt >= 1);
1197 + assert (cnt < GMP_LIMB_BITS);
1198 +
1199 + tnc = GMP_LIMB_BITS - cnt;
1200 + high_limb = *up++;
1201 + retval = (high_limb << tnc);
1202 + low_limb = high_limb >> cnt;
1203 +
1204 + for (i = n; --i != 0;)
1205 + {
1206 + high_limb = *up++;
1207 + *rp++ = low_limb | (high_limb << tnc);
1208 + low_limb = high_limb >> cnt;
1209 + }
1210 + *rp = low_limb;
1211 +
1212 + return retval;
1213 +}
1214 +
1215 +static mp_bitcnt_t
1216 +mpn_common_scan (mp_limb_t limb, mp_size_t i, mp_srcptr up, mp_size_t un,
1217 + mp_limb_t ux)
1218 +{
1219 + unsigned cnt;
1220 +
1221 + assert (ux == 0 || ux == GMP_LIMB_MAX);
1222 + assert (0 <= i && i <= un );
1223 +
1224 + while (limb == 0)
1225 + {
1226 + i++;
1227 + if (i == un)
1228 + return (ux == 0 ? ~(mp_bitcnt_t) 0 : un * GMP_LIMB_BITS);
1229 + limb = ux ^ up[i];
1230 + }
1231 + gmp_ctz (cnt, limb);
1232 + return (mp_bitcnt_t) i * GMP_LIMB_BITS + cnt;
1233 +}
1234 +
1235 +mp_bitcnt_t
1236 +mpn_scan1 (mp_srcptr ptr, mp_bitcnt_t bit)
1237 +{
1238 + mp_size_t i;
1239 + i = bit / GMP_LIMB_BITS;
1240 +
1241 + return mpn_common_scan ( ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
1242 + i, ptr, i, 0);
1243 +}
1244 +
1245 +mp_bitcnt_t
1246 +mpn_scan0 (mp_srcptr ptr, mp_bitcnt_t bit)
1247 +{
1248 + mp_size_t i;
1249 + i = bit / GMP_LIMB_BITS;
1250 +
1251 + return mpn_common_scan (~ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
1252 + i, ptr, i, GMP_LIMB_MAX);
1253 +}
1254 +
1255 +\f
1256 +/* MPN division interface. */
1257 +mp_limb_t
1258 +mpn_invert_3by2 (mp_limb_t u1, mp_limb_t u0)
1259 +{
1260 + mp_limb_t r, p, m;
1261 + unsigned ul, uh;
1262 + unsigned ql, qh;
1263 +
1264 + /* First, do a 2/1 inverse. */
1265 + /* The inverse m is defined as floor( (B^2 - 1 - u1)/u1 ), so that 0 <
1266 + * B^2 - (B + m) u1 <= u1 */
1267 + assert (u1 >= GMP_LIMB_HIGHBIT);
1268 +
1269 + ul = u1 & GMP_LLIMB_MASK;
1270 + uh = u1 >> (GMP_LIMB_BITS / 2);
1271 +
1272 + qh = ~u1 / uh;
1273 + r = ((~u1 - (mp_limb_t) qh * uh) << (GMP_LIMB_BITS / 2)) | GMP_LLIMB_MASK;
1274 +
1275 + p = (mp_limb_t) qh * ul;
1276 + /* Adjustment steps taken from udiv_qrnnd_c */
1277 + if (r < p)
1278 + {
1279 + qh--;
1280 + r += u1;
1281 + if (r >= u1) /* i.e. we didn't get carry when adding to r */
1282 + if (r < p)
1283 + {
1284 + qh--;
1285 + r += u1;
1286 + }
1287 + }
1288 + r -= p;
1289 +
1290 + /* Do a 3/2 division (with half limb size) */
1291 + p = (r >> (GMP_LIMB_BITS / 2)) * qh + r;
1292 + ql = (p >> (GMP_LIMB_BITS / 2)) + 1;
1293 +
1294 + /* By the 3/2 method, we don't need the high half limb. */
1295 + r = (r << (GMP_LIMB_BITS / 2)) + GMP_LLIMB_MASK - ql * u1;
1296 +
1297 + if (r >= (p << (GMP_LIMB_BITS / 2)))
1298 + {
1299 + ql--;
1300 + r += u1;
1301 + }
1302 + m = ((mp_limb_t) qh << (GMP_LIMB_BITS / 2)) + ql;
1303 + if (r >= u1)
1304 + {
1305 + m++;
1306 + r -= u1;
1307 + }
1308 +
1309 + if (u0 > 0)
1310 + {
1311 + mp_limb_t th, tl;
1312 + r = ~r;
1313 + r += u0;
1314 + if (r < u0)
1315 + {
1316 + m--;
1317 + if (r >= u1)
1318 + {
1319 + m--;
1320 + r -= u1;
1321 + }
1322 + r -= u1;
1323 + }
1324 + gmp_umul_ppmm (th, tl, u0, m);
1325 + r += th;
1326 + if (r < th)
1327 + {
1328 + m--;
1329 + m -= ((r > u1) | ((r == u1) & (tl > u0)));
1330 + }
1331 + }
1332 +
1333 + return m;
1334 +}
1335 +
1336 +struct gmp_div_inverse
1337 +{
1338 + /* Normalization shift count. */
1339 + unsigned shift;
1340 + /* Normalized divisor (d0 unused for mpn_div_qr_1) */
1341 + mp_limb_t d1, d0;
1342 + /* Inverse, for 2/1 or 3/2. */
1343 + mp_limb_t di;
1344 +};
1345 +
1346 +static void
1347 +mpn_div_qr_1_invert (struct gmp_div_inverse *inv, mp_limb_t d)
1348 +{
1349 + unsigned shift;
1350 +
1351 + assert (d > 0);
1352 + gmp_clz (shift, d);
1353 + inv->shift = shift;
1354 + inv->d1 = d << shift;
1355 + inv->di = mpn_invert_limb (inv->d1);
1356 +}
1357 +
1358 +static void
1359 +mpn_div_qr_2_invert (struct gmp_div_inverse *inv,
1360 + mp_limb_t d1, mp_limb_t d0)
1361 +{
1362 + unsigned shift;
1363 +
1364 + assert (d1 > 0);
1365 + gmp_clz (shift, d1);
1366 + inv->shift = shift;
1367 + if (shift > 0)
1368 + {
1369 + d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
1370 + d0 <<= shift;
1371 + }
1372 + inv->d1 = d1;
1373 + inv->d0 = d0;
1374 + inv->di = mpn_invert_3by2 (d1, d0);
1375 +}
1376 +
1377 +static void
1378 +mpn_div_qr_invert (struct gmp_div_inverse *inv,
1379 + mp_srcptr dp, mp_size_t dn)
1380 +{
1381 + assert (dn > 0);
1382 +
1383 + if (dn == 1)
1384 + mpn_div_qr_1_invert (inv, dp[0]);
1385 + else if (dn == 2)
1386 + mpn_div_qr_2_invert (inv, dp[1], dp[0]);
1387 + else
1388 + {
1389 + unsigned shift;
1390 + mp_limb_t d1, d0;
1391 +
1392 + d1 = dp[dn-1];
1393 + d0 = dp[dn-2];
1394 + assert (d1 > 0);
1395 + gmp_clz (shift, d1);
1396 + inv->shift = shift;
1397 + if (shift > 0)
1398 + {
1399 + d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
1400 + d0 = (d0 << shift) | (dp[dn-3] >> (GMP_LIMB_BITS - shift));
1401 + }
1402 + inv->d1 = d1;
1403 + inv->d0 = d0;
1404 + inv->di = mpn_invert_3by2 (d1, d0);
1405 + }
1406 +}
1407 +
1408 +/* Not matching current public gmp interface, rather corresponding to
1409 + the sbpi1_div_* functions. */
1410 +static mp_limb_t
1411 +mpn_div_qr_1_preinv (mp_ptr qp, mp_srcptr np, mp_size_t nn,
1412 + const struct gmp_div_inverse *inv)
1413 +{
1414 + mp_limb_t d, di;
1415 + mp_limb_t r;
1416 + mp_ptr tp = NULL;
1417 +
1418 + if (inv->shift > 0)
1419 + {
1420 + tp = gmp_xalloc_limbs (nn);
1421 + r = mpn_lshift (tp, np, nn, inv->shift);
1422 + np = tp;
1423 + }
1424 + else
1425 + r = 0;
1426 +
1427 + d = inv->d1;
1428 + di = inv->di;
1429 + while (nn-- > 0)
1430 + {
1431 + mp_limb_t q;
1432 +
1433 + gmp_udiv_qrnnd_preinv (q, r, r, np[nn], d, di);
1434 + if (qp)
1435 + qp[nn] = q;
1436 + }
1437 + if (inv->shift > 0)
1438 + gmp_free (tp);
1439 +
1440 + return r >> inv->shift;
1441 +}
1442 +
1443 +static mp_limb_t
1444 +mpn_div_qr_1 (mp_ptr qp, mp_srcptr np, mp_size_t nn, mp_limb_t d)
1445 +{
1446 + assert (d > 0);
1447 +
1448 + /* Special case for powers of two. */
1449 + if ((d & (d-1)) == 0)
1450 + {
1451 + mp_limb_t r = np[0] & (d-1);
1452 + if (qp)
1453 + {
1454 + if (d <= 1)
1455 + mpn_copyi (qp, np, nn);
1456 + else
1457 + {
1458 + unsigned shift;
1459 + gmp_ctz (shift, d);
1460 + mpn_rshift (qp, np, nn, shift);
1461 + }
1462 + }
1463 + return r;
1464 + }
1465 + else
1466 + {
1467 + struct gmp_div_inverse inv;
1468 + mpn_div_qr_1_invert (&inv, d);
1469 + return mpn_div_qr_1_preinv (qp, np, nn, &inv);
1470 + }
1471 +}
1472 +
1473 +static void
1474 +mpn_div_qr_2_preinv (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
1475 + const struct gmp_div_inverse *inv)
1476 +{
1477 + unsigned shift;
1478 + mp_size_t i;
1479 + mp_limb_t d1, d0, di, r1, r0;
1480 + mp_ptr tp;
1481 +
1482 + assert (nn >= 2);
1483 + shift = inv->shift;
1484 + d1 = inv->d1;
1485 + d0 = inv->d0;
1486 + di = inv->di;
1487 +
1488 + if (shift > 0)
1489 + {
1490 + tp = gmp_xalloc_limbs (nn);
1491 + r1 = mpn_lshift (tp, np, nn, shift);
1492 + np = tp;
1493 + }
1494 + else
1495 + r1 = 0;
1496 +
1497 + r0 = np[nn - 1];
1498 +
1499 + i = nn - 2;
1500 + do
1501 + {
1502 + mp_limb_t n0, q;
1503 + n0 = np[i];
1504 + gmp_udiv_qr_3by2 (q, r1, r0, r1, r0, n0, d1, d0, di);
1505 +
1506 + if (qp)
1507 + qp[i] = q;
1508 + }
1509 + while (--i >= 0);
1510 +
1511 + if (shift > 0)
1512 + {
1513 + assert ((r0 << (GMP_LIMB_BITS - shift)) == 0);
1514 + r0 = (r0 >> shift) | (r1 << (GMP_LIMB_BITS - shift));
1515 + r1 >>= shift;
1516 +
1517 + gmp_free (tp);
1518 + }
1519 +
1520 + rp[1] = r1;
1521 + rp[0] = r0;
1522 +}
1523 +
1524 +#if 0
1525 +static void
1526 +mpn_div_qr_2 (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
1527 + mp_limb_t d1, mp_limb_t d0)
1528 +{
1529 + struct gmp_div_inverse inv;
1530 + assert (nn >= 2);
1531 +
1532 + mpn_div_qr_2_invert (&inv, d1, d0);
1533 + mpn_div_qr_2_preinv (qp, rp, np, nn, &inv);
1534 +}
1535 +#endif
1536 +
1537 +static void
1538 +mpn_div_qr_pi1 (mp_ptr qp,
1539 + mp_ptr np, mp_size_t nn, mp_limb_t n1,
1540 + mp_srcptr dp, mp_size_t dn,
1541 + mp_limb_t dinv)
1542 +{
1543 + mp_size_t i;
1544 +
1545 + mp_limb_t d1, d0;
1546 + mp_limb_t cy, cy1;
1547 + mp_limb_t q;
1548 +
1549 + assert (dn > 2);
1550 + assert (nn >= dn);
1551 +
1552 + d1 = dp[dn - 1];
1553 + d0 = dp[dn - 2];
1554 +
1555 + assert ((d1 & GMP_LIMB_HIGHBIT) != 0);
1556 + /* Iteration variable is the index of the q limb.
1557 + *
1558 + * We divide <n1, np[dn-1+i], np[dn-2+i], np[dn-3+i],..., np[i]>
1559 + * by <d1, d0, dp[dn-3], ..., dp[0] >
1560 + */
1561 +
1562 + i = nn - dn;
1563 + do
1564 + {
1565 + mp_limb_t n0 = np[dn-1+i];
1566 +
1567 + if (n1 == d1 && n0 == d0)
1568 + {
1569 + q = GMP_LIMB_MAX;
1570 + mpn_submul_1 (np+i, dp, dn, q);
1571 + n1 = np[dn-1+i]; /* update n1, last loop's value will now be invalid */
1572 + }
1573 + else
1574 + {
1575 + gmp_udiv_qr_3by2 (q, n1, n0, n1, n0, np[dn-2+i], d1, d0, dinv);
1576 +
1577 + cy = mpn_submul_1 (np + i, dp, dn-2, q);
1578 +
1579 + cy1 = n0 < cy;
1580 + n0 = n0 - cy;
1581 + cy = n1 < cy1;
1582 + n1 = n1 - cy1;
1583 + np[dn-2+i] = n0;
1584 +
1585 + if (cy != 0)
1586 + {
1587 + n1 += d1 + mpn_add_n (np + i, np + i, dp, dn - 1);
1588 + q--;
1589 + }
1590 + }
1591 +
1592 + if (qp)
1593 + qp[i] = q;
1594 + }
1595 + while (--i >= 0);
1596 +
1597 + np[dn - 1] = n1;
1598 +}
1599 +
1600 +static void
1601 +mpn_div_qr_preinv (mp_ptr qp, mp_ptr np, mp_size_t nn,
1602 + mp_srcptr dp, mp_size_t dn,
1603 + const struct gmp_div_inverse *inv)
1604 +{
1605 + assert (dn > 0);
1606 + assert (nn >= dn);
1607 +
1608 + if (dn == 1)
1609 + np[0] = mpn_div_qr_1_preinv (qp, np, nn, inv);
1610 + else if (dn == 2)
1611 + mpn_div_qr_2_preinv (qp, np, np, nn, inv);
1612 + else
1613 + {
1614 + mp_limb_t nh;
1615 + unsigned shift;
1616 +
1617 + assert (inv->d1 == dp[dn-1]);
1618 + assert (inv->d0 == dp[dn-2]);
1619 + assert ((inv->d1 & GMP_LIMB_HIGHBIT) != 0);
1620 +
1621 + shift = inv->shift;
1622 + if (shift > 0)
1623 + nh = mpn_lshift (np, np, nn, shift);
1624 + else
1625 + nh = 0;
1626 +
1627 + mpn_div_qr_pi1 (qp, np, nn, nh, dp, dn, inv->di);
1628 +
1629 + if (shift > 0)
1630 + gmp_assert_nocarry (mpn_rshift (np, np, dn, shift));
1631 + }
1632 +}
1633 +
1634 +static void
1635 +mpn_div_qr (mp_ptr qp, mp_ptr np, mp_size_t nn, mp_srcptr dp, mp_size_t dn)
1636 +{
1637 + struct gmp_div_inverse inv;
1638 + mp_ptr tp = NULL;
1639 +
1640 + assert (dn > 0);
1641 + assert (nn >= dn);
1642 +
1643 + mpn_div_qr_invert (&inv, dp, dn);
1644 + if (dn > 2 && inv.shift > 0)
1645 + {
1646 + tp = gmp_xalloc_limbs (dn);
1647 + gmp_assert_nocarry (mpn_lshift (tp, dp, dn, inv.shift));
1648 + dp = tp;
1649 + }
1650 + mpn_div_qr_preinv (qp, np, nn, dp, dn, &inv);
1651 + if (tp)
1652 + gmp_free (tp);
1653 +}
1654 +
1655 +\f
1656 +/* MPN base conversion. */
1657 +static unsigned
1658 +mpn_base_power_of_two_p (unsigned b)
1659 +{
1660 + switch (b)
1661 + {
1662 + case 2: return 1;
1663 + case 4: return 2;
1664 + case 8: return 3;
1665 + case 16: return 4;
1666 + case 32: return 5;
1667 + case 64: return 6;
1668 + case 128: return 7;
1669 + case 256: return 8;
1670 + default: return 0;
1671 + }
1672 +}
1673 +
1674 +struct mpn_base_info
1675 +{
1676 + /* bb is the largest power of the base which fits in one limb, and
1677 + exp is the corresponding exponent. */
1678 + unsigned exp;
1679 + mp_limb_t bb;
1680 +};
1681 +
1682 +static void
1683 +mpn_get_base_info (struct mpn_base_info *info, mp_limb_t b)
1684 +{
1685 + mp_limb_t m;
1686 + mp_limb_t p;
1687 + unsigned exp;
1688 +
1689 + m = GMP_LIMB_MAX / b;
1690 + for (exp = 1, p = b; p <= m; exp++)
1691 + p *= b;
1692 +
1693 + info->exp = exp;
1694 + info->bb = p;
1695 +}
1696 +
1697 +static mp_bitcnt_t
1698 +mpn_limb_size_in_base_2 (mp_limb_t u)
1699 +{
1700 + unsigned shift;
1701 +
1702 + assert (u > 0);
1703 + gmp_clz (shift, u);
1704 + return GMP_LIMB_BITS - shift;
1705 +}
1706 +
1707 +static size_t
1708 +mpn_get_str_bits (unsigned char *sp, unsigned bits, mp_srcptr up, mp_size_t un)
1709 +{
1710 + unsigned char mask;
1711 + size_t sn, j;
1712 + mp_size_t i;
1713 + int shift;
1714 +
1715 + sn = ((un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1])
1716 + + bits - 1) / bits;
1717 +
1718 + mask = (1U << bits) - 1;
1719 +
1720 + for (i = 0, j = sn, shift = 0; j-- > 0;)
1721 + {
1722 + unsigned char digit = up[i] >> shift;
1723 +
1724 + shift += bits;
1725 +
1726 + if (shift >= GMP_LIMB_BITS && ++i < un)
1727 + {
1728 + shift -= GMP_LIMB_BITS;
1729 + digit |= up[i] << (bits - shift);
1730 + }
1731 + sp[j] = digit & mask;
1732 + }
1733 + return sn;
1734 +}
1735 +
1736 +/* We generate digits from the least significant end, and reverse at
1737 + the end. */
1738 +static size_t
1739 +mpn_limb_get_str (unsigned char *sp, mp_limb_t w,
1740 + const struct gmp_div_inverse *binv)
1741 +{
1742 + mp_size_t i;
1743 + for (i = 0; w > 0; i++)
1744 + {
1745 + mp_limb_t h, l, r;
1746 +
1747 + h = w >> (GMP_LIMB_BITS - binv->shift);
1748 + l = w << binv->shift;
1749 +
1750 + gmp_udiv_qrnnd_preinv (w, r, h, l, binv->d1, binv->di);
1751 + assert ( (r << (GMP_LIMB_BITS - binv->shift)) == 0);
1752 + r >>= binv->shift;
1753 +
1754 + sp[i] = r;
1755 + }
1756 + return i;
1757 +}
1758 +
1759 +static size_t
1760 +mpn_get_str_other (unsigned char *sp,
1761 + int base, const struct mpn_base_info *info,
1762 + mp_ptr up, mp_size_t un)
1763 +{
1764 + struct gmp_div_inverse binv;
1765 + size_t sn;
1766 + size_t i;
1767 +
1768 + mpn_div_qr_1_invert (&binv, base);
1769 +
1770 + sn = 0;
1771 +
1772 + if (un > 1)
1773 + {
1774 + struct gmp_div_inverse bbinv;
1775 + mpn_div_qr_1_invert (&bbinv, info->bb);
1776 +
1777 + do
1778 + {
1779 + mp_limb_t w;
1780 + size_t done;
1781 + w = mpn_div_qr_1_preinv (up, up, un, &bbinv);
1782 + un -= (up[un-1] == 0);
1783 + done = mpn_limb_get_str (sp + sn, w, &binv);
1784 +
1785 + for (sn += done; done < info->exp; done++)
1786 + sp[sn++] = 0;
1787 + }
1788 + while (un > 1);
1789 + }
1790 + sn += mpn_limb_get_str (sp + sn, up[0], &binv);
1791 +
1792 + /* Reverse order */
1793 + for (i = 0; 2*i + 1 < sn; i++)
1794 + {
1795 + unsigned char t = sp[i];
1796 + sp[i] = sp[sn - i - 1];
1797 + sp[sn - i - 1] = t;
1798 + }
1799 +
1800 + return sn;
1801 +}
1802 +
1803 +size_t
1804 +mpn_get_str (unsigned char *sp, int base, mp_ptr up, mp_size_t un)
1805 +{
1806 + unsigned bits;
1807 +
1808 + assert (un > 0);
1809 + assert (up[un-1] > 0);
1810 +
1811 + bits = mpn_base_power_of_two_p (base);
1812 + if (bits)
1813 + return mpn_get_str_bits (sp, bits, up, un);
1814 + else
1815 + {
1816 + struct mpn_base_info info;
1817 +
1818 + mpn_get_base_info (&info, base);
1819 + return mpn_get_str_other (sp, base, &info, up, un);
1820 + }
1821 +}
1822 +
1823 +static mp_size_t
1824 +mpn_set_str_bits (mp_ptr rp, const unsigned char *sp, size_t sn,
1825 + unsigned bits)
1826 +{
1827 + mp_size_t rn;
1828 + size_t j;
1829 + unsigned shift;
1830 +
1831 + for (j = sn, rn = 0, shift = 0; j-- > 0; )
1832 + {
1833 + if (shift == 0)
1834 + {
1835 + rp[rn++] = sp[j];
1836 + shift += bits;
1837 + }
1838 + else
1839 + {
1840 + rp[rn-1] |= (mp_limb_t) sp[j] << shift;
1841 + shift += bits;
1842 + if (shift >= GMP_LIMB_BITS)
1843 + {
1844 + shift -= GMP_LIMB_BITS;
1845 + if (shift > 0)
1846 + rp[rn++] = (mp_limb_t) sp[j] >> (bits - shift);
1847 + }
1848 + }
1849 + }
1850 + rn = mpn_normalized_size (rp, rn);
1851 + return rn;
1852 +}
1853 +
1854 +static mp_size_t
1855 +mpn_set_str_other (mp_ptr rp, const unsigned char *sp, size_t sn,
1856 + mp_limb_t b, const struct mpn_base_info *info)
1857 +{
1858 + mp_size_t rn;
1859 + mp_limb_t w;
1860 + unsigned k;
1861 + size_t j;
1862 +
1863 + k = 1 + (sn - 1) % info->exp;
1864 +
1865 + j = 0;
1866 + w = sp[j++];
1867 + for (; --k > 0; )
1868 + w = w * b + sp[j++];
1869 +
1870 + rp[0] = w;
1871 +
1872 + for (rn = (w > 0); j < sn;)
1873 + {
1874 + mp_limb_t cy;
1875 +
1876 + w = sp[j++];
1877 + for (k = 1; k < info->exp; k++)
1878 + w = w * b + sp[j++];
1879 +
1880 + cy = mpn_mul_1 (rp, rp, rn, info->bb);
1881 + cy += mpn_add_1 (rp, rp, rn, w);
1882 + if (cy > 0)
1883 + rp[rn++] = cy;
1884 + }
1885 + assert (j == sn);
1886 +
1887 + return rn;
1888 +}
1889 +
1890 +mp_size_t
1891 +mpn_set_str (mp_ptr rp, const unsigned char *sp, size_t sn, int base)
1892 +{
1893 + unsigned bits;
1894 +
1895 + if (sn == 0)
1896 + return 0;
1897 +
1898 + bits = mpn_base_power_of_two_p (base);
1899 + if (bits)
1900 + return mpn_set_str_bits (rp, sp, sn, bits);
1901 + else
1902 + {
1903 + struct mpn_base_info info;
1904 +
1905 + mpn_get_base_info (&info, base);
1906 + return mpn_set_str_other (rp, sp, sn, base, &info);
1907 + }
1908 +}
1909 +
1910 +\f
1911 +/* MPZ interface */
1912 +void
1913 +mpz_init (mpz_t r)
1914 +{
1915 + r->_mp_alloc = 1;
1916 + r->_mp_size = 0;
1917 + r->_mp_d = gmp_xalloc_limbs (1);
1918 +}
1919 +
1920 +/* The utility of this function is a bit limited, since many functions
1921 + assigns the result variable using mpz_swap. */
1922 +void
1923 +mpz_init2 (mpz_t r, mp_bitcnt_t bits)
1924 +{
1925 + mp_size_t rn;
1926 +
1927 + bits -= (bits != 0); /* Round down, except if 0 */
1928 + rn = 1 + bits / GMP_LIMB_BITS;
1929 +
1930 + r->_mp_alloc = rn;
1931 + r->_mp_size = 0;
1932 + r->_mp_d = gmp_xalloc_limbs (rn);
1933 +}
1934 +
1935 +void
1936 +mpz_clear (mpz_t r)
1937 +{
1938 + gmp_free (r->_mp_d);
1939 +}
1940 +
1941 +static void *
1942 +mpz_realloc (mpz_t r, mp_size_t size)
1943 +{
1944 + size = GMP_MAX (size, 1);
1945 +
1946 + r->_mp_d = gmp_xrealloc_limbs (r->_mp_d, size);
1947 + r->_mp_alloc = size;
1948 +
1949 + if (GMP_ABS (r->_mp_size) > size)
1950 + r->_mp_size = 0;
1951 +
1952 + return r->_mp_d;
1953 +}
1954 +
1955 +/* Realloc for an mpz_t WHAT if it has less than NEEDED limbs. */
1956 +#define MPZ_REALLOC(z,n) ((n) > (z)->_mp_alloc \
1957 + ? mpz_realloc(z,n) \
1958 + : (z)->_mp_d)
1959 +\f
1960 +/* MPZ assignment and basic conversions. */
1961 +void
1962 +mpz_set_si (mpz_t r, signed long int x)
1963 +{
1964 + if (x >= 0)
1965 + mpz_set_ui (r, x);
1966 + else /* (x < 0) */
1967 + {
1968 + r->_mp_size = -1;
1969 + r->_mp_d[0] = GMP_NEG_CAST (unsigned long int, x);
1970 + }
1971 +}
1972 +
1973 +void
1974 +mpz_set_ui (mpz_t r, unsigned long int x)
1975 +{
1976 + if (x > 0)
1977 + {
1978 + r->_mp_size = 1;
1979 + r->_mp_d[0] = x;
1980 + }
1981 + else
1982 + r->_mp_size = 0;
1983 +}
1984 +
1985 +void
1986 +mpz_set (mpz_t r, const mpz_t x)
1987 +{
1988 + /* Allow the NOP r == x */
1989 + if (r != x)
1990 + {
1991 + mp_size_t n;
1992 + mp_ptr rp;
1993 +
1994 + n = GMP_ABS (x->_mp_size);
1995 + rp = MPZ_REALLOC (r, n);
1996 +
1997 + mpn_copyi (rp, x->_mp_d, n);
1998 + r->_mp_size = x->_mp_size;
1999 + }
2000 +}
2001 +
2002 +void
2003 +mpz_init_set_si (mpz_t r, signed long int x)
2004 +{
2005 + mpz_init (r);
2006 + mpz_set_si (r, x);
2007 +}
2008 +
2009 +void
2010 +mpz_init_set_ui (mpz_t r, unsigned long int x)
2011 +{
2012 + mpz_init (r);
2013 + mpz_set_ui (r, x);
2014 +}
2015 +
2016 +void
2017 +mpz_init_set (mpz_t r, const mpz_t x)
2018 +{
2019 + mpz_init (r);
2020 + mpz_set (r, x);
2021 +}
2022 +
2023 +int
2024 +mpz_fits_slong_p (const mpz_t u)
2025 +{
2026 + mp_size_t us = u->_mp_size;
2027 +
2028 + if (us == 0)
2029 + return 1;
2030 + else if (us == 1)
2031 + return u->_mp_d[0] < GMP_LIMB_HIGHBIT;
2032 + else if (us == -1)
2033 + return u->_mp_d[0] <= GMP_LIMB_HIGHBIT;
2034 + else
2035 + return 0;
2036 +}
2037 +
2038 +int
2039 +mpz_fits_ulong_p (const mpz_t u)
2040 +{
2041 + mp_size_t us = u->_mp_size;
2042 +
2043 + return (us == (us > 0));
2044 +}
2045 +
2046 +long int
2047 +mpz_get_si (const mpz_t u)
2048 +{
2049 + mp_size_t us = u->_mp_size;
2050 +
2051 + if (us > 0)
2052 + return (long) (u->_mp_d[0] & ~GMP_LIMB_HIGHBIT);
2053 + else if (us < 0)
2054 + return (long) (- u->_mp_d[0] | GMP_LIMB_HIGHBIT);
2055 + else
2056 + return 0;
2057 +}
2058 +
2059 +unsigned long int
2060 +mpz_get_ui (const mpz_t u)
2061 +{
2062 + return u->_mp_size == 0 ? 0 : u->_mp_d[0];
2063 +}
2064 +
2065 +size_t
2066 +mpz_size (const mpz_t u)
2067 +{
2068 + return GMP_ABS (u->_mp_size);
2069 +}
2070 +
2071 +mp_limb_t
2072 +mpz_getlimbn (const mpz_t u, mp_size_t n)
2073 +{
2074 + if (n >= 0 && n < GMP_ABS (u->_mp_size))
2075 + return u->_mp_d[n];
2076 + else
2077 + return 0;
2078 +}
2079 +
2080 +void
2081 +mpz_realloc2 (mpz_t x, mp_bitcnt_t n)
2082 +{
2083 + mpz_realloc (x, 1 + (n - (n != 0)) / GMP_LIMB_BITS);
2084 +}
2085 +
2086 +mp_srcptr
2087 +mpz_limbs_read (mpz_srcptr x)
2088 +{
2089 + return x->_mp_d;;
2090 +}
2091 +
2092 +mp_ptr
2093 +mpz_limbs_modify (mpz_t x, mp_size_t n)
2094 +{
2095 + assert (n > 0);
2096 + return MPZ_REALLOC (x, n);
2097 +}
2098 +
2099 +mp_ptr
2100 +mpz_limbs_write (mpz_t x, mp_size_t n)
2101 +{
2102 + return mpz_limbs_modify (x, n);
2103 +}
2104 +
2105 +void
2106 +mpz_limbs_finish (mpz_t x, mp_size_t xs)
2107 +{
2108 + mp_size_t xn;
2109 + xn = mpn_normalized_size (x->_mp_d, GMP_ABS (xs));
2110 + x->_mp_size = xs < 0 ? -xn : xn;
2111 +}
2112 +
2113 +mpz_srcptr
2114 +mpz_roinit_n (mpz_t x, mp_srcptr xp, mp_size_t xs)
2115 +{
2116 + x->_mp_alloc = 0;
2117 + x->_mp_d = (mp_ptr) xp;
2118 + mpz_limbs_finish (x, xs);
2119 + return x;
2120 +}
2121 +
2122 +\f
2123 +/* Conversions and comparison to double. */
2124 +void
2125 +mpz_set_d (mpz_t r, double x)
2126 +{
2127 + int sign;
2128 + mp_ptr rp;
2129 + mp_size_t rn, i;
2130 + double B;
2131 + double Bi;
2132 + mp_limb_t f;
2133 +
2134 + /* x != x is true when x is a NaN, and x == x * 0.5 is true when x is
2135 + zero or infinity. */
2136 + if (x != x || x == x * 0.5)
2137 + {
2138 + r->_mp_size = 0;
2139 + return;
2140 + }
2141 +
2142 + sign = x < 0.0 ;
2143 + if (sign)
2144 + x = - x;
2145 +
2146 + if (x < 1.0)
2147 + {
2148 + r->_mp_size = 0;
2149 + return;
2150 + }
2151 + B = 2.0 * (double) GMP_LIMB_HIGHBIT;
2152 + Bi = 1.0 / B;
2153 + for (rn = 1; x >= B; rn++)
2154 + x *= Bi;
2155 +
2156 + rp = MPZ_REALLOC (r, rn);
2157 +
2158 + f = (mp_limb_t) x;
2159 + x -= f;
2160 + assert (x < 1.0);
2161 + i = rn-1;
2162 + rp[i] = f;
2163 + while (--i >= 0)
2164 + {
2165 + x = B * x;
2166 + f = (mp_limb_t) x;
2167 + x -= f;
2168 + assert (x < 1.0);
2169 + rp[i] = f;
2170 + }
2171 +
2172 + r->_mp_size = sign ? - rn : rn;
2173 +}
2174 +
2175 +void
2176 +mpz_init_set_d (mpz_t r, double x)
2177 +{
2178 + mpz_init (r);
2179 + mpz_set_d (r, x);
2180 +}
2181 +
2182 +double
2183 +mpz_get_d (const mpz_t u)
2184 +{
2185 + mp_size_t un;
2186 + double x;
2187 + double B = 2.0 * (double) GMP_LIMB_HIGHBIT;
2188 +
2189 + un = GMP_ABS (u->_mp_size);
2190 +
2191 + if (un == 0)
2192 + return 0.0;
2193 +
2194 + x = u->_mp_d[--un];
2195 + while (un > 0)
2196 + x = B*x + u->_mp_d[--un];
2197 +
2198 + if (u->_mp_size < 0)
2199 + x = -x;
2200 +
2201 + return x;
2202 +}
2203 +
2204 +int
2205 +mpz_cmpabs_d (const mpz_t x, double d)
2206 +{
2207 + mp_size_t xn;
2208 + double B, Bi;
2209 + mp_size_t i;
2210 +
2211 + xn = x->_mp_size;
2212 + d = GMP_ABS (d);
2213 +
2214 + if (xn != 0)
2215 + {
2216 + xn = GMP_ABS (xn);
2217 +
2218 + B = 2.0 * (double) GMP_LIMB_HIGHBIT;
2219 + Bi = 1.0 / B;
2220 +
2221 + /* Scale d so it can be compared with the top limb. */
2222 + for (i = 1; i < xn; i++)
2223 + d *= Bi;
2224 +
2225 + if (d >= B)
2226 + return -1;
2227 +
2228 + /* Compare floor(d) to top limb, subtract and cancel when equal. */
2229 + for (i = xn; i-- > 0;)
2230 + {
2231 + mp_limb_t f, xl;
2232 +
2233 + f = (mp_limb_t) d;
2234 + xl = x->_mp_d[i];
2235 + if (xl > f)
2236 + return 1;
2237 + else if (xl < f)
2238 + return -1;
2239 + d = B * (d - f);
2240 + }
2241 + }
2242 + return - (d > 0.0);
2243 +}
2244 +
2245 +int
2246 +mpz_cmp_d (const mpz_t x, double d)
2247 +{
2248 + if (x->_mp_size < 0)
2249 + {
2250 + if (d >= 0.0)
2251 + return -1;
2252 + else
2253 + return -mpz_cmpabs_d (x, d);
2254 + }
2255 + else
2256 + {
2257 + if (d < 0.0)
2258 + return 1;
2259 + else
2260 + return mpz_cmpabs_d (x, d);
2261 + }
2262 +}
2263 +
2264 +\f
2265 +/* MPZ comparisons and the like. */
2266 +int
2267 +mpz_sgn (const mpz_t u)
2268 +{
2269 + mp_size_t usize = u->_mp_size;
2270 +
2271 + return (usize > 0) - (usize < 0);
2272 +}
2273 +
2274 +int
2275 +mpz_cmp_si (const mpz_t u, long v)
2276 +{
2277 + mp_size_t usize = u->_mp_size;
2278 +
2279 + if (usize < -1)
2280 + return -1;
2281 + else if (v >= 0)
2282 + return mpz_cmp_ui (u, v);
2283 + else if (usize >= 0)
2284 + return 1;
2285 + else /* usize == -1 */
2286 + {
2287 + mp_limb_t ul = u->_mp_d[0];
2288 + if ((mp_limb_t)GMP_NEG_CAST (unsigned long int, v) < ul)
2289 + return -1;
2290 + else
2291 + return (mp_limb_t)GMP_NEG_CAST (unsigned long int, v) > ul;
2292 + }
2293 +}
2294 +
2295 +int
2296 +mpz_cmp_ui (const mpz_t u, unsigned long v)
2297 +{
2298 + mp_size_t usize = u->_mp_size;
2299 +
2300 + if (usize > 1)
2301 + return 1;
2302 + else if (usize < 0)
2303 + return -1;
2304 + else
2305 + {
2306 + mp_limb_t ul = (usize > 0) ? u->_mp_d[0] : 0;
2307 + return (ul > v) - (ul < v);
2308 + }
2309 +}
2310 +
2311 +int
2312 +mpz_cmp (const mpz_t a, const mpz_t b)
2313 +{
2314 + mp_size_t asize = a->_mp_size;
2315 + mp_size_t bsize = b->_mp_size;
2316 +
2317 + if (asize != bsize)
2318 + return (asize < bsize) ? -1 : 1;
2319 + else if (asize >= 0)
2320 + return mpn_cmp (a->_mp_d, b->_mp_d, asize);
2321 + else
2322 + return mpn_cmp (b->_mp_d, a->_mp_d, -asize);
2323 +}
2324 +
2325 +int
2326 +mpz_cmpabs_ui (const mpz_t u, unsigned long v)
2327 +{
2328 + mp_size_t un = GMP_ABS (u->_mp_size);
2329 + mp_limb_t ul;
2330 +
2331 + if (un > 1)
2332 + return 1;
2333 +
2334 + ul = (un == 1) ? u->_mp_d[0] : 0;
2335 +
2336 + return (ul > v) - (ul < v);
2337 +}
2338 +
2339 +int
2340 +mpz_cmpabs (const mpz_t u, const mpz_t v)
2341 +{
2342 + return mpn_cmp4 (u->_mp_d, GMP_ABS (u->_mp_size),
2343 + v->_mp_d, GMP_ABS (v->_mp_size));
2344 +}
2345 +
2346 +void
2347 +mpz_abs (mpz_t r, const mpz_t u)
2348 +{
2349 + if (r != u)
2350 + mpz_set (r, u);
2351 +
2352 + r->_mp_size = GMP_ABS (r->_mp_size);
2353 +}
2354 +
2355 +void
2356 +mpz_neg (mpz_t r, const mpz_t u)
2357 +{
2358 + if (r != u)
2359 + mpz_set (r, u);
2360 +
2361 + r->_mp_size = -r->_mp_size;
2362 +}
2363 +
2364 +void
2365 +mpz_swap (mpz_t u, mpz_t v)
2366 +{
2367 + MP_SIZE_T_SWAP (u->_mp_size, v->_mp_size);
2368 + MP_SIZE_T_SWAP (u->_mp_alloc, v->_mp_alloc);
2369 + MP_PTR_SWAP (u->_mp_d, v->_mp_d);
2370 +}
2371 +
2372 +\f
2373 +/* MPZ addition and subtraction */
2374 +
2375 +/* Adds to the absolute value. Returns new size, but doesn't store it. */
2376 +static mp_size_t
2377 +mpz_abs_add_ui (mpz_t r, const mpz_t a, unsigned long b)
2378 +{
2379 + mp_size_t an;
2380 + mp_ptr rp;
2381 + mp_limb_t cy;
2382 +
2383 + an = GMP_ABS (a->_mp_size);
2384 + if (an == 0)
2385 + {
2386 + r->_mp_d[0] = b;
2387 + return b > 0;
2388 + }
2389 +
2390 + rp = MPZ_REALLOC (r, an + 1);
2391 +
2392 + cy = mpn_add_1 (rp, a->_mp_d, an, b);
2393 + rp[an] = cy;
2394 + an += cy;
2395 +
2396 + return an;
2397 +}
2398 +
2399 +/* Subtract from the absolute value. Returns new size, (or -1 on underflow),
2400 + but doesn't store it. */
2401 +static mp_size_t
2402 +mpz_abs_sub_ui (mpz_t r, const mpz_t a, unsigned long b)
2403 +{
2404 + mp_size_t an = GMP_ABS (a->_mp_size);
2405 + mp_ptr rp = MPZ_REALLOC (r, an);
2406 +
2407 + if (an == 0)
2408 + {
2409 + rp[0] = b;
2410 + return -(b > 0);
2411 + }
2412 + else if (an == 1 && a->_mp_d[0] < b)
2413 + {
2414 + rp[0] = b - a->_mp_d[0];
2415 + return -1;
2416 + }
2417 + else
2418 + {
2419 + gmp_assert_nocarry (mpn_sub_1 (rp, a->_mp_d, an, b));
2420 + return mpn_normalized_size (rp, an);
2421 + }
2422 +}
2423 +
2424 +void
2425 +mpz_add_ui (mpz_t r, const mpz_t a, unsigned long b)
2426 +{
2427 + if (a->_mp_size >= 0)
2428 + r->_mp_size = mpz_abs_add_ui (r, a, b);
2429 + else
2430 + r->_mp_size = -mpz_abs_sub_ui (r, a, b);
2431 +}
2432 +
2433 +void
2434 +mpz_sub_ui (mpz_t r, const mpz_t a, unsigned long b)
2435 +{
2436 + if (a->_mp_size < 0)
2437 + r->_mp_size = -mpz_abs_add_ui (r, a, b);
2438 + else
2439 + r->_mp_size = mpz_abs_sub_ui (r, a, b);
2440 +}
2441 +
2442 +void
2443 +mpz_ui_sub (mpz_t r, unsigned long a, const mpz_t b)
2444 +{
2445 + if (b->_mp_size < 0)
2446 + r->_mp_size = mpz_abs_add_ui (r, b, a);
2447 + else
2448 + r->_mp_size = -mpz_abs_sub_ui (r, b, a);
2449 +}
2450 +
2451 +static mp_size_t
2452 +mpz_abs_add (mpz_t r, const mpz_t a, const mpz_t b)
2453 +{
2454 + mp_size_t an = GMP_ABS (a->_mp_size);
2455 + mp_size_t bn = GMP_ABS (b->_mp_size);
2456 + mp_ptr rp;
2457 + mp_limb_t cy;
2458 +
2459 + if (an < bn)
2460 + {
2461 + MPZ_SRCPTR_SWAP (a, b);
2462 + MP_SIZE_T_SWAP (an, bn);
2463 + }
2464 +
2465 + rp = MPZ_REALLOC (r, an + 1);
2466 + cy = mpn_add (rp, a->_mp_d, an, b->_mp_d, bn);
2467 +
2468 + rp[an] = cy;
2469 +
2470 + return an + cy;
2471 +}
2472 +
2473 +static mp_size_t
2474 +mpz_abs_sub (mpz_t r, const mpz_t a, const mpz_t b)
2475 +{
2476 + mp_size_t an = GMP_ABS (a->_mp_size);
2477 + mp_size_t bn = GMP_ABS (b->_mp_size);
2478 + int cmp;
2479 + mp_ptr rp;
2480 +
2481 + cmp = mpn_cmp4 (a->_mp_d, an, b->_mp_d, bn);
2482 + if (cmp > 0)
2483 + {
2484 + rp = MPZ_REALLOC (r, an);
2485 + gmp_assert_nocarry (mpn_sub (rp, a->_mp_d, an, b->_mp_d, bn));
2486 + return mpn_normalized_size (rp, an);
2487 + }
2488 + else if (cmp < 0)
2489 + {
2490 + rp = MPZ_REALLOC (r, bn);
2491 + gmp_assert_nocarry (mpn_sub (rp, b->_mp_d, bn, a->_mp_d, an));
2492 + return -mpn_normalized_size (rp, bn);
2493 + }
2494 + else
2495 + return 0;
2496 +}
2497 +
2498 +void
2499 +mpz_add (mpz_t r, const mpz_t a, const mpz_t b)
2500 +{
2501 + mp_size_t rn;
2502 +
2503 + if ( (a->_mp_size ^ b->_mp_size) >= 0)
2504 + rn = mpz_abs_add (r, a, b);
2505 + else
2506 + rn = mpz_abs_sub (r, a, b);
2507 +
2508 + r->_mp_size = a->_mp_size >= 0 ? rn : - rn;
2509 +}
2510 +
2511 +void
2512 +mpz_sub (mpz_t r, const mpz_t a, const mpz_t b)
2513 +{
2514 + mp_size_t rn;
2515 +
2516 + if ( (a->_mp_size ^ b->_mp_size) >= 0)
2517 + rn = mpz_abs_sub (r, a, b);
2518 + else
2519 + rn = mpz_abs_add (r, a, b);
2520 +
2521 + r->_mp_size = a->_mp_size >= 0 ? rn : - rn;
2522 +}
2523 +
2524 +\f
2525 +/* MPZ multiplication */
2526 +void
2527 +mpz_mul_si (mpz_t r, const mpz_t u, long int v)
2528 +{
2529 + if (v < 0)
2530 + {
2531 + mpz_mul_ui (r, u, GMP_NEG_CAST (unsigned long int, v));
2532 + mpz_neg (r, r);
2533 + }
2534 + else
2535 + mpz_mul_ui (r, u, (unsigned long int) v);
2536 +}
2537 +
2538 +void
2539 +mpz_mul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2540 +{
2541 + mp_size_t un, us;
2542 + mp_ptr tp;
2543 + mp_limb_t cy;
2544 +
2545 + us = u->_mp_size;
2546 +
2547 + if (us == 0 || v == 0)
2548 + {
2549 + r->_mp_size = 0;
2550 + return;
2551 + }
2552 +
2553 + un = GMP_ABS (us);
2554 +
2555 + tp = MPZ_REALLOC (r, un + 1);
2556 + cy = mpn_mul_1 (tp, u->_mp_d, un, v);
2557 + tp[un] = cy;
2558 +
2559 + un += (cy > 0);
2560 + r->_mp_size = (us < 0) ? - un : un;
2561 +}
2562 +
2563 +void
2564 +mpz_mul (mpz_t r, const mpz_t u, const mpz_t v)
2565 +{
2566 + int sign;
2567 + mp_size_t un, vn, rn;
2568 + mpz_t t;
2569 + mp_ptr tp;
2570 +
2571 + un = u->_mp_size;
2572 + vn = v->_mp_size;
2573 +
2574 + if (un == 0 || vn == 0)
2575 + {
2576 + r->_mp_size = 0;
2577 + return;
2578 + }
2579 +
2580 + sign = (un ^ vn) < 0;
2581 +
2582 + un = GMP_ABS (un);
2583 + vn = GMP_ABS (vn);
2584 +
2585 + mpz_init2 (t, (un + vn) * GMP_LIMB_BITS);
2586 +
2587 + tp = t->_mp_d;
2588 + if (un >= vn)
2589 + mpn_mul (tp, u->_mp_d, un, v->_mp_d, vn);
2590 + else
2591 + mpn_mul (tp, v->_mp_d, vn, u->_mp_d, un);
2592 +
2593 + rn = un + vn;
2594 + rn -= tp[rn-1] == 0;
2595 +
2596 + t->_mp_size = sign ? - rn : rn;
2597 + mpz_swap (r, t);
2598 + mpz_clear (t);
2599 +}
2600 +
2601 +void
2602 +mpz_mul_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bits)
2603 +{
2604 + mp_size_t un, rn;
2605 + mp_size_t limbs;
2606 + unsigned shift;
2607 + mp_ptr rp;
2608 +
2609 + un = GMP_ABS (u->_mp_size);
2610 + if (un == 0)
2611 + {
2612 + r->_mp_size = 0;
2613 + return;
2614 + }
2615 +
2616 + limbs = bits / GMP_LIMB_BITS;
2617 + shift = bits % GMP_LIMB_BITS;
2618 +
2619 + rn = un + limbs + (shift > 0);
2620 + rp = MPZ_REALLOC (r, rn);
2621 + if (shift > 0)
2622 + {
2623 + mp_limb_t cy = mpn_lshift (rp + limbs, u->_mp_d, un, shift);
2624 + rp[rn-1] = cy;
2625 + rn -= (cy == 0);
2626 + }
2627 + else
2628 + mpn_copyd (rp + limbs, u->_mp_d, un);
2629 +
2630 + while (limbs > 0)
2631 + rp[--limbs] = 0;
2632 +
2633 + r->_mp_size = (u->_mp_size < 0) ? - rn : rn;
2634 +}
2635 +
2636 +void
2637 +mpz_addmul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2638 +{
2639 + mpz_t t;
2640 + mpz_init (t);
2641 + mpz_mul_ui (t, u, v);
2642 + mpz_add (r, r, t);
2643 + mpz_clear (t);
2644 +}
2645 +
2646 +void
2647 +mpz_submul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2648 +{
2649 + mpz_t t;
2650 + mpz_init (t);
2651 + mpz_mul_ui (t, u, v);
2652 + mpz_sub (r, r, t);
2653 + mpz_clear (t);
2654 +}
2655 +
2656 +void
2657 +mpz_addmul (mpz_t r, const mpz_t u, const mpz_t v)
2658 +{
2659 + mpz_t t;
2660 + mpz_init (t);
2661 + mpz_mul (t, u, v);
2662 + mpz_add (r, r, t);
2663 + mpz_clear (t);
2664 +}
2665 +
2666 +void
2667 +mpz_submul (mpz_t r, const mpz_t u, const mpz_t v)
2668 +{
2669 + mpz_t t;
2670 + mpz_init (t);
2671 + mpz_mul (t, u, v);
2672 + mpz_sub (r, r, t);
2673 + mpz_clear (t);
2674 +}
2675 +
2676 +\f
2677 +/* MPZ division */
2678 +enum mpz_div_round_mode { GMP_DIV_FLOOR, GMP_DIV_CEIL, GMP_DIV_TRUNC };
2679 +
2680 +/* Allows q or r to be zero. Returns 1 iff remainder is non-zero. */
2681 +static int
2682 +mpz_div_qr (mpz_t q, mpz_t r,
2683 + const mpz_t n, const mpz_t d, enum mpz_div_round_mode mode)
2684 +{
2685 + mp_size_t ns, ds, nn, dn, qs;
2686 + ns = n->_mp_size;
2687 + ds = d->_mp_size;
2688 +
2689 + if (ds == 0)
2690 + gmp_die("mpz_div_qr: Divide by zero.");
2691 +
2692 + if (ns == 0)
2693 + {
2694 + if (q)
2695 + q->_mp_size = 0;
2696 + if (r)
2697 + r->_mp_size = 0;
2698 + return 0;
2699 + }
2700 +
2701 + nn = GMP_ABS (ns);
2702 + dn = GMP_ABS (ds);
2703 +
2704 + qs = ds ^ ns;
2705 +
2706 + if (nn < dn)
2707 + {
2708 + if (mode == GMP_DIV_CEIL && qs >= 0)
2709 + {
2710 + /* q = 1, r = n - d */
2711 + if (r)
2712 + mpz_sub (r, n, d);
2713 + if (q)
2714 + mpz_set_ui (q, 1);
2715 + }
2716 + else if (mode == GMP_DIV_FLOOR && qs < 0)
2717 + {
2718 + /* q = -1, r = n + d */
2719 + if (r)
2720 + mpz_add (r, n, d);
2721 + if (q)
2722 + mpz_set_si (q, -1);
2723 + }
2724 + else
2725 + {
2726 + /* q = 0, r = d */
2727 + if (r)
2728 + mpz_set (r, n);
2729 + if (q)
2730 + q->_mp_size = 0;
2731 + }
2732 + return 1;
2733 + }
2734 + else
2735 + {
2736 + mp_ptr np, qp;
2737 + mp_size_t qn, rn;
2738 + mpz_t tq, tr;
2739 +
2740 + mpz_init_set (tr, n);
2741 + np = tr->_mp_d;
2742 +
2743 + qn = nn - dn + 1;
2744 +
2745 + if (q)
2746 + {
2747 + mpz_init2 (tq, qn * GMP_LIMB_BITS);
2748 + qp = tq->_mp_d;
2749 + }
2750 + else
2751 + qp = NULL;
2752 +
2753 + mpn_div_qr (qp, np, nn, d->_mp_d, dn);
2754 +
2755 + if (qp)
2756 + {
2757 + qn -= (qp[qn-1] == 0);
2758 +
2759 + tq->_mp_size = qs < 0 ? -qn : qn;
2760 + }
2761 + rn = mpn_normalized_size (np, dn);
2762 + tr->_mp_size = ns < 0 ? - rn : rn;
2763 +
2764 + if (mode == GMP_DIV_FLOOR && qs < 0 && rn != 0)
2765 + {
2766 + if (q)
2767 + mpz_sub_ui (tq, tq, 1);
2768 + if (r)
2769 + mpz_add (tr, tr, d);
2770 + }
2771 + else if (mode == GMP_DIV_CEIL && qs >= 0 && rn != 0)
2772 + {
2773 + if (q)
2774 + mpz_add_ui (tq, tq, 1);
2775 + if (r)
2776 + mpz_sub (tr, tr, d);
2777 + }
2778 +
2779 + if (q)
2780 + {
2781 + mpz_swap (tq, q);
2782 + mpz_clear (tq);
2783 + }
2784 + if (r)
2785 + mpz_swap (tr, r);
2786 +
2787 + mpz_clear (tr);
2788 +
2789 + return rn != 0;
2790 + }
2791 +}
2792 +
2793 +void
2794 +mpz_cdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2795 +{
2796 + mpz_div_qr (q, r, n, d, GMP_DIV_CEIL);
2797 +}
2798 +
2799 +void
2800 +mpz_fdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2801 +{
2802 + mpz_div_qr (q, r, n, d, GMP_DIV_FLOOR);
2803 +}
2804 +
2805 +void
2806 +mpz_tdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2807 +{
2808 + mpz_div_qr (q, r, n, d, GMP_DIV_TRUNC);
2809 +}
2810 +
2811 +void
2812 +mpz_cdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2813 +{
2814 + mpz_div_qr (q, NULL, n, d, GMP_DIV_CEIL);
2815 +}
2816 +
2817 +void
2818 +mpz_fdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2819 +{
2820 + mpz_div_qr (q, NULL, n, d, GMP_DIV_FLOOR);
2821 +}
2822 +
2823 +void
2824 +mpz_tdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2825 +{
2826 + mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC);
2827 +}
2828 +
2829 +void
2830 +mpz_cdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2831 +{
2832 + mpz_div_qr (NULL, r, n, d, GMP_DIV_CEIL);
2833 +}
2834 +
2835 +void
2836 +mpz_fdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2837 +{
2838 + mpz_div_qr (NULL, r, n, d, GMP_DIV_FLOOR);
2839 +}
2840 +
2841 +void
2842 +mpz_tdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2843 +{
2844 + mpz_div_qr (NULL, r, n, d, GMP_DIV_TRUNC);
2845 +}
2846 +
2847 +void
2848 +mpz_mod (mpz_t r, const mpz_t n, const mpz_t d)
2849 +{
2850 + mpz_div_qr (NULL, r, n, d, d->_mp_size >= 0 ? GMP_DIV_FLOOR : GMP_DIV_CEIL);
2851 +}
2852 +
2853 +static void
2854 +mpz_div_q_2exp (mpz_t q, const mpz_t u, mp_bitcnt_t bit_index,
2855 + enum mpz_div_round_mode mode)
2856 +{
2857 + mp_size_t un, qn;
2858 + mp_size_t limb_cnt;
2859 + mp_ptr qp;
2860 + int adjust;
2861 +
2862 + un = u->_mp_size;
2863 + if (un == 0)
2864 + {
2865 + q->_mp_size = 0;
2866 + return;
2867 + }
2868 + limb_cnt = bit_index / GMP_LIMB_BITS;
2869 + qn = GMP_ABS (un) - limb_cnt;
2870 + bit_index %= GMP_LIMB_BITS;
2871 +
2872 + if (mode == ((un > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* un != 0 here. */
2873 + /* Note: Below, the final indexing at limb_cnt is valid because at
2874 + that point we have qn > 0. */
2875 + adjust = (qn <= 0
2876 + || !mpn_zero_p (u->_mp_d, limb_cnt)
2877 + || (u->_mp_d[limb_cnt]
2878 + & (((mp_limb_t) 1 << bit_index) - 1)));
2879 + else
2880 + adjust = 0;
2881 +
2882 + if (qn <= 0)
2883 + qn = 0;
2884 +
2885 + else
2886 + {
2887 + qp = MPZ_REALLOC (q, qn);
2888 +
2889 + if (bit_index != 0)
2890 + {
2891 + mpn_rshift (qp, u->_mp_d + limb_cnt, qn, bit_index);
2892 + qn -= qp[qn - 1] == 0;
2893 + }
2894 + else
2895 + {
2896 + mpn_copyi (qp, u->_mp_d + limb_cnt, qn);
2897 + }
2898 + }
2899 +
2900 + q->_mp_size = qn;
2901 +
2902 + if (adjust)
2903 + mpz_add_ui (q, q, 1);
2904 + if (un < 0)
2905 + mpz_neg (q, q);
2906 +}
2907 +
2908 +static void
2909 +mpz_div_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bit_index,
2910 + enum mpz_div_round_mode mode)
2911 +{
2912 + mp_size_t us, un, rn;
2913 + mp_ptr rp;
2914 + mp_limb_t mask;
2915 +
2916 + us = u->_mp_size;
2917 + if (us == 0 || bit_index == 0)
2918 + {
2919 + r->_mp_size = 0;
2920 + return;
2921 + }
2922 + rn = (bit_index + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
2923 + assert (rn > 0);
2924 +
2925 + rp = MPZ_REALLOC (r, rn);
2926 + un = GMP_ABS (us);
2927 +
2928 + mask = GMP_LIMB_MAX >> (rn * GMP_LIMB_BITS - bit_index);
2929 +
2930 + if (rn > un)
2931 + {
2932 + /* Quotient (with truncation) is zero, and remainder is
2933 + non-zero */
2934 + if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
2935 + {
2936 + /* Have to negate and sign extend. */
2937 + mp_size_t i;
2938 + mp_limb_t cy;
2939 +
2940 + for (cy = 1, i = 0; i < un; i++)
2941 + {
2942 + mp_limb_t s = ~u->_mp_d[i] + cy;
2943 + cy = s < cy;
2944 + rp[i] = s;
2945 + }
2946 + assert (cy == 0);
2947 + for (; i < rn - 1; i++)
2948 + rp[i] = GMP_LIMB_MAX;
2949 +
2950 + rp[rn-1] = mask;
2951 + us = -us;
2952 + }
2953 + else
2954 + {
2955 + /* Just copy */
2956 + if (r != u)
2957 + mpn_copyi (rp, u->_mp_d, un);
2958 +
2959 + rn = un;
2960 + }
2961 + }
2962 + else
2963 + {
2964 + if (r != u)
2965 + mpn_copyi (rp, u->_mp_d, rn - 1);
2966 +
2967 + rp[rn-1] = u->_mp_d[rn-1] & mask;
2968 +
2969 + if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
2970 + {
2971 + /* If r != 0, compute 2^{bit_count} - r. */
2972 + mp_size_t i;
2973 +
2974 + for (i = 0; i < rn && rp[i] == 0; i++)
2975 + ;
2976 + if (i < rn)
2977 + {
2978 + /* r > 0, need to flip sign. */
2979 + rp[i] = ~rp[i] + 1;
2980 + while (++i < rn)
2981 + rp[i] = ~rp[i];
2982 +
2983 + rp[rn-1] &= mask;
2984 +
2985 + /* us is not used for anything else, so we can modify it
2986 + here to indicate flipped sign. */
2987 + us = -us;
2988 + }
2989 + }
2990 + }
2991 + rn = mpn_normalized_size (rp, rn);
2992 + r->_mp_size = us < 0 ? -rn : rn;
2993 +}
2994 +
2995 +void
2996 +mpz_cdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2997 +{
2998 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_CEIL);
2999 +}
3000 +
3001 +void
3002 +mpz_fdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
3003 +{
3004 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_FLOOR);
3005 +}
3006 +
3007 +void
3008 +mpz_tdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
3009 +{
3010 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_TRUNC);
3011 +}
3012 +
3013 +void
3014 +mpz_cdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
3015 +{
3016 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_CEIL);
3017 +}
3018 +
3019 +void
3020 +mpz_fdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
3021 +{
3022 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_FLOOR);
3023 +}
3024 +
3025 +void
3026 +mpz_tdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
3027 +{
3028 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_TRUNC);
3029 +}
3030 +
3031 +void
3032 +mpz_divexact (mpz_t q, const mpz_t n, const mpz_t d)
3033 +{
3034 + gmp_assert_nocarry (mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC));
3035 +}
3036 +
3037 +int
3038 +mpz_divisible_p (const mpz_t n, const mpz_t d)
3039 +{
3040 + return mpz_div_qr (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
3041 +}
3042 +
3043 +int
3044 +mpz_congruent_p (const mpz_t a, const mpz_t b, const mpz_t m)
3045 +{
3046 + mpz_t t;
3047 + int res;
3048 +
3049 + /* a == b (mod 0) iff a == b */
3050 + if (mpz_sgn (m) == 0)
3051 + return (mpz_cmp (a, b) == 0);
3052 +
3053 + mpz_init (t);
3054 + mpz_sub (t, a, b);
3055 + res = mpz_divisible_p (t, m);
3056 + mpz_clear (t);
3057 +
3058 + return res;
3059 +}
3060 +
3061 +static unsigned long
3062 +mpz_div_qr_ui (mpz_t q, mpz_t r,
3063 + const mpz_t n, unsigned long d, enum mpz_div_round_mode mode)
3064 +{
3065 + mp_size_t ns, qn;
3066 + mp_ptr qp;
3067 + mp_limb_t rl;
3068 + mp_size_t rs;
3069 +
3070 + ns = n->_mp_size;
3071 + if (ns == 0)
3072 + {
3073 + if (q)
3074 + q->_mp_size = 0;
3075 + if (r)
3076 + r->_mp_size = 0;
3077 + return 0;
3078 + }
3079 +
3080 + qn = GMP_ABS (ns);
3081 + if (q)
3082 + qp = MPZ_REALLOC (q, qn);
3083 + else
3084 + qp = NULL;
3085 +
3086 + rl = mpn_div_qr_1 (qp, n->_mp_d, qn, d);
3087 + assert (rl < d);
3088 +
3089 + rs = rl > 0;
3090 + rs = (ns < 0) ? -rs : rs;
3091 +
3092 + if (rl > 0 && ( (mode == GMP_DIV_FLOOR && ns < 0)
3093 + || (mode == GMP_DIV_CEIL && ns >= 0)))
3094 + {
3095 + if (q)
3096 + gmp_assert_nocarry (mpn_add_1 (qp, qp, qn, 1));
3097 + rl = d - rl;
3098 + rs = -rs;
3099 + }
3100 +
3101 + if (r)
3102 + {
3103 + r->_mp_d[0] = rl;
3104 + r->_mp_size = rs;
3105 + }
3106 + if (q)
3107 + {
3108 + qn -= (qp[qn-1] == 0);
3109 + assert (qn == 0 || qp[qn-1] > 0);
3110 +
3111 + q->_mp_size = (ns < 0) ? - qn : qn;
3112 + }
3113 +
3114 + return rl;
3115 +}
3116 +
3117 +unsigned long
3118 +mpz_cdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
3119 +{
3120 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_CEIL);
3121 +}
3122 +
3123 +unsigned long
3124 +mpz_fdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
3125 +{
3126 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_FLOOR);
3127 +}
3128 +
3129 +unsigned long
3130 +mpz_tdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
3131 +{
3132 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_TRUNC);
3133 +}
3134 +
3135 +unsigned long
3136 +mpz_cdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
3137 +{
3138 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_CEIL);
3139 +}
3140 +
3141 +unsigned long
3142 +mpz_fdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
3143 +{
3144 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_FLOOR);
3145 +}
3146 +
3147 +unsigned long
3148 +mpz_tdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
3149 +{
3150 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC);
3151 +}
3152 +
3153 +unsigned long
3154 +mpz_cdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
3155 +{
3156 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_CEIL);
3157 +}
3158 +unsigned long
3159 +mpz_fdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
3160 +{
3161 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
3162 +}
3163 +unsigned long
3164 +mpz_tdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
3165 +{
3166 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_TRUNC);
3167 +}
3168 +
3169 +unsigned long
3170 +mpz_cdiv_ui (const mpz_t n, unsigned long d)
3171 +{
3172 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_CEIL);
3173 +}
3174 +
3175 +unsigned long
3176 +mpz_fdiv_ui (const mpz_t n, unsigned long d)
3177 +{
3178 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_FLOOR);
3179 +}
3180 +
3181 +unsigned long
3182 +mpz_tdiv_ui (const mpz_t n, unsigned long d)
3183 +{
3184 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC);
3185 +}
3186 +
3187 +unsigned long
3188 +mpz_mod_ui (mpz_t r, const mpz_t n, unsigned long d)
3189 +{
3190 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
3191 +}
3192 +
3193 +void
3194 +mpz_divexact_ui (mpz_t q, const mpz_t n, unsigned long d)
3195 +{
3196 + gmp_assert_nocarry (mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC));
3197 +}
3198 +
3199 +int
3200 +mpz_divisible_ui_p (const mpz_t n, unsigned long d)
3201 +{
3202 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
3203 +}
3204 +
3205 +\f
3206 +/* GCD */
3207 +static mp_limb_t
3208 +mpn_gcd_11 (mp_limb_t u, mp_limb_t v)
3209 +{
3210 + unsigned shift;
3211 +
3212 + assert ( (u | v) > 0);
3213 +
3214 + if (u == 0)
3215 + return v;
3216 + else if (v == 0)
3217 + return u;
3218 +
3219 + gmp_ctz (shift, u | v);
3220 +
3221 + u >>= shift;
3222 + v >>= shift;
3223 +
3224 + if ( (u & 1) == 0)
3225 + MP_LIMB_T_SWAP (u, v);
3226 +
3227 + while ( (v & 1) == 0)
3228 + v >>= 1;
3229 +
3230 + while (u != v)
3231 + {
3232 + if (u > v)
3233 + {
3234 + u -= v;
3235 + do
3236 + u >>= 1;
3237 + while ( (u & 1) == 0);
3238 + }
3239 + else
3240 + {
3241 + v -= u;
3242 + do
3243 + v >>= 1;
3244 + while ( (v & 1) == 0);
3245 + }
3246 + }
3247 + return u << shift;
3248 +}
3249 +
3250 +unsigned long
3251 +mpz_gcd_ui (mpz_t g, const mpz_t u, unsigned long v)
3252 +{
3253 + mp_size_t un;
3254 +
3255 + if (v == 0)
3256 + {
3257 + if (g)
3258 + mpz_abs (g, u);
3259 + }
3260 + else
3261 + {
3262 + un = GMP_ABS (u->_mp_size);
3263 + if (un != 0)
3264 + v = mpn_gcd_11 (mpn_div_qr_1 (NULL, u->_mp_d, un, v), v);
3265 +
3266 + if (g)
3267 + mpz_set_ui (g, v);
3268 + }
3269 +
3270 + return v;
3271 +}
3272 +
3273 +static mp_bitcnt_t
3274 +mpz_make_odd (mpz_t r)
3275 +{
3276 + mp_bitcnt_t shift;
3277 +
3278 + assert (r->_mp_size > 0);
3279 + /* Count trailing zeros, equivalent to mpn_scan1, because we know that there is a 1 */
3280 + shift = mpn_common_scan (r->_mp_d[0], 0, r->_mp_d, 0, 0);
3281 + mpz_tdiv_q_2exp (r, r, shift);
3282 +
3283 + return shift;
3284 +}
3285 +
3286 +void
3287 +mpz_gcd (mpz_t g, const mpz_t u, const mpz_t v)
3288 +{
3289 + mpz_t tu, tv;
3290 + mp_bitcnt_t uz, vz, gz;
3291 +
3292 + if (u->_mp_size == 0)
3293 + {
3294 + mpz_abs (g, v);
3295 + return;
3296 + }
3297 + if (v->_mp_size == 0)
3298 + {
3299 + mpz_abs (g, u);
3300 + return;
3301 + }
3302 +
3303 + mpz_init (tu);
3304 + mpz_init (tv);
3305 +
3306 + mpz_abs (tu, u);
3307 + uz = mpz_make_odd (tu);
3308 + mpz_abs (tv, v);
3309 + vz = mpz_make_odd (tv);
3310 + gz = GMP_MIN (uz, vz);
3311 +
3312 + if (tu->_mp_size < tv->_mp_size)
3313 + mpz_swap (tu, tv);
3314 +
3315 + mpz_tdiv_r (tu, tu, tv);
3316 + if (tu->_mp_size == 0)
3317 + {
3318 + mpz_swap (g, tv);
3319 + }
3320 + else
3321 + for (;;)
3322 + {
3323 + int c;
3324 +
3325 + mpz_make_odd (tu);
3326 + c = mpz_cmp (tu, tv);
3327 + if (c == 0)
3328 + {
3329 + mpz_swap (g, tu);
3330 + break;
3331 + }
3332 + if (c < 0)
3333 + mpz_swap (tu, tv);
3334 +
3335 + if (tv->_mp_size == 1)
3336 + {
3337 + mp_limb_t vl = tv->_mp_d[0];
3338 + mp_limb_t ul = mpz_tdiv_ui (tu, vl);
3339 + mpz_set_ui (g, mpn_gcd_11 (ul, vl));
3340 + break;
3341 + }
3342 + mpz_sub (tu, tu, tv);
3343 + }
3344 + mpz_clear (tu);
3345 + mpz_clear (tv);
3346 + mpz_mul_2exp (g, g, gz);
3347 +}
3348 +
3349 +void
3350 +mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, const mpz_t u, const mpz_t v)
3351 +{
3352 + mpz_t tu, tv, s0, s1, t0, t1;
3353 + mp_bitcnt_t uz, vz, gz;
3354 + mp_bitcnt_t power;
3355 +
3356 + if (u->_mp_size == 0)
3357 + {
3358 + /* g = 0 u + sgn(v) v */
3359 + signed long sign = mpz_sgn (v);
3360 + mpz_abs (g, v);
3361 + if (s)
3362 + mpz_set_ui (s, 0);
3363 + if (t)
3364 + mpz_set_si (t, sign);
3365 + return;
3366 + }
3367 +
3368 + if (v->_mp_size == 0)
3369 + {
3370 + /* g = sgn(u) u + 0 v */
3371 + signed long sign = mpz_sgn (u);
3372 + mpz_abs (g, u);
3373 + if (s)
3374 + mpz_set_si (s, sign);
3375 + if (t)
3376 + mpz_set_ui (t, 0);
3377 + return;
3378 + }
3379 +
3380 + mpz_init (tu);
3381 + mpz_init (tv);
3382 + mpz_init (s0);
3383 + mpz_init (s1);
3384 + mpz_init (t0);
3385 + mpz_init (t1);
3386 +
3387 + mpz_abs (tu, u);
3388 + uz = mpz_make_odd (tu);
3389 + mpz_abs (tv, v);
3390 + vz = mpz_make_odd (tv);
3391 + gz = GMP_MIN (uz, vz);
3392 +
3393 + uz -= gz;
3394 + vz -= gz;
3395 +
3396 + /* Cofactors corresponding to odd gcd. gz handled later. */
3397 + if (tu->_mp_size < tv->_mp_size)
3398 + {
3399 + mpz_swap (tu, tv);
3400 + MPZ_SRCPTR_SWAP (u, v);
3401 + MPZ_PTR_SWAP (s, t);
3402 + MP_BITCNT_T_SWAP (uz, vz);
3403 + }
3404 +
3405 + /* Maintain
3406 + *
3407 + * u = t0 tu + t1 tv
3408 + * v = s0 tu + s1 tv
3409 + *
3410 + * where u and v denote the inputs with common factors of two
3411 + * eliminated, and det (s0, t0; s1, t1) = 2^p. Then
3412 + *
3413 + * 2^p tu = s1 u - t1 v
3414 + * 2^p tv = -s0 u + t0 v
3415 + */
3416 +
3417 + /* After initial division, tu = q tv + tu', we have
3418 + *
3419 + * u = 2^uz (tu' + q tv)
3420 + * v = 2^vz tv
3421 + *
3422 + * or
3423 + *
3424 + * t0 = 2^uz, t1 = 2^uz q
3425 + * s0 = 0, s1 = 2^vz
3426 + */
3427 +
3428 + mpz_setbit (t0, uz);
3429 + mpz_tdiv_qr (t1, tu, tu, tv);
3430 + mpz_mul_2exp (t1, t1, uz);
3431 +
3432 + mpz_setbit (s1, vz);
3433 + power = uz + vz;
3434 +
3435 + if (tu->_mp_size > 0)
3436 + {
3437 + mp_bitcnt_t shift;
3438 + shift = mpz_make_odd (tu);
3439 + mpz_mul_2exp (t0, t0, shift);
3440 + mpz_mul_2exp (s0, s0, shift);
3441 + power += shift;
3442 +
3443 + for (;;)
3444 + {
3445 + int c;
3446 + c = mpz_cmp (tu, tv);
3447 + if (c == 0)
3448 + break;
3449 +
3450 + if (c < 0)
3451 + {
3452 + /* tv = tv' + tu
3453 + *
3454 + * u = t0 tu + t1 (tv' + tu) = (t0 + t1) tu + t1 tv'
3455 + * v = s0 tu + s1 (tv' + tu) = (s0 + s1) tu + s1 tv' */
3456 +
3457 + mpz_sub (tv, tv, tu);
3458 + mpz_add (t0, t0, t1);
3459 + mpz_add (s0, s0, s1);
3460 +
3461 + shift = mpz_make_odd (tv);
3462 + mpz_mul_2exp (t1, t1, shift);
3463 + mpz_mul_2exp (s1, s1, shift);
3464 + }
3465 + else
3466 + {
3467 + mpz_sub (tu, tu, tv);
3468 + mpz_add (t1, t0, t1);
3469 + mpz_add (s1, s0, s1);
3470 +
3471 + shift = mpz_make_odd (tu);
3472 + mpz_mul_2exp (t0, t0, shift);
3473 + mpz_mul_2exp (s0, s0, shift);
3474 + }
3475 + power += shift;
3476 + }
3477 + }
3478 +
3479 + /* Now tv = odd part of gcd, and -s0 and t0 are corresponding
3480 + cofactors. */
3481 +
3482 + mpz_mul_2exp (tv, tv, gz);
3483 + mpz_neg (s0, s0);
3484 +
3485 + /* 2^p g = s0 u + t0 v. Eliminate one factor of two at a time. To
3486 + adjust cofactors, we need u / g and v / g */
3487 +
3488 + mpz_divexact (s1, v, tv);
3489 + mpz_abs (s1, s1);
3490 + mpz_divexact (t1, u, tv);
3491 + mpz_abs (t1, t1);
3492 +
3493 + while (power-- > 0)
3494 + {
3495 + /* s0 u + t0 v = (s0 - v/g) u - (t0 + u/g) v */
3496 + if (mpz_odd_p (s0) || mpz_odd_p (t0))
3497 + {
3498 + mpz_sub (s0, s0, s1);
3499 + mpz_add (t0, t0, t1);
3500 + }
3501 + mpz_divexact_ui (s0, s0, 2);
3502 + mpz_divexact_ui (t0, t0, 2);
3503 + }
3504 +
3505 + /* Arrange so that |s| < |u| / 2g */
3506 + mpz_add (s1, s0, s1);
3507 + if (mpz_cmpabs (s0, s1) > 0)
3508 + {
3509 + mpz_swap (s0, s1);
3510 + mpz_sub (t0, t0, t1);
3511 + }
3512 + if (u->_mp_size < 0)
3513 + mpz_neg (s0, s0);
3514 + if (v->_mp_size < 0)
3515 + mpz_neg (t0, t0);
3516 +
3517 + mpz_swap (g, tv);
3518 + if (s)
3519 + mpz_swap (s, s0);
3520 + if (t)
3521 + mpz_swap (t, t0);
3522 +
3523 + mpz_clear (tu);
3524 + mpz_clear (tv);
3525 + mpz_clear (s0);
3526 + mpz_clear (s1);
3527 + mpz_clear (t0);
3528 + mpz_clear (t1);
3529 +}
3530 +
3531 +void
3532 +mpz_lcm (mpz_t r, const mpz_t u, const mpz_t v)
3533 +{
3534 + mpz_t g;
3535 +
3536 + if (u->_mp_size == 0 || v->_mp_size == 0)
3537 + {
3538 + r->_mp_size = 0;
3539 + return;
3540 + }
3541 +
3542 + mpz_init (g);
3543 +
3544 + mpz_gcd (g, u, v);
3545 + mpz_divexact (g, u, g);
3546 + mpz_mul (r, g, v);
3547 +
3548 + mpz_clear (g);
3549 + mpz_abs (r, r);
3550 +}
3551 +
3552 +void
3553 +mpz_lcm_ui (mpz_t r, const mpz_t u, unsigned long v)
3554 +{
3555 + if (v == 0 || u->_mp_size == 0)
3556 + {
3557 + r->_mp_size = 0;
3558 + return;
3559 + }
3560 +
3561 + v /= mpz_gcd_ui (NULL, u, v);
3562 + mpz_mul_ui (r, u, v);
3563 +
3564 + mpz_abs (r, r);
3565 +}
3566 +
3567 +int
3568 +mpz_invert (mpz_t r, const mpz_t u, const mpz_t m)
3569 +{
3570 + mpz_t g, tr;
3571 + int invertible;
3572 +
3573 + if (u->_mp_size == 0 || mpz_cmpabs_ui (m, 1) <= 0)
3574 + return 0;
3575 +
3576 + mpz_init (g);
3577 + mpz_init (tr);
3578 +
3579 + mpz_gcdext (g, tr, NULL, u, m);
3580 + invertible = (mpz_cmp_ui (g, 1) == 0);
3581 +
3582 + if (invertible)
3583 + {
3584 + if (tr->_mp_size < 0)
3585 + {
3586 + if (m->_mp_size >= 0)
3587 + mpz_add (tr, tr, m);
3588 + else
3589 + mpz_sub (tr, tr, m);
3590 + }
3591 + mpz_swap (r, tr);
3592 + }
3593 +
3594 + mpz_clear (g);
3595 + mpz_clear (tr);
3596 + return invertible;
3597 +}
3598 +
3599 +\f
3600 +/* Higher level operations (sqrt, pow and root) */
3601 +
3602 +void
3603 +mpz_pow_ui (mpz_t r, const mpz_t b, unsigned long e)
3604 +{
3605 + unsigned long bit;
3606 + mpz_t tr;
3607 + mpz_init_set_ui (tr, 1);
3608 +
3609 + bit = GMP_ULONG_HIGHBIT;
3610 + do
3611 + {
3612 + mpz_mul (tr, tr, tr);
3613 + if (e & bit)
3614 + mpz_mul (tr, tr, b);
3615 + bit >>= 1;
3616 + }
3617 + while (bit > 0);
3618 +
3619 + mpz_swap (r, tr);
3620 + mpz_clear (tr);
3621 +}
3622 +
3623 +void
3624 +mpz_ui_pow_ui (mpz_t r, unsigned long blimb, unsigned long e)
3625 +{
3626 + mpz_t b;
3627 + mpz_init_set_ui (b, blimb);
3628 + mpz_pow_ui (r, b, e);
3629 + mpz_clear (b);
3630 +}
3631 +
3632 +void
3633 +mpz_powm (mpz_t r, const mpz_t b, const mpz_t e, const mpz_t m)
3634 +{
3635 + mpz_t tr;
3636 + mpz_t base;
3637 + mp_size_t en, mn;
3638 + mp_srcptr mp;
3639 + struct gmp_div_inverse minv;
3640 + unsigned shift;
3641 + mp_ptr tp = NULL;
3642 +
3643 + en = GMP_ABS (e->_mp_size);
3644 + mn = GMP_ABS (m->_mp_size);
3645 + if (mn == 0)
3646 + gmp_die ("mpz_powm: Zero modulo.");
3647 +
3648 + if (en == 0)
3649 + {
3650 + mpz_set_ui (r, 1);
3651 + return;
3652 + }
3653 +
3654 + mp = m->_mp_d;
3655 + mpn_div_qr_invert (&minv, mp, mn);
3656 + shift = minv.shift;
3657 +
3658 + if (shift > 0)
3659 + {
3660 + /* To avoid shifts, we do all our reductions, except the final
3661 + one, using a *normalized* m. */
3662 + minv.shift = 0;
3663 +
3664 + tp = gmp_xalloc_limbs (mn);
3665 + gmp_assert_nocarry (mpn_lshift (tp, mp, mn, shift));
3666 + mp = tp;
3667 + }
3668 +
3669 + mpz_init (base);
3670 +
3671 + if (e->_mp_size < 0)
3672 + {
3673 + if (!mpz_invert (base, b, m))
3674 + gmp_die ("mpz_powm: Negative exponent and non-invertible base.");
3675 + }
3676 + else
3677 + {
3678 + mp_size_t bn;
3679 + mpz_abs (base, b);
3680 +
3681 + bn = base->_mp_size;
3682 + if (bn >= mn)
3683 + {
3684 + mpn_div_qr_preinv (NULL, base->_mp_d, base->_mp_size, mp, mn, &minv);
3685 + bn = mn;
3686 + }
3687 +
3688 + /* We have reduced the absolute value. Now take care of the
3689 + sign. Note that we get zero represented non-canonically as
3690 + m. */
3691 + if (b->_mp_size < 0)
3692 + {
3693 + mp_ptr bp = MPZ_REALLOC (base, mn);
3694 + gmp_assert_nocarry (mpn_sub (bp, mp, mn, bp, bn));
3695 + bn = mn;
3696 + }
3697 + base->_mp_size = mpn_normalized_size (base->_mp_d, bn);
3698 + }
3699 + mpz_init_set_ui (tr, 1);
3700 +
3701 + while (en-- > 0)
3702 + {
3703 + mp_limb_t w = e->_mp_d[en];
3704 + mp_limb_t bit;
3705 +
3706 + bit = GMP_LIMB_HIGHBIT;
3707 + do