301320c906c252085ec0b23016682ba380643e31
[openwrt/staging/pepe2k.git] /
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
5
6 Signed-off-by: Steven Barth <cyrus@openwrt.org>
7 ---
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
13
14 --- /dev/null
15 +++ b/include/mini-gmp.h
16 @@ -0,0 +1,294 @@
17 +/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
18 +
19 +Copyright 2011-2014 Free Software Foundation, Inc.
20 +
21 +This file is part of the GNU MP Library.
22 +
23 +The GNU MP Library is free software; you can redistribute it and/or modify
24 +it under the terms of either:
25 +
26 + * the GNU Lesser General Public License as published by the Free
27 + Software Foundation; either version 3 of the License, or (at your
28 + option) any later version.
29 +
30 +or
31 +
32 + * the GNU General Public License as published by the Free Software
33 + Foundation; either version 2 of the License, or (at your option) any
34 + later version.
35 +
36 +or both in parallel, as here.
37 +
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
41 +for more details.
42 +
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/. */
46 +
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
50 + is not installed.
51 +
52 + This file defines the public interface. */
53 +
54 +#ifndef __MINI_GMP_H__
55 +#define __MINI_GMP_H__
56 +
57 +/* For size_t */
58 +#include <stddef.h>
59 +
60 +#if defined (__cplusplus)
61 +extern "C" {
62 +#endif
63 +
64 +void mp_set_memory_functions (void *(*) (size_t),
65 + void *(*) (void *, size_t, size_t),
66 + void (*) (void *, size_t));
67 +
68 +void mp_get_memory_functions (void *(**) (size_t),
69 + void *(**) (void *, size_t, size_t),
70 + void (**) (void *, size_t));
71 +
72 +typedef unsigned long mp_limb_t;
73 +typedef long mp_size_t;
74 +typedef unsigned long mp_bitcnt_t;
75 +
76 +typedef mp_limb_t *mp_ptr;
77 +typedef const mp_limb_t *mp_srcptr;
78 +
79 +typedef struct
80 +{
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. */
87 +} __mpz_struct;
88 +
89 +typedef __mpz_struct mpz_t[1];
90 +
91 +typedef __mpz_struct *mpz_ptr;
92 +typedef const __mpz_struct *mpz_srcptr;
93 +
94 +extern const int mp_bits_per_limb;
95 +
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);
99 +
100 +int mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t);
101 +
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);
105 +
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);
109 +
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);
113 +
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);
119 +
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);
122 +
123 +mp_bitcnt_t mpn_scan0 (mp_srcptr, mp_bitcnt_t);
124 +mp_bitcnt_t mpn_scan1 (mp_srcptr, mp_bitcnt_t);
125 +
126 +mp_bitcnt_t mpn_popcount (mp_srcptr, mp_size_t);
127 +
128 +mp_limb_t mpn_invert_3by2 (mp_limb_t, mp_limb_t);
129 +#define mpn_invert_limb(x) mpn_invert_3by2 ((x), 0)
130 +
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);
133 +
134 +void mpz_init (mpz_t);
135 +void mpz_init2 (mpz_t, mp_bitcnt_t);
136 +void mpz_clear (mpz_t);
137 +
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))
140 +
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);
149 +
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);
153 +
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);
159 +
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);
168 +
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);
178 +
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);
185 +
186 +void mpz_mod (mpz_t, const mpz_t, const mpz_t);
187 +
188 +void mpz_divexact (mpz_t, const mpz_t, const mpz_t);
189 +
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);
192 +
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);
205 +
206 +unsigned long mpz_mod_ui (mpz_t, const mpz_t, unsigned long);
207 +
208 +void mpz_divexact_ui (mpz_t, const mpz_t, unsigned long);
209 +
210 +int mpz_divisible_ui_p (const mpz_t, unsigned long);
211 +
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);
218 +
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);
222 +
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);
227 +
228 +void mpz_rootrem (mpz_t, mpz_t, const mpz_t, unsigned long);
229 +int mpz_root (mpz_t, const mpz_t, unsigned long);
230 +
231 +void mpz_fac_ui (mpz_t, unsigned long);
232 +void mpz_bin_uiui (mpz_t, unsigned long, unsigned long);
233 +
234 +int mpz_probab_prime_p (const mpz_t, int);
235 +
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);
240 +
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);
245 +
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);
250 +
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);
258 +
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);
265 +
266 +#define MPZ_ROINIT_N(xp, xs) {{0, (xs),(xp) }}
267 +
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);
272 +
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);
277 +
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);
282 +
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);
302 +#endif
303 +
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);
306 +
307 +#if defined (__cplusplus)
308 +}
309 +#endif
310 +#endif /* __MINI_GMP_H__ */
311 --- /dev/null
312 +++ b/src/mini-gmp.c
313 @@ -0,0 +1,4386 @@
314 +/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
315 +
316 + Contributed to the GNU project by Niels Möller
317 +
318 +Copyright 1991-1997, 1999-2014 Free Software Foundation, Inc.
319 +
320 +This file is part of the GNU MP Library.
321 +
322 +The GNU MP Library is free software; you can redistribute it and/or modify
323 +it under the terms of either:
324 +
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.
328 +
329 +or
330 +
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
333 + later version.
334 +
335 +or both in parallel, as here.
336 +
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
340 +for more details.
341 +
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/. */
345 +
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. */
349 +
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. */
356 +
357 +#include <assert.h>
358 +#include <ctype.h>
359 +#include <limits.h>
360 +#include <stdio.h>
361 +#include <stdlib.h>
362 +#include <string.h>
363 +
364 +#include "mini-gmp.h"
365 +
366 +\f
367 +/* Macros */
368 +#define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
369 +
370 +#define GMP_LIMB_MAX (~ (mp_limb_t) 0)
371 +#define GMP_LIMB_HIGHBIT ((mp_limb_t) 1 << (GMP_LIMB_BITS - 1))
372 +
373 +#define GMP_HLIMB_BIT ((mp_limb_t) 1 << (GMP_LIMB_BITS / 2))
374 +#define GMP_LLIMB_MASK (GMP_HLIMB_BIT - 1)
375 +
376 +#define GMP_ULONG_BITS (sizeof(unsigned long) * CHAR_BIT)
377 +#define GMP_ULONG_HIGHBIT ((unsigned long) 1 << (GMP_ULONG_BITS - 1))
378 +
379 +#define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
380 +#define GMP_NEG_CAST(T,x) (-((T)((x) + 1) - 1))
381 +
382 +#define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
383 +#define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
384 +
385 +#define gmp_assert_nocarry(x) do { \
386 + mp_limb_t __cy = x; \
387 + assert (__cy == 0); \
388 + } while (0)
389 +
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; \
395 + __clz_c += 8) \
396 + __clz_x <<= 8; \
397 + for (; (__clz_x & GMP_LIMB_HIGHBIT) == 0; __clz_c++) \
398 + __clz_x <<= 1; \
399 + (count) = __clz_c; \
400 + } while (0)
401 +
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; \
407 + } while (0)
408 +
409 +#define gmp_add_ssaaaa(sh, sl, ah, al, bh, bl) \
410 + do { \
411 + mp_limb_t __x; \
412 + __x = (al) + (bl); \
413 + (sh) = (ah) + (bh) + (__x < (al)); \
414 + (sl) = __x; \
415 + } while (0)
416 +
417 +#define gmp_sub_ddmmss(sh, sl, ah, al, bh, bl) \
418 + do { \
419 + mp_limb_t __x; \
420 + __x = (al) - (bl); \
421 + (sh) = (ah) - (bh) - ((al) < (bl)); \
422 + (sl) = __x; \
423 + } while (0)
424 +
425 +#define gmp_umul_ppmm(w1, w0, u, v) \
426 + do { \
427 + mp_limb_t __x0, __x1, __x2, __x3; \
428 + unsigned __ul, __vl, __uh, __vh; \
429 + mp_limb_t __u = (u), __v = (v); \
430 + \
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); \
435 + \
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; \
440 + \
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. */ \
445 + \
446 + (w1) = __x3 + (__x1 >> (GMP_LIMB_BITS / 2)); \
447 + (w0) = (__x1 << (GMP_LIMB_BITS / 2)) + (__x0 & GMP_LLIMB_MASK); \
448 + } while (0)
449 +
450 +#define gmp_udiv_qrnnd_preinv(q, r, nh, nl, d, di) \
451 + do { \
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 */ \
457 + _qh += _mask; \
458 + _r += _mask & (d); \
459 + if (_r >= (d)) \
460 + { \
461 + _r -= (d); \
462 + _qh++; \
463 + } \
464 + \
465 + (r) = _r; \
466 + (q) = _qh; \
467 + } while (0)
468 +
469 +#define gmp_udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv) \
470 + do { \
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)); \
474 + \
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); \
480 + (q)++; \
481 + \
482 + /* Conditionally adjust q and the remainders */ \
483 + _mask = - (mp_limb_t) ((r1) >= _q0); \
484 + (q) += _mask; \
485 + gmp_add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0)); \
486 + if ((r1) >= (d1)) \
487 + { \
488 + if ((r1) > (d1) || (r0) >= (d0)) \
489 + { \
490 + (q)++; \
491 + gmp_sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0)); \
492 + } \
493 + } \
494 + } while (0)
495 +
496 +/* Swap macros. */
497 +#define MP_LIMB_T_SWAP(x, y) \
498 + do { \
499 + mp_limb_t __mp_limb_t_swap__tmp = (x); \
500 + (x) = (y); \
501 + (y) = __mp_limb_t_swap__tmp; \
502 + } while (0)
503 +#define MP_SIZE_T_SWAP(x, y) \
504 + do { \
505 + mp_size_t __mp_size_t_swap__tmp = (x); \
506 + (x) = (y); \
507 + (y) = __mp_size_t_swap__tmp; \
508 + } while (0)
509 +#define MP_BITCNT_T_SWAP(x,y) \
510 + do { \
511 + mp_bitcnt_t __mp_bitcnt_t_swap__tmp = (x); \
512 + (x) = (y); \
513 + (y) = __mp_bitcnt_t_swap__tmp; \
514 + } while (0)
515 +#define MP_PTR_SWAP(x, y) \
516 + do { \
517 + mp_ptr __mp_ptr_swap__tmp = (x); \
518 + (x) = (y); \
519 + (y) = __mp_ptr_swap__tmp; \
520 + } while (0)
521 +#define MP_SRCPTR_SWAP(x, y) \
522 + do { \
523 + mp_srcptr __mp_srcptr_swap__tmp = (x); \
524 + (x) = (y); \
525 + (y) = __mp_srcptr_swap__tmp; \
526 + } while (0)
527 +
528 +#define MPN_PTR_SWAP(xp,xs, yp,ys) \
529 + do { \
530 + MP_PTR_SWAP (xp, yp); \
531 + MP_SIZE_T_SWAP (xs, ys); \
532 + } while(0)
533 +#define MPN_SRCPTR_SWAP(xp,xs, yp,ys) \
534 + do { \
535 + MP_SRCPTR_SWAP (xp, yp); \
536 + MP_SIZE_T_SWAP (xs, ys); \
537 + } while(0)
538 +
539 +#define MPZ_PTR_SWAP(x, y) \
540 + do { \
541 + mpz_ptr __mpz_ptr_swap__tmp = (x); \
542 + (x) = (y); \
543 + (y) = __mpz_ptr_swap__tmp; \
544 + } while (0)
545 +#define MPZ_SRCPTR_SWAP(x, y) \
546 + do { \
547 + mpz_srcptr __mpz_srcptr_swap__tmp = (x); \
548 + (x) = (y); \
549 + (y) = __mpz_srcptr_swap__tmp; \
550 + } while (0)
551 +
552 +const int mp_bits_per_limb = GMP_LIMB_BITS;
553 +
554 +\f
555 +/* Memory allocation and other helper functions. */
556 +static void
557 +gmp_die (const char *msg)
558 +{
559 + fprintf (stderr, "%s\n", msg);
560 + abort();
561 +}
562 +
563 +static void *
564 +gmp_default_alloc (size_t size)
565 +{
566 + void *p;
567 +
568 + assert (size > 0);
569 +
570 + p = malloc (size);
571 + if (!p)
572 + gmp_die("gmp_default_alloc: Virtual memory exhausted.");
573 +
574 + return p;
575 +}
576 +
577 +static void *
578 +gmp_default_realloc (void *old, size_t old_size, size_t new_size)
579 +{
580 + mp_ptr p;
581 +
582 + p = realloc (old, new_size);
583 +
584 + if (!p)
585 + gmp_die("gmp_default_realoc: Virtual memory exhausted.");
586 +
587 + return p;
588 +}
589 +
590 +static void
591 +gmp_default_free (void *p, size_t size)
592 +{
593 + free (p);
594 +}
595 +
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;
599 +
600 +void
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))
604 +{
605 + if (alloc_func)
606 + *alloc_func = gmp_allocate_func;
607 +
608 + if (realloc_func)
609 + *realloc_func = gmp_reallocate_func;
610 +
611 + if (free_func)
612 + *free_func = gmp_free_func;
613 +}
614 +
615 +void
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))
619 +{
620 + if (!alloc_func)
621 + alloc_func = gmp_default_alloc;
622 + if (!realloc_func)
623 + realloc_func = gmp_default_realloc;
624 + if (!free_func)
625 + free_func = gmp_default_free;
626 +
627 + gmp_allocate_func = alloc_func;
628 + gmp_reallocate_func = realloc_func;
629 + gmp_free_func = free_func;
630 +}
631 +
632 +#define gmp_xalloc(size) ((*gmp_allocate_func)((size)))
633 +#define gmp_free(p) ((*gmp_free_func) ((p), 0))
634 +
635 +static mp_ptr
636 +gmp_xalloc_limbs (mp_size_t size)
637 +{
638 + return gmp_xalloc (size * sizeof (mp_limb_t));
639 +}
640 +
641 +static mp_ptr
642 +gmp_xrealloc_limbs (mp_ptr old, mp_size_t size)
643 +{
644 + assert (size > 0);
645 + return (*gmp_reallocate_func) (old, 0, size * sizeof (mp_limb_t));
646 +}
647 +
648 +\f
649 +/* MPN interface */
650 +
651 +void
652 +mpn_copyi (mp_ptr d, mp_srcptr s, mp_size_t n)
653 +{
654 + mp_size_t i;
655 + for (i = 0; i < n; i++)
656 + d[i] = s[i];
657 +}
658 +
659 +void
660 +mpn_copyd (mp_ptr d, mp_srcptr s, mp_size_t n)
661 +{
662 + while (n-- > 0)
663 + d[n] = s[n];
664 +}
665 +
666 +int
667 +mpn_cmp (mp_srcptr ap, mp_srcptr bp, mp_size_t n)
668 +{
669 + while (--n >= 0)
670 + {
671 + if (ap[n] != bp[n])
672 + return ap[n] > bp[n] ? 1 : -1;
673 + }
674 + return 0;
675 +}
676 +
677 +static int
678 +mpn_cmp4 (mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
679 +{
680 + if (an != bn)
681 + return an < bn ? -1 : 1;
682 + else
683 + return mpn_cmp (ap, bp, an);
684 +}
685 +
686 +static mp_size_t
687 +mpn_normalized_size (mp_srcptr xp, mp_size_t n)
688 +{
689 + for (; n > 0 && xp[n-1] == 0; n--)
690 + ;
691 + return n;
692 +}
693 +
694 +#define mpn_zero_p(xp, n) (mpn_normalized_size ((xp), (n)) == 0)
695 +
696 +void
697 +mpn_zero (mp_ptr rp, mp_size_t n)
698 +{
699 + mp_size_t i;
700 +
701 + for (i = 0; i < n; i++)
702 + rp[i] = 0;
703 +}
704 +
705 +mp_limb_t
706 +mpn_add_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
707 +{
708 + mp_size_t i;
709 +
710 + assert (n > 0);
711 + i = 0;
712 + do
713 + {
714 + mp_limb_t r = ap[i] + b;
715 + /* Carry out */
716 + b = (r < b);
717 + rp[i] = r;
718 + }
719 + while (++i < n);
720 +
721 + return b;
722 +}
723 +
724 +mp_limb_t
725 +mpn_add_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
726 +{
727 + mp_size_t i;
728 + mp_limb_t cy;
729 +
730 + for (i = 0, cy = 0; i < n; i++)
731 + {
732 + mp_limb_t a, b, r;
733 + a = ap[i]; b = bp[i];
734 + r = a + cy;
735 + cy = (r < cy);
736 + r += b;
737 + cy += (r < b);
738 + rp[i] = r;
739 + }
740 + return cy;
741 +}
742 +
743 +mp_limb_t
744 +mpn_add (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
745 +{
746 + mp_limb_t cy;
747 +
748 + assert (an >= bn);
749 +
750 + cy = mpn_add_n (rp, ap, bp, bn);
751 + if (an > bn)
752 + cy = mpn_add_1 (rp + bn, ap + bn, an - bn, cy);
753 + return cy;
754 +}
755 +
756 +mp_limb_t
757 +mpn_sub_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
758 +{
759 + mp_size_t i;
760 +
761 + assert (n > 0);
762 +
763 + i = 0;
764 + do
765 + {
766 + mp_limb_t a = ap[i];
767 + /* Carry out */
768 + mp_limb_t cy = a < b;;
769 + rp[i] = a - b;
770 + b = cy;
771 + }
772 + while (++i < n);
773 +
774 + return b;
775 +}
776 +
777 +mp_limb_t
778 +mpn_sub_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
779 +{
780 + mp_size_t i;
781 + mp_limb_t cy;
782 +
783 + for (i = 0, cy = 0; i < n; i++)
784 + {
785 + mp_limb_t a, b;
786 + a = ap[i]; b = bp[i];
787 + b += cy;
788 + cy = (b < cy);
789 + cy += (a < b);
790 + rp[i] = a - b;
791 + }
792 + return cy;
793 +}
794 +
795 +mp_limb_t
796 +mpn_sub (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
797 +{
798 + mp_limb_t cy;
799 +
800 + assert (an >= bn);
801 +
802 + cy = mpn_sub_n (rp, ap, bp, bn);
803 + if (an > bn)
804 + cy = mpn_sub_1 (rp + bn, ap + bn, an - bn, cy);
805 + return cy;
806 +}
807 +
808 +mp_limb_t
809 +mpn_mul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
810 +{
811 + mp_limb_t ul, cl, hpl, lpl;
812 +
813 + assert (n >= 1);
814 +
815 + cl = 0;
816 + do
817 + {
818 + ul = *up++;
819 + gmp_umul_ppmm (hpl, lpl, ul, vl);
820 +
821 + lpl += cl;
822 + cl = (lpl < cl) + hpl;
823 +
824 + *rp++ = lpl;
825 + }
826 + while (--n != 0);
827 +
828 + return cl;
829 +}
830 +
831 +mp_limb_t
832 +mpn_addmul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
833 +{
834 + mp_limb_t ul, cl, hpl, lpl, rl;
835 +
836 + assert (n >= 1);
837 +
838 + cl = 0;
839 + do
840 + {
841 + ul = *up++;
842 + gmp_umul_ppmm (hpl, lpl, ul, vl);
843 +
844 + lpl += cl;
845 + cl = (lpl < cl) + hpl;
846 +
847 + rl = *rp;
848 + lpl = rl + lpl;
849 + cl += lpl < rl;
850 + *rp++ = lpl;
851 + }
852 + while (--n != 0);
853 +
854 + return cl;
855 +}
856 +
857 +mp_limb_t
858 +mpn_submul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
859 +{
860 + mp_limb_t ul, cl, hpl, lpl, rl;
861 +
862 + assert (n >= 1);
863 +
864 + cl = 0;
865 + do
866 + {
867 + ul = *up++;
868 + gmp_umul_ppmm (hpl, lpl, ul, vl);
869 +
870 + lpl += cl;
871 + cl = (lpl < cl) + hpl;
872 +
873 + rl = *rp;
874 + lpl = rl - lpl;
875 + cl += lpl > rl;
876 + *rp++ = lpl;
877 + }
878 + while (--n != 0);
879 +
880 + return cl;
881 +}
882 +
883 +mp_limb_t
884 +mpn_mul (mp_ptr rp, mp_srcptr up, mp_size_t un, mp_srcptr vp, mp_size_t vn)
885 +{
886 + assert (un >= vn);
887 + assert (vn >= 1);
888 +
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
891 + way. */
892 +
893 + rp[un] = mpn_mul_1 (rp, up, un, vp[0]);
894 + rp += 1, vp += 1, vn -= 1;
895 +
896 + /* Now accumulate the product of up[] and the next higher limb from
897 + vp[]. */
898 +
899 + while (vn >= 1)
900 + {
901 + rp[un] = mpn_addmul_1 (rp, up, un, vp[0]);
902 + rp += 1, vp += 1, vn -= 1;
903 + }
904 + return rp[un - 1];
905 +}
906 +
907 +void
908 +mpn_mul_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
909 +{
910 + mpn_mul (rp, ap, n, bp, n);
911 +}
912 +
913 +void
914 +mpn_sqr (mp_ptr rp, mp_srcptr ap, mp_size_t n)
915 +{
916 + mpn_mul (rp, ap, n, ap, n);
917 +}
918 +
919 +mp_limb_t
920 +mpn_lshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
921 +{
922 + mp_limb_t high_limb, low_limb;
923 + unsigned int tnc;
924 + mp_size_t i;
925 + mp_limb_t retval;
926 +
927 + assert (n >= 1);
928 + assert (cnt >= 1);
929 + assert (cnt < GMP_LIMB_BITS);
930 +
931 + up += n;
932 + rp += n;
933 +
934 + tnc = GMP_LIMB_BITS - cnt;
935 + low_limb = *--up;
936 + retval = low_limb >> tnc;
937 + high_limb = (low_limb << cnt);
938 +
939 + for (i = n; --i != 0;)
940 + {
941 + low_limb = *--up;
942 + *--rp = high_limb | (low_limb >> tnc);
943 + high_limb = (low_limb << cnt);
944 + }
945 + *--rp = high_limb;
946 +
947 + return retval;
948 +}
949 +
950 +mp_limb_t
951 +mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
952 +{
953 + mp_limb_t high_limb, low_limb;
954 + unsigned int tnc;
955 + mp_size_t i;
956 + mp_limb_t retval;
957 +
958 + assert (n >= 1);
959 + assert (cnt >= 1);
960 + assert (cnt < GMP_LIMB_BITS);
961 +
962 + tnc = GMP_LIMB_BITS - cnt;
963 + high_limb = *up++;
964 + retval = (high_limb << tnc);
965 + low_limb = high_limb >> cnt;
966 +
967 + for (i = n; --i != 0;)
968 + {
969 + high_limb = *up++;
970 + *rp++ = low_limb | (high_limb << tnc);
971 + low_limb = high_limb >> cnt;
972 + }
973 + *rp = low_limb;
974 +
975 + return retval;
976 +}
977 +
978 +static mp_bitcnt_t
979 +mpn_common_scan (mp_limb_t limb, mp_size_t i, mp_srcptr up, mp_size_t un,
980 + mp_limb_t ux)
981 +{
982 + unsigned cnt;
983 +
984 + assert (ux == 0 || ux == GMP_LIMB_MAX);
985 + assert (0 <= i && i <= un );
986 +
987 + while (limb == 0)
988 + {
989 + i++;
990 + if (i == un)
991 + return (ux == 0 ? ~(mp_bitcnt_t) 0 : un * GMP_LIMB_BITS);
992 + limb = ux ^ up[i];
993 + }
994 + gmp_ctz (cnt, limb);
995 + return (mp_bitcnt_t) i * GMP_LIMB_BITS + cnt;
996 +}
997 +
998 +mp_bitcnt_t
999 +mpn_scan1 (mp_srcptr ptr, mp_bitcnt_t bit)
1000 +{
1001 + mp_size_t i;
1002 + i = bit / GMP_LIMB_BITS;
1003 +
1004 + return mpn_common_scan ( ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
1005 + i, ptr, i, 0);
1006 +}
1007 +
1008 +mp_bitcnt_t
1009 +mpn_scan0 (mp_srcptr ptr, mp_bitcnt_t bit)
1010 +{
1011 + mp_size_t i;
1012 + i = bit / GMP_LIMB_BITS;
1013 +
1014 + return mpn_common_scan (~ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
1015 + i, ptr, i, GMP_LIMB_MAX);
1016 +}
1017 +
1018 +\f
1019 +/* MPN division interface. */
1020 +mp_limb_t
1021 +mpn_invert_3by2 (mp_limb_t u1, mp_limb_t u0)
1022 +{
1023 + mp_limb_t r, p, m;
1024 + unsigned ul, uh;
1025 + unsigned ql, qh;
1026 +
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);
1031 +
1032 + ul = u1 & GMP_LLIMB_MASK;
1033 + uh = u1 >> (GMP_LIMB_BITS / 2);
1034 +
1035 + qh = ~u1 / uh;
1036 + r = ((~u1 - (mp_limb_t) qh * uh) << (GMP_LIMB_BITS / 2)) | GMP_LLIMB_MASK;
1037 +
1038 + p = (mp_limb_t) qh * ul;
1039 + /* Adjustment steps taken from udiv_qrnnd_c */
1040 + if (r < p)
1041 + {
1042 + qh--;
1043 + r += u1;
1044 + if (r >= u1) /* i.e. we didn't get carry when adding to r */
1045 + if (r < p)
1046 + {
1047 + qh--;
1048 + r += u1;
1049 + }
1050 + }
1051 + r -= p;
1052 +
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;
1056 +
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;
1059 +
1060 + if (r >= (p << (GMP_LIMB_BITS / 2)))
1061 + {
1062 + ql--;
1063 + r += u1;
1064 + }
1065 + m = ((mp_limb_t) qh << (GMP_LIMB_BITS / 2)) + ql;
1066 + if (r >= u1)
1067 + {
1068 + m++;
1069 + r -= u1;
1070 + }
1071 +
1072 + if (u0 > 0)
1073 + {
1074 + mp_limb_t th, tl;
1075 + r = ~r;
1076 + r += u0;
1077 + if (r < u0)
1078 + {
1079 + m--;
1080 + if (r >= u1)
1081 + {
1082 + m--;
1083 + r -= u1;
1084 + }
1085 + r -= u1;
1086 + }
1087 + gmp_umul_ppmm (th, tl, u0, m);
1088 + r += th;
1089 + if (r < th)
1090 + {
1091 + m--;
1092 + m -= ((r > u1) | ((r == u1) & (tl > u0)));
1093 + }
1094 + }
1095 +
1096 + return m;
1097 +}
1098 +
1099 +struct gmp_div_inverse
1100 +{
1101 + /* Normalization shift count. */
1102 + unsigned shift;
1103 + /* Normalized divisor (d0 unused for mpn_div_qr_1) */
1104 + mp_limb_t d1, d0;
1105 + /* Inverse, for 2/1 or 3/2. */
1106 + mp_limb_t di;
1107 +};
1108 +
1109 +static void
1110 +mpn_div_qr_1_invert (struct gmp_div_inverse *inv, mp_limb_t d)
1111 +{
1112 + unsigned shift;
1113 +
1114 + assert (d > 0);
1115 + gmp_clz (shift, d);
1116 + inv->shift = shift;
1117 + inv->d1 = d << shift;
1118 + inv->di = mpn_invert_limb (inv->d1);
1119 +}
1120 +
1121 +static void
1122 +mpn_div_qr_2_invert (struct gmp_div_inverse *inv,
1123 + mp_limb_t d1, mp_limb_t d0)
1124 +{
1125 + unsigned shift;
1126 +
1127 + assert (d1 > 0);
1128 + gmp_clz (shift, d1);
1129 + inv->shift = shift;
1130 + if (shift > 0)
1131 + {
1132 + d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
1133 + d0 <<= shift;
1134 + }
1135 + inv->d1 = d1;
1136 + inv->d0 = d0;
1137 + inv->di = mpn_invert_3by2 (d1, d0);
1138 +}
1139 +
1140 +static void
1141 +mpn_div_qr_invert (struct gmp_div_inverse *inv,
1142 + mp_srcptr dp, mp_size_t dn)
1143 +{
1144 + assert (dn > 0);
1145 +
1146 + if (dn == 1)
1147 + mpn_div_qr_1_invert (inv, dp[0]);
1148 + else if (dn == 2)
1149 + mpn_div_qr_2_invert (inv, dp[1], dp[0]);
1150 + else
1151 + {
1152 + unsigned shift;
1153 + mp_limb_t d1, d0;
1154 +
1155 + d1 = dp[dn-1];
1156 + d0 = dp[dn-2];
1157 + assert (d1 > 0);
1158 + gmp_clz (shift, d1);
1159 + inv->shift = shift;
1160 + if (shift > 0)
1161 + {
1162 + d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
1163 + d0 = (d0 << shift) | (dp[dn-3] >> (GMP_LIMB_BITS - shift));
1164 + }
1165 + inv->d1 = d1;
1166 + inv->d0 = d0;
1167 + inv->di = mpn_invert_3by2 (d1, d0);
1168 + }
1169 +}
1170 +
1171 +/* Not matching current public gmp interface, rather corresponding to
1172 + the sbpi1_div_* functions. */
1173 +static mp_limb_t
1174 +mpn_div_qr_1_preinv (mp_ptr qp, mp_srcptr np, mp_size_t nn,
1175 + const struct gmp_div_inverse *inv)
1176 +{
1177 + mp_limb_t d, di;
1178 + mp_limb_t r;
1179 + mp_ptr tp = NULL;
1180 +
1181 + if (inv->shift > 0)
1182 + {
1183 + tp = gmp_xalloc_limbs (nn);
1184 + r = mpn_lshift (tp, np, nn, inv->shift);
1185 + np = tp;
1186 + }
1187 + else
1188 + r = 0;
1189 +
1190 + d = inv->d1;
1191 + di = inv->di;
1192 + while (nn-- > 0)
1193 + {
1194 + mp_limb_t q;
1195 +
1196 + gmp_udiv_qrnnd_preinv (q, r, r, np[nn], d, di);
1197 + if (qp)
1198 + qp[nn] = q;
1199 + }
1200 + if (inv->shift > 0)
1201 + gmp_free (tp);
1202 +
1203 + return r >> inv->shift;
1204 +}
1205 +
1206 +static mp_limb_t
1207 +mpn_div_qr_1 (mp_ptr qp, mp_srcptr np, mp_size_t nn, mp_limb_t d)
1208 +{
1209 + assert (d > 0);
1210 +
1211 + /* Special case for powers of two. */
1212 + if ((d & (d-1)) == 0)
1213 + {
1214 + mp_limb_t r = np[0] & (d-1);
1215 + if (qp)
1216 + {
1217 + if (d <= 1)
1218 + mpn_copyi (qp, np, nn);
1219 + else
1220 + {
1221 + unsigned shift;
1222 + gmp_ctz (shift, d);
1223 + mpn_rshift (qp, np, nn, shift);
1224 + }
1225 + }
1226 + return r;
1227 + }
1228 + else
1229 + {
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);
1233 + }
1234 +}
1235 +
1236 +static void
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)
1239 +{
1240 + unsigned shift;
1241 + mp_size_t i;
1242 + mp_limb_t d1, d0, di, r1, r0;
1243 + mp_ptr tp;
1244 +
1245 + assert (nn >= 2);
1246 + shift = inv->shift;
1247 + d1 = inv->d1;
1248 + d0 = inv->d0;
1249 + di = inv->di;
1250 +
1251 + if (shift > 0)
1252 + {
1253 + tp = gmp_xalloc_limbs (nn);
1254 + r1 = mpn_lshift (tp, np, nn, shift);
1255 + np = tp;
1256 + }
1257 + else
1258 + r1 = 0;
1259 +
1260 + r0 = np[nn - 1];
1261 +
1262 + i = nn - 2;
1263 + do
1264 + {
1265 + mp_limb_t n0, q;
1266 + n0 = np[i];
1267 + gmp_udiv_qr_3by2 (q, r1, r0, r1, r0, n0, d1, d0, di);
1268 +
1269 + if (qp)
1270 + qp[i] = q;
1271 + }
1272 + while (--i >= 0);
1273 +
1274 + if (shift > 0)
1275 + {
1276 + assert ((r0 << (GMP_LIMB_BITS - shift)) == 0);
1277 + r0 = (r0 >> shift) | (r1 << (GMP_LIMB_BITS - shift));
1278 + r1 >>= shift;
1279 +
1280 + gmp_free (tp);
1281 + }
1282 +
1283 + rp[1] = r1;
1284 + rp[0] = r0;
1285 +}
1286 +
1287 +#if 0
1288 +static void
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)
1291 +{
1292 + struct gmp_div_inverse inv;
1293 + assert (nn >= 2);
1294 +
1295 + mpn_div_qr_2_invert (&inv, d1, d0);
1296 + mpn_div_qr_2_preinv (qp, rp, np, nn, &inv);
1297 +}
1298 +#endif
1299 +
1300 +static void
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,
1304 + mp_limb_t dinv)
1305 +{
1306 + mp_size_t i;
1307 +
1308 + mp_limb_t d1, d0;
1309 + mp_limb_t cy, cy1;
1310 + mp_limb_t q;
1311 +
1312 + assert (dn > 2);
1313 + assert (nn >= dn);
1314 +
1315 + d1 = dp[dn - 1];
1316 + d0 = dp[dn - 2];
1317 +
1318 + assert ((d1 & GMP_LIMB_HIGHBIT) != 0);
1319 + /* Iteration variable is the index of the q limb.
1320 + *
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] >
1323 + */
1324 +
1325 + i = nn - dn;
1326 + do
1327 + {
1328 + mp_limb_t n0 = np[dn-1+i];
1329 +
1330 + if (n1 == d1 && n0 == d0)
1331 + {
1332 + q = GMP_LIMB_MAX;
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 */
1335 + }
1336 + else
1337 + {
1338 + gmp_udiv_qr_3by2 (q, n1, n0, n1, n0, np[dn-2+i], d1, d0, dinv);
1339 +
1340 + cy = mpn_submul_1 (np + i, dp, dn-2, q);
1341 +
1342 + cy1 = n0 < cy;
1343 + n0 = n0 - cy;
1344 + cy = n1 < cy1;
1345 + n1 = n1 - cy1;
1346 + np[dn-2+i] = n0;
1347 +
1348 + if (cy != 0)
1349 + {
1350 + n1 += d1 + mpn_add_n (np + i, np + i, dp, dn - 1);
1351 + q--;
1352 + }
1353 + }
1354 +
1355 + if (qp)
1356 + qp[i] = q;
1357 + }
1358 + while (--i >= 0);
1359 +
1360 + np[dn - 1] = n1;
1361 +}
1362 +
1363 +static void
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)
1367 +{
1368 + assert (dn > 0);
1369 + assert (nn >= dn);
1370 +
1371 + if (dn == 1)
1372 + np[0] = mpn_div_qr_1_preinv (qp, np, nn, inv);
1373 + else if (dn == 2)
1374 + mpn_div_qr_2_preinv (qp, np, np, nn, inv);
1375 + else
1376 + {
1377 + mp_limb_t nh;
1378 + unsigned shift;
1379 +
1380 + assert (inv->d1 == dp[dn-1]);
1381 + assert (inv->d0 == dp[dn-2]);
1382 + assert ((inv->d1 & GMP_LIMB_HIGHBIT) != 0);
1383 +
1384 + shift = inv->shift;
1385 + if (shift > 0)
1386 + nh = mpn_lshift (np, np, nn, shift);
1387 + else
1388 + nh = 0;
1389 +
1390 + mpn_div_qr_pi1 (qp, np, nn, nh, dp, dn, inv->di);
1391 +
1392 + if (shift > 0)
1393 + gmp_assert_nocarry (mpn_rshift (np, np, dn, shift));
1394 + }
1395 +}
1396 +
1397 +static void
1398 +mpn_div_qr (mp_ptr qp, mp_ptr np, mp_size_t nn, mp_srcptr dp, mp_size_t dn)
1399 +{
1400 + struct gmp_div_inverse inv;
1401 + mp_ptr tp = NULL;
1402 +
1403 + assert (dn > 0);
1404 + assert (nn >= dn);
1405 +
1406 + mpn_div_qr_invert (&inv, dp, dn);
1407 + if (dn > 2 && inv.shift > 0)
1408 + {
1409 + tp = gmp_xalloc_limbs (dn);
1410 + gmp_assert_nocarry (mpn_lshift (tp, dp, dn, inv.shift));
1411 + dp = tp;
1412 + }
1413 + mpn_div_qr_preinv (qp, np, nn, dp, dn, &inv);
1414 + if (tp)
1415 + gmp_free (tp);
1416 +}
1417 +
1418 +\f
1419 +/* MPN base conversion. */
1420 +static unsigned
1421 +mpn_base_power_of_two_p (unsigned b)
1422 +{
1423 + switch (b)
1424 + {
1425 + case 2: return 1;
1426 + case 4: return 2;
1427 + case 8: return 3;
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;
1434 + }
1435 +}
1436 +
1437 +struct mpn_base_info
1438 +{
1439 + /* bb is the largest power of the base which fits in one limb, and
1440 + exp is the corresponding exponent. */
1441 + unsigned exp;
1442 + mp_limb_t bb;
1443 +};
1444 +
1445 +static void
1446 +mpn_get_base_info (struct mpn_base_info *info, mp_limb_t b)
1447 +{
1448 + mp_limb_t m;
1449 + mp_limb_t p;
1450 + unsigned exp;
1451 +
1452 + m = GMP_LIMB_MAX / b;
1453 + for (exp = 1, p = b; p <= m; exp++)
1454 + p *= b;
1455 +
1456 + info->exp = exp;
1457 + info->bb = p;
1458 +}
1459 +
1460 +static mp_bitcnt_t
1461 +mpn_limb_size_in_base_2 (mp_limb_t u)
1462 +{
1463 + unsigned shift;
1464 +
1465 + assert (u > 0);
1466 + gmp_clz (shift, u);
1467 + return GMP_LIMB_BITS - shift;
1468 +}
1469 +
1470 +static size_t
1471 +mpn_get_str_bits (unsigned char *sp, unsigned bits, mp_srcptr up, mp_size_t un)
1472 +{
1473 + unsigned char mask;
1474 + size_t sn, j;
1475 + mp_size_t i;
1476 + int shift;
1477 +
1478 + sn = ((un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1])
1479 + + bits - 1) / bits;
1480 +
1481 + mask = (1U << bits) - 1;
1482 +
1483 + for (i = 0, j = sn, shift = 0; j-- > 0;)
1484 + {
1485 + unsigned char digit = up[i] >> shift;
1486 +
1487 + shift += bits;
1488 +
1489 + if (shift >= GMP_LIMB_BITS && ++i < un)
1490 + {
1491 + shift -= GMP_LIMB_BITS;
1492 + digit |= up[i] << (bits - shift);
1493 + }
1494 + sp[j] = digit & mask;
1495 + }
1496 + return sn;
1497 +}
1498 +
1499 +/* We generate digits from the least significant end, and reverse at
1500 + the end. */
1501 +static size_t
1502 +mpn_limb_get_str (unsigned char *sp, mp_limb_t w,
1503 + const struct gmp_div_inverse *binv)
1504 +{
1505 + mp_size_t i;
1506 + for (i = 0; w > 0; i++)
1507 + {
1508 + mp_limb_t h, l, r;
1509 +
1510 + h = w >> (GMP_LIMB_BITS - binv->shift);
1511 + l = w << binv->shift;
1512 +
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;
1516 +
1517 + sp[i] = r;
1518 + }
1519 + return i;
1520 +}
1521 +
1522 +static size_t
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)
1526 +{
1527 + struct gmp_div_inverse binv;
1528 + size_t sn;
1529 + size_t i;
1530 +
1531 + mpn_div_qr_1_invert (&binv, base);
1532 +
1533 + sn = 0;
1534 +
1535 + if (un > 1)
1536 + {
1537 + struct gmp_div_inverse bbinv;
1538 + mpn_div_qr_1_invert (&bbinv, info->bb);
1539 +
1540 + do
1541 + {
1542 + mp_limb_t w;
1543 + size_t done;
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);
1547 +
1548 + for (sn += done; done < info->exp; done++)
1549 + sp[sn++] = 0;
1550 + }
1551 + while (un > 1);
1552 + }
1553 + sn += mpn_limb_get_str (sp + sn, up[0], &binv);
1554 +
1555 + /* Reverse order */
1556 + for (i = 0; 2*i + 1 < sn; i++)
1557 + {
1558 + unsigned char t = sp[i];
1559 + sp[i] = sp[sn - i - 1];
1560 + sp[sn - i - 1] = t;
1561 + }
1562 +
1563 + return sn;
1564 +}
1565 +
1566 +size_t
1567 +mpn_get_str (unsigned char *sp, int base, mp_ptr up, mp_size_t un)
1568 +{
1569 + unsigned bits;
1570 +
1571 + assert (un > 0);
1572 + assert (up[un-1] > 0);
1573 +
1574 + bits = mpn_base_power_of_two_p (base);
1575 + if (bits)
1576 + return mpn_get_str_bits (sp, bits, up, un);
1577 + else
1578 + {
1579 + struct mpn_base_info info;
1580 +
1581 + mpn_get_base_info (&info, base);
1582 + return mpn_get_str_other (sp, base, &info, up, un);
1583 + }
1584 +}
1585 +
1586 +static mp_size_t
1587 +mpn_set_str_bits (mp_ptr rp, const unsigned char *sp, size_t sn,
1588 + unsigned bits)
1589 +{
1590 + mp_size_t rn;
1591 + size_t j;
1592 + unsigned shift;
1593 +
1594 + for (j = sn, rn = 0, shift = 0; j-- > 0; )
1595 + {
1596 + if (shift == 0)
1597 + {
1598 + rp[rn++] = sp[j];
1599 + shift += bits;
1600 + }
1601 + else
1602 + {
1603 + rp[rn-1] |= (mp_limb_t) sp[j] << shift;
1604 + shift += bits;
1605 + if (shift >= GMP_LIMB_BITS)
1606 + {
1607 + shift -= GMP_LIMB_BITS;
1608 + if (shift > 0)
1609 + rp[rn++] = (mp_limb_t) sp[j] >> (bits - shift);
1610 + }
1611 + }
1612 + }
1613 + rn = mpn_normalized_size (rp, rn);
1614 + return rn;
1615 +}
1616 +
1617 +static mp_size_t
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)
1620 +{
1621 + mp_size_t rn;
1622 + mp_limb_t w;
1623 + unsigned k;
1624 + size_t j;
1625 +
1626 + k = 1 + (sn - 1) % info->exp;
1627 +
1628 + j = 0;
1629 + w = sp[j++];
1630 + for (; --k > 0; )
1631 + w = w * b + sp[j++];
1632 +
1633 + rp[0] = w;
1634 +
1635 + for (rn = (w > 0); j < sn;)
1636 + {
1637 + mp_limb_t cy;
1638 +
1639 + w = sp[j++];
1640 + for (k = 1; k < info->exp; k++)
1641 + w = w * b + sp[j++];
1642 +
1643 + cy = mpn_mul_1 (rp, rp, rn, info->bb);
1644 + cy += mpn_add_1 (rp, rp, rn, w);
1645 + if (cy > 0)
1646 + rp[rn++] = cy;
1647 + }
1648 + assert (j == sn);
1649 +
1650 + return rn;
1651 +}
1652 +
1653 +mp_size_t
1654 +mpn_set_str (mp_ptr rp, const unsigned char *sp, size_t sn, int base)
1655 +{
1656 + unsigned bits;
1657 +
1658 + if (sn == 0)
1659 + return 0;
1660 +
1661 + bits = mpn_base_power_of_two_p (base);
1662 + if (bits)
1663 + return mpn_set_str_bits (rp, sp, sn, bits);
1664 + else
1665 + {
1666 + struct mpn_base_info info;
1667 +
1668 + mpn_get_base_info (&info, base);
1669 + return mpn_set_str_other (rp, sp, sn, base, &info);
1670 + }
1671 +}
1672 +
1673 +\f
1674 +/* MPZ interface */
1675 +void
1676 +mpz_init (mpz_t r)
1677 +{
1678 + r->_mp_alloc = 1;
1679 + r->_mp_size = 0;
1680 + r->_mp_d = gmp_xalloc_limbs (1);
1681 +}
1682 +
1683 +/* The utility of this function is a bit limited, since many functions
1684 + assigns the result variable using mpz_swap. */
1685 +void
1686 +mpz_init2 (mpz_t r, mp_bitcnt_t bits)
1687 +{
1688 + mp_size_t rn;
1689 +
1690 + bits -= (bits != 0); /* Round down, except if 0 */
1691 + rn = 1 + bits / GMP_LIMB_BITS;
1692 +
1693 + r->_mp_alloc = rn;
1694 + r->_mp_size = 0;
1695 + r->_mp_d = gmp_xalloc_limbs (rn);
1696 +}
1697 +
1698 +void
1699 +mpz_clear (mpz_t r)
1700 +{
1701 + gmp_free (r->_mp_d);
1702 +}
1703 +
1704 +static void *
1705 +mpz_realloc (mpz_t r, mp_size_t size)
1706 +{
1707 + size = GMP_MAX (size, 1);
1708 +
1709 + r->_mp_d = gmp_xrealloc_limbs (r->_mp_d, size);
1710 + r->_mp_alloc = size;
1711 +
1712 + if (GMP_ABS (r->_mp_size) > size)
1713 + r->_mp_size = 0;
1714 +
1715 + return r->_mp_d;
1716 +}
1717 +
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) \
1721 + : (z)->_mp_d)
1722 +\f
1723 +/* MPZ assignment and basic conversions. */
1724 +void
1725 +mpz_set_si (mpz_t r, signed long int x)
1726 +{
1727 + if (x >= 0)
1728 + mpz_set_ui (r, x);
1729 + else /* (x < 0) */
1730 + {
1731 + r->_mp_size = -1;
1732 + r->_mp_d[0] = GMP_NEG_CAST (unsigned long int, x);
1733 + }
1734 +}
1735 +
1736 +void
1737 +mpz_set_ui (mpz_t r, unsigned long int x)
1738 +{
1739 + if (x > 0)
1740 + {
1741 + r->_mp_size = 1;
1742 + r->_mp_d[0] = x;
1743 + }
1744 + else
1745 + r->_mp_size = 0;
1746 +}
1747 +
1748 +void
1749 +mpz_set (mpz_t r, const mpz_t x)
1750 +{
1751 + /* Allow the NOP r == x */
1752 + if (r != x)
1753 + {
1754 + mp_size_t n;
1755 + mp_ptr rp;
1756 +
1757 + n = GMP_ABS (x->_mp_size);
1758 + rp = MPZ_REALLOC (r, n);
1759 +
1760 + mpn_copyi (rp, x->_mp_d, n);
1761 + r->_mp_size = x->_mp_size;
1762 + }
1763 +}
1764 +
1765 +void
1766 +mpz_init_set_si (mpz_t r, signed long int x)
1767 +{
1768 + mpz_init (r);
1769 + mpz_set_si (r, x);
1770 +}
1771 +
1772 +void
1773 +mpz_init_set_ui (mpz_t r, unsigned long int x)
1774 +{
1775 + mpz_init (r);
1776 + mpz_set_ui (r, x);
1777 +}
1778 +
1779 +void
1780 +mpz_init_set (mpz_t r, const mpz_t x)
1781 +{
1782 + mpz_init (r);
1783 + mpz_set (r, x);
1784 +}
1785 +
1786 +int
1787 +mpz_fits_slong_p (const mpz_t u)
1788 +{
1789 + mp_size_t us = u->_mp_size;
1790 +
1791 + if (us == 0)
1792 + return 1;
1793 + else if (us == 1)
1794 + return u->_mp_d[0] < GMP_LIMB_HIGHBIT;
1795 + else if (us == -1)
1796 + return u->_mp_d[0] <= GMP_LIMB_HIGHBIT;
1797 + else
1798 + return 0;
1799 +}
1800 +
1801 +int
1802 +mpz_fits_ulong_p (const mpz_t u)
1803 +{
1804 + mp_size_t us = u->_mp_size;
1805 +
1806 + return (us == (us > 0));
1807 +}
1808 +
1809 +long int
1810 +mpz_get_si (const mpz_t u)
1811 +{
1812 + mp_size_t us = u->_mp_size;
1813 +
1814 + if (us > 0)
1815 + return (long) (u->_mp_d[0] & ~GMP_LIMB_HIGHBIT);
1816 + else if (us < 0)
1817 + return (long) (- u->_mp_d[0] | GMP_LIMB_HIGHBIT);
1818 + else
1819 + return 0;
1820 +}
1821 +
1822 +unsigned long int
1823 +mpz_get_ui (const mpz_t u)
1824 +{
1825 + return u->_mp_size == 0 ? 0 : u->_mp_d[0];
1826 +}
1827 +
1828 +size_t
1829 +mpz_size (const mpz_t u)
1830 +{
1831 + return GMP_ABS (u->_mp_size);
1832 +}
1833 +
1834 +mp_limb_t
1835 +mpz_getlimbn (const mpz_t u, mp_size_t n)
1836 +{
1837 + if (n >= 0 && n < GMP_ABS (u->_mp_size))
1838 + return u->_mp_d[n];
1839 + else
1840 + return 0;
1841 +}
1842 +
1843 +void
1844 +mpz_realloc2 (mpz_t x, mp_bitcnt_t n)
1845 +{
1846 + mpz_realloc (x, 1 + (n - (n != 0)) / GMP_LIMB_BITS);
1847 +}
1848 +
1849 +mp_srcptr
1850 +mpz_limbs_read (mpz_srcptr x)
1851 +{
1852 + return x->_mp_d;;
1853 +}
1854 +
1855 +mp_ptr
1856 +mpz_limbs_modify (mpz_t x, mp_size_t n)
1857 +{
1858 + assert (n > 0);
1859 + return MPZ_REALLOC (x, n);
1860 +}
1861 +
1862 +mp_ptr
1863 +mpz_limbs_write (mpz_t x, mp_size_t n)
1864 +{
1865 + return mpz_limbs_modify (x, n);
1866 +}
1867 +
1868 +void
1869 +mpz_limbs_finish (mpz_t x, mp_size_t xs)
1870 +{
1871 + mp_size_t xn;
1872 + xn = mpn_normalized_size (x->_mp_d, GMP_ABS (xs));
1873 + x->_mp_size = xs < 0 ? -xn : xn;
1874 +}
1875 +
1876 +mpz_srcptr
1877 +mpz_roinit_n (mpz_t x, mp_srcptr xp, mp_size_t xs)
1878 +{
1879 + x->_mp_alloc = 0;
1880 + x->_mp_d = (mp_ptr) xp;
1881 + mpz_limbs_finish (x, xs);
1882 + return x;
1883 +}
1884 +
1885 +\f
1886 +/* Conversions and comparison to double. */
1887 +void
1888 +mpz_set_d (mpz_t r, double x)
1889 +{
1890 + int sign;
1891 + mp_ptr rp;
1892 + mp_size_t rn, i;
1893 + double B;
1894 + double Bi;
1895 + mp_limb_t f;
1896 +
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)
1900 + {
1901 + r->_mp_size = 0;
1902 + return;
1903 + }
1904 +
1905 + sign = x < 0.0 ;
1906 + if (sign)
1907 + x = - x;
1908 +
1909 + if (x < 1.0)
1910 + {
1911 + r->_mp_size = 0;
1912 + return;
1913 + }
1914 + B = 2.0 * (double) GMP_LIMB_HIGHBIT;
1915 + Bi = 1.0 / B;
1916 + for (rn = 1; x >= B; rn++)
1917 + x *= Bi;
1918 +
1919 + rp = MPZ_REALLOC (r, rn);
1920 +
1921 + f = (mp_limb_t) x;
1922 + x -= f;
1923 + assert (x < 1.0);
1924 + i = rn-1;
1925 + rp[i] = f;
1926 + while (--i >= 0)
1927 + {
1928 + x = B * x;
1929 + f = (mp_limb_t) x;
1930 + x -= f;
1931 + assert (x < 1.0);
1932 + rp[i] = f;
1933 + }
1934 +
1935 + r->_mp_size = sign ? - rn : rn;
1936 +}
1937 +
1938 +void
1939 +mpz_init_set_d (mpz_t r, double x)
1940 +{
1941 + mpz_init (r);
1942 + mpz_set_d (r, x);
1943 +}
1944 +
1945 +double
1946 +mpz_get_d (const mpz_t u)
1947 +{
1948 + mp_size_t un;
1949 + double x;
1950 + double B = 2.0 * (double) GMP_LIMB_HIGHBIT;
1951 +
1952 + un = GMP_ABS (u->_mp_size);
1953 +
1954 + if (un == 0)
1955 + return 0.0;
1956 +
1957 + x = u->_mp_d[--un];
1958 + while (un > 0)
1959 + x = B*x + u->_mp_d[--un];
1960 +
1961 + if (u->_mp_size < 0)
1962 + x = -x;
1963 +
1964 + return x;
1965 +}
1966 +
1967 +int
1968 +mpz_cmpabs_d (const mpz_t x, double d)
1969 +{
1970 + mp_size_t xn;
1971 + double B, Bi;
1972 + mp_size_t i;
1973 +
1974 + xn = x->_mp_size;
1975 + d = GMP_ABS (d);
1976 +
1977 + if (xn != 0)
1978 + {
1979 + xn = GMP_ABS (xn);
1980 +
1981 + B = 2.0 * (double) GMP_LIMB_HIGHBIT;
1982 + Bi = 1.0 / B;
1983 +
1984 + /* Scale d so it can be compared with the top limb. */
1985 + for (i = 1; i < xn; i++)
1986 + d *= Bi;
1987 +
1988 + if (d >= B)
1989 + return -1;
1990 +
1991 + /* Compare floor(d) to top limb, subtract and cancel when equal. */
1992 + for (i = xn; i-- > 0;)
1993 + {
1994 + mp_limb_t f, xl;
1995 +
1996 + f = (mp_limb_t) d;
1997 + xl = x->_mp_d[i];
1998 + if (xl > f)
1999 + return 1;
2000 + else if (xl < f)
2001 + return -1;
2002 + d = B * (d - f);
2003 + }
2004 + }
2005 + return - (d > 0.0);
2006 +}
2007 +
2008 +int
2009 +mpz_cmp_d (const mpz_t x, double d)
2010 +{
2011 + if (x->_mp_size < 0)
2012 + {
2013 + if (d >= 0.0)
2014 + return -1;
2015 + else
2016 + return -mpz_cmpabs_d (x, d);
2017 + }
2018 + else
2019 + {
2020 + if (d < 0.0)
2021 + return 1;
2022 + else
2023 + return mpz_cmpabs_d (x, d);
2024 + }
2025 +}
2026 +
2027 +\f
2028 +/* MPZ comparisons and the like. */
2029 +int
2030 +mpz_sgn (const mpz_t u)
2031 +{
2032 + mp_size_t usize = u->_mp_size;
2033 +
2034 + return (usize > 0) - (usize < 0);
2035 +}
2036 +
2037 +int
2038 +mpz_cmp_si (const mpz_t u, long v)
2039 +{
2040 + mp_size_t usize = u->_mp_size;
2041 +
2042 + if (usize < -1)
2043 + return -1;
2044 + else if (v >= 0)
2045 + return mpz_cmp_ui (u, v);
2046 + else if (usize >= 0)
2047 + return 1;
2048 + else /* usize == -1 */
2049 + {
2050 + mp_limb_t ul = u->_mp_d[0];
2051 + if ((mp_limb_t)GMP_NEG_CAST (unsigned long int, v) < ul)
2052 + return -1;
2053 + else
2054 + return (mp_limb_t)GMP_NEG_CAST (unsigned long int, v) > ul;
2055 + }
2056 +}
2057 +
2058 +int
2059 +mpz_cmp_ui (const mpz_t u, unsigned long v)
2060 +{
2061 + mp_size_t usize = u->_mp_size;
2062 +
2063 + if (usize > 1)
2064 + return 1;
2065 + else if (usize < 0)
2066 + return -1;
2067 + else
2068 + {
2069 + mp_limb_t ul = (usize > 0) ? u->_mp_d[0] : 0;
2070 + return (ul > v) - (ul < v);
2071 + }
2072 +}
2073 +
2074 +int
2075 +mpz_cmp (const mpz_t a, const mpz_t b)
2076 +{
2077 + mp_size_t asize = a->_mp_size;
2078 + mp_size_t bsize = b->_mp_size;
2079 +
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);
2084 + else
2085 + return mpn_cmp (b->_mp_d, a->_mp_d, -asize);
2086 +}
2087 +
2088 +int
2089 +mpz_cmpabs_ui (const mpz_t u, unsigned long v)
2090 +{
2091 + mp_size_t un = GMP_ABS (u->_mp_size);
2092 + mp_limb_t ul;
2093 +
2094 + if (un > 1)
2095 + return 1;
2096 +
2097 + ul = (un == 1) ? u->_mp_d[0] : 0;
2098 +
2099 + return (ul > v) - (ul < v);
2100 +}
2101 +
2102 +int
2103 +mpz_cmpabs (const mpz_t u, const mpz_t v)
2104 +{
2105 + return mpn_cmp4 (u->_mp_d, GMP_ABS (u->_mp_size),
2106 + v->_mp_d, GMP_ABS (v->_mp_size));
2107 +}
2108 +
2109 +void
2110 +mpz_abs (mpz_t r, const mpz_t u)
2111 +{
2112 + if (r != u)
2113 + mpz_set (r, u);
2114 +
2115 + r->_mp_size = GMP_ABS (r->_mp_size);
2116 +}
2117 +
2118 +void
2119 +mpz_neg (mpz_t r, const mpz_t u)
2120 +{
2121 + if (r != u)
2122 + mpz_set (r, u);
2123 +
2124 + r->_mp_size = -r->_mp_size;
2125 +}
2126 +
2127 +void
2128 +mpz_swap (mpz_t u, mpz_t v)
2129 +{
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);
2133 +}
2134 +
2135 +\f
2136 +/* MPZ addition and subtraction */
2137 +
2138 +/* Adds to the absolute value. Returns new size, but doesn't store it. */
2139 +static mp_size_t
2140 +mpz_abs_add_ui (mpz_t r, const mpz_t a, unsigned long b)
2141 +{
2142 + mp_size_t an;
2143 + mp_ptr rp;
2144 + mp_limb_t cy;
2145 +
2146 + an = GMP_ABS (a->_mp_size);
2147 + if (an == 0)
2148 + {
2149 + r->_mp_d[0] = b;
2150 + return b > 0;
2151 + }
2152 +
2153 + rp = MPZ_REALLOC (r, an + 1);
2154 +
2155 + cy = mpn_add_1 (rp, a->_mp_d, an, b);
2156 + rp[an] = cy;
2157 + an += cy;
2158 +
2159 + return an;
2160 +}
2161 +
2162 +/* Subtract from the absolute value. Returns new size, (or -1 on underflow),
2163 + but doesn't store it. */
2164 +static mp_size_t
2165 +mpz_abs_sub_ui (mpz_t r, const mpz_t a, unsigned long b)
2166 +{
2167 + mp_size_t an = GMP_ABS (a->_mp_size);
2168 + mp_ptr rp = MPZ_REALLOC (r, an);
2169 +
2170 + if (an == 0)
2171 + {
2172 + rp[0] = b;
2173 + return -(b > 0);
2174 + }
2175 + else if (an == 1 && a->_mp_d[0] < b)
2176 + {
2177 + rp[0] = b - a->_mp_d[0];
2178 + return -1;
2179 + }
2180 + else
2181 + {
2182 + gmp_assert_nocarry (mpn_sub_1 (rp, a->_mp_d, an, b));
2183 + return mpn_normalized_size (rp, an);
2184 + }
2185 +}
2186 +
2187 +void
2188 +mpz_add_ui (mpz_t r, const mpz_t a, unsigned long b)
2189 +{
2190 + if (a->_mp_size >= 0)
2191 + r->_mp_size = mpz_abs_add_ui (r, a, b);
2192 + else
2193 + r->_mp_size = -mpz_abs_sub_ui (r, a, b);
2194 +}
2195 +
2196 +void
2197 +mpz_sub_ui (mpz_t r, const mpz_t a, unsigned long b)
2198 +{
2199 + if (a->_mp_size < 0)
2200 + r->_mp_size = -mpz_abs_add_ui (r, a, b);
2201 + else
2202 + r->_mp_size = mpz_abs_sub_ui (r, a, b);
2203 +}
2204 +
2205 +void
2206 +mpz_ui_sub (mpz_t r, unsigned long a, const mpz_t b)
2207 +{
2208 + if (b->_mp_size < 0)
2209 + r->_mp_size = mpz_abs_add_ui (r, b, a);
2210 + else
2211 + r->_mp_size = -mpz_abs_sub_ui (r, b, a);
2212 +}
2213 +
2214 +static mp_size_t
2215 +mpz_abs_add (mpz_t r, const mpz_t a, const mpz_t b)
2216 +{
2217 + mp_size_t an = GMP_ABS (a->_mp_size);
2218 + mp_size_t bn = GMP_ABS (b->_mp_size);
2219 + mp_ptr rp;
2220 + mp_limb_t cy;
2221 +
2222 + if (an < bn)
2223 + {
2224 + MPZ_SRCPTR_SWAP (a, b);
2225 + MP_SIZE_T_SWAP (an, bn);
2226 + }
2227 +
2228 + rp = MPZ_REALLOC (r, an + 1);
2229 + cy = mpn_add (rp, a->_mp_d, an, b->_mp_d, bn);
2230 +
2231 + rp[an] = cy;
2232 +
2233 + return an + cy;
2234 +}
2235 +
2236 +static mp_size_t
2237 +mpz_abs_sub (mpz_t r, const mpz_t a, const mpz_t b)
2238 +{
2239 + mp_size_t an = GMP_ABS (a->_mp_size);
2240 + mp_size_t bn = GMP_ABS (b->_mp_size);
2241 + int cmp;
2242 + mp_ptr rp;
2243 +
2244 + cmp = mpn_cmp4 (a->_mp_d, an, b->_mp_d, bn);
2245 + if (cmp > 0)
2246 + {
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);
2250 + }
2251 + else if (cmp < 0)
2252 + {
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);
2256 + }
2257 + else
2258 + return 0;
2259 +}
2260 +
2261 +void
2262 +mpz_add (mpz_t r, const mpz_t a, const mpz_t b)
2263 +{
2264 + mp_size_t rn;
2265 +
2266 + if ( (a->_mp_size ^ b->_mp_size) >= 0)
2267 + rn = mpz_abs_add (r, a, b);
2268 + else
2269 + rn = mpz_abs_sub (r, a, b);
2270 +
2271 + r->_mp_size = a->_mp_size >= 0 ? rn : - rn;
2272 +}
2273 +
2274 +void
2275 +mpz_sub (mpz_t r, const mpz_t a, const mpz_t b)
2276 +{
2277 + mp_size_t rn;
2278 +
2279 + if ( (a->_mp_size ^ b->_mp_size) >= 0)
2280 + rn = mpz_abs_sub (r, a, b);
2281 + else
2282 + rn = mpz_abs_add (r, a, b);
2283 +
2284 + r->_mp_size = a->_mp_size >= 0 ? rn : - rn;
2285 +}
2286 +
2287 +\f
2288 +/* MPZ multiplication */
2289 +void
2290 +mpz_mul_si (mpz_t r, const mpz_t u, long int v)
2291 +{
2292 + if (v < 0)
2293 + {
2294 + mpz_mul_ui (r, u, GMP_NEG_CAST (unsigned long int, v));
2295 + mpz_neg (r, r);
2296 + }
2297 + else
2298 + mpz_mul_ui (r, u, (unsigned long int) v);
2299 +}
2300 +
2301 +void
2302 +mpz_mul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2303 +{
2304 + mp_size_t un, us;
2305 + mp_ptr tp;
2306 + mp_limb_t cy;
2307 +
2308 + us = u->_mp_size;
2309 +
2310 + if (us == 0 || v == 0)
2311 + {
2312 + r->_mp_size = 0;
2313 + return;
2314 + }
2315 +
2316 + un = GMP_ABS (us);
2317 +
2318 + tp = MPZ_REALLOC (r, un + 1);
2319 + cy = mpn_mul_1 (tp, u->_mp_d, un, v);
2320 + tp[un] = cy;
2321 +
2322 + un += (cy > 0);
2323 + r->_mp_size = (us < 0) ? - un : un;
2324 +}
2325 +
2326 +void
2327 +mpz_mul (mpz_t r, const mpz_t u, const mpz_t v)
2328 +{
2329 + int sign;
2330 + mp_size_t un, vn, rn;
2331 + mpz_t t;
2332 + mp_ptr tp;
2333 +
2334 + un = u->_mp_size;
2335 + vn = v->_mp_size;
2336 +
2337 + if (un == 0 || vn == 0)
2338 + {
2339 + r->_mp_size = 0;
2340 + return;
2341 + }
2342 +
2343 + sign = (un ^ vn) < 0;
2344 +
2345 + un = GMP_ABS (un);
2346 + vn = GMP_ABS (vn);
2347 +
2348 + mpz_init2 (t, (un + vn) * GMP_LIMB_BITS);
2349 +
2350 + tp = t->_mp_d;
2351 + if (un >= vn)
2352 + mpn_mul (tp, u->_mp_d, un, v->_mp_d, vn);
2353 + else
2354 + mpn_mul (tp, v->_mp_d, vn, u->_mp_d, un);
2355 +
2356 + rn = un + vn;
2357 + rn -= tp[rn-1] == 0;
2358 +
2359 + t->_mp_size = sign ? - rn : rn;
2360 + mpz_swap (r, t);
2361 + mpz_clear (t);
2362 +}
2363 +
2364 +void
2365 +mpz_mul_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bits)
2366 +{
2367 + mp_size_t un, rn;
2368 + mp_size_t limbs;
2369 + unsigned shift;
2370 + mp_ptr rp;
2371 +
2372 + un = GMP_ABS (u->_mp_size);
2373 + if (un == 0)
2374 + {
2375 + r->_mp_size = 0;
2376 + return;
2377 + }
2378 +
2379 + limbs = bits / GMP_LIMB_BITS;
2380 + shift = bits % GMP_LIMB_BITS;
2381 +
2382 + rn = un + limbs + (shift > 0);
2383 + rp = MPZ_REALLOC (r, rn);
2384 + if (shift > 0)
2385 + {
2386 + mp_limb_t cy = mpn_lshift (rp + limbs, u->_mp_d, un, shift);
2387 + rp[rn-1] = cy;
2388 + rn -= (cy == 0);
2389 + }
2390 + else
2391 + mpn_copyd (rp + limbs, u->_mp_d, un);
2392 +
2393 + while (limbs > 0)
2394 + rp[--limbs] = 0;
2395 +
2396 + r->_mp_size = (u->_mp_size < 0) ? - rn : rn;
2397 +}
2398 +
2399 +void
2400 +mpz_addmul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2401 +{
2402 + mpz_t t;
2403 + mpz_init (t);
2404 + mpz_mul_ui (t, u, v);
2405 + mpz_add (r, r, t);
2406 + mpz_clear (t);
2407 +}
2408 +
2409 +void
2410 +mpz_submul_ui (mpz_t r, const mpz_t u, unsigned long int v)
2411 +{
2412 + mpz_t t;
2413 + mpz_init (t);
2414 + mpz_mul_ui (t, u, v);
2415 + mpz_sub (r, r, t);
2416 + mpz_clear (t);
2417 +}
2418 +
2419 +void
2420 +mpz_addmul (mpz_t r, const mpz_t u, const mpz_t v)
2421 +{
2422 + mpz_t t;
2423 + mpz_init (t);
2424 + mpz_mul (t, u, v);
2425 + mpz_add (r, r, t);
2426 + mpz_clear (t);
2427 +}
2428 +
2429 +void
2430 +mpz_submul (mpz_t r, const mpz_t u, const mpz_t v)
2431 +{
2432 + mpz_t t;
2433 + mpz_init (t);
2434 + mpz_mul (t, u, v);
2435 + mpz_sub (r, r, t);
2436 + mpz_clear (t);
2437 +}
2438 +
2439 +\f
2440 +/* MPZ division */
2441 +enum mpz_div_round_mode { GMP_DIV_FLOOR, GMP_DIV_CEIL, GMP_DIV_TRUNC };
2442 +
2443 +/* Allows q or r to be zero. Returns 1 iff remainder is non-zero. */
2444 +static int
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)
2447 +{
2448 + mp_size_t ns, ds, nn, dn, qs;
2449 + ns = n->_mp_size;
2450 + ds = d->_mp_size;
2451 +
2452 + if (ds == 0)
2453 + gmp_die("mpz_div_qr: Divide by zero.");
2454 +
2455 + if (ns == 0)
2456 + {
2457 + if (q)
2458 + q->_mp_size = 0;
2459 + if (r)
2460 + r->_mp_size = 0;
2461 + return 0;
2462 + }
2463 +
2464 + nn = GMP_ABS (ns);
2465 + dn = GMP_ABS (ds);
2466 +
2467 + qs = ds ^ ns;
2468 +
2469 + if (nn < dn)
2470 + {
2471 + if (mode == GMP_DIV_CEIL && qs >= 0)
2472 + {
2473 + /* q = 1, r = n - d */
2474 + if (r)
2475 + mpz_sub (r, n, d);
2476 + if (q)
2477 + mpz_set_ui (q, 1);
2478 + }
2479 + else if (mode == GMP_DIV_FLOOR && qs < 0)
2480 + {
2481 + /* q = -1, r = n + d */
2482 + if (r)
2483 + mpz_add (r, n, d);
2484 + if (q)
2485 + mpz_set_si (q, -1);
2486 + }
2487 + else
2488 + {
2489 + /* q = 0, r = d */
2490 + if (r)
2491 + mpz_set (r, n);
2492 + if (q)
2493 + q->_mp_size = 0;
2494 + }
2495 + return 1;
2496 + }
2497 + else
2498 + {
2499 + mp_ptr np, qp;
2500 + mp_size_t qn, rn;
2501 + mpz_t tq, tr;
2502 +
2503 + mpz_init_set (tr, n);
2504 + np = tr->_mp_d;
2505 +
2506 + qn = nn - dn + 1;
2507 +
2508 + if (q)
2509 + {
2510 + mpz_init2 (tq, qn * GMP_LIMB_BITS);
2511 + qp = tq->_mp_d;
2512 + }
2513 + else
2514 + qp = NULL;
2515 +
2516 + mpn_div_qr (qp, np, nn, d->_mp_d, dn);
2517 +
2518 + if (qp)
2519 + {
2520 + qn -= (qp[qn-1] == 0);
2521 +
2522 + tq->_mp_size = qs < 0 ? -qn : qn;
2523 + }
2524 + rn = mpn_normalized_size (np, dn);
2525 + tr->_mp_size = ns < 0 ? - rn : rn;
2526 +
2527 + if (mode == GMP_DIV_FLOOR && qs < 0 && rn != 0)
2528 + {
2529 + if (q)
2530 + mpz_sub_ui (tq, tq, 1);
2531 + if (r)
2532 + mpz_add (tr, tr, d);
2533 + }
2534 + else if (mode == GMP_DIV_CEIL && qs >= 0 && rn != 0)
2535 + {
2536 + if (q)
2537 + mpz_add_ui (tq, tq, 1);
2538 + if (r)
2539 + mpz_sub (tr, tr, d);
2540 + }
2541 +
2542 + if (q)
2543 + {
2544 + mpz_swap (tq, q);
2545 + mpz_clear (tq);
2546 + }
2547 + if (r)
2548 + mpz_swap (tr, r);
2549 +
2550 + mpz_clear (tr);
2551 +
2552 + return rn != 0;
2553 + }
2554 +}
2555 +
2556 +void
2557 +mpz_cdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2558 +{
2559 + mpz_div_qr (q, r, n, d, GMP_DIV_CEIL);
2560 +}
2561 +
2562 +void
2563 +mpz_fdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2564 +{
2565 + mpz_div_qr (q, r, n, d, GMP_DIV_FLOOR);
2566 +}
2567 +
2568 +void
2569 +mpz_tdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
2570 +{
2571 + mpz_div_qr (q, r, n, d, GMP_DIV_TRUNC);
2572 +}
2573 +
2574 +void
2575 +mpz_cdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2576 +{
2577 + mpz_div_qr (q, NULL, n, d, GMP_DIV_CEIL);
2578 +}
2579 +
2580 +void
2581 +mpz_fdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2582 +{
2583 + mpz_div_qr (q, NULL, n, d, GMP_DIV_FLOOR);
2584 +}
2585 +
2586 +void
2587 +mpz_tdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
2588 +{
2589 + mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC);
2590 +}
2591 +
2592 +void
2593 +mpz_cdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2594 +{
2595 + mpz_div_qr (NULL, r, n, d, GMP_DIV_CEIL);
2596 +}
2597 +
2598 +void
2599 +mpz_fdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2600 +{
2601 + mpz_div_qr (NULL, r, n, d, GMP_DIV_FLOOR);
2602 +}
2603 +
2604 +void
2605 +mpz_tdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
2606 +{
2607 + mpz_div_qr (NULL, r, n, d, GMP_DIV_TRUNC);
2608 +}
2609 +
2610 +void
2611 +mpz_mod (mpz_t r, const mpz_t n, const mpz_t d)
2612 +{
2613 + mpz_div_qr (NULL, r, n, d, d->_mp_size >= 0 ? GMP_DIV_FLOOR : GMP_DIV_CEIL);
2614 +}
2615 +
2616 +static void
2617 +mpz_div_q_2exp (mpz_t q, const mpz_t u, mp_bitcnt_t bit_index,
2618 + enum mpz_div_round_mode mode)
2619 +{
2620 + mp_size_t un, qn;
2621 + mp_size_t limb_cnt;
2622 + mp_ptr qp;
2623 + int adjust;
2624 +
2625 + un = u->_mp_size;
2626 + if (un == 0)
2627 + {
2628 + q->_mp_size = 0;
2629 + return;
2630 + }
2631 + limb_cnt = bit_index / GMP_LIMB_BITS;
2632 + qn = GMP_ABS (un) - limb_cnt;
2633 + bit_index %= GMP_LIMB_BITS;
2634 +
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. */
2638 + adjust = (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)));
2642 + else
2643 + adjust = 0;
2644 +
2645 + if (qn <= 0)
2646 + qn = 0;
2647 +
2648 + else
2649 + {
2650 + qp = MPZ_REALLOC (q, qn);
2651 +
2652 + if (bit_index != 0)
2653 + {
2654 + mpn_rshift (qp, u->_mp_d + limb_cnt, qn, bit_index);
2655 + qn -= qp[qn - 1] == 0;
2656 + }
2657 + else
2658 + {
2659 + mpn_copyi (qp, u->_mp_d + limb_cnt, qn);
2660 + }
2661 + }
2662 +
2663 + q->_mp_size = qn;
2664 +
2665 + if (adjust)
2666 + mpz_add_ui (q, q, 1);
2667 + if (un < 0)
2668 + mpz_neg (q, q);
2669 +}
2670 +
2671 +static void
2672 +mpz_div_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bit_index,
2673 + enum mpz_div_round_mode mode)
2674 +{
2675 + mp_size_t us, un, rn;
2676 + mp_ptr rp;
2677 + mp_limb_t mask;
2678 +
2679 + us = u->_mp_size;
2680 + if (us == 0 || bit_index == 0)
2681 + {
2682 + r->_mp_size = 0;
2683 + return;
2684 + }
2685 + rn = (bit_index + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
2686 + assert (rn > 0);
2687 +
2688 + rp = MPZ_REALLOC (r, rn);
2689 + un = GMP_ABS (us);
2690 +
2691 + mask = GMP_LIMB_MAX >> (rn * GMP_LIMB_BITS - bit_index);
2692 +
2693 + if (rn > un)
2694 + {
2695 + /* Quotient (with truncation) is zero, and remainder is
2696 + non-zero */
2697 + if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
2698 + {
2699 + /* Have to negate and sign extend. */
2700 + mp_size_t i;
2701 + mp_limb_t cy;
2702 +
2703 + for (cy = 1, i = 0; i < un; i++)
2704 + {
2705 + mp_limb_t s = ~u->_mp_d[i] + cy;
2706 + cy = s < cy;
2707 + rp[i] = s;
2708 + }
2709 + assert (cy == 0);
2710 + for (; i < rn - 1; i++)
2711 + rp[i] = GMP_LIMB_MAX;
2712 +
2713 + rp[rn-1] = mask;
2714 + us = -us;
2715 + }
2716 + else
2717 + {
2718 + /* Just copy */
2719 + if (r != u)
2720 + mpn_copyi (rp, u->_mp_d, un);
2721 +
2722 + rn = un;
2723 + }
2724 + }
2725 + else
2726 + {
2727 + if (r != u)
2728 + mpn_copyi (rp, u->_mp_d, rn - 1);
2729 +
2730 + rp[rn-1] = u->_mp_d[rn-1] & mask;
2731 +
2732 + if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
2733 + {
2734 + /* If r != 0, compute 2^{bit_count} - r. */
2735 + mp_size_t i;
2736 +
2737 + for (i = 0; i < rn && rp[i] == 0; i++)
2738 + ;
2739 + if (i < rn)
2740 + {
2741 + /* r > 0, need to flip sign. */
2742 + rp[i] = ~rp[i] + 1;
2743 + while (++i < rn)
2744 + rp[i] = ~rp[i];
2745 +
2746 + rp[rn-1] &= mask;
2747 +
2748 + /* us is not used for anything else, so we can modify it
2749 + here to indicate flipped sign. */
2750 + us = -us;
2751 + }
2752 + }
2753 + }
2754 + rn = mpn_normalized_size (rp, rn);
2755 + r->_mp_size = us < 0 ? -rn : rn;
2756 +}
2757 +
2758 +void
2759 +mpz_cdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2760 +{
2761 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_CEIL);
2762 +}
2763 +
2764 +void
2765 +mpz_fdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2766 +{
2767 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_FLOOR);
2768 +}
2769 +
2770 +void
2771 +mpz_tdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2772 +{
2773 + mpz_div_q_2exp (r, u, cnt, GMP_DIV_TRUNC);
2774 +}
2775 +
2776 +void
2777 +mpz_cdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2778 +{
2779 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_CEIL);
2780 +}
2781 +
2782 +void
2783 +mpz_fdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2784 +{
2785 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_FLOOR);
2786 +}
2787 +
2788 +void
2789 +mpz_tdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
2790 +{
2791 + mpz_div_r_2exp (r, u, cnt, GMP_DIV_TRUNC);
2792 +}
2793 +
2794 +void
2795 +mpz_divexact (mpz_t q, const mpz_t n, const mpz_t d)
2796 +{
2797 + gmp_assert_nocarry (mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC));
2798 +}
2799 +
2800 +int
2801 +mpz_divisible_p (const mpz_t n, const mpz_t d)
2802 +{
2803 + return mpz_div_qr (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
2804 +}
2805 +
2806 +int
2807 +mpz_congruent_p (const mpz_t a, const mpz_t b, const mpz_t m)
2808 +{
2809 + mpz_t t;
2810 + int res;
2811 +
2812 + /* a == b (mod 0) iff a == b */
2813 + if (mpz_sgn (m) == 0)
2814 + return (mpz_cmp (a, b) == 0);
2815 +
2816 + mpz_init (t);
2817 + mpz_sub (t, a, b);
2818 + res = mpz_divisible_p (t, m);
2819 + mpz_clear (t);
2820 +
2821 + return res;
2822 +}
2823 +
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)
2827 +{
2828 + mp_size_t ns, qn;
2829 + mp_ptr qp;
2830 + mp_limb_t rl;
2831 + mp_size_t rs;
2832 +
2833 + ns = n->_mp_size;
2834 + if (ns == 0)
2835 + {
2836 + if (q)
2837 + q->_mp_size = 0;
2838 + if (r)
2839 + r->_mp_size = 0;
2840 + return 0;
2841 + }
2842 +
2843 + qn = GMP_ABS (ns);
2844 + if (q)
2845 + qp = MPZ_REALLOC (q, qn);
2846 + else
2847 + qp = NULL;
2848 +
2849 + rl = mpn_div_qr_1 (qp, n->_mp_d, qn, d);
2850 + assert (rl < d);
2851 +
2852 + rs = rl > 0;
2853 + rs = (ns < 0) ? -rs : rs;
2854 +
2855 + if (rl > 0 && ( (mode == GMP_DIV_FLOOR && ns < 0)
2856 + || (mode == GMP_DIV_CEIL && ns >= 0)))
2857 + {
2858 + if (q)
2859 + gmp_assert_nocarry (mpn_add_1 (qp, qp, qn, 1));
2860 + rl = d - rl;
2861 + rs = -rs;
2862 + }
2863 +
2864 + if (r)
2865 + {
2866 + r->_mp_d[0] = rl;
2867 + r->_mp_size = rs;
2868 + }
2869 + if (q)
2870 + {
2871 + qn -= (qp[qn-1] == 0);
2872 + assert (qn == 0 || qp[qn-1] > 0);
2873 +
2874 + q->_mp_size = (ns < 0) ? - qn : qn;
2875 + }
2876 +
2877 + return rl;
2878 +}
2879 +
2880 +unsigned long
2881 +mpz_cdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
2882 +{
2883 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_CEIL);
2884 +}
2885 +
2886 +unsigned long
2887 +mpz_fdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
2888 +{
2889 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_FLOOR);
2890 +}
2891 +
2892 +unsigned long
2893 +mpz_tdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
2894 +{
2895 + return mpz_div_qr_ui (q, r, n, d, GMP_DIV_TRUNC);
2896 +}
2897 +
2898 +unsigned long
2899 +mpz_cdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
2900 +{
2901 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_CEIL);
2902 +}
2903 +
2904 +unsigned long
2905 +mpz_fdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
2906 +{
2907 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_FLOOR);
2908 +}
2909 +
2910 +unsigned long
2911 +mpz_tdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
2912 +{
2913 + return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC);
2914 +}
2915 +
2916 +unsigned long
2917 +mpz_cdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
2918 +{
2919 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_CEIL);
2920 +}
2921 +unsigned long
2922 +mpz_fdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
2923 +{
2924 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
2925 +}
2926 +unsigned long
2927 +mpz_tdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
2928 +{
2929 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_TRUNC);
2930 +}
2931 +
2932 +unsigned long
2933 +mpz_cdiv_ui (const mpz_t n, unsigned long d)
2934 +{
2935 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_CEIL);
2936 +}
2937 +
2938 +unsigned long
2939 +mpz_fdiv_ui (const mpz_t n, unsigned long d)
2940 +{
2941 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_FLOOR);
2942 +}
2943 +
2944 +unsigned long
2945 +mpz_tdiv_ui (const mpz_t n, unsigned long d)
2946 +{
2947 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC);
2948 +}
2949 +
2950 +unsigned long
2951 +mpz_mod_ui (mpz_t r, const mpz_t n, unsigned long d)
2952 +{
2953 + return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
2954 +}
2955 +
2956 +void
2957 +mpz_divexact_ui (mpz_t q, const mpz_t n, unsigned long d)
2958 +{
2959 + gmp_assert_nocarry (mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC));
2960 +}
2961 +
2962 +int
2963 +mpz_divisible_ui_p (const mpz_t n, unsigned long d)
2964 +{
2965 + return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
2966 +}
2967 +
2968 +\f
2969 +/* GCD */
2970 +static mp_limb_t
2971 +mpn_gcd_11 (mp_limb_t u, mp_limb_t v)
2972 +{
2973 + unsigned shift;
2974 +
2975 + assert ( (u | v) > 0);
2976 +
2977 + if (u == 0)
2978 + return v;
2979 + else if (v == 0)
2980 + return u;
2981 +
2982 + gmp_ctz (shift, u | v);
2983 +
2984 + u >>= shift;
2985 + v >>= shift;
2986 +
2987 + if ( (u & 1) == 0)
2988 + MP_LIMB_T_SWAP (u, v);
2989 +
2990 + while ( (v & 1) == 0)
2991 + v >>= 1;
2992 +
2993 + while (u != v)
2994 + {
2995 + if (u > v)
2996 + {
2997 + u -= v;
2998 + do
2999 + u >>= 1;
3000 + while ( (u & 1) == 0);
3001 + }
3002 + else
3003 + {
3004 + v -= u;
3005 + do
3006 + v >>= 1;
3007 + while ( (v & 1) == 0);
3008 + }
3009 + }
3010 + return u << shift;
3011 +}
3012 +
3013 +unsigned long
3014 +mpz_gcd_ui (mpz_t g, const mpz_t u, unsigned long v)
3015 +{
3016 + mp_size_t un;
3017 +
3018 + if (v == 0)
3019 + {
3020 + if (g)
3021 + mpz_abs (g, u);
3022 + }
3023 + else
3024 + {
3025 + un = GMP_ABS (u->_mp_size);
3026 + if (un != 0)
3027 + v = mpn_gcd_11 (mpn_div_qr_1 (NULL, u->_mp_d, un, v), v);
3028 +
3029 + if (g)
3030 + mpz_set_ui (g, v);
3031 + }
3032 +
3033 + return v;
3034 +}
3035 +
3036 +static mp_bitcnt_t
3037 +mpz_make_odd (mpz_t r)
3038 +{
3039 + mp_bitcnt_t shift;
3040 +
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);
3045 +
3046 + return shift;
3047 +}
3048 +
3049 +void
3050 +mpz_gcd (mpz_t g, const mpz_t u, const mpz_t v)
3051 +{
3052 + mpz_t tu, tv;
3053 + mp_bitcnt_t uz, vz, gz;
3054 +
3055 + if (u->_mp_size == 0)
3056 + {
3057 + mpz_abs (g, v);
3058 + return;
3059 + }
3060 + if (v->_mp_size == 0)
3061 + {
3062 + mpz_abs (g, u);
3063 + return;
3064 + }
3065 +
3066 + mpz_init (tu);
3067 + mpz_init (tv);
3068 +
3069 + mpz_abs (tu, u);
3070 + uz = mpz_make_odd (tu);
3071 + mpz_abs (tv, v);
3072 + vz = mpz_make_odd (tv);
3073 + gz = GMP_MIN (uz, vz);
3074 +
3075 + if (tu->_mp_size < tv->_mp_size)
3076 + mpz_swap (tu, tv);
3077 +
3078 + mpz_tdiv_r (tu, tu, tv);
3079 + if (tu->_mp_size == 0)
3080 + {
3081 + mpz_swap (g, tv);
3082 + }
3083 + else
3084 + for (;;)
3085 + {
3086 + int c;
3087 +
3088 + mpz_make_odd (tu);
3089 + c = mpz_cmp (tu, tv);
3090 + if (c == 0)
3091 + {
3092 + mpz_swap (g, tu);
3093 + break;
3094 + }
3095 + if (c < 0)
3096 + mpz_swap (tu, tv);
3097 +
3098 + if (tv->_mp_size == 1)
3099 + {
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));
3103 + break;
3104 + }
3105 + mpz_sub (tu, tu, tv);
3106 + }
3107 + mpz_clear (tu);
3108 + mpz_clear (tv);
3109 + mpz_mul_2exp (g, g, gz);
3110 +}
3111 +
3112 +void
3113 +mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, const mpz_t u, const mpz_t v)
3114 +{
3115 + mpz_t tu, tv, s0, s1, t0, t1;
3116 + mp_bitcnt_t uz, vz, gz;
3117 + mp_bitcnt_t power;
3118 +
3119 + if (u->_mp_size == 0)
3120 + {
3121 + /* g = 0 u + sgn(v) v */
3122 + signed long sign = mpz_sgn (v);
3123 + mpz_abs (g, v);
3124 + if (s)
3125 + mpz_set_ui (s, 0);
3126 + if (t)
3127 + mpz_set_si (t, sign);
3128 + return;
3129 + }
3130 +
3131 + if (v->_mp_size == 0)
3132 + {
3133 + /* g = sgn(u) u + 0 v */
3134 + signed long sign = mpz_sgn (u);
3135 + mpz_abs (g, u);
3136 + if (s)
3137 + mpz_set_si (s, sign);
3138 + if (t)
3139 + mpz_set_ui (t, 0);
3140 + return;
3141 + }
3142 +
3143 + mpz_init (tu);
3144 + mpz_init (tv);
3145 + mpz_init (s0);
3146 + mpz_init (s1);
3147 + mpz_init (t0);
3148 + mpz_init (t1);
3149 +
3150 + mpz_abs (tu, u);
3151 + uz = mpz_make_odd (tu);
3152 + mpz_abs (tv, v);
3153 + vz = mpz_make_odd (tv);
3154 + gz = GMP_MIN (uz, vz);
3155 +
3156 + uz -= gz;
3157 + vz -= gz;
3158 +
3159 + /* Cofactors corresponding to odd gcd. gz handled later. */
3160 + if (tu->_mp_size < tv->_mp_size)
3161 + {
3162 + mpz_swap (tu, tv);
3163 + MPZ_SRCPTR_SWAP (u, v);
3164 + MPZ_PTR_SWAP (s, t);
3165 + MP_BITCNT_T_SWAP (uz, vz);
3166 + }
3167 +
3168 + /* Maintain
3169 + *
3170 + * u = t0 tu + t1 tv
3171 + * v = s0 tu + s1 tv
3172 + *
3173 + * where u and v denote the inputs with common factors of two
3174 + * eliminated, and det (s0, t0; s1, t1) = 2^p. Then
3175 + *
3176 + * 2^p tu = s1 u - t1 v
3177 + * 2^p tv = -s0 u + t0 v
3178 + */
3179 +
3180 + /* After initial division, tu = q tv + tu', we have
3181 + *
3182 + * u = 2^uz (tu' + q tv)
3183 + * v = 2^vz tv
3184 + *
3185 + * or
3186 + *
3187 + * t0 = 2^uz, t1 = 2^uz q
3188 + * s0 = 0, s1 = 2^vz
3189 + */
3190 +
3191 + mpz_setbit (t0, uz);
3192 + mpz_tdiv_qr (t1, tu, tu, tv);
3193 + mpz_mul_2exp (t1, t1, uz);
3194 +
3195 + mpz_setbit (s1, vz);
3196 + power = uz + vz;
3197 +
3198 + if (tu->_mp_size > 0)
3199 + {
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);
3204 + power += shift;
3205 +
3206 + for (;;)
3207 + {
3208 + int c;
3209 + c = mpz_cmp (tu, tv);
3210 + if (c == 0)
3211 + break;
3212 +
3213 + if (c < 0)
3214 + {
3215 + /* tv = tv' + tu
3216 + *
3217 + * u = t0 tu + t1 (tv' + tu) = (t0 + t1) tu + t1 tv'
3218 + * v = s0 tu + s1 (tv' + tu) = (s0 + s1) tu + s1 tv' */
3219 +
3220 + mpz_sub (tv, tv, tu);
3221 + mpz_add (t0, t0, t1);
3222 + mpz_add (s0, s0, s1);
3223 +
3224 + shift = mpz_make_odd (tv);
3225 + mpz_mul_2exp (t1, t1, shift);
3226 + mpz_mul_2exp (s1, s1, shift);
3227 + }
3228 + else
3229 + {
3230 + mpz_sub (tu, tu, tv);
3231 + mpz_add (t1, t0, t1);
3232 + mpz_add (s1, s0, s1);
3233 +
3234 + shift = mpz_make_odd (tu);
3235 + mpz_mul_2exp (t0, t0, shift);
3236 + mpz_mul_2exp (s0, s0, shift);
3237 + }
3238 + power += shift;
3239 + }
3240 + }
3241 +
3242 + /* Now tv = odd part of gcd, and -s0 and t0 are corresponding
3243 + cofactors. */
3244 +
3245 + mpz_mul_2exp (tv, tv, gz);
3246 + mpz_neg (s0, s0);
3247 +
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 */
3250 +
3251 + mpz_divexact (s1, v, tv);
3252 + mpz_abs (s1, s1);
3253 + mpz_divexact (t1, u, tv);
3254 + mpz_abs (t1, t1);
3255 +
3256 + while (power-- > 0)
3257 + {
3258 + /* s0 u + t0 v = (s0 - v/g) u - (t0 + u/g) v */
3259 + if (mpz_odd_p (s0) || mpz_odd_p (t0))
3260 + {
3261 + mpz_sub (s0, s0, s1);
3262 + mpz_add (t0, t0, t1);
3263 + }
3264 + mpz_divexact_ui (s0, s0, 2);
3265 + mpz_divexact_ui (t0, t0, 2);
3266 + }
3267 +
3268 + /* Arrange so that |s| < |u| / 2g */
3269 + mpz_add (s1, s0, s1);
3270 + if (mpz_cmpabs (s0, s1) > 0)
3271 + {
3272 + mpz_swap (s0, s1);
3273 + mpz_sub (t0, t0, t1);
3274 + }
3275 + if (u->_mp_size < 0)
3276 + mpz_neg (s0, s0);
3277 + if (v->_mp_size < 0)
3278 + mpz_neg (t0, t0);
3279 +
3280 + mpz_swap (g, tv);
3281 + if (s)
3282 + mpz_swap (s, s0);
3283 + if (t)
3284 + mpz_swap (t, t0);
3285 +
3286 + mpz_clear (tu);
3287 + mpz_clear (tv);
3288 + mpz_clear (s0);
3289 + mpz_clear (s1);
3290 + mpz_clear (t0);
3291 + mpz_clear (t1);
3292 +}
3293 +
3294 +void
3295 +mpz_lcm (mpz_t r, const mpz_t u, const mpz_t v)
3296 +{
3297 + mpz_t g;
3298 +
3299 + if (u->_mp_size == 0 || v->_mp_size == 0)
3300 + {
3301 + r->_mp_size = 0;
3302 + return;
3303 + }
3304 +
3305 + mpz_init (g);
3306 +
3307 + mpz_gcd (g, u, v);
3308 + mpz_divexact (g, u, g);
3309 + mpz_mul (r, g, v);
3310 +
3311 + mpz_clear (g);
3312 + mpz_abs (r, r);
3313 +}
3314 +
3315 +void
3316 +mpz_lcm_ui (mpz_t r, const mpz_t u, unsigned long v)
3317 +{
3318 + if (v == 0 || u->_mp_size == 0)
3319 + {
3320 + r->_mp_size = 0;
3321 + return;
3322 + }
3323 +
3324 + v /= mpz_gcd_ui (NULL, u, v);
3325 + mpz_mul_ui (r, u, v);
3326 +
3327 + mpz_abs (r, r);
3328 +}
3329 +
3330 +int
3331 +mpz_invert (mpz_t r, const mpz_t u, const mpz_t m)
3332 +{
3333 + mpz_t g, tr;
3334 + int invertible;
3335 +
3336 + if (u->_mp_size == 0 || mpz_cmpabs_ui (m, 1) <= 0)
3337 + return 0;
3338 +
3339 + mpz_init (g);
3340 + mpz_init (tr);
3341 +
3342 + mpz_gcdext (g, tr, NULL, u, m);
3343 + invertible = (mpz_cmp_ui (g, 1) == 0);
3344 +
3345 + if (invertible)
3346 + {
3347 + if (tr->_mp_size < 0)
3348 + {
3349 + if (m->_mp_size >= 0)
3350 + mpz_add (tr, tr, m);
3351 + else
3352 + mpz_sub (tr, tr, m);
3353 + }
3354 + mpz_swap (r, tr);
3355 + }
3356 +
3357 + mpz_clear (g);
3358 + mpz_clear (tr);
3359 + return invertible;
3360 +}
3361 +
3362 +\f
3363 +/* Higher level operations (sqrt, pow and root) */
3364 +
3365 +void
3366 +mpz_pow_ui (mpz_t r, const mpz_t b, unsigned long e)
3367 +{
3368 + unsigned long bit;
3369 + mpz_t tr;
3370 + mpz_init_set_ui (tr, 1);
3371 +
3372 + bit = GMP_ULONG_HIGHBIT;
3373 + do
3374 + {
3375 + mpz_mul (tr, tr, tr);
3376 + if (e & bit)
3377 + mpz_mul (tr, tr, b);
3378 + bit >>= 1;
3379 + }
3380 + while (bit > 0);
3381 +
3382 + mpz_swap (r, tr);
3383 + mpz_clear (tr);
3384 +}
3385 +
3386 +void
3387 +mpz_ui_pow_ui (mpz_t r, unsigned long blimb, unsigned long e)
3388 +{
3389 + mpz_t b;
3390 + mpz_init_set_ui (b, blimb);
3391 + mpz_pow_ui (r, b, e);
3392 + mpz_clear (b);
3393 +}
3394 +
3395 +void
3396 +mpz_powm (mpz_t r, const mpz_t b, const mpz_t e, const mpz_t m)
3397 +{
3398 + mpz_t tr;
3399 + mpz_t base;
3400 + mp_size_t en, mn;
3401 + mp_srcptr mp;
3402 + struct gmp_div_inverse minv;
3403 + unsigned shift;
3404 + mp_ptr tp = NULL;
3405 +
3406 + en = GMP_ABS (e->_mp_size);
3407 + mn = GMP_ABS (m->_mp_size);
3408 + if (mn == 0)
3409 + gmp_die ("mpz_powm: Zero modulo.");
3410 +
3411 + if (en == 0)
3412 + {
3413 + mpz_set_ui (r, 1);
3414 + return;
3415 + }
3416 +
3417 + mp = m->_mp_d;
3418 + mpn_div_qr_invert (&minv, mp, mn);
3419 + shift = minv.shift;
3420 +
3421 + if (shift > 0)
3422 + {
3423 + /* To avoid shifts, we do all our reductions, except the final
3424 + one, using a *normalized* m. */
3425 + minv.shift = 0;
3426 +
3427 + tp = gmp_xalloc_limbs (mn);
3428 + gmp_assert_nocarry (mpn_lshift (tp, mp, mn, shift));
3429 + mp = tp;
3430 + }
3431 +
3432 + mpz_init (base);
3433 +
3434 + if (e->_mp_size < 0)
3435 + {
3436 + if (!mpz_invert (base, b, m))
3437 + gmp_die ("mpz_powm: Negative exponent and non-invertible base.");
3438 + }
3439 + else
3440 + {
3441 + mp_size_t bn;
3442 + mpz_abs (base, b);
3443 +
3444 + bn = base->_mp_size;
3445 + if (bn >= mn)
3446 + {
3447 + mpn_div_qr_preinv (NULL, base->_mp_d, base->_mp_size, mp, mn, &minv);
3448 + bn = mn;
3449 + }
3450 +
3451 + /* We have reduced the absolute value. Now take care of the
3452 + sign. Note that we get zero represented non-canonically as
3453 + m. */
3454 + if (b->_mp_size < 0)
3455 + {
3456 + mp_ptr bp = MPZ_REALLOC (base, mn);
3457 + gmp_assert_nocarry (mpn_sub (bp, mp, mn, bp, bn));
3458 + bn = mn;
3459 + }
3460 + base->_mp_size = mpn_normalized_size (base->_mp_d, bn);
3461 + }
3462 + mpz_init_set_ui (tr, 1);
3463 +
3464 + while (en-- > 0)
3465 + {
3466 + mp_limb_t w = e->_mp_d[en];
3467 + mp_limb_t bit;
3468 +
3469 + bit = GMP_LIMB_HIGHBIT;
3470 + do
3471 + {
3472 + mpz_mul (tr, tr, tr);
3473 + if (w & bit)
3474 + mpz_mul (tr, tr, base);
3475 + if (tr->_mp_size > mn)
3476 + {
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);
3479 + }
3480 + bit >>= 1;
3481 + }
3482 + while (bit > 0);
3483 + }
3484 +
3485 + /* Final reduction */
3486 + if (tr->_mp_size >= mn)
3487 + {
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);
3491 + }
3492 + if (tp)
3493 + gmp_free (tp);
3494 +
3495 + mpz_swap (r, tr);
3496 + mpz_clear (tr);
3497 + mpz_clear (base);
3498 +}
3499 +
3500 +void
3501 +mpz_powm_ui (mpz_t r, const mpz_t b, unsigned long elimb, const mpz_t m)
3502 +{
3503 + mpz_t e;
3504 + mpz_init_set_ui (e, elimb);
3505 + mpz_powm (r, b, e, m);
3506 + mpz_clear (e);
3507 +}
3508 +
3509 +/* x=trunc(y^(1/z)), r=y-x^z */
3510 +void
3511 +mpz_rootrem (mpz_t x, mpz_t r, const mpz_t y, unsigned long z)
3512 +{
3513 + int sgn;
3514 + mpz_t t, u;
3515 +
3516 + sgn = y->_mp_size < 0;
3517 + if ((~z & sgn) != 0)
3518 + gmp_die ("mpz_rootrem: Negative argument, with even root.");
3519 + if (z == 0)
3520 + gmp_die ("mpz_rootrem: Zeroth root.");
3521 +
3522 + if (mpz_cmpabs_ui (y, 1) <= 0) {
3523 + if (x)
3524 + mpz_set (x, y);
3525 + if (r)
3526 + r->_mp_size = 0;
3527 + return;
3528 + }
3529 +
3530 + mpz_init (u);
3531 + {
3532 + mp_bitcnt_t tb;
3533 + tb = mpz_sizeinbase (y, 2) / z + 1;
3534 + mpz_init2 (t, tb);
3535 + mpz_setbit (t, tb);
3536 + }
3537 +
3538 + if (z == 2) /* simplify sqrt loop: z-1 == 1 */
3539 + do {
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 */ {
3546 + mpz_t v;
3547 +
3548 + mpz_init (v);
3549 + if (sgn)
3550 + mpz_neg (t, t);
3551 +
3552 + do {
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| */
3560 +
3561 + mpz_clear (v);
3562 + }
3563 +
3564 + if (r) {
3565 + mpz_pow_ui (t, u, z);
3566 + mpz_sub (r, y, t);
3567 + }
3568 + if (x)
3569 + mpz_swap (x, u);
3570 + mpz_clear (u);
3571 + mpz_clear (t);
3572 +}
3573 +
3574 +int
3575 +mpz_root (mpz_t x, const mpz_t y, unsigned long z)
3576 +{
3577 + int res;
3578 + mpz_t r;
3579 +
3580 + mpz_init (r);
3581 + mpz_rootrem (x, r, y, z);
3582 + res = r->_mp_size == 0;
3583 + mpz_clear (r);
3584 +
3585 + return res;
3586 +}
3587 +
3588 +/* Compute s = floor(sqrt(u)) and r = u - s^2. Allows r == NULL */
3589 +void
3590 +mpz_sqrtrem (mpz_t s, mpz_t r, const mpz_t u)
3591 +{
3592 + mpz_rootrem (s, r, u, 2);
3593 +}
3594 +
3595 +void
3596 +mpz_sqrt (mpz_t s, const mpz_t u)
3597 +{
3598 + mpz_rootrem (s, NULL, u, 2);
3599 +}
3600 +
3601 +int
3602 +mpz_perfect_square_p (const mpz_t u)
3603 +{
3604 + if (u->_mp_size <= 0)
3605 + return (u->_mp_size == 0);
3606 + else
3607 + return mpz_root (NULL, u, 2);
3608 +}
3609 +
3610 +int
3611 +mpn_perfect_square_p (mp_srcptr p, mp_size_t n)
3612 +{
3613 + mpz_t t;
3614 +
3615 + assert (n > 0);
3616 + assert (p [n-1] != 0);
3617 + return mpz_root (NULL, mpz_roinit_n (t, p, n), 2);
3618 +}
3619 +
3620 +mp_size_t
3621 +mpn_sqrtrem (mp_ptr sp, mp_ptr rp, mp_srcptr p, mp_size_t n)
3622 +{
3623 + mpz_t s, r, u;
3624 + mp_size_t res;
3625 +
3626 + assert (n > 0);
3627 + assert (p [n-1] != 0);
3628 +
3629 + mpz_init (r);
3630 + mpz_init (s);
3631 + mpz_rootrem (s, r, mpz_roinit_n (u, p, n), 2);
3632 +
3633 + assert (s->_mp_size == (n+1)/2);
3634 + mpn_copyd (sp, s->_mp_d, s->_mp_size);
3635 + mpz_clear (s);
3636 + res = r->_mp_size;
3637 + if (rp)
3638 + mpn_copyd (rp, r->_mp_d, res);
3639 + mpz_clear (r);
3640 + return res;
3641 +}
3642 +\f
3643 +/* Combinatorics */
3644 +
3645 +void
3646 +mpz_fac_ui (mpz_t x, unsigned long n)
3647 +{
3648 + mpz_set_ui (x, n + (n == 0));
3649 + for (;n > 2;)
3650 + mpz_mul_ui (x, x, --n);
3651 +}
3652 +
3653 +void
3654 +mpz_bin_uiui (mpz_t r, unsigned long n, unsigned long k)
3655 +{
3656 + mpz_t t;
3657 +
3658 + mpz_set_ui (r, k <= n);
3659 +
3660 + if (k > (n >> 1))
3661 + k = (k <= n) ? n - k : 0;
3662 +
3663 + mpz_init (t);
3664 + mpz_fac_ui (t, k);
3665 +
3666 + for (; k > 0; k--)
3667 + mpz_mul_ui (r, r, n--);
3668 +
3669 + mpz_divexact (r, r, t);
3670 + mpz_clear (t);
3671 +}
3672 +
3673 +\f
3674 +/* Primality testing */
3675 +static int
3676 +gmp_millerrabin (const mpz_t n, const mpz_t nm1, mpz_t y,
3677 + const mpz_t q, mp_bitcnt_t k)
3678 +{
3679 + assert (k > 0);
3680 +
3681 + /* Caller must initialize y to the base. */
3682 + mpz_powm (y, y, q, n);
3683 +
3684 + if (mpz_cmp_ui (y, 1) == 0 || mpz_cmp (y, nm1) == 0)
3685 + return 1;
3686 +
3687 + while (--k > 0)
3688 + {
3689 + mpz_powm_ui (y, y, 2, n);
3690 + if (mpz_cmp (y, nm1) == 0)
3691 + return 1;
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)
3696 + return 0;
3697 + }
3698 + return 0;
3699 +}
3700 +
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)
3704 +
3705 +/* Bit (p+1)/2 is set, for each odd prime <= 61 */
3706 +#define GMP_PRIME_MASK 0xc96996dcUL
3707 +
3708 +int
3709 +mpz_probab_prime_p (const mpz_t n, int reps)
3710 +{
3711 + mpz_t nm1;
3712 + mpz_t q;
3713 + mpz_t y;
3714 + mp_bitcnt_t k;
3715 + int is_prime;
3716 + int j;
3717 +
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;
3722 +
3723 + /* Above test excludes n == 0 */
3724 + assert (n->_mp_size != 0);
3725 +
3726 + if (mpz_cmpabs_ui (n, 64) < 0)
3727 + return (GMP_PRIME_MASK >> (n->_mp_d[0] >> 1)) & 2;
3728 +
3729 + if (mpz_gcd_ui (NULL, n, GMP_PRIME_PRODUCT) != 1)
3730 + return 0;
3731 +
3732 + /* All prime factors are >= 31. */
3733 + if (mpz_cmpabs_ui (n, 31*31) < 0)
3734 + return 2;
3735 +
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). */
3740 +
3741 + mpz_init (nm1);
3742 + mpz_init (q);
3743 + mpz_init (y);
3744 +
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);
3749 +
3750 + for (j = 0, is_prime = 1; is_prime & (j < reps); j++)
3751 + {
3752 + mpz_set_ui (y, (unsigned long) j*j+j+41);
3753 + if (mpz_cmp (y, nm1) >= 0)
3754 + {
3755 + /* Don't try any further bases. This "early" break does not affect
3756 + the result for any reasonable reps value (<=5000 was tested) */
3757 + assert (j >= 30);
3758 + break;
3759 + }
3760 + is_prime = gmp_millerrabin (n, nm1, y, q, k);
3761 + }
3762 + mpz_clear (nm1);
3763 + mpz_clear (q);
3764 + mpz_clear (y);
3765 +
3766 + return is_prime;
3767 +}
3768 +
3769 +\f
3770 +/* Logical operations and bit manipulation. */
3771 +
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
3776 +
3777 + xxxx10...0
3778 +
3779 + into
3780 +
3781 + yyyy10...0
3782 +
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.
3786 +
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. */
3793 +
3794 +int
3795 +mpz_tstbit (const mpz_t d, mp_bitcnt_t bit_index)
3796 +{
3797 + mp_size_t limb_index;
3798 + unsigned shift;
3799 + mp_size_t ds;
3800 + mp_size_t dn;
3801 + mp_limb_t w;
3802 + int bit;
3803 +
3804 + ds = d->_mp_size;
3805 + dn = GMP_ABS (ds);
3806 + limb_index = bit_index / GMP_LIMB_BITS;
3807 + if (limb_index >= dn)
3808 + return ds < 0;
3809 +
3810 + shift = bit_index % GMP_LIMB_BITS;
3811 + w = d->_mp_d[limb_index];
3812 + bit = (w >> shift) & 1;
3813 +
3814 + if (ds < 0)
3815 + {
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)
3819 + return bit ^ 1;
3820 + while (limb_index-- > 0)
3821 + if (d->_mp_d[limb_index] > 0)
3822 + return bit ^ 1;
3823 + }
3824 + return bit;
3825 +}
3826 +
3827 +static void
3828 +mpz_abs_add_bit (mpz_t d, mp_bitcnt_t bit_index)
3829 +{
3830 + mp_size_t dn, limb_index;
3831 + mp_limb_t bit;
3832 + mp_ptr dp;
3833 +
3834 + dn = GMP_ABS (d->_mp_size);
3835 +
3836 + limb_index = bit_index / GMP_LIMB_BITS;
3837 + bit = (mp_limb_t) 1 << (bit_index % GMP_LIMB_BITS);
3838 +
3839 + if (limb_index >= dn)
3840 + {
3841 + mp_size_t i;
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);
3845 +
3846 + dp[limb_index] = bit;
3847 + for (i = dn; i < limb_index; i++)
3848 + dp[i] = 0;
3849 + dn = limb_index + 1;
3850 + }
3851 + else
3852 + {
3853 + mp_limb_t cy;
3854 +
3855 + dp = d->_mp_d;
3856 +
3857 + cy = mpn_add_1 (dp + limb_index, dp + limb_index, dn - limb_index, bit);
3858 + if (cy > 0)
3859 + {
3860 + dp = MPZ_REALLOC (d, dn + 1);
3861 + dp[dn++] = cy;
3862 + }
3863 + }
3864 +
3865 + d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
3866 +}
3867 +
3868 +static void
3869 +mpz_abs_sub_bit (mpz_t d, mp_bitcnt_t bit_index)
3870 +{
3871 + mp_size_t dn, limb_index;
3872 + mp_ptr dp;
3873 + mp_limb_t bit;
3874 +
3875 + dn = GMP_ABS (d->_mp_size);
3876 + dp = d->_mp_d;
3877 +
3878 + limb_index = bit_index / GMP_LIMB_BITS;
3879 + bit = (mp_limb_t) 1 << (bit_index % GMP_LIMB_BITS);
3880 +
3881 + assert (limb_index < dn);
3882 +
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;
3887 +}
3888 +
3889 +void
3890 +mpz_setbit (mpz_t d, mp_bitcnt_t bit_index)
3891 +{
3892 + if (!mpz_tstbit (d, bit_index))
3893 + {
3894 + if (d->_mp_size >= 0)
3895 + mpz_abs_add_bit (d, bit_index);
3896 + else
3897 + mpz_abs_sub_bit (d, bit_index);
3898 + }
3899 +}
3900 +
3901 +void
3902 +mpz_clrbit (mpz_t d, mp_bitcnt_t bit_index)
3903 +{
3904 + if (mpz_tstbit (d, bit_index))
3905 + {
3906 + if (d->_mp_size >= 0)
3907 + mpz_abs_sub_bit (d, bit_index);
3908 + else
3909 + mpz_abs_add_bit (d, bit_index);
3910 + }
3911 +}
3912 +
3913 +void
3914 +mpz_combit (mpz_t d, mp_bitcnt_t bit_index)
3915 +{
3916 + if (mpz_tstbit (d, bit_index) ^ (d->_mp_size < 0))
3917 + mpz_abs_sub_bit (d, bit_index);
3918 + else
3919 + mpz_abs_add_bit (d, bit_index);
3920 +}
3921 +
3922 +void
3923 +mpz_com (mpz_t r, const mpz_t u)
3924 +{
3925 + mpz_neg (r, u);
3926 + mpz_sub_ui (r, r, 1);
3927 +}
3928 +
3929 +void
3930 +mpz_and (mpz_t r, const mpz_t u, const mpz_t v)
3931 +{
3932 + mp_size_t un, vn, rn, i;
3933 + mp_ptr up, vp, rp;
3934 +
3935 + mp_limb_t ux, vx, rx;
3936 + mp_limb_t uc, vc, rc;
3937 + mp_limb_t ul, vl, rl;
3938 +
3939 + un = GMP_ABS (u->_mp_size);
3940 + vn = GMP_ABS (v->_mp_size);
3941 + if (un < vn)
3942 + {
3943 + MPZ_SRCPTR_SWAP (u, v);
3944 + MP_SIZE_T_SWAP (un, vn);
3945 + }
3946 + if (vn == 0)
3947 + {
3948 + r->_mp_size = 0;
3949 + return;
3950 + }
3951 +
3952 + uc = u->_mp_size < 0;
3953 + vc = v->_mp_size < 0;
3954 + rc = uc & vc;
3955 +
3956 + ux = -uc;
3957 + vx = -vc;
3958 + rx = -rc;
3959 +
3960 + /* If the smaller input is positive, higher limbs don't matter. */
3961 + rn = vx ? un : vn;
3962 +
3963 + rp = MPZ_REALLOC (r, rn + rc);
3964 +
3965 + up = u->_mp_d;
3966 + vp = v->_mp_d;
3967 +
3968 + i = 0;
3969 + do
3970 + {
3971 + ul = (up[i] ^ ux) + uc;
3972 + uc = ul < uc;
3973 +
3974 + vl = (vp[i] ^ vx) + vc;
3975 + vc = vl < vc;
3976 +
3977 + rl = ( (ul & vl) ^ rx) + rc;
3978 + rc = rl < rc;
3979 + rp[i] = rl;
3980 + }
3981 + while (++i < vn);
3982 + assert (vc == 0);
3983 +
3984 + for (; i < rn; i++)
3985 + {
3986 + ul = (up[i] ^ ux) + uc;
3987 + uc = ul < uc;
3988 +
3989 + rl = ( (ul & vx) ^ rx) + rc;
3990 + rc = rl < rc;
3991 + rp[i] = rl;
3992 + }
3993 + if (rc)
3994 + rp[rn++] = rc;
3995 + else
3996 + rn = mpn_normalized_size (rp, rn);
3997 +
3998 + r->_mp_size = rx ? -rn : rn;
3999 +}
4000 +
4001 +void
4002 +mpz_ior (mpz_t r, const mpz_t u, const mpz_t v)
4003 +{
4004 + mp_size_t un, vn, rn, i;
4005 + mp_ptr up, vp, rp;
4006 +
4007 + mp_limb_t ux, vx, rx;
4008 + mp_limb_t uc, vc, rc;
4009 + mp_limb_t ul, vl, rl;
4010 +
4011 + un = GMP_ABS (u->_mp_size);
4012 + vn = GMP_ABS (v->_mp_size);
4013 + if (un < vn)
4014 + {
4015 + MPZ_SRCPTR_SWAP (u, v);
4016 + MP_SIZE_T_SWAP (un, vn);
4017 + }
4018 + if (vn == 0)
4019 + {
4020 + mpz_set (r, u);
4021 + return;
4022 + }
4023 +
4024 + uc = u->_mp_size < 0;
4025 + vc = v->_mp_size < 0;
4026 + rc = uc | vc;
4027 +
4028 + ux = -uc;
4029 + vx = -vc;
4030 + rx = -rc;
4031 +
4032 + /* If the smaller input is negative, by sign extension higher limbs
4033 + don't matter. */
4034 + rn = vx ? vn : un;
4035 +
4036 + rp = MPZ_REALLOC (r, rn + rc);
4037 +
4038 + up = u->_mp_d;
4039 + vp = v->_mp_d;
4040 +
4041 + i = 0;
4042 + do
4043 + {
4044 + ul = (up[i] ^ ux) + uc;
4045 + uc = ul < uc;
4046 +
4047 + vl = (vp[i] ^ vx) + vc;
4048 + vc = vl < vc;
4049 +
4050 + rl = ( (ul | vl) ^ rx) + rc;
4051 + rc = rl < rc;
4052 + rp[i] = rl;
4053 + }
4054 + while (++i < vn);
4055 + assert (vc == 0);
4056 +
4057 + for (; i < rn; i++)
4058 + {
4059 + ul = (up[i] ^ ux) + uc;
4060 + uc = ul < uc;
4061 +
4062 + rl = ( (ul | vx) ^ rx) + rc;
4063 + rc = rl < rc;
4064 + rp[i] = rl;
4065 + }
4066 + if (rc)
4067 + rp[rn++] = rc;
4068 + else
4069 + rn = mpn_normalized_size (rp, rn);
4070 +
4071 + r->_mp_size = rx ? -rn : rn;
4072 +}
4073 +
4074 +void
4075 +mpz_xor (mpz_t r, const mpz_t u, const mpz_t v)
4076 +{
4077 + mp_size_t un, vn, i;
4078 + mp_ptr up, vp, rp;
4079 +
4080 + mp_limb_t ux, vx, rx;
4081 + mp_limb_t uc, vc, rc;
4082 + mp_limb_t ul, vl, rl;
4083 +
4084 + un = GMP_ABS (u->_mp_size);
4085 + vn = GMP_ABS (v->_mp_size);
4086 + if (un < vn)
4087 + {
4088 + MPZ_SRCPTR_SWAP (u, v);
4089 + MP_SIZE_T_SWAP (un, vn);
4090 + }
4091 + if (vn == 0)
4092 + {
4093 + mpz_set (r, u);
4094 + return;
4095 + }
4096 +
4097 + uc = u->_mp_size < 0;
4098 + vc = v->_mp_size < 0;
4099 + rc = uc ^ vc;
4100 +
4101 + ux = -uc;
4102 + vx = -vc;
4103 + rx = -rc;
4104 +
4105 + rp = MPZ_REALLOC (r, un + rc);
4106 +
4107 + up = u->_mp_d;
4108 + vp = v->_mp_d;
4109 +
4110 + i = 0;
4111 + do
4112 + {
4113 + ul = (up[i] ^ ux) + uc;
4114 + uc = ul < uc;
4115 +
4116 + vl = (vp[i] ^ vx) + vc;
4117 + vc = vl < vc;
4118 +
4119 + rl = (ul ^ vl ^ rx) + rc;
4120 + rc = rl < rc;
4121 + rp[i] = rl;
4122 + }
4123 + while (++i < vn);
4124 + assert (vc == 0);
4125 +
4126 + for (; i < un; i++)
4127 + {
4128 + ul = (up[i] ^ ux) + uc;
4129 + uc = ul < uc;
4130 +
4131 + rl = (ul ^ ux) + rc;
4132 + rc = rl < rc;
4133 + rp[i] = rl;
4134 + }
4135 + if (rc)
4136 + rp[un++] = rc;
4137 + else
4138 + un = mpn_normalized_size (rp, un);
4139 +
4140 + r->_mp_size = rx ? -un : un;
4141 +}
4142 +
4143 +static unsigned
4144 +gmp_popcount_limb (mp_limb_t x)
4145 +{
4146 + unsigned c;
4147 +
4148 + /* Do 16 bits at a time, to avoid limb-sized constants. */
4149 + for (c = 0; x > 0; x >>= 16)
4150 + {
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);
4155 + c += w;
4156 + }
4157 + return c;
4158 +}
4159 +
4160 +mp_bitcnt_t
4161 +mpn_popcount (mp_srcptr p, mp_size_t n)
4162 +{
4163 + mp_size_t i;
4164 + mp_bitcnt_t c;
4165 +
4166 + for (c = 0, i = 0; i < n; i++)
4167 + c += gmp_popcount_limb (p[i]);
4168 +
4169 + return c;
4170 +}
4171 +
4172 +mp_bitcnt_t
4173 +mpz_popcount (const mpz_t u)
4174 +{
4175 + mp_size_t un;
4176 +
4177 + un = u->_mp_size;
4178 +
4179 + if (un < 0)
4180 + return ~(mp_bitcnt_t) 0;
4181 +
4182 + return mpn_popcount (u->_mp_d, un);
4183 +}
4184 +
4185 +mp_bitcnt_t
4186 +mpz_hamdist (const mpz_t u, const mpz_t v)
4187 +{
4188 + mp_size_t un, vn, i;
4189 + mp_limb_t uc, vc, ul, vl, comp;
4190 + mp_srcptr up, vp;
4191 + mp_bitcnt_t c;
4192 +
4193 + un = u->_mp_size;
4194 + vn = v->_mp_size;
4195 +
4196 + if ( (un ^ vn) < 0)
4197 + return ~(mp_bitcnt_t) 0;
4198 +
4199 + comp = - (uc = vc = (un < 0));
4200 + if (uc)
4201 + {
4202 + assert (vn < 0);
4203 + un = -un;
4204 + vn = -vn;
4205 + }
4206 +
4207 + up = u->_mp_d;
4208 + vp = v->_mp_d;
4209 +
4210 + if (un < vn)
4211 + MPN_SRCPTR_SWAP (up, un, vp, vn);
4212 +
4213 + for (i = 0, c = 0; i < vn; i++)
4214 + {
4215 + ul = (up[i] ^ comp) + uc;
4216 + uc = ul < uc;
4217 +
4218 + vl = (vp[i] ^ comp) + vc;
4219 + vc = vl < vc;
4220 +
4221 + c += gmp_popcount_limb (ul ^ vl);
4222 + }
4223 + assert (vc == 0);
4224 +
4225 + for (; i < un; i++)
4226 + {
4227 + ul = (up[i] ^ comp) + uc;
4228 + uc = ul < uc;
4229 +
4230 + c += gmp_popcount_limb (ul ^ comp);
4231 + }
4232 +
4233 + return c;
4234 +}
4235 +
4236 +mp_bitcnt_t
4237 +mpz_scan1 (const mpz_t u, mp_bitcnt_t starting_bit)
4238 +{
4239 + mp_ptr up;
4240 + mp_size_t us, un, i;
4241 + mp_limb_t limb, ux;
4242 +
4243 + us = u->_mp_size;
4244 + un = GMP_ABS (us);
4245 + i = starting_bit / GMP_LIMB_BITS;
4246 +
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. */
4249 + if (i >= un)
4250 + return (us >= 0 ? ~(mp_bitcnt_t) 0 : starting_bit);
4251 +
4252 + up = u->_mp_d;
4253 + ux = 0;
4254 + limb = up[i];
4255 +
4256 + if (starting_bit != 0)
4257 + {
4258 + if (us < 0)
4259 + {
4260 + ux = mpn_zero_p (up, i);
4261 + limb = ~ limb + ux;
4262 + ux = - (mp_limb_t) (limb >= ux);
4263 + }
4264 +
4265 + /* Mask to 0 all bits before starting_bit, thus ignoring them. */
4266 + limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
4267 + }
4268 +
4269 + return mpn_common_scan (limb, i, up, un, ux);
4270 +}
4271 +
4272 +mp_bitcnt_t
4273 +mpz_scan0 (const mpz_t u, mp_bitcnt_t starting_bit)
4274 +{
4275 + mp_ptr up;
4276 + mp_size_t us, un, i;
4277 + mp_limb_t limb, ux;
4278 +
4279 + us = u->_mp_size;
4280 + ux = - (mp_limb_t) (us >= 0);
4281 + un = GMP_ABS (us);
4282 + i = starting_bit / GMP_LIMB_BITS;
4283 +
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. */
4286 + if (i >= un)
4287 + return (ux ? starting_bit : ~(mp_bitcnt_t) 0);
4288 +
4289 + up = u->_mp_d;
4290 + limb = up[i] ^ ux;
4291 +
4292 + if (ux == 0)
4293 + limb -= mpn_zero_p (up, i); /* limb = ~(~limb + zero_p) */
4294 +
4295 + /* Mask all bits before starting_bit, thus ignoring them. */
4296 + limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
4297 +
4298 + return mpn_common_scan (limb, i, up, un, ux);
4299 +}
4300 +
4301 +\f
4302 +/* MPZ base conversion. */
4303 +
4304 +size_t
4305 +mpz_sizeinbase (const mpz_t u, int base)
4306 +{
4307 + mp_size_t un;
4308 + mp_srcptr up;
4309 + mp_ptr tp;
4310 + mp_bitcnt_t bits;
4311 + struct gmp_div_inverse bi;
4312 + size_t ndigits;
4313 +
4314 + assert (base >= 2);
4315 + assert (base <= 36);
4316 +
4317 + un = GMP_ABS (u->_mp_size);
4318 + if (un == 0)
4319 + return 1;
4320 +
4321 + up = u->_mp_d;
4322 +
4323 + bits = (un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1]);
4324 + switch (base)
4325 + {
4326 + case 2:
4327 + return bits;
4328 + case 4:
4329 + return (bits + 1) / 2;
4330 + case 8:
4331 + return (bits + 2) / 3;
4332 + case 16:
4333 + return (bits + 3) / 4;
4334 + case 32:
4335 + return (bits + 4) / 5;
4336 + /* FIXME: Do something more clever for the common case of base
4337 + 10. */
4338 + }
4339 +
4340 + tp = gmp_xalloc_limbs (un);
4341 + mpn_copyi (tp, up, un);
4342 + mpn_div_qr_1_invert (&bi, base);
4343 +
4344 + ndigits = 0;
4345 + do
4346 + {
4347 + ndigits++;
4348 + mpn_div_qr_1_preinv (tp, tp, un, &bi);
4349 + un -= (tp[un-1] == 0);
4350 + }
4351 + while (un > 0);
4352 +
4353 + gmp_free (tp);
4354 + return ndigits;
4355 +}
4356 +
4357 +char *
4358 +mpz_get_str (char *sp, int base, const mpz_t u)
4359 +{
4360 + unsigned bits;
4361 + const char *digits;
4362 + mp_size_t un;
4363 + size_t i, sn;
4364 +
4365 + if (base >= 0)
4366 + {
4367 + digits = "0123456789abcdefghijklmnopqrstuvwxyz";
4368 + }
4369 + else
4370 + {
4371 + base = -base;
4372 + digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4373 + }
4374 + if (base <= 1)
4375 + base = 10;
4376 + if (base > 36)
4377 + return NULL;
4378 +
4379 + sn = 1 + mpz_sizeinbase (u, base);
4380 + if (!sp)
4381 + sp = gmp_xalloc (1 + sn);
4382 +
4383 + un = GMP_ABS (u->_mp_size);
4384 +
4385 + if (un == 0)
4386 + {
4387 + sp[0] = '0';
4388 + sp[1] = '\0';
4389 + return sp;
4390 + }
4391 +
4392 + i = 0;
4393 +
4394 + if (u->_mp_size < 0)
4395 + sp[i++] = '-';
4396 +
4397 + bits = mpn_base_power_of_two_p (base);
4398 +
4399 + if (bits)
4400 + /* Not modified in this case. */
4401 + sn = i + mpn_get_str_bits ((unsigned char *) sp + i, bits, u->_mp_d, un);
4402 + else
4403 + {
4404 + struct mpn_base_info info;
4405 + mp_ptr tp;
4406 +
4407 + mpn_get_base_info (&info, base);
4408 + tp = gmp_xalloc_limbs (un);
4409 + mpn_copyi (tp, u->_mp_d, un);
4410 +
4411 + sn = i + mpn_get_str_other ((unsigned char *) sp + i, base, &info, tp, un);
4412 + gmp_free (tp);
4413 + }
4414 +
4415 + for (; i < sn; i++)
4416 + sp[i] = digits[(unsigned char) sp[i]];
4417 +
4418 + sp[sn] = '\0';
4419 + return sp;
4420 +}
4421 +
4422 +int
4423 +mpz_set_str (mpz_t r, const char *sp, int base)
4424 +{
4425 + unsigned bits;
4426 + mp_size_t rn, alloc;
4427 + mp_ptr rp;
4428 + size_t sn;
4429 + int sign;
4430 + unsigned char *dp;
4431 +
4432 + assert (base == 0 || (base >= 2 && base <= 36));
4433 +
4434 + while (isspace( (unsigned char) *sp))
4435 + sp++;
4436 +
4437 + sign = (*sp == '-');
4438 + sp += sign;
4439 +
4440 + if (base == 0)
4441 + {
4442 + if (*sp == '0')
4443 + {
4444 + sp++;
4445 + if (*sp == 'x' || *sp == 'X')
4446 + {
4447 + base = 16;
4448 + sp++;
4449 + }
4450 + else if (*sp == 'b' || *sp == 'B')
4451 + {
4452 + base = 2;
4453 + sp++;
4454 + }
4455 + else
4456 + base = 8;
4457 + }
4458 + else
4459 + base = 10;
4460 + }
4461 +
4462 + sn = strlen (sp);
4463 + dp = gmp_xalloc (sn + (sn == 0));
4464 +
4465 + for (sn = 0; *sp; sp++)
4466 + {
4467 + unsigned digit;
4468 +
4469 + if (isspace ((unsigned char) *sp))
4470 + continue;
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;
4477 + else
4478 + digit = base; /* fail */
4479 +
4480 + if (digit >= base)
4481 + {
4482 + gmp_free (dp);
4483 + r->_mp_size = 0;
4484 + return -1;
4485 + }
4486 +
4487 + dp[sn++] = digit;
4488 + }
4489 +
4490 + bits = mpn_base_power_of_two_p (base);
4491 +
4492 + if (bits > 0)
4493 + {
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);
4497 + }
4498 + else
4499 + {
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);
4505 + }
4506 + assert (rn <= alloc);
4507 + gmp_free (dp);
4508 +
4509 + r->_mp_size = sign ? - rn : rn;
4510 +
4511 + return 0;
4512 +}
4513 +
4514 +int
4515 +mpz_init_set_str (mpz_t r, const char *sp, int base)
4516 +{
4517 + mpz_init (r);
4518 + return mpz_set_str (r, sp, base);
4519 +}
4520 +
4521 +size_t
4522 +mpz_out_str (FILE *stream, int base, const mpz_t x)
4523 +{
4524 + char *str;
4525 + size_t len;
4526 +
4527 + str = mpz_get_str (NULL, base, x);
4528 + len = strlen (str);
4529 + len = fwrite (str, 1, len, stream);
4530 + gmp_free (str);
4531 + return len;
4532 +}
4533 +
4534 +\f
4535 +static int
4536 +gmp_detect_endian (void)
4537 +{
4538 + static const int i = 2;
4539 + const unsigned char *p = (const unsigned char *) &i;
4540 + return 1 - *p;
4541 +}
4542 +
4543 +/* Import and export. Does not support nails. */
4544 +void
4545 +mpz_import (mpz_t r, size_t count, int order, size_t size, int endian,
4546 + size_t nails, const void *src)
4547 +{
4548 + const unsigned char *p;
4549 + ptrdiff_t word_step;
4550 + mp_ptr rp;
4551 + mp_size_t rn;
4552 +
4553 + /* The current (partial) limb. */
4554 + mp_limb_t limb;
4555 + /* The number of bytes already copied to this limb (starting from
4556 + the low end). */
4557 + size_t bytes;
4558 + /* The index where the limb should be stored, when completed. */
4559 + mp_size_t i;
4560 +
4561 + if (nails != 0)
4562 + gmp_die ("mpz_import: Nails not supported.");
4563 +
4564 + assert (order == 1 || order == -1);
4565 + assert (endian >= -1 && endian <= 1);
4566 +
4567 + if (endian == 0)
4568 + endian = gmp_detect_endian ();
4569 +
4570 + p = (unsigned char *) src;
4571 +
4572 + word_step = (order != endian) ? 2 * size : 0;
4573 +
4574 + /* Process bytes from the least significant end, so point p at the
4575 + least significant word. */
4576 + if (order == 1)
4577 + {
4578 + p += size * (count - 1);
4579 + word_step = - word_step;
4580 + }
4581 +
4582 + /* And at least significant byte of that word. */
4583 + if (endian == 1)
4584 + p += (size - 1);
4585 +
4586 + rn = (size * count + sizeof(mp_limb_t) - 1) / sizeof(mp_limb_t);
4587 + rp = MPZ_REALLOC (r, rn);
4588 +
4589 + for (limb = 0, bytes = 0, i = 0; count > 0; count--, p += word_step)
4590 + {
4591 + size_t j;
4592 + for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
4593 + {
4594 + limb |= (mp_limb_t) *p << (bytes++ * CHAR_BIT);
4595 + if (bytes == sizeof(mp_limb_t))
4596 + {
4597 + rp[i++] = limb;
4598 + bytes = 0;
4599 + limb = 0;
4600 + }
4601 + }
4602 + }
4603 + assert (i + (bytes > 0) == rn);
4604 + if (limb != 0)
4605 + rp[i++] = limb;
4606 + else
4607 + i = mpn_normalized_size (rp, i);
4608 +
4609 + r->_mp_size = i;
4610 +}
4611 +
4612 +void *
4613 +mpz_export (void *r, size_t *countp, int order, size_t size, int endian,
4614 + size_t nails, const mpz_t u)
4615 +{
4616 + size_t count;
4617 + mp_size_t un;
4618 +
4619 + if (nails != 0)
4620 + gmp_die ("mpz_import: Nails not supported.");
4621 +
4622 + assert (order == 1 || order == -1);
4623 + assert (endian >= -1 && endian <= 1);
4624 + assert (size > 0 || u->_mp_size == 0);
4625 +
4626 + un = u->_mp_size;
4627 + count = 0;
4628 + if (un != 0)
4629 + {
4630 + size_t k;
4631 + unsigned char *p;
4632 + ptrdiff_t word_step;
4633 + /* The current (partial) limb. */
4634 + mp_limb_t limb;
4635 + /* The number of bytes left to to in this limb. */
4636 + size_t bytes;
4637 + /* The index where the limb was read. */
4638 + mp_size_t i;
4639 +
4640 + un = GMP_ABS (un);
4641 +
4642 + /* Count bytes in top limb. */
4643 + limb = u->_mp_d[un-1];
4644 + assert (limb != 0);
4645 +
4646 + k = 0;
4647 + do {
4648 + k++; limb >>= CHAR_BIT;
4649 + } while (limb != 0);
4650 +
4651 + count = (k + (un-1) * sizeof (mp_limb_t) + size - 1) / size;
4652 +
4653 + if (!r)
4654 + r = gmp_xalloc (count * size);
4655 +
4656 + if (endian == 0)
4657 + endian = gmp_detect_endian ();
4658 +
4659 + p = (unsigned char *) r;
4660 +
4661 + word_step = (order != endian) ? 2 * size : 0;
4662 +
4663 + /* Process bytes from the least significant end, so point p at the
4664 + least significant word. */
4665 + if (order == 1)
4666 + {
4667 + p += size * (count - 1);
4668 + word_step = - word_step;
4669 + }
4670 +
4671 + /* And at least significant byte of that word. */
4672 + if (endian == 1)
4673 + p += (size - 1);
4674 +
4675 + for (bytes = 0, i = 0, k = 0; k < count; k++, p += word_step)
4676 + {
4677 + size_t j;
4678 + for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
4679 + {
4680 + if (bytes == 0)
4681 + {
4682 + if (i < un)
4683 + limb = u->_mp_d[i++];
4684 + bytes = sizeof (mp_limb_t);
4685 + }
4686 + *p = limb;
4687 + limb >>= CHAR_BIT;
4688 + bytes--;
4689 + }
4690 + }
4691 + assert (i == un);
4692 + assert (k == count);
4693 + }
4694 +
4695 + if (countp)
4696 + *countp = count;
4697 +
4698 + return r;
4699 +}