remove empty dir
[ghc-hetmet.git] / ghc / rts / gmp / mpz / tdiv_r.c
1 /* mpz_tdiv_r(rem, dividend, divisor) -- Set REM to DIVIDEND mod DIVISOR.
2
3 Copyright (C) 1991, 1993, 1994, 2000 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 #if __STDC__
28 mpz_tdiv_r (mpz_ptr rem, mpz_srcptr num, mpz_srcptr den)
29 #else
30 mpz_tdiv_r (rem, num, den)
31      mpz_ptr rem;
32      mpz_srcptr num;
33      mpz_srcptr den;
34 #endif
35 {
36   mp_size_t ql;
37   mp_size_t ns, ds, nl, dl;
38   mp_ptr np, dp, qp, rp;
39   TMP_DECL (marker);
40
41   ns = SIZ (num);
42   ds = SIZ (den);
43   nl = ABS (ns);
44   dl = ABS (ds);
45   ql = nl - dl + 1;
46
47   if (dl == 0)
48     DIVIDE_BY_ZERO;
49
50   MPZ_REALLOC (rem, dl);
51
52   if (ql <= 0)
53     {
54       if (num != rem)
55         {
56           mp_ptr np, rp;
57           np = PTR (num);
58           rp = PTR (rem);
59           MPN_COPY (rp, np, nl);
60           SIZ (rem) = SIZ (num);
61         }
62       return;
63     }
64
65   TMP_MARK (marker);
66   qp = (mp_ptr) TMP_ALLOC (ql * BYTES_PER_MP_LIMB);
67   rp = PTR (rem);
68   np = PTR (num);
69   dp = PTR (den);
70
71   /* FIXME: We should think about how to handle the temporary allocation.
72      Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to
73      allocate temp space.  */
74
75   /* Copy denominator to temporary space if it overlaps with the remainder.  */
76   if (dp == rp)
77     {
78       mp_ptr tp;
79       tp = (mp_ptr) TMP_ALLOC (dl * BYTES_PER_MP_LIMB);
80       MPN_COPY (tp, dp, dl);
81       dp = tp;
82     }
83   /* Copy numerator to temporary space if it overlaps with the remainder.  */
84   if (np == rp)
85     {
86       mp_ptr tp;
87       tp = (mp_ptr) TMP_ALLOC (nl * BYTES_PER_MP_LIMB);
88       MPN_COPY (tp, np, nl);
89       np = tp;
90     }
91
92   mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);
93
94   MPN_NORMALIZE (rp, dl);
95
96   SIZ (rem) = ns >= 0 ? dl : -dl;
97   TMP_FREE (marker);
98 }