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 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. */
32
mpz_tdiv_qr (mpz_ptr quot, mpz_ptr rem, mpz_srcptr num, mpz_srcptr den)
33
#else /* BERKELEY_MP */
34
mdiv (mpz_srcptr num, mpz_srcptr den, mpz_ptr quot, mpz_ptr rem)
35
#endif /* BERKELEY_MP */
38
mp_size_t ns, ds, nl, dl;
39
mp_ptr np, dp, qp, rp;
51
MPZ_REALLOC (rem, dl);
60
MPN_COPY (rp, np, nl);
61
SIZ (rem) = SIZ (num);
63
/* This needs to follow the assignment to rem, in case the
64
numerator and quotient are the same. */
69
MPZ_REALLOC (quot, ql);
77
/* FIXME: We should think about how to handle the temporary allocation.
78
Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to
79
allocate temp space. */
81
/* Copy denominator to temporary space if it overlaps with the quotient
83
if (dp == rp || dp == qp)
86
tp = (mp_ptr) TMP_ALLOC (dl * BYTES_PER_MP_LIMB);
87
MPN_COPY (tp, dp, dl);
90
/* Copy numerator to temporary space if it overlaps with the quotient or
92
if (np == rp || np == qp)
95
tp = (mp_ptr) TMP_ALLOC (nl * BYTES_PER_MP_LIMB);
96
MPN_COPY (tp, np, nl);
100
mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);
102
ql -= qp[ql - 1] == 0;
103
MPN_NORMALIZE (rp, dl);
105
SIZ (quot) = (ns ^ ds) >= 0 ? ql : -ql;
106
SIZ (rem) = ns >= 0 ? dl : -dl;