Numworks Epsilon  1.4.1
Graphing Calculator Operating System
mpz.c
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 2014 Damien P. George
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include <string.h>
28 #include <assert.h>
29 
30 #include "py/mpz.h"
31 
32 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
33 
34 #define DIG_SIZE (MPZ_DIG_SIZE)
35 #define DIG_MASK ((MPZ_LONG_1 << DIG_SIZE) - 1)
36 #define DIG_MSB (MPZ_LONG_1 << (DIG_SIZE - 1))
37 #define DIG_BASE (MPZ_LONG_1 << DIG_SIZE)
38 
39 /*
40  mpz is an arbitrary precision integer type with a public API.
41 
42  mpn functions act on non-negative integers represented by an array of generalised
43  digits (eg a word per digit). You also need to specify separately the length of the
44  array. There is no public API for mpn. Rather, the functions are used by mpz to
45  implement its features.
46 
47  Integer values are stored little endian (first digit is first in memory).
48 
49  Definition of normalise: ?
50 */
51 
53  for (--idig; idig >= oidig && *idig == 0; --idig) {
54  }
55  return idig + 1 - oidig;
56 }
57 
58 /* compares i with j
59  returns sign(i - j)
60  assumes i, j are normalised
61 */
62 STATIC int mpn_cmp(const mpz_dig_t *idig, size_t ilen, const mpz_dig_t *jdig, size_t jlen) {
63  if (ilen < jlen) { return -1; }
64  if (ilen > jlen) { return 1; }
65 
66  for (idig += ilen, jdig += ilen; ilen > 0; --ilen) {
67  mpz_dbl_dig_signed_t cmp = (mpz_dbl_dig_t)*(--idig) - (mpz_dbl_dig_t)*(--jdig);
68  if (cmp < 0) { return -1; }
69  if (cmp > 0) { return 1; }
70  }
71 
72  return 0;
73 }
74 
75 /* computes i = j << n
76  returns number of digits in i
77  assumes enough memory in i; assumes normalised j; assumes n > 0
78  can have i, j pointing to same memory
79 */
80 STATIC size_t mpn_shl(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mp_uint_t n) {
81  mp_uint_t n_whole = (n + DIG_SIZE - 1) / DIG_SIZE;
82  mp_uint_t n_part = n % DIG_SIZE;
83  if (n_part == 0) {
84  n_part = DIG_SIZE;
85  }
86 
87  // start from the high end of the digit arrays
88  idig += jlen + n_whole - 1;
89  jdig += jlen - 1;
90 
91  // shift the digits
92  mpz_dbl_dig_t d = 0;
93  for (size_t i = jlen; i > 0; i--, idig--, jdig--) {
94  d |= *jdig;
95  *idig = (d >> (DIG_SIZE - n_part)) & DIG_MASK;
96  d <<= DIG_SIZE;
97  }
98 
99  // store remaining bits
100  *idig = (d >> (DIG_SIZE - n_part)) & DIG_MASK;
101  idig -= n_whole - 1;
102  memset(idig, 0, (n_whole - 1) * sizeof(mpz_dig_t));
103 
104  // work out length of result
105  jlen += n_whole;
106  while (jlen != 0 && idig[jlen - 1] == 0) {
107  jlen--;
108  }
109 
110  // return length of result
111  return jlen;
112 }
113 
114 /* computes i = j >> n
115  returns number of digits in i
116  assumes enough memory in i; assumes normalised j; assumes n > 0
117  can have i, j pointing to same memory
118 */
119 STATIC size_t mpn_shr(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mp_uint_t n) {
120  mp_uint_t n_whole = n / DIG_SIZE;
121  mp_uint_t n_part = n % DIG_SIZE;
122 
123  if (n_whole >= jlen) {
124  return 0;
125  }
126 
127  jdig += n_whole;
128  jlen -= n_whole;
129 
130  for (size_t i = jlen; i > 0; i--, idig++, jdig++) {
131  mpz_dbl_dig_t d = *jdig;
132  if (i > 1) {
133  d |= (mpz_dbl_dig_t)jdig[1] << DIG_SIZE;
134  }
135  d >>= n_part;
136  *idig = d & DIG_MASK;
137  }
138 
139  if (idig[-1] == 0) {
140  jlen--;
141  }
142 
143  return jlen;
144 }
145 
146 /* computes i = j + k
147  returns number of digits in i
148  assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen
149  can have i, j, k pointing to same memory
150 */
151 STATIC size_t mpn_add(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen) {
152  mpz_dig_t *oidig = idig;
153  mpz_dbl_dig_t carry = 0;
154 
155  jlen -= klen;
156 
157  for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
158  carry += (mpz_dbl_dig_t)*jdig + (mpz_dbl_dig_t)*kdig;
159  *idig = carry & DIG_MASK;
160  carry >>= DIG_SIZE;
161  }
162 
163  for (; jlen > 0; --jlen, ++idig, ++jdig) {
164  carry += *jdig;
165  *idig = carry & DIG_MASK;
166  carry >>= DIG_SIZE;
167  }
168 
169  if (carry != 0) {
170  *idig++ = carry;
171  }
172 
173  return idig - oidig;
174 }
175 
176 /* computes i = j - k
177  returns number of digits in i
178  assumes enough memory in i; assumes normalised j, k; assumes j >= k
179  can have i, j, k pointing to same memory
180 */
181 STATIC size_t mpn_sub(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen) {
182  mpz_dig_t *oidig = idig;
183  mpz_dbl_dig_signed_t borrow = 0;
184 
185  jlen -= klen;
186 
187  for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
188  borrow += (mpz_dbl_dig_t)*jdig - (mpz_dbl_dig_t)*kdig;
189  *idig = borrow & DIG_MASK;
190  borrow >>= DIG_SIZE;
191  }
192 
193  for (; jlen > 0; --jlen, ++idig, ++jdig) {
194  borrow += *jdig;
195  *idig = borrow & DIG_MASK;
196  borrow >>= DIG_SIZE;
197  }
198 
199  return mpn_remove_trailing_zeros(oidig, idig);
200 }
201 
202 #if MICROPY_OPT_MPZ_BITWISE
203 
204 /* computes i = j & k
205  returns number of digits in i
206  assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen (jlen argument not needed)
207  can have i, j, k pointing to same memory
208 */
209 STATIC size_t mpn_and(mpz_dig_t *idig, const mpz_dig_t *jdig, const mpz_dig_t *kdig, size_t klen) {
210  mpz_dig_t *oidig = idig;
211 
212  for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
213  *idig = *jdig & *kdig;
214  }
215 
216  return mpn_remove_trailing_zeros(oidig, idig);
217 }
218 
219 #endif
220 
221 /* i = -((-j) & (-k)) = ~((~j + 1) & (~k + 1)) + 1
222  i = (j & (-k)) = (j & (~k + 1)) = ( j & (~k + 1))
223  i = ((-j) & k) = ((~j + 1) & k) = ((~j + 1) & k )
224  computes general form:
225  i = (im ^ (((j ^ jm) + jc) & ((k ^ km) + kc))) + ic where Xm = Xc == 0 ? 0 : DIG_MASK
226  returns number of digits in i
227  assumes enough memory in i; assumes normalised j, k; assumes length j >= length k
228  can have i, j, k pointing to same memory
229 */
230 STATIC size_t mpn_and_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen,
231  mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
232  mpz_dig_t *oidig = idig;
233  mpz_dig_t imask = (0 == carryi) ? 0 : DIG_MASK;
234  mpz_dig_t jmask = (0 == carryj) ? 0 : DIG_MASK;
235  mpz_dig_t kmask = (0 == carryk) ? 0 : DIG_MASK;
236 
237  for (; jlen > 0; ++idig, ++jdig) {
238  carryj += *jdig ^ jmask;
239  carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
240  carryi += ((carryj & carryk) ^ imask) & DIG_MASK;
241  *idig = carryi & DIG_MASK;
242  carryk >>= DIG_SIZE;
243  carryj >>= DIG_SIZE;
244  carryi >>= DIG_SIZE;
245  }
246 
247  if (0 != carryi) {
248  *idig++ = carryi;
249  }
250 
251  return mpn_remove_trailing_zeros(oidig, idig);
252 }
253 
254 #if MICROPY_OPT_MPZ_BITWISE
255 
256 /* computes i = j | k
257  returns number of digits in i
258  assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen
259  can have i, j, k pointing to same memory
260 */
261 STATIC size_t mpn_or(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen) {
262  mpz_dig_t *oidig = idig;
263 
264  jlen -= klen;
265 
266  for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
267  *idig = *jdig | *kdig;
268  }
269 
270  for (; jlen > 0; --jlen, ++idig, ++jdig) {
271  *idig = *jdig;
272  }
273 
274  return idig - oidig;
275 }
276 
277 #endif
278 
279 /* i = -((-j) | (-k)) = ~((~j + 1) | (~k + 1)) + 1
280  i = -(j | (-k)) = -(j | (~k + 1)) = ~( j | (~k + 1)) + 1
281  i = -((-j) | k) = -((~j + 1) | k) = ~((~j + 1) | k ) + 1
282  computes general form:
283  i = ~(((j ^ jm) + jc) | ((k ^ km) + kc)) + 1 where Xm = Xc == 0 ? 0 : DIG_MASK
284  returns number of digits in i
285  assumes enough memory in i; assumes normalised j, k; assumes length j >= length k
286  can have i, j, k pointing to same memory
287 */
288 
289 #if MICROPY_OPT_MPZ_BITWISE
290 
291 STATIC size_t mpn_or_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen,
292  mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
293  mpz_dig_t *oidig = idig;
294  mpz_dbl_dig_t carryi = 1;
295  mpz_dig_t jmask = (0 == carryj) ? 0 : DIG_MASK;
296  mpz_dig_t kmask = (0 == carryk) ? 0 : DIG_MASK;
297 
298  for (; jlen > 0; ++idig, ++jdig) {
299  carryj += *jdig ^ jmask;
300  carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
301  carryi += ((carryj | carryk) ^ DIG_MASK) & DIG_MASK;
302  *idig = carryi & DIG_MASK;
303  carryk >>= DIG_SIZE;
304  carryj >>= DIG_SIZE;
305  carryi >>= DIG_SIZE;
306  }
307 
308  // At least one of j,k must be negative so the above for-loop runs at least
309  // once. For carryi to be non-zero here it must be equal to 1 at the end of
310  // each iteration of the loop. So the accumulation of carryi must overflow
311  // each time, ie carryi += 0xff..ff. So carryj|carryk must be 0 in the
312  // DIG_MASK bits on each iteration. But considering all cases of signs of
313  // j,k one sees that this is not possible.
314  assert(carryi == 0);
315 
316  return mpn_remove_trailing_zeros(oidig, idig);
317 }
318 
319 #else
320 
321 STATIC size_t mpn_or_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen,
322  mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
323  mpz_dig_t *oidig = idig;
324  mpz_dig_t imask = (0 == carryi) ? 0 : DIG_MASK;
325  mpz_dig_t jmask = (0 == carryj) ? 0 : DIG_MASK;
326  mpz_dig_t kmask = (0 == carryk) ? 0 : DIG_MASK;
327 
328  for (; jlen > 0; ++idig, ++jdig) {
329  carryj += *jdig ^ jmask;
330  carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
331  carryi += ((carryj | carryk) ^ imask) & DIG_MASK;
332  *idig = carryi & DIG_MASK;
333  carryk >>= DIG_SIZE;
334  carryj >>= DIG_SIZE;
335  carryi >>= DIG_SIZE;
336  }
337 
338  // See comment in above mpn_or_neg for why carryi must be 0.
339  assert(carryi == 0);
340 
341  return mpn_remove_trailing_zeros(oidig, idig);
342 }
343 
344 #endif
345 
346 #if MICROPY_OPT_MPZ_BITWISE
347 
348 /* computes i = j ^ k
349  returns number of digits in i
350  assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen
351  can have i, j, k pointing to same memory
352 */
353 STATIC size_t mpn_xor(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen) {
354  mpz_dig_t *oidig = idig;
355 
356  jlen -= klen;
357 
358  for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
359  *idig = *jdig ^ *kdig;
360  }
361 
362  for (; jlen > 0; --jlen, ++idig, ++jdig) {
363  *idig = *jdig;
364  }
365 
366  return mpn_remove_trailing_zeros(oidig, idig);
367 }
368 
369 #endif
370 
371 /* i = (-j) ^ (-k) = ~(j - 1) ^ ~(k - 1) = (j - 1) ^ (k - 1)
372  i = -(j ^ (-k)) = -(j ^ ~(k - 1)) = ~(j ^ ~(k - 1)) + 1 = (j ^ (k - 1)) + 1
373  i = -((-j) ^ k) = -(~(j - 1) ^ k) = ~(~(j - 1) ^ k) + 1 = ((j - 1) ^ k) + 1
374  computes general form:
375  i = ((j - 1 + jc) ^ (k - 1 + kc)) + ic
376  returns number of digits in i
377  assumes enough memory in i; assumes normalised j, k; assumes length j >= length k
378  can have i, j, k pointing to same memory
379 */
380 STATIC size_t mpn_xor_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen,
381  mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
382  mpz_dig_t *oidig = idig;
383 
384  for (; jlen > 0; ++idig, ++jdig) {
385  carryj += *jdig + DIG_MASK;
386  carryk += (--klen <= --jlen) ? (*kdig++ + DIG_MASK) : DIG_MASK;
387  carryi += (carryj ^ carryk) & DIG_MASK;
388  *idig = carryi & DIG_MASK;
389  carryk >>= DIG_SIZE;
390  carryj >>= DIG_SIZE;
391  carryi >>= DIG_SIZE;
392  }
393 
394  if (0 != carryi) {
395  *idig++ = carryi;
396  }
397 
398  return mpn_remove_trailing_zeros(oidig, idig);
399 }
400 
401 /* computes i = i * d1 + d2
402  returns number of digits in i
403  assumes enough memory in i; assumes normalised i; assumes dmul != 0
404 */
405 STATIC size_t mpn_mul_dig_add_dig(mpz_dig_t *idig, size_t ilen, mpz_dig_t dmul, mpz_dig_t dadd) {
406  mpz_dig_t *oidig = idig;
407  mpz_dbl_dig_t carry = dadd;
408 
409  for (; ilen > 0; --ilen, ++idig) {
410  carry += (mpz_dbl_dig_t)*idig * (mpz_dbl_dig_t)dmul; // will never overflow so long as DIG_SIZE <= 8*sizeof(mpz_dbl_dig_t)/2
411  *idig = carry & DIG_MASK;
412  carry >>= DIG_SIZE;
413  }
414 
415  if (carry != 0) {
416  *idig++ = carry;
417  }
418 
419  return idig - oidig;
420 }
421 
422 /* computes i = j * k
423  returns number of digits in i
424  assumes enough memory in i; assumes i is zeroed; assumes normalised j, k
425  can have j, k point to same memory
426 */
427 STATIC size_t mpn_mul(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mpz_dig_t *kdig, size_t klen) {
428  mpz_dig_t *oidig = idig;
429  size_t ilen = 0;
430 
431  for (; klen > 0; --klen, ++idig, ++kdig) {
432  mpz_dig_t *id = idig;
433  mpz_dbl_dig_t carry = 0;
434 
435  size_t jl = jlen;
436  for (mpz_dig_t *jd = jdig; jl > 0; --jl, ++jd, ++id) {
437  carry += (mpz_dbl_dig_t)*id + (mpz_dbl_dig_t)*jd * (mpz_dbl_dig_t)*kdig; // will never overflow so long as DIG_SIZE <= 8*sizeof(mpz_dbl_dig_t)/2
438  *id = carry & DIG_MASK;
439  carry >>= DIG_SIZE;
440  }
441 
442  if (carry != 0) {
443  *id++ = carry;
444  }
445 
446  ilen = id - oidig;
447  }
448 
449  return ilen;
450 }
451 
452 /* natural_div - quo * den + new_num = old_num (ie num is replaced with rem)
453  assumes den != 0
454  assumes num_dig has enough memory to be extended by 1 digit
455  assumes quo_dig has enough memory (as many digits as num)
456  assumes quo_dig is filled with zeros
457 */
458 STATIC void mpn_div(mpz_dig_t *num_dig, size_t *num_len, const mpz_dig_t *den_dig, size_t den_len, mpz_dig_t *quo_dig, size_t *quo_len) {
459  mpz_dig_t *orig_num_dig = num_dig;
460  mpz_dig_t *orig_quo_dig = quo_dig;
461  mpz_dig_t norm_shift = 0;
462  mpz_dbl_dig_t lead_den_digit;
463 
464  // handle simple cases
465  {
466  int cmp = mpn_cmp(num_dig, *num_len, den_dig, den_len);
467  if (cmp == 0) {
468  *num_len = 0;
469  quo_dig[0] = 1;
470  *quo_len = 1;
471  return;
472  } else if (cmp < 0) {
473  // numerator remains the same
474  *quo_len = 0;
475  return;
476  }
477  }
478 
479  // We need to normalise the denominator (leading bit of leading digit is 1)
480  // so that the division routine works. Since the denominator memory is
481  // read-only we do the normalisation on the fly, each time a digit of the
482  // denominator is needed. We need to know is how many bits to shift by.
483 
484  // count number of leading zeros in leading digit of denominator
485  {
486  mpz_dig_t d = den_dig[den_len - 1];
487  while ((d & DIG_MSB) == 0) {
488  d <<= 1;
489  ++norm_shift;
490  }
491  }
492 
493  // now need to shift numerator by same amount as denominator
494  // first, increase length of numerator in case we need more room to shift
495  num_dig[*num_len] = 0;
496  ++(*num_len);
497  for (mpz_dig_t *num = num_dig, carry = 0; num < num_dig + *num_len; ++num) {
498  mpz_dig_t n = *num;
499  *num = ((n << norm_shift) | carry) & DIG_MASK;
500  carry = (mpz_dbl_dig_t)n >> (DIG_SIZE - norm_shift);
501  }
502 
503  // cache the leading digit of the denominator
504  lead_den_digit = (mpz_dbl_dig_t)den_dig[den_len - 1] << norm_shift;
505  if (den_len >= 2) {
506  lead_den_digit |= (mpz_dbl_dig_t)den_dig[den_len - 2] >> (DIG_SIZE - norm_shift);
507  }
508 
509  // point num_dig to last digit in numerator
510  num_dig += *num_len - 1;
511 
512  // calculate number of digits in quotient
513  *quo_len = *num_len - den_len;
514 
515  // point to last digit to store for quotient
516  quo_dig += *quo_len - 1;
517 
518  // keep going while we have enough digits to divide
519  while (*num_len > den_len) {
520  mpz_dbl_dig_t quo = ((mpz_dbl_dig_t)*num_dig << DIG_SIZE) | num_dig[-1];
521 
522  // get approximate quotient
523  quo /= lead_den_digit;
524 
525  // Multiply quo by den and subtract from num to get remainder.
526  // We have different code here to handle different compile-time
527  // configurations of mpz:
528  //
529  // 1. DIG_SIZE is stricly less than half the number of bits
530  // available in mpz_dbl_dig_t. In this case we can use a
531  // slightly more optimal (in time and space) routine that
532  // uses the extra bits in mpz_dbl_dig_signed_t to store a
533  // sign bit.
534  //
535  // 2. DIG_SIZE is exactly half the number of bits available in
536  // mpz_dbl_dig_t. In this (common) case we need to be careful
537  // not to overflow the borrow variable. And the shifting of
538  // borrow needs some special logic (it's a shift right with
539  // round up).
540 
541  if (DIG_SIZE < 8 * sizeof(mpz_dbl_dig_t) / 2) {
542  const mpz_dig_t *d = den_dig;
543  mpz_dbl_dig_t d_norm = 0;
544  mpz_dbl_dig_signed_t borrow = 0;
545 
546  for (mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
547  d_norm = ((mpz_dbl_dig_t)*d << norm_shift) | (d_norm >> DIG_SIZE);
548  borrow += (mpz_dbl_dig_t)*n - (mpz_dbl_dig_t)quo * (d_norm & DIG_MASK); // will overflow if DIG_SIZE >= 8*sizeof(mpz_dbl_dig_t)/2
549  *n = borrow & DIG_MASK;
550  borrow >>= DIG_SIZE;
551  }
552  borrow += *num_dig; // will overflow if DIG_SIZE >= 8*sizeof(mpz_dbl_dig_t)/2
553  *num_dig = borrow & DIG_MASK;
554  borrow >>= DIG_SIZE;
555 
556  // adjust quotient if it is too big
557  for (; borrow != 0; --quo) {
558  d = den_dig;
559  d_norm = 0;
560  mpz_dbl_dig_t carry = 0;
561  for (mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
562  d_norm = ((mpz_dbl_dig_t)*d << norm_shift) | (d_norm >> DIG_SIZE);
563  carry += (mpz_dbl_dig_t)*n + (d_norm & DIG_MASK);
564  *n = carry & DIG_MASK;
565  carry >>= DIG_SIZE;
566  }
567  carry += *num_dig;
568  *num_dig = carry & DIG_MASK;
569  carry >>= DIG_SIZE;
570 
571  borrow += carry;
572  }
573  } else { // DIG_SIZE == 8 * sizeof(mpz_dbl_dig_t) / 2
574  const mpz_dig_t *d = den_dig;
575  mpz_dbl_dig_t d_norm = 0;
576  mpz_dbl_dig_t borrow = 0;
577 
578  for (mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
579  d_norm = ((mpz_dbl_dig_t)*d << norm_shift) | (d_norm >> DIG_SIZE);
580  mpz_dbl_dig_t x = (mpz_dbl_dig_t)quo * (d_norm & DIG_MASK);
581  if (x >= *n || *n - x <= borrow) {
582  borrow += (mpz_dbl_dig_t)x - (mpz_dbl_dig_t)*n;
583  *n = (-borrow) & DIG_MASK;
584  borrow = (borrow >> DIG_SIZE) + ((borrow & DIG_MASK) == 0 ? 0 : 1); // shift-right with round-up
585  } else {
586  *n = ((mpz_dbl_dig_t)*n - (mpz_dbl_dig_t)x - (mpz_dbl_dig_t)borrow) & DIG_MASK;
587  borrow = 0;
588  }
589  }
590  if (borrow >= *num_dig) {
591  borrow -= (mpz_dbl_dig_t)*num_dig;
592  *num_dig = (-borrow) & DIG_MASK;
593  borrow = (borrow >> DIG_SIZE) + ((borrow & DIG_MASK) == 0 ? 0 : 1); // shift-right with round-up
594  } else {
595  *num_dig = (*num_dig - borrow) & DIG_MASK;
596  borrow = 0;
597  }
598 
599  // adjust quotient if it is too big
600  for (; borrow != 0; --quo) {
601  d = den_dig;
602  d_norm = 0;
603  mpz_dbl_dig_t carry = 0;
604  for (mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
605  d_norm = ((mpz_dbl_dig_t)*d << norm_shift) | (d_norm >> DIG_SIZE);
606  carry += (mpz_dbl_dig_t)*n + (d_norm & DIG_MASK);
607  *n = carry & DIG_MASK;
608  carry >>= DIG_SIZE;
609  }
610  carry += (mpz_dbl_dig_t)*num_dig;
611  *num_dig = carry & DIG_MASK;
612  carry >>= DIG_SIZE;
613 
614  //assert(borrow >= carry); // enable this to check the logic
615  borrow -= carry;
616  }
617  }
618 
619  // store this digit of the quotient
620  *quo_dig = quo & DIG_MASK;
621  --quo_dig;
622 
623  // move down to next digit of numerator
624  --num_dig;
625  --(*num_len);
626  }
627 
628  // unnormalise numerator (remainder now)
629  for (mpz_dig_t *num = orig_num_dig + *num_len - 1, carry = 0; num >= orig_num_dig; --num) {
630  mpz_dig_t n = *num;
631  *num = ((n >> norm_shift) | carry) & DIG_MASK;
632  carry = (mpz_dbl_dig_t)n << (DIG_SIZE - norm_shift);
633  }
634 
635  // strip trailing zeros
636 
637  while (*quo_len > 0 && orig_quo_dig[*quo_len - 1] == 0) {
638  --(*quo_len);
639  }
640 
641  while (*num_len > 0 && orig_num_dig[*num_len - 1] == 0) {
642  --(*num_len);
643  }
644 }
645 
646 #define MIN_ALLOC (2)
647 
649  z->neg = 0;
650  z->fixed_dig = 0;
651  z->alloc = 0;
652  z->len = 0;
653  z->dig = NULL;
654 }
655 
657  mpz_init_zero(z);
658  mpz_set_from_int(z, val);
659 }
660 
661 void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, size_t alloc, mp_int_t val) {
662  z->neg = 0;
663  z->fixed_dig = 1;
664  z->alloc = alloc;
665  z->len = 0;
666  z->dig = dig;
667  mpz_set_from_int(z, val);
668 }
669 
670 void mpz_deinit(mpz_t *z) {
671  if (z != NULL && !z->fixed_dig) {
672  m_del(mpz_dig_t, z->dig, z->alloc);
673  }
674 }
675 
676 #if 0
677 these functions are unused
678 
679 mpz_t *mpz_zero(void) {
680  mpz_t *z = m_new_obj(mpz_t);
681  mpz_init_zero(z);
682  return z;
683 }
684 
685 mpz_t *mpz_from_int(mp_int_t val) {
686  mpz_t *z = mpz_zero();
687  mpz_set_from_int(z, val);
688  return z;
689 }
690 
691 mpz_t *mpz_from_ll(long long val, bool is_signed) {
692  mpz_t *z = mpz_zero();
693  mpz_set_from_ll(z, val, is_signed);
694  return z;
695 }
696 
697 #if MICROPY_PY_BUILTINS_FLOAT
698 mpz_t *mpz_from_float(mp_float_t val) {
699  mpz_t *z = mpz_zero();
700  mpz_set_from_float(z, val);
701  return z;
702 }
703 #endif
704 
705 mpz_t *mpz_from_str(const char *str, size_t len, bool neg, unsigned int base) {
706  mpz_t *z = mpz_zero();
707  mpz_set_from_str(z, str, len, neg, base);
708  return z;
709 }
710 #endif
711 
713  if (z != NULL) {
714  m_del(mpz_dig_t, z->dig, z->alloc);
715  m_del_obj(mpz_t, z);
716  }
717 }
718 
719 STATIC void mpz_need_dig(mpz_t *z, size_t need) {
720  if (need < MIN_ALLOC) {
721  need = MIN_ALLOC;
722  }
723 
724  if (z->dig == NULL || z->alloc < need) {
725  // if z has fixed digit buffer there's not much we can do as the caller will
726  // be expecting a buffer with at least "need" bytes (but it shouldn't happen)
727  assert(!z->fixed_dig);
728  z->dig = m_renew(mpz_dig_t, z->dig, z->alloc, need);
729  z->alloc = need;
730  }
731 }
732 
733 STATIC mpz_t *mpz_clone(const mpz_t *src) {
734  mpz_t *z = m_new_obj(mpz_t);
735  z->neg = src->neg;
736  z->fixed_dig = 0;
737  z->alloc = src->alloc;
738  z->len = src->len;
739  if (src->dig == NULL) {
740  z->dig = NULL;
741  } else {
742  z->dig = m_new(mpz_dig_t, z->alloc);
743  memcpy(z->dig, src->dig, src->alloc * sizeof(mpz_dig_t));
744  }
745  return z;
746 }
747 
748 /* sets dest = src
749  can have dest, src the same
750 */
751 void mpz_set(mpz_t *dest, const mpz_t *src) {
752  mpz_need_dig(dest, src->len);
753  dest->neg = src->neg;
754  dest->len = src->len;
755  memcpy(dest->dig, src->dig, src->len * sizeof(mpz_dig_t));
756 }
757 
759  if (val == 0) {
760  z->len = 0;
761  return;
762  }
763 
765 
766  mp_uint_t uval;
767  if (val < 0) {
768  z->neg = 1;
769  uval = -val;
770  } else {
771  z->neg = 0;
772  uval = val;
773  }
774 
775  z->len = 0;
776  while (uval > 0) {
777  z->dig[z->len++] = uval & DIG_MASK;
778  uval >>= DIG_SIZE;
779  }
780 }
781 
782 void mpz_set_from_ll(mpz_t *z, long long val, bool is_signed) {
784 
785  unsigned long long uval;
786  if (is_signed && val < 0) {
787  z->neg = 1;
788  uval = -val;
789  } else {
790  z->neg = 0;
791  uval = val;
792  }
793 
794  z->len = 0;
795  while (uval > 0) {
796  z->dig[z->len++] = uval & DIG_MASK;
797  uval >>= DIG_SIZE;
798  }
799 }
800 
801 #if MICROPY_PY_BUILTINS_FLOAT
802 void mpz_set_from_float(mpz_t *z, mp_float_t src) {
803 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
804 typedef uint64_t mp_float_int_t;
805 #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
806 typedef uint32_t mp_float_int_t;
807 #endif
808  union {
809  mp_float_t f;
810  #if MP_ENDIANNESS_LITTLE
811  struct { mp_float_int_t frc:MP_FLOAT_FRAC_BITS, exp:MP_FLOAT_EXP_BITS, sgn:1; } p;
812  #else
813  struct { mp_float_int_t sgn:1, exp:MP_FLOAT_EXP_BITS, frc:MP_FLOAT_FRAC_BITS; } p;
814  #endif
815  } u = {src};
816 
817  z->neg = u.p.sgn;
818  if (u.p.exp == 0) {
819  // value == 0 || value < 1
820  mpz_set_from_int(z, 0);
821  } else if (u.p.exp == ((1 << MP_FLOAT_EXP_BITS) - 1)) {
822  // u.p.frc == 0 indicates inf, else NaN
823  // should be handled by caller
824  mpz_set_from_int(z, 0);
825  } else {
826  const int adj_exp = (int)u.p.exp - MP_FLOAT_EXP_BIAS;
827  if (adj_exp < 0) {
828  // value < 1 , truncates to 0
829  mpz_set_from_int(z, 0);
830  } else if (adj_exp == 0) {
831  // 1 <= value < 2 , so truncates to 1
832  mpz_set_from_int(z, 1);
833  } else {
834  // 2 <= value
835  const int dig_cnt = (adj_exp + 1 + (DIG_SIZE - 1)) / DIG_SIZE;
836  const unsigned int rem = adj_exp % DIG_SIZE;
837  int dig_ind, shft;
838  mp_float_int_t frc = u.p.frc | ((mp_float_int_t)1 << MP_FLOAT_FRAC_BITS);
839 
840  if (adj_exp < MP_FLOAT_FRAC_BITS) {
841  shft = 0;
842  dig_ind = 0;
843  frc >>= MP_FLOAT_FRAC_BITS - adj_exp;
844  } else {
845  shft = (rem - MP_FLOAT_FRAC_BITS) % DIG_SIZE;
846  dig_ind = (adj_exp - MP_FLOAT_FRAC_BITS) / DIG_SIZE;
847  }
848  mpz_need_dig(z, dig_cnt);
849  z->len = dig_cnt;
850  if (dig_ind != 0) {
851  memset(z->dig, 0, dig_ind * sizeof(mpz_dig_t));
852  }
853  if (shft != 0) {
854  z->dig[dig_ind++] = (frc << shft) & DIG_MASK;
855  frc >>= DIG_SIZE - shft;
856  }
857 #if DIG_SIZE < (MP_FLOAT_FRAC_BITS + 1)
858  while (dig_ind != dig_cnt) {
859  z->dig[dig_ind++] = frc & DIG_MASK;
860  frc >>= DIG_SIZE;
861  }
862 #else
863  if (dig_ind != dig_cnt) {
864  z->dig[dig_ind] = frc;
865  }
866 #endif
867  }
868  }
869 }
870 #endif
871 
872 // returns number of bytes from str that were processed
873 size_t mpz_set_from_str(mpz_t *z, const char *str, size_t len, bool neg, unsigned int base) {
874  assert(base <= 36);
875 
876  const char *cur = str;
877  const char *top = str + len;
878 
879  mpz_need_dig(z, len * 8 / DIG_SIZE + 1);
880 
881  if (neg) {
882  z->neg = 1;
883  } else {
884  z->neg = 0;
885  }
886 
887  z->len = 0;
888  for (; cur < top; ++cur) { // XXX UTF8 next char
889  //mp_uint_t v = char_to_numeric(cur#); // XXX UTF8 get char
890  mp_uint_t v = *cur;
891  if ('0' <= v && v <= '9') {
892  v -= '0';
893  } else if ('A' <= v && v <= 'Z') {
894  v -= 'A' - 10;
895  } else if ('a' <= v && v <= 'z') {
896  v -= 'a' - 10;
897  } else {
898  break;
899  }
900  if (v >= base) {
901  break;
902  }
903  z->len = mpn_mul_dig_add_dig(z->dig, z->len, base, v);
904  }
905 
906  return cur - str;
907 }
908 
909 void mpz_set_from_bytes(mpz_t *z, bool big_endian, size_t len, const byte *buf) {
910  int delta = 1;
911  if (big_endian) {
912  buf += len - 1;
913  delta = -1;
914  }
915 
916  mpz_need_dig(z, (len * 8 + DIG_SIZE - 1) / DIG_SIZE);
917 
918  mpz_dig_t d = 0;
919  int num_bits = 0;
920  z->neg = 0;
921  z->len = 0;
922  while (len) {
923  while (len && num_bits < DIG_SIZE) {
924  d |= *buf << num_bits;
925  num_bits += 8;
926  buf += delta;
927  len--;
928  }
929  z->dig[z->len++] = d & DIG_MASK;
930  // Need this #if because it's C undefined behavior to do: uint32_t >> 32
931  #if DIG_SIZE != 8 && DIG_SIZE != 16 && DIG_SIZE != 32
932  d >>= DIG_SIZE;
933  #else
934  d = 0;
935  #endif
936  num_bits -= DIG_SIZE;
937  }
938 
939  z->len = mpn_remove_trailing_zeros(z->dig, z->dig + z->len);
940 }
941 
942 #if 0
943 these functions are unused
944 
945 bool mpz_is_pos(const mpz_t *z) {
946  return z->len > 0 && z->neg == 0;
947 }
948 
949 bool mpz_is_odd(const mpz_t *z) {
950  return z->len > 0 && (z->dig[0] & 1) != 0;
951 }
952 
953 bool mpz_is_even(const mpz_t *z) {
954  return z->len == 0 || (z->dig[0] & 1) == 0;
955 }
956 #endif
957 
958 int mpz_cmp(const mpz_t *z1, const mpz_t *z2) {
959  // to catch comparison of -0 with +0
960  if (z1->len == 0 && z2->len == 0) {
961  return 0;
962  }
963  int cmp = (int)z2->neg - (int)z1->neg;
964  if (cmp != 0) {
965  return cmp;
966  }
967  cmp = mpn_cmp(z1->dig, z1->len, z2->dig, z2->len);
968  if (z1->neg != 0) {
969  cmp = -cmp;
970  }
971  return cmp;
972 }
973 
974 #if 0
975 // obsolete
976 // compares mpz with an integer that fits within DIG_SIZE bits
977 mp_int_t mpz_cmp_sml_int(const mpz_t *z, mp_int_t sml_int) {
978  mp_int_t cmp;
979  if (z->neg == 0) {
980  if (sml_int < 0) return 1;
981  if (sml_int == 0) {
982  if (z->len == 0) return 0;
983  return 1;
984  }
985  if (z->len == 0) return -1;
986  assert(sml_int < (1 << DIG_SIZE));
987  if (z->len != 1) return 1;
988  cmp = z->dig[0] - sml_int;
989  } else {
990  if (sml_int > 0) return -1;
991  if (sml_int == 0) {
992  if (z->len == 0) return 0;
993  return -1;
994  }
995  if (z->len == 0) return 1;
996  assert(sml_int > -(1 << DIG_SIZE));
997  if (z->len != 1) return -1;
998  cmp = -z->dig[0] - sml_int;
999  }
1000  if (cmp < 0) return -1;
1001  if (cmp > 0) return 1;
1002  return 0;
1003 }
1004 #endif
1005 
1006 #if 0
1007 these functions are unused
1008 
1009 /* returns abs(z)
1010 */
1011 mpz_t *mpz_abs(const mpz_t *z) {
1012  mpz_t *z2 = mpz_clone(z);
1013  z2->neg = 0;
1014  return z2;
1015 }
1016 
1017 /* returns -z
1018 */
1019 mpz_t *mpz_neg(const mpz_t *z) {
1020  mpz_t *z2 = mpz_clone(z);
1021  z2->neg = 1 - z2->neg;
1022  return z2;
1023 }
1024 
1025 /* returns lhs + rhs
1026  can have lhs, rhs the same
1027 */
1028 mpz_t *mpz_add(const mpz_t *lhs, const mpz_t *rhs) {
1029  mpz_t *z = mpz_zero();
1030  mpz_add_inpl(z, lhs, rhs);
1031  return z;
1032 }
1033 
1034 /* returns lhs - rhs
1035  can have lhs, rhs the same
1036 */
1037 mpz_t *mpz_sub(const mpz_t *lhs, const mpz_t *rhs) {
1038  mpz_t *z = mpz_zero();
1039  mpz_sub_inpl(z, lhs, rhs);
1040  return z;
1041 }
1042 
1043 /* returns lhs * rhs
1044  can have lhs, rhs the same
1045 */
1046 mpz_t *mpz_mul(const mpz_t *lhs, const mpz_t *rhs) {
1047  mpz_t *z = mpz_zero();
1048  mpz_mul_inpl(z, lhs, rhs);
1049  return z;
1050 }
1051 
1052 /* returns lhs ** rhs
1053  can have lhs, rhs the same
1054 */
1055 mpz_t *mpz_pow(const mpz_t *lhs, const mpz_t *rhs) {
1056  mpz_t *z = mpz_zero();
1057  mpz_pow_inpl(z, lhs, rhs);
1058  return z;
1059 }
1060 
1061 /* computes new integers in quo and rem such that:
1062  quo * rhs + rem = lhs
1063  0 <= rem < rhs
1064  can have lhs, rhs the same
1065 */
1066 void mpz_divmod(const mpz_t *lhs, const mpz_t *rhs, mpz_t **quo, mpz_t **rem) {
1067  *quo = mpz_zero();
1068  *rem = mpz_zero();
1069  mpz_divmod_inpl(*quo, *rem, lhs, rhs);
1070 }
1071 #endif
1072 
1073 /* computes dest = abs(z)
1074  can have dest, z the same
1075 */
1076 void mpz_abs_inpl(mpz_t *dest, const mpz_t *z) {
1077  if (dest != z) {
1078  mpz_set(dest, z);
1079  }
1080  dest->neg = 0;
1081 }
1082 
1083 /* computes dest = -z
1084  can have dest, z the same
1085 */
1086 void mpz_neg_inpl(mpz_t *dest, const mpz_t *z) {
1087  if (dest != z) {
1088  mpz_set(dest, z);
1089  }
1090  dest->neg = 1 - dest->neg;
1091 }
1092 
1093 /* computes dest = ~z (= -z - 1)
1094  can have dest, z the same
1095 */
1096 void mpz_not_inpl(mpz_t *dest, const mpz_t *z) {
1097  if (dest != z) {
1098  mpz_set(dest, z);
1099  }
1100  if (dest->len == 0) {
1101  mpz_need_dig(dest, 1);
1102  dest->dig[0] = 1;
1103  dest->len = 1;
1104  dest->neg = 1;
1105  } else if (dest->neg) {
1106  dest->neg = 0;
1107  mpz_dig_t k = 1;
1108  dest->len = mpn_sub(dest->dig, dest->dig, dest->len, &k, 1);
1109  } else {
1110  mpz_need_dig(dest, dest->len + 1);
1111  mpz_dig_t k = 1;
1112  dest->len = mpn_add(dest->dig, dest->dig, dest->len, &k, 1);
1113  dest->neg = 1;
1114  }
1115 }
1116 
1117 /* computes dest = lhs << rhs
1118  can have dest, lhs the same
1119 */
1120 void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs) {
1121  if (lhs->len == 0 || rhs == 0) {
1122  mpz_set(dest, lhs);
1123  } else {
1124  mpz_need_dig(dest, lhs->len + (rhs + DIG_SIZE - 1) / DIG_SIZE);
1125  dest->len = mpn_shl(dest->dig, lhs->dig, lhs->len, rhs);
1126  dest->neg = lhs->neg;
1127  }
1128 }
1129 
1130 /* computes dest = lhs >> rhs
1131  can have dest, lhs the same
1132 */
1133 void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs) {
1134  if (lhs->len == 0 || rhs == 0) {
1135  mpz_set(dest, lhs);
1136  } else {
1137  mpz_need_dig(dest, lhs->len);
1138  dest->len = mpn_shr(dest->dig, lhs->dig, lhs->len, rhs);
1139  dest->neg = lhs->neg;
1140  if (dest->neg) {
1141  // arithmetic shift right, rounding to negative infinity
1142  mp_uint_t n_whole = rhs / DIG_SIZE;
1143  mp_uint_t n_part = rhs % DIG_SIZE;
1144  mpz_dig_t round_up = 0;
1145  for (size_t i = 0; i < lhs->len && i < n_whole; i++) {
1146  if (lhs->dig[i] != 0) {
1147  round_up = 1;
1148  break;
1149  }
1150  }
1151  if (n_whole < lhs->len && (lhs->dig[n_whole] & ((1 << n_part) - 1)) != 0) {
1152  round_up = 1;
1153  }
1154  if (round_up) {
1155  if (dest->len == 0) {
1156  // dest == 0, so need to add 1 by hand (answer will be -1)
1157  dest->dig[0] = 1;
1158  dest->len = 1;
1159  } else {
1160  // dest > 0, so can use mpn_add to add 1
1161  dest->len = mpn_add(dest->dig, dest->dig, dest->len, &round_up, 1);
1162  }
1163  }
1164  }
1165  }
1166 }
1167 
1168 /* computes dest = lhs + rhs
1169  can have dest, lhs, rhs the same
1170 */
1171 void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
1172  if (mpn_cmp(lhs->dig, lhs->len, rhs->dig, rhs->len) < 0) {
1173  const mpz_t *temp = lhs;
1174  lhs = rhs;
1175  rhs = temp;
1176  }
1177 
1178  if (lhs->neg == rhs->neg) {
1179  mpz_need_dig(dest, lhs->len + 1);
1180  dest->len = mpn_add(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
1181  } else {
1182  mpz_need_dig(dest, lhs->len);
1183  dest->len = mpn_sub(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
1184  }
1185 
1186  dest->neg = lhs->neg;
1187 }
1188 
1189 /* computes dest = lhs - rhs
1190  can have dest, lhs, rhs the same
1191 */
1192 void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
1193  bool neg = false;
1194 
1195  if (mpn_cmp(lhs->dig, lhs->len, rhs->dig, rhs->len) < 0) {
1196  const mpz_t *temp = lhs;
1197  lhs = rhs;
1198  rhs = temp;
1199  neg = true;
1200  }
1201 
1202  if (lhs->neg != rhs->neg) {
1203  mpz_need_dig(dest, lhs->len + 1);
1204  dest->len = mpn_add(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
1205  } else {
1206  mpz_need_dig(dest, lhs->len);
1207  dest->len = mpn_sub(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
1208  }
1209 
1210  if (neg) {
1211  dest->neg = 1 - lhs->neg;
1212  } else {
1213  dest->neg = lhs->neg;
1214  }
1215 }
1216 
1217 /* computes dest = lhs & rhs
1218  can have dest, lhs, rhs the same
1219 */
1220 void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
1221  // make sure lhs has the most digits
1222  if (lhs->len < rhs->len) {
1223  const mpz_t *temp = lhs;
1224  lhs = rhs;
1225  rhs = temp;
1226  }
1227 
1228  #if MICROPY_OPT_MPZ_BITWISE
1229 
1230  if ((0 == lhs->neg) && (0 == rhs->neg)) {
1231  mpz_need_dig(dest, lhs->len);
1232  dest->len = mpn_and(dest->dig, lhs->dig, rhs->dig, rhs->len);
1233  dest->neg = 0;
1234  } else {
1235  mpz_need_dig(dest, lhs->len + 1);
1236  dest->len = mpn_and_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
1237  lhs->neg == rhs->neg, 0 != lhs->neg, 0 != rhs->neg);
1238  dest->neg = lhs->neg & rhs->neg;
1239  }
1240 
1241  #else
1242 
1243  mpz_need_dig(dest, lhs->len + (lhs->neg || rhs->neg));
1244  dest->len = mpn_and_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
1245  (lhs->neg == rhs->neg) ? lhs->neg : 0, lhs->neg, rhs->neg);
1246  dest->neg = lhs->neg & rhs->neg;
1247 
1248  #endif
1249 }
1250 
1251 /* computes dest = lhs | rhs
1252  can have dest, lhs, rhs the same
1253 */
1254 void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
1255  // make sure lhs has the most digits
1256  if (lhs->len < rhs->len) {
1257  const mpz_t *temp = lhs;
1258  lhs = rhs;
1259  rhs = temp;
1260  }
1261 
1262  #if MICROPY_OPT_MPZ_BITWISE
1263 
1264  if ((0 == lhs->neg) && (0 == rhs->neg)) {
1265  mpz_need_dig(dest, lhs->len);
1266  dest->len = mpn_or(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
1267  dest->neg = 0;
1268  } else {
1269  mpz_need_dig(dest, lhs->len + 1);
1270  dest->len = mpn_or_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
1271  0 != lhs->neg, 0 != rhs->neg);
1272  dest->neg = 1;
1273  }
1274 
1275  #else
1276 
1277  mpz_need_dig(dest, lhs->len + (lhs->neg || rhs->neg));
1278  dest->len = mpn_or_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
1279  (lhs->neg || rhs->neg), lhs->neg, rhs->neg);
1280  dest->neg = lhs->neg | rhs->neg;
1281 
1282  #endif
1283 }
1284 
1285 /* computes dest = lhs ^ rhs
1286  can have dest, lhs, rhs the same
1287 */
1288 void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
1289  // make sure lhs has the most digits
1290  if (lhs->len < rhs->len) {
1291  const mpz_t *temp = lhs;
1292  lhs = rhs;
1293  rhs = temp;
1294  }
1295 
1296  #if MICROPY_OPT_MPZ_BITWISE
1297 
1298  if (lhs->neg == rhs->neg) {
1299  mpz_need_dig(dest, lhs->len);
1300  if (lhs->neg == 0) {
1301  dest->len = mpn_xor(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
1302  } else {
1303  dest->len = mpn_xor_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len, 0, 0, 0);
1304  }
1305  dest->neg = 0;
1306  } else {
1307  mpz_need_dig(dest, lhs->len + 1);
1308  dest->len = mpn_xor_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len, 1,
1309  0 == lhs->neg, 0 == rhs->neg);
1310  dest->neg = 1;
1311  }
1312 
1313  #else
1314 
1315  mpz_need_dig(dest, lhs->len + (lhs->neg || rhs->neg));
1316  dest->len = mpn_xor_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
1317  (lhs->neg != rhs->neg), 0 == lhs->neg, 0 == rhs->neg);
1318  dest->neg = lhs->neg ^ rhs->neg;
1319 
1320  #endif
1321 }
1322 
1323 /* computes dest = lhs * rhs
1324  can have dest, lhs, rhs the same
1325 */
1326 void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
1327  if (lhs->len == 0 || rhs->len == 0) {
1328  mpz_set_from_int(dest, 0);
1329  return;
1330  }
1331 
1332  mpz_t *temp = NULL;
1333  if (lhs == dest) {
1334  lhs = temp = mpz_clone(lhs);
1335  if (rhs == dest) {
1336  rhs = lhs;
1337  }
1338  } else if (rhs == dest) {
1339  rhs = temp = mpz_clone(rhs);
1340  }
1341 
1342  mpz_need_dig(dest, lhs->len + rhs->len); // min mem l+r-1, max mem l+r
1343  memset(dest->dig, 0, dest->alloc * sizeof(mpz_dig_t));
1344  dest->len = mpn_mul(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
1345 
1346  if (lhs->neg == rhs->neg) {
1347  dest->neg = 0;
1348  } else {
1349  dest->neg = 1;
1350  }
1351 
1352  mpz_free(temp);
1353 }
1354 
1355 /* computes dest = lhs ** rhs
1356  can have dest, lhs, rhs the same
1357 */
1358 void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
1359  if (lhs->len == 0 || rhs->neg != 0) {
1360  mpz_set_from_int(dest, 0);
1361  return;
1362  }
1363 
1364  if (rhs->len == 0) {
1365  mpz_set_from_int(dest, 1);
1366  return;
1367  }
1368 
1369  mpz_t *x = mpz_clone(lhs);
1370  mpz_t *n = mpz_clone(rhs);
1371 
1372  mpz_set_from_int(dest, 1);
1373 
1374  while (n->len > 0) {
1375  if ((n->dig[0] & 1) != 0) {
1376  mpz_mul_inpl(dest, dest, x);
1377  }
1378  n->len = mpn_shr(n->dig, n->dig, n->len, 1);
1379  if (n->len == 0) {
1380  break;
1381  }
1382  mpz_mul_inpl(x, x, x);
1383  }
1384 
1385  mpz_free(x);
1386  mpz_free(n);
1387 }
1388 
1389 /* computes dest = (lhs ** rhs) % mod
1390  can have dest, lhs, rhs the same; mod can't be the same as dest
1391 */
1392 void mpz_pow3_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs, const mpz_t *mod) {
1393  if (lhs->len == 0 || rhs->neg != 0) {
1394  mpz_set_from_int(dest, 0);
1395  return;
1396  }
1397 
1398  if (rhs->len == 0) {
1399  mpz_set_from_int(dest, 1);
1400  return;
1401  }
1402 
1403  mpz_t *x = mpz_clone(lhs);
1404  mpz_t *n = mpz_clone(rhs);
1405  mpz_t quo; mpz_init_zero(&quo);
1406 
1407  mpz_set_from_int(dest, 1);
1408 
1409  while (n->len > 0) {
1410  if ((n->dig[0] & 1) != 0) {
1411  mpz_mul_inpl(dest, dest, x);
1412  mpz_divmod_inpl(&quo, dest, dest, mod);
1413  }
1414  n->len = mpn_shr(n->dig, n->dig, n->len, 1);
1415  if (n->len == 0) {
1416  break;
1417  }
1418  mpz_mul_inpl(x, x, x);
1419  mpz_divmod_inpl(&quo, x, x, mod);
1420  }
1421 
1422  mpz_deinit(&quo);
1423  mpz_free(x);
1424  mpz_free(n);
1425 }
1426 
1427 #if 0
1428 these functions are unused
1429 
1430 /* computes gcd(z1, z2)
1431  based on Knuth's modified gcd algorithm (I think?)
1432  gcd(z1, z2) >= 0
1433  gcd(0, 0) = 0
1434  gcd(z, 0) = abs(z)
1435 */
1436 mpz_t *mpz_gcd(const mpz_t *z1, const mpz_t *z2) {
1437  if (z1->len == 0) {
1438  mpz_t *a = mpz_clone(z2);
1439  a->neg = 0;
1440  return a;
1441  } else if (z2->len == 0) {
1442  mpz_t *a = mpz_clone(z1);
1443  a->neg = 0;
1444  return a;
1445  }
1446 
1447  mpz_t *a = mpz_clone(z1);
1448  mpz_t *b = mpz_clone(z2);
1449  mpz_t c; mpz_init_zero(&c);
1450  a->neg = 0;
1451  b->neg = 0;
1452 
1453  for (;;) {
1454  if (mpz_cmp(a, b) < 0) {
1455  if (a->len == 0) {
1456  mpz_free(a);
1457  mpz_deinit(&c);
1458  return b;
1459  }
1460  mpz_t *t = a; a = b; b = t;
1461  }
1462  if (!(b->len >= 2 || (b->len == 1 && b->dig[0] > 1))) { // compute b > 0; could be mpz_cmp_small_int(b, 1) > 0
1463  break;
1464  }
1465  mpz_set(&c, b);
1466  do {
1467  mpz_add_inpl(&c, &c, &c);
1468  } while (mpz_cmp(&c, a) <= 0);
1469  c.len = mpn_shr(c.dig, c.dig, c.len, 1);
1470  mpz_sub_inpl(a, a, &c);
1471  }
1472 
1473  mpz_deinit(&c);
1474 
1475  if (b->len == 1 && b->dig[0] == 1) { // compute b == 1; could be mpz_cmp_small_int(b, 1) == 0
1476  mpz_free(a);
1477  return b;
1478  } else {
1479  mpz_free(b);
1480  return a;
1481  }
1482 }
1483 
1484 /* computes lcm(z1, z2)
1485  = abs(z1) / gcd(z1, z2) * abs(z2)
1486  lcm(z1, z1) >= 0
1487  lcm(0, 0) = 0
1488  lcm(z, 0) = 0
1489 */
1490 mpz_t *mpz_lcm(const mpz_t *z1, const mpz_t *z2) {
1491  if (z1->len == 0 || z2->len == 0) {
1492  return mpz_zero();
1493  }
1494 
1495  mpz_t *gcd = mpz_gcd(z1, z2);
1496  mpz_t *quo = mpz_zero();
1497  mpz_t *rem = mpz_zero();
1498  mpz_divmod_inpl(quo, rem, z1, gcd);
1499  mpz_mul_inpl(rem, quo, z2);
1500  mpz_free(gcd);
1501  mpz_free(quo);
1502  rem->neg = 0;
1503  return rem;
1504 }
1505 #endif
1506 
1507 /* computes new integers in quo and rem such that:
1508  quo * rhs + rem = lhs
1509  0 <= rem < rhs
1510  can have lhs, rhs the same
1511  assumes rhs != 0 (undefined behaviour if it is)
1512 */
1513 void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs) {
1514  assert(!mpz_is_zero(rhs));
1515 
1516  mpz_need_dig(dest_quo, lhs->len + 1); // +1 necessary?
1517  memset(dest_quo->dig, 0, (lhs->len + 1) * sizeof(mpz_dig_t));
1518  dest_quo->len = 0;
1519  mpz_need_dig(dest_rem, lhs->len + 1); // +1 necessary?
1520  mpz_set(dest_rem, lhs);
1521  mpn_div(dest_rem->dig, &dest_rem->len, rhs->dig, rhs->len, dest_quo->dig, &dest_quo->len);
1522 
1523  // check signs and do Python style modulo
1524  if (lhs->neg != rhs->neg) {
1525  dest_quo->neg = 1;
1526  if (!mpz_is_zero(dest_rem)) {
1527  mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
1528  mpz_add_inpl(dest_quo, dest_quo, &mpzone);
1529  mpz_add_inpl(dest_rem, dest_rem, rhs);
1530  }
1531  }
1532 }
1533 
1534 #if 0
1535 these functions are unused
1536 
1537 /* computes floor(lhs / rhs)
1538  can have lhs, rhs the same
1539 */
1540 mpz_t *mpz_div(const mpz_t *lhs, const mpz_t *rhs) {
1541  mpz_t *quo = mpz_zero();
1542  mpz_t rem; mpz_init_zero(&rem);
1543  mpz_divmod_inpl(quo, &rem, lhs, rhs);
1544  mpz_deinit(&rem);
1545  return quo;
1546 }
1547 
1548 /* computes lhs % rhs ( >= 0)
1549  can have lhs, rhs the same
1550 */
1551 mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs) {
1552  mpz_t quo; mpz_init_zero(&quo);
1553  mpz_t *rem = mpz_zero();
1554  mpz_divmod_inpl(&quo, rem, lhs, rhs);
1555  mpz_deinit(&quo);
1556  return rem;
1557 }
1558 #endif
1559 
1560 // must return actual int value if it fits in mp_int_t
1562  mp_int_t val = 0;
1563  mpz_dig_t *d = z->dig + z->len;
1564 
1565  while (d-- > z->dig) {
1566  val = (val << DIG_SIZE) | *d;
1567  }
1568 
1569  if (z->neg != 0) {
1570  val = -val;
1571  }
1572 
1573  return val;
1574 }
1575 
1576 bool mpz_as_int_checked(const mpz_t *i, mp_int_t *value) {
1577  mp_uint_t val = 0;
1578  mpz_dig_t *d = i->dig + i->len;
1579 
1580  while (d-- > i->dig) {
1581  if (val > (~(WORD_MSBIT_HIGH) >> DIG_SIZE)) {
1582  // will overflow
1583  return false;
1584  }
1585  val = (val << DIG_SIZE) | *d;
1586  }
1587 
1588  if (i->neg != 0) {
1589  val = -val;
1590  }
1591 
1592  *value = val;
1593  return true;
1594 }
1595 
1596 bool mpz_as_uint_checked(const mpz_t *i, mp_uint_t *value) {
1597  if (i->neg != 0) {
1598  // can't represent signed values
1599  return false;
1600  }
1601 
1602  mp_uint_t val = 0;
1603  mpz_dig_t *d = i->dig + i->len;
1604 
1605  while (d-- > i->dig) {
1606  if (val > (~(WORD_MSBIT_HIGH) >> (DIG_SIZE - 1))) {
1607  // will overflow
1608  return false;
1609  }
1610  val = (val << DIG_SIZE) | *d;
1611  }
1612 
1613  *value = val;
1614  return true;
1615 }
1616 
1617 // writes at most len bytes to buf (so buf should be zeroed before calling)
1618 void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf) {
1619  byte *b = buf;
1620  if (big_endian) {
1621  b += len;
1622  }
1623  mpz_dig_t *zdig = z->dig;
1624  int bits = 0;
1625  mpz_dbl_dig_t d = 0;
1626  mpz_dbl_dig_t carry = 1;
1627  for (size_t zlen = z->len; zlen > 0; --zlen) {
1628  bits += DIG_SIZE;
1629  d = (d << DIG_SIZE) | *zdig++;
1630  for (; bits >= 8; bits -= 8, d >>= 8) {
1631  mpz_dig_t val = d;
1632  if (z->neg) {
1633  val = (~val & 0xff) + carry;
1634  carry = val >> 8;
1635  }
1636  if (big_endian) {
1637  *--b = val;
1638  if (b == buf) {
1639  return;
1640  }
1641  } else {
1642  *b++ = val;
1643  if (b == buf + len) {
1644  return;
1645  }
1646  }
1647  }
1648  }
1649 }
1650 
1651 #if MICROPY_PY_BUILTINS_FLOAT
1652 mp_float_t mpz_as_float(const mpz_t *i) {
1653  mp_float_t val = 0;
1654  mpz_dig_t *d = i->dig + i->len;
1655 
1656  while (d-- > i->dig) {
1657  val = val * DIG_BASE + *d;
1658  }
1659 
1660  if (i->neg != 0) {
1661  val = -val;
1662  }
1663 
1664  return val;
1665 }
1666 #endif
1667 
1668 #if 0
1669 this function is unused
1670 char *mpz_as_str(const mpz_t *i, unsigned int base) {
1671  char *s = m_new(char, mp_int_format_size(mpz_max_num_bits(i), base, NULL, '\0'));
1672  mpz_as_str_inpl(i, base, NULL, 'a', '\0', s);
1673  return s;
1674 }
1675 #endif
1676 
1677 // assumes enough space as calculated by mp_int_format_size
1678 // returns length of string, not including null byte
1679 size_t mpz_as_str_inpl(const mpz_t *i, unsigned int base, const char *prefix, char base_char, char comma, char *str) {
1680  if (str == NULL) {
1681  return 0;
1682  }
1683  if (base < 2 || base > 32) {
1684  str[0] = 0;
1685  return 0;
1686  }
1687 
1688  size_t ilen = i->len;
1689 
1690  char *s = str;
1691  if (ilen == 0) {
1692  if (prefix) {
1693  while (*prefix)
1694  *s++ = *prefix++;
1695  }
1696  *s++ = '0';
1697  *s = '\0';
1698  return s - str;
1699  }
1700 
1701  // make a copy of mpz digits, so we can do the div/mod calculation
1702  mpz_dig_t *dig = m_new(mpz_dig_t, ilen);
1703  memcpy(dig, i->dig, ilen * sizeof(mpz_dig_t));
1704 
1705  // convert
1706  char *last_comma = str;
1707  bool done;
1708  do {
1709  mpz_dig_t *d = dig + ilen;
1710  mpz_dbl_dig_t a = 0;
1711 
1712  // compute next remainder
1713  while (--d >= dig) {
1714  a = (a << DIG_SIZE) | *d;
1715  *d = a / base;
1716  a %= base;
1717  }
1718 
1719  // convert to character
1720  a += '0';
1721  if (a > '9') {
1722  a += base_char - '9' - 1;
1723  }
1724  *s++ = a;
1725 
1726  // check if number is zero
1727  done = true;
1728  for (d = dig; d < dig + ilen; ++d) {
1729  if (*d != 0) {
1730  done = false;
1731  break;
1732  }
1733  }
1734  if (comma && (s - last_comma) == 3) {
1735  *s++ = comma;
1736  last_comma = s;
1737  }
1738  }
1739  while (!done);
1740 
1741  // free the copy of the digits array
1742  m_del(mpz_dig_t, dig, ilen);
1743 
1744  if (prefix) {
1745  const char *p = &prefix[strlen(prefix)];
1746  while (p > prefix) {
1747  *s++ = *--p;
1748  }
1749  }
1750  if (i->neg != 0) {
1751  *s++ = '-';
1752  }
1753 
1754  // reverse string
1755  for (char *u = str, *v = s - 1; u < v; ++u, --v) {
1756  char temp = *u;
1757  *u = *v;
1758  *v = temp;
1759  }
1760 
1761  *s = '\0'; // null termination
1762 
1763  return s - str;
1764 }
1765 
1766 #endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
#define exp(x)
Definition: math.h:176
void mpz_deinit(mpz_t *z)
Definition: mpz.c:670
void mpz_init_zero(mpz_t *z)
Definition: mpz.c:648
intptr_t mp_int_t
Definition: mpconfigport.h:73
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1288
void * memset(void *b, int c, size_t len)
Definition: memset.c:3
STATIC size_t mpn_shr(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mp_uint_t n)
Definition: mpz.c:119
#define assert(e)
Definition: assert.h:9
#define DIG_SIZE
Definition: mpz.c:34
void mpz_set_from_ll(mpz_t *z, long long val, bool is_signed)
Definition: mpz.c:782
#define m_del(type, ptr, num)
Definition: misc.h:77
void mpz_set_from_bytes(mpz_t *z, bool big_endian, size_t len, const byte *buf)
Definition: mpz.c:909
#define DIG_MSB
Definition: mpz.c:36
void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1326
STATIC size_t mpn_and_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen, mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk)
Definition: mpz.c:230
STATIC size_t mpn_or_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen, mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk)
Definition: mpz.c:321
size_t mpz_set_from_str(mpz_t *z, const char *str, size_t len, bool neg, unsigned int base)
Definition: mpz.c:873
void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1171
size_t len
Definition: mpz.h:93
STATIC size_t mpn_sub(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen)
Definition: mpz.c:181
void mpz_pow3_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs, const mpz_t *mod)
Definition: mpz.c:1392
#define WORD_MSBIT_HIGH
Definition: mpconfig.h:1189
#define STATIC
Definition: mpconfig.h:1178
#define DIG_BASE
Definition: mpz.c:37
void mpz_set_from_int(mpz_t *z, mp_int_t val)
Definition: mpz.c:758
size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma)
Definition: objint.c:207
mp_int_t mpz_hash(const mpz_t *z)
Definition: mpz.c:1561
void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf)
Definition: mpz.c:1618
void mpz_init_from_int(mpz_t *z, mp_int_t val)
Definition: mpz.c:656
STATIC size_t mpn_mul(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mpz_dig_t *kdig, size_t klen)
Definition: mpz.c:427
Definition: mpz.h:89
c(generic_all_nodes)
mpz_dig_t * dig
Definition: mpz.h:94
STATIC size_t mpn_remove_trailing_zeros(mpz_dig_t *oidig, mpz_dig_t *idig)
Definition: mpz.c:52
uint16_t mpz_dig_t
Definition: mpz.h:62
#define m_del_obj(type, ptr)
Definition: misc.h:80
size_t strlen(const char *s)
Definition: strlen.c:3
#define MIN_ALLOC
Definition: mpz.c:646
#define DIG_MASK
Definition: mpz.c:35
#define MPZ_NUM_DIG_FOR_LL
Definition: mpz.h:87
unsigned int uint32_t
Definition: stdint.h:6
#define NULL
Definition: stddef.h:4
void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs)
Definition: mpz.c:1133
void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1220
void mpz_neg_inpl(mpz_t *dest, const mpz_t *z)
Definition: mpz.c:1086
unsigned long long uint64_t
Definition: stdint.h:7
STATIC size_t mpn_xor_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen, mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk)
Definition: mpz.c:380
int32_t mpz_dbl_dig_signed_t
Definition: mpz.h:64
void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1192
STATIC size_t mpn_mul_dig_add_dig(mpz_dig_t *idig, size_t ilen, mpz_dig_t dmul, mpz_dig_t dadd)
Definition: mpz.c:405
void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1358
STATIC void mpz_need_dig(mpz_t *z, size_t need)
Definition: mpz.c:719
#define is_signed(typecode)
Definition: binary.c:186
void mpz_abs_inpl(mpz_t *dest, const mpz_t *z)
Definition: mpz.c:1076
unsigned char byte
Definition: misc.h:37
size_t alloc
Definition: mpz.h:92
void mpz_set(mpz_t *dest, const mpz_t *src)
Definition: mpz.c:751
int mpz_cmp(const mpz_t *z1, const mpz_t *z2)
Definition: mpz.c:958
#define m_renew(type, ptr, old_num, new_num)
Definition: misc.h:75
void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs)
Definition: mpz.c:1120
STATIC int mpn_cmp(const mpz_dig_t *idig, size_t ilen, const mpz_dig_t *jdig, size_t jlen)
Definition: mpz.c:62
size_t fixed_dig
Definition: mpz.h:91
void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1254
STATIC size_t mpn_add(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen)
Definition: mpz.c:151
void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, size_t alloc, mp_int_t val)
Definition: mpz.c:661
bool mpz_as_int_checked(const mpz_t *i, mp_int_t *value)
Definition: mpz.c:1576
STATIC size_t mpn_shl(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mp_uint_t n)
Definition: mpz.c:80
#define MPZ_NUM_DIG_FOR_INT
Definition: mpz.h:86
uint32_t mpz_dbl_dig_t
Definition: mpz.h:63
STATIC void mpz_free(mpz_t *z)
Definition: mpz.c:712
STATIC mpz_t * mpz_clone(const mpz_t *src)
Definition: mpz.c:733
size_t neg
Definition: mpz.h:90
#define m_new_obj(type)
Definition: misc.h:60
STATIC void mpn_div(mpz_dig_t *num_dig, size_t *num_len, const mpz_dig_t *den_dig, size_t den_len, mpz_dig_t *quo_dig, size_t *quo_len)
Definition: mpz.c:458
bool mpz_as_uint_checked(const mpz_t *i, mp_uint_t *value)
Definition: mpz.c:1596
void * memcpy(void *dst, const void *src, size_t n)
Definition: memcpy.c:3
size_t mpz_as_str_inpl(const mpz_t *i, unsigned int base, const char *prefix, char base_char, char comma, char *str)
Definition: mpz.c:1679
void mpz_not_inpl(mpz_t *dest, const mpz_t *z)
Definition: mpz.c:1096
#define m_new(type, num)
Definition: misc.h:57
void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1513