source: trunk/third/gmp/mpf/div.c @ 18191

Revision 18191, 3.4 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/* mpf_div -- Divide two floats.
2
3Copyright 1993, 1994, 1996, 2000, 2001, 2002 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 02111-1307, USA. */
21
22#include "gmp.h"
23#include "gmp-impl.h"
24#include "longlong.h"
25
26void
27mpf_div (mpf_ptr r, mpf_srcptr u, mpf_srcptr v)
28{
29  mp_srcptr up, vp;
30  mp_ptr rp, tp, rtp;
31  mp_size_t usize, vsize;
32  mp_size_t rsize, tsize;
33  mp_size_t sign_quotient;
34  mp_size_t prec;
35  mp_limb_t q_limb;
36  mp_exp_t rexp;
37  TMP_DECL (marker);
38
39  usize = u->_mp_size;
40  vsize = v->_mp_size;
41  sign_quotient = usize ^ vsize;
42  usize = ABS (usize);
43  vsize = ABS (vsize);
44  prec = r->_mp_prec;
45
46  if (vsize == 0)
47    DIVIDE_BY_ZERO;
48
49  if (usize == 0)
50    {
51      r->_mp_size = 0;
52      r->_mp_exp = 0;
53      return;
54    }
55
56  TMP_MARK (marker);
57  rexp = u->_mp_exp - v->_mp_exp;
58
59  rp = r->_mp_d;
60  up = u->_mp_d;
61  vp = v->_mp_d;
62
63  if (vsize > prec)
64    {
65      vp += vsize - prec;
66      vsize = prec;
67    }
68
69  tsize = vsize + prec;
70  tp = (mp_ptr) TMP_ALLOC ((tsize + 1) * BYTES_PER_MP_LIMB);
71
72  if (usize > tsize)
73    {
74      up += usize - tsize;
75      usize = tsize;
76      rtp = tp;
77    }
78  else
79    {
80      MPN_ZERO (tp, tsize - usize);
81      rtp = tp + (tsize - usize);
82    }
83
84
85  /* Normalize the divisor and the dividend.  */
86  if ((vp[vsize-1] & GMP_NUMB_HIGHBIT) == 0)
87    {
88      mp_ptr tmp;
89      mp_limb_t nlimb;
90      unsigned normalization_steps;
91
92      count_leading_zeros (normalization_steps, vp[vsize - 1]);
93      normalization_steps -= GMP_NAIL_BITS;
94
95      /* Shift up the divisor setting the most significant bit of
96         the most significant limb.  Use temporary storage not to clobber
97         the original contents of the divisor.  */
98      tmp = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
99      mpn_lshift (tmp, vp, vsize, normalization_steps);
100      vp = tmp;
101
102      /* Shift up the dividend, possibly introducing a new most
103         significant word.  Move the shifted dividend in the remainder
104         at the same time.  */
105      nlimb = mpn_lshift (rtp, up, usize, normalization_steps);
106      if (nlimb != 0)
107        {
108          rtp[usize] = nlimb;
109          tsize++;
110          rexp++;
111        }
112    }
113  else
114    {
115      /* The divisor is already normalized, as required.
116         Copy it to temporary space if it overlaps with the quotient.  */
117      if (vp - rp <= tsize - vsize)
118        {
119          mp_ptr tmp = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
120          MPN_COPY (tmp, vp, vsize);
121          vp = (mp_srcptr) tmp;
122        }
123
124      /* Move the dividend to the remainder.  */
125      MPN_COPY (rtp, up, usize);
126    }
127
128  q_limb = mpn_divmod (rp, tp, tsize, vp, vsize);
129  rsize = tsize - vsize;
130  if (q_limb)
131    {
132      rp[rsize] = q_limb;
133      rsize++;
134      rexp++;
135    }
136
137  r->_mp_size = sign_quotient >= 0 ? rsize : -rsize;
138  r->_mp_exp = rexp;
139  TMP_FREE (marker);
140}
Note: See TracBrowser for help on using the repository browser.