1 /* mpn_divrem -- Divide natural numbers, producing both remainder and
2 quotient. This is now just a middle layer for calling the new
5 Copyright 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2005 Free
6 Software Foundation, Inc.
8 This file is part of the GNU MP Library.
10 The GNU MP Library is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
15 The GNU MP Library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 mpn_divrem (mp_ptr qp, mp_size_t qxn,
29 mp_ptr np, mp_size_t nn,
30 mp_srcptr dp, mp_size_t dn)
35 ASSERT (dp[dn-1] & GMP_NUMB_HIGHBIT);
36 ASSERT (! MPN_OVERLAP_P (np, nn, dp, dn));
37 ASSERT (! MPN_OVERLAP_P (qp, nn-dn+qxn, np, nn) || qp==np+dn+qxn);
38 ASSERT (! MPN_OVERLAP_P (qp, nn-dn+qxn, dp, dn));
50 q2p = (mp_ptr) TMP_ALLOC ((nn + qxn) * BYTES_PER_MP_LIMB);
52 np[0] = mpn_divrem_1 (q2p, qxn, np, nn, dp[0]);
54 MPN_COPY (qp, q2p, qn);
62 return mpn_divrem_2 (qp, qxn, np, nn, dp);
72 if (UNLIKELY (qxn != 0))
75 n2p = (mp_ptr) TMP_ALLOC ((nn + qxn) * BYTES_PER_MP_LIMB);
77 MPN_COPY (n2p + qxn, np, nn);
78 q2p = (mp_ptr) TMP_ALLOC ((nn - dn + qxn + 1) * BYTES_PER_MP_LIMB);
79 rp = (mp_ptr) TMP_ALLOC (dn * BYTES_PER_MP_LIMB);
80 mpn_tdiv_qr (q2p, rp, 0L, n2p, nn + qxn, dp, dn);
81 MPN_COPY (np, rp, dn);
83 MPN_COPY (qp, q2p, qn);
88 q2p = (mp_ptr) TMP_ALLOC ((nn - dn + 1) * BYTES_PER_MP_LIMB);
89 rp = (mp_ptr) TMP_ALLOC (dn * BYTES_PER_MP_LIMB);
90 mpn_tdiv_qr (q2p, rp, 0L, np, nn, dp, dn);
91 MPN_COPY (np, rp, dn); /* overwrite np area with remainder */
93 MPN_COPY (qp, q2p, qn);