[BACK]Return to remove.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpz

Annotation of OpenXM_contrib/gmp/mpz/remove.c, Revision 1.1.1.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>