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