1 From fd1f58387c4dc1d90c8943167ef2aa9b23367890 Mon Sep 17 00:00:00 2001
2 From: Steven Barth <cyrus@openwrt.org>
3 Date: Mon, 15 Dec 2014 14:25:59 +0100
4 Subject: [PATCH 4/5] mini-gmp: add mini-gmp source and headers from gmplib 6.0
6 Signed-off-by: Steven Barth <cyrus@openwrt.org>
8 include/mini-gmp.h | 294 ++++
9 src/mini-gmp.c | 4386 ++++++++++++++++++++++++++++++++++++++++++++++++++++
10 2 files changed, 4680 insertions(+)
11 create mode 100644 include/mini-gmp.h
12 create mode 100644 src/mini-gmp.c
15 +++ b/include/mini-gmp.h
17 +/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
19 +Copyright 2011-2014 Free Software Foundation, Inc.
21 +This file is part of the GNU MP Library.
23 +The GNU MP Library is free software; you can redistribute it and/or modify
24 +it under the terms of either:
26 + * the GNU Lesser General Public License as published by the Free
27 + Software Foundation; either version 3 of the License, or (at your
28 + option) any later version.
32 + * the GNU General Public License as published by the Free Software
33 + Foundation; either version 2 of the License, or (at your option) any
36 +or both in parallel, as here.
38 +The GNU MP Library is distributed in the hope that it will be useful, but
39 +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
40 +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
43 +You should have received copies of the GNU General Public License and the
44 +GNU Lesser General Public License along with the GNU MP Library. If not,
45 +see https://www.gnu.org/licenses/. */
47 +/* About mini-gmp: This is a minimal implementation of a subset of the
48 + GMP interface. It is intended for inclusion into applications which
49 + have modest bignums needs, as a fallback when the real GMP library
52 + This file defines the public interface. */
54 +#ifndef __MINI_GMP_H__
55 +#define __MINI_GMP_H__
60 +#if defined (__cplusplus)
64 +void mp_set_memory_functions (void *(*) (size_t),
65 + void *(*) (void *, size_t, size_t),
66 + void (*) (void *, size_t));
68 +void mp_get_memory_functions (void *(**) (size_t),
69 + void *(**) (void *, size_t, size_t),
70 + void (**) (void *, size_t));
72 +typedef unsigned long mp_limb_t;
73 +typedef long mp_size_t;
74 +typedef unsigned long mp_bitcnt_t;
76 +typedef mp_limb_t *mp_ptr;
77 +typedef const mp_limb_t *mp_srcptr;
81 + int _mp_alloc; /* Number of *limbs* allocated and pointed
82 + to by the _mp_d field. */
83 + int _mp_size; /* abs(_mp_size) is the number of limbs the
84 + last field points to. If _mp_size is
85 + negative this is a negative number. */
86 + mp_limb_t *_mp_d; /* Pointer to the limbs. */
89 +typedef __mpz_struct mpz_t[1];
91 +typedef __mpz_struct *mpz_ptr;
92 +typedef const __mpz_struct *mpz_srcptr;
94 +extern const int mp_bits_per_limb;
96 +void mpn_copyi (mp_ptr, mp_srcptr, mp_size_t);
97 +void mpn_copyd (mp_ptr, mp_srcptr, mp_size_t);
98 +void mpn_zero (mp_ptr, mp_size_t);
100 +int mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t);
102 +mp_limb_t mpn_add_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
103 +mp_limb_t mpn_add_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
104 +mp_limb_t mpn_add (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
106 +mp_limb_t mpn_sub_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
107 +mp_limb_t mpn_sub_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
108 +mp_limb_t mpn_sub (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
110 +mp_limb_t mpn_mul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
111 +mp_limb_t mpn_addmul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
112 +mp_limb_t mpn_submul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
114 +mp_limb_t mpn_mul (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
115 +void mpn_mul_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
116 +void mpn_sqr (mp_ptr, mp_srcptr, mp_size_t);
117 +int mpn_perfect_square_p (mp_srcptr, mp_size_t);
118 +mp_size_t mpn_sqrtrem (mp_ptr, mp_ptr, mp_srcptr, mp_size_t);
120 +mp_limb_t mpn_lshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
121 +mp_limb_t mpn_rshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
123 +mp_bitcnt_t mpn_scan0 (mp_srcptr, mp_bitcnt_t);
124 +mp_bitcnt_t mpn_scan1 (mp_srcptr, mp_bitcnt_t);
126 +mp_bitcnt_t mpn_popcount (mp_srcptr, mp_size_t);
128 +mp_limb_t mpn_invert_3by2 (mp_limb_t, mp_limb_t);
129 +#define mpn_invert_limb(x) mpn_invert_3by2 ((x), 0)
131 +size_t mpn_get_str (unsigned char *, int, mp_ptr, mp_size_t);
132 +mp_size_t mpn_set_str (mp_ptr, const unsigned char *, size_t, int);
134 +void mpz_init (mpz_t);
135 +void mpz_init2 (mpz_t, mp_bitcnt_t);
136 +void mpz_clear (mpz_t);
138 +#define mpz_odd_p(z) (((z)->_mp_size != 0) & (int) (z)->_mp_d[0])
139 +#define mpz_even_p(z) (! mpz_odd_p (z))
141 +int mpz_sgn (const mpz_t);
142 +int mpz_cmp_si (const mpz_t, long);
143 +int mpz_cmp_ui (const mpz_t, unsigned long);
144 +int mpz_cmp (const mpz_t, const mpz_t);
145 +int mpz_cmpabs_ui (const mpz_t, unsigned long);
146 +int mpz_cmpabs (const mpz_t, const mpz_t);
147 +int mpz_cmp_d (const mpz_t, double);
148 +int mpz_cmpabs_d (const mpz_t, double);
150 +void mpz_abs (mpz_t, const mpz_t);
151 +void mpz_neg (mpz_t, const mpz_t);
152 +void mpz_swap (mpz_t, mpz_t);
154 +void mpz_add_ui (mpz_t, const mpz_t, unsigned long);
155 +void mpz_add (mpz_t, const mpz_t, const mpz_t);
156 +void mpz_sub_ui (mpz_t, const mpz_t, unsigned long);
157 +void mpz_ui_sub (mpz_t, unsigned long, const mpz_t);
158 +void mpz_sub (mpz_t, const mpz_t, const mpz_t);
160 +void mpz_mul_si (mpz_t, const mpz_t, long int);
161 +void mpz_mul_ui (mpz_t, const mpz_t, unsigned long int);
162 +void mpz_mul (mpz_t, const mpz_t, const mpz_t);
163 +void mpz_mul_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
164 +void mpz_addmul_ui (mpz_t, const mpz_t, unsigned long int);
165 +void mpz_addmul (mpz_t, const mpz_t, const mpz_t);
166 +void mpz_submul_ui (mpz_t, const mpz_t, unsigned long int);
167 +void mpz_submul (mpz_t, const mpz_t, const mpz_t);
169 +void mpz_cdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
170 +void mpz_fdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
171 +void mpz_tdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
172 +void mpz_cdiv_q (mpz_t, const mpz_t, const mpz_t);
173 +void mpz_fdiv_q (mpz_t, const mpz_t, const mpz_t);
174 +void mpz_tdiv_q (mpz_t, const mpz_t, const mpz_t);
175 +void mpz_cdiv_r (mpz_t, const mpz_t, const mpz_t);
176 +void mpz_fdiv_r (mpz_t, const mpz_t, const mpz_t);
177 +void mpz_tdiv_r (mpz_t, const mpz_t, const mpz_t);
179 +void mpz_cdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
180 +void mpz_fdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
181 +void mpz_tdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
182 +void mpz_cdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
183 +void mpz_fdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
184 +void mpz_tdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
186 +void mpz_mod (mpz_t, const mpz_t, const mpz_t);
188 +void mpz_divexact (mpz_t, const mpz_t, const mpz_t);
190 +int mpz_divisible_p (const mpz_t, const mpz_t);
191 +int mpz_congruent_p (const mpz_t, const mpz_t, const mpz_t);
193 +unsigned long mpz_cdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
194 +unsigned long mpz_fdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
195 +unsigned long mpz_tdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
196 +unsigned long mpz_cdiv_q_ui (mpz_t, const mpz_t, unsigned long);
197 +unsigned long mpz_fdiv_q_ui (mpz_t, const mpz_t, unsigned long);
198 +unsigned long mpz_tdiv_q_ui (mpz_t, const mpz_t, unsigned long);
199 +unsigned long mpz_cdiv_r_ui (mpz_t, const mpz_t, unsigned long);
200 +unsigned long mpz_fdiv_r_ui (mpz_t, const mpz_t, unsigned long);
201 +unsigned long mpz_tdiv_r_ui (mpz_t, const mpz_t, unsigned long);
202 +unsigned long mpz_cdiv_ui (const mpz_t, unsigned long);
203 +unsigned long mpz_fdiv_ui (const mpz_t, unsigned long);
204 +unsigned long mpz_tdiv_ui (const mpz_t, unsigned long);
206 +unsigned long mpz_mod_ui (mpz_t, const mpz_t, unsigned long);
208 +void mpz_divexact_ui (mpz_t, const mpz_t, unsigned long);
210 +int mpz_divisible_ui_p (const mpz_t, unsigned long);
212 +unsigned long mpz_gcd_ui (mpz_t, const mpz_t, unsigned long);
213 +void mpz_gcd (mpz_t, const mpz_t, const mpz_t);
214 +void mpz_gcdext (mpz_t, mpz_t, mpz_t, const mpz_t, const mpz_t);
215 +void mpz_lcm_ui (mpz_t, const mpz_t, unsigned long);
216 +void mpz_lcm (mpz_t, const mpz_t, const mpz_t);
217 +int mpz_invert (mpz_t, const mpz_t, const mpz_t);
219 +void mpz_sqrtrem (mpz_t, mpz_t, const mpz_t);
220 +void mpz_sqrt (mpz_t, const mpz_t);
221 +int mpz_perfect_square_p (const mpz_t);
223 +void mpz_pow_ui (mpz_t, const mpz_t, unsigned long);
224 +void mpz_ui_pow_ui (mpz_t, unsigned long, unsigned long);
225 +void mpz_powm (mpz_t, const mpz_t, const mpz_t, const mpz_t);
226 +void mpz_powm_ui (mpz_t, const mpz_t, unsigned long, const mpz_t);
228 +void mpz_rootrem (mpz_t, mpz_t, const mpz_t, unsigned long);
229 +int mpz_root (mpz_t, const mpz_t, unsigned long);
231 +void mpz_fac_ui (mpz_t, unsigned long);
232 +void mpz_bin_uiui (mpz_t, unsigned long, unsigned long);
234 +int mpz_probab_prime_p (const mpz_t, int);
236 +int mpz_tstbit (const mpz_t, mp_bitcnt_t);
237 +void mpz_setbit (mpz_t, mp_bitcnt_t);
238 +void mpz_clrbit (mpz_t, mp_bitcnt_t);
239 +void mpz_combit (mpz_t, mp_bitcnt_t);
241 +void mpz_com (mpz_t, const mpz_t);
242 +void mpz_and (mpz_t, const mpz_t, const mpz_t);
243 +void mpz_ior (mpz_t, const mpz_t, const mpz_t);
244 +void mpz_xor (mpz_t, const mpz_t, const mpz_t);
246 +mp_bitcnt_t mpz_popcount (const mpz_t);
247 +mp_bitcnt_t mpz_hamdist (const mpz_t, const mpz_t);
248 +mp_bitcnt_t mpz_scan0 (const mpz_t, mp_bitcnt_t);
249 +mp_bitcnt_t mpz_scan1 (const mpz_t, mp_bitcnt_t);
251 +int mpz_fits_slong_p (const mpz_t);
252 +int mpz_fits_ulong_p (const mpz_t);
253 +long int mpz_get_si (const mpz_t);
254 +unsigned long int mpz_get_ui (const mpz_t);
255 +double mpz_get_d (const mpz_t);
256 +size_t mpz_size (const mpz_t);
257 +mp_limb_t mpz_getlimbn (const mpz_t, mp_size_t);
259 +void mpz_realloc2 (mpz_t, mp_bitcnt_t);
260 +mp_srcptr mpz_limbs_read (mpz_srcptr);
261 +mp_ptr mpz_limbs_modify (mpz_t, mp_size_t);
262 +mp_ptr mpz_limbs_write (mpz_t, mp_size_t);
263 +void mpz_limbs_finish (mpz_t, mp_size_t);
264 +mpz_srcptr mpz_roinit_n (mpz_t, mp_srcptr, mp_size_t);
266 +#define MPZ_ROINIT_N(xp, xs) {{0, (xs),(xp) }}
268 +void mpz_set_si (mpz_t, signed long int);
269 +void mpz_set_ui (mpz_t, unsigned long int);
270 +void mpz_set (mpz_t, const mpz_t);
271 +void mpz_set_d (mpz_t, double);
273 +void mpz_init_set_si (mpz_t, signed long int);
274 +void mpz_init_set_ui (mpz_t, unsigned long int);
275 +void mpz_init_set (mpz_t, const mpz_t);
276 +void mpz_init_set_d (mpz_t, double);
278 +size_t mpz_sizeinbase (const mpz_t, int);
279 +char *mpz_get_str (char *, int, const mpz_t);
280 +int mpz_set_str (mpz_t, const char *, int);
281 +int mpz_init_set_str (mpz_t, const char *, int);
283 +/* This long list taken from gmp.h. */
284 +/* For reference, "defined(EOF)" cannot be used here. In g++ 2.95.4,
285 + <iostream> defines EOF but not FILE. */
286 +#if defined (FILE) \
287 + || defined (H_STDIO) \
288 + || defined (_H_STDIO) /* AIX */ \
289 + || defined (_STDIO_H) /* glibc, Sun, SCO */ \
290 + || defined (_STDIO_H_) /* BSD, OSF */ \
291 + || defined (__STDIO_H) /* Borland */ \
292 + || defined (__STDIO_H__) /* IRIX */ \
293 + || defined (_STDIO_INCLUDED) /* HPUX */ \
294 + || defined (__dj_include_stdio_h_) /* DJGPP */ \
295 + || defined (_FILE_DEFINED) /* Microsoft */ \
296 + || defined (__STDIO__) /* Apple MPW MrC */ \
297 + || defined (_MSL_STDIO_H) /* Metrowerks */ \
298 + || defined (_STDIO_H_INCLUDED) /* QNX4 */ \
299 + || defined (_ISO_STDIO_ISO_H) /* Sun C++ */ \
300 + || defined (__STDIO_LOADED) /* VMS */
301 +size_t mpz_out_str (FILE *, int, const mpz_t);
304 +void mpz_import (mpz_t, size_t, int, size_t, int, size_t, const void *);
305 +void *mpz_export (void *, size_t *, int, size_t, int, size_t, const mpz_t);
307 +#if defined (__cplusplus)
310 +#endif /* __MINI_GMP_H__ */
314 +/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
316 + Contributed to the GNU project by Niels Möller
318 +Copyright 1991-1997, 1999-2014 Free Software Foundation, Inc.
320 +This file is part of the GNU MP Library.
322 +The GNU MP Library is free software; you can redistribute it and/or modify
323 +it under the terms of either:
325 + * the GNU Lesser General Public License as published by the Free
326 + Software Foundation; either version 3 of the License, or (at your
327 + option) any later version.
331 + * the GNU General Public License as published by the Free Software
332 + Foundation; either version 2 of the License, or (at your option) any
335 +or both in parallel, as here.
337 +The GNU MP Library is distributed in the hope that it will be useful, but
338 +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
339 +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
342 +You should have received copies of the GNU General Public License and the
343 +GNU Lesser General Public License along with the GNU MP Library. If not,
344 +see https://www.gnu.org/licenses/. */
346 +/* NOTE: All functions in this file which are not declared in
347 + mini-gmp.h are internal, and are not intended to be compatible
348 + neither with GMP nor with future versions of mini-gmp. */
350 +/* Much of the material copied from GMP files, including: gmp-impl.h,
351 + longlong.h, mpn/generic/add_n.c, mpn/generic/addmul_1.c,
352 + mpn/generic/lshift.c, mpn/generic/mul_1.c,
353 + mpn/generic/mul_basecase.c, mpn/generic/rshift.c,
354 + mpn/generic/sbpi1_div_qr.c, mpn/generic/sub_n.c,
355 + mpn/generic/submul_1.c. */
364 +#include "mini-gmp.h"
368 +#define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
370 +#define GMP_LIMB_MAX (~ (mp_limb_t) 0)
371 +#define GMP_LIMB_HIGHBIT ((mp_limb_t) 1 << (GMP_LIMB_BITS - 1))
373 +#define GMP_HLIMB_BIT ((mp_limb_t) 1 << (GMP_LIMB_BITS / 2))
374 +#define GMP_LLIMB_MASK (GMP_HLIMB_BIT - 1)
376 +#define GMP_ULONG_BITS (sizeof(unsigned long) * CHAR_BIT)
377 +#define GMP_ULONG_HIGHBIT ((unsigned long) 1 << (GMP_ULONG_BITS - 1))
379 +#define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
380 +#define GMP_NEG_CAST(T,x) (-((T)((x) + 1) - 1))
382 +#define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
383 +#define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
385 +#define gmp_assert_nocarry(x) do { \
386 + mp_limb_t __cy = x; \
387 + assert (__cy == 0); \
390 +#define gmp_clz(count, x) do { \
391 + mp_limb_t __clz_x = (x); \
392 + unsigned __clz_c; \
393 + for (__clz_c = 0; \
394 + (__clz_x & ((mp_limb_t) 0xff << (GMP_LIMB_BITS - 8))) == 0; \
397 + for (; (__clz_x & GMP_LIMB_HIGHBIT) == 0; __clz_c++) \
399 + (count) = __clz_c; \
402 +#define gmp_ctz(count, x) do { \
403 + mp_limb_t __ctz_x = (x); \
404 + unsigned __ctz_c = 0; \
405 + gmp_clz (__ctz_c, __ctz_x & - __ctz_x); \
406 + (count) = GMP_LIMB_BITS - 1 - __ctz_c; \
409 +#define gmp_add_ssaaaa(sh, sl, ah, al, bh, bl) \
412 + __x = (al) + (bl); \
413 + (sh) = (ah) + (bh) + (__x < (al)); \
417 +#define gmp_sub_ddmmss(sh, sl, ah, al, bh, bl) \
420 + __x = (al) - (bl); \
421 + (sh) = (ah) - (bh) - ((al) < (bl)); \
425 +#define gmp_umul_ppmm(w1, w0, u, v) \
427 + mp_limb_t __x0, __x1, __x2, __x3; \
428 + unsigned __ul, __vl, __uh, __vh; \
429 + mp_limb_t __u = (u), __v = (v); \
431 + __ul = __u & GMP_LLIMB_MASK; \
432 + __uh = __u >> (GMP_LIMB_BITS / 2); \
433 + __vl = __v & GMP_LLIMB_MASK; \
434 + __vh = __v >> (GMP_LIMB_BITS / 2); \
436 + __x0 = (mp_limb_t) __ul * __vl; \
437 + __x1 = (mp_limb_t) __ul * __vh; \
438 + __x2 = (mp_limb_t) __uh * __vl; \
439 + __x3 = (mp_limb_t) __uh * __vh; \
441 + __x1 += __x0 >> (GMP_LIMB_BITS / 2);/* this can't give carry */ \
442 + __x1 += __x2; /* but this indeed can */ \
443 + if (__x1 < __x2) /* did we get it? */ \
444 + __x3 += GMP_HLIMB_BIT; /* yes, add it in the proper pos. */ \
446 + (w1) = __x3 + (__x1 >> (GMP_LIMB_BITS / 2)); \
447 + (w0) = (__x1 << (GMP_LIMB_BITS / 2)) + (__x0 & GMP_LLIMB_MASK); \
450 +#define gmp_udiv_qrnnd_preinv(q, r, nh, nl, d, di) \
452 + mp_limb_t _qh, _ql, _r, _mask; \
453 + gmp_umul_ppmm (_qh, _ql, (nh), (di)); \
454 + gmp_add_ssaaaa (_qh, _ql, _qh, _ql, (nh) + 1, (nl)); \
455 + _r = (nl) - _qh * (d); \
456 + _mask = -(mp_limb_t) (_r > _ql); /* both > and >= are OK */ \
458 + _r += _mask & (d); \
469 +#define gmp_udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv) \
471 + mp_limb_t _q0, _t1, _t0, _mask; \
472 + gmp_umul_ppmm ((q), _q0, (n2), (dinv)); \
473 + gmp_add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1)); \
475 + /* Compute the two most significant limbs of n - q'd */ \
476 + (r1) = (n1) - (d1) * (q); \
477 + gmp_sub_ddmmss ((r1), (r0), (r1), (n0), (d1), (d0)); \
478 + gmp_umul_ppmm (_t1, _t0, (d0), (q)); \
479 + gmp_sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0); \
482 + /* Conditionally adjust q and the remainders */ \
483 + _mask = - (mp_limb_t) ((r1) >= _q0); \
485 + gmp_add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0)); \
486 + if ((r1) >= (d1)) \
488 + if ((r1) > (d1) || (r0) >= (d0)) \
491 + gmp_sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0)); \
497 +#define MP_LIMB_T_SWAP(x, y) \
499 + mp_limb_t __mp_limb_t_swap__tmp = (x); \
501 + (y) = __mp_limb_t_swap__tmp; \
503 +#define MP_SIZE_T_SWAP(x, y) \
505 + mp_size_t __mp_size_t_swap__tmp = (x); \
507 + (y) = __mp_size_t_swap__tmp; \
509 +#define MP_BITCNT_T_SWAP(x,y) \
511 + mp_bitcnt_t __mp_bitcnt_t_swap__tmp = (x); \
513 + (y) = __mp_bitcnt_t_swap__tmp; \
515 +#define MP_PTR_SWAP(x, y) \
517 + mp_ptr __mp_ptr_swap__tmp = (x); \
519 + (y) = __mp_ptr_swap__tmp; \
521 +#define MP_SRCPTR_SWAP(x, y) \
523 + mp_srcptr __mp_srcptr_swap__tmp = (x); \
525 + (y) = __mp_srcptr_swap__tmp; \
528 +#define MPN_PTR_SWAP(xp,xs, yp,ys) \
530 + MP_PTR_SWAP (xp, yp); \
531 + MP_SIZE_T_SWAP (xs, ys); \
533 +#define MPN_SRCPTR_SWAP(xp,xs, yp,ys) \
535 + MP_SRCPTR_SWAP (xp, yp); \
536 + MP_SIZE_T_SWAP (xs, ys); \
539 +#define MPZ_PTR_SWAP(x, y) \
541 + mpz_ptr __mpz_ptr_swap__tmp = (x); \
543 + (y) = __mpz_ptr_swap__tmp; \
545 +#define MPZ_SRCPTR_SWAP(x, y) \
547 + mpz_srcptr __mpz_srcptr_swap__tmp = (x); \
549 + (y) = __mpz_srcptr_swap__tmp; \
552 +const int mp_bits_per_limb = GMP_LIMB_BITS;
555 +/* Memory allocation and other helper functions. */
557 +gmp_die (const char *msg)
559 + fprintf (stderr, "%s\n", msg);
564 +gmp_default_alloc (size_t size)
572 + gmp_die("gmp_default_alloc: Virtual memory exhausted.");
578 +gmp_default_realloc (void *old, size_t old_size, size_t new_size)
582 + p = realloc (old, new_size);
585 + gmp_die("gmp_default_realoc: Virtual memory exhausted.");
591 +gmp_default_free (void *p, size_t size)
596 +static void * (*gmp_allocate_func) (size_t) = gmp_default_alloc;
597 +static void * (*gmp_reallocate_func) (void *, size_t, size_t) = gmp_default_realloc;
598 +static void (*gmp_free_func) (void *, size_t) = gmp_default_free;
601 +mp_get_memory_functions (void *(**alloc_func) (size_t),
602 + void *(**realloc_func) (void *, size_t, size_t),
603 + void (**free_func) (void *, size_t))
606 + *alloc_func = gmp_allocate_func;
609 + *realloc_func = gmp_reallocate_func;
612 + *free_func = gmp_free_func;
616 +mp_set_memory_functions (void *(*alloc_func) (size_t),
617 + void *(*realloc_func) (void *, size_t, size_t),
618 + void (*free_func) (void *, size_t))
621 + alloc_func = gmp_default_alloc;
623 + realloc_func = gmp_default_realloc;
625 + free_func = gmp_default_free;
627 + gmp_allocate_func = alloc_func;
628 + gmp_reallocate_func = realloc_func;
629 + gmp_free_func = free_func;
632 +#define gmp_xalloc(size) ((*gmp_allocate_func)((size)))
633 +#define gmp_free(p) ((*gmp_free_func) ((p), 0))
636 +gmp_xalloc_limbs (mp_size_t size)
638 + return gmp_xalloc (size * sizeof (mp_limb_t));
642 +gmp_xrealloc_limbs (mp_ptr old, mp_size_t size)
645 + return (*gmp_reallocate_func) (old, 0, size * sizeof (mp_limb_t));
652 +mpn_copyi (mp_ptr d, mp_srcptr s, mp_size_t n)
655 + for (i = 0; i < n; i++)
660 +mpn_copyd (mp_ptr d, mp_srcptr s, mp_size_t n)
667 +mpn_cmp (mp_srcptr ap, mp_srcptr bp, mp_size_t n)
671 + if (ap[n] != bp[n])
672 + return ap[n] > bp[n] ? 1 : -1;
678 +mpn_cmp4 (mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
681 + return an < bn ? -1 : 1;
683 + return mpn_cmp (ap, bp, an);
687 +mpn_normalized_size (mp_srcptr xp, mp_size_t n)
689 + for (; n > 0 && xp[n-1] == 0; n--)
694 +#define mpn_zero_p(xp, n) (mpn_normalized_size ((xp), (n)) == 0)
697 +mpn_zero (mp_ptr rp, mp_size_t n)
701 + for (i = 0; i < n; i++)
706 +mpn_add_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
714 + mp_limb_t r = ap[i] + b;
725 +mpn_add_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
730 + for (i = 0, cy = 0; i < n; i++)
733 + a = ap[i]; b = bp[i];
744 +mpn_add (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
750 + cy = mpn_add_n (rp, ap, bp, bn);
752 + cy = mpn_add_1 (rp + bn, ap + bn, an - bn, cy);
757 +mpn_sub_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
766 + mp_limb_t a = ap[i];
768 + mp_limb_t cy = a < b;;
778 +mpn_sub_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
783 + for (i = 0, cy = 0; i < n; i++)
786 + a = ap[i]; b = bp[i];
796 +mpn_sub (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
802 + cy = mpn_sub_n (rp, ap, bp, bn);
804 + cy = mpn_sub_1 (rp + bn, ap + bn, an - bn, cy);
809 +mpn_mul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
811 + mp_limb_t ul, cl, hpl, lpl;
819 + gmp_umul_ppmm (hpl, lpl, ul, vl);
822 + cl = (lpl < cl) + hpl;
832 +mpn_addmul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
834 + mp_limb_t ul, cl, hpl, lpl, rl;
842 + gmp_umul_ppmm (hpl, lpl, ul, vl);
845 + cl = (lpl < cl) + hpl;
858 +mpn_submul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
860 + mp_limb_t ul, cl, hpl, lpl, rl;
868 + gmp_umul_ppmm (hpl, lpl, ul, vl);
871 + cl = (lpl < cl) + hpl;
884 +mpn_mul (mp_ptr rp, mp_srcptr up, mp_size_t un, mp_srcptr vp, mp_size_t vn)
889 + /* We first multiply by the low order limb. This result can be
890 + stored, not added, to rp. We also avoid a loop for zeroing this
893 + rp[un] = mpn_mul_1 (rp, up, un, vp[0]);
894 + rp += 1, vp += 1, vn -= 1;
896 + /* Now accumulate the product of up[] and the next higher limb from
901 + rp[un] = mpn_addmul_1 (rp, up, un, vp[0]);
902 + rp += 1, vp += 1, vn -= 1;
908 +mpn_mul_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
910 + mpn_mul (rp, ap, n, bp, n);
914 +mpn_sqr (mp_ptr rp, mp_srcptr ap, mp_size_t n)
916 + mpn_mul (rp, ap, n, ap, n);
920 +mpn_lshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
922 + mp_limb_t high_limb, low_limb;
929 + assert (cnt < GMP_LIMB_BITS);
934 + tnc = GMP_LIMB_BITS - cnt;
936 + retval = low_limb >> tnc;
937 + high_limb = (low_limb << cnt);
939 + for (i = n; --i != 0;)
942 + *--rp = high_limb | (low_limb >> tnc);
943 + high_limb = (low_limb << cnt);
951 +mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
953 + mp_limb_t high_limb, low_limb;
960 + assert (cnt < GMP_LIMB_BITS);
962 + tnc = GMP_LIMB_BITS - cnt;
964 + retval = (high_limb << tnc);
965 + low_limb = high_limb >> cnt;
967 + for (i = n; --i != 0;)
970 + *rp++ = low_limb | (high_limb << tnc);
971 + low_limb = high_limb >> cnt;
979 +mpn_common_scan (mp_limb_t limb, mp_size_t i, mp_srcptr up, mp_size_t un,
984 + assert (ux == 0 || ux == GMP_LIMB_MAX);
985 + assert (0 <= i && i <= un );
991 + return (ux == 0 ? ~(mp_bitcnt_t) 0 : un * GMP_LIMB_BITS);
994 + gmp_ctz (cnt, limb);
995 + return (mp_bitcnt_t) i * GMP_LIMB_BITS + cnt;
999 +mpn_scan1 (mp_srcptr ptr, mp_bitcnt_t bit)
1002 + i = bit / GMP_LIMB_BITS;
1004 + return mpn_common_scan ( ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
1009 +mpn_scan0 (mp_srcptr ptr, mp_bitcnt_t bit)
1012 + i = bit / GMP_LIMB_BITS;
1014 + return mpn_common_scan (~ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
1015 + i, ptr, i, GMP_LIMB_MAX);
1019 +/* MPN division interface. */
1021 +mpn_invert_3by2 (mp_limb_t u1, mp_limb_t u0)
1023 + mp_limb_t r, p, m;
1027 + /* First, do a 2/1 inverse. */
1028 + /* The inverse m is defined as floor( (B^2 - 1 - u1)/u1 ), so that 0 <
1029 + * B^2 - (B + m) u1 <= u1 */
1030 + assert (u1 >= GMP_LIMB_HIGHBIT);
1032 + ul = u1 & GMP_LLIMB_MASK;
1033 + uh = u1 >> (GMP_LIMB_BITS / 2);
1036 + r = ((~u1 - (mp_limb_t) qh * uh) << (GMP_LIMB_BITS / 2)) | GMP_LLIMB_MASK;
1038 + p = (mp_limb_t) qh * ul;
1039 + /* Adjustment steps taken from udiv_qrnnd_c */
1044 + if (r >= u1) /* i.e. we didn't get carry when adding to r */
1053 + /* Do a 3/2 division (with half limb size) */
1054 + p = (r >> (GMP_LIMB_BITS / 2)) * qh + r;
1055 + ql = (p >> (GMP_LIMB_BITS / 2)) + 1;
1057 + /* By the 3/2 method, we don't need the high half limb. */
1058 + r = (r << (GMP_LIMB_BITS / 2)) + GMP_LLIMB_MASK - ql * u1;
1060 + if (r >= (p << (GMP_LIMB_BITS / 2)))
1065 + m = ((mp_limb_t) qh << (GMP_LIMB_BITS / 2)) + ql;
1087 + gmp_umul_ppmm (th, tl, u0, m);
1092 + m -= ((r > u1) | ((r == u1) & (tl > u0)));
1099 +struct gmp_div_inverse
1101 + /* Normalization shift count. */
1103 + /* Normalized divisor (d0 unused for mpn_div_qr_1) */
1105 + /* Inverse, for 2/1 or 3/2. */
1110 +mpn_div_qr_1_invert (struct gmp_div_inverse *inv, mp_limb_t d)
1115 + gmp_clz (shift, d);
1116 + inv->shift = shift;
1117 + inv->d1 = d << shift;
1118 + inv->di = mpn_invert_limb (inv->d1);
1122 +mpn_div_qr_2_invert (struct gmp_div_inverse *inv,
1123 + mp_limb_t d1, mp_limb_t d0)
1128 + gmp_clz (shift, d1);
1129 + inv->shift = shift;
1132 + d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
1137 + inv->di = mpn_invert_3by2 (d1, d0);
1141 +mpn_div_qr_invert (struct gmp_div_inverse *inv,
1142 + mp_srcptr dp, mp_size_t dn)
1147 + mpn_div_qr_1_invert (inv, dp[0]);
1149 + mpn_div_qr_2_invert (inv, dp[1], dp[0]);
1158 + gmp_clz (shift, d1);
1159 + inv->shift = shift;
1162 + d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
1163 + d0 = (d0 << shift) | (dp[dn-3] >> (GMP_LIMB_BITS - shift));
1167 + inv->di = mpn_invert_3by2 (d1, d0);
1171 +/* Not matching current public gmp interface, rather corresponding to
1172 + the sbpi1_div_* functions. */
1174 +mpn_div_qr_1_preinv (mp_ptr qp, mp_srcptr np, mp_size_t nn,
1175 + const struct gmp_div_inverse *inv)
1181 + if (inv->shift > 0)
1183 + tp = gmp_xalloc_limbs (nn);
1184 + r = mpn_lshift (tp, np, nn, inv->shift);
1196 + gmp_udiv_qrnnd_preinv (q, r, r, np[nn], d, di);
1200 + if (inv->shift > 0)
1203 + return r >> inv->shift;
1207 +mpn_div_qr_1 (mp_ptr qp, mp_srcptr np, mp_size_t nn, mp_limb_t d)
1211 + /* Special case for powers of two. */
1212 + if ((d & (d-1)) == 0)
1214 + mp_limb_t r = np[0] & (d-1);
1218 + mpn_copyi (qp, np, nn);
1222 + gmp_ctz (shift, d);
1223 + mpn_rshift (qp, np, nn, shift);
1230 + struct gmp_div_inverse inv;
1231 + mpn_div_qr_1_invert (&inv, d);
1232 + return mpn_div_qr_1_preinv (qp, np, nn, &inv);
1237 +mpn_div_qr_2_preinv (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
1238 + const struct gmp_div_inverse *inv)
1242 + mp_limb_t d1, d0, di, r1, r0;
1246 + shift = inv->shift;
1253 + tp = gmp_xalloc_limbs (nn);
1254 + r1 = mpn_lshift (tp, np, nn, shift);
1267 + gmp_udiv_qr_3by2 (q, r1, r0, r1, r0, n0, d1, d0, di);
1276 + assert ((r0 << (GMP_LIMB_BITS - shift)) == 0);
1277 + r0 = (r0 >> shift) | (r1 << (GMP_LIMB_BITS - shift));
1289 +mpn_div_qr_2 (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
1290 + mp_limb_t d1, mp_limb_t d0)
1292 + struct gmp_div_inverse inv;
1295 + mpn_div_qr_2_invert (&inv, d1, d0);
1296 + mpn_div_qr_2_preinv (qp, rp, np, nn, &inv);
1301 +mpn_div_qr_pi1 (mp_ptr qp,
1302 + mp_ptr np, mp_size_t nn, mp_limb_t n1,
1303 + mp_srcptr dp, mp_size_t dn,
1309 + mp_limb_t cy, cy1;
1313 + assert (nn >= dn);
1318 + assert ((d1 & GMP_LIMB_HIGHBIT) != 0);
1319 + /* Iteration variable is the index of the q limb.
1321 + * We divide <n1, np[dn-1+i], np[dn-2+i], np[dn-3+i],..., np[i]>
1322 + * by <d1, d0, dp[dn-3], ..., dp[0] >
1328 + mp_limb_t n0 = np[dn-1+i];
1330 + if (n1 == d1 && n0 == d0)
1333 + mpn_submul_1 (np+i, dp, dn, q);
1334 + n1 = np[dn-1+i]; /* update n1, last loop's value will now be invalid */
1338 + gmp_udiv_qr_3by2 (q, n1, n0, n1, n0, np[dn-2+i], d1, d0, dinv);
1340 + cy = mpn_submul_1 (np + i, dp, dn-2, q);
1350 + n1 += d1 + mpn_add_n (np + i, np + i, dp, dn - 1);
1364 +mpn_div_qr_preinv (mp_ptr qp, mp_ptr np, mp_size_t nn,
1365 + mp_srcptr dp, mp_size_t dn,
1366 + const struct gmp_div_inverse *inv)
1369 + assert (nn >= dn);
1372 + np[0] = mpn_div_qr_1_preinv (qp, np, nn, inv);
1374 + mpn_div_qr_2_preinv (qp, np, np, nn, inv);
1380 + assert (inv->d1 == dp[dn-1]);
1381 + assert (inv->d0 == dp[dn-2]);
1382 + assert ((inv->d1 & GMP_LIMB_HIGHBIT) != 0);
1384 + shift = inv->shift;
1386 + nh = mpn_lshift (np, np, nn, shift);
1390 + mpn_div_qr_pi1 (qp, np, nn, nh, dp, dn, inv->di);
1393 + gmp_assert_nocarry (mpn_rshift (np, np, dn, shift));
1398 +mpn_div_qr (mp_ptr qp, mp_ptr np, mp_size_t nn, mp_srcptr dp, mp_size_t dn)
1400 + struct gmp_div_inverse inv;
1404 + assert (nn >= dn);
1406 + mpn_div_qr_invert (&inv, dp, dn);
1407 + if (dn > 2 && inv.shift > 0)
1409 + tp = gmp_xalloc_limbs (dn);
1410 + gmp_assert_nocarry (mpn_lshift (tp, dp, dn, inv.shift));
1413 + mpn_div_qr_preinv (qp, np, nn, dp, dn, &inv);
1419 +/* MPN base conversion. */
1421 +mpn_base_power_of_two_p (unsigned b)
1428 + case 16: return 4;
1429 + case 32: return 5;
1430 + case 64: return 6;
1431 + case 128: return 7;
1432 + case 256: return 8;
1433 + default: return 0;
1437 +struct mpn_base_info
1439 + /* bb is the largest power of the base which fits in one limb, and
1440 + exp is the corresponding exponent. */
1446 +mpn_get_base_info (struct mpn_base_info *info, mp_limb_t b)
1452 + m = GMP_LIMB_MAX / b;
1453 + for (exp = 1, p = b; p <= m; exp++)
1461 +mpn_limb_size_in_base_2 (mp_limb_t u)
1466 + gmp_clz (shift, u);
1467 + return GMP_LIMB_BITS - shift;
1471 +mpn_get_str_bits (unsigned char *sp, unsigned bits, mp_srcptr up, mp_size_t un)
1473 + unsigned char mask;
1478 + sn = ((un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1])
1479 + + bits - 1) / bits;
1481 + mask = (1U << bits) - 1;
1483 + for (i = 0, j = sn, shift = 0; j-- > 0;)
1485 + unsigned char digit = up[i] >> shift;
1489 + if (shift >= GMP_LIMB_BITS && ++i < un)
1491 + shift -= GMP_LIMB_BITS;
1492 + digit |= up[i] << (bits - shift);
1494 + sp[j] = digit & mask;
1499 +/* We generate digits from the least significant end, and reverse at
1502 +mpn_limb_get_str (unsigned char *sp, mp_limb_t w,
1503 + const struct gmp_div_inverse *binv)
1506 + for (i = 0; w > 0; i++)
1508 + mp_limb_t h, l, r;
1510 + h = w >> (GMP_LIMB_BITS - binv->shift);
1511 + l = w << binv->shift;
1513 + gmp_udiv_qrnnd_preinv (w, r, h, l, binv->d1, binv->di);
1514 + assert ( (r << (GMP_LIMB_BITS - binv->shift)) == 0);
1515 + r >>= binv->shift;
1523 +mpn_get_str_other (unsigned char *sp,
1524 + int base, const struct mpn_base_info *info,
1525 + mp_ptr up, mp_size_t un)
1527 + struct gmp_div_inverse binv;
1531 + mpn_div_qr_1_invert (&binv, base);
1537 + struct gmp_div_inverse bbinv;
1538 + mpn_div_qr_1_invert (&bbinv, info->bb);
1544 + w = mpn_div_qr_1_preinv (up, up, un, &bbinv);
1545 + un -= (up[un-1] == 0);
1546 + done = mpn_limb_get_str (sp + sn, w, &binv);
1548 + for (sn += done; done < info->exp; done++)
1553 + sn += mpn_limb_get_str (sp + sn, up[0], &binv);
1555 + /* Reverse order */
1556 + for (i = 0; 2*i + 1 < sn; i++)
1558 + unsigned char t = sp[i];
1559 + sp[i] = sp[sn - i - 1];
1560 + sp[sn - i - 1] = t;
1567 +mpn_get_str (unsigned char *sp, int base, mp_ptr up, mp_size_t un)
1572 + assert (up[un-1] > 0);
1574 + bits = mpn_base_power_of_two_p (base);
1576 + return mpn_get_str_bits (sp, bits, up, un);
1579 + struct mpn_base_info info;
1581 + mpn_get_base_info (&info, base);
1582 + return mpn_get_str_other (sp, base, &info, up, un);
1587 +mpn_set_str_bits (mp_ptr rp, const unsigned char *sp, size_t sn,
1594 + for (j = sn, rn = 0, shift = 0; j-- > 0; )
1603 + rp[rn-1] |= (mp_limb_t) sp[j] << shift;
1605 + if (shift >= GMP_LIMB_BITS)
1607 + shift -= GMP_LIMB_BITS;
1609 + rp[rn++] = (mp_limb_t) sp[j] >> (bits - shift);
1613 + rn = mpn_normalized_size (rp, rn);
1618 +mpn_set_str_other (mp_ptr rp, const unsigned char *sp, size_t sn,
1619 + mp_limb_t b, const struct mpn_base_info *info)
1626 + k = 1 + (sn - 1) % info->exp;
1631 + w = w * b + sp[j++];
1635 + for (rn = (w > 0); j < sn;)
1640 + for (k = 1; k < info->exp; k++)
1641 + w = w * b + sp[j++];
1643 + cy = mpn_mul_1 (rp, rp, rn, info->bb);
1644 + cy += mpn_add_1 (rp, rp, rn, w);
1654 +mpn_set_str (mp_ptr rp, const unsigned char *sp, size_t sn, int base)
1661 + bits = mpn_base_power_of_two_p (base);
1663 + return mpn_set_str_bits (rp, sp, sn, bits);
1666 + struct mpn_base_info info;
1668 + mpn_get_base_info (&info, base);
1669 + return mpn_set_str_other (rp, sp, sn, base, &info);
1674 +/* MPZ interface */
1680 + r->_mp_d = gmp_xalloc_limbs (1);
1683 +/* The utility of this function is a bit limited, since many functions
1684 + assigns the result variable using mpz_swap. */
1686 +mpz_init2 (mpz_t r, mp_bitcnt_t bits)
1690 + bits -= (bits != 0); /* Round down, except if 0 */
1691 + rn = 1 + bits / GMP_LIMB_BITS;
1693 + r->_mp_alloc = rn;
1695 + r->_mp_d = gmp_xalloc_limbs (rn);
1699 +mpz_clear (mpz_t r)
1701 + gmp_free (r->_mp_d);
1705 +mpz_realloc (mpz_t r, mp_size_t size)
1707 + size = GMP_MAX (size, 1);
1709 + r->_mp_d = gmp_xrealloc_limbs (r->_mp_d, size);
1710 + r->_mp_alloc = size;
1712 + if (GMP_ABS (r->_mp_size) > size)
1718 +/* Realloc for an mpz_t WHAT if it has less than NEEDED limbs. */
1719 +#define MPZ_REALLOC(z,n) ((n) > (z)->_mp_alloc \
1720 + ? mpz_realloc(z,n) \
1723 +/* MPZ assignment and basic conversions. */
1725 +mpz_set_si (mpz_t r, signed long int x)
1728 + mpz_set_ui (r, x);
1729 + else /* (x < 0) */
1732 + r->_mp_d[0] = GMP_NEG_CAST (unsigned long int, x);
1737 +mpz_set_ui (mpz_t r, unsigned long int x)
1749 +mpz_set (mpz_t r, const mpz_t x)
1751 + /* Allow the NOP r == x */
1757 + n = GMP_ABS (x->_mp_size);
1758 + rp = MPZ_REALLOC (r, n);
1760 + mpn_copyi (rp, x->_mp_d, n);
1761 + r->_mp_size = x->_mp_size;
1766 +mpz_init_set_si (mpz_t r, signed long int x)
1769 + mpz_set_si (r, x);
1773 +mpz_init_set_ui (mpz_t r, unsigned long int x)
1776 + mpz_set_ui (r, x);
1780 +mpz_init_set (mpz_t r, const mpz_t x)
1787 +mpz_fits_slong_p (const mpz_t u)
1789 + mp_size_t us = u->_mp_size;
1794 + return u->_mp_d[0] < GMP_LIMB_HIGHBIT;
1795 + else if (us == -1)
1796 + return u->_mp_d[0] <= GMP_LIMB_HIGHBIT;
1802 +mpz_fits_ulong_p (const mpz_t u)
1804 + mp_size_t us = u->_mp_size;
1806 + return (us == (us > 0));
1810 +mpz_get_si (const mpz_t u)
1812 + mp_size_t us = u->_mp_size;
1815 + return (long) (u->_mp_d[0] & ~GMP_LIMB_HIGHBIT);
1817 + return (long) (- u->_mp_d[0] | GMP_LIMB_HIGHBIT);
1823 +mpz_get_ui (const mpz_t u)
1825 + return u->_mp_size == 0 ? 0 : u->_mp_d[0];
1829 +mpz_size (const mpz_t u)
1831 + return GMP_ABS (u->_mp_size);
1835 +mpz_getlimbn (const mpz_t u, mp_size_t n)
1837 + if (n >= 0 && n < GMP_ABS (u->_mp_size))
1838 + return u->_mp_d[n];
1844 +mpz_realloc2 (mpz_t x, mp_bitcnt_t n)
1846 + mpz_realloc (x, 1 + (n - (n != 0)) / GMP_LIMB_BITS);
1850 +mpz_limbs_read (mpz_srcptr x)
1856 +mpz_limbs_modify (mpz_t x, mp_size_t n)
1859 + return MPZ_REALLOC (x, n);
1863 +mpz_limbs_write (mpz_t x, mp_size_t n)
1865 + return mpz_limbs_modify (x, n);
1869 +mpz_limbs_finish (mpz_t x, mp_size_t xs)
1872 + xn = mpn_normalized_size (x->_mp_d, GMP_ABS (xs));
1873 + x->_mp_size = xs < 0 ? -xn : xn;
1877 +mpz_roinit_n (mpz_t x, mp_srcptr xp, mp_size_t xs)
1880 + x->_mp_d = (mp_ptr) xp;
1881 + mpz_limbs_finish (x, xs);
1886 +/* Conversions and comparison to double. */
1888 +mpz_set_d (mpz_t r, double x)
1897 + /* x != x is true when x is a NaN, and x == x * 0.5 is true when x is
1898 + zero or infinity. */
1899 + if (x != x || x == x * 0.5)
1914 + B = 2.0 * (double) GMP_LIMB_HIGHBIT;
1916 + for (rn = 1; x >= B; rn++)
1919 + rp = MPZ_REALLOC (r, rn);
1921 + f = (mp_limb_t) x;
1929 + f = (mp_limb_t) x;
1935 + r->_mp_size = sign ? - rn : rn;
1939 +mpz_init_set_d (mpz_t r, double x)
1946 +mpz_get_d (const mpz_t u)
1950 + double B = 2.0 * (double) GMP_LIMB_HIGHBIT;
1952 + un = GMP_ABS (u->_mp_size);
1957 + x = u->_mp_d[--un];
1959 + x = B*x + u->_mp_d[--un];
1961 + if (u->_mp_size < 0)
1968 +mpz_cmpabs_d (const mpz_t x, double d)
1979 + xn = GMP_ABS (xn);
1981 + B = 2.0 * (double) GMP_LIMB_HIGHBIT;
1984 + /* Scale d so it can be compared with the top limb. */
1985 + for (i = 1; i < xn; i++)
1991 + /* Compare floor(d) to top limb, subtract and cancel when equal. */
1992 + for (i = xn; i-- > 0;)
1996 + f = (mp_limb_t) d;
2005 + return - (d > 0.0);
2009 +mpz_cmp_d (const mpz_t x, double d)
2011 + if (x->_mp_size < 0)
2016 + return -mpz_cmpabs_d (x, d);
2023 + return mpz_cmpabs_d (x, d);
2028 +/* MPZ comparisons and the like. */
2030 +mpz_sgn (const mpz_t u)
2032 + mp_size_t usize = u->_mp_size;
2034 + return (usize > 0) - (usize < 0);
2038 +mpz_cmp_si (const mpz_t u, long v)
2040 + mp_size_t usize = u->_mp_size;
2045 + return mpz_cmp_ui (u, v);
2046 + else if (usize >= 0)
2048 + else /* usize == -1 */
2050 + mp_limb_t ul = u->_mp_d[0];
2051 + if ((mp_limb_t)GMP_NEG_CAST (unsigned long int, v) < ul)
2054 + return (mp_limb_t)GMP_NEG_CAST (unsigned long int, v) > ul;
2059 +mpz_cmp_ui (const mpz_t u, unsigned long v)
2061 + mp_size_t usize = u->_mp_size;
2065 + else if (usize < 0)
2069 + mp_limb_t ul = (usize > 0) ? u->_mp_d[0] : 0;
2070 + return (ul > v) - (ul < v);
2075 +mpz_cmp (const mpz_t a, const mpz_t b)
2077 + mp_size_t asize = a->_mp_size;
2078 + mp_size_t bsize = b->_mp_size;
2080 + if (asize != bsize)
2081 + return (asize < bsize) ? -1 : 1;
2082 + else if (asize >= 0)
2083 + return mpn_cmp (a->_mp_d, b->_mp_d, asize);
2085 + return mpn_cmp (b->_mp_d, a->_mp_d, -asize);
2089 +mpz_cmpabs_ui (const mpz_t u, unsigned long v)
2091 + mp_size_t un = GMP_ABS (u->_mp_size);
2097 + ul = (un == 1) ? u->_mp_d[0] : 0;
2099 + return (ul > v) - (ul < v);
2103 +mpz_cmpabs (const mpz_t u, const mpz_t v)
2105 + return mpn_cmp4 (u->_mp_d, GMP_ABS (u->_mp_size),
2106 + v->_mp_d, GMP_ABS (v->_mp_size));
2110 +mpz_abs (mpz_t r, const mpz_t u)
2115 + r->_mp_size = GMP_ABS (r->_mp_size);
2119 +mpz_neg (mpz_t r, const mpz_t u)
2124 + r->_mp_size = -r->_mp_size;
2128 +mpz_swap (mpz_t u, mpz_t v)
2130 + MP_SIZE_T_SWAP (u->_mp_size, v->_mp_size);
2131 + MP_SIZE_T_SWAP (u->_mp_alloc, v->_mp_alloc);
2132 + MP_PTR_SWAP (u->_mp_d, v->_mp_d);
2136 +/* MPZ addition and subtraction */
2138 +/* Adds to the absolute value. Returns new size, but doesn't store it. */
2140 +mpz_abs_add_ui (mpz_t r, const mpz_t a, unsigned long b)
2146 + an = GMP_ABS (a->_mp_size);
2153 + rp = MPZ_REALLOC (r, an + 1);
2155 + cy = mpn_add_1 (rp, a->_mp_d, an, b);
2162 +/* Subtract from the absolute value. Returns new size, (or -1 on underflow),
2163 + but doesn't store it. */
2165 +mpz_abs_sub_ui (mpz_t r, const mpz_t a, unsigned long b)
2167 + mp_size_t an = GMP_ABS (a->_mp_size);
2168 + mp_ptr rp = MPZ_REALLOC (r, an);
2175 + else if (an == 1 && a->_mp_d[0] < b)
2177 + rp[0] = b - a->_mp_d[0];
2182 + gmp_assert_nocarry (mpn_sub_1 (rp, a->_mp_d, an, b));
2183 + return mpn_normalized_size (rp, an);
2188 +mpz_add_ui (mpz_t r, const mpz_t a, unsigned long b)
2190 + if (a->_mp_size >= 0)
2191 + r->_mp_size = mpz_abs_add_ui (r, a, b);
2193 + r->_mp_size = -mpz_abs_sub_ui (r, a, b);
2197 +mpz_sub_ui (mpz_t r, const mpz_t a, unsigned long b)
2199 + if (a->_mp_size < 0)
2200 + r->_mp_size = -mpz_abs_add_ui (r, a, b);
2202 + r->_mp_size = mpz_abs_sub_ui (r, a, b);
2206 +mpz_ui_sub (mpz_t r, unsigned long a, const mpz_t b)
2208 + if (b->_mp_size < 0)
2209 + r->_mp_size = mpz_abs_add_ui (r, b, a);
2211 + r->_mp_size = -mpz_abs_sub_ui (r, b, a);
2215 +mpz_abs_add (mpz_t r, const mpz_t a, const mpz_t b)
2217 + mp_size_t an = GMP_ABS (a->_mp_size);
2218 + mp_size_t bn = GMP_ABS (b->_mp_size);
2224 + MPZ_SRCPTR_SWAP (a, b);
2225 + MP_SIZE_T_SWAP (an, bn);
2228 + rp = MPZ_REALLOC (r, an + 1);
2229 + cy = mpn_add (rp, a->_mp_d, an, b->_mp_d, bn);
2237 +mpz_abs_sub (mpz_t r, const mpz_t a, const mpz_t b)
2239 + mp_size_t an = GMP_ABS (a->_mp_size);
2240 + mp_size_t bn = GMP_ABS (b->_mp_size);
2244 + cmp = mpn_cmp4 (a->_mp_d, an, b->_mp_d, bn);
2247 + rp = MPZ_REALLOC (r, an);
2248 + gmp_assert_nocarry (mpn_sub (rp, a->_mp_d, an, b->_mp_d, bn));
2249 + return mpn_normalized_size (rp, an);
2253 + rp = MPZ_REALLOC (r, bn);
2254 + gmp_assert_nocarry (mpn_sub (rp, b->_mp_d, bn, a->_mp_d, an));
2255 + return -mpn_normalized_size (rp, bn);
2262 +mpz_add (mpz_t r, const mpz_t a, const mpz_t b)
2266 + if ( (a->_mp_size ^ b->_mp_size) >= 0)
2267 + rn = mpz_abs_add (r, a, b);
2269 + rn = mpz_abs_sub (r, a, b);
2271 + r->_mp_size = a->_mp_size >= 0 ? rn : - rn;
2275 +mpz_sub (mpz_t r, const mpz_t a, const mpz_t b)
2279 + if ( (a->_mp_size ^ b->_mp_size) >= 0)
2280 + rn = mpz_abs_sub (r, a, b);
2282 + rn = mpz_abs_add (r, a, b);
2284 + r->_mp_size = a->_mp_size >= 0 ? rn : - rn;
2288 +/* MPZ multiplication */
2290 +mpz_mul_si (mpz_t r, const mpz_t u, long int v)
2294 + mpz_mul_ui (r, u, GMP_NEG_CAST (unsigned long int, v));
2298 + mpz_mul_ui (r, u, (unsigned long int) v);
2302 +mpz_mul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2310 + if (us == 0 || v == 0)
2316 + un = GMP_ABS (us);
2318 + tp = MPZ_REALLOC (r, un + 1);
2319 + cy = mpn_mul_1 (tp, u->_mp_d, un, v);
2323 + r->_mp_size = (us < 0) ? - un : un;
2327 +mpz_mul (mpz_t r, const mpz_t u, const mpz_t v)
2330 + mp_size_t un, vn, rn;
2337 + if (un == 0 || vn == 0)
2343 + sign = (un ^ vn) < 0;
2345 + un = GMP_ABS (un);
2346 + vn = GMP_ABS (vn);
2348 + mpz_init2 (t, (un + vn) * GMP_LIMB_BITS);
2352 + mpn_mul (tp, u->_mp_d, un, v->_mp_d, vn);
2354 + mpn_mul (tp, v->_mp_d, vn, u->_mp_d, un);
2357 + rn -= tp[rn-1] == 0;
2359 + t->_mp_size = sign ? - rn : rn;
2365 +mpz_mul_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bits)
2372 + un = GMP_ABS (u->_mp_size);
2379 + limbs = bits / GMP_LIMB_BITS;
2380 + shift = bits % GMP_LIMB_BITS;
2382 + rn = un + limbs + (shift > 0);
2383 + rp = MPZ_REALLOC (r, rn);
2386 + mp_limb_t cy = mpn_lshift (rp + limbs, u->_mp_d, un, shift);
2391 + mpn_copyd (rp + limbs, u->_mp_d, un);
2396 + r->_mp_size = (u->_mp_size < 0) ? - rn : rn;
2400 +mpz_addmul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2404 + mpz_mul_ui (t, u, v);
2405 + mpz_add (r, r, t);
2410 +mpz_submul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2414 + mpz_mul_ui (t, u, v);
2415 + mpz_sub (r, r, t);
2420 +mpz_addmul (mpz_t r, const mpz_t u, const mpz_t v)
2424 + mpz_mul (t, u, v);
2425 + mpz_add (r, r, t);
2430 +mpz_submul (mpz_t r, const mpz_t u, const mpz_t v)
2434 + mpz_mul (t, u, v);
2435 + mpz_sub (r, r, t);
2441 +enum mpz_div_round_mode { GMP_DIV_FLOOR, GMP_DIV_CEIL, GMP_DIV_TRUNC };
2443 +/* Allows q or r to be zero. Returns 1 iff remainder is non-zero. */
2445 +mpz_div_qr (mpz_t q, mpz_t r,
2446 + const mpz_t n, const mpz_t d, enum mpz_div_round_mode mode)
2448 + mp_size_t ns, ds, nn, dn, qs;
2453 + gmp_die("mpz_div_qr: Divide by zero.");
2464 + nn = GMP_ABS (ns);
2465 + dn = GMP_ABS (ds);
2471 + if (mode == GMP_DIV_CEIL && qs >= 0)
2473 + /* q = 1, r = n - d */
2475 + mpz_sub (r, n, d);
2477 + mpz_set_ui (q, 1);
2479 + else if (mode == GMP_DIV_FLOOR && qs < 0)
2481 + /* q = -1, r = n + d */
2483 + mpz_add (r, n, d);
2485 + mpz_set_si (q, -1);
2489 + /* q = 0, r = d */
2503 + mpz_init_set (tr, n);
2510 + mpz_init2 (tq, qn * GMP_LIMB_BITS);
2516 + mpn_div_qr (qp, np, nn, d->_mp_d, dn);
2520 + qn -= (qp[qn-1] == 0);
2522 + tq->_mp_size = qs < 0 ? -qn : qn;
2524 + rn = mpn_normalized_size (np, dn);
2525 + tr->_mp_size = ns < 0 ? - rn : rn;
2527 + if (mode == GMP_DIV_FLOOR && qs < 0 && rn != 0)
2530 + mpz_sub_ui (tq, tq, 1);
2532 + mpz_add (tr, tr, d);
2534 + else if (mode == GMP_DIV_CEIL && qs >= 0 && rn != 0)
2537 + mpz_add_ui (tq, tq, 1);
2539 + mpz_sub (tr, tr, d);
2557 +mpz_cdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2559 + mpz_div_qr (q, r, n, d, GMP_DIV_CEIL);
2563 +mpz_fdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2565 + mpz_div_qr (q, r, n, d, GMP_DIV_FLOOR);
2569 +mpz_tdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2571 + mpz_div_qr (q, r, n, d, GMP_DIV_TRUNC);
2575 +mpz_cdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2577 + mpz_div_qr (q, NULL, n, d, GMP_DIV_CEIL);
2581 +mpz_fdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2583 + mpz_div_qr (q, NULL, n, d, GMP_DIV_FLOOR);
2587 +mpz_tdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2589 + mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC);
2593 +mpz_cdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2595 + mpz_div_qr (NULL, r, n, d, GMP_DIV_CEIL);
2599 +mpz_fdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2601 + mpz_div_qr (NULL, r, n, d, GMP_DIV_FLOOR);
2605 +mpz_tdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2607 + mpz_div_qr (NULL, r, n, d, GMP_DIV_TRUNC);
2611 +mpz_mod (mpz_t r, const mpz_t n, const mpz_t d)
2613 + mpz_div_qr (NULL, r, n, d, d->_mp_size >= 0 ? GMP_DIV_FLOOR : GMP_DIV_CEIL);
2617 +mpz_div_q_2exp (mpz_t q, const mpz_t u, mp_bitcnt_t bit_index,
2618 + enum mpz_div_round_mode mode)
2621 + mp_size_t limb_cnt;
2631 + limb_cnt = bit_index / GMP_LIMB_BITS;
2632 + qn = GMP_ABS (un) - limb_cnt;
2633 + bit_index %= GMP_LIMB_BITS;
2635 + if (mode == ((un > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* un != 0 here. */
2636 + /* Note: Below, the final indexing at limb_cnt is valid because at
2637 + that point we have qn > 0. */
2639 + || !mpn_zero_p (u->_mp_d, limb_cnt)
2640 + || (u->_mp_d[limb_cnt]
2641 + & (((mp_limb_t) 1 << bit_index) - 1)));
2650 + qp = MPZ_REALLOC (q, qn);
2652 + if (bit_index != 0)
2654 + mpn_rshift (qp, u->_mp_d + limb_cnt, qn, bit_index);
2655 + qn -= qp[qn - 1] == 0;
2659 + mpn_copyi (qp, u->_mp_d + limb_cnt, qn);
2666 + mpz_add_ui (q, q, 1);
2672 +mpz_div_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bit_index,
2673 + enum mpz_div_round_mode mode)
2675 + mp_size_t us, un, rn;
2680 + if (us == 0 || bit_index == 0)
2685 + rn = (bit_index + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
2688 + rp = MPZ_REALLOC (r, rn);
2689 + un = GMP_ABS (us);
2691 + mask = GMP_LIMB_MAX >> (rn * GMP_LIMB_BITS - bit_index);
2695 + /* Quotient (with truncation) is zero, and remainder is
2697 + if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
2699 + /* Have to negate and sign extend. */
2703 + for (cy = 1, i = 0; i < un; i++)
2705 + mp_limb_t s = ~u->_mp_d[i] + cy;
2710 + for (; i < rn - 1; i++)
2711 + rp[i] = GMP_LIMB_MAX;
2720 + mpn_copyi (rp, u->_mp_d, un);
2728 + mpn_copyi (rp, u->_mp_d, rn - 1);
2730 + rp[rn-1] = u->_mp_d[rn-1] & mask;
2732 + if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
2734 + /* If r != 0, compute 2^{bit_count} - r. */
2737 + for (i = 0; i < rn && rp[i] == 0; i++)
2741 + /* r > 0, need to flip sign. */
2742 + rp[i] = ~rp[i] + 1;
2748 + /* us is not used for anything else, so we can modify it
2749 + here to indicate flipped sign. */
2754 + rn = mpn_normalized_size (rp, rn);
2755 + r->_mp_size = us < 0 ? -rn : rn;
2759 +mpz_cdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2761 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_CEIL);
2765 +mpz_fdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2767 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_FLOOR);
2771 +mpz_tdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2773 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_TRUNC);
2777 +mpz_cdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2779 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_CEIL);
2783 +mpz_fdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2785 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_FLOOR);
2789 +mpz_tdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2791 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_TRUNC);
2795 +mpz_divexact (mpz_t q, const mpz_t n, const mpz_t d)
2797 + gmp_assert_nocarry (mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC));
2801 +mpz_divisible_p (const mpz_t n, const mpz_t d)
2803 + return mpz_div_qr (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
2807 +mpz_congruent_p (const mpz_t a, const mpz_t b, const mpz_t m)
2812 + /* a == b (mod 0) iff a == b */
2813 + if (mpz_sgn (m) == 0)
2814 + return (mpz_cmp (a, b) == 0);
2817 + mpz_sub (t, a, b);
2818 + res = mpz_divisible_p (t, m);
2824 +static unsigned long
2825 +mpz_div_qr_ui (mpz_t q, mpz_t r,
2826 + const mpz_t n, unsigned long d, enum mpz_div_round_mode mode)
2843 + qn = GMP_ABS (ns);
2845 + qp = MPZ_REALLOC (q, qn);
2849 + rl = mpn_div_qr_1 (qp, n->_mp_d, qn, d);
2853 + rs = (ns < 0) ? -rs : rs;
2855 + if (rl > 0 && ( (mode == GMP_DIV_FLOOR && ns < 0)
2856 + || (mode == GMP_DIV_CEIL && ns >= 0)))
2859 + gmp_assert_nocarry (mpn_add_1 (qp, qp, qn, 1));
2871 + qn -= (qp[qn-1] == 0);
2872 + assert (qn == 0 || qp[qn-1] > 0);
2874 + q->_mp_size = (ns < 0) ? - qn : qn;
2881 +mpz_cdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
2883 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_CEIL);
2887 +mpz_fdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
2889 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_FLOOR);
2893 +mpz_tdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
2895 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_TRUNC);
2899 +mpz_cdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
2901 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_CEIL);
2905 +mpz_fdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
2907 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_FLOOR);
2911 +mpz_tdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
2913 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC);
2917 +mpz_cdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
2919 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_CEIL);
2922 +mpz_fdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
2924 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
2927 +mpz_tdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
2929 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_TRUNC);
2933 +mpz_cdiv_ui (const mpz_t n, unsigned long d)
2935 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_CEIL);
2939 +mpz_fdiv_ui (const mpz_t n, unsigned long d)
2941 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_FLOOR);
2945 +mpz_tdiv_ui (const mpz_t n, unsigned long d)
2947 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC);
2951 +mpz_mod_ui (mpz_t r, const mpz_t n, unsigned long d)
2953 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
2957 +mpz_divexact_ui (mpz_t q, const mpz_t n, unsigned long d)
2959 + gmp_assert_nocarry (mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC));
2963 +mpz_divisible_ui_p (const mpz_t n, unsigned long d)
2965 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
2971 +mpn_gcd_11 (mp_limb_t u, mp_limb_t v)
2975 + assert ( (u | v) > 0);
2982 + gmp_ctz (shift, u | v);
2987 + if ( (u & 1) == 0)
2988 + MP_LIMB_T_SWAP (u, v);
2990 + while ( (v & 1) == 0)
3000 + while ( (u & 1) == 0);
3007 + while ( (v & 1) == 0);
3010 + return u << shift;
3014 +mpz_gcd_ui (mpz_t g, const mpz_t u, unsigned long v)
3025 + un = GMP_ABS (u->_mp_size);
3027 + v = mpn_gcd_11 (mpn_div_qr_1 (NULL, u->_mp_d, un, v), v);
3030 + mpz_set_ui (g, v);
3037 +mpz_make_odd (mpz_t r)
3039 + mp_bitcnt_t shift;
3041 + assert (r->_mp_size > 0);
3042 + /* Count trailing zeros, equivalent to mpn_scan1, because we know that there is a 1 */
3043 + shift = mpn_common_scan (r->_mp_d[0], 0, r->_mp_d, 0, 0);
3044 + mpz_tdiv_q_2exp (r, r, shift);
3050 +mpz_gcd (mpz_t g, const mpz_t u, const mpz_t v)
3053 + mp_bitcnt_t uz, vz, gz;
3055 + if (u->_mp_size == 0)
3060 + if (v->_mp_size == 0)
3070 + uz = mpz_make_odd (tu);
3072 + vz = mpz_make_odd (tv);
3073 + gz = GMP_MIN (uz, vz);
3075 + if (tu->_mp_size < tv->_mp_size)
3076 + mpz_swap (tu, tv);
3078 + mpz_tdiv_r (tu, tu, tv);
3079 + if (tu->_mp_size == 0)
3088 + mpz_make_odd (tu);
3089 + c = mpz_cmp (tu, tv);
3096 + mpz_swap (tu, tv);
3098 + if (tv->_mp_size == 1)
3100 + mp_limb_t vl = tv->_mp_d[0];
3101 + mp_limb_t ul = mpz_tdiv_ui (tu, vl);
3102 + mpz_set_ui (g, mpn_gcd_11 (ul, vl));
3105 + mpz_sub (tu, tu, tv);
3109 + mpz_mul_2exp (g, g, gz);
3113 +mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, const mpz_t u, const mpz_t v)
3115 + mpz_t tu, tv, s0, s1, t0, t1;
3116 + mp_bitcnt_t uz, vz, gz;
3117 + mp_bitcnt_t power;
3119 + if (u->_mp_size == 0)
3121 + /* g = 0 u + sgn(v) v */
3122 + signed long sign = mpz_sgn (v);
3125 + mpz_set_ui (s, 0);
3127 + mpz_set_si (t, sign);
3131 + if (v->_mp_size == 0)
3133 + /* g = sgn(u) u + 0 v */
3134 + signed long sign = mpz_sgn (u);
3137 + mpz_set_si (s, sign);
3139 + mpz_set_ui (t, 0);
3151 + uz = mpz_make_odd (tu);
3153 + vz = mpz_make_odd (tv);
3154 + gz = GMP_MIN (uz, vz);
3159 + /* Cofactors corresponding to odd gcd. gz handled later. */
3160 + if (tu->_mp_size < tv->_mp_size)
3162 + mpz_swap (tu, tv);
3163 + MPZ_SRCPTR_SWAP (u, v);
3164 + MPZ_PTR_SWAP (s, t);
3165 + MP_BITCNT_T_SWAP (uz, vz);
3170 + * u = t0 tu + t1 tv
3171 + * v = s0 tu + s1 tv
3173 + * where u and v denote the inputs with common factors of two
3174 + * eliminated, and det (s0, t0; s1, t1) = 2^p. Then
3176 + * 2^p tu = s1 u - t1 v
3177 + * 2^p tv = -s0 u + t0 v
3180 + /* After initial division, tu = q tv + tu', we have
3182 + * u = 2^uz (tu' + q tv)
3187 + * t0 = 2^uz, t1 = 2^uz q
3188 + * s0 = 0, s1 = 2^vz
3191 + mpz_setbit (t0, uz);
3192 + mpz_tdiv_qr (t1, tu, tu, tv);
3193 + mpz_mul_2exp (t1, t1, uz);
3195 + mpz_setbit (s1, vz);
3198 + if (tu->_mp_size > 0)
3200 + mp_bitcnt_t shift;
3201 + shift = mpz_make_odd (tu);
3202 + mpz_mul_2exp (t0, t0, shift);
3203 + mpz_mul_2exp (s0, s0, shift);
3209 + c = mpz_cmp (tu, tv);
3217 + * u = t0 tu + t1 (tv' + tu) = (t0 + t1) tu + t1 tv'
3218 + * v = s0 tu + s1 (tv' + tu) = (s0 + s1) tu + s1 tv' */
3220 + mpz_sub (tv, tv, tu);
3221 + mpz_add (t0, t0, t1);
3222 + mpz_add (s0, s0, s1);
3224 + shift = mpz_make_odd (tv);
3225 + mpz_mul_2exp (t1, t1, shift);
3226 + mpz_mul_2exp (s1, s1, shift);
3230 + mpz_sub (tu, tu, tv);
3231 + mpz_add (t1, t0, t1);
3232 + mpz_add (s1, s0, s1);
3234 + shift = mpz_make_odd (tu);
3235 + mpz_mul_2exp (t0, t0, shift);
3236 + mpz_mul_2exp (s0, s0, shift);
3242 + /* Now tv = odd part of gcd, and -s0 and t0 are corresponding
3245 + mpz_mul_2exp (tv, tv, gz);
3248 + /* 2^p g = s0 u + t0 v. Eliminate one factor of two at a time. To
3249 + adjust cofactors, we need u / g and v / g */
3251 + mpz_divexact (s1, v, tv);
3253 + mpz_divexact (t1, u, tv);
3256 + while (power-- > 0)
3258 + /* s0 u + t0 v = (s0 - v/g) u - (t0 + u/g) v */
3259 + if (mpz_odd_p (s0) || mpz_odd_p (t0))
3261 + mpz_sub (s0, s0, s1);
3262 + mpz_add (t0, t0, t1);
3264 + mpz_divexact_ui (s0, s0, 2);
3265 + mpz_divexact_ui (t0, t0, 2);
3268 + /* Arrange so that |s| < |u| / 2g */
3269 + mpz_add (s1, s0, s1);
3270 + if (mpz_cmpabs (s0, s1) > 0)
3272 + mpz_swap (s0, s1);
3273 + mpz_sub (t0, t0, t1);
3275 + if (u->_mp_size < 0)
3277 + if (v->_mp_size < 0)
3295 +mpz_lcm (mpz_t r, const mpz_t u, const mpz_t v)
3299 + if (u->_mp_size == 0 || v->_mp_size == 0)
3307 + mpz_gcd (g, u, v);
3308 + mpz_divexact (g, u, g);
3309 + mpz_mul (r, g, v);
3316 +mpz_lcm_ui (mpz_t r, const mpz_t u, unsigned long v)
3318 + if (v == 0 || u->_mp_size == 0)
3324 + v /= mpz_gcd_ui (NULL, u, v);
3325 + mpz_mul_ui (r, u, v);
3331 +mpz_invert (mpz_t r, const mpz_t u, const mpz_t m)
3336 + if (u->_mp_size == 0 || mpz_cmpabs_ui (m, 1) <= 0)
3342 + mpz_gcdext (g, tr, NULL, u, m);
3343 + invertible = (mpz_cmp_ui (g, 1) == 0);
3347 + if (tr->_mp_size < 0)
3349 + if (m->_mp_size >= 0)
3350 + mpz_add (tr, tr, m);
3352 + mpz_sub (tr, tr, m);
3359 + return invertible;
3363 +/* Higher level operations (sqrt, pow and root) */
3366 +mpz_pow_ui (mpz_t r, const mpz_t b, unsigned long e)
3368 + unsigned long bit;
3370 + mpz_init_set_ui (tr, 1);
3372 + bit = GMP_ULONG_HIGHBIT;
3375 + mpz_mul (tr, tr, tr);
3377 + mpz_mul (tr, tr, b);
3387 +mpz_ui_pow_ui (mpz_t r, unsigned long blimb, unsigned long e)
3390 + mpz_init_set_ui (b, blimb);
3391 + mpz_pow_ui (r, b, e);
3396 +mpz_powm (mpz_t r, const mpz_t b, const mpz_t e, const mpz_t m)
3402 + struct gmp_div_inverse minv;
3406 + en = GMP_ABS (e->_mp_size);
3407 + mn = GMP_ABS (m->_mp_size);
3409 + gmp_die ("mpz_powm: Zero modulo.");
3413 + mpz_set_ui (r, 1);
3418 + mpn_div_qr_invert (&minv, mp, mn);
3419 + shift = minv.shift;
3423 + /* To avoid shifts, we do all our reductions, except the final
3424 + one, using a *normalized* m. */
3427 + tp = gmp_xalloc_limbs (mn);
3428 + gmp_assert_nocarry (mpn_lshift (tp, mp, mn, shift));
3434 + if (e->_mp_size < 0)
3436 + if (!mpz_invert (base, b, m))
3437 + gmp_die ("mpz_powm: Negative exponent and non-invertible base.");
3442 + mpz_abs (base, b);
3444 + bn = base->_mp_size;
3447 + mpn_div_qr_preinv (NULL, base->_mp_d, base->_mp_size, mp, mn, &minv);
3451 + /* We have reduced the absolute value. Now take care of the
3452 + sign. Note that we get zero represented non-canonically as
3454 + if (b->_mp_size < 0)
3456 + mp_ptr bp = MPZ_REALLOC (base, mn);
3457 + gmp_assert_nocarry (mpn_sub (bp, mp, mn, bp, bn));
3460 + base->_mp_size = mpn_normalized_size (base->_mp_d, bn);
3462 + mpz_init_set_ui (tr, 1);
3466 + mp_limb_t w = e->_mp_d[en];
3469 + bit = GMP_LIMB_HIGHBIT;
3472 + mpz_mul (tr, tr, tr);
3474 + mpz_mul (tr, tr, base);
3475 + if (tr->_mp_size > mn)
3477 + mpn_div_qr_preinv (NULL, tr->_mp_d, tr->_mp_size, mp, mn, &minv);
3478 + tr->_mp_size = mpn_normalized_size (tr->_mp_d, mn);
3485 + /* Final reduction */
3486 + if (tr->_mp_size >= mn)
3488 + minv.shift = shift;
3489 + mpn_div_qr_preinv (NULL, tr->_mp_d, tr->_mp_size, mp, mn, &minv);
3490 + tr->_mp_size = mpn_normalized_size (tr->_mp_d, mn);
3501 +mpz_powm_ui (mpz_t r, const mpz_t b, unsigned long elimb, const mpz_t m)
3504 + mpz_init_set_ui (e, elimb);
3505 + mpz_powm (r, b, e, m);
3509 +/* x=trunc(y^(1/z)), r=y-x^z */
3511 +mpz_rootrem (mpz_t x, mpz_t r, const mpz_t y, unsigned long z)
3516 + sgn = y->_mp_size < 0;
3517 + if ((~z & sgn) != 0)
3518 + gmp_die ("mpz_rootrem: Negative argument, with even root.");
3520 + gmp_die ("mpz_rootrem: Zeroth root.");
3522 + if (mpz_cmpabs_ui (y, 1) <= 0) {
3533 + tb = mpz_sizeinbase (y, 2) / z + 1;
3534 + mpz_init2 (t, tb);
3535 + mpz_setbit (t, tb);
3538 + if (z == 2) /* simplify sqrt loop: z-1 == 1 */
3540 + mpz_swap (u, t); /* u = x */
3541 + mpz_tdiv_q (t, y, u); /* t = y/x */
3542 + mpz_add (t, t, u); /* t = y/x + x */
3543 + mpz_tdiv_q_2exp (t, t, 1); /* x'= (y/x + x)/2 */
3544 + } while (mpz_cmpabs (t, u) < 0); /* |x'| < |x| */
3545 + else /* z != 2 */ {
3553 + mpz_swap (u, t); /* u = x */
3554 + mpz_pow_ui (t, u, z - 1); /* t = x^(z-1) */
3555 + mpz_tdiv_q (t, y, t); /* t = y/x^(z-1) */
3556 + mpz_mul_ui (v, u, z - 1); /* v = x*(z-1) */
3557 + mpz_add (t, t, v); /* t = y/x^(z-1) + x*(z-1) */
3558 + mpz_tdiv_q_ui (t, t, z); /* x'=(y/x^(z-1) + x*(z-1))/z */
3559 + } while (mpz_cmpabs (t, u) < 0); /* |x'| < |x| */
3565 + mpz_pow_ui (t, u, z);
3566 + mpz_sub (r, y, t);
3575 +mpz_root (mpz_t x, const mpz_t y, unsigned long z)
3581 + mpz_rootrem (x, r, y, z);
3582 + res = r->_mp_size == 0;
3588 +/* Compute s = floor(sqrt(u)) and r = u - s^2. Allows r == NULL */
3590 +mpz_sqrtrem (mpz_t s, mpz_t r, const mpz_t u)
3592 + mpz_rootrem (s, r, u, 2);
3596 +mpz_sqrt (mpz_t s, const mpz_t u)
3598 + mpz_rootrem (s, NULL, u, 2);
3602 +mpz_perfect_square_p (const mpz_t u)
3604 + if (u->_mp_size <= 0)
3605 + return (u->_mp_size == 0);
3607 + return mpz_root (NULL, u, 2);
3611 +mpn_perfect_square_p (mp_srcptr p, mp_size_t n)
3616 + assert (p [n-1] != 0);
3617 + return mpz_root (NULL, mpz_roinit_n (t, p, n), 2);
3621 +mpn_sqrtrem (mp_ptr sp, mp_ptr rp, mp_srcptr p, mp_size_t n)
3627 + assert (p [n-1] != 0);
3631 + mpz_rootrem (s, r, mpz_roinit_n (u, p, n), 2);
3633 + assert (s->_mp_size == (n+1)/2);
3634 + mpn_copyd (sp, s->_mp_d, s->_mp_size);
3636 + res = r->_mp_size;
3638 + mpn_copyd (rp, r->_mp_d, res);
3643 +/* Combinatorics */
3646 +mpz_fac_ui (mpz_t x, unsigned long n)
3648 + mpz_set_ui (x, n + (n == 0));
3650 + mpz_mul_ui (x, x, --n);
3654 +mpz_bin_uiui (mpz_t r, unsigned long n, unsigned long k)
3658 + mpz_set_ui (r, k <= n);
3661 + k = (k <= n) ? n - k : 0;
3664 + mpz_fac_ui (t, k);
3666 + for (; k > 0; k--)
3667 + mpz_mul_ui (r, r, n--);
3669 + mpz_divexact (r, r, t);
3674 +/* Primality testing */
3676 +gmp_millerrabin (const mpz_t n, const mpz_t nm1, mpz_t y,
3677 + const mpz_t q, mp_bitcnt_t k)
3681 + /* Caller must initialize y to the base. */
3682 + mpz_powm (y, y, q, n);
3684 + if (mpz_cmp_ui (y, 1) == 0 || mpz_cmp (y, nm1) == 0)
3689 + mpz_powm_ui (y, y, 2, n);
3690 + if (mpz_cmp (y, nm1) == 0)
3692 + /* y == 1 means that the previous y was a non-trivial square root
3693 + of 1 (mod n). y == 0 means that n is a power of the base.
3694 + In either case, n is not prime. */
3695 + if (mpz_cmp_ui (y, 1) <= 0)
3701 +/* This product is 0xc0cfd797, and fits in 32 bits. */
3702 +#define GMP_PRIME_PRODUCT \
3703 + (3UL*5UL*7UL*11UL*13UL*17UL*19UL*23UL*29UL)
3705 +/* Bit (p+1)/2 is set, for each odd prime <= 61 */
3706 +#define GMP_PRIME_MASK 0xc96996dcUL
3709 +mpz_probab_prime_p (const mpz_t n, int reps)
3718 + /* Note that we use the absolute value of n only, for compatibility
3719 + with the real GMP. */
3720 + if (mpz_even_p (n))
3721 + return (mpz_cmpabs_ui (n, 2) == 0) ? 2 : 0;
3723 + /* Above test excludes n == 0 */
3724 + assert (n->_mp_size != 0);
3726 + if (mpz_cmpabs_ui (n, 64) < 0)
3727 + return (GMP_PRIME_MASK >> (n->_mp_d[0] >> 1)) & 2;
3729 + if (mpz_gcd_ui (NULL, n, GMP_PRIME_PRODUCT) != 1)
3732 + /* All prime factors are >= 31. */
3733 + if (mpz_cmpabs_ui (n, 31*31) < 0)
3736 + /* Use Miller-Rabin, with a deterministic sequence of bases, a[j] =
3737 + j^2 + j + 41 using Euler's polynomial. We potentially stop early,
3738 + if a[j] >= n - 1. Since n >= 31*31, this can happen only if reps >
3739 + 30 (a[30] == 971 > 31*31 == 961). */
3745 + /* Find q and k, where q is odd and n = 1 + 2**k * q. */
3746 + nm1->_mp_size = mpz_abs_sub_ui (nm1, n, 1);
3747 + k = mpz_scan1 (nm1, 0);
3748 + mpz_tdiv_q_2exp (q, nm1, k);
3750 + for (j = 0, is_prime = 1; is_prime & (j < reps); j++)
3752 + mpz_set_ui (y, (unsigned long) j*j+j+41);
3753 + if (mpz_cmp (y, nm1) >= 0)
3755 + /* Don't try any further bases. This "early" break does not affect
3756 + the result for any reasonable reps value (<=5000 was tested) */
3760 + is_prime = gmp_millerrabin (n, nm1, y, q, k);
3770 +/* Logical operations and bit manipulation. */
3772 +/* Numbers are treated as if represented in two's complement (and
3773 + infinitely sign extended). For a negative values we get the two's
3774 + complement from -x = ~x + 1, where ~ is bitwise complement.
3775 + Negation transforms
3783 + where yyyy is the bitwise complement of xxxx. So least significant
3784 + bits, up to and including the first one bit, are unchanged, and
3785 + the more significant bits are all complemented.
3787 + To change a bit from zero to one in a negative number, subtract the
3788 + corresponding power of two from the absolute value. This can never
3789 + underflow. To change a bit from one to zero, add the corresponding
3790 + power of two, and this might overflow. E.g., if x = -001111, the
3791 + two's complement is 110001. Clearing the least significant bit, we
3792 + get two's complement 110000, and -010000. */
3795 +mpz_tstbit (const mpz_t d, mp_bitcnt_t bit_index)
3797 + mp_size_t limb_index;
3805 + dn = GMP_ABS (ds);
3806 + limb_index = bit_index / GMP_LIMB_BITS;
3807 + if (limb_index >= dn)
3810 + shift = bit_index % GMP_LIMB_BITS;
3811 + w = d->_mp_d[limb_index];
3812 + bit = (w >> shift) & 1;
3816 + /* d < 0. Check if any of the bits below is set: If so, our bit
3817 + must be complemented. */
3818 + if (shift > 0 && (w << (GMP_LIMB_BITS - shift)) > 0)
3820 + while (limb_index-- > 0)
3821 + if (d->_mp_d[limb_index] > 0)
3828 +mpz_abs_add_bit (mpz_t d, mp_bitcnt_t bit_index)
3830 + mp_size_t dn, limb_index;
3834 + dn = GMP_ABS (d->_mp_size);
3836 + limb_index = bit_index / GMP_LIMB_BITS;
3837 + bit = (mp_limb_t) 1 << (bit_index % GMP_LIMB_BITS);
3839 + if (limb_index >= dn)
3842 + /* The bit should be set outside of the end of the number.
3843 + We have to increase the size of the number. */
3844 + dp = MPZ_REALLOC (d, limb_index + 1);
3846 + dp[limb_index] = bit;
3847 + for (i = dn; i < limb_index; i++)
3849 + dn = limb_index + 1;
3857 + cy = mpn_add_1 (dp + limb_index, dp + limb_index, dn - limb_index, bit);
3860 + dp = MPZ_REALLOC (d, dn + 1);
3865 + d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
3869 +mpz_abs_sub_bit (mpz_t d, mp_bitcnt_t bit_index)
3871 + mp_size_t dn, limb_index;
3875 + dn = GMP_ABS (d->_mp_size);
3878 + limb_index = bit_index / GMP_LIMB_BITS;
3879 + bit = (mp_limb_t) 1 << (bit_index % GMP_LIMB_BITS);
3881 + assert (limb_index < dn);
3883 + gmp_assert_nocarry (mpn_sub_1 (dp + limb_index, dp + limb_index,
3884 + dn - limb_index, bit));
3885 + dn = mpn_normalized_size (dp, dn);
3886 + d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
3890 +mpz_setbit (mpz_t d, mp_bitcnt_t bit_index)
3892 + if (!mpz_tstbit (d, bit_index))
3894 + if (d->_mp_size >= 0)
3895 + mpz_abs_add_bit (d, bit_index);
3897 + mpz_abs_sub_bit (d, bit_index);
3902 +mpz_clrbit (mpz_t d, mp_bitcnt_t bit_index)
3904 + if (mpz_tstbit (d, bit_index))
3906 + if (d->_mp_size >= 0)
3907 + mpz_abs_sub_bit (d, bit_index);
3909 + mpz_abs_add_bit (d, bit_index);
3914 +mpz_combit (mpz_t d, mp_bitcnt_t bit_index)
3916 + if (mpz_tstbit (d, bit_index) ^ (d->_mp_size < 0))
3917 + mpz_abs_sub_bit (d, bit_index);
3919 + mpz_abs_add_bit (d, bit_index);
3923 +mpz_com (mpz_t r, const mpz_t u)
3926 + mpz_sub_ui (r, r, 1);
3930 +mpz_and (mpz_t r, const mpz_t u, const mpz_t v)
3932 + mp_size_t un, vn, rn, i;
3933 + mp_ptr up, vp, rp;
3935 + mp_limb_t ux, vx, rx;
3936 + mp_limb_t uc, vc, rc;
3937 + mp_limb_t ul, vl, rl;
3939 + un = GMP_ABS (u->_mp_size);
3940 + vn = GMP_ABS (v->_mp_size);
3943 + MPZ_SRCPTR_SWAP (u, v);
3944 + MP_SIZE_T_SWAP (un, vn);
3952 + uc = u->_mp_size < 0;
3953 + vc = v->_mp_size < 0;
3960 + /* If the smaller input is positive, higher limbs don't matter. */
3961 + rn = vx ? un : vn;
3963 + rp = MPZ_REALLOC (r, rn + rc);
3971 + ul = (up[i] ^ ux) + uc;
3974 + vl = (vp[i] ^ vx) + vc;
3977 + rl = ( (ul & vl) ^ rx) + rc;
3984 + for (; i < rn; i++)
3986 + ul = (up[i] ^ ux) + uc;
3989 + rl = ( (ul & vx) ^ rx) + rc;
3996 + rn = mpn_normalized_size (rp, rn);
3998 + r->_mp_size = rx ? -rn : rn;
4002 +mpz_ior (mpz_t r, const mpz_t u, const mpz_t v)
4004 + mp_size_t un, vn, rn, i;
4005 + mp_ptr up, vp, rp;
4007 + mp_limb_t ux, vx, rx;
4008 + mp_limb_t uc, vc, rc;
4009 + mp_limb_t ul, vl, rl;
4011 + un = GMP_ABS (u->_mp_size);
4012 + vn = GMP_ABS (v->_mp_size);
4015 + MPZ_SRCPTR_SWAP (u, v);
4016 + MP_SIZE_T_SWAP (un, vn);
4024 + uc = u->_mp_size < 0;
4025 + vc = v->_mp_size < 0;
4032 + /* If the smaller input is negative, by sign extension higher limbs
4034 + rn = vx ? vn : un;
4036 + rp = MPZ_REALLOC (r, rn + rc);
4044 + ul = (up[i] ^ ux) + uc;
4047 + vl = (vp[i] ^ vx) + vc;
4050 + rl = ( (ul | vl) ^ rx) + rc;
4057 + for (; i < rn; i++)
4059 + ul = (up[i] ^ ux) + uc;
4062 + rl = ( (ul | vx) ^ rx) + rc;
4069 + rn = mpn_normalized_size (rp, rn);
4071 + r->_mp_size = rx ? -rn : rn;
4075 +mpz_xor (mpz_t r, const mpz_t u, const mpz_t v)
4077 + mp_size_t un, vn, i;
4078 + mp_ptr up, vp, rp;
4080 + mp_limb_t ux, vx, rx;
4081 + mp_limb_t uc, vc, rc;
4082 + mp_limb_t ul, vl, rl;
4084 + un = GMP_ABS (u->_mp_size);
4085 + vn = GMP_ABS (v->_mp_size);
4088 + MPZ_SRCPTR_SWAP (u, v);
4089 + MP_SIZE_T_SWAP (un, vn);
4097 + uc = u->_mp_size < 0;
4098 + vc = v->_mp_size < 0;
4105 + rp = MPZ_REALLOC (r, un + rc);
4113 + ul = (up[i] ^ ux) + uc;
4116 + vl = (vp[i] ^ vx) + vc;
4119 + rl = (ul ^ vl ^ rx) + rc;
4126 + for (; i < un; i++)
4128 + ul = (up[i] ^ ux) + uc;
4131 + rl = (ul ^ ux) + rc;
4138 + un = mpn_normalized_size (rp, un);
4140 + r->_mp_size = rx ? -un : un;
4144 +gmp_popcount_limb (mp_limb_t x)
4148 + /* Do 16 bits at a time, to avoid limb-sized constants. */
4149 + for (c = 0; x > 0; x >>= 16)
4151 + unsigned w = ((x >> 1) & 0x5555) + (x & 0x5555);
4152 + w = ((w >> 2) & 0x3333) + (w & 0x3333);
4153 + w = ((w >> 4) & 0x0f0f) + (w & 0x0f0f);
4154 + w = (w >> 8) + (w & 0x00ff);
4161 +mpn_popcount (mp_srcptr p, mp_size_t n)
4166 + for (c = 0, i = 0; i < n; i++)
4167 + c += gmp_popcount_limb (p[i]);
4173 +mpz_popcount (const mpz_t u)
4180 + return ~(mp_bitcnt_t) 0;
4182 + return mpn_popcount (u->_mp_d, un);
4186 +mpz_hamdist (const mpz_t u, const mpz_t v)
4188 + mp_size_t un, vn, i;
4189 + mp_limb_t uc, vc, ul, vl, comp;
4196 + if ( (un ^ vn) < 0)
4197 + return ~(mp_bitcnt_t) 0;
4199 + comp = - (uc = vc = (un < 0));
4211 + MPN_SRCPTR_SWAP (up, un, vp, vn);
4213 + for (i = 0, c = 0; i < vn; i++)
4215 + ul = (up[i] ^ comp) + uc;
4218 + vl = (vp[i] ^ comp) + vc;
4221 + c += gmp_popcount_limb (ul ^ vl);
4225 + for (; i < un; i++)
4227 + ul = (up[i] ^ comp) + uc;
4230 + c += gmp_popcount_limb (ul ^ comp);
4237 +mpz_scan1 (const mpz_t u, mp_bitcnt_t starting_bit)
4240 + mp_size_t us, un, i;
4241 + mp_limb_t limb, ux;
4244 + un = GMP_ABS (us);
4245 + i = starting_bit / GMP_LIMB_BITS;
4247 + /* Past the end there's no 1 bits for u>=0, or an immediate 1 bit
4248 + for u<0. Notice this test picks up any u==0 too. */
4250 + return (us >= 0 ? ~(mp_bitcnt_t) 0 : starting_bit);
4256 + if (starting_bit != 0)
4260 + ux = mpn_zero_p (up, i);
4261 + limb = ~ limb + ux;
4262 + ux = - (mp_limb_t) (limb >= ux);
4265 + /* Mask to 0 all bits before starting_bit, thus ignoring them. */
4266 + limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
4269 + return mpn_common_scan (limb, i, up, un, ux);
4273 +mpz_scan0 (const mpz_t u, mp_bitcnt_t starting_bit)
4276 + mp_size_t us, un, i;
4277 + mp_limb_t limb, ux;
4280 + ux = - (mp_limb_t) (us >= 0);
4281 + un = GMP_ABS (us);
4282 + i = starting_bit / GMP_LIMB_BITS;
4284 + /* When past end, there's an immediate 0 bit for u>=0, or no 0 bits for
4285 + u<0. Notice this test picks up all cases of u==0 too. */
4287 + return (ux ? starting_bit : ~(mp_bitcnt_t) 0);
4290 + limb = up[i] ^ ux;
4293 + limb -= mpn_zero_p (up, i); /* limb = ~(~limb + zero_p) */
4295 + /* Mask all bits before starting_bit, thus ignoring them. */
4296 + limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
4298 + return mpn_common_scan (limb, i, up, un, ux);
4302 +/* MPZ base conversion. */
4305 +mpz_sizeinbase (const mpz_t u, int base)
4311 + struct gmp_div_inverse bi;
4314 + assert (base >= 2);
4315 + assert (base <= 36);
4317 + un = GMP_ABS (u->_mp_size);
4323 + bits = (un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1]);
4329 + return (bits + 1) / 2;
4331 + return (bits + 2) / 3;
4333 + return (bits + 3) / 4;
4335 + return (bits + 4) / 5;
4336 + /* FIXME: Do something more clever for the common case of base
4340 + tp = gmp_xalloc_limbs (un);
4341 + mpn_copyi (tp, up, un);
4342 + mpn_div_qr_1_invert (&bi, base);
4348 + mpn_div_qr_1_preinv (tp, tp, un, &bi);
4349 + un -= (tp[un-1] == 0);
4358 +mpz_get_str (char *sp, int base, const mpz_t u)
4361 + const char *digits;
4367 + digits = "0123456789abcdefghijklmnopqrstuvwxyz";
4372 + digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4379 + sn = 1 + mpz_sizeinbase (u, base);
4381 + sp = gmp_xalloc (1 + sn);
4383 + un = GMP_ABS (u->_mp_size);
4394 + if (u->_mp_size < 0)
4397 + bits = mpn_base_power_of_two_p (base);
4400 + /* Not modified in this case. */
4401 + sn = i + mpn_get_str_bits ((unsigned char *) sp + i, bits, u->_mp_d, un);
4404 + struct mpn_base_info info;
4407 + mpn_get_base_info (&info, base);
4408 + tp = gmp_xalloc_limbs (un);
4409 + mpn_copyi (tp, u->_mp_d, un);
4411 + sn = i + mpn_get_str_other ((unsigned char *) sp + i, base, &info, tp, un);
4415 + for (; i < sn; i++)
4416 + sp[i] = digits[(unsigned char) sp[i]];
4423 +mpz_set_str (mpz_t r, const char *sp, int base)
4426 + mp_size_t rn, alloc;
4430 + unsigned char *dp;
4432 + assert (base == 0 || (base >= 2 && base <= 36));
4434 + while (isspace( (unsigned char) *sp))
4437 + sign = (*sp == '-');
4445 + if (*sp == 'x' || *sp == 'X')
4450 + else if (*sp == 'b' || *sp == 'B')
4463 + dp = gmp_xalloc (sn + (sn == 0));
4465 + for (sn = 0; *sp; sp++)
4469 + if (isspace ((unsigned char) *sp))
4471 + if (*sp >= '0' && *sp <= '9')
4472 + digit = *sp - '0';
4473 + else if (*sp >= 'a' && *sp <= 'z')
4474 + digit = *sp - 'a' + 10;
4475 + else if (*sp >= 'A' && *sp <= 'Z')
4476 + digit = *sp - 'A' + 10;
4478 + digit = base; /* fail */
4480 + if (digit >= base)
4490 + bits = mpn_base_power_of_two_p (base);
4494 + alloc = (sn * bits + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
4495 + rp = MPZ_REALLOC (r, alloc);
4496 + rn = mpn_set_str_bits (rp, dp, sn, bits);
4500 + struct mpn_base_info info;
4501 + mpn_get_base_info (&info, base);
4502 + alloc = (sn + info.exp - 1) / info.exp;
4503 + rp = MPZ_REALLOC (r, alloc);
4504 + rn = mpn_set_str_other (rp, dp, sn, base, &info);
4506 + assert (rn <= alloc);
4509 + r->_mp_size = sign ? - rn : rn;
4515 +mpz_init_set_str (mpz_t r, const char *sp, int base)
4518 + return mpz_set_str (r, sp, base);
4522 +mpz_out_str (FILE *stream, int base, const mpz_t x)
4527 + str = mpz_get_str (NULL, base, x);
4528 + len = strlen (str);
4529 + len = fwrite (str, 1, len, stream);
4536 +gmp_detect_endian (void)
4538 + static const int i = 2;
4539 + const unsigned char *p = (const unsigned char *) &i;
4543 +/* Import and export. Does not support nails. */
4545 +mpz_import (mpz_t r, size_t count, int order, size_t size, int endian,
4546 + size_t nails, const void *src)
4548 + const unsigned char *p;
4549 + ptrdiff_t word_step;
4553 + /* The current (partial) limb. */
4555 + /* The number of bytes already copied to this limb (starting from
4558 + /* The index where the limb should be stored, when completed. */
4562 + gmp_die ("mpz_import: Nails not supported.");
4564 + assert (order == 1 || order == -1);
4565 + assert (endian >= -1 && endian <= 1);
4568 + endian = gmp_detect_endian ();
4570 + p = (unsigned char *) src;
4572 + word_step = (order != endian) ? 2 * size : 0;
4574 + /* Process bytes from the least significant end, so point p at the
4575 + least significant word. */
4578 + p += size * (count - 1);
4579 + word_step = - word_step;
4582 + /* And at least significant byte of that word. */
4586 + rn = (size * count + sizeof(mp_limb_t) - 1) / sizeof(mp_limb_t);
4587 + rp = MPZ_REALLOC (r, rn);
4589 + for (limb = 0, bytes = 0, i = 0; count > 0; count--, p += word_step)
4592 + for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
4594 + limb |= (mp_limb_t) *p << (bytes++ * CHAR_BIT);
4595 + if (bytes == sizeof(mp_limb_t))
4603 + assert (i + (bytes > 0) == rn);
4607 + i = mpn_normalized_size (rp, i);
4613 +mpz_export (void *r, size_t *countp, int order, size_t size, int endian,
4614 + size_t nails, const mpz_t u)
4620 + gmp_die ("mpz_import: Nails not supported.");
4622 + assert (order == 1 || order == -1);
4623 + assert (endian >= -1 && endian <= 1);
4624 + assert (size > 0 || u->_mp_size == 0);
4632 + ptrdiff_t word_step;
4633 + /* The current (partial) limb. */
4635 + /* The number of bytes left to to in this limb. */
4637 + /* The index where the limb was read. */
4640 + un = GMP_ABS (un);
4642 + /* Count bytes in top limb. */
4643 + limb = u->_mp_d[un-1];
4644 + assert (limb != 0);
4648 + k++; limb >>= CHAR_BIT;
4649 + } while (limb != 0);
4651 + count = (k + (un-1) * sizeof (mp_limb_t) + size - 1) / size;
4654 + r = gmp_xalloc (count * size);
4657 + endian = gmp_detect_endian ();
4659 + p = (unsigned char *) r;
4661 + word_step = (order != endian) ? 2 * size : 0;
4663 + /* Process bytes from the least significant end, so point p at the
4664 + least significant word. */
4667 + p += size * (count - 1);
4668 + word_step = - word_step;
4671 + /* And at least significant byte of that word. */
4675 + for (bytes = 0, i = 0, k = 0; k < count; k++, p += word_step)
4678 + for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
4683 + limb = u->_mp_d[i++];
4684 + bytes = sizeof (mp_limb_t);
4687 + limb >>= CHAR_BIT;
4692 + assert (k == count);