~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/mpz/tdiv_q.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpz_tdiv_q -- divide two integers and produce a quotient.
 
2
 
 
3
Copyright 1991, 1993, 1994, 1996, 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_q (mpz_ptr quot, 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
  if (ql <= 0)
 
44
    {
 
45
      SIZ (quot) = 0;
 
46
      return;
 
47
    }
 
48
 
 
49
  MPZ_REALLOC (quot, ql);
 
50
 
 
51
  TMP_MARK (marker);
 
52
  qp = PTR (quot);
 
53
  rp = (mp_ptr) TMP_ALLOC (dl * BYTES_PER_MP_LIMB);
 
54
  np = PTR (num);
 
55
  dp = PTR (den);
 
56
 
 
57
  /* FIXME: We should think about how to handle the temporary allocation.
 
58
     Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to
 
59
     allocate temp space.  */
 
60
 
 
61
  /* Copy denominator to temporary space if it overlaps with the quotient.  */
 
62
  if (dp == qp)
 
63
    {
 
64
      mp_ptr tp;
 
65
      tp = (mp_ptr) TMP_ALLOC (dl * BYTES_PER_MP_LIMB);
 
66
      MPN_COPY (tp, dp, dl);
 
67
      dp = tp;
 
68
    }
 
69
  /* Copy numerator to temporary space if it overlaps with the quotient.  */
 
70
  if (np == qp)
 
71
    {
 
72
      mp_ptr tp;
 
73
      tp = (mp_ptr) TMP_ALLOC (nl * BYTES_PER_MP_LIMB);
 
74
      MPN_COPY (tp, np, nl);
 
75
      np = tp;
 
76
    }
 
77
 
 
78
  mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);
 
79
 
 
80
  ql -=  qp[ql - 1] == 0;
 
81
 
 
82
  SIZ (quot) = (ns ^ ds) >= 0 ? ql : -ql;
 
83
  TMP_FREE (marker);
 
84
}