1 /* mpz_tdiv_qr(quot,rem,dividend,divisor) -- Set QUOT to DIVIDEND/DIVISOR,
2 and REM to DIVIDEND mod DIVISOR.
4 Copyright (C) 1991, 1993, 1994, 2000 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
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 2.1 of the License, or (at your
11 option) any later version.
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.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
35 mpz_tdiv_qr (mpz_ptr quot, mpz_ptr rem, mpz_srcptr num, mpz_srcptr den)
37 mpz_tdiv_qr (quot, rem, num, den)
44 #else /* BERKELEY_MP */
48 mdiv (mpz_srcptr num, mpz_srcptr den, mpz_ptr quot, mpz_ptr rem)
50 mdiv (num, den, quot, rem)
57 #endif /* BERKELEY_MP */
60 mp_size_t ns, ds, nl, dl;
61 mp_ptr np, dp, qp, rp;
73 MPZ_REALLOC (rem, dl);
82 MPN_COPY (rp, np, nl);
83 SIZ (rem) = SIZ (num);
85 /* This needs to follow the assignment to rem, in case the
86 numerator and quotient are the same. */
91 MPZ_REALLOC (quot, ql);
99 /* FIXME: We should think about how to handle the temporary allocation.
100 Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to
101 allocate temp space. */
103 /* Copy denominator to temporary space if it overlaps with the quotient
105 if (dp == rp || dp == qp)
108 tp = (mp_ptr) TMP_ALLOC (dl * BYTES_PER_MP_LIMB);
109 MPN_COPY (tp, dp, dl);
112 /* Copy numerator to temporary space if it overlaps with the quotient or
114 if (np == rp || np == qp)
117 tp = (mp_ptr) TMP_ALLOC (nl * BYTES_PER_MP_LIMB);
118 MPN_COPY (tp, np, nl);
122 mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);
124 ql -= qp[ql - 1] == 0;
125 MPN_NORMALIZE (rp, dl);
127 SIZ (quot) = (ns ^ ds) >= 0 ? ql : -ql;
128 SIZ (rem) = ns >= 0 ? dl : -dl;