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

« back to all changes in this revision

Viewing changes to gmp3/mpf/div.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
/* mpf_div -- Divide two floats.
 
2
 
 
3
Copyright 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
mpf_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] & MP_LIMB_T_HIGHBIT))
 
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
 
 
94
      /* Shift up the divisor setting the most significant bit of
 
95
         the most significant limb.  Use temporary storage not to clobber
 
96
         the original contents of the divisor.  */
 
97
      tmp = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
 
98
      mpn_lshift (tmp, vp, vsize, normalization_steps);
 
99
      vp = tmp;
 
100
 
 
101
      /* Shift up the dividend, possibly introducing a new most
 
102
         significant word.  Move the shifted dividend in the remainder
 
103
         at the same time.  */
 
104
      nlimb = mpn_lshift (rtp, up, usize, normalization_steps);
 
105
      if (nlimb != 0)
 
106
        {
 
107
          rtp[usize] = nlimb;
 
108
          tsize++;
 
109
          rexp++;
 
110
        }
 
111
    }
 
112
  else
 
113
    {
 
114
      /* The divisor is already normalized, as required.
 
115
         Copy it to temporary space if it overlaps with the quotient.  */
 
116
      if (vp - rp <= tsize - vsize)
 
117
        {
 
118
          mp_ptr tmp = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
 
119
          MPN_COPY (tmp, vp, vsize);
 
120
          vp = (mp_srcptr) tmp;
 
121
        }
 
122
 
 
123
      /* Move the dividend to the remainder.  */
 
124
      MPN_COPY (rtp, up, usize);
 
125
    }
 
126
 
 
127
  q_limb = mpn_divmod (rp, tp, tsize, vp, vsize);
 
128
  rsize = tsize - vsize;
 
129
  if (q_limb)
 
130
    {
 
131
      rp[rsize] = q_limb;
 
132
      rsize++;
 
133
      rexp++;
 
134
    }
 
135
 
 
136
  r->_mp_size = sign_quotient >= 0 ? rsize : -rsize;
 
137
  r->_mp_exp = rexp;
 
138
  TMP_FREE (marker);
 
139
}