xref: /dragonfly/contrib/gmp/mpz/remove.c (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /* mpz_remove -- divide out a factor and return its multiplicity.
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4*86d7f5d3SJohn Marino 
5*86d7f5d3SJohn Marino This file is part of the GNU MP Library.
6*86d7f5d3SJohn Marino 
7*86d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
8*86d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
9*86d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
10*86d7f5d3SJohn Marino option) any later version.
11*86d7f5d3SJohn Marino 
12*86d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
13*86d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14*86d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15*86d7f5d3SJohn Marino License for more details.
16*86d7f5d3SJohn Marino 
17*86d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
18*86d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19*86d7f5d3SJohn Marino 
20*86d7f5d3SJohn Marino #include "gmp.h"
21*86d7f5d3SJohn Marino #include "gmp-impl.h"
22*86d7f5d3SJohn Marino 
23*86d7f5d3SJohn Marino mp_bitcnt_t
mpz_remove(mpz_ptr dest,mpz_srcptr src,mpz_srcptr f)24*86d7f5d3SJohn Marino mpz_remove (mpz_ptr dest, mpz_srcptr src, mpz_srcptr f)
25*86d7f5d3SJohn Marino {
26*86d7f5d3SJohn Marino   mpz_t fpow[GMP_LIMB_BITS];		/* Really MP_SIZE_T_BITS */
27*86d7f5d3SJohn Marino   mpz_t x, rem;
28*86d7f5d3SJohn Marino   mp_bitcnt_t pwr;
29*86d7f5d3SJohn Marino   int p;
30*86d7f5d3SJohn Marino 
31*86d7f5d3SJohn Marino   if (mpz_cmp_ui (f, 1) <= 0)
32*86d7f5d3SJohn Marino     DIVIDE_BY_ZERO;
33*86d7f5d3SJohn Marino 
34*86d7f5d3SJohn Marino   if (SIZ (src) == 0)
35*86d7f5d3SJohn Marino     {
36*86d7f5d3SJohn Marino       if (src != dest)
37*86d7f5d3SJohn Marino         mpz_set (dest, src);
38*86d7f5d3SJohn Marino       return 0;
39*86d7f5d3SJohn Marino     }
40*86d7f5d3SJohn Marino 
41*86d7f5d3SJohn Marino   if (mpz_cmp_ui (f, 2) == 0)
42*86d7f5d3SJohn Marino     {
43*86d7f5d3SJohn Marino       mp_bitcnt_t s0;
44*86d7f5d3SJohn Marino       s0 = mpz_scan1 (src, 0);
45*86d7f5d3SJohn Marino       mpz_div_2exp (dest, src, s0);
46*86d7f5d3SJohn Marino       return s0;
47*86d7f5d3SJohn Marino     }
48*86d7f5d3SJohn Marino 
49*86d7f5d3SJohn Marino   /* We could perhaps compute mpz_scan1(src,0)/mpz_scan1(f,0).  It is an
50*86d7f5d3SJohn Marino      upper bound of the result we're seeking.  We could also shift down the
51*86d7f5d3SJohn Marino      operands so that they become odd, to make intermediate values smaller.  */
52*86d7f5d3SJohn Marino 
53*86d7f5d3SJohn Marino   mpz_init (rem);
54*86d7f5d3SJohn Marino   mpz_init (x);
55*86d7f5d3SJohn Marino 
56*86d7f5d3SJohn Marino   pwr = 0;
57*86d7f5d3SJohn Marino   mpz_init (fpow[0]);
58*86d7f5d3SJohn Marino   mpz_set (fpow[0], f);
59*86d7f5d3SJohn Marino   mpz_set (dest, src);
60*86d7f5d3SJohn Marino 
61*86d7f5d3SJohn Marino   /* Divide by f, f^2, ..., f^(2^k) until we get a remainder for f^(2^k).  */
62*86d7f5d3SJohn Marino   for (p = 0;; p++)
63*86d7f5d3SJohn Marino     {
64*86d7f5d3SJohn Marino       mpz_tdiv_qr (x, rem, dest, fpow[p]);
65*86d7f5d3SJohn Marino       if (SIZ (rem) != 0)
66*86d7f5d3SJohn Marino 	break;
67*86d7f5d3SJohn Marino       mpz_init (fpow[p + 1]);
68*86d7f5d3SJohn Marino       mpz_mul (fpow[p + 1], fpow[p], fpow[p]);
69*86d7f5d3SJohn Marino       mpz_set (dest, x);
70*86d7f5d3SJohn Marino     }
71*86d7f5d3SJohn Marino 
72*86d7f5d3SJohn Marino   pwr = (1L << p) - 1;
73*86d7f5d3SJohn Marino 
74*86d7f5d3SJohn Marino   mpz_clear (fpow[p]);
75*86d7f5d3SJohn Marino 
76*86d7f5d3SJohn Marino   /* Divide by f^(2^(k-1)), f^(2^(k-2)), ..., f for all divisors that give a
77*86d7f5d3SJohn Marino      zero remainder.  */
78*86d7f5d3SJohn Marino   while (--p >= 0)
79*86d7f5d3SJohn Marino     {
80*86d7f5d3SJohn Marino       mpz_tdiv_qr (x, rem, dest, fpow[p]);
81*86d7f5d3SJohn Marino       if (SIZ (rem) == 0)
82*86d7f5d3SJohn Marino 	{
83*86d7f5d3SJohn Marino 	  pwr += 1L << p;
84*86d7f5d3SJohn Marino 	  mpz_set (dest, x);
85*86d7f5d3SJohn Marino 	}
86*86d7f5d3SJohn Marino       mpz_clear (fpow[p]);
87*86d7f5d3SJohn Marino     }
88*86d7f5d3SJohn Marino 
89*86d7f5d3SJohn Marino   mpz_clear (x);
90*86d7f5d3SJohn Marino   mpz_clear (rem);
91*86d7f5d3SJohn Marino   return pwr;
92*86d7f5d3SJohn Marino }
93