OLD | NEW |
| (Empty) |
1 /* mpn_divrem -- Divide natural numbers, producing both remainder and | |
2 quotient. This is now just a middle layer for calling the new | |
3 internal mpn_tdiv_qr. | |
4 | |
5 Copyright 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2005 Free | |
6 Software Foundation, Inc. | |
7 | |
8 This file is part of the GNU MP Library. | |
9 | |
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. | |
14 | |
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. | |
19 | |
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/. */ | |
22 | |
23 #include "gmp.h" | |
24 #include "gmp-impl.h" | |
25 #include "longlong.h" | |
26 | |
27 mp_limb_t | |
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) | |
31 { | |
32 ASSERT (qxn >= 0); | |
33 ASSERT (nn >= dn); | |
34 ASSERT (dn >= 1); | |
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)); | |
39 ASSERT_MPN (np, nn); | |
40 ASSERT_MPN (dp, dn); | |
41 | |
42 if (dn == 1) | |
43 { | |
44 mp_limb_t ret; | |
45 mp_ptr q2p; | |
46 mp_size_t qn; | |
47 TMP_DECL; | |
48 | |
49 TMP_MARK; | |
50 q2p = (mp_ptr) TMP_ALLOC ((nn + qxn) * BYTES_PER_MP_LIMB); | |
51 | |
52 np[0] = mpn_divrem_1 (q2p, qxn, np, nn, dp[0]); | |
53 qn = nn + qxn - 1; | |
54 MPN_COPY (qp, q2p, qn); | |
55 ret = q2p[qn]; | |
56 | |
57 TMP_FREE; | |
58 return ret; | |
59 } | |
60 else if (dn == 2) | |
61 { | |
62 return mpn_divrem_2 (qp, qxn, np, nn, dp); | |
63 } | |
64 else | |
65 { | |
66 mp_ptr rp, q2p; | |
67 mp_limb_t qhl; | |
68 mp_size_t qn; | |
69 TMP_DECL; | |
70 | |
71 TMP_MARK; | |
72 if (UNLIKELY (qxn != 0)) | |
73 { | |
74 mp_ptr n2p; | |
75 n2p = (mp_ptr) TMP_ALLOC ((nn + qxn) * BYTES_PER_MP_LIMB); | |
76 MPN_ZERO (n2p, qxn); | |
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); | |
82 qn = nn - dn + qxn; | |
83 MPN_COPY (qp, q2p, qn); | |
84 qhl = q2p[qn]; | |
85 } | |
86 else | |
87 { | |
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 */ | |
92 qn = nn - dn; | |
93 MPN_COPY (qp, q2p, qn); | |
94 qhl = q2p[qn]; | |
95 } | |
96 TMP_FREE; | |
97 return qhl; | |
98 } | |
99 } | |
OLD | NEW |