1 | /* mpz_tdiv_qr(quot,rem,dividend,divisor) -- Set QUOT to DIVIDEND/DIVISOR, |
---|
2 | and REM to DIVIDEND mod DIVISOR. |
---|
3 | |
---|
4 | Copyright 1991, 1993, 1994, 2000, 2001 Free Software Foundation, Inc. |
---|
5 | |
---|
6 | This file is part of the GNU MP Library. |
---|
7 | |
---|
8 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU Lesser General Public License as published by |
---|
10 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
11 | option) any later version. |
---|
12 | |
---|
13 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
14 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
15 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
16 | License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU Lesser General Public License |
---|
19 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
20 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
21 | MA 02111-1307, USA. */ |
---|
22 | |
---|
23 | #include "gmp.h" |
---|
24 | #include "gmp-impl.h" |
---|
25 | #include "longlong.h" |
---|
26 | #ifdef BERKELEY_MP |
---|
27 | #include "mp.h" |
---|
28 | #endif |
---|
29 | |
---|
30 | void |
---|
31 | #ifndef BERKELEY_MP |
---|
32 | mpz_tdiv_qr (mpz_ptr quot, mpz_ptr rem, mpz_srcptr num, mpz_srcptr den) |
---|
33 | #else /* BERKELEY_MP */ |
---|
34 | mdiv (mpz_srcptr num, mpz_srcptr den, mpz_ptr quot, mpz_ptr rem) |
---|
35 | #endif /* BERKELEY_MP */ |
---|
36 | { |
---|
37 | mp_size_t ql; |
---|
38 | mp_size_t ns, ds, nl, dl; |
---|
39 | mp_ptr np, dp, qp, rp; |
---|
40 | TMP_DECL (marker); |
---|
41 | |
---|
42 | ns = SIZ (num); |
---|
43 | ds = SIZ (den); |
---|
44 | nl = ABS (ns); |
---|
45 | dl = ABS (ds); |
---|
46 | ql = nl - dl + 1; |
---|
47 | |
---|
48 | if (dl == 0) |
---|
49 | DIVIDE_BY_ZERO; |
---|
50 | |
---|
51 | MPZ_REALLOC (rem, dl); |
---|
52 | |
---|
53 | if (ql <= 0) |
---|
54 | { |
---|
55 | if (num != rem) |
---|
56 | { |
---|
57 | mp_ptr np, rp; |
---|
58 | np = PTR (num); |
---|
59 | rp = PTR (rem); |
---|
60 | MPN_COPY (rp, np, nl); |
---|
61 | SIZ (rem) = SIZ (num); |
---|
62 | } |
---|
63 | /* This needs to follow the assignment to rem, in case the |
---|
64 | numerator and quotient are the same. */ |
---|
65 | SIZ (quot) = 0; |
---|
66 | return; |
---|
67 | } |
---|
68 | |
---|
69 | MPZ_REALLOC (quot, ql); |
---|
70 | |
---|
71 | TMP_MARK (marker); |
---|
72 | qp = PTR (quot); |
---|
73 | rp = PTR (rem); |
---|
74 | np = PTR (num); |
---|
75 | dp = PTR (den); |
---|
76 | |
---|
77 | /* FIXME: We should think about how to handle the temporary allocation. |
---|
78 | Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to |
---|
79 | allocate temp space. */ |
---|
80 | |
---|
81 | /* Copy denominator to temporary space if it overlaps with the quotient |
---|
82 | or remainder. */ |
---|
83 | if (dp == rp || dp == qp) |
---|
84 | { |
---|
85 | mp_ptr tp; |
---|
86 | tp = (mp_ptr) TMP_ALLOC (dl * BYTES_PER_MP_LIMB); |
---|
87 | MPN_COPY (tp, dp, dl); |
---|
88 | dp = tp; |
---|
89 | } |
---|
90 | /* Copy numerator to temporary space if it overlaps with the quotient or |
---|
91 | remainder. */ |
---|
92 | if (np == rp || np == qp) |
---|
93 | { |
---|
94 | mp_ptr tp; |
---|
95 | tp = (mp_ptr) TMP_ALLOC (nl * BYTES_PER_MP_LIMB); |
---|
96 | MPN_COPY (tp, np, nl); |
---|
97 | np = tp; |
---|
98 | } |
---|
99 | |
---|
100 | mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl); |
---|
101 | |
---|
102 | ql -= qp[ql - 1] == 0; |
---|
103 | MPN_NORMALIZE (rp, dl); |
---|
104 | |
---|
105 | SIZ (quot) = (ns ^ ds) >= 0 ? ql : -ql; |
---|
106 | SIZ (rem) = ns >= 0 ? dl : -dl; |
---|
107 | TMP_FREE (marker); |
---|
108 | } |
---|