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-2019 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 /* Modified by Mikulas Patocka to fit in Ajla */
35 /* NOTE: All functions in this file which are not declared in
36 mini-gmp.h are internal, and are not intended to be compatible
37 with GMP or with future versions of mini-gmp. */
39 /* Much of the material copied from GMP files, including: gmp-impl.h,
40 longlong.h, mpn/generic/add_n.c, mpn/generic/addmul_1.c,
41 mpn/generic/lshift.c, mpn/generic/mul_1.c,
42 mpn/generic/mul_basecase.c, mpn/generic/rshift.c,
43 mpn/generic/sbpi1_div_qr.c, mpn/generic/sub_n.c,
44 mpn/generic/submul_1.c. */
58 #define assert(x) ajla_assert(x, (file_line, "gmp assertion failed"))
61 #define GMP_LIMB_BITS ((int)sizeof(mp_limb_t) * CHAR_BIT)
63 #define GMP_LIMB_MAX ((mp_limb_t) ~ (mp_limb_t) 0)
64 #define GMP_LIMB_HIGHBIT ((mp_limb_t) 1 << (GMP_LIMB_BITS - 1))
66 #define GMP_HLIMB_BIT ((mp_limb_t) 1 << (GMP_LIMB_BITS / 2))
67 #define GMP_LLIMB_MASK (GMP_HLIMB_BIT - 1)
69 #define GMP_ULONG_BITS (sizeof(unsigned long) * CHAR_BIT)
70 #define GMP_ULONG_HIGHBIT ((unsigned long) 1 << (GMP_ULONG_BITS - 1))
72 #define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
73 #define GMP_NEG_CAST(T,x) (-((T)((x) + 1) - 1))
75 #define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
76 #define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
78 #define GMP_CMP(a,b) (((a) > (b)) - ((a) < (b)))
80 #if defined(DBL_MANT_DIG) && FLT_RADIX == 2
81 #define GMP_DBL_MANT_BITS DBL_MANT_DIG
83 #define GMP_DBL_MANT_BITS (53)
86 /* Return non-zero if xp,xsize and yp,ysize overlap.
87 If xp+xsize<=yp there's no overlap, or if yp+ysize<=xp there's no
88 overlap. If both these are false, there's an overlap. */
89 #define GMP_MPN_OVERLAP_P(xp, xsize, yp, ysize) \
90 ((xp) + (xsize) > (yp) && (yp) + (ysize) > (xp))
92 #define gmp_assert_nocarry(x) do { \
93 mp_limb_t attr_unused __cy = (x); \
97 #define gmp_clz(count, x) do { \
98 mp_limb_t __clz_x = (x); \
99 unsigned __clz_c = 0; \
100 int LOCAL_SHIFT_BITS = 8; \
101 if (GMP_LIMB_BITS > LOCAL_SHIFT_BITS) \
103 (__clz_x & ((mp_limb_t) 0xff << (GMP_LIMB_BITS - 8))) == 0; \
105 { __clz_x <<= LOCAL_SHIFT_BITS; } \
106 for (; (__clz_x & GMP_LIMB_HIGHBIT) == 0; __clz_c++) \
111 #define gmp_ctz(count, x) do { \
112 mp_limb_t __ctz_x = (x); \
113 unsigned __ctz_c = 0; \
114 gmp_clz (__ctz_c, __ctz_x & - __ctz_x); \
115 (count) = GMP_LIMB_BITS - 1 - __ctz_c; \
118 #define gmp_add_ssaaaa(sh, sl, ah, al, bh, bl) \
122 (sh) = (ah) + (bh) + (__x < (al)); \
126 #define gmp_sub_ddmmss(sh, sl, ah, al, bh, bl) \
130 (sh) = (ah) - (bh) - ((al) < (bl)); \
134 #define gmp_umul_ppmm(w1, w0, u, v) \
136 int LOCAL_GMP_LIMB_BITS = GMP_LIMB_BITS; \
137 if (sizeof(unsigned int) * CHAR_BIT >= 2 * GMP_LIMB_BITS) \
139 unsigned int __ww = (unsigned int) (u) * (v); \
140 w0 = (mp_limb_t) __ww; \
141 w1 = (mp_limb_t) (__ww >> LOCAL_GMP_LIMB_BITS); \
143 else if (GMP_ULONG_BITS >= 2 * GMP_LIMB_BITS) \
145 unsigned long int __ww = (unsigned long int) (u) * (v); \
146 w0 = (mp_limb_t) __ww; \
147 w1 = (mp_limb_t) (__ww >> LOCAL_GMP_LIMB_BITS); \
150 mp_limb_t __x0, __x1, __x2, __x3; \
151 unsigned __ul, __vl, __uh, __vh; \
152 mp_limb_t __u = (u), __v = (v); \
154 __ul = __u & GMP_LLIMB_MASK; \
155 __uh = __u >> (GMP_LIMB_BITS / 2); \
156 __vl = __v & GMP_LLIMB_MASK; \
157 __vh = __v >> (GMP_LIMB_BITS / 2); \
159 __x0 = (mp_limb_t) __ul * __vl; \
160 __x1 = (mp_limb_t) __ul * __vh; \
161 __x2 = (mp_limb_t) __uh * __vl; \
162 __x3 = (mp_limb_t) __uh * __vh; \
164 __x1 += __x0 >> (GMP_LIMB_BITS / 2);/* this can't give carry */ \
165 __x1 += __x2; /* but this indeed can */ \
166 if (__x1 < __x2) /* did we get it? */ \
167 __x3 += GMP_HLIMB_BIT; /* yes, add it in the proper pos. */ \
169 (w1) = __x3 + (__x1 >> (GMP_LIMB_BITS / 2)); \
170 (w0) = (__x1 << (GMP_LIMB_BITS / 2)) + (__x0 & GMP_LLIMB_MASK); \
174 #define gmp_udiv_qrnnd_preinv(q, r, nh, nl, d, di) \
176 mp_limb_t _qh, _ql, _r, _mask; \
177 gmp_umul_ppmm (_qh, _ql, (nh), (di)); \
178 gmp_add_ssaaaa (_qh, _ql, _qh, _ql, (nh) + 1, (nl)); \
179 _r = (nl) - _qh * (d); \
180 _mask = -(mp_limb_t) (_r > _ql); /* both > and >= are OK */ \
193 #define gmp_udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv) \
195 mp_limb_t _q0, _t1, _t0, _mask; \
196 gmp_umul_ppmm ((q), _q0, (n2), (dinv)); \
197 gmp_add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1)); \
199 /* Compute the two most significant limbs of n - q'd */ \
200 (r1) = (n1) - (d1) * (q); \
201 gmp_sub_ddmmss ((r1), (r0), (r1), (n0), (d1), (d0)); \
202 gmp_umul_ppmm (_t1, _t0, (d0), (q)); \
203 gmp_sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0); \
206 /* Conditionally adjust q and the remainders */ \
207 _mask = - (mp_limb_t) ((r1) >= _q0); \
209 gmp_add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0)); \
212 if ((r1) > (d1) || (r0) >= (d0)) \
215 gmp_sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0)); \
221 #define MP_LIMB_T_SWAP(x, y) \
223 mp_limb_t __mp_limb_t_swap__tmp = (x); \
225 (y) = __mp_limb_t_swap__tmp; \
227 #define MP_SIZE_T_SWAP(x, y) \
229 mp_size_t __mp_size_t_swap__tmp = (x); \
231 (y) = __mp_size_t_swap__tmp; \
233 #define MP_BITCNT_T_SWAP(x,y) \
235 mp_bitcnt_t __mp_bitcnt_t_swap__tmp = (x); \
237 (y) = __mp_bitcnt_t_swap__tmp; \
239 #define MP_PTR_SWAP(x, y) \
241 mp_ptr __mp_ptr_swap__tmp = (x); \
243 (y) = __mp_ptr_swap__tmp; \
245 #define MP_SRCPTR_SWAP(x, y) \
247 mp_srcptr __mp_srcptr_swap__tmp = (x); \
249 (y) = __mp_srcptr_swap__tmp; \
252 #define MPN_PTR_SWAP(xp,xs, yp,ys) \
254 MP_PTR_SWAP (xp, yp); \
255 MP_SIZE_T_SWAP (xs, ys); \
257 #define MPN_SRCPTR_SWAP(xp,xs, yp,ys) \
259 MP_SRCPTR_SWAP (xp, yp); \
260 MP_SIZE_T_SWAP (xs, ys); \
263 #define MPZ_PTR_SWAP(x, y) \
265 mpz_ptr __mpz_ptr_swap__tmp = (x); \
267 (y) = __mpz_ptr_swap__tmp; \
269 #define MPZ_SRCPTR_SWAP(x, y) \
271 mpz_srcptr __mpz_srcptr_swap__tmp = (x); \
273 (y) = __mpz_srcptr_swap__tmp; \
276 const int mp_bits_per_limb
= GMP_LIMB_BITS
;
279 /* Memory allocation and other helper functions. */
281 gmp_die (const char *msg
)
283 internal(file_line
, "%s", msg
);
287 gmp_default_alloc (size_t size
)
295 gmp_die("gmp_default_alloc: Virtual memory exhausted.");
301 gmp_default_realloc (void *old
, size_t attr_unused unused_old_size
, size_t new_size
)
305 p
= realloc (old
, new_size
);
308 gmp_die("gmp_default_realloc: Virtual memory exhausted.");
314 gmp_default_free (void *p
, size_t attr_unused unused_size
)
319 static void * (*gmp_allocate_func
) (size_t) = gmp_default_alloc
;
320 static void * (*gmp_reallocate_func
) (void *, size_t, size_t) = gmp_default_realloc
;
321 static void (*gmp_free_func
) (void *, size_t) = gmp_default_free
;
324 mp_set_memory_functions (void *(*alloc_func
) (size_t),
325 void *(*realloc_func
) (void *, size_t, size_t),
326 void (*free_func
) (void *, size_t))
329 alloc_func
= gmp_default_alloc
;
331 realloc_func
= gmp_default_realloc
;
333 free_func
= gmp_default_free
;
335 gmp_allocate_func
= alloc_func
;
336 gmp_reallocate_func
= realloc_func
;
337 gmp_free_func
= free_func
;
340 #define gmp_xalloc(size) ((*gmp_allocate_func)((size)))
341 #define gmp_free(p) ((*gmp_free_func) ((p), 0))
344 gmp_xalloc_limbs (mp_size_t size
)
346 return (mp_ptr
) gmp_xalloc (size
* sizeof (mp_limb_t
));
350 gmp_xrealloc_limbs (mp_ptr old
, mp_size_t size
)
353 return (mp_ptr
) (*gmp_reallocate_func
) (old
, 0, size
* sizeof (mp_limb_t
));
360 mpn_copyi (mp_ptr d
, mp_srcptr s
, mp_size_t n
)
363 for (i
= 0; i
< n
; i
++)
368 mpn_copyd (mp_ptr d
, mp_srcptr s
, mp_size_t n
)
375 mpn_cmp (mp_srcptr ap
, mp_srcptr bp
, mp_size_t n
)
380 return ap
[n
] > bp
[n
] ? 1 : -1;
386 mpn_cmp4 (mp_srcptr ap
, mp_size_t an
, mp_srcptr bp
, mp_size_t bn
)
389 return an
< bn
? -1 : 1;
391 return mpn_cmp (ap
, bp
, an
);
395 mpn_normalized_size (mp_srcptr xp
, mp_size_t n
)
397 while (n
> 0 && xp
[n
-1] == 0)
403 mpn_zero_p(mp_srcptr rp
, mp_size_t n
)
405 return mpn_normalized_size (rp
, n
) == 0;
409 mpn_zero (mp_ptr rp
, mp_size_t n
)
416 mpn_add_1 (mp_ptr rp
, mp_srcptr ap
, mp_size_t n
, mp_limb_t b
)
424 mp_limb_t r
= ap
[i
] + b
;
435 mpn_add_n (mp_ptr rp
, mp_srcptr ap
, mp_srcptr bp
, mp_size_t n
)
440 for (i
= 0, cy
= 0; i
< n
; i
++)
443 a
= ap
[i
]; b
= bp
[i
];
454 mpn_add (mp_ptr rp
, mp_srcptr ap
, mp_size_t an
, mp_srcptr bp
, mp_size_t bn
)
460 cy
= mpn_add_n (rp
, ap
, bp
, bn
);
462 cy
= mpn_add_1 (rp
+ bn
, ap
+ bn
, an
- bn
, cy
);
467 mpn_sub_1 (mp_ptr rp
, mp_srcptr ap
, mp_size_t n
, mp_limb_t b
)
478 mp_limb_t cy
= a
< b
;
488 mpn_sub_n (mp_ptr rp
, mp_srcptr ap
, mp_srcptr bp
, mp_size_t n
)
493 for (i
= 0, cy
= 0; i
< n
; i
++)
496 a
= ap
[i
]; b
= bp
[i
];
506 mpn_sub (mp_ptr rp
, mp_srcptr ap
, mp_size_t an
, mp_srcptr bp
, mp_size_t bn
)
512 cy
= mpn_sub_n (rp
, ap
, bp
, bn
);
514 cy
= mpn_sub_1 (rp
+ bn
, ap
+ bn
, an
- bn
, cy
);
519 mpn_mul_1 (mp_ptr rp
, mp_srcptr up
, mp_size_t n
, mp_limb_t vl
)
521 mp_limb_t ul
, cl
, hpl
, lpl
;
529 gmp_umul_ppmm (hpl
, lpl
, ul
, vl
);
532 cl
= (lpl
< cl
) + hpl
;
542 mpn_addmul_1 (mp_ptr rp
, mp_srcptr up
, mp_size_t n
, mp_limb_t vl
)
544 mp_limb_t ul
, cl
, hpl
, lpl
, rl
;
552 gmp_umul_ppmm (hpl
, lpl
, ul
, vl
);
555 cl
= (lpl
< cl
) + hpl
;
568 mpn_submul_1 (mp_ptr rp
, mp_srcptr up
, mp_size_t n
, mp_limb_t vl
)
570 mp_limb_t ul
, cl
, hpl
, lpl
, rl
;
578 gmp_umul_ppmm (hpl
, lpl
, ul
, vl
);
581 cl
= (lpl
< cl
) + hpl
;
594 mpn_mul (mp_ptr rp
, mp_srcptr up
, mp_size_t un
, mp_srcptr vp
, mp_size_t vn
)
598 assert (!GMP_MPN_OVERLAP_P(rp
, un
+ vn
, up
, un
));
599 assert (!GMP_MPN_OVERLAP_P(rp
, un
+ vn
, vp
, vn
));
601 /* We first multiply by the low order limb. This result can be
602 stored, not added, to rp. We also avoid a loop for zeroing this
605 rp
[un
] = mpn_mul_1 (rp
, up
, un
, vp
[0]);
607 /* Now accumulate the product of up[] and the next higher limb from
613 rp
[un
] = mpn_addmul_1 (rp
, up
, un
, vp
[0]);
620 mpn_lshift (mp_ptr rp
, mp_srcptr up
, mp_size_t n
, unsigned int cnt
)
622 mp_limb_t high_limb
, low_limb
;
628 assert (cnt
< GMP_LIMB_BITS
);
633 tnc
= GMP_LIMB_BITS
- cnt
;
635 retval
= low_limb
>> tnc
;
636 high_limb
= (low_limb
<< cnt
);
641 *--rp
= high_limb
| (low_limb
>> tnc
);
642 high_limb
= (low_limb
<< cnt
);
650 mpn_rshift (mp_ptr rp
, mp_srcptr up
, mp_size_t n
, unsigned int cnt
)
652 mp_limb_t high_limb
, low_limb
;
658 assert (cnt
< GMP_LIMB_BITS
);
660 tnc
= GMP_LIMB_BITS
- cnt
;
662 retval
= (high_limb
<< tnc
);
663 low_limb
= high_limb
>> cnt
;
668 *rp
++ = low_limb
| (high_limb
<< tnc
);
669 low_limb
= high_limb
>> cnt
;
677 mpn_common_scan (mp_limb_t limb
, mp_size_t i
, mp_srcptr up
, mp_size_t un
,
682 assert (ux
== 0 || ux
== GMP_LIMB_MAX
);
683 assert (0 <= i
&& i
<= un
);
689 return (ux
== 0 ? ~(mp_bitcnt_t
) 0 : un
* (mp_bitcnt_t
)GMP_LIMB_BITS
);
693 return (mp_bitcnt_t
) i
* GMP_LIMB_BITS
+ cnt
;
697 mpn_scan1 (mp_srcptr ptr
, mp_bitcnt_t bit
)
700 i
= bit
/ GMP_LIMB_BITS
;
702 return mpn_common_scan ( ptr
[i
] & (GMP_LIMB_MAX
<< (bit
% GMP_LIMB_BITS
)),
707 /* MPN division interface. */
709 /* The 3/2 inverse is defined as
711 m = floor( (B^3-1) / (B u1 + u0)) - B
714 mpn_invert_3by2 (mp_limb_t u1
, mp_limb_t u0
)
722 /* For notation, let b denote the half-limb base, so that B = b^2.
723 Split u1 = b uh + ul. */
724 ul
= u1
& GMP_LLIMB_MASK
;
725 uh
= u1
>> (GMP_LIMB_BITS
/ 2);
727 /* Approximation of the high half of quotient. Differs from the 2/1
728 inverse of the half limb uh, since we have already subtracted
730 qh
= (u1
^ GMP_LIMB_MAX
) / uh
;
732 /* Adjust to get a half-limb 3/2 inverse, i.e., we want
734 qh' = floor( (b^3 - 1) / u) - b = floor ((b^3 - b u - 1) / u
735 = floor( (b (~u) + b-1) / u),
739 r = b (~u) + b-1 - qh (b uh + ul)
740 = b (~u - qh uh) + b-1 - qh ul
742 Subtraction of qh ul may underflow, which implies adjustments.
743 But by normalization, 2 u >= B > qh ul, so we need to adjust by
747 r
= ((~u1
- (mp_limb_t
) qh
* uh
) << (GMP_LIMB_BITS
/ 2)) | GMP_LLIMB_MASK
;
749 p
= (mp_limb_t
) qh
* ul
;
750 /* Adjustment steps taken from udiv_qrnnd_c */
755 if (r
>= u1
) /* i.e. we didn't get carry when adding to r */
764 /* Low half of the quotient is
766 ql = floor ( (b r + b-1) / u1).
768 This is a 3/2 division (on half-limbs), for which qh is a
771 p
= (r
>> (GMP_LIMB_BITS
/ 2)) * qh
+ r
;
772 /* Unlike full-limb 3/2, we can add 1 without overflow. For this to
773 work, it is essential that ql is a full mp_limb_t. */
774 ql
= (p
>> (GMP_LIMB_BITS
/ 2)) + 1;
776 /* By the 3/2 trick, we don't need the high half limb. */
777 r
= (r
<< (GMP_LIMB_BITS
/ 2)) + GMP_LLIMB_MASK
- ql
* u1
;
779 if (r
>= (GMP_LIMB_MAX
& (p
<< (GMP_LIMB_BITS
/ 2))))
784 m
= ((mp_limb_t
) qh
<< (GMP_LIMB_BITS
/ 2)) + ql
;
792 /* Now m is the 2/1 inverse of u1. If u0 > 0, adjust it to become a
809 gmp_umul_ppmm (th
, tl
, u0
, m
);
814 m
-= ((r
> u1
) | ((r
== u1
) & (tl
> u0
)));
821 struct gmp_div_inverse
823 /* Normalization shift count. */
825 /* Normalized divisor (d0 unused for mpn_div_qr_1) */
827 /* Inverse, for 2/1 or 3/2. */
832 mpn_div_qr_1_invert (struct gmp_div_inverse
*inv
, mp_limb_t d
)
839 inv
->d1
= d
<< shift
;
840 inv
->di
= mpn_invert_limb (inv
->d1
);
844 mpn_div_qr_2_invert (struct gmp_div_inverse
*inv
,
845 mp_limb_t d1
, mp_limb_t d0
)
854 d1
= (d1
<< shift
) | (d0
>> (GMP_LIMB_BITS
- shift
));
859 inv
->di
= mpn_invert_3by2 (d1
, d0
);
863 mpn_div_qr_invert (struct gmp_div_inverse
*inv
,
864 mp_srcptr dp
, mp_size_t dn
)
869 mpn_div_qr_1_invert (inv
, dp
[0]);
871 mpn_div_qr_2_invert (inv
, dp
[1], dp
[0]);
884 d1
= (d1
<< shift
) | (d0
>> (GMP_LIMB_BITS
- shift
));
885 d0
= (d0
<< shift
) | (dp
[dn
-3] >> (GMP_LIMB_BITS
- shift
));
889 inv
->di
= mpn_invert_3by2 (d1
, d0
);
893 /* Not matching current public gmp interface, rather corresponding to
894 the sbpi1_div_* functions. */
896 mpn_div_qr_1_preinv (mp_ptr qp
, mp_srcptr np
, mp_size_t nn
,
897 const struct gmp_div_inverse
*inv
)
905 /* Shift, reusing qp area if possible. In-place shift if qp == np. */
906 tp
= qp
? qp
: gmp_xalloc_limbs (nn
);
907 r
= mpn_lshift (tp
, np
, nn
, inv
->shift
);
919 gmp_udiv_qrnnd_preinv (q
, r
, r
, np
[nn
], d
, di
);
923 if ((inv
->shift
> 0) && (tp
!= qp
))
926 return r
>> inv
->shift
;
930 mpn_div_qr_2_preinv (mp_ptr qp
, mp_ptr np
, mp_size_t nn
,
931 const struct gmp_div_inverse
*inv
)
935 mp_limb_t d1
, d0
, di
, r1
, r0
;
944 r1
= mpn_lshift (np
, np
, nn
, shift
);
955 gmp_udiv_qr_3by2 (q
, r1
, r0
, r1
, r0
, n0
, d1
, d0
, di
);
964 assert ((r0
& (GMP_LIMB_MAX
>> (GMP_LIMB_BITS
- shift
))) == 0);
965 r0
= (r0
>> shift
) | (r1
<< (GMP_LIMB_BITS
- shift
));
974 mpn_div_qr_pi1 (mp_ptr qp
,
975 mp_ptr np
, mp_size_t nn
, mp_limb_t n1
,
976 mp_srcptr dp
, mp_size_t dn
,
991 assert ((d1
& GMP_LIMB_HIGHBIT
) != 0);
992 /* Iteration variable is the index of the q limb.
994 * We divide <n1, np[dn-1+i], np[dn-2+i], np[dn-3+i],..., np[i]>
995 * by <d1, d0, dp[dn-3], ..., dp[0] >
1001 mp_limb_t n0
= np
[dn
-1+i
];
1003 if (n1
== d1
&& n0
== d0
)
1006 mpn_submul_1 (np
+i
, dp
, dn
, q
);
1007 n1
= np
[dn
-1+i
]; /* update n1, last loop's value will now be invalid */
1011 gmp_udiv_qr_3by2 (q
, n1
, n0
, n1
, n0
, np
[dn
-2+i
], d1
, d0
, dinv
);
1013 cy
= mpn_submul_1 (np
+ i
, dp
, dn
-2, q
);
1023 n1
+= d1
+ mpn_add_n (np
+ i
, np
+ i
, dp
, dn
- 1);
1037 mpn_div_qr_preinv (mp_ptr qp
, mp_ptr np
, mp_size_t nn
,
1038 mp_srcptr dp
, mp_size_t dn
,
1039 const struct gmp_div_inverse
*inv
)
1045 np
[0] = mpn_div_qr_1_preinv (qp
, np
, nn
, inv
);
1047 mpn_div_qr_2_preinv (qp
, np
, nn
, inv
);
1053 assert (inv
->d1
== dp
[dn
-1]);
1054 assert (inv
->d0
== dp
[dn
-2]);
1055 assert ((inv
->d1
& GMP_LIMB_HIGHBIT
) != 0);
1059 nh
= mpn_lshift (np
, np
, nn
, shift
);
1063 mpn_div_qr_pi1 (qp
, np
, nn
, nh
, dp
, dn
, inv
->di
);
1066 gmp_assert_nocarry (mpn_rshift (np
, np
, dn
, shift
));
1071 mpn_div_qr (mp_ptr qp
, mp_ptr np
, mp_size_t nn
, mp_srcptr dp
, mp_size_t dn
)
1073 struct gmp_div_inverse inv
;
1079 mpn_div_qr_invert (&inv
, dp
, dn
);
1080 if (dn
> 2 && inv
.shift
> 0)
1082 tp
= gmp_xalloc_limbs (dn
);
1083 gmp_assert_nocarry (mpn_lshift (tp
, dp
, dn
, inv
.shift
));
1086 mpn_div_qr_preinv (qp
, np
, nn
, dp
, dn
, &inv
);
1093 mpn_limb_size_in_base_2 (mp_limb_t u
)
1099 return GMP_LIMB_BITS
- shift
;
1107 static const mp_limb_t dummy_limb
= GMP_LIMB_MAX
& 0xc1a0;
1111 r
->_mp_d
= (mp_ptr
) &dummy_limb
;
1114 /* The utility of this function is a bit limited, since many functions
1115 assigns the result variable using mpz_swap. */
1117 mpz_init2 (mpz_t r
, mp_bitcnt_t bits
)
1121 bits
-= (bits
!= 0); /* Round down, except if 0 */
1122 rn
= 1 + bits
/ GMP_LIMB_BITS
;
1126 r
->_mp_d
= gmp_xalloc_limbs (rn
);
1133 gmp_free (r
->_mp_d
);
1137 mpz_realloc (mpz_t r
, mp_size_t size
)
1139 size
= GMP_MAX (size
, 1);
1142 r
->_mp_d
= gmp_xrealloc_limbs (r
->_mp_d
, size
);
1144 r
->_mp_d
= gmp_xalloc_limbs (size
);
1145 r
->_mp_alloc
= size
;
1147 if (GMP_ABS (r
->_mp_size
) > size
)
1153 /* Realloc for an mpz_t WHAT if it has less than NEEDED limbs. */
1154 #define MPZ_REALLOC(z,n) ((n) > (z)->_mp_alloc \
1155 ? mpz_realloc(z,n) \
1158 /* MPZ assignment and basic conversions. */
1160 mpz_set_si (mpz_t r
, signed long int x
)
1165 if (GMP_LIMB_BITS
< GMP_ULONG_BITS
)
1167 mpz_set_ui (r
, GMP_NEG_CAST (unsigned long int, x
));
1173 MPZ_REALLOC (r
, 1)[0] = GMP_NEG_CAST (unsigned long int, x
);
1178 mpz_set_ui (mpz_t r
, unsigned long int x
)
1183 MPZ_REALLOC (r
, 1)[0] = x
;
1184 if (GMP_LIMB_BITS
< GMP_ULONG_BITS
)
1186 int LOCAL_GMP_LIMB_BITS
= GMP_LIMB_BITS
;
1187 while (x
>>= LOCAL_GMP_LIMB_BITS
)
1190 MPZ_REALLOC (r
, r
->_mp_size
)[r
->_mp_size
- 1] = x
;
1199 mpz_set (mpz_t r
, const mpz_t x
)
1201 /* Allow the NOP r == x */
1207 n
= GMP_ABS (x
->_mp_size
);
1208 rp
= MPZ_REALLOC (r
, n
);
1210 mpn_copyi (rp
, x
->_mp_d
, n
);
1211 r
->_mp_size
= x
->_mp_size
;
1216 mpz_init_set_si (mpz_t r
, signed long int x
)
1223 mpz_init_set_ui (mpz_t r
, unsigned long int x
)
1230 mpz_init_set (mpz_t r
, const mpz_t x
)
1237 mpz_cmp_ui (const mpz_t u
, unsigned long v
);
1239 mpz_cmpabs_ui (const mpz_t u
, unsigned long v
);
1242 mpz_fits_slong_p (const mpz_t u
)
1244 return (LONG_MAX
+ LONG_MIN
== 0 || mpz_cmp_ui (u
, LONG_MAX
) <= 0) &&
1245 mpz_cmpabs_ui (u
, GMP_NEG_CAST (unsigned long int, LONG_MIN
)) <= 0;
1249 mpn_absfits_ulong_p (mp_srcptr up
, mp_size_t un
)
1251 int ulongsize
= GMP_ULONG_BITS
/ GMP_LIMB_BITS
;
1252 mp_limb_t ulongrem
= 0;
1254 if (GMP_ULONG_BITS
% GMP_LIMB_BITS
!= 0)
1255 ulongrem
= (mp_limb_t
) (ULONG_MAX
>> GMP_LIMB_BITS
* ulongsize
) + 1;
1257 return un
<= ulongsize
|| (up
[ulongsize
] < ulongrem
&& un
== ulongsize
+ 1);
1261 mpz_fits_ulong_p (const mpz_t u
)
1263 mp_size_t us
= u
->_mp_size
;
1265 return us
>= 0 && mpn_absfits_ulong_p (u
->_mp_d
, us
);
1269 mpz_get_si (const mpz_t u
)
1271 unsigned long r
= mpz_get_ui (u
);
1272 unsigned long c
= -LONG_MAX
- LONG_MIN
;
1274 if (u
->_mp_size
< 0)
1275 /* This expression is necessary to properly handle -LONG_MIN */
1276 return -(long) c
- (long) ((r
- c
) & LONG_MAX
);
1278 return (long) (r
& LONG_MAX
);
1282 mpz_get_ui (const mpz_t u
)
1284 if (GMP_LIMB_BITS
< GMP_ULONG_BITS
)
1286 int LOCAL_GMP_LIMB_BITS
= GMP_LIMB_BITS
;
1287 unsigned long r
= 0;
1288 mp_size_t n
= GMP_ABS (u
->_mp_size
);
1289 n
= GMP_MIN (n
, 1 + (mp_size_t
) (GMP_ULONG_BITS
- 1) / GMP_LIMB_BITS
);
1291 r
= (r
<< LOCAL_GMP_LIMB_BITS
) + u
->_mp_d
[n
];
1295 return u
->_mp_size
== 0 ? 0 : u
->_mp_d
[0];
1299 mpz_size (const mpz_t u
)
1301 return GMP_ABS (u
->_mp_size
);
1305 /* MPZ comparisons and the like. */
1307 mpz_sgn (const mpz_t u
)
1309 return GMP_CMP (u
->_mp_size
, 0);
1313 mpz_cmp_ui (const mpz_t u
, unsigned long v
)
1315 mp_size_t usize
= u
->_mp_size
;
1320 return mpz_cmpabs_ui (u
, v
);
1324 mpz_cmp (const mpz_t a
, const mpz_t b
)
1326 mp_size_t asize
= a
->_mp_size
;
1327 mp_size_t bsize
= b
->_mp_size
;
1330 return (asize
< bsize
) ? -1 : 1;
1331 else if (asize
>= 0)
1332 return mpn_cmp (a
->_mp_d
, b
->_mp_d
, asize
);
1334 return mpn_cmp (b
->_mp_d
, a
->_mp_d
, -asize
);
1338 mpz_cmpabs_ui (const mpz_t u
, unsigned long v
)
1340 mp_size_t un
= GMP_ABS (u
->_mp_size
);
1342 if (! mpn_absfits_ulong_p (u
->_mp_d
, un
))
1346 unsigned long uu
= mpz_get_ui (u
);
1347 return GMP_CMP(uu
, v
);
1352 mpz_neg (mpz_t r
, const mpz_t u
)
1355 r
->_mp_size
= -r
->_mp_size
;
1359 mpz_swap (mpz_t u
, mpz_t v
)
1361 MP_SIZE_T_SWAP (u
->_mp_size
, v
->_mp_size
);
1362 MP_SIZE_T_SWAP (u
->_mp_alloc
, v
->_mp_alloc
);
1363 MP_PTR_SWAP (u
->_mp_d
, v
->_mp_d
);
1367 /* MPZ addition and subtraction */
1371 mpz_add_ui (mpz_t r
, const mpz_t a
, unsigned long b
)
1374 mpz_init_set_ui (bb
, b
);
1380 mpz_ui_sub (mpz_t r
, unsigned long a
, const mpz_t b
);
1383 mpz_sub_ui (mpz_t r
, const mpz_t a
, unsigned long b
)
1385 mpz_ui_sub (r
, b
, a
);
1390 mpz_ui_sub (mpz_t r
, unsigned long a
, const mpz_t b
)
1393 mpz_add_ui (r
, r
, a
);
1397 mpz_abs_add (mpz_t r
, const mpz_t a
, const mpz_t b
)
1399 mp_size_t an
= GMP_ABS (a
->_mp_size
);
1400 mp_size_t bn
= GMP_ABS (b
->_mp_size
);
1406 MPZ_SRCPTR_SWAP (a
, b
);
1407 MP_SIZE_T_SWAP (an
, bn
);
1410 rp
= MPZ_REALLOC (r
, an
+ 1);
1411 cy
= mpn_add (rp
, a
->_mp_d
, an
, b
->_mp_d
, bn
);
1419 mpz_abs_sub (mpz_t r
, const mpz_t a
, const mpz_t b
)
1421 mp_size_t an
= GMP_ABS (a
->_mp_size
);
1422 mp_size_t bn
= GMP_ABS (b
->_mp_size
);
1426 cmp
= mpn_cmp4 (a
->_mp_d
, an
, b
->_mp_d
, bn
);
1429 rp
= MPZ_REALLOC (r
, an
);
1430 gmp_assert_nocarry (mpn_sub (rp
, a
->_mp_d
, an
, b
->_mp_d
, bn
));
1431 return mpn_normalized_size (rp
, an
);
1435 rp
= MPZ_REALLOC (r
, bn
);
1436 gmp_assert_nocarry (mpn_sub (rp
, b
->_mp_d
, bn
, a
->_mp_d
, an
));
1437 return -mpn_normalized_size (rp
, bn
);
1444 mpz_add (mpz_t r
, const mpz_t a
, const mpz_t b
)
1448 if ( (a
->_mp_size
^ b
->_mp_size
) >= 0)
1449 rn
= mpz_abs_add (r
, a
, b
);
1451 rn
= mpz_abs_sub (r
, a
, b
);
1453 r
->_mp_size
= a
->_mp_size
>= 0 ? rn
: - rn
;
1457 mpz_sub (mpz_t r
, const mpz_t a
, const mpz_t b
)
1461 if ( (a
->_mp_size
^ b
->_mp_size
) >= 0)
1462 rn
= mpz_abs_sub (r
, a
, b
);
1464 rn
= mpz_abs_add (r
, a
, b
);
1466 r
->_mp_size
= a
->_mp_size
>= 0 ? rn
: - rn
;
1471 mpz_mul (mpz_t r
, const mpz_t u
, const mpz_t v
)
1474 mp_size_t un
, vn
, rn
;
1481 if (un
== 0 || vn
== 0)
1487 sign
= (un
^ vn
) < 0;
1492 mpz_init2 (t
, (un
+ vn
) * GMP_LIMB_BITS
);
1496 mpn_mul (tp
, u
->_mp_d
, un
, v
->_mp_d
, vn
);
1498 mpn_mul (tp
, v
->_mp_d
, vn
, u
->_mp_d
, un
);
1501 rn
-= tp
[rn
-1] == 0;
1503 t
->_mp_size
= sign
? - rn
: rn
;
1509 mpz_mul_2exp (mpz_t r
, const mpz_t u
, mp_bitcnt_t bits
)
1516 un
= GMP_ABS (u
->_mp_size
);
1523 limbs
= bits
/ GMP_LIMB_BITS
;
1524 shift
= bits
% GMP_LIMB_BITS
;
1526 rn
= un
+ limbs
+ (shift
> 0);
1527 rp
= MPZ_REALLOC (r
, rn
);
1530 mp_limb_t cy
= mpn_lshift (rp
+ limbs
, u
->_mp_d
, un
, shift
);
1535 mpn_copyd (rp
+ limbs
, u
->_mp_d
, un
);
1537 mpn_zero (rp
, limbs
);
1539 r
->_mp_size
= (u
->_mp_size
< 0) ? - rn
: rn
;
1544 enum mpz_div_round_mode
{ GMP_DIV_FLOOR
, GMP_DIV_CEIL
, GMP_DIV_TRUNC
};
1546 /* Allows q or r to be zero. Returns 1 iff remainder is non-zero. */
1548 mpz_div_qr (mpz_t q
, mpz_t r
,
1549 const mpz_t n
, const mpz_t d
, enum mpz_div_round_mode mode
)
1551 mp_size_t ns
, ds
, nn
, dn
, qs
;
1556 gmp_die("mpz_div_qr: Divide by zero.");
1574 if (mode
== GMP_DIV_CEIL
&& qs
>= 0)
1576 /* q = 1, r = n - d */
1582 else if (mode
== GMP_DIV_FLOOR
&& qs
< 0)
1584 /* q = -1, r = n + d */
1606 mpz_init_set (tr
, n
);
1613 mpz_init2 (tq
, qn
* GMP_LIMB_BITS
);
1619 mpn_div_qr (qp
, np
, nn
, d
->_mp_d
, dn
);
1623 qn
-= (qp
[qn
-1] == 0);
1625 tq
->_mp_size
= qs
< 0 ? -qn
: qn
;
1627 rn
= mpn_normalized_size (np
, dn
);
1628 tr
->_mp_size
= ns
< 0 ? - rn
: rn
;
1630 if (mode
== GMP_DIV_FLOOR
&& qs
< 0 && rn
!= 0)
1633 mpz_sub_ui (tq
, tq
, 1);
1635 mpz_add (tr
, tr
, d
);
1637 else if (mode
== GMP_DIV_CEIL
&& qs
>= 0 && rn
!= 0)
1640 mpz_add_ui (tq
, tq
, 1);
1642 mpz_sub (tr
, tr
, d
);
1660 mpz_tdiv_q (mpz_t q
, const mpz_t n
, const mpz_t d
)
1662 mpz_div_qr (q
, NULL
, n
, d
, GMP_DIV_TRUNC
);
1666 mpz_tdiv_r (mpz_t r
, const mpz_t n
, const mpz_t d
)
1668 mpz_div_qr (NULL
, r
, n
, d
, GMP_DIV_TRUNC
);
1672 mpz_div_q_2exp (mpz_t q
, const mpz_t u
, mp_bitcnt_t bit_index
,
1673 enum mpz_div_round_mode mode
)
1686 limb_cnt
= bit_index
/ GMP_LIMB_BITS
;
1687 qn
= GMP_ABS (un
) - limb_cnt
;
1688 bit_index
%= GMP_LIMB_BITS
;
1690 if (mode
== ((un
> 0) ? GMP_DIV_CEIL
: GMP_DIV_FLOOR
)) /* un != 0 here. */
1691 /* Note: Below, the final indexing at limb_cnt is valid because at
1692 that point we have qn > 0. */
1694 || !mpn_zero_p (u
->_mp_d
, limb_cnt
)
1695 || (u
->_mp_d
[limb_cnt
]
1696 & (((mp_limb_t
) 1 << bit_index
) - 1)));
1704 qp
= MPZ_REALLOC (q
, qn
);
1708 mpn_rshift (qp
, u
->_mp_d
+ limb_cnt
, qn
, bit_index
);
1709 qn
-= qp
[qn
- 1] == 0;
1713 mpn_copyi (qp
, u
->_mp_d
+ limb_cnt
, qn
);
1720 mpz_add_ui (q
, q
, 1);
1726 mpz_fdiv_q_2exp (mpz_t r
, const mpz_t u
, mp_bitcnt_t cnt
)
1728 mpz_div_q_2exp (r
, u
, cnt
, GMP_DIV_FLOOR
);
1732 mpz_tdiv_q_2exp (mpz_t r
, const mpz_t u
, mp_bitcnt_t cnt
)
1734 mpz_div_q_2exp (r
, u
, cnt
, GMP_DIV_TRUNC
);
1738 /* Logical operations and bit manipulation. */
1740 /* Numbers are treated as if represented in two's complement (and
1741 infinitely sign extended). For a negative values we get the two's
1742 complement from -x = ~x + 1, where ~ is bitwise complement.
1751 where yyyy is the bitwise complement of xxxx. So least significant
1752 bits, up to and including the first one bit, are unchanged, and
1753 the more significant bits are all complemented.
1755 To change a bit from zero to one in a negative number, subtract the
1756 corresponding power of two from the absolute value. This can never
1757 underflow. To change a bit from one to zero, add the corresponding
1758 power of two, and this might overflow. E.g., if x = -001111, the
1759 two's complement is 110001. Clearing the least significant bit, we
1760 get two's complement 110000, and -010000. */
1763 mpz_tstbit (const mpz_t d
, mp_bitcnt_t bit_index
)
1765 mp_size_t limb_index
;
1774 limb_index
= bit_index
/ GMP_LIMB_BITS
;
1775 if (limb_index
>= dn
)
1778 shift
= bit_index
% GMP_LIMB_BITS
;
1779 w
= d
->_mp_d
[limb_index
];
1780 bit
= (w
>> shift
) & 1;
1784 /* d < 0. Check if any of the bits below is set: If so, our bit
1785 must be complemented. */
1786 if (shift
> 0 && (mp_limb_t
) (w
<< (GMP_LIMB_BITS
- shift
)) > 0)
1788 while (--limb_index
>= 0)
1789 if (d
->_mp_d
[limb_index
] > 0)
1796 mpz_abs_add_bit (mpz_t d
, mp_bitcnt_t bit_index
)
1798 mp_size_t dn
, limb_index
;
1802 dn
= GMP_ABS (d
->_mp_size
);
1804 limb_index
= bit_index
/ GMP_LIMB_BITS
;
1805 bit
= (mp_limb_t
) 1 << (bit_index
% GMP_LIMB_BITS
);
1807 if (limb_index
>= dn
)
1810 /* The bit should be set outside of the end of the number.
1811 We have to increase the size of the number. */
1812 dp
= MPZ_REALLOC (d
, limb_index
+ 1);
1814 dp
[limb_index
] = bit
;
1815 for (i
= dn
; i
< limb_index
; i
++)
1817 dn
= limb_index
+ 1;
1825 cy
= mpn_add_1 (dp
+ limb_index
, dp
+ limb_index
, dn
- limb_index
, bit
);
1828 dp
= MPZ_REALLOC (d
, dn
+ 1);
1833 d
->_mp_size
= (d
->_mp_size
< 0) ? - dn
: dn
;
1837 mpz_abs_sub_bit (mpz_t d
, mp_bitcnt_t bit_index
)
1839 mp_size_t dn
, limb_index
;
1843 dn
= GMP_ABS (d
->_mp_size
);
1846 limb_index
= bit_index
/ GMP_LIMB_BITS
;
1847 bit
= (mp_limb_t
) 1 << (bit_index
% GMP_LIMB_BITS
);
1849 assert (limb_index
< dn
);
1851 gmp_assert_nocarry (mpn_sub_1 (dp
+ limb_index
, dp
+ limb_index
,
1852 dn
- limb_index
, bit
));
1853 dn
= mpn_normalized_size (dp
, dn
);
1854 d
->_mp_size
= (d
->_mp_size
< 0) ? - dn
: dn
;
1858 mpz_setbit (mpz_t d
, mp_bitcnt_t bit_index
)
1860 if (!mpz_tstbit (d
, bit_index
))
1862 if (d
->_mp_size
>= 0)
1863 mpz_abs_add_bit (d
, bit_index
);
1865 mpz_abs_sub_bit (d
, bit_index
);
1870 mpz_clrbit (mpz_t d
, mp_bitcnt_t bit_index
)
1872 if (mpz_tstbit (d
, bit_index
))
1874 if (d
->_mp_size
>= 0)
1875 mpz_abs_sub_bit (d
, bit_index
);
1877 mpz_abs_add_bit (d
, bit_index
);
1882 mpz_com (mpz_t r
, const mpz_t u
)
1884 mpz_add_ui (r
, u
, 1);
1889 mpz_and (mpz_t r
, const mpz_t u
, const mpz_t v
)
1891 mp_size_t un
, vn
, rn
, i
;
1894 mp_limb_t ux
, vx
, rx
;
1895 mp_limb_t uc
, vc
, rc
;
1896 mp_limb_t ul
, vl
, rl
;
1898 un
= GMP_ABS (u
->_mp_size
);
1899 vn
= GMP_ABS (v
->_mp_size
);
1902 MPZ_SRCPTR_SWAP (u
, v
);
1903 MP_SIZE_T_SWAP (un
, vn
);
1911 uc
= u
->_mp_size
< 0;
1912 vc
= v
->_mp_size
< 0;
1919 /* If the smaller input is positive, higher limbs don't matter. */
1922 rp
= MPZ_REALLOC (r
, rn
+ (mp_size_t
) rc
);
1930 ul
= (up
[i
] ^ ux
) + uc
;
1933 vl
= (vp
[i
] ^ vx
) + vc
;
1936 rl
= ( (ul
& vl
) ^ rx
) + rc
;
1945 ul
= (up
[i
] ^ ux
) + uc
;
1948 rl
= ( (ul
& vx
) ^ rx
) + rc
;
1955 rn
= mpn_normalized_size (rp
, rn
);
1957 r
->_mp_size
= rx
? -rn
: rn
;
1961 mpz_ior (mpz_t r
, const mpz_t u
, const mpz_t v
)
1963 mp_size_t un
, vn
, rn
, i
;
1966 mp_limb_t ux
, vx
, rx
;
1967 mp_limb_t uc
, vc
, rc
;
1968 mp_limb_t ul
, vl
, rl
;
1970 un
= GMP_ABS (u
->_mp_size
);
1971 vn
= GMP_ABS (v
->_mp_size
);
1974 MPZ_SRCPTR_SWAP (u
, v
);
1975 MP_SIZE_T_SWAP (un
, vn
);
1983 uc
= u
->_mp_size
< 0;
1984 vc
= v
->_mp_size
< 0;
1991 /* If the smaller input is negative, by sign extension higher limbs
1995 rp
= MPZ_REALLOC (r
, rn
+ (mp_size_t
) rc
);
2003 ul
= (up
[i
] ^ ux
) + uc
;
2006 vl
= (vp
[i
] ^ vx
) + vc
;
2009 rl
= ( (ul
| vl
) ^ rx
) + rc
;
2018 ul
= (up
[i
] ^ ux
) + uc
;
2021 rl
= ( (ul
| vx
) ^ rx
) + rc
;
2028 rn
= mpn_normalized_size (rp
, rn
);
2030 r
->_mp_size
= rx
? -rn
: rn
;
2034 mpz_xor (mpz_t r
, const mpz_t u
, const mpz_t v
)
2036 mp_size_t un
, vn
, i
;
2039 mp_limb_t ux
, vx
, rx
;
2040 mp_limb_t uc
, vc
, rc
;
2041 mp_limb_t ul
, vl
, rl
;
2043 un
= GMP_ABS (u
->_mp_size
);
2044 vn
= GMP_ABS (v
->_mp_size
);
2047 MPZ_SRCPTR_SWAP (u
, v
);
2048 MP_SIZE_T_SWAP (un
, vn
);
2056 uc
= u
->_mp_size
< 0;
2057 vc
= v
->_mp_size
< 0;
2064 rp
= MPZ_REALLOC (r
, un
+ (mp_size_t
) rc
);
2072 ul
= (up
[i
] ^ ux
) + uc
;
2075 vl
= (vp
[i
] ^ vx
) + vc
;
2078 rl
= (ul
^ vl
^ rx
) + rc
;
2087 ul
= (up
[i
] ^ ux
) + uc
;
2090 rl
= (ul
^ ux
) + rc
;
2097 un
= mpn_normalized_size (rp
, un
);
2099 r
->_mp_size
= rx
? -un
: un
;
2103 gmp_popcount_limb (mp_limb_t x
)
2107 /* Do 16 bits at a time, to avoid limb-sized constants. */
2108 int LOCAL_SHIFT_BITS
= 16;
2111 unsigned w
= x
- ((x
>> 1) & 0x5555);
2112 w
= ((w
>> 2) & 0x3333) + (w
& 0x3333);
2114 w
= ((w
>> 8) & 0x000f) + (w
& 0x000f);
2116 if (GMP_LIMB_BITS
> LOCAL_SHIFT_BITS
)
2117 x
>>= LOCAL_SHIFT_BITS
;
2125 mpn_popcount (mp_srcptr p
, mp_size_t n
)
2130 for (c
= 0, i
= 0; i
< n
; i
++)
2131 c
+= gmp_popcount_limb (p
[i
]);
2137 mpz_popcount (const mpz_t u
)
2144 return ~(mp_bitcnt_t
) 0;
2146 return mpn_popcount (u
->_mp_d
, un
);
2151 mpz_scan1 (const mpz_t u
, mp_bitcnt_t starting_bit
)
2154 mp_size_t us
, un
, i
;
2159 i
= starting_bit
/ GMP_LIMB_BITS
;
2161 /* Past the end there's no 1 bits for u>=0, or an immediate 1 bit
2162 for u<0. Notice this test picks up any u==0 too. */
2164 return (us
>= 0 ? ~(mp_bitcnt_t
) 0 : starting_bit
);
2170 if (starting_bit
!= 0)
2174 ux
= mpn_zero_p (up
, i
);
2176 ux
= - (mp_limb_t
) (limb
>= ux
);
2179 /* Mask to 0 all bits before starting_bit, thus ignoring them. */
2180 limb
&= GMP_LIMB_MAX
<< (starting_bit
% GMP_LIMB_BITS
);
2183 return mpn_common_scan (limb
, i
, up
, un
, ux
);
2187 /* MPZ base conversion. */
2190 mpz_sizeinbase (const mpz_t u
, int base
)
2196 struct gmp_div_inverse bi
;
2200 assert (base
<= 62);
2202 un
= GMP_ABS (u
->_mp_size
);
2208 bits
= (un
- 1) * GMP_LIMB_BITS
+ mpn_limb_size_in_base_2 (up
[un
-1]);
2214 return (bits
+ 1) / 2;
2216 return (bits
+ 2) / 3;
2218 return (bits
+ 3) / 4;
2220 return (bits
+ 4) / 5;
2221 /* FIXME: Do something more clever for the common case of base
2225 tp
= gmp_xalloc_limbs (un
);
2226 mpn_copyi (tp
, up
, un
);
2227 mpn_div_qr_1_invert (&bi
, base
);
2233 mpn_div_qr_1_preinv (tp
, tp
, un
, &bi
);
2234 un
-= (tp
[un
-1] == 0);
2244 gmp_detect_endian (void)
2246 static const int i
= 2;
2247 const unsigned char *p
= (const unsigned char *) &i
;
2251 /* Import and export. Does not support nails. */
2253 mpz_import (mpz_t r
, size_t count
, int order
, size_t size
, int endian
,
2254 size_t nails
, const void *src
)
2256 const unsigned char *p
;
2257 ptrdiff_t word_step
;
2261 /* The current (partial) limb. */
2263 /* The number of bytes already copied to this limb (starting from
2266 /* The index where the limb should be stored, when completed. */
2270 gmp_die ("mpz_import: Nails not supported.");
2272 assert (order
== 1 || order
== -1);
2273 assert (endian
>= -1 && endian
<= 1);
2276 endian
= gmp_detect_endian ();
2278 p
= (unsigned char *) src
;
2280 word_step
= (order
!= endian
) ? 2 * size
: 0;
2282 /* Process bytes from the least significant end, so point p at the
2283 least significant word. */
2286 p
+= size
* (count
- 1);
2287 word_step
= - word_step
;
2290 /* And at least significant byte of that word. */
2294 rn
= (size
* count
+ sizeof(mp_limb_t
) - 1) / sizeof(mp_limb_t
);
2295 rp
= MPZ_REALLOC (r
, rn
);
2297 for (limb
= 0, bytes
= 0, i
= 0; count
> 0; count
--, p
+= word_step
)
2300 for (j
= 0; j
< size
; j
++, p
-= (ptrdiff_t) endian
)
2302 limb
|= (mp_limb_t
) *p
<< (bytes
++ * CHAR_BIT
);
2303 if (bytes
== sizeof(mp_limb_t
))
2311 assert (i
+ (bytes
> 0) == rn
);
2315 i
= mpn_normalized_size (rp
, i
);
2321 mpz_export (void *r
, size_t *countp
, int order
, size_t size
, int endian
,
2322 size_t nails
, const mpz_t u
)
2328 gmp_die ("mpz_import: Nails not supported.");
2330 assert (order
== 1 || order
== -1);
2331 assert (endian
>= -1 && endian
<= 1);
2332 assert (size
> 0 || u
->_mp_size
== 0);
2340 ptrdiff_t word_step
;
2341 /* The current (partial) limb. */
2343 /* The number of bytes left to do in this limb. */
2345 /* The index where the limb was read. */
2350 /* Count bytes in top limb. */
2351 limb
= u
->_mp_d
[un
-1];
2354 k
= (GMP_LIMB_BITS
<= CHAR_BIT
);
2358 int LOCAL_CHAR_BIT
= CHAR_BIT
;
2359 k
++; limb
>>= LOCAL_CHAR_BIT
;
2360 } while (limb
!= 0);
2362 /* else limb = 0; */
2364 count
= (k
+ (un
-1) * sizeof (mp_limb_t
) + size
- 1) / size
;
2367 r
= gmp_xalloc (count
* size
);
2370 endian
= gmp_detect_endian ();
2372 p
= (unsigned char *) r
;
2374 word_step
= (order
!= endian
) ? 2 * size
: 0;
2376 /* Process bytes from the least significant end, so point p at the
2377 least significant word. */
2380 p
+= size
* (count
- 1);
2381 word_step
= - word_step
;
2384 /* And at least significant byte of that word. */
2388 for (bytes
= 0, i
= 0, k
= 0; k
< count
; k
++, p
+= word_step
)
2391 for (j
= 0; j
< size
; ++j
, p
-= (ptrdiff_t) endian
)
2393 if (sizeof (mp_limb_t
) == 1)
2402 int LOCAL_CHAR_BIT
= CHAR_BIT
;
2406 limb
= u
->_mp_d
[i
++];
2407 bytes
= sizeof (mp_limb_t
);
2410 limb
>>= LOCAL_CHAR_BIT
;
2416 assert (k
== count
);