source: trunk/third/gmp/mpz/tdiv_qr.c @ 18191

Revision 18191, 2.8 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* mpz_tdiv_qr(quot,rem,dividend,divisor) -- Set QUOT to DIVIDEND/DIVISOR,
2   and REM to DIVIDEND mod DIVISOR.
3
4Copyright 1991, 1993, 1994, 2000, 2001 Free Software Foundation, Inc.
5
6This file is part of the GNU MP Library.
7
8The GNU MP Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 2.1 of the License, or (at your
11option) any later version.
12
13The GNU MP Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
20the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21MA 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
30void
31#ifndef BERKELEY_MP
32mpz_tdiv_qr (mpz_ptr quot, mpz_ptr rem, mpz_srcptr num, mpz_srcptr den)
33#else /* BERKELEY_MP */
34mdiv (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}
Note: See TracBrowser for help on using the repository browser.