1 /* mini-gmp, a minimalistic implementation of a GNU GMP subset.
3 Contributed to the GNU project by Niels Möller
5 Copyright 1991-1997, 1999-2015 Free Software Foundation, Inc.
7 This file is part of the GNU MP Library.
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free Software
19 Foundation; either version 2 of the License, or (at your option) any
22 or both in parallel, as here.
24 The GNU MP Library is distributed in the hope that it will be useful, but
25 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 You should have received copies of the GNU General Public License and the
30 GNU Lesser General Public License along with the GNU MP Library. If not,
31 see https://www.gnu.org/licenses/. */
33 /* NOTE: All functions in this file which are not declared in
34 mini-gmp.h are internal, and are not intended to be compatible
35 neither with GMP nor with future versions of mini-gmp. */
37 /* Much of the material copied from GMP files, including: gmp-impl.h,
38 longlong.h, mpn/generic/add_n.c, mpn/generic/addmul_1.c,
39 mpn/generic/lshift.c, mpn/generic/mul_1.c,
40 mpn/generic/mul_basecase.c, mpn/generic/rshift.c,
41 mpn/generic/sbpi1_div_qr.c, mpn/generic/sub_n.c,
42 mpn/generic/submul_1.c. */
55 #define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
57 #define GMP_LIMB_MAX (~ (mp_limb_t) 0)
58 #define GMP_LIMB_HIGHBIT ((mp_limb_t) 1 << (GMP_LIMB_BITS - 1))
60 #define GMP_HLIMB_BIT ((mp_limb_t) 1 << (GMP_LIMB_BITS / 2))
61 #define GMP_LLIMB_MASK (GMP_HLIMB_BIT - 1)
63 #define GMP_ULONG_BITS (sizeof(uint64_t) * CHAR_BIT)
64 #define GMP_ULONG_HIGHBIT ((uint64_t) 1 << (GMP_ULONG_BITS - 1))
66 #define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
67 #define GMP_NEG_CAST(T,x) (-(int64_t)((T)((x) + 1) - 1))
69 #define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
70 #define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
72 #define gmp_assert_nocarry(x) do { \
73 mp_limb_t __cy = x; if ( __cy != 0 ) {} \
77 #define gmp_clz(count, x) do { \
78 mp_limb_t __clz_x = (x); \
81 (__clz_x & ((mp_limb_t) 0xff << (GMP_LIMB_BITS - 8))) == 0; \
84 for (; (__clz_x & GMP_LIMB_HIGHBIT) == 0; __clz_c++) \
89 #define gmp_ctz(count, x) do { \
90 mp_limb_t __ctz_x = (x); \
91 uint32_t __ctz_c = 0; \
92 gmp_clz (__ctz_c, __ctz_x & - (int64_t)__ctz_x); \
93 (count) = GMP_LIMB_BITS - 1 - (int64_t)__ctz_c; \
96 #define gmp_add_ssaaaa(sh, sl, ah, al, bh, bl) \
100 (sh) = (ah) + (bh) + (__x < (al)); \
104 #define gmp_sub_ddmmss(sh, sl, ah, al, bh, bl) \
108 (sh) = (ah) - (bh) - ((al) < (bl)); \
112 #define gmp_umul_ppmm(w1, w0, u, v) \
114 mp_limb_t __x0, __x1, __x2, __x3; \
115 uint32_t __ul, __vl, __uh, __vh; \
116 mp_limb_t __u = (u), __v = (v); \
118 __ul = __u & GMP_LLIMB_MASK; \
119 __uh = __u >> (GMP_LIMB_BITS / 2); \
120 __vl = __v & GMP_LLIMB_MASK; \
121 __vh = __v >> (GMP_LIMB_BITS / 2); \
123 __x0 = (mp_limb_t) __ul * __vl; \
124 __x1 = (mp_limb_t) __ul * __vh; \
125 __x2 = (mp_limb_t) __uh * __vl; \
126 __x3 = (mp_limb_t) __uh * __vh; \
128 __x1 += __x0 >> (GMP_LIMB_BITS / 2);/* this can't give carry */ \
129 __x1 += __x2; /* but this indeed can */ \
130 if (__x1 < __x2) /* did we get it? */ \
131 __x3 += GMP_HLIMB_BIT; /* yes, add it in the proper pos. */ \
133 (w1) = __x3 + (__x1 >> (GMP_LIMB_BITS / 2)); \
134 (w0) = (__x1 << (GMP_LIMB_BITS / 2)) + (__x0 & GMP_LLIMB_MASK); \
137 #define gmp_udiv_qrnnd_preinv(q, r, nh, nl, d, di) \
139 mp_limb_t _qh, _ql, _r, _mask; \
140 gmp_umul_ppmm (_qh, _ql, (nh), (di)); \
141 gmp_add_ssaaaa (_qh, _ql, _qh, _ql, (nh) + 1, (nl)); \
142 _r = (nl) - _qh * (d); \
143 _mask = -(_r > _ql); /* both > and >= are OK */ \
156 #define gmp_udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv) \
158 mp_limb_t _q0, _t1, _t0, _mask; \
159 gmp_umul_ppmm ((q), _q0, (n2), (dinv)); \
160 gmp_add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1)); \
162 /* Compute the two most significant limbs of n - q'd */ \
163 (r1) = (n1) - (d1) * (q); \
164 gmp_sub_ddmmss ((r1), (r0), (r1), (n0), (d1), (d0)); \
165 gmp_umul_ppmm (_t1, _t0, (d0), (q)); \
166 gmp_sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0); \
169 /* Conditionally adjust q and the remainders */ \
170 _mask = -((r1) >= _q0); \
172 gmp_add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0)); \
175 if ((r1) > (d1) || (r0) >= (d0)) \
178 gmp_sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0)); \
184 #define MP_LIMB_T_SWAP(x, y) \
186 mp_limb_t __mp_limb_t_swap__tmp = (x); \
188 (y) = __mp_limb_t_swap__tmp; \
190 #define MP_SIZE_T_SWAP(x, y) \
192 mp_size_t __mp_size_t_swap__tmp = (x); \
194 (y) = __mp_size_t_swap__tmp; \
196 #define MP_SIZE_sT_SWAP(x, y) \
198 uint32_t __mp_size_t_swap__tmp = (x); \
200 (y) = __mp_size_t_swap__tmp; \
203 #define MP_BITCNT_T_SWAP(x,y) \
205 mp_bitcnt_t __mp_bitcnt_t_swap__tmp = (x); \
207 (y) = __mp_bitcnt_t_swap__tmp; \
209 #define MP_PTR_SWAP(x, y) \
211 mp_ptr __mp_ptr_swap__tmp = (x); \
213 (y) = __mp_ptr_swap__tmp; \
215 #define MP_SRCPTR_SWAP(x, y) \
217 mp_srcptr __mp_srcptr_swap__tmp = (x); \
219 (y) = __mp_srcptr_swap__tmp; \
222 #define MPN_PTR_SWAP(xp,xs, yp,ys) \
224 MP_PTR_SWAP (xp, yp); \
225 MP_SIZE_T_SWAP (xs, ys); \
227 #define MPN_SRCPTR_SWAP(xp,xs, yp,ys) \
229 MP_SRCPTR_SWAP (xp, yp); \
230 MP_SIZE_T_SWAP (xs, ys); \
233 #define MPZ_PTR_SWAP(x, y) \
235 mpz_ptr __mpz_ptr_swap__tmp = (x); \
237 (y) = __mpz_ptr_swap__tmp; \
239 #define MPZ_SRCPTR_SWAP(x, y) \
241 mpz_srcptr __mpz_srcptr_swap__tmp = (x); \
243 (y) = __mpz_srcptr_swap__tmp; \
246 /* Realloc for an mpz_t WHAT if it has less than NEEDED limbs. */
247 #define MPZ_REALLOC(z,n) ((n) > (z)->_mp_alloc \
252 struct gmp_div_inverse
254 /* Normalization shift count. */
256 /* Normalized divisor (d0 unused for mpn_div_qr_1) */
258 /* Inverse, for 2/1 or 3/2. */
263 enum mpz_div_round_mode { GMP_DIV_FLOOR, GMP_DIV_CEIL, GMP_DIV_TRUNC };
266 const int mp_bits_per_limb = GMP_LIMB_BITS;
270 /* bb is the largest power of the base which fits in one limb, and
271 exp is the corresponding exponent. */
278 /* Memory allocation and other helper functions. */
281 gmp_default_alloc (size_t size)
289 gmp_die("gmp_default_alloc: Virtual memory exhausted.");
295 gmp_default_realloc (void *old, size_t old_size, size_t new_size)
299 p = realloc (old, new_size);
302 gmp_die("gmp_default_realloc: Virtual memory exhausted.");
308 gmp_default_free (void *p, size_t size)
313 static void * (*gmp_allocate_func) (size_t) = gmp_default_alloc;
314 static void * (*gmp_reallocate_func) (void *, size_t, size_t) = gmp_default_realloc;
315 static void (*gmp_free_func) (void *, size_t) = gmp_default_free;
318 mp_get_memory_functions (void *(**alloc_func) (size_t),
319 void *(**realloc_func) (void *, size_t, size_t),
320 void (**free_func) (void *, size_t))
323 *alloc_func = gmp_allocate_func;
326 *realloc_func = gmp_reallocate_func;
329 *free_func = gmp_free_func;
333 mp_set_memory_functions (void *(*alloc_func) (size_t),
334 void *(*realloc_func) (void *, size_t, size_t),
335 void (*free_func) (void *, size_t))
338 alloc_func = gmp_default_alloc;
340 realloc_func = gmp_default_realloc;
342 free_func = gmp_default_free;
344 gmp_allocate_func = alloc_func;
345 gmp_reallocate_func = realloc_func;
346 gmp_free_func = free_func;
349 #define gmp_xalloc(size) ((*gmp_allocate_func)((size)))
350 #define gmp_free(p) ((*gmp_free_func) ((p), 0))
360 int mpn_zero_p(mp_srcptr rp, mp_size_t n)
362 return mpn_normalized_size (rp, n) == 0;
369 mpn_mul_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
371 mpn_mul (rp, ap, n, bp, n);
375 mpn_sqr (mp_ptr rp, mp_srcptr ap, mp_size_t n)
377 mpn_mul (rp, ap, n, ap, n);
383 mpn_common_scan (mp_limb_t limb, mp_size_t i, mp_srcptr up, mp_size_t un,
388 assert (ux == 0 || ux == GMP_LIMB_MAX);
389 assert (0 <= i && i <= un );
395 return (ux == 0 ? ~(mp_bitcnt_t) 0 : un * GMP_LIMB_BITS);
399 return (mp_bitcnt_t) i * GMP_LIMB_BITS + cnt;
403 mpn_scan1 (mp_srcptr ptr, mp_bitcnt_t bit)
406 i = bit / GMP_LIMB_BITS;
408 return mpn_common_scan ( ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
413 mpn_scan0 (mp_srcptr ptr, mp_bitcnt_t bit)
416 i = bit / GMP_LIMB_BITS;
418 return mpn_common_scan (~ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
419 i, ptr, i, GMP_LIMB_MAX);
430 mpn_div_qr_2 (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
431 mp_limb_t d1, mp_limb_t d0)
433 struct gmp_div_inverse inv;
436 mpn_div_qr_2_invert (&inv, d1, d0);
437 mpn_div_qr_2_preinv (qp, rp, np, nn, &inv);
442 /* MPN base conversion. */
445 mpn_limb_size_in_base_2 (mp_limb_t u)
451 return GMP_LIMB_BITS - shift;
455 mpn_get_str_bits (uint8_t *sp, uint32_t bits, mp_srcptr up, mp_size_t un)
462 sn = ((un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1])
465 mask = (1U << bits) - 1;
467 for (i = 0, j = sn, shift = 0; j-- > 0;)
469 uint8_t digit = up[i] >> shift;
473 if (shift >= GMP_LIMB_BITS && ++i < un)
475 shift -= GMP_LIMB_BITS;
476 digit |= up[i] << (bits - shift);
478 sp[j] = digit & mask;
483 /* We generate digits from the least significant end, and reverse at
486 mpn_limb_get_str (uint8_t *sp, mp_limb_t w,
487 const struct gmp_div_inverse *binv)
490 for (i = 0; w > 0; i++)
494 h = w >> (GMP_LIMB_BITS - binv->shift);
495 l = w << binv->shift;
497 gmp_udiv_qrnnd_preinv (w, r, h, l, binv->d1, binv->di);
498 assert ( (r << (GMP_LIMB_BITS - binv->shift)) == 0);
507 mpn_get_str_other (uint8_t *sp,
508 int base, const struct mpn_base_info *info,
509 mp_ptr up, mp_size_t un)
511 struct gmp_div_inverse binv;
515 mpn_div_qr_1_invert (&binv, base);
521 struct gmp_div_inverse bbinv;
522 mpn_div_qr_1_invert (&bbinv, info->bb);
528 w = mpn_div_qr_1_preinv (up, up, un, &bbinv);
529 un -= (up[un-1] == 0);
530 done = mpn_limb_get_str (sp + sn, w, &binv);
532 for (sn += done; done < info->exp; done++)
537 sn += mpn_limb_get_str (sp + sn, up[0], &binv);
540 for (i = 0; 2*i + 1 < sn; i++)
543 sp[i] = sp[sn - i - 1];
551 mpn_get_str (uint8_t *sp, int base, mp_ptr up, mp_size_t un)
556 assert (up[un-1] > 0);
558 bits = mpn_base_power_of_two_p (base);
560 return mpn_get_str_bits (sp, bits, up, un);
563 struct mpn_base_info info;
565 mpn_get_base_info (&info, base);
566 return mpn_get_str_other (sp, base, &info, up, un);
571 mpn_set_str (mp_ptr rp, const uint8_t *sp, size_t sn, int base)
578 bits = mpn_base_power_of_two_p (base);
580 return mpn_set_str_bits (rp, sp, sn, bits);
583 struct mpn_base_info info;
585 mpn_get_base_info (&info, base);
586 return mpn_set_str_other (rp, sp, sn, base, &info);
596 /* MPZ assignment and basic conversions. */
599 mpz_init_set_si (mpz_t r, int64_t x)
609 mpz_fits_slong_p (const mpz_t u)
611 mp_size_t us = u->_mp_size;
614 return u->_mp_d[0] < GMP_LIMB_HIGHBIT;
616 return u->_mp_d[0] <= GMP_LIMB_HIGHBIT;
622 mpz_fits_ulong_p (const mpz_t u)
624 mp_size_t us = u->_mp_size;
626 return (us == (us > 0));
630 mpz_get_si (const mpz_t u)
632 mp_size_t us = u->_mp_size;
635 return (long) (u->_mp_d[0] & ~GMP_LIMB_HIGHBIT);
637 return (long) (- u->_mp_d[0] | GMP_LIMB_HIGHBIT);
645 mpz_size (const mpz_t u)
647 return GMP_ABS (u->_mp_size);
651 mpz_getlimbn (const mpz_t u, mp_size_t n)
653 if (n >= 0 && n < GMP_ABS (u->_mp_size))
660 mpz_realloc2 (mpz_t x, mp_bitcnt_t n)
662 mpz_realloc (x, 1 + (n - (n != 0)) / GMP_LIMB_BITS);
666 mpz_limbs_read (mpz_srcptr x)
672 mpz_limbs_modify (mpz_t x, mp_size_t n)
675 return MPZ_REALLOC (x, n);
679 mpz_limbs_write (mpz_t x, mp_size_t n)
681 return mpz_limbs_modify (x, n);
685 mpz_limbs_finish (mpz_t x, mp_size_t xs)
688 xn = mpn_normalized_size (x->_mp_d, GMP_ABS (xs));
689 x->_mp_size = xs < 0 ? -xn : xn;
693 mpz_roinit_n (mpz_t x, mp_srcptr xp, mp_size_t xs)
696 x->_mp_d = (mp_ptr) xp;
697 mpz_limbs_finish (x, xs);
702 /* Conversions and comparison to double. */
704 mpz_set_d (mpz_t r, double x)
713 /* x != x is true when x is a NaN, and x == x * 0.5 is true when x is
715 if (x != x || x == x * 0.5)
730 B = 2.0 * (double) GMP_LIMB_HIGHBIT;
732 for (rn = 1; x >= B; rn++)
735 rp = MPZ_REALLOC (r, rn);
751 r->_mp_size = sign ? - rn : rn;
755 mpz_init_set_d (mpz_t r, double x)
762 mpz_get_d (const mpz_t u)
766 double B = 2.0 * (double) GMP_LIMB_HIGHBIT;
768 un = GMP_ABS (u->_mp_size);
775 x = B*x + u->_mp_d[--un];
784 mpz_cmpabs_d (const mpz_t x, double d)
797 B = 2.0 * (double) GMP_LIMB_HIGHBIT;
800 /* Scale d so it can be compared with the top limb. */
801 for (i = 1; i < xn; i++)
807 /* Compare floor(d) to top limb, subtract and cancel when equal. */
808 for (i = xn; i-- > 0;)
825 mpz_cmp_d (const mpz_t x, double d)
832 return -mpz_cmpabs_d (x, d);
839 return mpz_cmpabs_d (x, d);
844 /* MPZ comparisons and the like. */
846 mpz_sgn (const mpz_t u)
848 mp_size_t usize = u->_mp_size;
850 return (usize > 0) - (usize < 0);
854 mpz_cmp_si (const mpz_t u, long v)
856 mp_size_t usize = u->_mp_size;
861 return mpz_cmp_ui (u, v);
864 else /* usize == -1 */
866 mp_limb_t ul = u->_mp_d[0];
867 if ((mp_limb_t)GMP_NEG_CAST (uint64_t, v) < ul)
870 return (mp_limb_t)GMP_NEG_CAST (uint64_t, v) > ul;
875 mpz_cmp_ui (const mpz_t u, uint64_t v)
877 mp_size_t usize = u->_mp_size;
885 mp_limb_t ul = (usize > 0) ? u->_mp_d[0] : 0;
886 return (ul > v) - (ul < v);
891 mpz_cmpabs_ui (const mpz_t u, uint64_t v)
893 mp_size_t un = GMP_ABS (u->_mp_size);
899 ul = (un == 1) ? u->_mp_d[0] : 0;
901 return (ul > v) - (ul < v);
905 mpz_cmpabs (const mpz_t u, const mpz_t v)
907 return mpn_cmp4 (u->_mp_d, GMP_ABS (u->_mp_size),
908 v->_mp_d, GMP_ABS (v->_mp_size));
912 mpz_abs (mpz_t r, const mpz_t u)
915 r->_mp_size = GMP_ABS (r->_mp_size);
919 mpz_neg (mpz_t r, const mpz_t u)
922 r->_mp_size = -r->_mp_size;
927 /* MPZ addition and subtraction */
936 mpz_ui_sub (mpz_t r, uint64_t a, const mpz_t b)
939 r->_mp_size = mpz_abs_add_ui (r, b, a);
941 r->_mp_size = -mpz_abs_sub_ui (r, b, a);
948 /* MPZ multiplication */
950 mpz_mul_si (mpz_t r, const mpz_t u, long int v)
954 mpz_mul_ui (r, u, GMP_NEG_CAST (uint64_t, v));
958 mpz_mul_ui (r, u, (uint64_t) v);
965 mpz_addmul_ui (mpz_t r, const mpz_t u, uint64_t v)
969 mpz_mul_ui (t, u, v);
975 mpz_submul_ui (mpz_t r, const mpz_t u, uint64_t v)
979 mpz_mul_ui (t, u, v);
985 mpz_addmul (mpz_t r, const mpz_t u, const mpz_t v)
995 mpz_submul (mpz_t r, const mpz_t u, const mpz_t v)
1008 mpz_fdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
1010 mpz_div_qr (q, r, n, d, GMP_DIV_FLOOR);
1015 mpz_cdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
1017 mpz_div_qr (q, NULL, n, d, GMP_DIV_CEIL);
1021 mpz_fdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
1023 mpz_div_qr (q, NULL, n, d, GMP_DIV_FLOOR);
1027 mpz_tdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
1029 mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC);
1033 mpz_cdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
1035 mpz_div_qr (NULL, r, n, d, GMP_DIV_CEIL);
1039 mpz_fdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
1041 mpz_div_qr (NULL, r, n, d, GMP_DIV_FLOOR);
1045 mpz_tdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
1047 mpz_div_qr (NULL, r, n, d, GMP_DIV_TRUNC);
1051 mpz_mod (mpz_t r, const mpz_t n, const mpz_t d)
1053 mpz_div_qr (NULL, r, n, d, d->_mp_size >= 0 ? GMP_DIV_FLOOR : GMP_DIV_CEIL);
1057 mpz_div_q_2exp (mpz_t q, const mpz_t u, mp_bitcnt_t bit_index,
1058 enum mpz_div_round_mode mode)
1071 limb_cnt = bit_index / GMP_LIMB_BITS;
1072 qn = GMP_ABS (un) - limb_cnt;
1073 bit_index %= GMP_LIMB_BITS;
1075 if (mode == ((un > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* un != 0 here. */
1076 /* Note: Below, the final indexing at limb_cnt is valid because at
1077 that point we have qn > 0. */
1079 || !mpn_zero_p (u->_mp_d, limb_cnt)
1080 || (u->_mp_d[limb_cnt]
1081 & (((mp_limb_t) 1 << bit_index) - 1)));
1090 qp = MPZ_REALLOC (q, qn);
1094 mpn_rshift (qp, u->_mp_d + limb_cnt, qn, bit_index);
1095 qn -= qp[qn - 1] == 0;
1099 mpn_copyi (qp, u->_mp_d + limb_cnt, qn);
1106 mpz_add_ui (q, q, 1);
1112 mpz_div_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bit_index,
1113 enum mpz_div_round_mode mode)
1115 mp_size_t us, un, rn;
1120 if (us == 0 || bit_index == 0)
1125 rn = (bit_index + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
1128 rp = MPZ_REALLOC (r, rn);
1131 mask = GMP_LIMB_MAX >> (rn * GMP_LIMB_BITS - bit_index);
1135 /* Quotient (with truncation) is zero, and remainder is
1137 if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
1139 /* Have to negate and sign extend. */
1143 for (cy = 1, i = 0; i < un; i++)
1145 mp_limb_t s = ~u->_mp_d[i] + cy;
1150 for (; i < rn - 1; i++)
1151 rp[i] = GMP_LIMB_MAX;
1160 mpn_copyi (rp, u->_mp_d, un);
1168 mpn_copyi (rp, u->_mp_d, rn - 1);
1170 rp[rn-1] = u->_mp_d[rn-1] & mask;
1172 if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
1174 /* If r != 0, compute 2^{bit_count} - r. */
1177 for (i = 0; i < rn && rp[i] == 0; i++)
1181 /* r > 0, need to flip sign. */
1188 /* us is not used for anything else, so we can modify it
1189 here to indicate flipped sign. */
1194 rn = mpn_normalized_size (rp, rn);
1195 r->_mp_size = us < 0 ? -rn : rn;
1199 mpz_cdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
1201 mpz_div_q_2exp (r, u, cnt, GMP_DIV_CEIL);
1205 mpz_fdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
1207 mpz_div_q_2exp (r, u, cnt, GMP_DIV_FLOOR);
1211 mpz_tdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
1213 mpz_div_q_2exp (r, u, cnt, GMP_DIV_TRUNC);
1217 mpz_cdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
1219 mpz_div_r_2exp (r, u, cnt, GMP_DIV_CEIL);
1223 mpz_fdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
1225 mpz_div_r_2exp (r, u, cnt, GMP_DIV_FLOOR);
1229 mpz_tdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
1231 mpz_div_r_2exp (r, u, cnt, GMP_DIV_TRUNC);
1235 mpz_divexact (mpz_t q, const mpz_t n, const mpz_t d)
1237 gmp_assert_nocarry (mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC));
1241 mpz_divisible_p (const mpz_t n, const mpz_t d)
1243 return mpz_div_qr (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
1247 mpz_congruent_p (const mpz_t a, const mpz_t b, const mpz_t m)
1252 /* a == b (mod 0) iff a == b */
1253 if (mpz_sgn (m) == 0)
1254 return (mpz_cmp (a, b) == 0);
1258 res = mpz_divisible_p (t, m);
1266 mpz_cdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, uint64_t d)
1268 return mpz_div_qr_ui (q, r, n, d, GMP_DIV_CEIL);
1272 mpz_fdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, uint64_t d)
1274 return mpz_div_qr_ui (q, r, n, d, GMP_DIV_FLOOR);
1278 mpz_tdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, uint64_t d)
1280 return mpz_div_qr_ui (q, r, n, d, GMP_DIV_TRUNC);
1284 mpz_cdiv_q_ui (mpz_t q, const mpz_t n, uint64_t d)
1286 return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_CEIL);
1290 mpz_fdiv_q_ui (mpz_t q, const mpz_t n, uint64_t d)
1292 return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_FLOOR);
1296 mpz_tdiv_q_ui (mpz_t q, const mpz_t n, uint64_t d)
1298 return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC);
1302 mpz_cdiv_r_ui (mpz_t r, const mpz_t n, uint64_t d)
1304 return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_CEIL);
1307 mpz_fdiv_r_ui (mpz_t r, const mpz_t n, uint64_t d)
1309 return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
1312 mpz_tdiv_r_ui (mpz_t r, const mpz_t n, uint64_t d)
1314 return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_TRUNC);
1318 mpz_cdiv_ui (const mpz_t n, uint64_t d)
1320 return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_CEIL);
1324 mpz_fdiv_ui (const mpz_t n, uint64_t d)
1326 return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_FLOOR);
1330 mpz_tdiv_ui (const mpz_t n, uint64_t d)
1332 return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC);
1336 mpz_mod_ui (mpz_t r, const mpz_t n, uint64_t d)
1338 return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
1342 mpz_divexact_ui (mpz_t q, const mpz_t n, uint64_t d)
1344 gmp_assert_nocarry (mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC));
1348 mpz_divisible_ui_p (const mpz_t n, uint64_t d)
1350 return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
1356 mpn_gcd_11 (mp_limb_t u, mp_limb_t v)
1360 assert ( (u | v) > 0);
1367 gmp_ctz (shift, u | v);
1373 MP_LIMB_T_SWAP (u, v);
1375 while ( (v & 1) == 0)
1385 while ( (u & 1) == 0);
1392 while ( (v & 1) == 0);
1399 mpz_gcd_ui (mpz_t g, const mpz_t u, uint64_t v)
1410 un = GMP_ABS (u->_mp_size);
1412 v = mpn_gcd_11 (mpn_div_qr_1 (NULL, u->_mp_d, un, v), v);
1422 mpz_make_odd (mpz_t r)
1426 assert (r->_mp_size > 0);
1427 /* Count trailing zeros, equivalent to mpn_scan1, because we know that there is a 1 */
1428 shift = mpn_common_scan (r->_mp_d[0], 0, r->_mp_d, 0, 0);
1429 mpz_tdiv_q_2exp (r, r, shift);
1435 mpz_gcd (mpz_t g, const mpz_t u, const mpz_t v)
1438 mp_bitcnt_t uz, vz, gz;
1440 if (u->_mp_size == 0)
1445 if (v->_mp_size == 0)
1455 uz = mpz_make_odd (tu);
1457 vz = mpz_make_odd (tv);
1458 gz = GMP_MIN (uz, vz);
1460 if (tu->_mp_size < tv->_mp_size)
1463 mpz_tdiv_r (tu, tu, tv);
1464 if (tu->_mp_size == 0)
1474 c = mpz_cmp (tu, tv);
1483 if (tv->_mp_size == 1)
1485 mp_limb_t vl = tv->_mp_d[0];
1486 mp_limb_t ul = mpz_tdiv_ui (tu, vl);
1487 mpz_set_ui (g, mpn_gcd_11 (ul, vl));
1490 mpz_sub (tu, tu, tv);
1494 mpz_mul_2exp (g, g, gz);
1498 mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, const mpz_t u, const mpz_t v)
1500 mpz_t tu, tv, s0, s1, t0, t1;
1501 mp_bitcnt_t uz, vz, gz;
1504 if (u->_mp_size == 0)
1506 /* g = 0 u + sgn(v) v */
1507 int64_t sign = mpz_sgn (v);
1512 mpz_set_si (t, sign);
1516 if (v->_mp_size == 0)
1518 /* g = sgn(u) u + 0 v */
1519 int64_t sign = mpz_sgn (u);
1522 mpz_set_si (s, sign);
1536 uz = mpz_make_odd (tu);
1538 vz = mpz_make_odd (tv);
1539 gz = GMP_MIN (uz, vz);
1544 /* Cofactors corresponding to odd gcd. gz handled later. */
1545 if (tu->_mp_size < tv->_mp_size)
1548 MPZ_SRCPTR_SWAP (u, v);
1549 MPZ_PTR_SWAP (s, t);
1550 MP_BITCNT_T_SWAP (uz, vz);
1558 * where u and v denote the inputs with common factors of two
1559 * eliminated, and det (s0, t0; s1, t1) = 2^p. Then
1561 * 2^p tu = s1 u - t1 v
1562 * 2^p tv = -s0 u + t0 v
1565 /* After initial division, tu = q tv + tu', we have
1567 * u = 2^uz (tu' + q tv)
1572 * t0 = 2^uz, t1 = 2^uz q
1576 mpz_setbit (t0, uz);
1577 mpz_tdiv_qr (t1, tu, tu, tv);
1578 mpz_mul_2exp (t1, t1, uz);
1580 mpz_setbit (s1, vz);
1583 if (tu->_mp_size > 0)
1586 shift = mpz_make_odd (tu);
1587 mpz_mul_2exp (t0, t0, shift);
1588 mpz_mul_2exp (s0, s0, shift);
1594 c = mpz_cmp (tu, tv);
1602 * u = t0 tu + t1 (tv' + tu) = (t0 + t1) tu + t1 tv'
1603 * v = s0 tu + s1 (tv' + tu) = (s0 + s1) tu + s1 tv' */
1605 mpz_sub (tv, tv, tu);
1606 mpz_add (t0, t0, t1);
1607 mpz_add (s0, s0, s1);
1609 shift = mpz_make_odd (tv);
1610 mpz_mul_2exp (t1, t1, shift);
1611 mpz_mul_2exp (s1, s1, shift);
1615 mpz_sub (tu, tu, tv);
1616 mpz_add (t1, t0, t1);
1617 mpz_add (s1, s0, s1);
1619 shift = mpz_make_odd (tu);
1620 mpz_mul_2exp (t0, t0, shift);
1621 mpz_mul_2exp (s0, s0, shift);
1627 /* Now tv = odd part of gcd, and -s0 and t0 are corresponding
1630 mpz_mul_2exp (tv, tv, gz);
1633 /* 2^p g = s0 u + t0 v. Eliminate one factor of two at a time. To
1634 adjust cofactors, we need u / g and v / g */
1636 mpz_divexact (s1, v, tv);
1638 mpz_divexact (t1, u, tv);
1643 /* s0 u + t0 v = (s0 - v/g) u - (t0 + u/g) v */
1644 if (mpz_odd_p (s0) || mpz_odd_p (t0))
1646 mpz_sub (s0, s0, s1);
1647 mpz_add (t0, t0, t1);
1649 mpz_divexact_ui (s0, s0, 2);
1650 mpz_divexact_ui (t0, t0, 2);
1653 /* Arrange so that |s| < |u| / 2g */
1654 mpz_add (s1, s0, s1);
1655 if (mpz_cmpabs (s0, s1) > 0)
1658 mpz_sub (t0, t0, t1);
1660 if (u->_mp_size < 0)
1662 if (v->_mp_size < 0)
1680 mpz_lcm (mpz_t r, const mpz_t u, const mpz_t v)
1684 if (u->_mp_size == 0 || v->_mp_size == 0)
1693 mpz_divexact (g, u, g);
1701 mpz_lcm_ui (mpz_t r, const mpz_t u, uint64_t v)
1703 if (v == 0 || u->_mp_size == 0)
1709 v /= mpz_gcd_ui (NULL, u, v);
1710 mpz_mul_ui (r, u, v);
1716 mpz_invert (mpz_t r, const mpz_t u, const mpz_t m)
1721 if (u->_mp_size == 0 || mpz_cmpabs_ui (m, 1) <= 0)
1727 mpz_gcdext (g, tr, NULL, u, m);
1728 invertible = (mpz_cmp_ui (g, 1) == 0);
1732 if (tr->_mp_size < 0)
1734 if (m->_mp_size >= 0)
1735 mpz_add (tr, tr, m);
1737 mpz_sub (tr, tr, m);
1748 /* Higher level operations (sqrt, pow and root) */
1751 mpz_pow_ui (mpz_t r, const mpz_t b, uint64_t e)
1755 mpz_init_set_ui (tr, 1);
1757 bit = GMP_ULONG_HIGHBIT;
1760 mpz_mul (tr, tr, tr);
1762 mpz_mul (tr, tr, b);
1772 mpz_ui_pow_ui (mpz_t r, uint64_t blimb, uint64_t e)
1775 mpz_pow_ui (r, mpz_roinit_n (b, &blimb, 1), e);
1779 mpz_powm (mpz_t r, const mpz_t b, const mpz_t e, const mpz_t m)
1785 struct gmp_div_inverse minv;
1789 en = GMP_ABS (e->_mp_size);
1790 mn = GMP_ABS (m->_mp_size);
1792 gmp_die ("mpz_powm: Zero modulo.");
1801 mpn_div_qr_invert (&minv, mp, mn);
1806 /* To avoid shifts, we do all our reductions, except the final
1807 one, using a *normalized* m. */
1810 tp = gmp_xalloc_limbs (mn);
1811 gmp_assert_nocarry (mpn_lshift (tp, mp, mn, shift));
1817 if (e->_mp_size < 0)
1819 if (!mpz_invert (base, b, m))
1820 gmp_die ("mpz_powm: Negative exponent and non-invertible base.");
1827 bn = base->_mp_size;
1830 mpn_div_qr_preinv (NULL, base->_mp_d, base->_mp_size, mp, mn, &minv);
1834 /* We have reduced the absolute value. Now take care of the
1835 sign. Note that we get zero represented non-canonically as
1837 if (b->_mp_size < 0)
1839 mp_ptr bp = MPZ_REALLOC (base, mn);
1840 gmp_assert_nocarry (mpn_sub (bp, mp, mn, bp, bn));
1843 base->_mp_size = mpn_normalized_size (base->_mp_d, bn);
1845 mpz_init_set_ui (tr, 1);
1849 mp_limb_t w = e->_mp_d[en];
1852 bit = GMP_LIMB_HIGHBIT;
1855 mpz_mul (tr, tr, tr);
1857 mpz_mul (tr, tr, base);
1858 if (tr->_mp_size > mn)
1860 mpn_div_qr_preinv (NULL, tr->_mp_d, tr->_mp_size, mp, mn, &minv);
1861 tr->_mp_size = mpn_normalized_size (tr->_mp_d, mn);
1868 /* Final reduction */
1869 if (tr->_mp_size >= mn)
1872 mpn_div_qr_preinv (NULL, tr->_mp_d, tr->_mp_size, mp, mn, &minv);
1873 tr->_mp_size = mpn_normalized_size (tr->_mp_d, mn);
1884 mpz_powm_ui (mpz_t r, const mpz_t b, uint64_t elimb, const mpz_t m)
1887 mpz_powm (r, b, mpz_roinit_n (e, &elimb, 1), m);
1890 /* x=trunc(y^(1/z)), r=y-x^z */
1892 mpz_rootrem (mpz_t x, mpz_t r, const mpz_t y, uint64_t z)
1897 sgn = y->_mp_size < 0;
1898 if ((~z & sgn) != 0)
1899 gmp_die ("mpz_rootrem: Negative argument, with even root.");
1901 gmp_die ("mpz_rootrem: Zeroth root.");
1903 if (mpz_cmpabs_ui (y, 1) <= 0) {
1914 tb = mpz_sizeinbase (y, 2) / z + 1;
1915 mpz_init2 (t, tb + 1);
1919 if (z == 2) /* simplify sqrt loop: z-1 == 1 */
1921 mpz_swap (u, t); /* u = x */
1922 mpz_tdiv_q (t, y, u); /* t = y/x */
1923 mpz_add (t, t, u); /* t = y/x + x */
1924 mpz_tdiv_q_2exp (t, t, 1); /* x'= (y/x + x)/2 */
1925 } while (mpz_cmpabs (t, u) < 0); /* |x'| < |x| */
1934 mpz_swap (u, t); /* u = x */
1935 mpz_pow_ui (t, u, z - 1); /* t = x^(z-1) */
1936 mpz_tdiv_q (t, y, t); /* t = y/x^(z-1) */
1937 mpz_mul_ui (v, u, z - 1); /* v = x*(z-1) */
1938 mpz_add (t, t, v); /* t = y/x^(z-1) + x*(z-1) */
1939 mpz_tdiv_q_ui (t, t, z); /* x'=(y/x^(z-1) + x*(z-1))/z */
1940 } while (mpz_cmpabs (t, u) < 0); /* |x'| < |x| */
1946 mpz_pow_ui (t, u, z);
1956 mpz_root (mpz_t x, const mpz_t y, uint64_t z)
1962 mpz_rootrem (x, r, y, z);
1963 res = r->_mp_size == 0;
1969 /* Compute s = floor(sqrt(u)) and r = u - s^2. Allows r == NULL */
1971 mpz_sqrtrem (mpz_t s, mpz_t r, const mpz_t u)
1973 mpz_rootrem (s, r, u, 2);
1977 mpz_sqrt (mpz_t s, const mpz_t u)
1979 mpz_rootrem (s, NULL, u, 2);
1983 mpz_perfect_square_p (const mpz_t u)
1985 if (u->_mp_size <= 0)
1986 return (u->_mp_size == 0);
1988 return mpz_root (NULL, u, 2);
1992 mpn_perfect_square_p (mp_srcptr p, mp_size_t n)
1997 assert (p [n-1] != 0);
1998 return mpz_root (NULL, mpz_roinit_n (t, p, n), 2);
2002 mpn_sqrtrem (mp_ptr sp, mp_ptr rp, mp_srcptr p, mp_size_t n)
2008 assert (p [n-1] != 0);
2012 mpz_rootrem (s, r, mpz_roinit_n (u, p, n), 2);
2014 assert (s->_mp_size == (n+1)/2);
2015 mpn_copyd (sp, s->_mp_d, s->_mp_size);
2019 mpn_copyd (rp, r->_mp_d, res);
2027 mpz_fac_ui (mpz_t x, uint64_t n)
2029 mpz_set_ui (x, n + (n == 0));
2031 mpz_mul_ui (x, x, --n);
2035 mpz_bin_uiui (mpz_t r, uint64_t n, uint64_t k)
2039 mpz_set_ui (r, k <= n);
2042 k = (k <= n) ? n - k : 0;
2048 mpz_mul_ui (r, r, n--);
2050 mpz_divexact (r, r, t);
2055 /* Primality testing */
2057 gmp_millerrabin (const mpz_t n, const mpz_t nm1, mpz_t y,
2058 const mpz_t q, mp_bitcnt_t k)
2062 /* Caller must initialize y to the base. */
2063 mpz_powm (y, y, q, n);
2065 if (mpz_cmp_ui (y, 1) == 0 || mpz_cmp (y, nm1) == 0)
2070 mpz_powm_ui (y, y, 2, n);
2071 if (mpz_cmp (y, nm1) == 0)
2073 /* y == 1 means that the previous y was a non-trivial square root
2074 of 1 (mod n). y == 0 means that n is a power of the base.
2075 In either case, n is not prime. */
2076 if (mpz_cmp_ui (y, 1) <= 0)
2082 /* This product is 0xc0cfd797, and fits in 32 bits. */
2083 #define GMP_PRIME_PRODUCT \
2084 (3UL*5UL*7UL*11UL*13UL*17UL*19UL*23UL*29UL)
2086 /* Bit (p+1)/2 is set, for each odd prime <= 61 */
2087 #define GMP_PRIME_MASK 0xc96996dcUL
2090 mpz_probab_prime_p (const mpz_t n, int reps)
2099 /* Note that we use the absolute value of n only, for compatibility
2100 with the real GMP. */
2102 return (mpz_cmpabs_ui (n, 2) == 0) ? 2 : 0;
2104 /* Above test excludes n == 0 */
2105 assert (n->_mp_size != 0);
2107 if (mpz_cmpabs_ui (n, 64) < 0)
2108 return (GMP_PRIME_MASK >> (n->_mp_d[0] >> 1)) & 2;
2110 if (mpz_gcd_ui (NULL, n, GMP_PRIME_PRODUCT) != 1)
2113 /* All prime factors are >= 31. */
2114 if (mpz_cmpabs_ui (n, 31*31) < 0)
2117 /* Use Miller-Rabin, with a deterministic sequence of bases, a[j] =
2118 j^2 + j + 41 using Euler's polynomial. We potentially stop early,
2119 if a[j] >= n - 1. Since n >= 31*31, this can happen only if reps >
2120 30 (a[30] == 971 > 31*31 == 961). */
2126 /* Find q and k, where q is odd and n = 1 + 2**k * q. */
2127 nm1->_mp_size = mpz_abs_sub_ui (nm1, n, 1);
2128 k = mpz_scan1 (nm1, 0);
2129 mpz_tdiv_q_2exp (q, nm1, k);
2131 for (j = 0, is_prime = 1; is_prime & (j < reps); j++)
2133 mpz_set_ui (y, (uint64_t) j*j+j+41);
2134 if (mpz_cmp (y, nm1) >= 0)
2136 /* Don't try any further bases. This "early" break does not affect
2137 the result for any reasonable reps value (<=5000 was tested) */
2141 is_prime = gmp_millerrabin (n, nm1, y, q, k);
2151 /* Logical operations and bit manipulation. */
2153 /* Numbers are treated as if represented in two's complement (and
2154 infinitely sign extended). For a negative values we get the two's
2155 complement from -x = ~x + 1, where ~ is bitwise complement.
2164 where yyyy is the bitwise complement of xxxx. So least significant
2165 bits, up to and including the first one bit, are unchanged, and
2166 the more significant bits are all complemented.
2168 To change a bit from zero to one in a negative number, subtract the
2169 corresponding power of two from the absolute value. This can never
2170 underflow. To change a bit from one to zero, add the corresponding
2171 power of two, and this might overflow. E.g., if x = -001111, the
2172 two's complement is 110001. Clearing the least significant bit, we
2173 get two's complement 110000, and -010000. */
2176 mpz_tstbit (const mpz_t d, mp_bitcnt_t bit_index)
2178 mp_size_t limb_index;
2187 limb_index = bit_index / GMP_LIMB_BITS;
2188 if (limb_index >= dn)
2191 shift = bit_index % GMP_LIMB_BITS;
2192 w = d->_mp_d[limb_index];
2193 bit = (w >> shift) & 1;
2197 /* d < 0. Check if any of the bits below is set: If so, our bit
2198 must be complemented. */
2199 if (shift > 0 && (w << (GMP_LIMB_BITS - shift)) > 0)
2201 while (--limb_index >= 0)
2202 if (d->_mp_d[limb_index] > 0)
2209 mpz_abs_add_bit (mpz_t d, mp_bitcnt_t bit_index)
2211 mp_size_t dn, limb_index;
2215 dn = GMP_ABS (d->_mp_size);
2217 limb_index = bit_index / GMP_LIMB_BITS;
2218 bit = (mp_limb_t) 1 << (bit_index % GMP_LIMB_BITS);
2220 if (limb_index >= dn)
2223 /* The bit should be set outside of the end of the number.
2224 We have to increase the size of the number. */
2225 dp = MPZ_REALLOC (d, limb_index + 1);
2227 dp[limb_index] = bit;
2228 for (i = dn; i < limb_index; i++)
2230 dn = limb_index + 1;
2238 cy = mpn_add_1 (dp + limb_index, dp + limb_index, dn - limb_index, bit);
2241 dp = MPZ_REALLOC (d, dn + 1);
2246 d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
2250 mpz_abs_sub_bit (mpz_t d, mp_bitcnt_t bit_index)
2252 mp_size_t dn, limb_index;
2256 dn = GMP_ABS (d->_mp_size);
2259 limb_index = bit_index / GMP_LIMB_BITS;
2260 bit = (mp_limb_t) 1 << (bit_index % GMP_LIMB_BITS);
2262 assert (limb_index < dn);
2264 gmp_assert_nocarry (mpn_sub_1 (dp + limb_index, dp + limb_index,
2265 dn - limb_index, bit));
2266 dn = mpn_normalized_size (dp, dn);
2267 d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
2271 mpz_setbit (mpz_t d, mp_bitcnt_t bit_index)
2273 if (!mpz_tstbit (d, bit_index))
2275 if (d->_mp_size >= 0)
2276 mpz_abs_add_bit (d, bit_index);
2278 mpz_abs_sub_bit (d, bit_index);
2283 mpz_clrbit (mpz_t d, mp_bitcnt_t bit_index)
2285 if (mpz_tstbit (d, bit_index))
2287 if (d->_mp_size >= 0)
2288 mpz_abs_sub_bit (d, bit_index);
2290 mpz_abs_add_bit (d, bit_index);
2295 mpz_combit (mpz_t d, mp_bitcnt_t bit_index)
2297 if (mpz_tstbit (d, bit_index) ^ (d->_mp_size < 0))
2298 mpz_abs_sub_bit (d, bit_index);
2300 mpz_abs_add_bit (d, bit_index);
2304 mpz_com (mpz_t r, const mpz_t u)
2307 mpz_sub_ui (r, r, 1);
2311 mpz_and (mpz_t r, const mpz_t u, const mpz_t v)
2313 mp_size_t un, vn, rn, i;
2316 mp_limb_t ux, vx, rx;
2317 mp_limb_t uc, vc, rc;
2318 mp_limb_t ul, vl, rl;
2320 un = GMP_ABS (u->_mp_size);
2321 vn = GMP_ABS (v->_mp_size);
2324 MPZ_SRCPTR_SWAP (u, v);
2325 MP_SIZE_T_SWAP (un, vn);
2333 uc = u->_mp_size < 0;
2334 vc = v->_mp_size < 0;
2341 /* If the smaller input is positive, higher limbs don't matter. */
2344 rp = MPZ_REALLOC (r, rn + rc);
2352 ul = (up[i] ^ ux) + uc;
2355 vl = (vp[i] ^ vx) + vc;
2358 rl = ( (ul & vl) ^ rx) + rc;
2367 ul = (up[i] ^ ux) + uc;
2370 rl = ( (ul & vx) ^ rx) + rc;
2377 rn = mpn_normalized_size (rp, rn);
2379 r->_mp_size = rx ? -rn : rn;
2383 mpz_ior (mpz_t r, const mpz_t u, const mpz_t v)
2385 mp_size_t un, vn, rn, i;
2388 mp_limb_t ux, vx, rx;
2389 mp_limb_t uc, vc, rc;
2390 mp_limb_t ul, vl, rl;
2392 un = GMP_ABS (u->_mp_size);
2393 vn = GMP_ABS (v->_mp_size);
2396 MPZ_SRCPTR_SWAP (u, v);
2397 MP_SIZE_T_SWAP (un, vn);
2405 uc = u->_mp_size < 0;
2406 vc = v->_mp_size < 0;
2413 /* If the smaller input is negative, by sign extension higher limbs
2417 rp = MPZ_REALLOC (r, rn + rc);
2425 ul = (up[i] ^ ux) + uc;
2428 vl = (vp[i] ^ vx) + vc;
2431 rl = ( (ul | vl) ^ rx) + rc;
2440 ul = (up[i] ^ ux) + uc;
2443 rl = ( (ul | vx) ^ rx) + rc;
2450 rn = mpn_normalized_size (rp, rn);
2452 r->_mp_size = rx ? -rn : rn;
2456 mpz_xor (mpz_t r, const mpz_t u, const mpz_t v)
2458 mp_size_t un, vn, i;
2461 mp_limb_t ux, vx, rx;
2462 mp_limb_t uc, vc, rc;
2463 mp_limb_t ul, vl, rl;
2465 un = GMP_ABS (u->_mp_size);
2466 vn = GMP_ABS (v->_mp_size);
2469 MPZ_SRCPTR_SWAP (u, v);
2470 MP_SIZE_T_SWAP (un, vn);
2478 uc = u->_mp_size < 0;
2479 vc = v->_mp_size < 0;
2486 rp = MPZ_REALLOC (r, un + rc);
2494 ul = (up[i] ^ ux) + uc;
2497 vl = (vp[i] ^ vx) + vc;
2500 rl = (ul ^ vl ^ rx) + rc;
2509 ul = (up[i] ^ ux) + uc;
2512 rl = (ul ^ ux) + rc;
2519 un = mpn_normalized_size (rp, un);
2521 r->_mp_size = rx ? -un : un;
2525 gmp_popcount_limb (mp_limb_t x)
2529 /* Do 16 bits at a time, to avoid limb-sized constants. */
2530 for (c = 0; x > 0; x >>= 16)
2532 uint32_t w = ((x >> 1) & 0x5555) + (x & 0x5555);
2533 w = ((w >> 2) & 0x3333) + (w & 0x3333);
2534 w = ((w >> 4) & 0x0f0f) + (w & 0x0f0f);
2535 w = (w >> 8) + (w & 0x00ff);
2542 mpn_popcount (mp_srcptr p, mp_size_t n)
2547 for (c = 0, i = 0; i < n; i++)
2548 c += gmp_popcount_limb (p[i]);
2554 mpz_popcount (const mpz_t u)
2561 return ~(mp_bitcnt_t) 0;
2563 return mpn_popcount (u->_mp_d, un);
2567 mpz_hamdist (const mpz_t u, const mpz_t v)
2569 mp_size_t un, vn, i;
2570 mp_limb_t uc, vc, ul, vl, comp;
2578 return ~(mp_bitcnt_t) 0;
2580 comp = - (uc = vc = (un < 0));
2592 MPN_SRCPTR_SWAP (up, un, vp, vn);
2594 for (i = 0, c = 0; i < vn; i++)
2596 ul = (up[i] ^ comp) + uc;
2599 vl = (vp[i] ^ comp) + vc;
2602 c += gmp_popcount_limb (ul ^ vl);
2608 ul = (up[i] ^ comp) + uc;
2611 c += gmp_popcount_limb (ul ^ comp);
2618 mpz_scan1 (const mpz_t u, mp_bitcnt_t starting_bit)
2621 mp_size_t us, un, i;
2626 i = starting_bit / GMP_LIMB_BITS;
2628 /* Past the end there's no 1 bits for u>=0, or an immediate 1 bit
2629 for u<0. Notice this test picks up any u==0 too. */
2631 return (us >= 0 ? ~(mp_bitcnt_t) 0 : starting_bit);
2637 if (starting_bit != 0)
2641 ux = mpn_zero_p (up, i);
2643 ux = - (mp_limb_t) (limb >= ux);
2646 /* Mask to 0 all bits before starting_bit, thus ignoring them. */
2647 limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
2650 return mpn_common_scan (limb, i, up, un, ux);
2654 mpz_scan0 (const mpz_t u, mp_bitcnt_t starting_bit)
2657 mp_size_t us, un, i;
2661 ux = - (mp_limb_t) (us >= 0);
2663 i = starting_bit / GMP_LIMB_BITS;
2665 /* When past end, there's an immediate 0 bit for u>=0, or no 0 bits for
2666 u<0. Notice this test picks up all cases of u==0 too. */
2668 return (ux ? starting_bit : ~(mp_bitcnt_t) 0);
2674 limb -= mpn_zero_p (up, i); /* limb = ~(~limb + zero_p) */
2676 /* Mask all bits before starting_bit, thus ignoring them. */
2677 limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
2679 return mpn_common_scan (limb, i, up, un, ux);
2683 /* MPZ base conversion. */
2686 mpz_sizeinbase (const mpz_t u, int base)
2692 struct gmp_div_inverse bi;
2696 assert (base <= 36);
2698 un = GMP_ABS (u->_mp_size);
2704 bits = (un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1]);
2710 return (bits + 1) / 2;
2712 return (bits + 2) / 3;
2714 return (bits + 3) / 4;
2716 return (bits + 4) / 5;
2717 /* FIXME: Do something more clever for the common case of base
2721 tp = gmp_xalloc_limbs (un);
2722 mpn_copyi (tp, up, un);
2723 mpn_div_qr_1_invert (&bi, base);
2729 mpn_div_qr_1_preinv (tp, tp, un, &bi);
2730 un -= (tp[un-1] == 0);
2739 mpz_get_str (char *sp, int base, const mpz_t u)
2748 digits = "0123456789abcdefghijklmnopqrstuvwxyz";
2753 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2760 sn = 1 + mpz_sizeinbase (u, base);
2762 sp = (char *) gmp_xalloc (1 + sn);
2764 un = GMP_ABS (u->_mp_size);
2775 if (u->_mp_size < 0)
2778 bits = mpn_base_power_of_two_p (base);
2781 /* Not modified in this case. */
2782 sn = i + mpn_get_str_bits ((uint8_t *) sp + i, bits, u->_mp_d, un);
2785 struct mpn_base_info info;
2788 mpn_get_base_info (&info, base);
2789 tp = gmp_xalloc_limbs (un);
2790 mpn_copyi (tp, u->_mp_d, un);
2792 sn = i + mpn_get_str_other ((uint8_t *) sp + i, base, &info, tp, un);
2797 sp[i] = digits[(uint8_t) sp[i]];
2804 mpz_out_str (FILE *stream, int base, const mpz_t x)
2809 str = mpz_get_str (NULL, base, x);
2811 len = fwrite (str, 1, len, stream);
2818 static void gmp_die (const char *msg)
2820 fprintf (stderr, "%s\n", msg);
2824 static mp_ptr gmp_xalloc_limbs (mp_size_t size)
2826 return (mp_ptr) malloc(size * sizeof(mp_limb_t));
2829 static int gmp_detect_endian (void)
2831 static const int i = 2;
2832 const uint8_t *p = (const uint8_t *) &i;
2836 void *mpz_export (void *r, size_t *countp, int order, size_t size, int endian,size_t nails, const mpz_t u)
2842 gmp_die ("mpz_import: Nails not supported.");
2844 assert (order == 1 || order == -1);
2845 assert (endian >= -1 && endian <= 1);
2846 assert (size > 0 || u->_mp_size == 0);
2854 ptrdiff_t word_step;
2855 /* The current (partial) limb. */
2857 /* The number of bytes left to to in this limb. */
2859 /* The index where the limb was read. */
2864 /* Count bytes in top limb. */
2865 limb = u->_mp_d[un-1];
2870 k++; limb >>= CHAR_BIT;
2871 } while (limb != 0);
2873 count = (k + (un-1) * sizeof (mp_limb_t) + size - 1) / size;
2876 r = malloc (count * size);
2879 endian = gmp_detect_endian ();
2883 word_step = (order != endian) ? 2 * size : 0;
2885 /* Process bytes from the least significant end, so point p at the
2886 least significant word. */
2889 p += size * (count - 1);
2890 word_step = - word_step;
2893 /* And at least significant byte of that word. */
2897 for (bytes = 0, i = 0, k = 0; k < count; k++, p += word_step)
2900 for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
2905 limb = u->_mp_d[i++];
2906 bytes = sizeof (mp_limb_t);
2914 assert (k == count);
2923 /////////////////////////////////
2926 static mp_size_t mpn_normalized_size (mp_srcptr xp, mp_size_t n)
2928 while (n > 0 && xp[n-1] == 0)
2934 static mp_ptr gmp_xrealloc_limbs (mp_ptr old, mp_size_t size)
2937 return (mp_ptr) realloc(old, size * sizeof (mp_limb_t));
2942 mpz_realloc (mpz_t r, mp_size_t size)
2944 size = GMP_MAX (size, 1);
2946 r->_mp_d = gmp_xrealloc_limbs (r->_mp_d, size);
2947 r->_mp_alloc = (int32_t)size;
2949 if (GMP_ABS (r->_mp_size) > size)
2955 /* Import and export. Does not support nails. */
2957 mpz_import (mpz_t r, size_t count, int order, size_t size, int endian,size_t nails, const void *src)
2960 ptrdiff_t word_step;
2964 // The current (partial) limb.
2966 // The number of bytes already copied to this limb (starting from the low end).
2968 // The index where the limb should be stored, when completed.
2972 gmp_die ("mpz_import: Nails not supported.");
2974 assert (order == 1 || order == -1);
2975 assert (endian >= -1 && endian <= 1);
2978 endian = gmp_detect_endian ();
2980 p = (uint8_t *) src;
2982 word_step = (order != endian) ? 2 * size : 0;
2984 // Process bytes from the least significant end, so point p at the least significant word
2987 p += size * (count - 1);
2988 word_step = - word_step;
2990 // And at least significant byte of that word
2993 rn = (size * count + sizeof(mp_limb_t) - 1) / sizeof(mp_limb_t);
2994 rp = MPZ_REALLOC (r, rn);
2995 for (limb = 0, bytes = 0, i = 0; count > 0; count--, p += word_step)
2998 for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
3000 limb |= (mp_limb_t) *p << (bytes++ * CHAR_BIT);
3001 if (bytes == sizeof(mp_limb_t))
3009 assert (i + (bytes > 0) == rn);
3012 else i = mpn_normalized_size (rp, i);
3013 r->_mp_size = (int32_t)i;
3016 void mpz_init (mpz_t r)
3020 r->_mp_d = gmp_xalloc_limbs (1);
3030 mpn_get_base_info (struct mpn_base_info *info, mp_limb_t b)
3036 m = GMP_LIMB_MAX / b;
3037 for (exp = 1, p = b; p <= m; exp++)
3044 static uint32_t mpn_base_power_of_two_p (uint32_t b)
3061 static mp_size_t mpn_set_str_bits (mp_ptr rp, const uint8_t *sp, size_t sn, uint32_t bits)
3067 for (j = sn, rn = 0, shift = 0; j-- > 0; )
3076 rp[rn-1] |= (mp_limb_t) sp[j] << shift;
3078 if (shift >= GMP_LIMB_BITS)
3080 shift -= GMP_LIMB_BITS;
3082 rp[rn++] = (mp_limb_t) sp[j] >> (bits - shift);
3086 rn = mpn_normalized_size (rp, rn);
3091 mpn_add_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
3099 mp_limb_t r = ap[i] + b;
3110 mpn_mul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
3112 mp_limb_t ul, cl, hpl, lpl;
3120 gmp_umul_ppmm (hpl, lpl, ul, vl);
3123 cl = (lpl < cl) + hpl;
3133 mpn_set_str_other (mp_ptr rp, const uint8_t *sp, size_t sn,
3134 mp_limb_t b, const struct mpn_base_info *info)
3141 k = 1 + (sn - 1) % info->exp;
3146 w = w * b + sp[j++];
3150 for (rn = (w > 0); j < sn;)
3155 for (k = 1; k < info->exp; k++)
3156 w = w * b + sp[j++];
3158 cy = mpn_mul_1 (rp, rp, rn, info->bb);
3159 cy += mpn_add_1 (rp, rp, rn, w);
3168 void mpn_copyi (mp_ptr d, mp_srcptr s, mp_size_t n)
3171 for (i = 0; i < n; i++)
3175 void mpz_set (mpz_t r, const mpz_t x)
3177 /* Allow the NOP r == x */
3183 n = GMP_ABS (x->_mp_size);
3184 rp = MPZ_REALLOC (r, n);
3186 mpn_copyi (rp, x->_mp_d, n);
3187 r->_mp_size = x->_mp_size;
3191 int mpz_set_str (mpz_t r, const char *sp, int base)
3194 mp_size_t rn, alloc;
3200 assert (base == 0 || (base >= 2 && base <= 36));
3202 while (isspace( (uint8_t) *sp))
3205 sign = (*sp == '-');
3213 if (*sp == 'x' || *sp == 'X')
3218 else if (*sp == 'b' || *sp == 'B')
3231 dp = (uint8_t *) malloc (sn + (sn == 0));
3233 for (sn = 0; *sp; sp++)
3237 if (isspace ((uint8_t) *sp))
3239 if (*sp >= '0' && *sp <= '9')
3241 else if (*sp >= 'a' && *sp <= 'z')
3242 digit = *sp - 'a' + 10;
3243 else if (*sp >= 'A' && *sp <= 'Z')
3244 digit = *sp - 'A' + 10;
3246 digit = base; /* fail */
3258 bits = mpn_base_power_of_two_p (base);
3262 alloc = (sn * bits + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
3263 rp = MPZ_REALLOC (r, alloc);
3264 rn = mpn_set_str_bits (rp, dp, sn, bits);
3268 struct mpn_base_info info;
3269 mpn_get_base_info (&info, base);
3270 alloc = (sn + info.exp - 1) / info.exp;
3271 rp = MPZ_REALLOC (r, alloc);
3272 rn = mpn_set_str_other (rp, dp, sn, base, &info);
3274 assert (rn <= alloc);
3277 r->_mp_size = (int32_t)(sign ? - rn : rn);
3282 void mpz_init_set (mpz_t r, const mpz_t x)
3288 int mpz_init_set_str (mpz_t r, const char *sp, int base)
3291 return mpz_set_str (r, sp, base);
3294 int mpn_cmp (mp_srcptr ap, mp_srcptr bp, mp_size_t n)
3299 return ap[n] > bp[n] ? 1 : -1;
3304 int mpz_cmp (const mpz_t a, const mpz_t b)
3306 mp_size_t asize = a->_mp_size;
3307 mp_size_t bsize = b->_mp_size;
3310 return (asize < bsize) ? -1 : 1;
3311 else if (asize >= 0)
3312 return mpn_cmp (a->_mp_d, b->_mp_d, asize);
3314 return mpn_cmp (b->_mp_d, a->_mp_d, -asize);
3317 static void mpn_div_qr_1_invert (struct gmp_div_inverse *inv, mp_limb_t d)
3324 inv->d1 = d << shift;
3325 inv->di = mpn_invert_limb (inv->d1);
3328 /* MPN division interface. */
3330 mpn_invert_3by2 (mp_limb_t u1, mp_limb_t u0)
3336 /* First, do a 2/1 inverse. */
3337 /* The inverse m is defined as floor( (B^2 - 1 - u1)/u1 ), so that 0 <
3338 * B^2 - (B + m) u1 <= u1 */
3339 assert (u1 >= GMP_LIMB_HIGHBIT);
3341 ul = u1 & GMP_LLIMB_MASK;
3342 uh = u1 >> (GMP_LIMB_BITS / 2);
3344 qh = (uint32_t)(~u1 / uh);
3345 r = ((~u1 - (mp_limb_t) qh * uh) << (GMP_LIMB_BITS / 2)) | GMP_LLIMB_MASK;
3347 p = (mp_limb_t) qh * ul;
3348 /* Adjustment steps taken from udiv_qrnnd_c */
3353 if (r >= u1) /* i.e. we didn't get carry when adding to r */
3362 /* Do a 3/2 division (with half limb size) */
3363 p = (r >> (GMP_LIMB_BITS / 2)) * qh + r;
3364 ql = (p >> (GMP_LIMB_BITS / 2)) + 1;
3366 /* By the 3/2 method, we don't need the high half limb. */
3367 r = (r << (GMP_LIMB_BITS / 2)) + GMP_LLIMB_MASK - ql * u1;
3369 if (r >= (p << (GMP_LIMB_BITS / 2)))
3374 m = ((mp_limb_t) qh << (GMP_LIMB_BITS / 2)) + ql;
3396 gmp_umul_ppmm (th, tl, u0, m);
3401 m -= ((r > u1) | ((r == u1) & (tl > u0)));
3408 static void mpn_div_qr_2_invert (struct gmp_div_inverse *inv,mp_limb_t d1, mp_limb_t d0)
3413 gmp_clz (shift, d1);
3417 d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
3422 inv->di = mpn_invert_3by2 (d1, d0);
3425 static void mpn_div_qr_invert (struct gmp_div_inverse *inv, mp_srcptr dp, mp_size_t dn)
3430 mpn_div_qr_1_invert (inv, dp[0]);
3432 mpn_div_qr_2_invert (inv, dp[1], dp[0]);
3441 gmp_clz (shift, d1);
3445 d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
3446 d0 = (d0 << shift) | (dp[dn-3] >> (GMP_LIMB_BITS - shift));
3450 inv->di = mpn_invert_3by2 (d1, d0);
3454 mp_limb_t mpn_lshift(mp_ptr rp, mp_srcptr up, mp_size_t n, uint32_t cnt)
3456 mp_limb_t high_limb, low_limb;
3462 assert (cnt < GMP_LIMB_BITS);
3467 tnc = GMP_LIMB_BITS - cnt;
3469 retval = low_limb >> tnc;
3470 high_limb = (low_limb << cnt);
3475 *--rp = high_limb | (low_limb >> tnc);
3476 high_limb = (low_limb << cnt);
3483 // Not matching current public gmp interface, rather corresponding to the sbpi1_div_* functions.
3484 static mp_limb_t mpn_div_qr_1_preinv (mp_ptr qp, mp_srcptr np, mp_size_t nn,const struct gmp_div_inverse *inv)
3492 tp = gmp_xalloc_limbs (nn);
3493 r = mpn_lshift (tp, np, nn, inv->shift);
3505 gmp_udiv_qrnnd_preinv (q, r, r, np[nn], d, di);
3512 return r >> inv->shift;
3514 static void mpn_div_qr_2_preinv (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn, const struct gmp_div_inverse *inv)
3518 mp_limb_t d1, d0, di, r1, r0;
3529 tp = gmp_xalloc_limbs (nn);
3530 r1 = mpn_lshift (tp, np, nn, shift);
3543 gmp_udiv_qr_3by2 (q, r1, r0, r1, r0, n0, d1, d0, di);
3552 assert ((r0 << (GMP_LIMB_BITS - shift)) == 0);
3553 r0 = (r0 >> shift) | (r1 << (GMP_LIMB_BITS - shift));
3563 mp_limb_t mpn_sub_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
3568 for (i = 0, cy = 0; i < n; i++)
3571 a = ap[i]; b = bp[i];
3580 mp_limb_t mpn_sub (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
3586 cy = mpn_sub_n (rp, ap, bp, bn);
3588 cy = mpn_sub_1 (rp + bn, ap + bn, an - bn, cy);
3592 mp_limb_t mpn_submul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
3594 mp_limb_t ul, cl, hpl, lpl, rl;
3602 gmp_umul_ppmm (hpl, lpl, ul, vl);
3605 cl = (lpl < cl) + hpl;
3617 static void mpn_div_qr_pi1 (mp_ptr qp,mp_ptr np, mp_size_t nn, mp_limb_t n1,mp_srcptr dp, mp_size_t dn,mp_limb_t dinv)
3631 assert ((d1 & GMP_LIMB_HIGHBIT) != 0);
3632 /* Iteration variable is the index of the q limb.
3634 * We divide <n1, np[dn-1+i], np[dn-2+i], np[dn-3+i],..., np[i]>
3635 * by <d1, d0, dp[dn-3], ..., dp[0] >
3641 mp_limb_t n0 = np[dn-1+i];
3643 if (n1 == d1 && n0 == d0)
3646 mpn_submul_1 (np+i, dp, dn, q);
3647 n1 = np[dn-1+i]; /* update n1, last loop's value will now be invalid */
3651 gmp_udiv_qr_3by2 (q, n1, n0, n1, n0, np[dn-2+i], d1, d0, dinv);
3653 cy = mpn_submul_1 (np + i, dp, dn-2, q);
3663 n1 += d1 + mpn_add_n (np + i, np + i, dp, dn - 1);
3676 mp_limb_t mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, uint32_t cnt)
3678 mp_limb_t high_limb, low_limb;
3684 assert (cnt < GMP_LIMB_BITS);
3686 tnc = GMP_LIMB_BITS - cnt;
3688 retval = (high_limb << tnc);
3689 low_limb = high_limb >> cnt;
3694 *rp++ = low_limb | (high_limb << tnc);
3695 low_limb = high_limb >> cnt;
3702 static void mpn_div_qr_preinv (mp_ptr qp, mp_ptr np, mp_size_t nn,mp_srcptr dp, mp_size_t dn,const struct gmp_div_inverse *inv)
3708 np[0] = mpn_div_qr_1_preinv (qp, np, nn, inv);
3710 mpn_div_qr_2_preinv (qp, np, np, nn, inv);
3716 assert (inv->d1 == dp[dn-1]);
3717 assert (inv->d0 == dp[dn-2]);
3718 assert ((inv->d1 & GMP_LIMB_HIGHBIT) != 0);
3722 nh = mpn_lshift (np, np, nn, shift);
3726 mpn_div_qr_pi1 (qp, np, nn, nh, dp, dn, inv->di);
3729 gmp_assert_nocarry (mpn_rshift (np, np, dn, shift));
3733 static void mpn_div_qr (mp_ptr qp, mp_ptr np, mp_size_t nn, mp_srcptr dp, mp_size_t dn)
3735 struct gmp_div_inverse inv;
3741 mpn_div_qr_invert (&inv, dp, dn);
3742 if (dn > 2 && inv.shift > 0)
3744 tp = gmp_xalloc_limbs (dn);
3745 gmp_assert_nocarry (mpn_lshift (tp, dp, dn, inv.shift));
3748 mpn_div_qr_preinv (qp, np, nn, dp, dn, &inv);
3754 mpn_cmp4 (mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
3757 return an < bn ? -1 : 1;
3759 return mpn_cmp (ap, bp, an);
3762 static mp_size_t mpz_abs_sub (mpz_t r, const mpz_t a, const mpz_t b)
3764 mp_size_t an = GMP_ABS (a->_mp_size);
3765 mp_size_t bn = GMP_ABS (b->_mp_size);
3769 cmp = mpn_cmp4 (a->_mp_d, an, b->_mp_d, bn);
3772 rp = MPZ_REALLOC (r, an);
3773 gmp_assert_nocarry (mpn_sub (rp, a->_mp_d, an, b->_mp_d, bn));
3774 return mpn_normalized_size (rp, an);
3778 rp = MPZ_REALLOC (r, bn);
3779 gmp_assert_nocarry (mpn_sub (rp, b->_mp_d, bn, a->_mp_d, an));
3780 return -mpn_normalized_size (rp, bn);
3786 mp_limb_t mpn_add_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
3791 for (i = 0, cy = 0; i < n; i++)
3794 a = ap[i]; b = bp[i];
3805 mpn_add (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
3811 cy = mpn_add_n (rp, ap, bp, bn);
3813 cy = mpn_add_1 (rp + bn, ap + bn, an - bn, cy);
3817 static mp_size_t mpz_abs_add (mpz_t r, const mpz_t a, const mpz_t b)
3819 mp_size_t an = GMP_ABS (a->_mp_size);
3820 mp_size_t bn = GMP_ABS (b->_mp_size);
3826 MPZ_SRCPTR_SWAP (a, b);
3827 MP_SIZE_T_SWAP (an, bn);
3830 rp = MPZ_REALLOC (r, an + 1);
3831 cy = mpn_add (rp, a->_mp_d, an, b->_mp_d, bn);
3839 void mpz_sub (mpz_t r, const mpz_t a, const mpz_t b)
3843 if ( (a->_mp_size ^ b->_mp_size) >= 0)
3844 rn = mpz_abs_sub (r, a, b);
3846 rn = mpz_abs_add (r, a, b);
3848 r->_mp_size = (int)(a->_mp_size >= 0 ? rn : - rn);
3851 /* Adds to the absolute value. Returns new size, but doesn't store it. */
3852 static mp_size_t mpz_abs_add_ui (mpz_t r, const mpz_t a, uint64_t b)
3858 an = GMP_ABS (a->_mp_size);
3865 rp = MPZ_REALLOC (r, an + 1);
3867 cy = mpn_add_1 (rp, a->_mp_d, an, b);
3874 mp_limb_t mpn_sub_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
3883 mp_limb_t a = ap[i];
3885 mp_limb_t cy = a < b;;
3894 // Subtract from the absolute value. Returns new size, (or -1 on underflow), but doesn't store it.
3895 static mp_size_t mpz_abs_sub_ui (mpz_t r, const mpz_t a, uint64_t b)
3897 mp_size_t an = GMP_ABS (a->_mp_size);
3898 mp_ptr rp = MPZ_REALLOC (r, an);
3905 else if (an == 1 && a->_mp_d[0] < b)
3907 rp[0] = b - a->_mp_d[0];
3912 gmp_assert_nocarry (mpn_sub_1 (rp, a->_mp_d, an, b));
3913 return mpn_normalized_size (rp, an);
3917 void mpz_sub_ui (mpz_t r, const mpz_t a, uint64_t b)
3919 if (a->_mp_size < 0)
3920 r->_mp_size = (int)-mpz_abs_add_ui (r, a, b);
3922 r->_mp_size = (int)mpz_abs_sub_ui (r, a, b);
3925 void mpz_add (mpz_t r, const mpz_t a, const mpz_t b)
3929 if ( (a->_mp_size ^ b->_mp_size) >= 0)
3930 rn = mpz_abs_add (r, a, b);
3932 rn = mpz_abs_sub (r, a, b);
3934 r->_mp_size = (int32_t)(a->_mp_size >= 0 ? rn : - rn);
3937 void mpz_add_ui (mpz_t r, const mpz_t a, uint64_t b)
3939 if (a->_mp_size >= 0)
3940 r->_mp_size = (int32_t)mpz_abs_add_ui (r, a, b);
3942 r->_mp_size = (int32_t)-mpz_abs_sub_ui (r, a, b);
3945 /* The utility of this function is a bit limited, since many functions
3946 assigns the result variable using mpz_swap. */
3947 void mpz_init2 (mpz_t r, mp_bitcnt_t bits)
3951 bits -= (bits != 0); /* Round down, except if 0 */
3952 rn = 1 + bits / GMP_LIMB_BITS;
3954 r->_mp_alloc = (int32_t)rn;
3956 r->_mp_d = gmp_xalloc_limbs (rn);
3959 void mpz_set_ui (mpz_t r, uint64_t x)
3970 void mpz_set_si (mpz_t r, int64_t x)
3977 r->_mp_d[0] = GMP_NEG_CAST (uint64_t, x);
3981 void mpz_swap (mpz_t u, mpz_t v)
3983 MP_SIZE_sT_SWAP (u->_mp_size, v->_mp_size);
3984 MP_SIZE_sT_SWAP (u->_mp_alloc, v->_mp_alloc);
3985 MP_PTR_SWAP (u->_mp_d, v->_mp_d);
3988 // Allows q or r to be zero. Returns 1 iff remainder is non-zero.
3989 static int mpz_div_qr (mpz_t q, mpz_t r,const mpz_t n, const mpz_t d, enum mpz_div_round_mode mode)
3991 mp_size_t ns, ds, nn, dn, qs;
3996 gmp_die("mpz_div_qr: Divide by zero.");
4014 if (mode == GMP_DIV_CEIL && qs >= 0)
4016 /* q = 1, r = n - d */
4022 else if (mode == GMP_DIV_FLOOR && qs < 0)
4024 /* q = -1, r = n + d */
4046 mpz_init_set (tr, n);
4053 mpz_init2 (tq, qn * GMP_LIMB_BITS);
4059 mpn_div_qr (qp, np, nn, d->_mp_d, dn);
4063 qn -= (qp[qn-1] == 0);
4065 tq->_mp_size = (uint32_t)(qs < 0 ? -qn : qn);
4067 rn = mpn_normalized_size (np, dn);
4068 tr->_mp_size = (uint32_t)(ns < 0 ? - rn : rn);
4070 if (mode == GMP_DIV_FLOOR && qs < 0 && rn != 0)
4073 mpz_sub_ui (tq, tq, 1);
4075 mpz_add (tr, tr, d);
4077 else if (mode == GMP_DIV_CEIL && qs >= 0 && rn != 0)
4080 mpz_add_ui (tq, tq, 1);
4082 mpz_sub (tr, tr, d);
4099 void mpz_cdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
4101 mpz_div_qr (q, r, n, d, GMP_DIV_CEIL);
4104 uint64_t mpz_get_ui (const mpz_t u)
4106 return u->_mp_size == 0 ? 0 : u->_mp_d[0];
4109 void mpz_tdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
4111 mpz_div_qr (q, r, n, d, GMP_DIV_TRUNC);
4114 void mpz_init_set_ui (mpz_t r, uint64_t x)
4120 mp_limb_t mpn_addmul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
4122 mp_limb_t ul, cl, hpl, lpl, rl;
4130 gmp_umul_ppmm (hpl, lpl, ul, vl);
4133 cl = (lpl < cl) + hpl;
4145 mp_limb_t mpn_mul (mp_ptr rp, mp_srcptr up, mp_size_t un, mp_srcptr vp, mp_size_t vn)
4150 /* We first multiply by the low order limb. This result can be
4151 stored, not added, to rp. We also avoid a loop for zeroing this
4154 rp[un] = mpn_mul_1 (rp, up, un, vp[0]);
4156 /* Now accumulate the product of up[] and the next higher limb from
4162 rp[un] = mpn_addmul_1 (rp, up, un, vp[0]);
4167 void mpz_mul (mpz_t r, const mpz_t u, const mpz_t v)
4170 mp_size_t un, vn, rn;
4177 if (un == 0 || vn == 0)
4183 sign = (un ^ vn) < 0;
4188 mpz_init2 (t, (un + vn) * GMP_LIMB_BITS);
4192 mpn_mul (tp, u->_mp_d, un, v->_mp_d, vn);
4194 mpn_mul (tp, v->_mp_d, vn, u->_mp_d, un);
4197 rn -= tp[rn-1] == 0;
4199 t->_mp_size = (int32_t)(sign ? - rn : rn);
4203 void mpz_mul_ui (mpz_t r, const mpz_t u, uint64_t v)
4205 mp_size_t un, us; mp_ptr tp; mp_limb_t cy;
4208 if (us == 0 || v == 0)
4214 tp = MPZ_REALLOC (r, un + 1);
4215 cy = mpn_mul_1 (tp, u->_mp_d, un, v);
4218 r->_mp_size = (int32_t)((us < 0) ? -un : un);
4221 static mp_limb_t mpn_div_qr_1 (mp_ptr qp, mp_srcptr np, mp_size_t nn, mp_limb_t d)
4224 // Special case for powers of two.
4225 if ((d & (d-1)) == 0)
4227 mp_limb_t r = np[0] & (d-1);
4231 mpn_copyi (qp, np, nn);
4236 mpn_rshift (qp, np, nn, (uint32_t)shift);
4243 struct gmp_div_inverse inv;
4244 mpn_div_qr_1_invert (&inv, d);
4245 return mpn_div_qr_1_preinv (qp, np, nn, &inv);
4249 static uint64_t mpz_div_qr_ui (mpz_t q, mpz_t r,const mpz_t n, uint64_t d, enum mpz_div_round_mode mode)
4251 mp_size_t ns,qn; mp_ptr qp; mp_limb_t rl; mp_size_t rs;
4263 qp = MPZ_REALLOC (q, qn);
4265 rl = mpn_div_qr_1 (qp, n->_mp_d, qn, d);
4268 rs = (ns < 0) ? -rs : rs;
4269 if (rl > 0 && ( (mode == GMP_DIV_FLOOR && ns < 0) || (mode == GMP_DIV_CEIL && ns >= 0)))
4272 gmp_assert_nocarry (mpn_add_1 (qp, qp, qn, 1));
4279 r->_mp_size = (int32_t)rs;
4283 qn -= (qp[qn-1] == 0);
4284 assert (qn == 0 || qp[qn-1] > 0);
4285 q->_mp_size = (int32_t)((ns < 0) ? -qn : qn);
4290 uint64_t mpz_tdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, uint64_t d)
4292 return mpz_div_qr_ui (q, r, n, d, GMP_DIV_TRUNC);
4295 void mpn_copyd (mp_ptr d, mp_srcptr s, mp_size_t n)
4301 void mpn_zero(mp_ptr rp, mp_size_t n)
4307 void mpz_mul_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bits)
4309 mp_size_t un,rn,limbs; uint32_t shift; mp_ptr rp;
4310 un = GMP_ABS (u->_mp_size);
4316 limbs = bits / GMP_LIMB_BITS;
4317 shift = bits % GMP_LIMB_BITS;
4318 rn = un + limbs + (shift > 0);
4319 rp = MPZ_REALLOC (r, rn);
4322 mp_limb_t cy = mpn_lshift (rp + limbs, u->_mp_d, un, shift);
4326 else mpn_copyd (rp + limbs, u->_mp_d, un);
4327 mpn_zero (rp, limbs);
4328 r->_mp_size = (int32_t)((u->_mp_size < 0) ? - rn : rn);
4333 static const char base58_chars[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
4335 char *bitcoin_base58encode(char *coinaddr,uint8_t *data,int32_t datalen)
4337 mpz_t bn0,bn58,dv,rem,bn; char rs[128]; int32_t i,n=0;
4338 //mpz_init_set_str(bn58,"58",10);
4339 //mpz_init_set_str(bn0,"0",10);
4340 mpz_init_set_ui(bn58,58);
4341 mpz_init_set_ui(bn0,0);
4342 mpz_init(dv), mpz_init(rem), mpz_init(bn);
4343 mpz_import(bn,datalen,1,sizeof(data[0]),0,0,data);
4344 while ( mpz_cmp(bn,bn0) > 0 )
4346 mpz_tdiv_qr(bn,rem,bn,bn58);
4347 rs[n++] = base58_chars[mpz_get_ui(rem)];
4349 for (i=0; i<datalen; i++)
4352 rs[n++] = base58_chars[0];
4356 coinaddr[n - i - 1] = rs[i];
4358 mpz_clear(bn0), mpz_clear(bn58), mpz_clear(dv), mpz_clear(rem), mpz_clear(bn);
4362 int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr)
4364 uint32_t zeroes,be_sz=0; size_t count; const char *p,*p1; mpz_t bn58,bn;
4365 mpz_init_set_ui(bn58,58);
4366 mpz_init_set_ui(bn,0);
4367 while ( isspace((uint32_t)(*coinaddr & 0xff)) )
4369 for (p=coinaddr; *p; p++)
4371 p1 = strchr(base58_chars,*p);
4374 while (isspace((uint32_t)*p))
4378 mpz_clear(bn), mpz_clear(bn58);
4383 mpz_mul(bn,bn,bn58);
4384 mpz_add_ui(bn,bn,(int32_t)(p1 - base58_chars));
4387 for (p=coinaddr; *p==base58_chars[0]; p++)
4389 mpz_export(data+zeroes,&count,1,sizeof(data[0]),-1,0,bn);
4390 if ( count >= 2 && data[count - 1] == 0 && data[count - 2] >= 0x80 )
4392 be_sz = (uint32_t)count + (uint32_t)zeroes;
4393 //memset(data,0,be_sz);
4394 //for (i=0; i<count; i++)
4395 // data[i+zeroes] = revdata[count - 1 - i];
4396 //printf("len.%d be_sz.%d zeroes.%d data[0] %02x %02x\n",be_sz+zeroes,be_sz,zeroes,data[0],data[1]);
4397 mpz_clear(bn), mpz_clear(bn58);
4401 //#include "../includes/curve25519.h"
4403 void mpz_from_bits256(mpz_t bn,bits256 x)
4406 mpz_init_set_ui(bn,x.ulongs[3]);
4407 for (i=2; i>=0; i--)
4409 mpz_mul_2exp(bn,bn,64);
4410 if ( x.ulongs[i] != 0 )
4411 mpz_add_ui(bn,bn,x.ulongs[i]);
4415 bits256 mpz_to_bits256(mpz_t bn)
4417 bits256 x,rev; size_t count; int32_t i;
4418 mpz_export(rev.bytes,&count,1,sizeof(uint64_t),1,0,bn);
4419 for (i=0; i<32; i++)
4420 x.bytes[i] = rev.bytes[31-i];
4424 bits256 mpz_muldivcmp(bits256 oldval,int32_t mulval,int32_t divval,bits256 targetval)
4426 mpz_t bn,target; bits256 newval;
4427 //printf("mulval.%d divval.%d]\n",mulval,divval);
4428 mpz_init(bn), mpz_init(target);
4429 mpz_from_bits256(bn,oldval);
4430 mpz_mul_ui(bn,bn,mulval);
4431 mpz_tdiv_qr_ui(bn,NULL,bn,divval);
4432 if ( bn->_mp_size <= 0 || mpz_cmp(bn,target) > 0 )
4434 newval = mpz_to_bits256(bn);
4435 //char *bits256_str(char *,bits256);
4436 //char str[65],str2[65]; printf("%s mul.%d div.%d -> %s size.%d\n",bits256_str(str,oldval),mulval,divval,bits256_str(str2,newval),bn->_mp_size);
4437 mpz_clear(bn), mpz_clear(target);
4441 bits256 mpz_div64(bits256 hash,uint64_t divval)
4443 mpz_t bn; bits256 newval;
4445 mpz_from_bits256(bn,hash);
4446 mpz_tdiv_qr_ui(bn,NULL,bn,divval);
4447 newval = mpz_to_bits256(bn);
4448 //char *bits256_str(char *,bits256);
4449 //char str[65],str2[65]; printf("%s div.%lld -> %s size.%d\n",bits256_str(str,hash),(long long)divval,bits256_str(str2,newval),bn->_mp_size);