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