1 /* mpz_remove -- divide out a factor and return its multiplicity.
3 Copyright 1998-2002, 2012 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
35 mpz_remove (mpz_ptr dest
, mpz_srcptr src
, mpz_srcptr f
)
39 mp_size_t sn
, fn
, afn
;
48 if (UNLIKELY ((afn
<= (fp0
== 1)) /* mpz_cmpabs_ui (f, 1) <= 0 */
51 /* f = 0 or f = +- 1 or src = 0 */
64 dp
= MPZ_REALLOC (dest
, dn
);
66 pwr
= mpn_remove (dp
, &dn
, PTR(src
), dn
, PTR(f
), afn
, ~(mp_bitcnt_t
) 0);
68 SIZ (dest
) = ((pwr
& (fn
< 0)) ^ (sn
< 0)) ? -dn
: dn
;
70 else if (afn
== (fp0
== 2))
71 { /* mpz_cmpabs_ui (f, 2) == 0 */
72 pwr
= mpz_scan1 (src
, 0);
73 mpz_div_2exp (dest
, src
, pwr
);
74 if (pwr
& (fn
< 0)) /*((pwr % 2 == 1) && (SIZ (f) < 0))*/
85 mpz_tdiv_qr (x
, rem
, src
, f
);
88 mpz_t fpow
[GMP_LIMB_BITS
]; /* Really MP_SIZE_T_BITS */
91 #if WANT_ORIGINAL_DEST
95 /* We could perhaps compute mpz_scan1(src,0)/mpz_scan1(f,0). It is an
96 upper bound of the result we're seeking. We could also shift down the
97 operands so that they become odd, to make intermediate values
99 mpz_init_set (fpow
[0], f
);
103 /* Divide by f, f^2 ... f^(2^k) until we get a remainder for f^(2^k). */
104 while (ABSIZ (dest
) >= 2 * ABSIZ (fpow
[p
- 1]) - 1)
107 mpz_mul (fpow
[p
], fpow
[p
- 1], fpow
[p
- 1]);
108 mpz_tdiv_qr (x
, rem
, dest
, fpow
[p
]);
109 if (SIZ (rem
) != 0) {
117 pwr
= ((mp_bitcnt_t
)1 << p
) - 1;
119 /* Divide by f^(2^(k-1)), f^(2^(k-2)), ..., f for all divisors that give
123 mpz_tdiv_qr (x
, rem
, dest
, fpow
[p
]);
126 pwr
+= (mp_bitcnt_t
)1 << p
;
132 #if WANT_ORIGINAL_DEST