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
6 This introduces --without-libgmp which includes mini-gmp into nft
7 (adding ~30k) but avoids linking libgmp which is >400k.
9 This is useful for embedded distributions not using gmp otherwise.
11 Note: currently --without-libgmp must be used with --disable-debug.
13 Signed-off-by: Steven Barth <cyrus@openwrt.org>
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 +-
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
36 @@ -72,8 +72,19 @@ AM_CONDITIONAL([BUILD_PDF], [test "$DBLA
37 PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3])
38 PKG_CHECK_MODULES([LIBNFTNL], [libnftnl >= 1.0.2])
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)])], [],
45 +AS_IF([test "x$with_libgmp" != xno], [
46 +AC_CHECK_LIB([gmp],[__gmpz_init], , AC_MSG_ERROR([No suitable version of libgmp found]))
47 +AC_DEFINE([HAVE_LIBGMP], [1], [])
49 +AM_CONDITIONAL([BUILD_MINIGMP], [test "x$with_libgmp" == xno])
52 +AS_IF([test "x$with_libgmp" != xyes -a "x$CONFIG_DEBUG" = xy], [
53 +AC_MSG_ERROR([--without-libgmp MUST be used with --disable-debug])
56 AC_ARG_WITH([cli], [AS_HELP_STRING([--without-cli],
57 [disable interactive CLI (libreadline support)])],
59 +++ b/include/bignum.h
61 +#ifndef NFTABLES_BIGNUM_H
62 +#define NFTABLES_BIGNUM_H
69 +#include <mini-gmp.h>
73 +int gmp_printf(const char *format, const mpz_t value);
77 +#endif /* NFTABLES_BIGNUM_H */
78 --- a/include/expression.h
79 +++ b/include/expression.h
81 #define NFTABLES_EXPRESSION_H
86 #include <linux/netfilter/nf_tables.h>
89 --- a/include/gmputil.h
90 +++ b/include/gmputil.h
92 #ifndef NFTABLES_GMPUTIL_H
93 #define NFTABLES_GMPUTIL_H
97 #include <asm/byteorder.h>
101 +++ b/include/mini-gmp.h
103 +/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
105 +Copyright 2011-2014 Free Software Foundation, Inc.
107 +This file is part of the GNU MP Library.
109 +The GNU MP Library is free software; you can redistribute it and/or modify
110 +it under the terms of either:
112 + * the GNU Lesser General Public License as published by the Free
113 + Software Foundation; either version 3 of the License, or (at your
114 + option) any later version.
118 + * the GNU General Public License as published by the Free Software
119 + Foundation; either version 2 of the License, or (at your option) any
122 +or both in parallel, as here.
124 +The GNU MP Library is distributed in the hope that it will be useful, but
125 +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
126 +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
129 +You should have received copies of the GNU General Public License and the
130 +GNU Lesser General Public License along with the GNU MP Library. If not,
131 +see https://www.gnu.org/licenses/. */
133 +/* About mini-gmp: This is a minimal implementation of a subset of the
134 + GMP interface. It is intended for inclusion into applications which
135 + have modest bignums needs, as a fallback when the real GMP library
138 + This file defines the public interface. */
140 +#ifndef __MINI_GMP_H__
141 +#define __MINI_GMP_H__
146 +#if defined (__cplusplus)
150 +void mp_set_memory_functions (void *(*) (size_t),
151 + void *(*) (void *, size_t, size_t),
152 + void (*) (void *, size_t));
154 +void mp_get_memory_functions (void *(**) (size_t),
155 + void *(**) (void *, size_t, size_t),
156 + void (**) (void *, size_t));
158 +typedef unsigned long mp_limb_t;
159 +typedef long mp_size_t;
160 +typedef unsigned long mp_bitcnt_t;
162 +typedef mp_limb_t *mp_ptr;
163 +typedef const mp_limb_t *mp_srcptr;
167 + int _mp_alloc; /* Number of *limbs* allocated and pointed
168 + to by the _mp_d field. */
169 + int _mp_size; /* abs(_mp_size) is the number of limbs the
170 + last field points to. If _mp_size is
171 + negative this is a negative number. */
172 + mp_limb_t *_mp_d; /* Pointer to the limbs. */
175 +typedef __mpz_struct mpz_t[1];
177 +typedef __mpz_struct *mpz_ptr;
178 +typedef const __mpz_struct *mpz_srcptr;
180 +extern const int mp_bits_per_limb;
182 +void mpn_copyi (mp_ptr, mp_srcptr, mp_size_t);
183 +void mpn_copyd (mp_ptr, mp_srcptr, mp_size_t);
184 +void mpn_zero (mp_ptr, mp_size_t);
186 +int mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t);
188 +mp_limb_t mpn_add_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
189 +mp_limb_t mpn_add_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
190 +mp_limb_t mpn_add (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
192 +mp_limb_t mpn_sub_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
193 +mp_limb_t mpn_sub_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
194 +mp_limb_t mpn_sub (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
196 +mp_limb_t mpn_mul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
197 +mp_limb_t mpn_addmul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
198 +mp_limb_t mpn_submul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
200 +mp_limb_t mpn_mul (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
201 +void mpn_mul_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
202 +void mpn_sqr (mp_ptr, mp_srcptr, mp_size_t);
203 +int mpn_perfect_square_p (mp_srcptr, mp_size_t);
204 +mp_size_t mpn_sqrtrem (mp_ptr, mp_ptr, mp_srcptr, mp_size_t);
206 +mp_limb_t mpn_lshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
207 +mp_limb_t mpn_rshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
209 +mp_bitcnt_t mpn_scan0 (mp_srcptr, mp_bitcnt_t);
210 +mp_bitcnt_t mpn_scan1 (mp_srcptr, mp_bitcnt_t);
212 +mp_bitcnt_t mpn_popcount (mp_srcptr, mp_size_t);
214 +mp_limb_t mpn_invert_3by2 (mp_limb_t, mp_limb_t);
215 +#define mpn_invert_limb(x) mpn_invert_3by2 ((x), 0)
217 +size_t mpn_get_str (unsigned char *, int, mp_ptr, mp_size_t);
218 +mp_size_t mpn_set_str (mp_ptr, const unsigned char *, size_t, int);
220 +void mpz_init (mpz_t);
221 +void mpz_init2 (mpz_t, mp_bitcnt_t);
222 +void mpz_clear (mpz_t);
224 +#define mpz_odd_p(z) (((z)->_mp_size != 0) & (int) (z)->_mp_d[0])
225 +#define mpz_even_p(z) (! mpz_odd_p (z))
227 +int mpz_sgn (const mpz_t);
228 +int mpz_cmp_si (const mpz_t, long);
229 +int mpz_cmp_ui (const mpz_t, unsigned long);
230 +int mpz_cmp (const mpz_t, const mpz_t);
231 +int mpz_cmpabs_ui (const mpz_t, unsigned long);
232 +int mpz_cmpabs (const mpz_t, const mpz_t);
233 +int mpz_cmp_d (const mpz_t, double);
234 +int mpz_cmpabs_d (const mpz_t, double);
236 +void mpz_abs (mpz_t, const mpz_t);
237 +void mpz_neg (mpz_t, const mpz_t);
238 +void mpz_swap (mpz_t, mpz_t);
240 +void mpz_add_ui (mpz_t, const mpz_t, unsigned long);
241 +void mpz_add (mpz_t, const mpz_t, const mpz_t);
242 +void mpz_sub_ui (mpz_t, const mpz_t, unsigned long);
243 +void mpz_ui_sub (mpz_t, unsigned long, const mpz_t);
244 +void mpz_sub (mpz_t, const mpz_t, const mpz_t);
246 +void mpz_mul_si (mpz_t, const mpz_t, long int);
247 +void mpz_mul_ui (mpz_t, const mpz_t, unsigned long int);
248 +void mpz_mul (mpz_t, const mpz_t, const mpz_t);
249 +void mpz_mul_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
250 +void mpz_addmul_ui (mpz_t, const mpz_t, unsigned long int);
251 +void mpz_addmul (mpz_t, const mpz_t, const mpz_t);
252 +void mpz_submul_ui (mpz_t, const mpz_t, unsigned long int);
253 +void mpz_submul (mpz_t, const mpz_t, const mpz_t);
255 +void mpz_cdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
256 +void mpz_fdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
257 +void mpz_tdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
258 +void mpz_cdiv_q (mpz_t, const mpz_t, const mpz_t);
259 +void mpz_fdiv_q (mpz_t, const mpz_t, const mpz_t);
260 +void mpz_tdiv_q (mpz_t, const mpz_t, const mpz_t);
261 +void mpz_cdiv_r (mpz_t, const mpz_t, const mpz_t);
262 +void mpz_fdiv_r (mpz_t, const mpz_t, const mpz_t);
263 +void mpz_tdiv_r (mpz_t, const mpz_t, const mpz_t);
265 +void mpz_cdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
266 +void mpz_fdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
267 +void mpz_tdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
268 +void mpz_cdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
269 +void mpz_fdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
270 +void mpz_tdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
272 +void mpz_mod (mpz_t, const mpz_t, const mpz_t);
274 +void mpz_divexact (mpz_t, const mpz_t, const mpz_t);
276 +int mpz_divisible_p (const mpz_t, const mpz_t);
277 +int mpz_congruent_p (const mpz_t, const mpz_t, const mpz_t);
279 +unsigned long mpz_cdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
280 +unsigned long mpz_fdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
281 +unsigned long mpz_tdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
282 +unsigned long mpz_cdiv_q_ui (mpz_t, const mpz_t, unsigned long);
283 +unsigned long mpz_fdiv_q_ui (mpz_t, const mpz_t, unsigned long);
284 +unsigned long mpz_tdiv_q_ui (mpz_t, const mpz_t, unsigned long);
285 +unsigned long mpz_cdiv_r_ui (mpz_t, const mpz_t, unsigned long);
286 +unsigned long mpz_fdiv_r_ui (mpz_t, const mpz_t, unsigned long);
287 +unsigned long mpz_tdiv_r_ui (mpz_t, const mpz_t, unsigned long);
288 +unsigned long mpz_cdiv_ui (const mpz_t, unsigned long);
289 +unsigned long mpz_fdiv_ui (const mpz_t, unsigned long);
290 +unsigned long mpz_tdiv_ui (const mpz_t, unsigned long);
292 +unsigned long mpz_mod_ui (mpz_t, const mpz_t, unsigned long);
294 +void mpz_divexact_ui (mpz_t, const mpz_t, unsigned long);
296 +int mpz_divisible_ui_p (const mpz_t, unsigned long);
298 +unsigned long mpz_gcd_ui (mpz_t, const mpz_t, unsigned long);
299 +void mpz_gcd (mpz_t, const mpz_t, const mpz_t);
300 +void mpz_gcdext (mpz_t, mpz_t, mpz_t, const mpz_t, const mpz_t);
301 +void mpz_lcm_ui (mpz_t, const mpz_t, unsigned long);
302 +void mpz_lcm (mpz_t, const mpz_t, const mpz_t);
303 +int mpz_invert (mpz_t, const mpz_t, const mpz_t);
305 +void mpz_sqrtrem (mpz_t, mpz_t, const mpz_t);
306 +void mpz_sqrt (mpz_t, const mpz_t);
307 +int mpz_perfect_square_p (const mpz_t);
309 +void mpz_pow_ui (mpz_t, const mpz_t, unsigned long);
310 +void mpz_ui_pow_ui (mpz_t, unsigned long, unsigned long);
311 +void mpz_powm (mpz_t, const mpz_t, const mpz_t, const mpz_t);
312 +void mpz_powm_ui (mpz_t, const mpz_t, unsigned long, const mpz_t);
314 +void mpz_rootrem (mpz_t, mpz_t, const mpz_t, unsigned long);
315 +int mpz_root (mpz_t, const mpz_t, unsigned long);
317 +void mpz_fac_ui (mpz_t, unsigned long);
318 +void mpz_bin_uiui (mpz_t, unsigned long, unsigned long);
320 +int mpz_probab_prime_p (const mpz_t, int);
322 +int mpz_tstbit (const mpz_t, mp_bitcnt_t);
323 +void mpz_setbit (mpz_t, mp_bitcnt_t);
324 +void mpz_clrbit (mpz_t, mp_bitcnt_t);
325 +void mpz_combit (mpz_t, mp_bitcnt_t);
327 +void mpz_com (mpz_t, const mpz_t);
328 +void mpz_and (mpz_t, const mpz_t, const mpz_t);
329 +void mpz_ior (mpz_t, const mpz_t, const mpz_t);
330 +void mpz_xor (mpz_t, const mpz_t, const mpz_t);
332 +mp_bitcnt_t mpz_popcount (const mpz_t);
333 +mp_bitcnt_t mpz_hamdist (const mpz_t, const mpz_t);
334 +mp_bitcnt_t mpz_scan0 (const mpz_t, mp_bitcnt_t);
335 +mp_bitcnt_t mpz_scan1 (const mpz_t, mp_bitcnt_t);
337 +int mpz_fits_slong_p (const mpz_t);
338 +int mpz_fits_ulong_p (const mpz_t);
339 +long int mpz_get_si (const mpz_t);
340 +unsigned long int mpz_get_ui (const mpz_t);
341 +double mpz_get_d (const mpz_t);
342 +size_t mpz_size (const mpz_t);
343 +mp_limb_t mpz_getlimbn (const mpz_t, mp_size_t);
345 +void mpz_realloc2 (mpz_t, mp_bitcnt_t);
346 +mp_srcptr mpz_limbs_read (mpz_srcptr);
347 +mp_ptr mpz_limbs_modify (mpz_t, mp_size_t);
348 +mp_ptr mpz_limbs_write (mpz_t, mp_size_t);
349 +void mpz_limbs_finish (mpz_t, mp_size_t);
350 +mpz_srcptr mpz_roinit_n (mpz_t, mp_srcptr, mp_size_t);
352 +#define MPZ_ROINIT_N(xp, xs) {{0, (xs),(xp) }}
354 +void mpz_set_si (mpz_t, signed long int);
355 +void mpz_set_ui (mpz_t, unsigned long int);
356 +void mpz_set (mpz_t, const mpz_t);
357 +void mpz_set_d (mpz_t, double);
359 +void mpz_init_set_si (mpz_t, signed long int);
360 +void mpz_init_set_ui (mpz_t, unsigned long int);
361 +void mpz_init_set (mpz_t, const mpz_t);
362 +void mpz_init_set_d (mpz_t, double);
364 +size_t mpz_sizeinbase (const mpz_t, int);
365 +char *mpz_get_str (char *, int, const mpz_t);
366 +int mpz_set_str (mpz_t, const char *, int);
367 +int mpz_init_set_str (mpz_t, const char *, int);
369 +/* This long list taken from gmp.h. */
370 +/* For reference, "defined(EOF)" cannot be used here. In g++ 2.95.4,
371 + <iostream> defines EOF but not FILE. */
372 +#if defined (FILE) \
373 + || defined (H_STDIO) \
374 + || defined (_H_STDIO) /* AIX */ \
375 + || defined (_STDIO_H) /* glibc, Sun, SCO */ \
376 + || defined (_STDIO_H_) /* BSD, OSF */ \
377 + || defined (__STDIO_H) /* Borland */ \
378 + || defined (__STDIO_H__) /* IRIX */ \
379 + || defined (_STDIO_INCLUDED) /* HPUX */ \
380 + || defined (__dj_include_stdio_h_) /* DJGPP */ \
381 + || defined (_FILE_DEFINED) /* Microsoft */ \
382 + || defined (__STDIO__) /* Apple MPW MrC */ \
383 + || defined (_MSL_STDIO_H) /* Metrowerks */ \
384 + || defined (_STDIO_H_INCLUDED) /* QNX4 */ \
385 + || defined (_ISO_STDIO_ISO_H) /* Sun C++ */ \
386 + || defined (__STDIO_LOADED) /* VMS */
387 +size_t mpz_out_str (FILE *, int, const mpz_t);
390 +void mpz_import (mpz_t, size_t, int, size_t, int, size_t, const void *);
391 +void *mpz_export (void *, size_t *, int, size_t, int, size_t, const mpz_t);
393 +#if defined (__cplusplus)
396 +#endif /* __MINI_GMP_H__ */
397 --- a/include/utils.h
398 +++ b/include/utils.h
406 #define BITS_PER_BYTE 8
409 #define pr_debug(fmt, arg...) gmp_printf(fmt, ##arg)
411 -#define pr_debug(fmt, arg...) ({ if (false) gmp_printf(fmt, ##arg); 0; })
412 +#define pr_debug(fmt, arg...)
415 #define __fmtstring(x, y) __attribute__((format(printf, x, y)))
418 @@ -275,11 +275,9 @@ static struct error_record *integer_type
425 - if (gmp_sscanf(sym->identifier, "%Zu%n", v, &len) != 1 ||
426 - (int)strlen(sym->identifier) != len) {
427 + if (mpz_set_str(v, sym->identifier, 0)) {
429 return error(&sym->location, "Could not parse %s",
433 @@ -44,6 +44,7 @@ static void erec_destroy(struct error_re
437 +__attribute__((format(printf, 3, 0)))
438 struct error_record *erec_vcreate(enum error_record_types type,
439 const struct location *loc,
440 const char *fmt, va_list ap)
441 @@ -55,10 +56,11 @@ struct error_record *erec_vcreate(enum e
442 erec->num_locations = 0;
443 erec_add_location(erec, loc);
445 - gmp_vasprintf(&erec->msg, fmt, ap);
446 + if (vasprintf(&erec->msg, fmt, ap)) {}
450 +__attribute__((format(printf, 3, 4)))
451 struct error_record *erec_create(enum error_record_types type,
452 const struct location *loc,
453 const char *fmt, ...)
456 @@ -232,9 +232,13 @@ static int expr_evaluate_value(struct ev
458 mpz_init_bitmask(mask, ctx->ectx.len);
459 if (mpz_cmp((*expr)->value, mask) > 0) {
460 + char *valstr = mpz_get_str(NULL, 10, (*expr)->value);
461 + char *rangestr = mpz_get_str(NULL, 10, mask);
462 expr_error(ctx->msgs, *expr,
463 - "Value %Zu exceeds valid range 0-%Zu",
464 - (*expr)->value, mask);
465 + "Value %s exceeds valid range 0-%s",
480 #include <nftables.h>
481 #include <datatype.h>
483 +++ b/src/mini-gmp-printf.c
486 +#include <stdbool.h>
491 +// nftables mostly uses gmp_printf as below so we build a minimalistic
492 +// version to avoid the awkwardness of wrapping printf.
493 +// This requires rewriting other occurences of gmp_printf or
494 +// variants which are rare (only 1 so far).
495 +// Also we exclude pr_debug here since this is a rathole
496 +// and if debugging is desired then libgmp can be used.
498 +int gmp_printf(const char *f, const mpz_t value)
503 + if (fputc(*f, stdout) != *f)
508 + unsigned long prec = 0;
515 + prec = strtoul(++f, (char**)&f, 10);
522 + else if (*f == 'x')
527 + len = mpz_sizeinbase(value, base);
528 + while (prec-- > len) {
529 + if (fputc('0', stdout) != '0')
535 + str = mpz_get_str(NULL, base, value);
536 + ok = str && fwrite(str, 1, len, stdout) == len;
551 +/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
553 + Contributed to the GNU project by Niels Möller
555 +Copyright 1991-1997, 1999-2014 Free Software Foundation, Inc.
557 +This file is part of the GNU MP Library.
559 +The GNU MP Library is free software; you can redistribute it and/or modify
560 +it under the terms of either:
562 + * the GNU Lesser General Public License as published by the Free
563 + Software Foundation; either version 3 of the License, or (at your
564 + option) any later version.
568 + * the GNU General Public License as published by the Free Software
569 + Foundation; either version 2 of the License, or (at your option) any
572 +or both in parallel, as here.
574 +The GNU MP Library is distributed in the hope that it will be useful, but
575 +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
576 +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
579 +You should have received copies of the GNU General Public License and the
580 +GNU Lesser General Public License along with the GNU MP Library. If not,
581 +see https://www.gnu.org/licenses/. */
583 +/* NOTE: All functions in this file which are not declared in
584 + mini-gmp.h are internal, and are not intended to be compatible
585 + neither with GMP nor with future versions of mini-gmp. */
587 +/* Much of the material copied from GMP files, including: gmp-impl.h,
588 + longlong.h, mpn/generic/add_n.c, mpn/generic/addmul_1.c,
589 + mpn/generic/lshift.c, mpn/generic/mul_1.c,
590 + mpn/generic/mul_basecase.c, mpn/generic/rshift.c,
591 + mpn/generic/sbpi1_div_qr.c, mpn/generic/sub_n.c,
592 + mpn/generic/submul_1.c. */
601 +#include "mini-gmp.h"
605 +#define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
607 +#define GMP_LIMB_MAX (~ (mp_limb_t) 0)
608 +#define GMP_LIMB_HIGHBIT ((mp_limb_t) 1 << (GMP_LIMB_BITS - 1))
610 +#define GMP_HLIMB_BIT ((mp_limb_t) 1 << (GMP_LIMB_BITS / 2))
611 +#define GMP_LLIMB_MASK (GMP_HLIMB_BIT - 1)
613 +#define GMP_ULONG_BITS (sizeof(unsigned long) * CHAR_BIT)
614 +#define GMP_ULONG_HIGHBIT ((unsigned long) 1 << (GMP_ULONG_BITS - 1))
616 +#define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
617 +#define GMP_NEG_CAST(T,x) (-((T)((x) + 1) - 1))
619 +#define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
620 +#define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
622 +#define gmp_assert_nocarry(x) do { \
623 + mp_limb_t __cy = x; \
624 + assert (__cy == 0); \
627 +#define gmp_clz(count, x) do { \
628 + mp_limb_t __clz_x = (x); \
629 + unsigned __clz_c; \
630 + for (__clz_c = 0; \
631 + (__clz_x & ((mp_limb_t) 0xff << (GMP_LIMB_BITS - 8))) == 0; \
634 + for (; (__clz_x & GMP_LIMB_HIGHBIT) == 0; __clz_c++) \
636 + (count) = __clz_c; \
639 +#define gmp_ctz(count, x) do { \
640 + mp_limb_t __ctz_x = (x); \
641 + unsigned __ctz_c = 0; \
642 + gmp_clz (__ctz_c, __ctz_x & - __ctz_x); \
643 + (count) = GMP_LIMB_BITS - 1 - __ctz_c; \
646 +#define gmp_add_ssaaaa(sh, sl, ah, al, bh, bl) \
649 + __x = (al) + (bl); \
650 + (sh) = (ah) + (bh) + (__x < (al)); \
654 +#define gmp_sub_ddmmss(sh, sl, ah, al, bh, bl) \
657 + __x = (al) - (bl); \
658 + (sh) = (ah) - (bh) - ((al) < (bl)); \
662 +#define gmp_umul_ppmm(w1, w0, u, v) \
664 + mp_limb_t __x0, __x1, __x2, __x3; \
665 + unsigned __ul, __vl, __uh, __vh; \
666 + mp_limb_t __u = (u), __v = (v); \
668 + __ul = __u & GMP_LLIMB_MASK; \
669 + __uh = __u >> (GMP_LIMB_BITS / 2); \
670 + __vl = __v & GMP_LLIMB_MASK; \
671 + __vh = __v >> (GMP_LIMB_BITS / 2); \
673 + __x0 = (mp_limb_t) __ul * __vl; \
674 + __x1 = (mp_limb_t) __ul * __vh; \
675 + __x2 = (mp_limb_t) __uh * __vl; \
676 + __x3 = (mp_limb_t) __uh * __vh; \
678 + __x1 += __x0 >> (GMP_LIMB_BITS / 2);/* this can't give carry */ \
679 + __x1 += __x2; /* but this indeed can */ \
680 + if (__x1 < __x2) /* did we get it? */ \
681 + __x3 += GMP_HLIMB_BIT; /* yes, add it in the proper pos. */ \
683 + (w1) = __x3 + (__x1 >> (GMP_LIMB_BITS / 2)); \
684 + (w0) = (__x1 << (GMP_LIMB_BITS / 2)) + (__x0 & GMP_LLIMB_MASK); \
687 +#define gmp_udiv_qrnnd_preinv(q, r, nh, nl, d, di) \
689 + mp_limb_t _qh, _ql, _r, _mask; \
690 + gmp_umul_ppmm (_qh, _ql, (nh), (di)); \
691 + gmp_add_ssaaaa (_qh, _ql, _qh, _ql, (nh) + 1, (nl)); \
692 + _r = (nl) - _qh * (d); \
693 + _mask = -(mp_limb_t) (_r > _ql); /* both > and >= are OK */ \
695 + _r += _mask & (d); \
706 +#define gmp_udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv) \
708 + mp_limb_t _q0, _t1, _t0, _mask; \
709 + gmp_umul_ppmm ((q), _q0, (n2), (dinv)); \
710 + gmp_add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1)); \
712 + /* Compute the two most significant limbs of n - q'd */ \
713 + (r1) = (n1) - (d1) * (q); \
714 + gmp_sub_ddmmss ((r1), (r0), (r1), (n0), (d1), (d0)); \
715 + gmp_umul_ppmm (_t1, _t0, (d0), (q)); \
716 + gmp_sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0); \
719 + /* Conditionally adjust q and the remainders */ \
720 + _mask = - (mp_limb_t) ((r1) >= _q0); \
722 + gmp_add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0)); \
723 + if ((r1) >= (d1)) \
725 + if ((r1) > (d1) || (r0) >= (d0)) \
728 + gmp_sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0)); \
734 +#define MP_LIMB_T_SWAP(x, y) \
736 + mp_limb_t __mp_limb_t_swap__tmp = (x); \
738 + (y) = __mp_limb_t_swap__tmp; \
740 +#define MP_SIZE_T_SWAP(x, y) \
742 + mp_size_t __mp_size_t_swap__tmp = (x); \
744 + (y) = __mp_size_t_swap__tmp; \
746 +#define MP_BITCNT_T_SWAP(x,y) \
748 + mp_bitcnt_t __mp_bitcnt_t_swap__tmp = (x); \
750 + (y) = __mp_bitcnt_t_swap__tmp; \
752 +#define MP_PTR_SWAP(x, y) \
754 + mp_ptr __mp_ptr_swap__tmp = (x); \
756 + (y) = __mp_ptr_swap__tmp; \
758 +#define MP_SRCPTR_SWAP(x, y) \
760 + mp_srcptr __mp_srcptr_swap__tmp = (x); \
762 + (y) = __mp_srcptr_swap__tmp; \
765 +#define MPN_PTR_SWAP(xp,xs, yp,ys) \
767 + MP_PTR_SWAP (xp, yp); \
768 + MP_SIZE_T_SWAP (xs, ys); \
770 +#define MPN_SRCPTR_SWAP(xp,xs, yp,ys) \
772 + MP_SRCPTR_SWAP (xp, yp); \
773 + MP_SIZE_T_SWAP (xs, ys); \
776 +#define MPZ_PTR_SWAP(x, y) \
778 + mpz_ptr __mpz_ptr_swap__tmp = (x); \
780 + (y) = __mpz_ptr_swap__tmp; \
782 +#define MPZ_SRCPTR_SWAP(x, y) \
784 + mpz_srcptr __mpz_srcptr_swap__tmp = (x); \
786 + (y) = __mpz_srcptr_swap__tmp; \
789 +const int mp_bits_per_limb = GMP_LIMB_BITS;
792 +/* Memory allocation and other helper functions. */
794 +gmp_die (const char *msg)
796 + fprintf (stderr, "%s\n", msg);
801 +gmp_default_alloc (size_t size)
809 + gmp_die("gmp_default_alloc: Virtual memory exhausted.");
815 +gmp_default_realloc (void *old, size_t old_size, size_t new_size)
819 + p = realloc (old, new_size);
822 + gmp_die("gmp_default_realoc: Virtual memory exhausted.");
828 +gmp_default_free (void *p, size_t size)
833 +static void * (*gmp_allocate_func) (size_t) = gmp_default_alloc;
834 +static void * (*gmp_reallocate_func) (void *, size_t, size_t) = gmp_default_realloc;
835 +static void (*gmp_free_func) (void *, size_t) = gmp_default_free;
838 +mp_get_memory_functions (void *(**alloc_func) (size_t),
839 + void *(**realloc_func) (void *, size_t, size_t),
840 + void (**free_func) (void *, size_t))
843 + *alloc_func = gmp_allocate_func;
846 + *realloc_func = gmp_reallocate_func;
849 + *free_func = gmp_free_func;
853 +mp_set_memory_functions (void *(*alloc_func) (size_t),
854 + void *(*realloc_func) (void *, size_t, size_t),
855 + void (*free_func) (void *, size_t))
858 + alloc_func = gmp_default_alloc;
860 + realloc_func = gmp_default_realloc;
862 + free_func = gmp_default_free;
864 + gmp_allocate_func = alloc_func;
865 + gmp_reallocate_func = realloc_func;
866 + gmp_free_func = free_func;
869 +#define gmp_xalloc(size) ((*gmp_allocate_func)((size)))
870 +#define gmp_free(p) ((*gmp_free_func) ((p), 0))
873 +gmp_xalloc_limbs (mp_size_t size)
875 + return gmp_xalloc (size * sizeof (mp_limb_t));
879 +gmp_xrealloc_limbs (mp_ptr old, mp_size_t size)
882 + return (*gmp_reallocate_func) (old, 0, size * sizeof (mp_limb_t));
889 +mpn_copyi (mp_ptr d, mp_srcptr s, mp_size_t n)
892 + for (i = 0; i < n; i++)
897 +mpn_copyd (mp_ptr d, mp_srcptr s, mp_size_t n)
904 +mpn_cmp (mp_srcptr ap, mp_srcptr bp, mp_size_t n)
908 + if (ap[n] != bp[n])
909 + return ap[n] > bp[n] ? 1 : -1;
915 +mpn_cmp4 (mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
918 + return an < bn ? -1 : 1;
920 + return mpn_cmp (ap, bp, an);
924 +mpn_normalized_size (mp_srcptr xp, mp_size_t n)
926 + for (; n > 0 && xp[n-1] == 0; n--)
931 +#define mpn_zero_p(xp, n) (mpn_normalized_size ((xp), (n)) == 0)
934 +mpn_zero (mp_ptr rp, mp_size_t n)
938 + for (i = 0; i < n; i++)
943 +mpn_add_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
951 + mp_limb_t r = ap[i] + b;
962 +mpn_add_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
967 + for (i = 0, cy = 0; i < n; i++)
970 + a = ap[i]; b = bp[i];
981 +mpn_add (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
987 + cy = mpn_add_n (rp, ap, bp, bn);
989 + cy = mpn_add_1 (rp + bn, ap + bn, an - bn, cy);
994 +mpn_sub_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
1003 + mp_limb_t a = ap[i];
1005 + mp_limb_t cy = a < b;;
1015 +mpn_sub_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
1020 + for (i = 0, cy = 0; i < n; i++)
1023 + a = ap[i]; b = bp[i];
1033 +mpn_sub (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
1037 + assert (an >= bn);
1039 + cy = mpn_sub_n (rp, ap, bp, bn);
1041 + cy = mpn_sub_1 (rp + bn, ap + bn, an - bn, cy);
1046 +mpn_mul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
1048 + mp_limb_t ul, cl, hpl, lpl;
1056 + gmp_umul_ppmm (hpl, lpl, ul, vl);
1059 + cl = (lpl < cl) + hpl;
1069 +mpn_addmul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
1071 + mp_limb_t ul, cl, hpl, lpl, rl;
1079 + gmp_umul_ppmm (hpl, lpl, ul, vl);
1082 + cl = (lpl < cl) + hpl;
1095 +mpn_submul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
1097 + mp_limb_t ul, cl, hpl, lpl, rl;
1105 + gmp_umul_ppmm (hpl, lpl, ul, vl);
1108 + cl = (lpl < cl) + hpl;
1121 +mpn_mul (mp_ptr rp, mp_srcptr up, mp_size_t un, mp_srcptr vp, mp_size_t vn)
1123 + assert (un >= vn);
1126 + /* We first multiply by the low order limb. This result can be
1127 + stored, not added, to rp. We also avoid a loop for zeroing this
1130 + rp[un] = mpn_mul_1 (rp, up, un, vp[0]);
1131 + rp += 1, vp += 1, vn -= 1;
1133 + /* Now accumulate the product of up[] and the next higher limb from
1138 + rp[un] = mpn_addmul_1 (rp, up, un, vp[0]);
1139 + rp += 1, vp += 1, vn -= 1;
1141 + return rp[un - 1];
1145 +mpn_mul_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
1147 + mpn_mul (rp, ap, n, bp, n);
1151 +mpn_sqr (mp_ptr rp, mp_srcptr ap, mp_size_t n)
1153 + mpn_mul (rp, ap, n, ap, n);
1157 +mpn_lshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
1159 + mp_limb_t high_limb, low_limb;
1165 + assert (cnt >= 1);
1166 + assert (cnt < GMP_LIMB_BITS);
1171 + tnc = GMP_LIMB_BITS - cnt;
1173 + retval = low_limb >> tnc;
1174 + high_limb = (low_limb << cnt);
1176 + for (i = n; --i != 0;)
1179 + *--rp = high_limb | (low_limb >> tnc);
1180 + high_limb = (low_limb << cnt);
1182 + *--rp = high_limb;
1188 +mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
1190 + mp_limb_t high_limb, low_limb;
1196 + assert (cnt >= 1);
1197 + assert (cnt < GMP_LIMB_BITS);
1199 + tnc = GMP_LIMB_BITS - cnt;
1200 + high_limb = *up++;
1201 + retval = (high_limb << tnc);
1202 + low_limb = high_limb >> cnt;
1204 + for (i = n; --i != 0;)
1206 + high_limb = *up++;
1207 + *rp++ = low_limb | (high_limb << tnc);
1208 + low_limb = high_limb >> cnt;
1216 +mpn_common_scan (mp_limb_t limb, mp_size_t i, mp_srcptr up, mp_size_t un,
1221 + assert (ux == 0 || ux == GMP_LIMB_MAX);
1222 + assert (0 <= i && i <= un );
1228 + return (ux == 0 ? ~(mp_bitcnt_t) 0 : un * GMP_LIMB_BITS);
1229 + limb = ux ^ up[i];
1231 + gmp_ctz (cnt, limb);
1232 + return (mp_bitcnt_t) i * GMP_LIMB_BITS + cnt;
1236 +mpn_scan1 (mp_srcptr ptr, mp_bitcnt_t bit)
1239 + i = bit / GMP_LIMB_BITS;
1241 + return mpn_common_scan ( ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
1246 +mpn_scan0 (mp_srcptr ptr, mp_bitcnt_t bit)
1249 + i = bit / GMP_LIMB_BITS;
1251 + return mpn_common_scan (~ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
1252 + i, ptr, i, GMP_LIMB_MAX);
1256 +/* MPN division interface. */
1258 +mpn_invert_3by2 (mp_limb_t u1, mp_limb_t u0)
1260 + mp_limb_t r, p, m;
1264 + /* First, do a 2/1 inverse. */
1265 + /* The inverse m is defined as floor( (B^2 - 1 - u1)/u1 ), so that 0 <
1266 + * B^2 - (B + m) u1 <= u1 */
1267 + assert (u1 >= GMP_LIMB_HIGHBIT);
1269 + ul = u1 & GMP_LLIMB_MASK;
1270 + uh = u1 >> (GMP_LIMB_BITS / 2);
1273 + r = ((~u1 - (mp_limb_t) qh * uh) << (GMP_LIMB_BITS / 2)) | GMP_LLIMB_MASK;
1275 + p = (mp_limb_t) qh * ul;
1276 + /* Adjustment steps taken from udiv_qrnnd_c */
1281 + if (r >= u1) /* i.e. we didn't get carry when adding to r */
1290 + /* Do a 3/2 division (with half limb size) */
1291 + p = (r >> (GMP_LIMB_BITS / 2)) * qh + r;
1292 + ql = (p >> (GMP_LIMB_BITS / 2)) + 1;
1294 + /* By the 3/2 method, we don't need the high half limb. */
1295 + r = (r << (GMP_LIMB_BITS / 2)) + GMP_LLIMB_MASK - ql * u1;
1297 + if (r >= (p << (GMP_LIMB_BITS / 2)))
1302 + m = ((mp_limb_t) qh << (GMP_LIMB_BITS / 2)) + ql;
1324 + gmp_umul_ppmm (th, tl, u0, m);
1329 + m -= ((r > u1) | ((r == u1) & (tl > u0)));
1336 +struct gmp_div_inverse
1338 + /* Normalization shift count. */
1340 + /* Normalized divisor (d0 unused for mpn_div_qr_1) */
1342 + /* Inverse, for 2/1 or 3/2. */
1347 +mpn_div_qr_1_invert (struct gmp_div_inverse *inv, mp_limb_t d)
1352 + gmp_clz (shift, d);
1353 + inv->shift = shift;
1354 + inv->d1 = d << shift;
1355 + inv->di = mpn_invert_limb (inv->d1);
1359 +mpn_div_qr_2_invert (struct gmp_div_inverse *inv,
1360 + mp_limb_t d1, mp_limb_t d0)
1365 + gmp_clz (shift, d1);
1366 + inv->shift = shift;
1369 + d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
1374 + inv->di = mpn_invert_3by2 (d1, d0);
1378 +mpn_div_qr_invert (struct gmp_div_inverse *inv,
1379 + mp_srcptr dp, mp_size_t dn)
1384 + mpn_div_qr_1_invert (inv, dp[0]);
1386 + mpn_div_qr_2_invert (inv, dp[1], dp[0]);
1395 + gmp_clz (shift, d1);
1396 + inv->shift = shift;
1399 + d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
1400 + d0 = (d0 << shift) | (dp[dn-3] >> (GMP_LIMB_BITS - shift));
1404 + inv->di = mpn_invert_3by2 (d1, d0);
1408 +/* Not matching current public gmp interface, rather corresponding to
1409 + the sbpi1_div_* functions. */
1411 +mpn_div_qr_1_preinv (mp_ptr qp, mp_srcptr np, mp_size_t nn,
1412 + const struct gmp_div_inverse *inv)
1418 + if (inv->shift > 0)
1420 + tp = gmp_xalloc_limbs (nn);
1421 + r = mpn_lshift (tp, np, nn, inv->shift);
1433 + gmp_udiv_qrnnd_preinv (q, r, r, np[nn], d, di);
1437 + if (inv->shift > 0)
1440 + return r >> inv->shift;
1444 +mpn_div_qr_1 (mp_ptr qp, mp_srcptr np, mp_size_t nn, mp_limb_t d)
1448 + /* Special case for powers of two. */
1449 + if ((d & (d-1)) == 0)
1451 + mp_limb_t r = np[0] & (d-1);
1455 + mpn_copyi (qp, np, nn);
1459 + gmp_ctz (shift, d);
1460 + mpn_rshift (qp, np, nn, shift);
1467 + struct gmp_div_inverse inv;
1468 + mpn_div_qr_1_invert (&inv, d);
1469 + return mpn_div_qr_1_preinv (qp, np, nn, &inv);
1474 +mpn_div_qr_2_preinv (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
1475 + const struct gmp_div_inverse *inv)
1479 + mp_limb_t d1, d0, di, r1, r0;
1483 + shift = inv->shift;
1490 + tp = gmp_xalloc_limbs (nn);
1491 + r1 = mpn_lshift (tp, np, nn, shift);
1504 + gmp_udiv_qr_3by2 (q, r1, r0, r1, r0, n0, d1, d0, di);
1513 + assert ((r0 << (GMP_LIMB_BITS - shift)) == 0);
1514 + r0 = (r0 >> shift) | (r1 << (GMP_LIMB_BITS - shift));
1526 +mpn_div_qr_2 (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
1527 + mp_limb_t d1, mp_limb_t d0)
1529 + struct gmp_div_inverse inv;
1532 + mpn_div_qr_2_invert (&inv, d1, d0);
1533 + mpn_div_qr_2_preinv (qp, rp, np, nn, &inv);
1538 +mpn_div_qr_pi1 (mp_ptr qp,
1539 + mp_ptr np, mp_size_t nn, mp_limb_t n1,
1540 + mp_srcptr dp, mp_size_t dn,
1546 + mp_limb_t cy, cy1;
1550 + assert (nn >= dn);
1555 + assert ((d1 & GMP_LIMB_HIGHBIT) != 0);
1556 + /* Iteration variable is the index of the q limb.
1558 + * We divide <n1, np[dn-1+i], np[dn-2+i], np[dn-3+i],..., np[i]>
1559 + * by <d1, d0, dp[dn-3], ..., dp[0] >
1565 + mp_limb_t n0 = np[dn-1+i];
1567 + if (n1 == d1 && n0 == d0)
1570 + mpn_submul_1 (np+i, dp, dn, q);
1571 + n1 = np[dn-1+i]; /* update n1, last loop's value will now be invalid */
1575 + gmp_udiv_qr_3by2 (q, n1, n0, n1, n0, np[dn-2+i], d1, d0, dinv);
1577 + cy = mpn_submul_1 (np + i, dp, dn-2, q);
1587 + n1 += d1 + mpn_add_n (np + i, np + i, dp, dn - 1);
1601 +mpn_div_qr_preinv (mp_ptr qp, mp_ptr np, mp_size_t nn,
1602 + mp_srcptr dp, mp_size_t dn,
1603 + const struct gmp_div_inverse *inv)
1606 + assert (nn >= dn);
1609 + np[0] = mpn_div_qr_1_preinv (qp, np, nn, inv);
1611 + mpn_div_qr_2_preinv (qp, np, np, nn, inv);
1617 + assert (inv->d1 == dp[dn-1]);
1618 + assert (inv->d0 == dp[dn-2]);
1619 + assert ((inv->d1 & GMP_LIMB_HIGHBIT) != 0);
1621 + shift = inv->shift;
1623 + nh = mpn_lshift (np, np, nn, shift);
1627 + mpn_div_qr_pi1 (qp, np, nn, nh, dp, dn, inv->di);
1630 + gmp_assert_nocarry (mpn_rshift (np, np, dn, shift));
1635 +mpn_div_qr (mp_ptr qp, mp_ptr np, mp_size_t nn, mp_srcptr dp, mp_size_t dn)
1637 + struct gmp_div_inverse inv;
1641 + assert (nn >= dn);
1643 + mpn_div_qr_invert (&inv, dp, dn);
1644 + if (dn > 2 && inv.shift > 0)
1646 + tp = gmp_xalloc_limbs (dn);
1647 + gmp_assert_nocarry (mpn_lshift (tp, dp, dn, inv.shift));
1650 + mpn_div_qr_preinv (qp, np, nn, dp, dn, &inv);
1656 +/* MPN base conversion. */
1658 +mpn_base_power_of_two_p (unsigned b)
1665 + case 16: return 4;
1666 + case 32: return 5;
1667 + case 64: return 6;
1668 + case 128: return 7;
1669 + case 256: return 8;
1670 + default: return 0;
1674 +struct mpn_base_info
1676 + /* bb is the largest power of the base which fits in one limb, and
1677 + exp is the corresponding exponent. */
1683 +mpn_get_base_info (struct mpn_base_info *info, mp_limb_t b)
1689 + m = GMP_LIMB_MAX / b;
1690 + for (exp = 1, p = b; p <= m; exp++)
1698 +mpn_limb_size_in_base_2 (mp_limb_t u)
1703 + gmp_clz (shift, u);
1704 + return GMP_LIMB_BITS - shift;
1708 +mpn_get_str_bits (unsigned char *sp, unsigned bits, mp_srcptr up, mp_size_t un)
1710 + unsigned char mask;
1715 + sn = ((un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1])
1716 + + bits - 1) / bits;
1718 + mask = (1U << bits) - 1;
1720 + for (i = 0, j = sn, shift = 0; j-- > 0;)
1722 + unsigned char digit = up[i] >> shift;
1726 + if (shift >= GMP_LIMB_BITS && ++i < un)
1728 + shift -= GMP_LIMB_BITS;
1729 + digit |= up[i] << (bits - shift);
1731 + sp[j] = digit & mask;
1736 +/* We generate digits from the least significant end, and reverse at
1739 +mpn_limb_get_str (unsigned char *sp, mp_limb_t w,
1740 + const struct gmp_div_inverse *binv)
1743 + for (i = 0; w > 0; i++)
1745 + mp_limb_t h, l, r;
1747 + h = w >> (GMP_LIMB_BITS - binv->shift);
1748 + l = w << binv->shift;
1750 + gmp_udiv_qrnnd_preinv (w, r, h, l, binv->d1, binv->di);
1751 + assert ( (r << (GMP_LIMB_BITS - binv->shift)) == 0);
1752 + r >>= binv->shift;
1760 +mpn_get_str_other (unsigned char *sp,
1761 + int base, const struct mpn_base_info *info,
1762 + mp_ptr up, mp_size_t un)
1764 + struct gmp_div_inverse binv;
1768 + mpn_div_qr_1_invert (&binv, base);
1774 + struct gmp_div_inverse bbinv;
1775 + mpn_div_qr_1_invert (&bbinv, info->bb);
1781 + w = mpn_div_qr_1_preinv (up, up, un, &bbinv);
1782 + un -= (up[un-1] == 0);
1783 + done = mpn_limb_get_str (sp + sn, w, &binv);
1785 + for (sn += done; done < info->exp; done++)
1790 + sn += mpn_limb_get_str (sp + sn, up[0], &binv);
1792 + /* Reverse order */
1793 + for (i = 0; 2*i + 1 < sn; i++)
1795 + unsigned char t = sp[i];
1796 + sp[i] = sp[sn - i - 1];
1797 + sp[sn - i - 1] = t;
1804 +mpn_get_str (unsigned char *sp, int base, mp_ptr up, mp_size_t un)
1809 + assert (up[un-1] > 0);
1811 + bits = mpn_base_power_of_two_p (base);
1813 + return mpn_get_str_bits (sp, bits, up, un);
1816 + struct mpn_base_info info;
1818 + mpn_get_base_info (&info, base);
1819 + return mpn_get_str_other (sp, base, &info, up, un);
1824 +mpn_set_str_bits (mp_ptr rp, const unsigned char *sp, size_t sn,
1831 + for (j = sn, rn = 0, shift = 0; j-- > 0; )
1840 + rp[rn-1] |= (mp_limb_t) sp[j] << shift;
1842 + if (shift >= GMP_LIMB_BITS)
1844 + shift -= GMP_LIMB_BITS;
1846 + rp[rn++] = (mp_limb_t) sp[j] >> (bits - shift);
1850 + rn = mpn_normalized_size (rp, rn);
1855 +mpn_set_str_other (mp_ptr rp, const unsigned char *sp, size_t sn,
1856 + mp_limb_t b, const struct mpn_base_info *info)
1863 + k = 1 + (sn - 1) % info->exp;
1868 + w = w * b + sp[j++];
1872 + for (rn = (w > 0); j < sn;)
1877 + for (k = 1; k < info->exp; k++)
1878 + w = w * b + sp[j++];
1880 + cy = mpn_mul_1 (rp, rp, rn, info->bb);
1881 + cy += mpn_add_1 (rp, rp, rn, w);
1891 +mpn_set_str (mp_ptr rp, const unsigned char *sp, size_t sn, int base)
1898 + bits = mpn_base_power_of_two_p (base);
1900 + return mpn_set_str_bits (rp, sp, sn, bits);
1903 + struct mpn_base_info info;
1905 + mpn_get_base_info (&info, base);
1906 + return mpn_set_str_other (rp, sp, sn, base, &info);
1911 +/* MPZ interface */
1917 + r->_mp_d = gmp_xalloc_limbs (1);
1920 +/* The utility of this function is a bit limited, since many functions
1921 + assigns the result variable using mpz_swap. */
1923 +mpz_init2 (mpz_t r, mp_bitcnt_t bits)
1927 + bits -= (bits != 0); /* Round down, except if 0 */
1928 + rn = 1 + bits / GMP_LIMB_BITS;
1930 + r->_mp_alloc = rn;
1932 + r->_mp_d = gmp_xalloc_limbs (rn);
1936 +mpz_clear (mpz_t r)
1938 + gmp_free (r->_mp_d);
1942 +mpz_realloc (mpz_t r, mp_size_t size)
1944 + size = GMP_MAX (size, 1);
1946 + r->_mp_d = gmp_xrealloc_limbs (r->_mp_d, size);
1947 + r->_mp_alloc = size;
1949 + if (GMP_ABS (r->_mp_size) > size)
1955 +/* Realloc for an mpz_t WHAT if it has less than NEEDED limbs. */
1956 +#define MPZ_REALLOC(z,n) ((n) > (z)->_mp_alloc \
1957 + ? mpz_realloc(z,n) \
1960 +/* MPZ assignment and basic conversions. */
1962 +mpz_set_si (mpz_t r, signed long int x)
1965 + mpz_set_ui (r, x);
1966 + else /* (x < 0) */
1969 + r->_mp_d[0] = GMP_NEG_CAST (unsigned long int, x);
1974 +mpz_set_ui (mpz_t r, unsigned long int x)
1986 +mpz_set (mpz_t r, const mpz_t x)
1988 + /* Allow the NOP r == x */
1994 + n = GMP_ABS (x->_mp_size);
1995 + rp = MPZ_REALLOC (r, n);
1997 + mpn_copyi (rp, x->_mp_d, n);
1998 + r->_mp_size = x->_mp_size;
2003 +mpz_init_set_si (mpz_t r, signed long int x)
2006 + mpz_set_si (r, x);
2010 +mpz_init_set_ui (mpz_t r, unsigned long int x)
2013 + mpz_set_ui (r, x);
2017 +mpz_init_set (mpz_t r, const mpz_t x)
2024 +mpz_fits_slong_p (const mpz_t u)
2026 + mp_size_t us = u->_mp_size;
2031 + return u->_mp_d[0] < GMP_LIMB_HIGHBIT;
2032 + else if (us == -1)
2033 + return u->_mp_d[0] <= GMP_LIMB_HIGHBIT;
2039 +mpz_fits_ulong_p (const mpz_t u)
2041 + mp_size_t us = u->_mp_size;
2043 + return (us == (us > 0));
2047 +mpz_get_si (const mpz_t u)
2049 + mp_size_t us = u->_mp_size;
2052 + return (long) (u->_mp_d[0] & ~GMP_LIMB_HIGHBIT);
2054 + return (long) (- u->_mp_d[0] | GMP_LIMB_HIGHBIT);
2060 +mpz_get_ui (const mpz_t u)
2062 + return u->_mp_size == 0 ? 0 : u->_mp_d[0];
2066 +mpz_size (const mpz_t u)
2068 + return GMP_ABS (u->_mp_size);
2072 +mpz_getlimbn (const mpz_t u, mp_size_t n)
2074 + if (n >= 0 && n < GMP_ABS (u->_mp_size))
2075 + return u->_mp_d[n];
2081 +mpz_realloc2 (mpz_t x, mp_bitcnt_t n)
2083 + mpz_realloc (x, 1 + (n - (n != 0)) / GMP_LIMB_BITS);
2087 +mpz_limbs_read (mpz_srcptr x)
2093 +mpz_limbs_modify (mpz_t x, mp_size_t n)
2096 + return MPZ_REALLOC (x, n);
2100 +mpz_limbs_write (mpz_t x, mp_size_t n)
2102 + return mpz_limbs_modify (x, n);
2106 +mpz_limbs_finish (mpz_t x, mp_size_t xs)
2109 + xn = mpn_normalized_size (x->_mp_d, GMP_ABS (xs));
2110 + x->_mp_size = xs < 0 ? -xn : xn;
2114 +mpz_roinit_n (mpz_t x, mp_srcptr xp, mp_size_t xs)
2117 + x->_mp_d = (mp_ptr) xp;
2118 + mpz_limbs_finish (x, xs);
2123 +/* Conversions and comparison to double. */
2125 +mpz_set_d (mpz_t r, double x)
2134 + /* x != x is true when x is a NaN, and x == x * 0.5 is true when x is
2135 + zero or infinity. */
2136 + if (x != x || x == x * 0.5)
2151 + B = 2.0 * (double) GMP_LIMB_HIGHBIT;
2153 + for (rn = 1; x >= B; rn++)
2156 + rp = MPZ_REALLOC (r, rn);
2158 + f = (mp_limb_t) x;
2166 + f = (mp_limb_t) x;
2172 + r->_mp_size = sign ? - rn : rn;
2176 +mpz_init_set_d (mpz_t r, double x)
2183 +mpz_get_d (const mpz_t u)
2187 + double B = 2.0 * (double) GMP_LIMB_HIGHBIT;
2189 + un = GMP_ABS (u->_mp_size);
2194 + x = u->_mp_d[--un];
2196 + x = B*x + u->_mp_d[--un];
2198 + if (u->_mp_size < 0)
2205 +mpz_cmpabs_d (const mpz_t x, double d)
2216 + xn = GMP_ABS (xn);
2218 + B = 2.0 * (double) GMP_LIMB_HIGHBIT;
2221 + /* Scale d so it can be compared with the top limb. */
2222 + for (i = 1; i < xn; i++)
2228 + /* Compare floor(d) to top limb, subtract and cancel when equal. */
2229 + for (i = xn; i-- > 0;)
2233 + f = (mp_limb_t) d;
2242 + return - (d > 0.0);
2246 +mpz_cmp_d (const mpz_t x, double d)
2248 + if (x->_mp_size < 0)
2253 + return -mpz_cmpabs_d (x, d);
2260 + return mpz_cmpabs_d (x, d);
2265 +/* MPZ comparisons and the like. */
2267 +mpz_sgn (const mpz_t u)
2269 + mp_size_t usize = u->_mp_size;
2271 + return (usize > 0) - (usize < 0);
2275 +mpz_cmp_si (const mpz_t u, long v)
2277 + mp_size_t usize = u->_mp_size;
2282 + return mpz_cmp_ui (u, v);
2283 + else if (usize >= 0)
2285 + else /* usize == -1 */
2287 + mp_limb_t ul = u->_mp_d[0];
2288 + if ((mp_limb_t)GMP_NEG_CAST (unsigned long int, v) < ul)
2291 + return (mp_limb_t)GMP_NEG_CAST (unsigned long int, v) > ul;
2296 +mpz_cmp_ui (const mpz_t u, unsigned long v)
2298 + mp_size_t usize = u->_mp_size;
2302 + else if (usize < 0)
2306 + mp_limb_t ul = (usize > 0) ? u->_mp_d[0] : 0;
2307 + return (ul > v) - (ul < v);
2312 +mpz_cmp (const mpz_t a, const mpz_t b)
2314 + mp_size_t asize = a->_mp_size;
2315 + mp_size_t bsize = b->_mp_size;
2317 + if (asize != bsize)
2318 + return (asize < bsize) ? -1 : 1;
2319 + else if (asize >= 0)
2320 + return mpn_cmp (a->_mp_d, b->_mp_d, asize);
2322 + return mpn_cmp (b->_mp_d, a->_mp_d, -asize);
2326 +mpz_cmpabs_ui (const mpz_t u, unsigned long v)
2328 + mp_size_t un = GMP_ABS (u->_mp_size);
2334 + ul = (un == 1) ? u->_mp_d[0] : 0;
2336 + return (ul > v) - (ul < v);
2340 +mpz_cmpabs (const mpz_t u, const mpz_t v)
2342 + return mpn_cmp4 (u->_mp_d, GMP_ABS (u->_mp_size),
2343 + v->_mp_d, GMP_ABS (v->_mp_size));
2347 +mpz_abs (mpz_t r, const mpz_t u)
2352 + r->_mp_size = GMP_ABS (r->_mp_size);
2356 +mpz_neg (mpz_t r, const mpz_t u)
2361 + r->_mp_size = -r->_mp_size;
2365 +mpz_swap (mpz_t u, mpz_t v)
2367 + MP_SIZE_T_SWAP (u->_mp_size, v->_mp_size);
2368 + MP_SIZE_T_SWAP (u->_mp_alloc, v->_mp_alloc);
2369 + MP_PTR_SWAP (u->_mp_d, v->_mp_d);
2373 +/* MPZ addition and subtraction */
2375 +/* Adds to the absolute value. Returns new size, but doesn't store it. */
2377 +mpz_abs_add_ui (mpz_t r, const mpz_t a, unsigned long b)
2383 + an = GMP_ABS (a->_mp_size);
2390 + rp = MPZ_REALLOC (r, an + 1);
2392 + cy = mpn_add_1 (rp, a->_mp_d, an, b);
2399 +/* Subtract from the absolute value. Returns new size, (or -1 on underflow),
2400 + but doesn't store it. */
2402 +mpz_abs_sub_ui (mpz_t r, const mpz_t a, unsigned long b)
2404 + mp_size_t an = GMP_ABS (a->_mp_size);
2405 + mp_ptr rp = MPZ_REALLOC (r, an);
2412 + else if (an == 1 && a->_mp_d[0] < b)
2414 + rp[0] = b - a->_mp_d[0];
2419 + gmp_assert_nocarry (mpn_sub_1 (rp, a->_mp_d, an, b));
2420 + return mpn_normalized_size (rp, an);
2425 +mpz_add_ui (mpz_t r, const mpz_t a, unsigned long b)
2427 + if (a->_mp_size >= 0)
2428 + r->_mp_size = mpz_abs_add_ui (r, a, b);
2430 + r->_mp_size = -mpz_abs_sub_ui (r, a, b);
2434 +mpz_sub_ui (mpz_t r, const mpz_t a, unsigned long b)
2436 + if (a->_mp_size < 0)
2437 + r->_mp_size = -mpz_abs_add_ui (r, a, b);
2439 + r->_mp_size = mpz_abs_sub_ui (r, a, b);
2443 +mpz_ui_sub (mpz_t r, unsigned long a, const mpz_t b)
2445 + if (b->_mp_size < 0)
2446 + r->_mp_size = mpz_abs_add_ui (r, b, a);
2448 + r->_mp_size = -mpz_abs_sub_ui (r, b, a);
2452 +mpz_abs_add (mpz_t r, const mpz_t a, const mpz_t b)
2454 + mp_size_t an = GMP_ABS (a->_mp_size);
2455 + mp_size_t bn = GMP_ABS (b->_mp_size);
2461 + MPZ_SRCPTR_SWAP (a, b);
2462 + MP_SIZE_T_SWAP (an, bn);
2465 + rp = MPZ_REALLOC (r, an + 1);
2466 + cy = mpn_add (rp, a->_mp_d, an, b->_mp_d, bn);
2474 +mpz_abs_sub (mpz_t r, const mpz_t a, const mpz_t b)
2476 + mp_size_t an = GMP_ABS (a->_mp_size);
2477 + mp_size_t bn = GMP_ABS (b->_mp_size);
2481 + cmp = mpn_cmp4 (a->_mp_d, an, b->_mp_d, bn);
2484 + rp = MPZ_REALLOC (r, an);
2485 + gmp_assert_nocarry (mpn_sub (rp, a->_mp_d, an, b->_mp_d, bn));
2486 + return mpn_normalized_size (rp, an);
2490 + rp = MPZ_REALLOC (r, bn);
2491 + gmp_assert_nocarry (mpn_sub (rp, b->_mp_d, bn, a->_mp_d, an));
2492 + return -mpn_normalized_size (rp, bn);
2499 +mpz_add (mpz_t r, const mpz_t a, const mpz_t b)
2503 + if ( (a->_mp_size ^ b->_mp_size) >= 0)
2504 + rn = mpz_abs_add (r, a, b);
2506 + rn = mpz_abs_sub (r, a, b);
2508 + r->_mp_size = a->_mp_size >= 0 ? rn : - rn;
2512 +mpz_sub (mpz_t r, const mpz_t a, const mpz_t b)
2516 + if ( (a->_mp_size ^ b->_mp_size) >= 0)
2517 + rn = mpz_abs_sub (r, a, b);
2519 + rn = mpz_abs_add (r, a, b);
2521 + r->_mp_size = a->_mp_size >= 0 ? rn : - rn;
2525 +/* MPZ multiplication */
2527 +mpz_mul_si (mpz_t r, const mpz_t u, long int v)
2531 + mpz_mul_ui (r, u, GMP_NEG_CAST (unsigned long int, v));
2535 + mpz_mul_ui (r, u, (unsigned long int) v);
2539 +mpz_mul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2547 + if (us == 0 || v == 0)
2553 + un = GMP_ABS (us);
2555 + tp = MPZ_REALLOC (r, un + 1);
2556 + cy = mpn_mul_1 (tp, u->_mp_d, un, v);
2560 + r->_mp_size = (us < 0) ? - un : un;
2564 +mpz_mul (mpz_t r, const mpz_t u, const mpz_t v)
2567 + mp_size_t un, vn, rn;
2574 + if (un == 0 || vn == 0)
2580 + sign = (un ^ vn) < 0;
2582 + un = GMP_ABS (un);
2583 + vn = GMP_ABS (vn);
2585 + mpz_init2 (t, (un + vn) * GMP_LIMB_BITS);
2589 + mpn_mul (tp, u->_mp_d, un, v->_mp_d, vn);
2591 + mpn_mul (tp, v->_mp_d, vn, u->_mp_d, un);
2594 + rn -= tp[rn-1] == 0;
2596 + t->_mp_size = sign ? - rn : rn;
2602 +mpz_mul_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bits)
2609 + un = GMP_ABS (u->_mp_size);
2616 + limbs = bits / GMP_LIMB_BITS;
2617 + shift = bits % GMP_LIMB_BITS;
2619 + rn = un + limbs + (shift > 0);
2620 + rp = MPZ_REALLOC (r, rn);
2623 + mp_limb_t cy = mpn_lshift (rp + limbs, u->_mp_d, un, shift);
2628 + mpn_copyd (rp + limbs, u->_mp_d, un);
2633 + r->_mp_size = (u->_mp_size < 0) ? - rn : rn;
2637 +mpz_addmul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2641 + mpz_mul_ui (t, u, v);
2642 + mpz_add (r, r, t);
2647 +mpz_submul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2651 + mpz_mul_ui (t, u, v);
2652 + mpz_sub (r, r, t);
2657 +mpz_addmul (mpz_t r, const mpz_t u, const mpz_t v)
2661 + mpz_mul (t, u, v);
2662 + mpz_add (r, r, t);
2667 +mpz_submul (mpz_t r, const mpz_t u, const mpz_t v)
2671 + mpz_mul (t, u, v);
2672 + mpz_sub (r, r, t);
2678 +enum mpz_div_round_mode { GMP_DIV_FLOOR, GMP_DIV_CEIL, GMP_DIV_TRUNC };
2680 +/* Allows q or r to be zero. Returns 1 iff remainder is non-zero. */
2682 +mpz_div_qr (mpz_t q, mpz_t r,
2683 + const mpz_t n, const mpz_t d, enum mpz_div_round_mode mode)
2685 + mp_size_t ns, ds, nn, dn, qs;
2690 + gmp_die("mpz_div_qr: Divide by zero.");
2701 + nn = GMP_ABS (ns);
2702 + dn = GMP_ABS (ds);
2708 + if (mode == GMP_DIV_CEIL && qs >= 0)
2710 + /* q = 1, r = n - d */
2712 + mpz_sub (r, n, d);
2714 + mpz_set_ui (q, 1);
2716 + else if (mode == GMP_DIV_FLOOR && qs < 0)
2718 + /* q = -1, r = n + d */
2720 + mpz_add (r, n, d);
2722 + mpz_set_si (q, -1);
2726 + /* q = 0, r = d */
2740 + mpz_init_set (tr, n);
2747 + mpz_init2 (tq, qn * GMP_LIMB_BITS);
2753 + mpn_div_qr (qp, np, nn, d->_mp_d, dn);
2757 + qn -= (qp[qn-1] == 0);
2759 + tq->_mp_size = qs < 0 ? -qn : qn;
2761 + rn = mpn_normalized_size (np, dn);
2762 + tr->_mp_size = ns < 0 ? - rn : rn;
2764 + if (mode == GMP_DIV_FLOOR && qs < 0 && rn != 0)
2767 + mpz_sub_ui (tq, tq, 1);
2769 + mpz_add (tr, tr, d);
2771 + else if (mode == GMP_DIV_CEIL && qs >= 0 && rn != 0)
2774 + mpz_add_ui (tq, tq, 1);
2776 + mpz_sub (tr, tr, d);
2794 +mpz_cdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2796 + mpz_div_qr (q, r, n, d, GMP_DIV_CEIL);
2800 +mpz_fdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2802 + mpz_div_qr (q, r, n, d, GMP_DIV_FLOOR);
2806 +mpz_tdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2808 + mpz_div_qr (q, r, n, d, GMP_DIV_TRUNC);
2812 +mpz_cdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2814 + mpz_div_qr (q, NULL, n, d, GMP_DIV_CEIL);
2818 +mpz_fdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2820 + mpz_div_qr (q, NULL, n, d, GMP_DIV_FLOOR);
2824 +mpz_tdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2826 + mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC);
2830 +mpz_cdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2832 + mpz_div_qr (NULL, r, n, d, GMP_DIV_CEIL);
2836 +mpz_fdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2838 + mpz_div_qr (NULL, r, n, d, GMP_DIV_FLOOR);
2842 +mpz_tdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2844 + mpz_div_qr (NULL, r, n, d, GMP_DIV_TRUNC);
2848 +mpz_mod (mpz_t r, const mpz_t n, const mpz_t d)
2850 + mpz_div_qr (NULL, r, n, d, d->_mp_size >= 0 ? GMP_DIV_FLOOR : GMP_DIV_CEIL);
2854 +mpz_div_q_2exp (mpz_t q, const mpz_t u, mp_bitcnt_t bit_index,
2855 + enum mpz_div_round_mode mode)
2858 + mp_size_t limb_cnt;
2868 + limb_cnt = bit_index / GMP_LIMB_BITS;
2869 + qn = GMP_ABS (un) - limb_cnt;
2870 + bit_index %= GMP_LIMB_BITS;
2872 + if (mode == ((un > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* un != 0 here. */
2873 + /* Note: Below, the final indexing at limb_cnt is valid because at
2874 + that point we have qn > 0. */
2876 + || !mpn_zero_p (u->_mp_d, limb_cnt)
2877 + || (u->_mp_d[limb_cnt]
2878 + & (((mp_limb_t) 1 << bit_index) - 1)));
2887 + qp = MPZ_REALLOC (q, qn);
2889 + if (bit_index != 0)
2891 + mpn_rshift (qp, u->_mp_d + limb_cnt, qn, bit_index);
2892 + qn -= qp[qn - 1] == 0;
2896 + mpn_copyi (qp, u->_mp_d + limb_cnt, qn);
2903 + mpz_add_ui (q, q, 1);
2909 +mpz_div_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bit_index,
2910 + enum mpz_div_round_mode mode)
2912 + mp_size_t us, un, rn;
2917 + if (us == 0 || bit_index == 0)
2922 + rn = (bit_index + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
2925 + rp = MPZ_REALLOC (r, rn);
2926 + un = GMP_ABS (us);
2928 + mask = GMP_LIMB_MAX >> (rn * GMP_LIMB_BITS - bit_index);
2932 + /* Quotient (with truncation) is zero, and remainder is
2934 + if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
2936 + /* Have to negate and sign extend. */
2940 + for (cy = 1, i = 0; i < un; i++)
2942 + mp_limb_t s = ~u->_mp_d[i] + cy;
2947 + for (; i < rn - 1; i++)
2948 + rp[i] = GMP_LIMB_MAX;
2957 + mpn_copyi (rp, u->_mp_d, un);
2965 + mpn_copyi (rp, u->_mp_d, rn - 1);
2967 + rp[rn-1] = u->_mp_d[rn-1] & mask;
2969 + if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
2971 + /* If r != 0, compute 2^{bit_count} - r. */
2974 + for (i = 0; i < rn && rp[i] == 0; i++)
2978 + /* r > 0, need to flip sign. */
2979 + rp[i] = ~rp[i] + 1;
2985 + /* us is not used for anything else, so we can modify it
2986 + here to indicate flipped sign. */
2991 + rn = mpn_normalized_size (rp, rn);
2992 + r->_mp_size = us < 0 ? -rn : rn;
2996 +mpz_cdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2998 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_CEIL);
3002 +mpz_fdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
3004 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_FLOOR);
3008 +mpz_tdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
3010 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_TRUNC);
3014 +mpz_cdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
3016 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_CEIL);
3020 +mpz_fdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
3022 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_FLOOR);
3026 +mpz_tdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
3028 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_TRUNC);
3032 +mpz_divexact (mpz_t q, const mpz_t n, const mpz_t d)
3034 + gmp_assert_nocarry (mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC));
3038 +mpz_divisible_p (const mpz_t n, const mpz_t d)
3040 + return mpz_div_qr (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
3044 +mpz_congruent_p (const mpz_t a, const mpz_t b, const mpz_t m)
3049 + /* a == b (mod 0) iff a == b */
3050 + if (mpz_sgn (m) == 0)
3051 + return (mpz_cmp (a, b) == 0);
3054 + mpz_sub (t, a, b);
3055 + res = mpz_divisible_p (t, m);
3061 +static unsigned long
3062 +mpz_div_qr_ui (mpz_t q, mpz_t r,
3063 + const mpz_t n, unsigned long d, enum mpz_div_round_mode mode)
3080 + qn = GMP_ABS (ns);
3082 + qp = MPZ_REALLOC (q, qn);
3086 + rl = mpn_div_qr_1 (qp, n->_mp_d, qn, d);
3090 + rs = (ns < 0) ? -rs : rs;
3092 + if (rl > 0 && ( (mode == GMP_DIV_FLOOR && ns < 0)
3093 + || (mode == GMP_DIV_CEIL && ns >= 0)))
3096 + gmp_assert_nocarry (mpn_add_1 (qp, qp, qn, 1));
3108 + qn -= (qp[qn-1] == 0);
3109 + assert (qn == 0 || qp[qn-1] > 0);
3111 + q->_mp_size = (ns < 0) ? - qn : qn;
3118 +mpz_cdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
3120 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_CEIL);
3124 +mpz_fdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
3126 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_FLOOR);
3130 +mpz_tdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
3132 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_TRUNC);
3136 +mpz_cdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
3138 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_CEIL);
3142 +mpz_fdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
3144 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_FLOOR);
3148 +mpz_tdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
3150 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC);
3154 +mpz_cdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
3156 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_CEIL);
3159 +mpz_fdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
3161 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
3164 +mpz_tdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
3166 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_TRUNC);
3170 +mpz_cdiv_ui (const mpz_t n, unsigned long d)
3172 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_CEIL);
3176 +mpz_fdiv_ui (const mpz_t n, unsigned long d)
3178 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_FLOOR);
3182 +mpz_tdiv_ui (const mpz_t n, unsigned long d)
3184 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC);
3188 +mpz_mod_ui (mpz_t r, const mpz_t n, unsigned long d)
3190 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
3194 +mpz_divexact_ui (mpz_t q, const mpz_t n, unsigned long d)
3196 + gmp_assert_nocarry (mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC));
3200 +mpz_divisible_ui_p (const mpz_t n, unsigned long d)
3202 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
3208 +mpn_gcd_11 (mp_limb_t u, mp_limb_t v)
3212 + assert ( (u | v) > 0);
3219 + gmp_ctz (shift, u | v);
3224 + if ( (u & 1) == 0)
3225 + MP_LIMB_T_SWAP (u, v);
3227 + while ( (v & 1) == 0)
3237 + while ( (u & 1) == 0);
3244 + while ( (v & 1) == 0);
3247 + return u << shift;
3251 +mpz_gcd_ui (mpz_t g, const mpz_t u, unsigned long v)
3262 + un = GMP_ABS (u->_mp_size);
3264 + v = mpn_gcd_11 (mpn_div_qr_1 (NULL, u->_mp_d, un, v), v);
3267 + mpz_set_ui (g, v);
3274 +mpz_make_odd (mpz_t r)
3276 + mp_bitcnt_t shift;
3278 + assert (r->_mp_size > 0);
3279 + /* Count trailing zeros, equivalent to mpn_scan1, because we know that there is a 1 */
3280 + shift = mpn_common_scan (r->_mp_d[0], 0, r->_mp_d, 0, 0);
3281 + mpz_tdiv_q_2exp (r, r, shift);
3287 +mpz_gcd (mpz_t g, const mpz_t u, const mpz_t v)
3290 + mp_bitcnt_t uz, vz, gz;
3292 + if (u->_mp_size == 0)
3297 + if (v->_mp_size == 0)
3307 + uz = mpz_make_odd (tu);
3309 + vz = mpz_make_odd (tv);
3310 + gz = GMP_MIN (uz, vz);
3312 + if (tu->_mp_size < tv->_mp_size)
3313 + mpz_swap (tu, tv);
3315 + mpz_tdiv_r (tu, tu, tv);
3316 + if (tu->_mp_size == 0)
3325 + mpz_make_odd (tu);
3326 + c = mpz_cmp (tu, tv);
3333 + mpz_swap (tu, tv);
3335 + if (tv->_mp_size == 1)
3337 + mp_limb_t vl = tv->_mp_d[0];
3338 + mp_limb_t ul = mpz_tdiv_ui (tu, vl);
3339 + mpz_set_ui (g, mpn_gcd_11 (ul, vl));
3342 + mpz_sub (tu, tu, tv);
3346 + mpz_mul_2exp (g, g, gz);
3350 +mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, const mpz_t u, const mpz_t v)
3352 + mpz_t tu, tv, s0, s1, t0, t1;
3353 + mp_bitcnt_t uz, vz, gz;
3354 + mp_bitcnt_t power;
3356 + if (u->_mp_size == 0)
3358 + /* g = 0 u + sgn(v) v */
3359 + signed long sign = mpz_sgn (v);
3362 + mpz_set_ui (s, 0);
3364 + mpz_set_si (t, sign);
3368 + if (v->_mp_size == 0)
3370 + /* g = sgn(u) u + 0 v */
3371 + signed long sign = mpz_sgn (u);
3374 + mpz_set_si (s, sign);
3376 + mpz_set_ui (t, 0);
3388 + uz = mpz_make_odd (tu);
3390 + vz = mpz_make_odd (tv);
3391 + gz = GMP_MIN (uz, vz);
3396 + /* Cofactors corresponding to odd gcd. gz handled later. */
3397 + if (tu->_mp_size < tv->_mp_size)
3399 + mpz_swap (tu, tv);
3400 + MPZ_SRCPTR_SWAP (u, v);
3401 + MPZ_PTR_SWAP (s, t);
3402 + MP_BITCNT_T_SWAP (uz, vz);
3407 + * u = t0 tu + t1 tv
3408 + * v = s0 tu + s1 tv
3410 + * where u and v denote the inputs with common factors of two
3411 + * eliminated, and det (s0, t0; s1, t1) = 2^p. Then
3413 + * 2^p tu = s1 u - t1 v
3414 + * 2^p tv = -s0 u + t0 v
3417 + /* After initial division, tu = q tv + tu', we have
3419 + * u = 2^uz (tu' + q tv)
3424 + * t0 = 2^uz, t1 = 2^uz q
3425 + * s0 = 0, s1 = 2^vz
3428 + mpz_setbit (t0, uz);
3429 + mpz_tdiv_qr (t1, tu, tu, tv);
3430 + mpz_mul_2exp (t1, t1, uz);
3432 + mpz_setbit (s1, vz);
3435 + if (tu->_mp_size > 0)
3437 + mp_bitcnt_t shift;
3438 + shift = mpz_make_odd (tu);
3439 + mpz_mul_2exp (t0, t0, shift);
3440 + mpz_mul_2exp (s0, s0, shift);
3446 + c = mpz_cmp (tu, tv);
3454 + * u = t0 tu + t1 (tv' + tu) = (t0 + t1) tu + t1 tv'
3455 + * v = s0 tu + s1 (tv' + tu) = (s0 + s1) tu + s1 tv' */
3457 + mpz_sub (tv, tv, tu);
3458 + mpz_add (t0, t0, t1);
3459 + mpz_add (s0, s0, s1);
3461 + shift = mpz_make_odd (tv);
3462 + mpz_mul_2exp (t1, t1, shift);
3463 + mpz_mul_2exp (s1, s1, shift);
3467 + mpz_sub (tu, tu, tv);
3468 + mpz_add (t1, t0, t1);
3469 + mpz_add (s1, s0, s1);
3471 + shift = mpz_make_odd (tu);
3472 + mpz_mul_2exp (t0, t0, shift);
3473 + mpz_mul_2exp (s0, s0, shift);
3479 + /* Now tv = odd part of gcd, and -s0 and t0 are corresponding
3482 + mpz_mul_2exp (tv, tv, gz);
3485 + /* 2^p g = s0 u + t0 v. Eliminate one factor of two at a time. To
3486 + adjust cofactors, we need u / g and v / g */
3488 + mpz_divexact (s1, v, tv);
3490 + mpz_divexact (t1, u, tv);
3493 + while (power-- > 0)
3495 + /* s0 u + t0 v = (s0 - v/g) u - (t0 + u/g) v */
3496 + if (mpz_odd_p (s0) || mpz_odd_p (t0))
3498 + mpz_sub (s0, s0, s1);
3499 + mpz_add (t0, t0, t1);
3501 + mpz_divexact_ui (s0, s0, 2);
3502 + mpz_divexact_ui (t0, t0, 2);
3505 + /* Arrange so that |s| < |u| / 2g */
3506 + mpz_add (s1, s0, s1);
3507 + if (mpz_cmpabs (s0, s1) > 0)
3509 + mpz_swap (s0, s1);
3510 + mpz_sub (t0, t0, t1);
3512 + if (u->_mp_size < 0)
3514 + if (v->_mp_size < 0)
3532 +mpz_lcm (mpz_t r, const mpz_t u, const mpz_t v)
3536 + if (u->_mp_size == 0 || v->_mp_size == 0)
3544 + mpz_gcd (g, u, v);
3545 + mpz_divexact (g, u, g);
3546 + mpz_mul (r, g, v);
3553 +mpz_lcm_ui (mpz_t r, const mpz_t u, unsigned long v)
3555 + if (v == 0 || u->_mp_size == 0)
3561 + v /= mpz_gcd_ui (NULL, u, v);
3562 + mpz_mul_ui (r, u, v);
3568 +mpz_invert (mpz_t r, const mpz_t u, const mpz_t m)
3573 + if (u->_mp_size == 0 || mpz_cmpabs_ui (m, 1) <= 0)
3579 + mpz_gcdext (g, tr, NULL, u, m);
3580 + invertible = (mpz_cmp_ui (g, 1) == 0);
3584 + if (tr->_mp_size < 0)
3586 + if (m->_mp_size >= 0)
3587 + mpz_add (tr, tr, m);
3589 + mpz_sub (tr, tr, m);
3596 + return invertible;
3600 +/* Higher level operations (sqrt, pow and root) */
3603 +mpz_pow_ui (mpz_t r, const mpz_t b, unsigned long e)
3605 + unsigned long bit;
3607 + mpz_init_set_ui (tr, 1);
3609 + bit = GMP_ULONG_HIGHBIT;
3612 + mpz_mul (tr, tr, tr);
3614 + mpz_mul (tr, tr, b);
3624 +mpz_ui_pow_ui (mpz_t r, unsigned long blimb, unsigned long e)
3627 + mpz_init_set_ui (b, blimb);
3628 + mpz_pow_ui (r, b, e);
3633 +mpz_powm (mpz_t r, const mpz_t b, const mpz_t e, const mpz_t m)
3639 + struct gmp_div_inverse minv;
3643 + en = GMP_ABS (e->_mp_size);
3644 + mn = GMP_ABS (m->_mp_size);
3646 + gmp_die ("mpz_powm: Zero modulo.");
3650 + mpz_set_ui (r, 1);
3655 + mpn_div_qr_invert (&minv, mp, mn);
3656 + shift = minv.shift;
3660 + /* To avoid shifts, we do all our reductions, except the final
3661 + one, using a *normalized* m. */
3664 + tp = gmp_xalloc_limbs (mn);
3665 + gmp_assert_nocarry (mpn_lshift (tp, mp, mn, shift));
3671 + if (e->_mp_size < 0)
3673 + if (!mpz_invert (base, b, m))
3674 + gmp_die ("mpz_powm: Negative exponent and non-invertible base.");
3679 + mpz_abs (base, b);
3681 + bn = base->_mp_size;
3684 + mpn_div_qr_preinv (NULL, base->_mp_d, base->_mp_size, mp, mn, &minv);
3688 + /* We have reduced the absolute value. Now take care of the
3689 + sign. Note that we get zero represented non-canonically as
3691 + if (b->_mp_size < 0)
3693 + mp_ptr bp = MPZ_REALLOC (base, mn);
3694 + gmp_assert_nocarry (mpn_sub (bp, mp, mn, bp, bn));
3697 + base->_mp_size = mpn_normalized_size (base->_mp_d, bn);
3699 + mpz_init_set_ui (tr, 1);
3703 + mp_limb_t w = e->_mp_d[en];
3706 + bit = GMP_LIMB_HIGHBIT;
3709 + mpz_mul (tr, tr, tr);
3711 + mpz_mul (tr, tr, base);
3712 + if (tr->_mp_size > mn)
3714 + mpn_div_qr_preinv (NULL, tr->_mp_d, tr->_mp_size, mp, mn, &minv);
3715 + tr->_mp_size = mpn_normalized_size (tr->_mp_d, mn);
3722 + /* Final reduction */
3723 + if (tr->_mp_size >= mn)
3725 + minv.shift = shift;
3726 + mpn_div_qr_preinv (NULL, tr->_mp_d, tr->_mp_size, mp, mn, &minv);
3727 + tr->_mp_size = mpn_normalized_size (tr->_mp_d, mn);
3738 +mpz_powm_ui (mpz_t r, const mpz_t b, unsigned long elimb, const mpz_t m)
3741 + mpz_init_set_ui (e, elimb);
3742 + mpz_powm (r, b, e, m);
3746 +/* x=trunc(y^(1/z)), r=y-x^z */
3748 +mpz_rootrem (mpz_t x, mpz_t r, const mpz_t y, unsigned long z)
3753 + sgn = y->_mp_size < 0;
3754 + if ((~z & sgn) != 0)
3755 + gmp_die ("mpz_rootrem: Negative argument, with even root.");
3757 + gmp_die ("mpz_rootrem: Zeroth root.");
3759 + if (mpz_cmpabs_ui (y, 1) <= 0) {
3770 + tb = mpz_sizeinbase (y, 2) / z + 1;
3771 + mpz_init2 (t, tb);
3772 + mpz_setbit (t, tb);
3775 + if (z == 2) /* simplify sqrt loop: z-1 == 1 */
3777 + mpz_swap (u, t); /* u = x */
3778 + mpz_tdiv_q (t, y, u); /* t = y/x */
3779 + mpz_add (t, t, u); /* t = y/x + x */
3780 + mpz_tdiv_q_2exp (t, t, 1); /* x'= (y/x + x)/2 */
3781 + } while (mpz_cmpabs (t, u) < 0); /* |x'| < |x| */
3782 + else /* z != 2 */ {
3790 + mpz_swap (u, t); /* u = x */
3791 + mpz_pow_ui (t, u, z - 1); /* t = x^(z-1) */
3792 + mpz_tdiv_q (t, y, t); /* t = y/x^(z-1) */
3793 + mpz_mul_ui (v, u, z - 1); /* v = x*(z-1) */
3794 + mpz_add (t, t, v); /* t = y/x^(z-1) + x*(z-1) */
3795 + mpz_tdiv_q_ui (t, t, z); /* x'=(y/x^(z-1) + x*(z-1))/z */
3796 + } while (mpz_cmpabs (t, u) < 0); /* |x'| < |x| */
3802 + mpz_pow_ui (t, u, z);
3803 + mpz_sub (r, y, t);
3812 +mpz_root (mpz_t x, const mpz_t y, unsigned long z)
3818 + mpz_rootrem (x, r, y, z);
3819 + res = r->_mp_size == 0;
3825 +/* Compute s = floor(sqrt(u)) and r = u - s^2. Allows r == NULL */
3827 +mpz_sqrtrem (mpz_t s, mpz_t r, const mpz_t u)
3829 + mpz_rootrem (s, r, u, 2);
3833 +mpz_sqrt (mpz_t s, const mpz_t u)
3835 + mpz_rootrem (s, NULL, u, 2);
3839 +mpz_perfect_square_p (const mpz_t u)
3841 + if (u->_mp_size <= 0)
3842 + return (u->_mp_size == 0);
3844 + return mpz_root (NULL, u, 2);
3848 +mpn_perfect_square_p (mp_srcptr p, mp_size_t n)
3853 + assert (p [n-1] != 0);
3854 + return mpz_root (NULL, mpz_roinit_n (t, p, n), 2);
3858 +mpn_sqrtrem (mp_ptr sp, mp_ptr rp, mp_srcptr p, mp_size_t n)
3864 + assert (p [n-1] != 0);
3868 + mpz_rootrem (s, r, mpz_roinit_n (u, p, n), 2);
3870 + assert (s->_mp_size == (n+1)/2);
3871 + mpn_copyd (sp, s->_mp_d, s->_mp_size);
3873 + res = r->_mp_size;
3875 + mpn_copyd (rp, r->_mp_d, res);
3880 +/* Combinatorics */
3883 +mpz_fac_ui (mpz_t x, unsigned long n)
3885 + mpz_set_ui (x, n + (n == 0));
3887 + mpz_mul_ui (x, x, --n);
3891 +mpz_bin_uiui (mpz_t r, unsigned long n, unsigned long k)
3895 + mpz_set_ui (r, k <= n);
3898 + k = (k <= n) ? n - k : 0;
3901 + mpz_fac_ui (t, k);
3903 + for (; k > 0; k--)
3904 + mpz_mul_ui (r, r, n--);
3906 + mpz_divexact (r, r, t);
3911 +/* Primality testing */
3913 +gmp_millerrabin (const mpz_t n, const mpz_t nm1, mpz_t y,
3914 + const mpz_t q, mp_bitcnt_t k)
3918 + /* Caller must initialize y to the base. */
3919 + mpz_powm (y, y, q, n);
3921 + if (mpz_cmp_ui (y, 1) == 0 || mpz_cmp (y, nm1) == 0)
3926 + mpz_powm_ui (y, y, 2, n);
3927 + if (mpz_cmp (y, nm1) == 0)
3929 + /* y == 1 means that the previous y was a non-trivial square root
3930 + of 1 (mod n). y == 0 means that n is a power of the base.
3931 + In either case, n is not prime. */
3932 + if (mpz_cmp_ui (y, 1) <= 0)
3938 +/* This product is 0xc0cfd797, and fits in 32 bits. */
3939 +#define GMP_PRIME_PRODUCT \
3940 + (3UL*5UL*7UL*11UL*13UL*17UL*19UL*23UL*29UL)
3942 +/* Bit (p+1)/2 is set, for each odd prime <= 61 */
3943 +#define GMP_PRIME_MASK 0xc96996dcUL
3946 +mpz_probab_prime_p (const mpz_t n, int reps)
3955 + /* Note that we use the absolute value of n only, for compatibility
3956 + with the real GMP. */
3957 + if (mpz_even_p (n))
3958 + return (mpz_cmpabs_ui (n, 2) == 0) ? 2 : 0;
3960 + /* Above test excludes n == 0 */
3961 + assert (n->_mp_size != 0);
3963 + if (mpz_cmpabs_ui (n, 64) < 0)
3964 + return (GMP_PRIME_MASK >> (n->_mp_d[0] >> 1)) & 2;
3966 + if (mpz_gcd_ui (NULL, n, GMP_PRIME_PRODUCT) != 1)
3969 + /* All prime factors are >= 31. */
3970 + if (mpz_cmpabs_ui (n, 31*31) < 0)
3973 + /* Use Miller-Rabin, with a deterministic sequence of bases, a[j] =
3974 + j^2 + j + 41 using Euler's polynomial. We potentially stop early,
3975 + if a[j] >= n - 1. Since n >= 31*31, this can happen only if reps >
3976 + 30 (a[30] == 971 > 31*31 == 961). */
3982 + /* Find q and k, where q is odd and n = 1 + 2**k * q. */
3983 + nm1->_mp_size = mpz_abs_sub_ui (nm1, n, 1);
3984 + k = mpz_scan1 (nm1, 0);
3985 + mpz_tdiv_q_2exp (q, nm1, k);
3987 + for (j = 0, is_prime = 1; is_prime & (j < reps); j++)
3989 + mpz_set_ui (y, (unsigned long) j*j+j+41);
3990 + if (mpz_cmp (y, nm1) >= 0)
3992 + /* Don't try any further bases. This "early" break does not affect
3993 + the result for any reasonable reps value (<=5000 was tested) */
3997 + is_prime = gmp_millerrabin (n, nm1, y, q, k);
4007 +/* Logical operations and bit manipulation. */
4009 +/* Numbers are treated as if represented in two's complement (and
4010 + infinitely sign extended). For a negative values we get the two's
4011 + complement from -x = ~x + 1, where ~ is bitwise complement.
4012 + Negation transforms
4020 + where yyyy is the bitwise complement of xxxx. So least significant
4021 + bits, up to and including the first one bit, are unchanged, and
4022 + the more significant bits are all complemented.
4024 + To change a bit from zero to one in a negative number, subtract the
4025 + corresponding power of two from the absolute value. This can never
4026 + underflow. To change a bit from one to zero, add the corresponding
4027 + power of two, and this might overflow. E.g., if x = -001111, the
4028 + two's complement is 110001. Clearing the least significant bit, we
4029 + get two's complement 110000, and -010000. */
4032 +mpz_tstbit (const mpz_t d, mp_bitcnt_t bit_index)
4034 + mp_size_t limb_index;
4042 + dn = GMP_ABS (ds);
4043 + limb_index = bit_index / GMP_LIMB_BITS;
4044 + if (limb_index >= dn)
4047 + shift = bit_index % GMP_LIMB_BITS;
4048 + w = d->_mp_d[limb_index];
4049 + bit = (w >> shift) & 1;
4053 + /* d < 0. Check if any of the bits below is set: If so, our bit
4054 + must be complemented. */
4055 + if (shift > 0 && (w << (GMP_LIMB_BITS - shift)) > 0)
4057 + while (limb_index-- > 0)
4058 + if (d->_mp_d[limb_index] > 0)
4065 +mpz_abs_add_bit (mpz_t d, mp_bitcnt_t bit_index)
4067 + mp_size_t dn, limb_index;
4071 + dn = GMP_ABS (d->_mp_size);
4073 + limb_index = bit_index / GMP_LIMB_BITS;
4074 + bit = (mp_limb_t) 1 << (bit_index % GMP_LIMB_BITS);
4076 + if (limb_index >= dn)
4079 + /* The bit should be set outside of the end of the number.
4080 + We have to increase the size of the number. */
4081 + dp = MPZ_REALLOC (d, limb_index + 1);
4083 + dp[limb_index] = bit;
4084 + for (i = dn; i < limb_index; i++)
4086 + dn = limb_index + 1;
4094 + cy = mpn_add_1 (dp + limb_index, dp + limb_index, dn - limb_index, bit);
4097 + dp = MPZ_REALLOC (d, dn + 1);
4102 + d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
4106 +mpz_abs_sub_bit (mpz_t d, mp_bitcnt_t bit_index)
4108 + mp_size_t dn, limb_index;
4112 + dn = GMP_ABS (d->_mp_size);
4115 + limb_index = bit_index / GMP_LIMB_BITS;
4116 + bit = (mp_limb_t) 1 << (bit_index % GMP_LIMB_BITS);
4118 + assert (limb_index < dn);
4120 + gmp_assert_nocarry (mpn_sub_1 (dp + limb_index, dp + limb_index,
4121 + dn - limb_index, bit));
4122 + dn = mpn_normalized_size (dp, dn);
4123 + d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
4127 +mpz_setbit (mpz_t d, mp_bitcnt_t bit_index)
4129 + if (!mpz_tstbit (d, bit_index))
4131 + if (d->_mp_size >= 0)
4132 + mpz_abs_add_bit (d, bit_index);
4134 + mpz_abs_sub_bit (d, bit_index);
4139 +mpz_clrbit (mpz_t d, mp_bitcnt_t bit_index)
4141 + if (mpz_tstbit (d, bit_index))
4143 + if (d->_mp_size >= 0)
4144 + mpz_abs_sub_bit (d, bit_index);
4146 + mpz_abs_add_bit (d, bit_index);
4151 +mpz_combit (mpz_t d, mp_bitcnt_t bit_index)
4153 + if (mpz_tstbit (d, bit_index) ^ (d->_mp_size < 0))
4154 + mpz_abs_sub_bit (d, bit_index);
4156 + mpz_abs_add_bit (d, bit_index);
4160 +mpz_com (mpz_t r, const mpz_t u)
4163 + mpz_sub_ui (r, r, 1);
4167 +mpz_and (mpz_t r, const mpz_t u, const mpz_t v)
4169 + mp_size_t un, vn, rn, i;
4170 + mp_ptr up, vp, rp;
4172 + mp_limb_t ux, vx, rx;
4173 + mp_limb_t uc, vc, rc;
4174 + mp_limb_t ul, vl, rl;
4176 + un = GMP_ABS (u->_mp_size);
4177 + vn = GMP_ABS (v->_mp_size);
4180 + MPZ_SRCPTR_SWAP (u, v);
4181 + MP_SIZE_T_SWAP (un, vn);
4189 + uc = u->_mp_size < 0;
4190 + vc = v->_mp_size < 0;
4197 + /* If the smaller input is positive, higher limbs don't matter. */
4198 + rn = vx ? un : vn;
4200 + rp = MPZ_REALLOC (r, rn + rc);
4208 + ul = (up[i] ^ ux) + uc;
4211 + vl = (vp[i] ^ vx) + vc;
4214 + rl = ( (ul & vl) ^ rx) + rc;
4221 + for (; i < rn; i++)
4223 + ul = (up[i] ^ ux) + uc;
4226 + rl = ( (ul & vx) ^ rx) + rc;
4233 + rn = mpn_normalized_size (rp, rn);
4235 + r->_mp_size = rx ? -rn : rn;
4239 +mpz_ior (mpz_t r, const mpz_t u, const mpz_t v)
4241 + mp_size_t un, vn, rn, i;
4242 + mp_ptr up, vp, rp;
4244 + mp_limb_t ux, vx, rx;
4245 + mp_limb_t uc, vc, rc;
4246 + mp_limb_t ul, vl, rl;
4248 + un = GMP_ABS (u->_mp_size);
4249 + vn = GMP_ABS (v->_mp_size);
4252 + MPZ_SRCPTR_SWAP (u, v);
4253 + MP_SIZE_T_SWAP (un, vn);
4261 + uc = u->_mp_size < 0;
4262 + vc = v->_mp_size < 0;
4269 + /* If the smaller input is negative, by sign extension higher limbs
4271 + rn = vx ? vn : un;
4273 + rp = MPZ_REALLOC (r, rn + rc);
4281 + ul = (up[i] ^ ux) + uc;
4284 + vl = (vp[i] ^ vx) + vc;
4287 + rl = ( (ul | vl) ^ rx) + rc;
4294 + for (; i < rn; i++)
4296 + ul = (up[i] ^ ux) + uc;
4299 + rl = ( (ul | vx) ^ rx) + rc;
4306 + rn = mpn_normalized_size (rp, rn);
4308 + r->_mp_size = rx ? -rn : rn;
4312 +mpz_xor (mpz_t r, const mpz_t u, const mpz_t v)
4314 + mp_size_t un, vn, i;
4315 + mp_ptr up, vp, rp;
4317 + mp_limb_t ux, vx, rx;
4318 + mp_limb_t uc, vc, rc;
4319 + mp_limb_t ul, vl, rl;
4321 + un = GMP_ABS (u->_mp_size);
4322 + vn = GMP_ABS (v->_mp_size);
4325 + MPZ_SRCPTR_SWAP (u, v);
4326 + MP_SIZE_T_SWAP (un, vn);
4334 + uc = u->_mp_size < 0;
4335 + vc = v->_mp_size < 0;
4342 + rp = MPZ_REALLOC (r, un + rc);
4350 + ul = (up[i] ^ ux) + uc;
4353 + vl = (vp[i] ^ vx) + vc;
4356 + rl = (ul ^ vl ^ rx) + rc;
4363 + for (; i < un; i++)
4365 + ul = (up[i] ^ ux) + uc;
4368 + rl = (ul ^ ux) + rc;
4375 + un = mpn_normalized_size (rp, un);
4377 + r->_mp_size = rx ? -un : un;
4381 +gmp_popcount_limb (mp_limb_t x)
4385 + /* Do 16 bits at a time, to avoid limb-sized constants. */
4386 + for (c = 0; x > 0; x >>= 16)
4388 + unsigned w = ((x >> 1) & 0x5555) + (x & 0x5555);
4389 + w = ((w >> 2) & 0x3333) + (w & 0x3333);
4390 + w = ((w >> 4) & 0x0f0f) + (w & 0x0f0f);
4391 + w = (w >> 8) + (w & 0x00ff);
4398 +mpn_popcount (mp_srcptr p, mp_size_t n)
4403 + for (c = 0, i = 0; i < n; i++)
4404 + c += gmp_popcount_limb (p[i]);
4410 +mpz_popcount (const mpz_t u)
4417 + return ~(mp_bitcnt_t) 0;
4419 + return mpn_popcount (u->_mp_d, un);
4423 +mpz_hamdist (const mpz_t u, const mpz_t v)
4425 + mp_size_t un, vn, i;
4426 + mp_limb_t uc, vc, ul, vl, comp;
4433 + if ( (un ^ vn) < 0)
4434 + return ~(mp_bitcnt_t) 0;
4436 + comp = - (uc = vc = (un < 0));
4448 + MPN_SRCPTR_SWAP (up, un, vp, vn);
4450 + for (i = 0, c = 0; i < vn; i++)
4452 + ul = (up[i] ^ comp) + uc;
4455 + vl = (vp[i] ^ comp) + vc;
4458 + c += gmp_popcount_limb (ul ^ vl);
4462 + for (; i < un; i++)
4464 + ul = (up[i] ^ comp) + uc;
4467 + c += gmp_popcount_limb (ul ^ comp);
4474 +mpz_scan1 (const mpz_t u, mp_bitcnt_t starting_bit)
4477 + mp_size_t us, un, i;
4478 + mp_limb_t limb, ux;
4481 + un = GMP_ABS (us);
4482 + i = starting_bit / GMP_LIMB_BITS;
4484 + /* Past the end there's no 1 bits for u>=0, or an immediate 1 bit
4485 + for u<0. Notice this test picks up any u==0 too. */
4487 + return (us >= 0 ? ~(mp_bitcnt_t) 0 : starting_bit);
4493 + if (starting_bit != 0)
4497 + ux = mpn_zero_p (up, i);
4498 + limb = ~ limb + ux;
4499 + ux = - (mp_limb_t) (limb >= ux);
4502 + /* Mask to 0 all bits before starting_bit, thus ignoring them. */
4503 + limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
4506 + return mpn_common_scan (limb, i, up, un, ux);
4510 +mpz_scan0 (const mpz_t u, mp_bitcnt_t starting_bit)
4513 + mp_size_t us, un, i;
4514 + mp_limb_t limb, ux;
4517 + ux = - (mp_limb_t) (us >= 0);
4518 + un = GMP_ABS (us);
4519 + i = starting_bit / GMP_LIMB_BITS;
4521 + /* When past end, there's an immediate 0 bit for u>=0, or no 0 bits for
4522 + u<0. Notice this test picks up all cases of u==0 too. */
4524 + return (ux ? starting_bit : ~(mp_bitcnt_t) 0);
4527 + limb = up[i] ^ ux;
4530 + limb -= mpn_zero_p (up, i); /* limb = ~(~limb + zero_p) */
4532 + /* Mask all bits before starting_bit, thus ignoring them. */
4533 + limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
4535 + return mpn_common_scan (limb, i, up, un, ux);
4539 +/* MPZ base conversion. */
4542 +mpz_sizeinbase (const mpz_t u, int base)
4548 + struct gmp_div_inverse bi;
4551 + assert (base >= 2);
4552 + assert (base <= 36);
4554 + un = GMP_ABS (u->_mp_size);
4560 + bits = (un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1]);
4566 + return (bits + 1) / 2;
4568 + return (bits + 2) / 3;
4570 + return (bits + 3) / 4;
4572 + return (bits + 4) / 5;
4573 + /* FIXME: Do something more clever for the common case of base
4577 + tp = gmp_xalloc_limbs (un);
4578 + mpn_copyi (tp, up, un);
4579 + mpn_div_qr_1_invert (&bi, base);
4585 + mpn_div_qr_1_preinv (tp, tp, un, &bi);
4586 + un -= (tp[un-1] == 0);
4595 +mpz_get_str (char *sp, int base, const mpz_t u)
4598 + const char *digits;
4604 + digits = "0123456789abcdefghijklmnopqrstuvwxyz";
4609 + digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4616 + sn = 1 + mpz_sizeinbase (u, base);
4618 + sp = gmp_xalloc (1 + sn);
4620 + un = GMP_ABS (u->_mp_size);
4631 + if (u->_mp_size < 0)
4634 + bits = mpn_base_power_of_two_p (base);
4637 + /* Not modified in this case. */
4638 + sn = i + mpn_get_str_bits ((unsigned char *) sp + i, bits, u->_mp_d, un);
4641 + struct mpn_base_info info;
4644 + mpn_get_base_info (&info, base);
4645 + tp = gmp_xalloc_limbs (un);
4646 + mpn_copyi (tp, u->_mp_d, un);
4648 + sn = i + mpn_get_str_other ((unsigned char *) sp + i, base, &info, tp, un);
4652 + for (; i < sn; i++)
4653 + sp[i] = digits[(unsigned char) sp[i]];
4660 +mpz_set_str (mpz_t r, const char *sp, int base)
4663 + mp_size_t rn, alloc;
4667 + unsigned char *dp;
4669 + assert (base == 0 || (base >= 2 && base <= 36));
4671 + while (isspace( (unsigned char) *sp))
4674 + sign = (*sp == '-');
4682 + if (*sp == 'x' || *sp == 'X')
4687 + else if (*sp == 'b' || *sp == 'B')
4700 + dp = gmp_xalloc (sn + (sn == 0));
4702 + for (sn = 0; *sp; sp++)
4706 + if (isspace ((unsigned char) *sp))
4708 + if (*sp >= '0' && *sp <= '9')
4709 + digit = *sp - '0';
4710 + else if (*sp >= 'a' && *sp <= 'z')
4711 + digit = *sp - 'a' + 10;
4712 + else if (*sp >= 'A' && *sp <= 'Z')
4713 + digit = *sp - 'A' + 10;
4715 + digit = base; /* fail */
4717 + if (digit >= base)
4727 + bits = mpn_base_power_of_two_p (base);
4731 + alloc = (sn * bits + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
4732 + rp = MPZ_REALLOC (r, alloc);
4733 + rn = mpn_set_str_bits (rp, dp, sn, bits);
4737 + struct mpn_base_info info;
4738 + mpn_get_base_info (&info, base);
4739 + alloc = (sn + info.exp - 1) / info.exp;
4740 + rp = MPZ_REALLOC (r, alloc);
4741 + rn = mpn_set_str_other (rp, dp, sn, base, &info);
4743 + assert (rn <= alloc);
4746 + r->_mp_size = sign ? - rn : rn;
4752 +mpz_init_set_str (mpz_t r, const char *sp, int base)
4755 + return mpz_set_str (r, sp, base);
4759 +mpz_out_str (FILE *stream, int base, const mpz_t x)
4764 + str = mpz_get_str (NULL, base, x);
4765 + len = strlen (str);
4766 + len = fwrite (str, 1, len, stream);
4773 +gmp_detect_endian (void)
4775 + static const int i = 2;
4776 + const unsigned char *p = (const unsigned char *) &i;
4780 +/* Import and export. Does not support nails. */
4782 +mpz_import (mpz_t r, size_t count, int order, size_t size, int endian,
4783 + size_t nails, const void *src)
4785 + const unsigned char *p;
4786 + ptrdiff_t word_step;
4790 + /* The current (partial) limb. */
4792 + /* The number of bytes already copied to this limb (starting from
4795 + /* The index where the limb should be stored, when completed. */
4799 + gmp_die ("mpz_import: Nails not supported.");
4801 + assert (order == 1 || order == -1);
4802 + assert (endian >= -1 && endian <= 1);
4805 + endian = gmp_detect_endian ();
4807 + p = (unsigned char *) src;
4809 + word_step = (order != endian) ? 2 * size : 0;
4811 + /* Process bytes from the least significant end, so point p at the
4812 + least significant word. */
4815 + p += size * (count - 1);
4816 + word_step = - word_step;
4819 + /* And at least significant byte of that word. */
4823 + rn = (size * count + sizeof(mp_limb_t) - 1) / sizeof(mp_limb_t);
4824 + rp = MPZ_REALLOC (r, rn);
4826 + for (limb = 0, bytes = 0, i = 0; count > 0; count--, p += word_step)
4829 + for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
4831 + limb |= (mp_limb_t) *p << (bytes++ * CHAR_BIT);
4832 + if (bytes == sizeof(mp_limb_t))
4840 + assert (i + (bytes > 0) == rn);
4844 + i = mpn_normalized_size (rp, i);
4850 +mpz_export (void *r, size_t *countp, int order, size_t size, int endian,
4851 + size_t nails, const mpz_t u)
4857 + gmp_die ("mpz_import: Nails not supported.");
4859 + assert (order == 1 || order == -1);
4860 + assert (endian >= -1 && endian <= 1);
4861 + assert (size > 0 || u->_mp_size == 0);
4869 + ptrdiff_t word_step;
4870 + /* The current (partial) limb. */
4872 + /* The number of bytes left to to in this limb. */
4874 + /* The index where the limb was read. */
4877 + un = GMP_ABS (un);
4879 + /* Count bytes in top limb. */
4880 + limb = u->_mp_d[un-1];
4881 + assert (limb != 0);
4885 + k++; limb >>= CHAR_BIT;
4886 + } while (limb != 0);
4888 + count = (k + (un-1) * sizeof (mp_limb_t) + size - 1) / size;
4891 + r = gmp_xalloc (count * size);
4894 + endian = gmp_detect_endian ();
4896 + p = (unsigned char *) r;
4898 + word_step = (order != endian) ? 2 * size : 0;
4900 + /* Process bytes from the least significant end, so point p at the
4901 + least significant word. */
4904 + p += size * (count - 1);
4905 + word_step = - word_step;
4908 + /* And at least significant byte of that word. */
4912 + for (bytes = 0, i = 0, k = 0; k < count; k++, p += word_step)
4915 + for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
4920 + limb = u->_mp_d[i++];
4921 + bytes = sizeof (mp_limb_t);
4924 + limb >>= CHAR_BIT;
4929 + assert (k == count);
4937 --- a/src/Makefile.am
4938 +++ b/src/Makefile.am
4939 @@ -48,4 +48,8 @@ if BUILD_CLI
4940 nft_SOURCES += cli.c
4944 +nft_SOURCES += mini-gmp.c mini-gmp-printf.c
4947 nft_LDADD = ${LIBMNL_LIBS} ${LIBNFTNL_LIBS}