186d7f5d3SJohn Marino /* mpz_tdiv_r(rem, dividend, divisor) -- Set REM to DIVIDEND mod DIVISOR.
286d7f5d3SJohn Marino
386d7f5d3SJohn Marino Copyright 1991, 1993, 1994, 2000, 2001, 2005 Free Software Foundation, Inc.
486d7f5d3SJohn Marino
586d7f5d3SJohn Marino This file is part of the GNU MP Library.
686d7f5d3SJohn Marino
786d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
886d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
986d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1086d7f5d3SJohn Marino option) any later version.
1186d7f5d3SJohn Marino
1286d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1386d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1486d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1586d7f5d3SJohn Marino License for more details.
1686d7f5d3SJohn Marino
1786d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
1886d7f5d3SJohn Marino along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
1986d7f5d3SJohn Marino
2086d7f5d3SJohn Marino #include "gmp.h"
2186d7f5d3SJohn Marino #include "gmp-impl.h"
2286d7f5d3SJohn Marino #include "longlong.h"
2386d7f5d3SJohn Marino
2486d7f5d3SJohn Marino void
mpz_tdiv_r(mpz_ptr rem,mpz_srcptr num,mpz_srcptr den)2586d7f5d3SJohn Marino mpz_tdiv_r (mpz_ptr rem, mpz_srcptr num, mpz_srcptr den)
2686d7f5d3SJohn Marino {
2786d7f5d3SJohn Marino mp_size_t ql;
2886d7f5d3SJohn Marino mp_size_t ns, ds, nl, dl;
2986d7f5d3SJohn Marino mp_ptr np, dp, qp, rp;
3086d7f5d3SJohn Marino TMP_DECL;
3186d7f5d3SJohn Marino
3286d7f5d3SJohn Marino ns = SIZ (num);
3386d7f5d3SJohn Marino ds = SIZ (den);
3486d7f5d3SJohn Marino nl = ABS (ns);
3586d7f5d3SJohn Marino dl = ABS (ds);
3686d7f5d3SJohn Marino ql = nl - dl + 1;
3786d7f5d3SJohn Marino
3886d7f5d3SJohn Marino if (dl == 0)
3986d7f5d3SJohn Marino DIVIDE_BY_ZERO;
4086d7f5d3SJohn Marino
4186d7f5d3SJohn Marino MPZ_REALLOC (rem, dl);
4286d7f5d3SJohn Marino
4386d7f5d3SJohn Marino if (ql <= 0)
4486d7f5d3SJohn Marino {
4586d7f5d3SJohn Marino if (num != rem)
4686d7f5d3SJohn Marino {
4786d7f5d3SJohn Marino mp_ptr np, rp;
4886d7f5d3SJohn Marino np = PTR (num);
4986d7f5d3SJohn Marino rp = PTR (rem);
5086d7f5d3SJohn Marino MPN_COPY (rp, np, nl);
5186d7f5d3SJohn Marino SIZ (rem) = SIZ (num);
5286d7f5d3SJohn Marino }
5386d7f5d3SJohn Marino return;
5486d7f5d3SJohn Marino }
5586d7f5d3SJohn Marino
5686d7f5d3SJohn Marino TMP_MARK;
5786d7f5d3SJohn Marino qp = TMP_ALLOC_LIMBS (ql);
5886d7f5d3SJohn Marino rp = PTR (rem);
5986d7f5d3SJohn Marino np = PTR (num);
6086d7f5d3SJohn Marino dp = PTR (den);
6186d7f5d3SJohn Marino
6286d7f5d3SJohn Marino /* FIXME: We should think about how to handle the temporary allocation.
6386d7f5d3SJohn Marino Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to
6486d7f5d3SJohn Marino allocate temp space. */
6586d7f5d3SJohn Marino
6686d7f5d3SJohn Marino /* Copy denominator to temporary space if it overlaps with the remainder. */
6786d7f5d3SJohn Marino if (dp == rp)
6886d7f5d3SJohn Marino {
6986d7f5d3SJohn Marino mp_ptr tp;
7086d7f5d3SJohn Marino tp = TMP_ALLOC_LIMBS (dl);
7186d7f5d3SJohn Marino MPN_COPY (tp, dp, dl);
7286d7f5d3SJohn Marino dp = tp;
7386d7f5d3SJohn Marino }
7486d7f5d3SJohn Marino /* Copy numerator to temporary space if it overlaps with the remainder. */
7586d7f5d3SJohn Marino if (np == rp)
7686d7f5d3SJohn Marino {
7786d7f5d3SJohn Marino mp_ptr tp;
7886d7f5d3SJohn Marino tp = TMP_ALLOC_LIMBS (nl);
7986d7f5d3SJohn Marino MPN_COPY (tp, np, nl);
8086d7f5d3SJohn Marino np = tp;
8186d7f5d3SJohn Marino }
8286d7f5d3SJohn Marino
8386d7f5d3SJohn Marino mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);
8486d7f5d3SJohn Marino
8586d7f5d3SJohn Marino MPN_NORMALIZE (rp, dl);
8686d7f5d3SJohn Marino
8786d7f5d3SJohn Marino SIZ (rem) = ns >= 0 ? dl : -dl;
8886d7f5d3SJohn Marino TMP_FREE;
8986d7f5d3SJohn Marino }
90