1 /* mpz_divexact -- finds quotient when known that quot * den == num && den != 0. 2 3 Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2005, 4 2006, 2007 Free Software Foundation, Inc. 5 6 This file is part of the GNU MP Library. 7 8 The GNU MP Library is free software; you can redistribute it and/or modify 9 it under the terms of the GNU Lesser General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or (at your 11 option) any later version. 12 13 The GNU MP Library is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16 License for more details. 17 18 You should have received a copy of the GNU Lesser General Public License 19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 20 21 /* Ken Weber (kweber@mat.ufrgs.br, kweber@mcs.kent.edu) 22 23 Funding for this work has been partially provided by Conselho Nacional 24 de Desenvolvimento Cienti'fico e Tecnolo'gico (CNPq) do Brazil, Grant 25 301314194-2, and was done while I was a visiting reseacher in the Instituto 26 de Matema'tica at Universidade Federal do Rio Grande do Sul (UFRGS). 27 28 References: 29 T. Jebelean, An algorithm for exact division, Journal of Symbolic 30 Computation, v. 15, 1993, pp. 169-180. */ 31 32 #include "gmp.h" 33 #include "gmp-impl.h" 34 #include "longlong.h" 35 36 void 37 mpz_divexact (mpz_ptr quot, mpz_srcptr num, mpz_srcptr den) 38 { 39 mp_ptr qp, tp; 40 mp_size_t qsize, tsize; 41 mp_srcptr np, dp; 42 mp_size_t nsize, dsize; 43 TMP_DECL; 44 45 #if WANT_ASSERT 46 { 47 mpz_t rem; 48 mpz_init (rem); 49 mpz_tdiv_r (rem, num, den); 50 ASSERT (SIZ(rem) == 0); 51 mpz_clear (rem); 52 } 53 #endif 54 55 nsize = ABS (num->_mp_size); 56 dsize = ABS (den->_mp_size); 57 58 qsize = nsize - dsize + 1; 59 if (quot->_mp_alloc < qsize) 60 _mpz_realloc (quot, qsize); 61 62 np = num->_mp_d; 63 dp = den->_mp_d; 64 qp = quot->_mp_d; 65 66 if (nsize < dsize) 67 { 68 /* This special case avoids segfaults below when the function is 69 incorrectly called with |N| < |D|, N != 0. It also handles the 70 well-defined case N = 0. */ 71 quot->_mp_size = 0; 72 return; 73 } 74 75 if (dsize <= 1) 76 { 77 if (dsize == 1) 78 { 79 MPN_DIVREM_OR_DIVEXACT_1 (qp, np, nsize, dp[0]); 80 qsize -= qp[qsize - 1] == 0; 81 quot->_mp_size = (num->_mp_size ^ den->_mp_size) >= 0 ? qsize : -qsize; 82 return; 83 } 84 85 /* Generate divide-by-zero error since dsize == 0. */ 86 DIVIDE_BY_ZERO; 87 } 88 89 /* Avoid quadratic behaviour, but do it conservatively. */ 90 if (qsize > 1500) 91 { 92 mpz_tdiv_q (quot, num, den); 93 return; 94 } 95 96 TMP_MARK; 97 98 /* QUOT <-- NUM/2^r, T <-- DEN/2^r where = r number of twos in DEN. */ 99 while (dp[0] == 0) 100 np += 1, nsize -= 1, dp += 1, dsize -= 1; 101 tsize = MIN (qsize, dsize); 102 if ((dp[0] & 1) != 0) 103 { 104 if (quot == den) /* QUOT and DEN overlap. */ 105 { 106 tp = (mp_ptr) TMP_ALLOC (tsize * BYTES_PER_MP_LIMB); 107 MPN_COPY (tp, dp, tsize); 108 } 109 else 110 tp = (mp_ptr) dp; 111 if (qp != np) 112 MPN_COPY_INCR (qp, np, qsize); 113 } 114 else 115 { 116 unsigned int r; 117 tp = (mp_ptr) TMP_ALLOC (tsize * BYTES_PER_MP_LIMB); 118 count_trailing_zeros (r, dp[0]); 119 mpn_rshift (tp, dp, tsize, r); 120 if (dsize > tsize) 121 tp[tsize - 1] |= (dp[tsize] << (GMP_NUMB_BITS - r)) & GMP_NUMB_MASK; 122 mpn_rshift (qp, np, qsize, r); 123 if (nsize > qsize) 124 qp[qsize - 1] |= (np[qsize] << (GMP_NUMB_BITS - r)) & GMP_NUMB_MASK; 125 } 126 127 /* Now QUOT <-- QUOT/T. */ 128 mpn_bdivmod (qp, qp, qsize, tp, tsize, qsize * GMP_NUMB_BITS); 129 MPN_NORMALIZE (qp, qsize); 130 131 quot->_mp_size = (num->_mp_size ^ den->_mp_size) >= 0 ? qsize : -qsize; 132 133 TMP_FREE; 134 } 135