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