1 /* mini-gmp, a minimalistic implementation of a GNU GMP subset.
3 Contributed to the GNU project by Niels Möller
5 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
6 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
7 Free Software Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
24 /* NOTE: All functions in this file which are not declared in
25 mini-gmp.h are internal, and are not intended to be compatible
26 neither with GMP nor with future versions of mini-gmp. */
28 /* Much of the material copied from GMP files, including: gmp-impl.h,
29 longlong.h, mpn/generic/add_n.c, mpn/generic/addmul_1.c,
30 mpn/generic/lshift.c, mpn/generic/mul_1.c,
31 mpn/generic/mul_basecase.c, mpn/generic/rshift.c,
32 mpn/generic/sbpi1_div_qr.c, mpn/generic/sub_n.c,
33 mpn/generic/submul_1.c. */
46 #define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
48 #define GMP_LIMB_MAX (~ (mp_limb_t) 0)
49 #define GMP_LIMB_HIGHBIT ((mp_limb_t) 1 << (GMP_LIMB_BITS - 1))
51 #define GMP_HLIMB_BIT ((mp_limb_t) 1 << (GMP_LIMB_BITS / 2))
52 #define GMP_LLIMB_MASK (GMP_HLIMB_BIT - 1)
54 #define GMP_ULONG_BITS (sizeof(unsigned long) * CHAR_BIT)
55 #define GMP_ULONG_HIGHBIT ((unsigned long) 1 << (GMP_ULONG_BITS - 1))
57 #define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
58 #define GMP_NEG_CAST(T,x) (-((T)((x) + 1) - 1))
60 #define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
61 #define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
63 #define gmp_assert_nocarry(x) do { \
68 #define gmp_clz(count, x) do { \
69 mp_limb_t __clz_x = (x); \
72 (__clz_x & ((mp_limb_t) 0xff << (GMP_LIMB_BITS - 8))) == 0; \
75 for (; (__clz_x & GMP_LIMB_HIGHBIT) == 0; __clz_c++) \
80 #define gmp_ctz(count, x) do { \
81 mp_limb_t __ctz_x = (x); \
82 unsigned __ctz_c = 0; \
83 gmp_clz (__ctz_c, __ctz_x & - __ctz_x); \
84 (count) = GMP_LIMB_BITS - 1 - __ctz_c; \
87 #define gmp_add_ssaaaa(sh, sl, ah, al, bh, bl) \
91 (sh) = (ah) + (bh) + (__x < (al)); \
95 #define gmp_sub_ddmmss(sh, sl, ah, al, bh, bl) \
99 (sh) = (ah) - (bh) - ((al) < (bl)); \
103 #define gmp_umul_ppmm(w1, w0, u, v) \
105 mp_limb_t __x0, __x1, __x2, __x3; \
106 unsigned __ul, __vl, __uh, __vh; \
107 mp_limb_t __u = (u), __v = (v); \
109 __ul = __u & GMP_LLIMB_MASK; \
110 __uh = __u >> (GMP_LIMB_BITS / 2); \
111 __vl = __v & GMP_LLIMB_MASK; \
112 __vh = __v >> (GMP_LIMB_BITS / 2); \
114 __x0 = (mp_limb_t) __ul * __vl; \
115 __x1 = (mp_limb_t) __ul * __vh; \
116 __x2 = (mp_limb_t) __uh * __vl; \
117 __x3 = (mp_limb_t) __uh * __vh; \
119 __x1 += __x0 >> (GMP_LIMB_BITS / 2);/* this can't give carry */ \
120 __x1 += __x2; /* but this indeed can */ \
121 if (__x1 < __x2) /* did we get it? */ \
122 __x3 += GMP_HLIMB_BIT; /* yes, add it in the proper pos. */ \
124 (w1) = __x3 + (__x1 >> (GMP_LIMB_BITS / 2)); \
125 (w0) = (__x1 << (GMP_LIMB_BITS / 2)) + (__x0 & GMP_LLIMB_MASK); \
128 #define gmp_udiv_qrnnd_preinv(q, r, nh, nl, d, di) \
130 mp_limb_t _qh, _ql, _r, _mask; \
131 gmp_umul_ppmm (_qh, _ql, (nh), (di)); \
132 gmp_add_ssaaaa (_qh, _ql, _qh, _ql, (nh) + 1, (nl)); \
133 _r = (nl) - _qh * (d); \
134 _mask = -(mp_limb_t) (_r > _ql); /* both > and >= are OK */ \
147 #define gmp_udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv) \
149 mp_limb_t _q0, _t1, _t0, _mask; \
150 gmp_umul_ppmm ((q), _q0, (n2), (dinv)); \
151 gmp_add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1)); \
153 /* Compute the two most significant limbs of n - q'd */ \
154 (r1) = (n1) - (d1) * (q); \
155 gmp_sub_ddmmss ((r1), (r0), (r1), (n0), (d1), (d0)); \
156 gmp_umul_ppmm (_t1, _t0, (d0), (q)); \
157 gmp_sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0); \
160 /* Conditionally adjust q and the remainders */ \
161 _mask = - (mp_limb_t) ((r1) >= _q0); \
163 gmp_add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0)); \
166 if ((r1) > (d1) || (r0) >= (d0)) \
169 gmp_sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0)); \
175 #define MP_LIMB_T_SWAP(x, y) \
177 mp_limb_t __mp_limb_t_swap__tmp = (x); \
179 (y) = __mp_limb_t_swap__tmp; \
181 #define MP_SIZE_T_SWAP(x, y) \
183 mp_size_t __mp_size_t_swap__tmp = (x); \
185 (y) = __mp_size_t_swap__tmp; \
187 #define MP_BITCNT_T_SWAP(x,y) \
189 mp_bitcnt_t __mp_bitcnt_t_swap__tmp = (x); \
191 (y) = __mp_bitcnt_t_swap__tmp; \
193 #define MP_PTR_SWAP(x, y) \
195 mp_ptr __mp_ptr_swap__tmp = (x); \
197 (y) = __mp_ptr_swap__tmp; \
199 #define MP_SRCPTR_SWAP(x, y) \
201 mp_srcptr __mp_srcptr_swap__tmp = (x); \
203 (y) = __mp_srcptr_swap__tmp; \
206 #define MPN_PTR_SWAP(xp,xs, yp,ys) \
208 MP_PTR_SWAP (xp, yp); \
209 MP_SIZE_T_SWAP (xs, ys); \
211 #define MPN_SRCPTR_SWAP(xp,xs, yp,ys) \
213 MP_SRCPTR_SWAP (xp, yp); \
214 MP_SIZE_T_SWAP (xs, ys); \
217 #define MPZ_PTR_SWAP(x, y) \
219 mpz_ptr __mpz_ptr_swap__tmp = (x); \
221 (y) = __mpz_ptr_swap__tmp; \
223 #define MPZ_SRCPTR_SWAP(x, y) \
225 mpz_srcptr __mpz_srcptr_swap__tmp = (x); \
227 (y) = __mpz_srcptr_swap__tmp; \
231 /* Memory allocation and other helper functions. */
233 gmp_die (const char *msg
)
235 fprintf (stderr
, "%s\n", msg
);
240 gmp_default_alloc (size_t size
)
248 gmp_die("gmp_default_alloc: Virtual memory exhausted.");
254 gmp_default_realloc (void *old
, size_t old_size
, size_t new_size
)
258 p
= realloc (old
, new_size
);
261 gmp_die("gmp_default_realoc: Virtual memory exhausted.");
267 gmp_default_free (void *p
, size_t size
)
272 static void * (*gmp_allocate_func
) (size_t) = gmp_default_alloc
;
273 static void * (*gmp_reallocate_func
) (void *, size_t, size_t) = gmp_default_realloc
;
274 static void (*gmp_free_func
) (void *, size_t) = gmp_default_free
;
277 mp_get_memory_functions (void *(**alloc_func
) (size_t),
278 void *(**realloc_func
) (void *, size_t, size_t),
279 void (**free_func
) (void *, size_t))
282 *alloc_func
= gmp_allocate_func
;
285 *realloc_func
= gmp_reallocate_func
;
288 *free_func
= gmp_free_func
;
292 mp_set_memory_functions (void *(*alloc_func
) (size_t),
293 void *(*realloc_func
) (void *, size_t, size_t),
294 void (*free_func
) (void *, size_t))
297 alloc_func
= gmp_default_alloc
;
299 realloc_func
= gmp_default_realloc
;
301 free_func
= gmp_default_free
;
303 gmp_allocate_func
= alloc_func
;
304 gmp_reallocate_func
= realloc_func
;
305 gmp_free_func
= free_func
;
308 #define gmp_xalloc(size) ((*gmp_allocate_func)((size)))
309 #define gmp_free(p) ((*gmp_free_func) ((p), 0))
312 gmp_xalloc_limbs (mp_size_t size
)
314 return gmp_xalloc (size
* sizeof (mp_limb_t
));
318 gmp_xrealloc_limbs (mp_ptr old
, mp_size_t size
)
321 return (*gmp_reallocate_func
) (old
, 0, size
* sizeof (mp_limb_t
));
328 mpn_copyi (mp_ptr d
, mp_srcptr s
, mp_size_t n
)
331 for (i
= 0; i
< n
; i
++)
336 mpn_copyd (mp_ptr d
, mp_srcptr s
, mp_size_t n
)
343 mpn_cmp (mp_srcptr ap
, mp_srcptr bp
, mp_size_t n
)
347 if (ap
[n
-1] < bp
[n
-1])
349 else if (ap
[n
-1] > bp
[n
-1])
356 mpn_cmp4 (mp_srcptr ap
, mp_size_t an
, mp_srcptr bp
, mp_size_t bn
)
363 return mpn_cmp (ap
, bp
, an
);
367 mpn_normalized_size (mp_srcptr xp
, mp_size_t n
)
369 for (; n
> 0 && xp
[n
-1] == 0; n
--)
374 #define mpn_zero_p(xp, n) (mpn_normalized_size ((xp), (n)) == 0)
377 mpn_add_1 (mp_ptr rp
, mp_srcptr ap
, mp_size_t n
, mp_limb_t b
)
383 for (i
= 0; i
< n
; i
++)
385 mp_limb_t r
= ap
[i
] + b
;
394 mpn_add_n (mp_ptr rp
, mp_srcptr ap
, mp_srcptr bp
, mp_size_t n
)
399 for (i
= 0, cy
= 0; i
< n
; i
++)
402 a
= ap
[i
]; b
= bp
[i
];
413 mpn_add (mp_ptr rp
, mp_srcptr ap
, mp_size_t an
, mp_srcptr bp
, mp_size_t bn
)
419 cy
= mpn_add_n (rp
, ap
, bp
, bn
);
421 cy
= mpn_add_1 (rp
+ bn
, ap
+ bn
, an
- bn
, cy
);
426 mpn_sub_1 (mp_ptr rp
, mp_srcptr ap
, mp_size_t n
, mp_limb_t b
)
432 for (i
= 0; i
< n
; i
++)
436 mp_limb_t cy
= a
< b
;;
444 mpn_sub_n (mp_ptr rp
, mp_srcptr ap
, mp_srcptr bp
, mp_size_t n
)
449 for (i
= 0, cy
= 0; i
< n
; i
++)
452 a
= ap
[i
]; b
= bp
[i
];
462 mpn_sub (mp_ptr rp
, mp_srcptr ap
, mp_size_t an
, mp_srcptr bp
, mp_size_t bn
)
468 cy
= mpn_sub_n (rp
, ap
, bp
, bn
);
470 cy
= mpn_sub_1 (rp
+ bn
, ap
+ bn
, an
- bn
, cy
);
475 mpn_mul_1 (mp_ptr rp
, mp_srcptr up
, mp_size_t n
, mp_limb_t vl
)
477 mp_limb_t ul
, cl
, hpl
, lpl
;
485 gmp_umul_ppmm (hpl
, lpl
, ul
, vl
);
488 cl
= (lpl
< cl
) + hpl
;
498 mpn_addmul_1 (mp_ptr rp
, mp_srcptr up
, mp_size_t n
, mp_limb_t vl
)
500 mp_limb_t ul
, cl
, hpl
, lpl
, rl
;
508 gmp_umul_ppmm (hpl
, lpl
, ul
, vl
);
511 cl
= (lpl
< cl
) + hpl
;
524 mpn_submul_1 (mp_ptr rp
, mp_srcptr up
, mp_size_t n
, mp_limb_t vl
)
526 mp_limb_t ul
, cl
, hpl
, lpl
, rl
;
534 gmp_umul_ppmm (hpl
, lpl
, ul
, vl
);
537 cl
= (lpl
< cl
) + hpl
;
550 mpn_mul (mp_ptr rp
, mp_srcptr up
, mp_size_t un
, mp_srcptr vp
, mp_size_t vn
)
555 /* We first multiply by the low order limb. This result can be
556 stored, not added, to rp. We also avoid a loop for zeroing this
559 rp
[un
] = mpn_mul_1 (rp
, up
, un
, vp
[0]);
560 rp
+= 1, vp
+= 1, vn
-= 1;
562 /* Now accumulate the product of up[] and the next higher limb from
567 rp
[un
] = mpn_addmul_1 (rp
, up
, un
, vp
[0]);
568 rp
+= 1, vp
+= 1, vn
-= 1;
574 mpn_mul_n (mp_ptr rp
, mp_srcptr ap
, mp_srcptr bp
, mp_size_t n
)
576 mpn_mul (rp
, ap
, n
, bp
, n
);
580 mpn_sqr (mp_ptr rp
, mp_srcptr ap
, mp_size_t n
)
582 mpn_mul (rp
, ap
, n
, ap
, n
);
586 mpn_lshift (mp_ptr rp
, mp_srcptr up
, mp_size_t n
, unsigned int cnt
)
588 mp_limb_t high_limb
, low_limb
;
595 assert (cnt
< GMP_LIMB_BITS
);
600 tnc
= GMP_LIMB_BITS
- cnt
;
602 retval
= low_limb
>> tnc
;
603 high_limb
= (low_limb
<< cnt
);
605 for (i
= n
- 1; i
!= 0; i
--)
608 *--rp
= high_limb
| (low_limb
>> tnc
);
609 high_limb
= (low_limb
<< cnt
);
617 mpn_rshift (mp_ptr rp
, mp_srcptr up
, mp_size_t n
, unsigned int cnt
)
619 mp_limb_t high_limb
, low_limb
;
626 assert (cnt
< GMP_LIMB_BITS
);
628 tnc
= GMP_LIMB_BITS
- cnt
;
630 retval
= (high_limb
<< tnc
);
631 low_limb
= high_limb
>> cnt
;
633 for (i
= n
- 1; i
!= 0; i
--)
636 *rp
++ = low_limb
| (high_limb
<< tnc
);
637 low_limb
= high_limb
>> cnt
;
645 /* MPN division interface. */
647 mpn_invert_3by2 (mp_limb_t u1
, mp_limb_t u0
)
653 /* First, do a 2/1 inverse. */
654 /* The inverse m is defined as floor( (B^2 - 1 - u1)/u1 ), so that 0 <
655 * B^2 - (B + m) u1 <= u1 */
656 assert (u1
>= GMP_LIMB_HIGHBIT
);
658 ul
= u1
& GMP_LLIMB_MASK
;
659 uh
= u1
>> (GMP_LIMB_BITS
/ 2);
662 r
= ((~u1
- (mp_limb_t
) qh
* uh
) << (GMP_LIMB_BITS
/ 2)) | GMP_LLIMB_MASK
;
664 p
= (mp_limb_t
) qh
* ul
;
665 /* Adjustment steps taken from udiv_qrnnd_c */
670 if (r
>= u1
) /* i.e. we didn't get carry when adding to r */
679 /* Do a 3/2 division (with half limb size) */
680 p
= (r
>> (GMP_LIMB_BITS
/ 2)) * qh
+ r
;
681 ql
= (p
>> (GMP_LIMB_BITS
/ 2)) + 1;
683 /* By the 3/2 method, we don't need the high half limb. */
684 r
= (r
<< (GMP_LIMB_BITS
/ 2)) + GMP_LLIMB_MASK
- ql
* u1
;
686 if (r
>= (p
<< (GMP_LIMB_BITS
/ 2)))
691 m
= ((mp_limb_t
) qh
<< (GMP_LIMB_BITS
/ 2)) + ql
;
713 gmp_umul_ppmm (th
, tl
, u0
, m
);
718 if (r
> u1
|| (r
== u1
&& tl
> u0
))
726 struct gmp_div_inverse
728 /* Normalization shift count. */
730 /* Normalized divisor (d0 unused for mpn_div_qr_1) */
732 /* Inverse, for 2/1 or 3/2. */
737 mpn_div_qr_1_invert (struct gmp_div_inverse
*inv
, mp_limb_t d
)
744 inv
->d1
= d
<< shift
;
745 inv
->di
= mpn_invert_limb (inv
->d1
);
749 mpn_div_qr_2_invert (struct gmp_div_inverse
*inv
,
750 mp_limb_t d1
, mp_limb_t d0
)
759 d1
= (d1
<< shift
) | (d0
>> (GMP_LIMB_BITS
- shift
));
764 inv
->di
= mpn_invert_3by2 (d1
, d0
);
768 mpn_div_qr_invert (struct gmp_div_inverse
*inv
,
769 mp_srcptr dp
, mp_size_t dn
)
774 mpn_div_qr_1_invert (inv
, dp
[0]);
776 mpn_div_qr_2_invert (inv
, dp
[1], dp
[0]);
789 d1
= (d1
<< shift
) | (d0
>> (GMP_LIMB_BITS
- shift
));
790 d0
= (d0
<< shift
) | (dp
[dn
-3] >> (GMP_LIMB_BITS
- shift
));
794 inv
->di
= mpn_invert_3by2 (d1
, d0
);
798 /* Not matching current public gmp interface, rather corresponding to
799 the sbpi1_div_* functions. */
801 mpn_div_qr_1_preinv (mp_ptr qp
, mp_srcptr np
, mp_size_t nn
,
802 const struct gmp_div_inverse
*inv
)
810 tp
= gmp_xalloc_limbs (nn
);
811 r
= mpn_lshift (tp
, np
, nn
, inv
->shift
);
823 gmp_udiv_qrnnd_preinv (q
, r
, r
, np
[nn
], d
, di
);
830 return r
>> inv
->shift
;
834 mpn_div_qr_1 (mp_ptr qp
, mp_srcptr np
, mp_size_t nn
, mp_limb_t d
)
838 /* Special case for powers of two. */
839 if (d
> 1 && (d
& (d
-1)) == 0)
842 mp_limb_t r
= np
[0] & (d
-1);
845 mpn_rshift (qp
, np
, nn
, shift
);
851 struct gmp_div_inverse inv
;
852 mpn_div_qr_1_invert (&inv
, d
);
853 return mpn_div_qr_1_preinv (qp
, np
, nn
, &inv
);
858 mpn_div_qr_2_preinv (mp_ptr qp
, mp_ptr rp
, mp_srcptr np
, mp_size_t nn
,
859 const struct gmp_div_inverse
*inv
)
863 mp_limb_t d1
, d0
, di
, r1
, r0
;
874 tp
= gmp_xalloc_limbs (nn
);
875 r1
= mpn_lshift (tp
, np
, nn
, shift
);
883 for (i
= nn
- 2; i
>= 0; i
--)
887 gmp_udiv_qr_3by2 (q
, r1
, r0
, r1
, r0
, n0
, d1
, d0
, di
);
895 assert ((r0
<< (GMP_LIMB_BITS
- shift
)) == 0);
896 r0
= (r0
>> shift
) | (r1
<< (GMP_LIMB_BITS
- shift
));
908 mpn_div_qr_2 (mp_ptr qp
, mp_ptr rp
, mp_srcptr np
, mp_size_t nn
,
909 mp_limb_t d1
, mp_limb_t d0
)
911 struct gmp_div_inverse inv
;
914 mpn_div_qr_2_invert (&inv
, d1
, d0
);
915 mpn_div_qr_2_preinv (qp
, rp
, np
, nn
, &inv
);
920 mpn_div_qr_pi1 (mp_ptr qp
,
921 mp_ptr np
, mp_size_t nn
, mp_limb_t n1
,
922 mp_srcptr dp
, mp_size_t dn
,
933 assert ((dp
[dn
-1] & GMP_LIMB_HIGHBIT
) != 0);
938 /* Iteration variable is the index of the q limb.
940 * We divide <n1, np[dn-1+i], np[dn-2+i], np[dn-3+i],..., np[i]>
941 * by <d1, d0, dp[dn-3], ..., dp[0] >
944 for (i
= nn
- dn
; i
>= 0; i
--)
946 mp_limb_t n0
= np
[dn
-1+i
];
948 if (n1
== d1
&& n0
== d0
)
951 mpn_submul_1 (np
+i
, dp
, dn
, q
);
952 n1
= np
[dn
-1+i
]; /* update n1, last loop's value will now be invalid */
956 gmp_udiv_qr_3by2 (q
, n1
, n0
, n1
, n0
, np
[dn
-2+i
], d1
, d0
, dinv
);
958 cy
= mpn_submul_1 (np
+ i
, dp
, dn
-2, q
);
968 n1
+= d1
+ mpn_add_n (np
+ i
, np
+ i
, dp
, dn
- 1);
981 mpn_div_qr_preinv (mp_ptr qp
, mp_ptr np
, mp_size_t nn
,
982 mp_srcptr dp
, mp_size_t dn
,
983 const struct gmp_div_inverse
*inv
)
989 np
[0] = mpn_div_qr_1_preinv (qp
, np
, nn
, inv
);
991 mpn_div_qr_2_preinv (qp
, np
, np
, nn
, inv
);
997 assert (dp
[dn
-1] & GMP_LIMB_HIGHBIT
);
1001 nh
= mpn_lshift (np
, np
, nn
, shift
);
1005 assert (inv
->d1
== dp
[dn
-1]);
1006 assert (inv
->d0
== dp
[dn
-2]);
1008 mpn_div_qr_pi1 (qp
, np
, nn
, nh
, dp
, dn
, inv
->di
);
1011 gmp_assert_nocarry (mpn_rshift (np
, np
, dn
, shift
));
1016 mpn_div_qr (mp_ptr qp
, mp_ptr np
, mp_size_t nn
, mp_srcptr dp
, mp_size_t dn
)
1018 struct gmp_div_inverse inv
;
1024 mpn_div_qr_invert (&inv
, dp
, dn
);
1025 if (dn
> 2 && inv
.shift
> 0)
1027 tp
= gmp_xalloc_limbs (dn
);
1028 gmp_assert_nocarry (mpn_lshift (tp
, dp
, dn
, inv
.shift
));
1031 mpn_div_qr_preinv (qp
, np
, nn
, dp
, dn
, &inv
);
1037 /* MPN base conversion. */
1039 mpn_base_power_of_two_p (unsigned b
)
1055 struct mpn_base_info
1057 /* bb is the largest power of the base which fits in one limb, and
1058 exp is the corresponding exponent. */
1064 mpn_get_base_info (struct mpn_base_info
*info
, mp_limb_t b
)
1070 m
= GMP_LIMB_MAX
/ b
;
1071 for (exp
= 1, p
= b
; p
<= m
; exp
++)
1079 mpn_limb_size_in_base_2 (mp_limb_t u
)
1085 return GMP_LIMB_BITS
- shift
;
1089 mpn_get_str_bits (unsigned char *sp
, unsigned bits
, mp_srcptr up
, mp_size_t un
)
1096 sn
= ((un
- 1) * GMP_LIMB_BITS
+ mpn_limb_size_in_base_2 (up
[un
-1])
1099 mask
= (1U << bits
) - 1;
1101 for (i
= 0, j
= sn
, shift
= 0; j
-- > 0;)
1103 unsigned char digit
= up
[i
] >> shift
;
1107 if (shift
>= GMP_LIMB_BITS
&& ++i
< un
)
1109 shift
-= GMP_LIMB_BITS
;
1110 digit
|= up
[i
] << (bits
- shift
);
1112 sp
[j
] = digit
& mask
;
1117 /* We generate digits from the least significant end, and reverse at
1120 mpn_limb_get_str (unsigned char *sp
, mp_limb_t w
,
1121 const struct gmp_div_inverse
*binv
)
1124 for (i
= 0; w
> 0; i
++)
1128 h
= w
>> (GMP_LIMB_BITS
- binv
->shift
);
1129 l
= w
<< binv
->shift
;
1131 gmp_udiv_qrnnd_preinv (w
, r
, h
, l
, binv
->d1
, binv
->di
);
1132 assert ( (r
<< (GMP_LIMB_BITS
- binv
->shift
)) == 0);
1141 mpn_get_str_other (unsigned char *sp
,
1142 int base
, const struct mpn_base_info
*info
,
1143 mp_ptr up
, mp_size_t un
)
1145 struct gmp_div_inverse binv
;
1149 mpn_div_qr_1_invert (&binv
, base
);
1155 struct gmp_div_inverse bbinv
;
1156 mpn_div_qr_1_invert (&bbinv
, info
->bb
);
1162 w
= mpn_div_qr_1_preinv (up
, up
, un
, &bbinv
);
1163 un
-= (up
[un
-1] == 0);
1164 done
= mpn_limb_get_str (sp
+ sn
, w
, &binv
);
1166 for (sn
+= done
; done
< info
->exp
; done
++)
1171 sn
+= mpn_limb_get_str (sp
+ sn
, up
[0], &binv
);
1174 for (i
= 0; 2*i
+ 1 < sn
; i
++)
1176 unsigned char t
= sp
[i
];
1177 sp
[i
] = sp
[sn
- i
- 1];
1185 mpn_get_str (unsigned char *sp
, int base
, mp_ptr up
, mp_size_t un
)
1190 assert (up
[un
-1] > 0);
1192 bits
= mpn_base_power_of_two_p (base
);
1194 return mpn_get_str_bits (sp
, bits
, up
, un
);
1197 struct mpn_base_info info
;
1199 mpn_get_base_info (&info
, base
);
1200 return mpn_get_str_other (sp
, base
, &info
, up
, un
);
1205 mpn_set_str_bits (mp_ptr rp
, const unsigned char *sp
, size_t sn
,
1212 for (j
= sn
, rn
= 0, shift
= 0; j
-- > 0; )
1221 rp
[rn
-1] |= (mp_limb_t
) sp
[j
] << shift
;
1223 if (shift
>= GMP_LIMB_BITS
)
1225 shift
-= GMP_LIMB_BITS
;
1227 rp
[rn
++] = (mp_limb_t
) sp
[j
] >> (bits
- shift
);
1231 rn
= mpn_normalized_size (rp
, rn
);
1236 mpn_set_str_other (mp_ptr rp
, const unsigned char *sp
, size_t sn
,
1237 mp_limb_t b
, const struct mpn_base_info
*info
)
1245 first
= 1 + (sn
- 1) % info
->exp
;
1249 for (k
= 1; k
< first
; k
++)
1250 w
= w
* b
+ sp
[j
++];
1254 for (rn
= (w
> 0); j
< sn
;)
1259 for (k
= 1; k
< info
->exp
; k
++)
1260 w
= w
* b
+ sp
[j
++];
1262 cy
= mpn_mul_1 (rp
, rp
, rn
, info
->bb
);
1263 cy
+= mpn_add_1 (rp
, rp
, rn
, w
);
1273 mpn_set_str (mp_ptr rp
, const unsigned char *sp
, size_t sn
, int base
)
1280 bits
= mpn_base_power_of_two_p (base
);
1282 return mpn_set_str_bits (rp
, sp
, sn
, bits
);
1285 struct mpn_base_info info
;
1287 mpn_get_base_info (&info
, base
);
1288 return mpn_set_str_other (rp
, sp
, sn
, base
, &info
);
1299 r
->_mp_d
= gmp_xalloc_limbs (1);
1302 /* The utility of this function is a bit limited, since many functions
1303 assings the result variable using mpz_swap. */
1305 mpz_init2 (mpz_t r
, mp_bitcnt_t bits
)
1309 bits
-= (bits
!= 0); /* Round down, except if 0 */
1310 rn
= 1 + bits
/ GMP_LIMB_BITS
;
1314 r
->_mp_d
= gmp_xalloc_limbs (rn
);
1320 gmp_free (r
->_mp_d
);
1324 mpz_realloc (mpz_t r
, mp_size_t size
)
1326 size
= GMP_MAX (size
, 1);
1328 r
->_mp_d
= gmp_xrealloc_limbs (r
->_mp_d
, size
);
1329 r
->_mp_alloc
= size
;
1331 if (GMP_ABS (r
->_mp_size
) > size
)
1337 /* Realloc for an mpz_t WHAT if it has less than NEEDED limbs. */
1338 #define MPZ_REALLOC(z,n) ((n) > (z)->_mp_alloc \
1339 ? mpz_realloc(z,n) \
1342 /* MPZ assignment and basic conversions. */
1344 mpz_set_si (mpz_t r
, signed long int x
)
1351 r
->_mp_d
[0] = GMP_NEG_CAST (unsigned long int, x
);
1356 mpz_set_ui (mpz_t r
, unsigned long int x
)
1368 mpz_set (mpz_t r
, const mpz_t x
)
1370 /* Allow the NOP r == x */
1376 n
= GMP_ABS (x
->_mp_size
);
1377 rp
= MPZ_REALLOC (r
, n
);
1379 mpn_copyi (rp
, x
->_mp_d
, n
);
1380 r
->_mp_size
= x
->_mp_size
;
1385 mpz_init_set_si (mpz_t r
, signed long int x
)
1392 mpz_init_set_ui (mpz_t r
, unsigned long int x
)
1399 mpz_init_set (mpz_t r
, const mpz_t x
)
1406 mpz_fits_slong_p (const mpz_t u
)
1408 mp_size_t us
= u
->_mp_size
;
1413 return u
->_mp_d
[0] < GMP_LIMB_HIGHBIT
;
1415 return u
->_mp_d
[0] <= GMP_LIMB_HIGHBIT
;
1421 mpz_fits_ulong_p (const mpz_t u
)
1423 mp_size_t us
= u
->_mp_size
;
1425 return us
== 0 || us
== 1;
1429 mpz_get_si (const mpz_t u
)
1431 mp_size_t us
= u
->_mp_size
;
1434 return (long) (u
->_mp_d
[0] & ~GMP_LIMB_HIGHBIT
);
1436 return (long) (- u
->_mp_d
[0] | GMP_LIMB_HIGHBIT
);
1442 mpz_get_ui (const mpz_t u
)
1444 return u
->_mp_size
== 0 ? 0 : u
->_mp_d
[0];
1448 mpz_size (const mpz_t u
)
1450 return GMP_ABS (u
->_mp_size
);
1454 mpz_getlimbn (const mpz_t u
, mp_size_t n
)
1456 if (n
>= 0 && n
< GMP_ABS (u
->_mp_size
))
1463 /* Conversions and comparison to double. */
1465 mpz_set_d (mpz_t r
, double x
)
1474 /* x != x is true when x is a NaN, and x == x * 0.5 is true when x is
1475 zero or infinity. */
1476 if (x
== 0.0 || x
!= x
|| x
== x
* 0.5)
1495 B
= 2.0 * (double) GMP_LIMB_HIGHBIT
;
1497 for (rn
= 1; x
>= B
; rn
++)
1500 rp
= MPZ_REALLOC (r
, rn
);
1506 for (i
= rn
-1; i
-- > 0; )
1515 r
->_mp_size
= sign
? - rn
: rn
;
1519 mpz_init_set_d (mpz_t r
, double x
)
1526 mpz_get_d (const mpz_t u
)
1530 double B
= 2.0 * (double) GMP_LIMB_HIGHBIT
;
1532 un
= GMP_ABS (u
->_mp_size
);
1539 x
= B
*x
+ u
->_mp_d
[--un
];
1541 if (u
->_mp_size
< 0)
1548 mpz_cmpabs_d (const mpz_t x
, double d
)
1561 B
= 2.0 * (double) GMP_LIMB_HIGHBIT
;
1564 /* Scale d so it can be compared with the top limb. */
1565 for (i
= 1; i
< xn
; i
++)
1571 /* Compare floor(d) to top limb, subtract and cancel when equal. */
1572 for (i
= xn
; i
-- > 0;)
1589 mpz_cmp_d (const mpz_t x
, double d
)
1591 if (x
->_mp_size
< 0)
1596 return -mpz_cmpabs_d (x
, d
);
1603 return mpz_cmpabs_d (x
, d
);
1608 /* MPZ comparisons and the like. */
1610 mpz_sgn (const mpz_t u
)
1612 mp_size_t usize
= u
->_mp_size
;
1623 mpz_cmp_si (const mpz_t u
, long v
)
1625 mp_size_t usize
= u
->_mp_size
;
1630 return mpz_cmp_ui (u
, v
);
1631 else if (usize
>= 0)
1633 else /* usize == -1 */
1635 mp_limb_t ul
= u
->_mp_d
[0];
1636 if ((mp_limb_t
)GMP_NEG_CAST (unsigned long int, v
) < ul
)
1638 else if ( (mp_limb_t
)GMP_NEG_CAST (unsigned long int, v
) > ul
)
1645 mpz_cmp_ui (const mpz_t u
, unsigned long v
)
1647 mp_size_t usize
= u
->_mp_size
;
1655 mp_limb_t ul
= (usize
> 0) ? u
->_mp_d
[0] : 0;
1665 mpz_cmp (const mpz_t a
, const mpz_t b
)
1667 mp_size_t asize
= a
->_mp_size
;
1668 mp_size_t bsize
= b
->_mp_size
;
1672 else if (asize
< bsize
)
1675 return mpn_cmp (a
->_mp_d
, b
->_mp_d
, asize
);
1677 return -mpn_cmp (a
->_mp_d
, b
->_mp_d
, -asize
);
1683 mpz_cmpabs_ui (const mpz_t u
, unsigned long v
)
1685 mp_size_t un
= GMP_ABS (u
->_mp_size
);
1691 ul
= (un
== 1) ? u
->_mp_d
[0] : 0;
1702 mpz_cmpabs (const mpz_t u
, const mpz_t v
)
1704 return mpn_cmp4 (u
->_mp_d
, GMP_ABS (u
->_mp_size
),
1705 v
->_mp_d
, GMP_ABS (v
->_mp_size
));
1709 mpz_abs (mpz_t r
, const mpz_t u
)
1714 r
->_mp_size
= GMP_ABS (r
->_mp_size
);
1718 mpz_neg (mpz_t r
, const mpz_t u
)
1723 r
->_mp_size
= -r
->_mp_size
;
1727 mpz_swap (mpz_t u
, mpz_t v
)
1729 MP_SIZE_T_SWAP (u
->_mp_size
, v
->_mp_size
);
1730 MP_SIZE_T_SWAP (u
->_mp_alloc
, v
->_mp_alloc
);
1731 MP_PTR_SWAP (u
->_mp_d
, v
->_mp_d
);
1735 /* MPZ addition and subtraction */
1737 /* Adds to the absolute value. Returns new size, but doesn't store it. */
1739 mpz_abs_add_ui (mpz_t r
, const mpz_t a
, unsigned long b
)
1745 an
= GMP_ABS (a
->_mp_size
);
1752 rp
= MPZ_REALLOC (r
, an
+ 1);
1754 cy
= mpn_add_1 (rp
, a
->_mp_d
, an
, b
);
1761 /* Subtract from the absolute value. Returns new size, (or -1 on underflow),
1762 but doesn't store it. */
1764 mpz_abs_sub_ui (mpz_t r
, const mpz_t a
, unsigned long b
)
1766 mp_size_t an
= GMP_ABS (a
->_mp_size
);
1767 mp_ptr rp
= MPZ_REALLOC (r
, an
);
1774 else if (an
== 1 && a
->_mp_d
[0] < b
)
1776 rp
[0] = b
- a
->_mp_d
[0];
1781 gmp_assert_nocarry (mpn_sub_1 (rp
, a
->_mp_d
, an
, b
));
1782 return mpn_normalized_size (rp
, an
);
1787 mpz_add_ui (mpz_t r
, const mpz_t a
, unsigned long b
)
1789 if (a
->_mp_size
>= 0)
1790 r
->_mp_size
= mpz_abs_add_ui (r
, a
, b
);
1792 r
->_mp_size
= -mpz_abs_sub_ui (r
, a
, b
);
1796 mpz_sub_ui (mpz_t r
, const mpz_t a
, unsigned long b
)
1798 if (a
->_mp_size
< 0)
1799 r
->_mp_size
= -mpz_abs_add_ui (r
, a
, b
);
1801 r
->_mp_size
= mpz_abs_sub_ui (r
, a
, b
);
1805 mpz_ui_sub (mpz_t r
, unsigned long a
, const mpz_t b
)
1807 if (b
->_mp_size
< 0)
1808 r
->_mp_size
= mpz_abs_add_ui (r
, b
, a
);
1810 r
->_mp_size
= -mpz_abs_sub_ui (r
, b
, a
);
1814 mpz_abs_add (mpz_t r
, const mpz_t a
, const mpz_t b
)
1816 mp_size_t an
= GMP_ABS (a
->_mp_size
);
1817 mp_size_t bn
= GMP_ABS (b
->_mp_size
);
1822 rn
= GMP_MAX (an
, bn
);
1823 rp
= MPZ_REALLOC (r
, rn
+ 1);
1825 cy
= mpn_add (rp
, a
->_mp_d
, an
, b
->_mp_d
, bn
);
1827 cy
= mpn_add (rp
, b
->_mp_d
, bn
, a
->_mp_d
, an
);
1831 return rn
+ (cy
> 0);
1835 mpz_abs_sub (mpz_t r
, const mpz_t a
, const mpz_t b
)
1837 mp_size_t an
= GMP_ABS (a
->_mp_size
);
1838 mp_size_t bn
= GMP_ABS (b
->_mp_size
);
1842 cmp
= mpn_cmp4 (a
->_mp_d
, an
, b
->_mp_d
, bn
);
1845 rp
= MPZ_REALLOC (r
, an
);
1846 gmp_assert_nocarry (mpn_sub (rp
, a
->_mp_d
, an
, b
->_mp_d
, bn
));
1847 return mpn_normalized_size (rp
, an
);
1851 rp
= MPZ_REALLOC (r
, bn
);
1852 gmp_assert_nocarry (mpn_sub (rp
, b
->_mp_d
, bn
, a
->_mp_d
, an
));
1853 return -mpn_normalized_size (rp
, bn
);
1860 mpz_add (mpz_t r
, const mpz_t a
, const mpz_t b
)
1864 if ( (a
->_mp_size
^ b
->_mp_size
) >= 0)
1865 rn
= mpz_abs_add (r
, a
, b
);
1867 rn
= mpz_abs_sub (r
, a
, b
);
1869 r
->_mp_size
= a
->_mp_size
>= 0 ? rn
: - rn
;
1873 mpz_sub (mpz_t r
, const mpz_t a
, const mpz_t b
)
1877 if ( (a
->_mp_size
^ b
->_mp_size
) >= 0)
1878 rn
= mpz_abs_sub (r
, a
, b
);
1880 rn
= mpz_abs_add (r
, a
, b
);
1882 r
->_mp_size
= a
->_mp_size
>= 0 ? rn
: - rn
;
1886 /* MPZ multiplication */
1888 mpz_mul_si (mpz_t r
, const mpz_t u
, long int v
)
1892 mpz_mul_ui (r
, u
, GMP_NEG_CAST (unsigned long int, v
));
1896 mpz_mul_ui (r
, u
, (unsigned long int) v
);
1900 mpz_mul_ui (mpz_t r
, const mpz_t u
, unsigned long int v
)
1907 un
= GMP_ABS (u
->_mp_size
);
1909 if (un
== 0 || v
== 0)
1915 mpz_init2 (t
, (un
+ 1) * GMP_LIMB_BITS
);
1918 cy
= mpn_mul_1 (tp
, u
->_mp_d
, un
, v
);
1921 t
->_mp_size
= un
+ (cy
> 0);
1922 if (u
->_mp_size
< 0)
1923 t
->_mp_size
= - t
->_mp_size
;
1930 mpz_mul (mpz_t r
, const mpz_t u
, const mpz_t v
)
1933 mp_size_t un
, vn
, rn
;
1937 un
= GMP_ABS (u
->_mp_size
);
1938 vn
= GMP_ABS (v
->_mp_size
);
1940 if (un
== 0 || vn
== 0)
1946 sign
= (u
->_mp_size
^ v
->_mp_size
) < 0;
1948 mpz_init2 (t
, (un
+ vn
) * GMP_LIMB_BITS
);
1952 mpn_mul (tp
, u
->_mp_d
, un
, v
->_mp_d
, vn
);
1954 mpn_mul (tp
, v
->_mp_d
, vn
, u
->_mp_d
, un
);
1957 rn
-= tp
[rn
-1] == 0;
1959 t
->_mp_size
= sign
? - rn
: rn
;
1965 mpz_mul_2exp (mpz_t r
, const mpz_t u
, mp_bitcnt_t bits
)
1972 un
= GMP_ABS (u
->_mp_size
);
1979 limbs
= bits
/ GMP_LIMB_BITS
;
1980 shift
= bits
% GMP_LIMB_BITS
;
1982 rn
= un
+ limbs
+ (shift
> 0);
1983 rp
= MPZ_REALLOC (r
, rn
);
1986 mp_limb_t cy
= mpn_lshift (rp
+ limbs
, u
->_mp_d
, un
, shift
);
1991 mpn_copyd (rp
+ limbs
, u
->_mp_d
, un
);
1996 r
->_mp_size
= (u
->_mp_size
< 0) ? - rn
: rn
;
2001 enum mpz_div_round_mode
{ GMP_DIV_FLOOR
, GMP_DIV_CEIL
, GMP_DIV_TRUNC
};
2003 /* Allows q or r to be zero. Returns 1 iff remainder is non-zero. */
2005 mpz_div_qr (mpz_t q
, mpz_t r
,
2006 const mpz_t n
, const mpz_t d
, enum mpz_div_round_mode mode
)
2008 mp_size_t ns
, ds
, nn
, dn
, qs
;
2013 gmp_die("mpz_div_qr: Divide by zero.");
2031 if (mode
== GMP_DIV_CEIL
&& qs
>= 0)
2033 /* q = 1, r = n - d */
2039 else if (mode
== GMP_DIV_FLOOR
&& qs
< 0)
2041 /* q = -1, r = n + d */
2071 mpz_init2 (tq
, qn
* GMP_LIMB_BITS
);
2077 mpn_div_qr (qp
, np
, nn
, d
->_mp_d
, dn
);
2081 qn
-= (qp
[qn
-1] == 0);
2083 tq
->_mp_size
= qs
< 0 ? -qn
: qn
;
2085 rn
= mpn_normalized_size (np
, dn
);
2086 tr
->_mp_size
= ns
< 0 ? - rn
: rn
;
2088 if (mode
== GMP_DIV_FLOOR
&& qs
< 0 && rn
!= 0)
2091 mpz_sub_ui (tq
, tq
, 1);
2093 mpz_add (tr
, tr
, d
);
2095 else if (mode
== GMP_DIV_CEIL
&& qs
>= 0 && rn
!= 0)
2098 mpz_add_ui (tq
, tq
, 1);
2100 mpz_sub (tr
, tr
, d
);
2118 mpz_cdiv_qr (mpz_t q
, mpz_t r
, const mpz_t n
, const mpz_t d
)
2120 mpz_div_qr (q
, r
, n
, d
, GMP_DIV_CEIL
);
2124 mpz_fdiv_qr (mpz_t q
, mpz_t r
, const mpz_t n
, const mpz_t d
)
2126 mpz_div_qr (q
, r
, n
, d
, GMP_DIV_FLOOR
);
2130 mpz_tdiv_qr (mpz_t q
, mpz_t r
, const mpz_t n
, const mpz_t d
)
2132 mpz_div_qr (q
, r
, n
, d
, GMP_DIV_TRUNC
);
2136 mpz_cdiv_q (mpz_t q
, const mpz_t n
, const mpz_t d
)
2138 mpz_div_qr (q
, NULL
, n
, d
, GMP_DIV_CEIL
);
2142 mpz_fdiv_q (mpz_t q
, const mpz_t n
, const mpz_t d
)
2144 mpz_div_qr (q
, NULL
, n
, d
, GMP_DIV_FLOOR
);
2148 mpz_tdiv_q (mpz_t q
, const mpz_t n
, const mpz_t d
)
2150 mpz_div_qr (q
, NULL
, n
, d
, GMP_DIV_TRUNC
);
2154 mpz_cdiv_r (mpz_t r
, const mpz_t n
, const mpz_t d
)
2156 mpz_div_qr (NULL
, r
, n
, d
, GMP_DIV_CEIL
);
2160 mpz_fdiv_r (mpz_t r
, const mpz_t n
, const mpz_t d
)
2162 mpz_div_qr (NULL
, r
, n
, d
, GMP_DIV_FLOOR
);
2166 mpz_tdiv_r (mpz_t r
, const mpz_t n
, const mpz_t d
)
2168 mpz_div_qr (NULL
, r
, n
, d
, GMP_DIV_TRUNC
);
2172 mpz_mod (mpz_t r
, const mpz_t n
, const mpz_t d
)
2174 if (d
->_mp_size
>= 0)
2175 mpz_div_qr (NULL
, r
, n
, d
, GMP_DIV_FLOOR
);
2177 mpz_div_qr (NULL
, r
, n
, d
, GMP_DIV_CEIL
);
2181 mpz_div_q_2exp (mpz_t q
, const mpz_t u
, mp_bitcnt_t bit_index
,
2182 enum mpz_div_round_mode mode
)
2195 limb_cnt
= bit_index
/ GMP_LIMB_BITS
;
2196 qn
= GMP_ABS (un
) - limb_cnt
;
2197 bit_index
%= GMP_LIMB_BITS
;
2199 if (mode
== ((un
> 0) ? GMP_DIV_CEIL
: GMP_DIV_FLOOR
)) /* un != 0 here. */
2200 /* Note: Below, the final indexing at limb_cnt is valid because at
2201 that point we have qn > 0. */
2203 || !mpn_zero_p (u
->_mp_d
, limb_cnt
)
2204 || (u
->_mp_d
[limb_cnt
]
2205 & (((mp_limb_t
) 1 << bit_index
) - 1)));
2214 qp
= MPZ_REALLOC (q
, qn
);
2218 mpn_rshift (qp
, u
->_mp_d
+ limb_cnt
, qn
, bit_index
);
2219 qn
-= qp
[qn
- 1] == 0;
2223 mpn_copyi (qp
, u
->_mp_d
+ limb_cnt
, qn
);
2229 mpz_add_ui (q
, q
, adjust
);
2235 mpz_div_r_2exp (mpz_t r
, const mpz_t u
, mp_bitcnt_t bit_index
,
2236 enum mpz_div_round_mode mode
)
2238 mp_size_t us
, un
, rn
;
2243 if (us
== 0 || bit_index
== 0)
2248 rn
= (bit_index
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
2251 rp
= MPZ_REALLOC (r
, rn
);
2254 mask
= GMP_LIMB_MAX
>> (rn
* GMP_LIMB_BITS
- bit_index
);
2258 /* Quotient (with truncation) is zero, and remainder is
2260 if (mode
== ((us
> 0) ? GMP_DIV_CEIL
: GMP_DIV_FLOOR
)) /* us != 0 here. */
2262 /* Have to negate and sign extend. */
2266 for (cy
= 1, i
= 0; i
< un
; i
++)
2268 mp_limb_t s
= ~u
->_mp_d
[i
] + cy
;
2273 for (; i
< rn
- 1; i
++)
2274 rp
[i
] = GMP_LIMB_MAX
;
2283 mpn_copyi (rp
, u
->_mp_d
, un
);
2291 mpn_copyi (rp
, u
->_mp_d
, rn
- 1);
2293 rp
[rn
-1] = u
->_mp_d
[rn
-1] & mask
;
2295 if (mode
== ((us
> 0) ? GMP_DIV_CEIL
: GMP_DIV_FLOOR
)) /* us != 0 here. */
2297 /* If r != 0, compute 2^{bit_count} - r. */
2300 for (i
= 0; i
< rn
&& rp
[i
] == 0; i
++)
2304 /* r > 0, need to flip sign. */
2306 for (i
++; i
< rn
; i
++)
2311 /* us is not used for anything else, so we can modify it
2312 here to indicate flipped sign. */
2317 rn
= mpn_normalized_size (rp
, rn
);
2318 r
->_mp_size
= us
< 0 ? -rn
: rn
;
2322 mpz_cdiv_q_2exp (mpz_t r
, const mpz_t u
, mp_bitcnt_t cnt
)
2324 mpz_div_q_2exp (r
, u
, cnt
, GMP_DIV_CEIL
);
2328 mpz_fdiv_q_2exp (mpz_t r
, const mpz_t u
, mp_bitcnt_t cnt
)
2330 mpz_div_q_2exp (r
, u
, cnt
, GMP_DIV_FLOOR
);
2334 mpz_tdiv_q_2exp (mpz_t r
, const mpz_t u
, mp_bitcnt_t cnt
)
2336 mpz_div_q_2exp (r
, u
, cnt
, GMP_DIV_TRUNC
);
2340 mpz_cdiv_r_2exp (mpz_t r
, const mpz_t u
, mp_bitcnt_t cnt
)
2342 mpz_div_r_2exp (r
, u
, cnt
, GMP_DIV_CEIL
);
2346 mpz_fdiv_r_2exp (mpz_t r
, const mpz_t u
, mp_bitcnt_t cnt
)
2348 mpz_div_r_2exp (r
, u
, cnt
, GMP_DIV_FLOOR
);
2352 mpz_tdiv_r_2exp (mpz_t r
, const mpz_t u
, mp_bitcnt_t cnt
)
2354 mpz_div_r_2exp (r
, u
, cnt
, GMP_DIV_TRUNC
);
2358 mpz_divexact (mpz_t q
, const mpz_t n
, const mpz_t d
)
2360 gmp_assert_nocarry (mpz_div_qr (q
, NULL
, n
, d
, GMP_DIV_TRUNC
));
2364 mpz_divisible_p (const mpz_t n
, const mpz_t d
)
2366 return mpz_div_qr (NULL
, NULL
, n
, d
, GMP_DIV_TRUNC
) == 0;
2369 static unsigned long
2370 mpz_div_qr_ui (mpz_t q
, mpz_t r
,
2371 const mpz_t n
, unsigned long d
, enum mpz_div_round_mode mode
)
2390 qp
= MPZ_REALLOC (q
, qn
);
2394 rl
= mpn_div_qr_1 (qp
, n
->_mp_d
, qn
, d
);
2398 rs
= (ns
< 0) ? -rs
: rs
;
2400 if (rl
> 0 && ( (mode
== GMP_DIV_FLOOR
&& ns
< 0)
2401 || (mode
== GMP_DIV_CEIL
&& ns
>= 0)))
2404 gmp_assert_nocarry (mpn_add_1 (qp
, qp
, qn
, 1));
2416 qn
-= (qp
[qn
-1] == 0);
2417 assert (qn
== 0 || qp
[qn
-1] > 0);
2419 q
->_mp_size
= (ns
< 0) ? - qn
: qn
;
2426 mpz_cdiv_qr_ui (mpz_t q
, mpz_t r
, const mpz_t n
, unsigned long d
)
2428 return mpz_div_qr_ui (q
, r
, n
, d
, GMP_DIV_CEIL
);
2432 mpz_fdiv_qr_ui (mpz_t q
, mpz_t r
, const mpz_t n
, unsigned long d
)
2434 return mpz_div_qr_ui (q
, r
, n
, d
, GMP_DIV_FLOOR
);
2438 mpz_tdiv_qr_ui (mpz_t q
, mpz_t r
, const mpz_t n
, unsigned long d
)
2440 return mpz_div_qr_ui (q
, r
, n
, d
, GMP_DIV_TRUNC
);
2444 mpz_cdiv_q_ui (mpz_t q
, const mpz_t n
, unsigned long d
)
2446 return mpz_div_qr_ui (q
, NULL
, n
, d
, GMP_DIV_CEIL
);
2450 mpz_fdiv_q_ui (mpz_t q
, const mpz_t n
, unsigned long d
)
2452 return mpz_div_qr_ui (q
, NULL
, n
, d
, GMP_DIV_FLOOR
);
2456 mpz_tdiv_q_ui (mpz_t q
, const mpz_t n
, unsigned long d
)
2458 return mpz_div_qr_ui (q
, NULL
, n
, d
, GMP_DIV_TRUNC
);
2462 mpz_cdiv_r_ui (mpz_t r
, const mpz_t n
, unsigned long d
)
2464 return mpz_div_qr_ui (NULL
, r
, n
, d
, GMP_DIV_CEIL
);
2467 mpz_fdiv_r_ui (mpz_t r
, const mpz_t n
, unsigned long d
)
2469 return mpz_div_qr_ui (NULL
, r
, n
, d
, GMP_DIV_FLOOR
);
2472 mpz_tdiv_r_ui (mpz_t r
, const mpz_t n
, unsigned long d
)
2474 return mpz_div_qr_ui (NULL
, r
, n
, d
, GMP_DIV_TRUNC
);
2478 mpz_cdiv_ui (const mpz_t n
, unsigned long d
)
2480 return mpz_div_qr_ui (NULL
, NULL
, n
, d
, GMP_DIV_CEIL
);
2484 mpz_fdiv_ui (const mpz_t n
, unsigned long d
)
2486 return mpz_div_qr_ui (NULL
, NULL
, n
, d
, GMP_DIV_FLOOR
);
2490 mpz_tdiv_ui (const mpz_t n
, unsigned long d
)
2492 return mpz_div_qr_ui (NULL
, NULL
, n
, d
, GMP_DIV_TRUNC
);
2496 mpz_mod_ui (mpz_t r
, const mpz_t n
, unsigned long d
)
2498 return mpz_div_qr_ui (NULL
, r
, n
, d
, GMP_DIV_FLOOR
);
2502 mpz_divexact_ui (mpz_t q
, const mpz_t n
, unsigned long d
)
2504 gmp_assert_nocarry (mpz_div_qr_ui (q
, NULL
, n
, d
, GMP_DIV_TRUNC
));
2508 mpz_divisible_ui_p (const mpz_t n
, unsigned long d
)
2510 return mpz_div_qr_ui (NULL
, NULL
, n
, d
, GMP_DIV_TRUNC
) == 0;
2516 mpn_gcd_11 (mp_limb_t u
, mp_limb_t v
)
2520 assert ( (u
| v
) > 0);
2527 gmp_ctz (shift
, u
| v
);
2533 MP_LIMB_T_SWAP (u
, v
);
2535 while ( (v
& 1) == 0)
2545 while ( (u
& 1) == 0);
2552 while ( (v
& 1) == 0);
2559 mpz_gcd_ui (mpz_t g
, const mpz_t u
, unsigned long v
)
2570 un
= GMP_ABS (u
->_mp_size
);
2572 v
= mpn_gcd_11 (mpn_div_qr_1 (NULL
, u
->_mp_d
, un
, v
), v
);
2582 mpz_make_odd (mpz_t r
, const mpz_t u
)
2584 mp_size_t un
, rn
, i
;
2588 un
= GMP_ABS (u
->_mp_size
);
2591 for (i
= 0; u
->_mp_d
[i
] == 0; i
++)
2594 gmp_ctz (shift
, u
->_mp_d
[i
]);
2597 rp
= MPZ_REALLOC (r
, rn
);
2600 mpn_rshift (rp
, u
->_mp_d
+ i
, rn
, shift
);
2601 rn
-= (rp
[rn
-1] == 0);
2604 mpn_copyi (rp
, u
->_mp_d
+ i
, rn
);
2607 return i
* GMP_LIMB_BITS
+ shift
;
2611 mpz_gcd (mpz_t g
, const mpz_t u
, const mpz_t v
)
2614 mp_bitcnt_t uz
, vz
, gz
;
2616 if (u
->_mp_size
== 0)
2621 if (v
->_mp_size
== 0)
2630 uz
= mpz_make_odd (tu
, u
);
2631 vz
= mpz_make_odd (tv
, v
);
2632 gz
= GMP_MIN (uz
, vz
);
2634 if (tu
->_mp_size
< tv
->_mp_size
)
2637 mpz_tdiv_r (tu
, tu
, tv
);
2638 if (tu
->_mp_size
== 0)
2647 mpz_make_odd (tu
, tu
);
2648 c
= mpz_cmp (tu
, tv
);
2657 if (tv
->_mp_size
== 1)
2659 mp_limb_t vl
= tv
->_mp_d
[0];
2660 mp_limb_t ul
= mpz_tdiv_ui (tu
, vl
);
2661 mpz_set_ui (g
, mpn_gcd_11 (ul
, vl
));
2664 mpz_sub (tu
, tu
, tv
);
2668 mpz_mul_2exp (g
, g
, gz
);
2672 mpz_gcdext (mpz_t g
, mpz_t s
, mpz_t t
, const mpz_t u
, const mpz_t v
)
2674 mpz_t tu
, tv
, s0
, s1
, t0
, t1
;
2675 mp_bitcnt_t uz
, vz
, gz
;
2678 if (u
->_mp_size
== 0)
2680 /* g = 0 u + sgn(v) v */
2681 signed long sign
= mpz_sgn (v
);
2686 mpz_set_si (t
, sign
);
2690 if (v
->_mp_size
== 0)
2692 /* g = sgn(u) u + 0 v */
2693 signed long sign
= mpz_sgn (u
);
2696 mpz_set_si (s
, sign
);
2709 uz
= mpz_make_odd (tu
, u
);
2710 vz
= mpz_make_odd (tv
, v
);
2711 gz
= GMP_MIN (uz
, vz
);
2716 /* Cofactors corresponding to odd gcd. gz handled later. */
2717 if (tu
->_mp_size
< tv
->_mp_size
)
2720 MPZ_SRCPTR_SWAP (u
, v
);
2721 MPZ_PTR_SWAP (s
, t
);
2722 MP_BITCNT_T_SWAP (uz
, vz
);
2730 * where u and v denote the inputs with common factors of two
2731 * eliminated, and det (s0, t0; s1, t1) = 2^p. Then
2733 * 2^p tu = s1 u - t1 v
2734 * 2^p tv = -s0 u + t0 v
2737 /* After initial division, tu = q tv + tu', we have
2739 * u = 2^uz (tu' + q tv)
2744 * t0 = 2^uz, t1 = 2^uz q
2748 mpz_setbit (t0
, uz
);
2749 mpz_tdiv_qr (t1
, tu
, tu
, tv
);
2750 mpz_mul_2exp (t1
, t1
, uz
);
2752 mpz_setbit (s1
, vz
);
2755 if (tu
->_mp_size
> 0)
2758 shift
= mpz_make_odd (tu
, tu
);
2759 mpz_mul_2exp (t0
, t0
, shift
);
2760 mpz_mul_2exp (s0
, s0
, shift
);
2766 c
= mpz_cmp (tu
, tv
);
2774 * u = t0 tu + t1 (tv' + tu) = (t0 + t1) tu + t1 tv'
2775 * v = s0 tu + s1 (tv' + tu) = (s0 + s1) tu + s1 tv' */
2777 mpz_sub (tv
, tv
, tu
);
2778 mpz_add (t0
, t0
, t1
);
2779 mpz_add (s0
, s0
, s1
);
2781 shift
= mpz_make_odd (tv
, tv
);
2782 mpz_mul_2exp (t1
, t1
, shift
);
2783 mpz_mul_2exp (s1
, s1
, shift
);
2787 mpz_sub (tu
, tu
, tv
);
2788 mpz_add (t1
, t0
, t1
);
2789 mpz_add (s1
, s0
, s1
);
2791 shift
= mpz_make_odd (tu
, tu
);
2792 mpz_mul_2exp (t0
, t0
, shift
);
2793 mpz_mul_2exp (s0
, s0
, shift
);
2799 /* Now tv = odd part of gcd, and -s0 and t0 are corresponding
2802 mpz_mul_2exp (tv
, tv
, gz
);
2805 /* 2^p g = s0 u + t0 v. Eliminate one factor of two at a time. To
2806 adjust cofactors, we need u / g and v / g */
2808 mpz_divexact (s1
, v
, tv
);
2810 mpz_divexact (t1
, u
, tv
);
2815 /* s0 u + t0 v = (s0 - v/g) u - (t0 + u/g) v */
2816 if (mpz_odd_p (s0
) || mpz_odd_p (t0
))
2818 mpz_sub (s0
, s0
, s1
);
2819 mpz_add (t0
, t0
, t1
);
2821 mpz_divexact_ui (s0
, s0
, 2);
2822 mpz_divexact_ui (t0
, t0
, 2);
2825 /* Arrange so that |s| < |u| / 2g */
2826 mpz_add (s1
, s0
, s1
);
2827 if (mpz_cmpabs (s0
, s1
) > 0)
2830 mpz_sub (t0
, t0
, t1
);
2832 if (u
->_mp_size
< 0)
2834 if (v
->_mp_size
< 0)
2852 mpz_lcm (mpz_t r
, const mpz_t u
, const mpz_t v
)
2856 if (u
->_mp_size
== 0 || v
->_mp_size
== 0)
2865 mpz_divexact (g
, u
, g
);
2873 mpz_lcm_ui (mpz_t r
, const mpz_t u
, unsigned long v
)
2875 if (v
== 0 || u
->_mp_size
== 0)
2881 v
/= mpz_gcd_ui (NULL
, u
, v
);
2882 mpz_mul_ui (r
, u
, v
);
2888 mpz_invert (mpz_t r
, const mpz_t u
, const mpz_t m
)
2893 if (u
->_mp_size
== 0 || mpz_cmpabs_ui (m
, 1) <= 0)
2899 mpz_gcdext (g
, tr
, NULL
, u
, m
);
2900 invertible
= (mpz_cmp_ui (g
, 1) == 0);
2904 if (tr
->_mp_size
< 0)
2906 if (m
->_mp_size
>= 0)
2907 mpz_add (tr
, tr
, m
);
2909 mpz_sub (tr
, tr
, m
);
2920 /* Higher level operations (sqrt, pow and root) */
2923 mpz_pow_ui (mpz_t r
, const mpz_t b
, unsigned long e
)
2927 mpz_init_set_ui (tr
, 1);
2929 for (bit
= GMP_ULONG_HIGHBIT
; bit
> 0; bit
>>= 1)
2931 mpz_mul (tr
, tr
, tr
);
2933 mpz_mul (tr
, tr
, b
);
2940 mpz_ui_pow_ui (mpz_t r
, unsigned long blimb
, unsigned long e
)
2943 mpz_init_set_ui (b
, blimb
);
2944 mpz_pow_ui (r
, b
, e
);
2949 mpz_powm (mpz_t r
, const mpz_t b
, const mpz_t e
, const mpz_t m
)
2955 struct gmp_div_inverse minv
;
2959 en
= GMP_ABS (e
->_mp_size
);
2960 mn
= GMP_ABS (m
->_mp_size
);
2962 gmp_die ("mpz_powm: Zero modulo.");
2971 mpn_div_qr_invert (&minv
, mp
, mn
);
2976 /* To avoid shifts, we do all our reductions, except the final
2977 one, using a *normalized* m. */
2980 tp
= gmp_xalloc_limbs (mn
);
2981 gmp_assert_nocarry (mpn_lshift (tp
, mp
, mn
, shift
));
2987 if (e
->_mp_size
< 0)
2989 if (!mpz_invert (base
, b
, m
))
2990 gmp_die ("mpz_powm: Negative exponent and non-invertibe base.");
2997 bn
= base
->_mp_size
;
3000 mpn_div_qr_preinv (NULL
, base
->_mp_d
, base
->_mp_size
, mp
, mn
, &minv
);
3004 /* We have reduced the absolute value. Now take care of the
3005 sign. Note that we get zero represented non-canonically as
3007 if (b
->_mp_size
< 0)
3009 mp_ptr bp
= MPZ_REALLOC (base
, mn
);
3010 gmp_assert_nocarry (mpn_sub (bp
, mp
, mn
, bp
, bn
));
3013 base
->_mp_size
= mpn_normalized_size (base
->_mp_d
, bn
);
3015 mpz_init_set_ui (tr
, 1);
3019 mp_limb_t w
= e
->_mp_d
[en
];
3022 for (bit
= GMP_LIMB_HIGHBIT
; bit
> 0; bit
>>= 1)
3024 mpz_mul (tr
, tr
, tr
);
3026 mpz_mul (tr
, tr
, base
);
3027 if (tr
->_mp_size
> mn
)
3029 mpn_div_qr_preinv (NULL
, tr
->_mp_d
, tr
->_mp_size
, mp
, mn
, &minv
);
3030 tr
->_mp_size
= mpn_normalized_size (tr
->_mp_d
, mn
);
3035 /* Final reduction */
3036 if (tr
->_mp_size
>= mn
)
3039 mpn_div_qr_preinv (NULL
, tr
->_mp_d
, tr
->_mp_size
, mp
, mn
, &minv
);
3040 tr
->_mp_size
= mpn_normalized_size (tr
->_mp_d
, mn
);
3051 mpz_powm_ui (mpz_t r
, const mpz_t b
, unsigned long elimb
, const mpz_t m
)
3054 mpz_init_set_ui (e
, elimb
);
3055 mpz_powm (r
, b
, e
, m
);
3059 /* x=trunc(y^(1/z)), r=y-x^z */
3061 mpz_rootrem (mpz_t x
, mpz_t r
, const mpz_t y
, unsigned long z
)
3066 sgn
= y
->_mp_size
< 0;
3067 if (sgn
&& (z
& 1) == 0)
3068 gmp_die ("mpz_rootrem: Negative argument, with even root.");
3070 gmp_die ("mpz_rootrem: Zeroth root.");
3072 if (mpz_cmpabs_ui (y
, 1) <= 0) {
3081 mpz_setbit (t
, mpz_sizeinbase (y
, 2) / z
+ 1);
3083 if (z
== 2) /* simplify sqrt loop: z-1 == 1 */
3085 mpz_swap (u
, t
); /* u = x */
3086 mpz_tdiv_q (t
, y
, u
); /* t = y/x */
3087 mpz_add (t
, t
, u
); /* t = y/x + x */
3088 mpz_tdiv_q_2exp (t
, t
, 1); /* x'= (y/x + x)/2 */
3089 } while (mpz_cmpabs (t
, u
) < 0); /* |x'| < |x| */
3098 mpz_swap (u
, t
); /* u = x */
3099 mpz_pow_ui (t
, u
, z
- 1); /* t = x^(z-1) */
3100 mpz_tdiv_q (t
, y
, t
); /* t = y/x^(z-1) */
3101 mpz_mul_ui (v
, u
, z
- 1); /* v = x*(z-1) */
3102 mpz_add (t
, t
, v
); /* t = y/x^(z-1) + x*(z-1) */
3103 mpz_tdiv_q_ui (t
, t
, z
); /* x'=(y/x^(z-1) + x*(z-1))/z */
3104 } while (mpz_cmpabs (t
, u
) < 0); /* |x'| < |x| */
3110 mpz_pow_ui (t
, u
, z
);
3119 mpz_root (mpz_t x
, const mpz_t y
, unsigned long z
)
3125 mpz_rootrem (x
, r
, y
, z
);
3126 res
= r
->_mp_size
== 0;
3132 /* Compute s = floor(sqrt(u)) and r = u - s^2. Allows r == NULL */
3134 mpz_sqrtrem (mpz_t s
, mpz_t r
, const mpz_t u
)
3136 mpz_rootrem (s
, r
, u
, 2);
3140 mpz_sqrt (mpz_t s
, const mpz_t u
)
3142 mpz_rootrem (s
, NULL
, u
, 2);
3149 mpz_fac_ui (mpz_t x
, unsigned long n
)
3157 mpz_mul_ui (x
, x
, n
);
3161 mpz_bin_uiui (mpz_t r
, unsigned long n
, unsigned long k
)
3172 mpz_divexact (r
, r
, t
);
3173 mpz_fac_ui (t
, n
- k
);
3174 mpz_divexact (r
, r
, t
);
3179 /* Logical operations and bit manipulation. */
3181 /* Numbers are treated as if represented in two's complement (and
3182 infinitely sign extended). For a negative values we get the two's
3183 complement from -x = ~x + 1, where ~ is bitwise complementt.
3192 where yyyy is the bitwise complement of xxxx. So least significant
3193 bits, up to and including the first one bit, are unchanged, and
3194 the more significant bits are all complemented.
3196 To change a bit from zero to one in a negative number, subtract the
3197 corresponding power of two from the absolute value. This can never
3198 underflow. To change a bit from one to zero, add the corresponding
3199 power of two, and this might overflow. E.g., if x = -001111, the
3200 two's complement is 110001. Clearing the least significant bit, we
3201 get two's complement 110000, and -010000. */
3204 mpz_tstbit (const mpz_t d
, mp_bitcnt_t bit_index
)
3206 mp_size_t limb_index
;
3215 limb_index
= bit_index
/ GMP_LIMB_BITS
;
3216 if (limb_index
>= dn
)
3219 shift
= bit_index
% GMP_LIMB_BITS
;
3220 w
= d
->_mp_d
[limb_index
];
3221 bit
= (w
>> shift
) & 1;
3225 /* d < 0. Check if any of the bits below is set: If so, our bit
3226 must be complemented. */
3227 if (shift
> 0 && (w
<< (GMP_LIMB_BITS
- shift
)) > 0)
3229 while (limb_index
-- > 0)
3230 if (d
->_mp_d
[limb_index
] > 0)
3237 mpz_abs_add_bit (mpz_t d
, mp_bitcnt_t bit_index
)
3239 mp_size_t dn
, limb_index
;
3243 dn
= GMP_ABS (d
->_mp_size
);
3245 limb_index
= bit_index
/ GMP_LIMB_BITS
;
3246 bit
= (mp_limb_t
) 1 << (bit_index
% GMP_LIMB_BITS
);
3248 if (limb_index
>= dn
)
3251 /* The bit should be set outside of the end of the number.
3252 We have to increase the size of the number. */
3253 dp
= MPZ_REALLOC (d
, limb_index
+ 1);
3255 dp
[limb_index
] = bit
;
3256 for (i
= dn
; i
< limb_index
; i
++)
3258 dn
= limb_index
+ 1;
3266 cy
= mpn_add_1 (dp
+ limb_index
, dp
+ limb_index
, dn
- limb_index
, bit
);
3269 dp
= MPZ_REALLOC (d
, dn
+ 1);
3274 d
->_mp_size
= (d
->_mp_size
< 0) ? - dn
: dn
;
3278 mpz_abs_sub_bit (mpz_t d
, mp_bitcnt_t bit_index
)
3280 mp_size_t dn
, limb_index
;
3284 dn
= GMP_ABS (d
->_mp_size
);
3287 limb_index
= bit_index
/ GMP_LIMB_BITS
;
3288 bit
= (mp_limb_t
) 1 << (bit_index
% GMP_LIMB_BITS
);
3290 assert (limb_index
< dn
);
3292 gmp_assert_nocarry (mpn_sub_1 (dp
+ limb_index
, dp
+ limb_index
,
3293 dn
- limb_index
, bit
));
3294 dn
-= (dp
[dn
-1] == 0);
3295 d
->_mp_size
= (d
->_mp_size
< 0) ? - dn
: dn
;
3299 mpz_setbit (mpz_t d
, mp_bitcnt_t bit_index
)
3301 if (!mpz_tstbit (d
, bit_index
))
3303 if (d
->_mp_size
>= 0)
3304 mpz_abs_add_bit (d
, bit_index
);
3306 mpz_abs_sub_bit (d
, bit_index
);
3311 mpz_clrbit (mpz_t d
, mp_bitcnt_t bit_index
)
3313 if (mpz_tstbit (d
, bit_index
))
3315 if (d
->_mp_size
>= 0)
3316 mpz_abs_sub_bit (d
, bit_index
);
3318 mpz_abs_add_bit (d
, bit_index
);
3323 mpz_combit (mpz_t d
, mp_bitcnt_t bit_index
)
3325 if (mpz_tstbit (d
, bit_index
) ^ (d
->_mp_size
< 0))
3326 mpz_abs_sub_bit (d
, bit_index
);
3328 mpz_abs_add_bit (d
, bit_index
);
3332 mpz_com (mpz_t r
, const mpz_t u
)
3335 mpz_sub_ui (r
, r
, 1);
3339 mpz_and (mpz_t r
, const mpz_t u
, const mpz_t v
)
3341 mp_size_t un
, vn
, rn
, i
;
3344 mp_limb_t ux
, vx
, rx
;
3345 mp_limb_t uc
, vc
, rc
;
3346 mp_limb_t ul
, vl
, rl
;
3348 un
= GMP_ABS (u
->_mp_size
);
3349 vn
= GMP_ABS (v
->_mp_size
);
3352 MPZ_SRCPTR_SWAP (u
, v
);
3353 MP_SIZE_T_SWAP (un
, vn
);
3361 uc
= u
->_mp_size
< 0;
3362 vc
= v
->_mp_size
< 0;
3369 /* If the smaller input is positive, higher limbs don't matter. */
3372 rp
= MPZ_REALLOC (r
, rn
+ rc
);
3377 for (i
= 0; i
< vn
; i
++)
3379 ul
= (up
[i
] ^ ux
) + uc
;
3382 vl
= (vp
[i
] ^ vx
) + vc
;
3385 rl
= ( (ul
& vl
) ^ rx
) + rc
;
3393 ul
= (up
[i
] ^ ux
) + uc
;
3396 rl
= ( (ul
& vx
) ^ rx
) + rc
;
3403 rn
= mpn_normalized_size (rp
, rn
);
3405 r
->_mp_size
= rx
? -rn
: rn
;
3409 mpz_ior (mpz_t r
, const mpz_t u
, const mpz_t v
)
3411 mp_size_t un
, vn
, rn
, i
;
3414 mp_limb_t ux
, vx
, rx
;
3415 mp_limb_t uc
, vc
, rc
;
3416 mp_limb_t ul
, vl
, rl
;
3418 un
= GMP_ABS (u
->_mp_size
);
3419 vn
= GMP_ABS (v
->_mp_size
);
3422 MPZ_SRCPTR_SWAP (u
, v
);
3423 MP_SIZE_T_SWAP (un
, vn
);
3431 uc
= u
->_mp_size
< 0;
3432 vc
= v
->_mp_size
< 0;
3439 /* If the smaller input is negative, by sign extension higher limbs
3443 rp
= MPZ_REALLOC (r
, rn
+ rc
);
3448 for (i
= 0; i
< vn
; i
++)
3450 ul
= (up
[i
] ^ ux
) + uc
;
3453 vl
= (vp
[i
] ^ vx
) + vc
;
3456 rl
= ( (ul
| vl
) ^ rx
) + rc
;
3464 ul
= (up
[i
] ^ ux
) + uc
;
3467 rl
= ( (ul
| vx
) ^ rx
) + rc
;
3474 rn
= mpn_normalized_size (rp
, rn
);
3476 r
->_mp_size
= rx
? -rn
: rn
;
3480 mpz_xor (mpz_t r
, const mpz_t u
, const mpz_t v
)
3482 mp_size_t un
, vn
, i
;
3485 mp_limb_t ux
, vx
, rx
;
3486 mp_limb_t uc
, vc
, rc
;
3487 mp_limb_t ul
, vl
, rl
;
3489 un
= GMP_ABS (u
->_mp_size
);
3490 vn
= GMP_ABS (v
->_mp_size
);
3493 MPZ_SRCPTR_SWAP (u
, v
);
3494 MP_SIZE_T_SWAP (un
, vn
);
3502 uc
= u
->_mp_size
< 0;
3503 vc
= v
->_mp_size
< 0;
3510 rp
= MPZ_REALLOC (r
, un
+ rc
);
3515 for (i
= 0; i
< vn
; i
++)
3517 ul
= (up
[i
] ^ ux
) + uc
;
3520 vl
= (vp
[i
] ^ vx
) + vc
;
3523 rl
= (ul
^ vl
^ rx
) + rc
;
3531 ul
= (up
[i
] ^ ux
) + uc
;
3534 rl
= (ul
^ ux
) + rc
;
3541 un
= mpn_normalized_size (rp
, un
);
3543 r
->_mp_size
= rx
? -un
: un
;
3547 gmp_popcount_limb (mp_limb_t x
)
3551 /* Do 16 bits at a time, to avoid limb-sized constants. */
3552 for (c
= 0; x
> 0; x
>>= 16)
3554 unsigned w
= ((x
>> 1) & 0x5555) + (x
& 0x5555);
3555 w
= ((w
>> 2) & 0x3333) + (w
& 0x3333);
3556 w
= ((w
>> 4) & 0x0f0f) + (w
& 0x0f0f);
3557 w
= (w
>> 8) + (w
& 0x00ff);
3564 mpz_popcount (const mpz_t u
)
3572 return ~(mp_bitcnt_t
) 0;
3574 for (c
= 0, i
= 0; i
< un
; i
++)
3575 c
+= gmp_popcount_limb (u
->_mp_d
[i
]);
3581 mpz_hamdist (const mpz_t u
, const mpz_t v
)
3583 mp_size_t un
, vn
, i
;
3584 mp_limb_t uc
, vc
, ul
, vl
, comp
;
3592 return ~(mp_bitcnt_t
) 0;
3600 comp
= - (mp_limb_t
) 1;
3609 MPN_SRCPTR_SWAP (up
, un
, vp
, vn
);
3611 for (i
= 0, c
= 0; i
< vn
; i
++)
3613 ul
= (up
[i
] ^ comp
) + uc
;
3616 vl
= (vp
[i
] ^ comp
) + vc
;
3619 c
+= gmp_popcount_limb (ul
^ vl
);
3625 ul
= (up
[i
] ^ comp
) + uc
;
3628 c
+= gmp_popcount_limb (ul
^ comp
);
3635 mpz_scan1 (const mpz_t u
, mp_bitcnt_t starting_bit
)
3638 mp_size_t us
, un
, i
;
3639 mp_limb_t limb
, ux
, uc
;
3645 i
= starting_bit
/ GMP_LIMB_BITS
;
3647 /* Past the end there's no 1 bits for u>=0, or an immediate 1 bit
3648 for u<0. Notice this test picks up any u==0 too. */
3650 return (us
>= 0 ? ~(mp_bitcnt_t
) 0 : starting_bit
);
3655 uc
= mpn_zero_p (up
, i
);
3660 limb
= (ux
^ up
[i
]) + uc
;
3663 /* Mask to 0 all bits before starting_bit, thus ignoring them. */
3664 limb
&= (GMP_LIMB_MAX
<< (starting_bit
% GMP_LIMB_BITS
));
3672 /* For the u > 0 case, this can happen only for the first
3673 masked limb. For the u < 0 case, it happens when the
3674 highest limbs of the absolute value are all ones. */
3675 return (us
>= 0 ? ~(mp_bitcnt_t
) 0 : un
* GMP_LIMB_BITS
);
3677 limb
= (ux
^ up
[i
]) + uc
;
3680 gmp_ctz (cnt
, limb
);
3681 return (mp_bitcnt_t
) i
* GMP_LIMB_BITS
+ cnt
;
3685 mpz_scan0 (const mpz_t u
, mp_bitcnt_t starting_bit
)
3688 mp_size_t us
, un
, i
;
3689 mp_limb_t limb
, ux
, uc
;
3695 i
= starting_bit
/ GMP_LIMB_BITS
;
3697 /* When past end, there's an immediate 0 bit for u>=0, or no 0 bits for
3698 u<0. Notice this test picks up all cases of u==0 too. */
3700 return (us
>= 0 ? starting_bit
: ~(mp_bitcnt_t
) 0);
3705 uc
= mpn_zero_p (up
, i
);
3710 limb
= (ux
^ up
[i
]) + uc
;
3713 /* Mask to 1 all bits before starting_bit, thus ignoring them. */
3714 limb
|= ((mp_limb_t
) 1 << (starting_bit
% GMP_LIMB_BITS
)) - 1;
3716 while (limb
== GMP_LIMB_MAX
)
3722 return (us
>= 0 ? un
* GMP_LIMB_BITS
: ~(mp_bitcnt_t
) 0);
3724 limb
= (ux
^ up
[i
]) + uc
;
3727 gmp_ctz (cnt
, ~limb
);
3728 return (mp_bitcnt_t
) i
* GMP_LIMB_BITS
+ cnt
;
3732 /* MPZ base conversion. */
3735 mpz_sizeinbase (const mpz_t u
, int base
)
3741 struct gmp_div_inverse bi
;
3745 assert (base
<= 36);
3747 un
= GMP_ABS (u
->_mp_size
);
3753 bits
= (un
- 1) * GMP_LIMB_BITS
+ mpn_limb_size_in_base_2 (up
[un
-1]);
3759 return (bits
+ 1) / 2;
3761 return (bits
+ 2) / 3;
3763 return (bits
+ 3) / 4;
3765 return (bits
+ 4) / 5;
3766 /* FIXME: Do something more clever for the common case of base
3770 tp
= gmp_xalloc_limbs (un
);
3771 mpn_copyi (tp
, up
, un
);
3772 mpn_div_qr_1_invert (&bi
, base
);
3774 for (ndigits
= 0; un
> 0; ndigits
++)
3776 mpn_div_qr_1_preinv (tp
, tp
, un
, &bi
);
3777 un
-= (tp
[un
-1] == 0);
3784 mpz_get_str (char *sp
, int base
, const mpz_t u
)
3793 digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
3798 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3805 sn
= 1 + mpz_sizeinbase (u
, base
);
3807 sp
= gmp_xalloc (1 + sn
);
3809 un
= GMP_ABS (u
->_mp_size
);
3820 if (u
->_mp_size
< 0)
3823 bits
= mpn_base_power_of_two_p (base
);
3826 /* Not modified in this case. */
3827 sn
= i
+ mpn_get_str_bits ((unsigned char *) sp
+ i
, bits
, u
->_mp_d
, un
);
3830 struct mpn_base_info info
;
3833 mpn_get_base_info (&info
, base
);
3834 tp
= gmp_xalloc_limbs (un
);
3835 mpn_copyi (tp
, u
->_mp_d
, un
);
3837 sn
= i
+ mpn_get_str_other ((unsigned char *) sp
+ i
, base
, &info
, tp
, un
);
3842 sp
[i
] = digits
[(unsigned char) sp
[i
]];
3849 mpz_set_str (mpz_t r
, const char *sp
, int base
)
3852 mp_size_t rn
, alloc
;
3859 assert (base
== 0 || (base
>= 2 && base
<= 36));
3861 while (isspace( (unsigned char) *sp
))
3877 if (*sp
== 'x' || *sp
== 'X')
3882 else if (*sp
== 'b' || *sp
== 'B')
3895 dp
= gmp_xalloc (sn
+ (sn
== 0));
3897 for (dn
= 0; *sp
; sp
++)
3901 if (isspace ((unsigned char) *sp
))
3903 if (*sp
>= '0' && *sp
<= '9')
3905 else if (*sp
>= 'a' && *sp
<= 'z')
3906 digit
= *sp
- 'a' + 10;
3907 else if (*sp
>= 'A' && *sp
<= 'Z')
3908 digit
= *sp
- 'A' + 10;
3910 digit
= base
; /* fail */
3922 bits
= mpn_base_power_of_two_p (base
);
3926 alloc
= (sn
* bits
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
3927 rp
= MPZ_REALLOC (r
, alloc
);
3928 rn
= mpn_set_str_bits (rp
, dp
, dn
, bits
);
3932 struct mpn_base_info info
;
3933 mpn_get_base_info (&info
, base
);
3934 alloc
= (sn
+ info
.exp
- 1) / info
.exp
;
3935 rp
= MPZ_REALLOC (r
, alloc
);
3936 rn
= mpn_set_str_other (rp
, dp
, dn
, base
, &info
);
3938 assert (rn
<= alloc
);
3941 r
->_mp_size
= sign
? - rn
: rn
;
3947 mpz_init_set_str (mpz_t r
, const char *sp
, int base
)
3950 return mpz_set_str (r
, sp
, base
);
3954 mpz_out_str (FILE *stream
, int base
, const mpz_t x
)
3959 str
= mpz_get_str (NULL
, base
, x
);
3961 len
= fwrite (str
, 1, len
, stream
);
3968 gmp_detect_endian (void)
3970 static const int i
= 1;
3971 const unsigned char *p
= (const unsigned char *) &i
;
3980 /* Import and export. Does not support nails. */
3982 mpz_import (mpz_t r
, size_t count
, int order
, size_t size
, int endian
,
3983 size_t nails
, const void *src
)
3985 const unsigned char *p
;
3986 ptrdiff_t word_step
;
3990 /* The current (partial) limb. */
3992 /* The number of bytes already copied to this limb (starting from
3995 /* The index where the limb should be stored, when completed. */
3999 gmp_die ("mpz_import: Nails not supported.");
4001 assert (order
== 1 || order
== -1);
4002 assert (endian
>= -1 && endian
<= 1);
4005 endian
= gmp_detect_endian ();
4007 p
= (unsigned char *) src
;
4009 word_step
= (order
!= endian
) ? 2 * size
: 0;
4011 /* Process bytes from the least significant end, so point p at the
4012 least significant word. */
4015 p
+= size
* (count
- 1);
4016 word_step
= - word_step
;
4019 /* And at least significant byte of that word. */
4023 rn
= (size
* count
+ sizeof(mp_limb_t
) - 1) / sizeof(mp_limb_t
);
4024 rp
= MPZ_REALLOC (r
, rn
);
4026 for (limb
= 0, bytes
= 0, i
= 0; count
> 0; count
--, p
+= word_step
)
4029 for (j
= 0; j
< size
; j
++, p
-= (ptrdiff_t) endian
)
4031 limb
|= (mp_limb_t
) *p
<< (bytes
++ * CHAR_BIT
);
4032 if (bytes
== sizeof(mp_limb_t
))
4044 r
->_mp_size
= mpn_normalized_size (rp
, i
);
4048 mpz_export (void *r
, size_t *countp
, int order
, size_t size
, int endian
,
4049 size_t nails
, const mpz_t u
)
4052 ptrdiff_t word_step
;
4056 /* The current (partial) limb. */
4058 /* The number of bytes left to to in this limb. */
4060 /* The index where the limb was read. */
4064 gmp_die ("mpz_import: Nails not supported.");
4066 assert (order
== 1 || order
== -1);
4067 assert (endian
>= -1 && endian
<= 1);
4068 assert (size
> 0 || u
->_mp_size
== 0);
4070 un
= GMP_ABS (u
->_mp_size
);
4078 /* Count bytes in top limb. */
4079 for (limb
= u
->_mp_d
[un
-1], k
= 0; limb
> 0; k
++, limb
>>= CHAR_BIT
)
4084 count
= (k
+ (un
-1) * sizeof (mp_limb_t
) + size
- 1) / size
;
4087 r
= gmp_xalloc (count
* size
);
4090 endian
= gmp_detect_endian ();
4092 p
= (unsigned char *) r
;
4094 word_step
= (order
!= endian
) ? 2 * size
: 0;
4096 /* Process bytes from the least significant end, so point p at the
4097 least significant word. */
4100 p
+= size
* (count
- 1);
4101 word_step
= - word_step
;
4104 /* And at least significant byte of that word. */
4108 for (bytes
= 0, i
= 0, k
= 0; k
< count
; k
++, p
+= word_step
)
4111 for (j
= 0; j
< size
; j
++, p
-= (ptrdiff_t) endian
)
4116 limb
= u
->_mp_d
[i
++];
4117 bytes
= sizeof (mp_limb_t
);
4125 assert (k
== count
);