1 /* mpz_tdiv_r(rem, dividend, divisor) -- Set REM to DIVIDEND mod DIVISOR.
3 Copyright 1991, 1993, 1994, 2000, 2001, 2005 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
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 3 of the License, or (at your
10 option) any later version.
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.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
25 mpz_tdiv_r (mpz_ptr rem, mpz_srcptr num, mpz_srcptr den)
28 mp_size_t ns, ds, nl, dl;
29 mp_ptr np, dp, qp, rp;
41 MPZ_REALLOC (rem, dl);
50 MPN_COPY (rp, np, nl);
51 SIZ (rem) = SIZ (num);
57 qp = TMP_ALLOC_LIMBS (ql);
62 /* FIXME: We should think about how to handle the temporary allocation.
63 Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to
64 allocate temp space. */
66 /* Copy denominator to temporary space if it overlaps with the remainder. */
70 tp = TMP_ALLOC_LIMBS (dl);
71 MPN_COPY (tp, dp, dl);
74 /* Copy numerator to temporary space if it overlaps with the remainder. */
78 tp = TMP_ALLOC_LIMBS (nl);
79 MPN_COPY (tp, np, nl);
83 mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);
85 MPN_NORMALIZE (rp, dl);
87 SIZ (rem) = ns >= 0 ? dl : -dl;