1 /* mpn_divrem -- Divide natural numbers, producing both remainder and
2 quotient. This is now just a middle layer calling mpn_tdiv_qr.
4 Copyright 1993-1997, 1999-2002, 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 either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
21 or both in parallel, as here.
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
37 mpn_divrem (mp_ptr qp
, mp_size_t qxn
,
38 mp_ptr np
, mp_size_t nn
,
39 mp_srcptr dp
, mp_size_t dn
)
44 ASSERT (dp
[dn
-1] & GMP_NUMB_HIGHBIT
);
45 ASSERT (! MPN_OVERLAP_P (np
, nn
, dp
, dn
));
46 ASSERT (! MPN_OVERLAP_P (qp
, nn
-dn
+qxn
, np
, nn
) || qp
==np
+dn
+qxn
);
47 ASSERT (! MPN_OVERLAP_P (qp
, nn
-dn
+qxn
, dp
, dn
));
59 q2p
= TMP_ALLOC_LIMBS (nn
+ qxn
);
61 np
[0] = mpn_divrem_1 (q2p
, qxn
, np
, nn
, dp
[0]);
63 MPN_COPY (qp
, q2p
, qn
);
71 return mpn_divrem_2 (qp
, qxn
, np
, nn
, dp
);
81 if (UNLIKELY (qxn
!= 0))
84 n2p
= TMP_ALLOC_LIMBS (nn
+ qxn
);
86 MPN_COPY (n2p
+ qxn
, np
, nn
);
87 q2p
= TMP_ALLOC_LIMBS (nn
- dn
+ qxn
+ 1);
88 rp
= TMP_ALLOC_LIMBS (dn
);
89 mpn_tdiv_qr (q2p
, rp
, 0L, n2p
, nn
+ qxn
, dp
, dn
);
90 MPN_COPY (np
, rp
, dn
);
92 MPN_COPY (qp
, q2p
, qn
);
97 q2p
= TMP_ALLOC_LIMBS (nn
- dn
+ 1);
98 rp
= TMP_ALLOC_LIMBS (dn
);
99 mpn_tdiv_qr (q2p
, rp
, 0L, np
, nn
, dp
, dn
);
100 MPN_COPY (np
, rp
, dn
); /* overwrite np area with remainder */
102 MPN_COPY (qp
, q2p
, qn
);