1 /* mpz_tdiv_qr(quot,rem,dividend,divisor) -- Set QUOT to DIVIDEND/DIVISOR,
2 and REM to DIVIDEND mod DIVISOR.
4 Copyright 1991, 1993, 1994, 2000, 2001, 2005 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 3 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. If not, see http://www.gnu.org/licenses/. */
30 mpz_tdiv_qr (mpz_ptr quot, mpz_ptr rem, mpz_srcptr num, mpz_srcptr den)
31 #else /* BERKELEY_MP */
32 mdiv (mpz_srcptr num, mpz_srcptr den, mpz_ptr quot, mpz_ptr rem)
33 #endif /* BERKELEY_MP */
36 mp_size_t ns, ds, nl, dl;
37 mp_ptr np, dp, qp, rp;
49 MPZ_REALLOC (rem, dl);
58 MPN_COPY (rp, np, nl);
59 SIZ (rem) = SIZ (num);
61 /* This needs to follow the assignment to rem, in case the
62 numerator and quotient are the same. */
67 MPZ_REALLOC (quot, ql);
75 /* FIXME: We should think about how to handle the temporary allocation.
76 Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to
77 allocate temp space. */
79 /* Copy denominator to temporary space if it overlaps with the quotient
81 if (dp == rp || dp == qp)
84 tp = TMP_ALLOC_LIMBS (dl);
85 MPN_COPY (tp, dp, dl);
88 /* Copy numerator to temporary space if it overlaps with the quotient or
90 if (np == rp || np == qp)
93 tp = TMP_ALLOC_LIMBS (nl);
94 MPN_COPY (tp, np, nl);
98 mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);
100 ql -= qp[ql - 1] == 0;
101 MPN_NORMALIZE (rp, dl);
103 SIZ (quot) = (ns ^ ds) >= 0 ? ql : -ql;
104 SIZ (rem) = ns >= 0 ? dl : -dl;