summaryrefslogtreecommitdiff
path: root/div.c
blob: f48d47fafba80277bb6aa6bf372bc11d0a646142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* mpfr_div -- divide two floating-point numbers

Copyright (C) 1999 Free Software Foundation.

This file is part of the MPFR Library.

The MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

The MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
License for more details.

You should have received a copy of the GNU Library General Public License
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "gmp.h"
#include "mpfr.h"
#include "longlong.h"
#include "gmp-impl.h"

/* #define DEBUG */

void
#if __STDC__
mpfr_div (mpfr_ptr r, mpfr_srcptr u, mpfr_srcptr v, mp_rnd_t rnd_mode)
#else
mpfr_div (r, u, v, rnd_mode)
     mpfr_ptr r;
     mpfr_srcptr u;
     mpfr_srcptr v;
     mp_rnd_t rnd_mode;
#endif
{
  mp_srcptr up, vp;
  mp_ptr rp, tp, tp0, tmp, tmp2;
  mp_size_t usize, vsize, rrsize, oldrrsize;
  mp_size_t rsize;
  mp_size_t sign_quotient;
  mp_size_t prec, err;
  mp_limb_t q_limb;
  mp_exp_t rexp;
  long k, mult, vn, t; 
  unsigned long cc = 0, rw, nw; 
  char can_round = 0; 
  TMP_DECL (marker);

  if (MPFR_IS_NAN(u) || MPFR_IS_NAN(v)) { MPFR_SET_NAN(r); return; }
  if (MPFR_IS_INF(u)) 
    { 
      if (MPFR_IS_INF(v)) 
	{
	  MPFR_SET_NAN(r); return; 
	}
      else
	{ 
	  MPFR_SET_INF(r); 
	  if (MPFR_SIGN(r) != MPFR_SIGN(u) * MPFR_SIGN(v)) 
	    { MPFR_CHANGE_SIGN(r); }
	  return; 
	}
    }
  else
    { 
      if (MPFR_IS_INF(v)) 
	{
	  MPFR_SET_ZERO(r); 
	  if (MPFR_SIGN(r) != MPFR_SIGN(u) * MPFR_SIGN(v)) 
	    { MPFR_CHANGE_SIGN(r); }
	  return; 
	}
    }

  usize = (MPFR_PREC(u) - 1)/BITS_PER_MP_LIMB + 1; 
  vsize = (MPFR_PREC(v) - 1)/BITS_PER_MP_LIMB + 1; 
  sign_quotient = ((MPFR_SIGN(u) * MPFR_SIGN(v) > 0) ? 1 : -1); 
  prec = MPFR_PREC(r);


  if (!MPFR_NOTZERO(v))
    {
      if (!MPFR_NOTZERO(u)) 
	{ MPFR_SET_NAN(r); return; }
      else
	{
	  MPFR_SET_INF(r); 
	  if (MPFR_SIGN(r) != MPFR_SIGN(v) * MPFR_SIGN(u)) 
	    MPFR_CHANGE_SIGN(r); 
	  return;
	}
    }
  
  if (!MPFR_NOTZERO(u)) { MPFR_SET_ZERO(r); return; }

  up = u->_mp_d;
  vp = v->_mp_d;

#ifdef DEBUG
      printf("Entering division : "); 
      for(k = usize - 1; k >= 0; k--) { printf("%lu ", up[k]); }
      printf(" by "); 
      for(k = vsize - 1; k >= 0; k--) { printf("%lu ", vp[k]); }
      printf(".\n"); 
#endif

  /* Compare the mantissas */
  mult = mpn_cmp(up, vp, (usize > vsize ? vsize : usize)); 
  if (mult == 0 && vsize > usize)
    {
      vn = vsize - usize; 
      while (vn >= 0) if (vp[vn--]) { mult = 1; break; }
      /* On peut diagnostiquer ici pour pas cher le cas u = v */
    }
  else { mult = (mult < 0 ? 1 : 0); }

  rsize = (MPFR_PREC(r) + 3)/BITS_PER_MP_LIMB + 1; 
  rrsize = MPFR_PREC(r)/BITS_PER_MP_LIMB + 1;
  /* Three extra bits are needed in order to get the quotient with enough
     precision ; take one extra bit for rrsize in order to solve more 
     easily the problem of rounding to nearest. */

  /* ATTENTION, UMPFR_SIZE DOIT RESTER > A VMPFR_SIZE !!!!!!!! */

  do
    {
      TMP_MARK (marker);

      rexp = u->_mp_exp - v->_mp_exp;
      
      err = rsize*BITS_PER_MP_LIMB; 
      if (rsize < vsize) { err-=2; } 
      if (rsize < usize) { err--; }
      if (err > rrsize * BITS_PER_MP_LIMB) 
	{ err = rrsize * BITS_PER_MP_LIMB; }
      
      tp0 = (mp_ptr) TMP_ALLOC ((rsize+rrsize) * BYTES_PER_MP_LIMB);
      /* fill by zero rrsize low limbs of t */
      MPN_ZERO(tp0, rrsize); tp = tp0 + rrsize; 
      rp = (mp_ptr) TMP_ALLOC ((rrsize+1) * BYTES_PER_MP_LIMB); 

      if (vsize >= rsize) { 
	tmp = (mp_ptr) vp + vsize - rsize;
      }
      else { 
	tmp = (mp_ptr) TMP_ALLOC (rsize * BYTES_PER_MP_LIMB);
	MPN_COPY (tmp + rsize - vsize, vp, vsize);
	MPN_ZERO (tmp, rsize - vsize); 
      }

      if (usize >= rsize) { 
	MPN_COPY (tp, up + usize - rsize, rsize);
      }
      else {
	MPN_COPY (tp + rsize - usize, up, usize); 
	MPN_ZERO (tp, rsize - usize); 
      }

      /* Do the real job */
 
#ifdef DEBUG
      printf("Dividing : "); 
      for(k = rsize - 1; k >= 0; k--) { printf("%lu ", tp[k]); }
      printf(" by "); 
      for(k = rsize - 1; k >= 0; k--) { printf("%lu ", tmp[k]); }
      printf(".\n"); 
#endif

#if (__GNU_MP_VERSION < 3)
      q_limb = mpn_divrem (rp, 0, tp0, rsize+rrsize, tmp, rsize);
      tp = tp0; /* location of remainder */
#else /* mpn_tdiv_qr is the preferred division interface in GMP 3 */
      tmp2 = (mp_ptr) TMP_ALLOC (rsize * BYTES_PER_MP_LIMB);
      mpn_tdiv_qr(rp, tmp2, 0, tp0, rsize+rrsize, tmp, rsize);
      q_limb = rp[rrsize];
      tp = tmp2; /* location of remainder */
#endif

#ifdef DEBUG
      printf("The result is : \n"); 
      printf("Quotient : "); 
      for(k = rrsize - 1; k >= 0; k--) { printf("%lu ", rp[k]); }
      printf("Remainder : "); 
      for(k = rsize - 1; k >= 0; k--) { printf("%lu ", tp[k]); }
      printf("(q_limb = %lu)\n", q_limb); 
#endif
      
      /* msb-normalize the result */
      
      if (q_limb)
	{
	  count_leading_zeros(k, q_limb); 
	  mpn_rshift(rp, rp, rrsize, BITS_PER_MP_LIMB - k);
	  rp[rrsize - 1] |= (q_limb << k);
	  rexp += BITS_PER_MP_LIMB - k; 
	}
      else
	{ 
	  count_leading_zeros(k, rp[rrsize - 1]); 
	  if (k) { mpn_lshift(rp, rp, rrsize, k); }
	  rexp -= k; 
	}

      can_round = (mpfr_can_round_raw(rp, rrsize, sign_quotient, err, 
				     GMP_RNDN, rnd_mode, MPFR_PREC(r))
	|| (usize == rsize && vsize == rsize && 
	    mpfr_can_round_raw(rp, rrsize, sign_quotient, err, 
			       GMP_RNDZ, rnd_mode, MPFR_PREC(r))));

      /* If we used all the limbs of both the dividend and the divisor, 
	 then we have the correct RNDZ rounding */

      if (!can_round && (rsize < usize || rsize < vsize)) 
	{
#ifdef DEBUG
	  printf("Increasing the precision.\n");
#endif
	  TMP_FREE(marker);
	  /* in case we can't round at the first iteration,
	     jump right away to the maximum precision of both
	     operands, to avoid multiple iterations when for 
	     example the divisor is 1.0.
	  */
	  if (rsize < usize) rsize = usize - 1;
	  if (rsize < vsize) rsize = vsize - 1;
	}
    }
  while (!can_round && (rsize < usize || rsize < vsize) 
	 && (rsize++) && (rrsize++));

  /* ON PEUT PROBABLEMENT SE DEBROUILLER DES QUE rsize >= vsize */
  /* MAIS IL FAUT AJOUTER LE BOUT QUI MANQUE DE usize A rsize */
    
  oldrrsize = rrsize;
  rrsize = (MPFR_PREC(r) - 1)/BITS_PER_MP_LIMB + 1;

  if (can_round) 
    {
      cc = mpfr_round_raw(rp, rp, err, (sign_quotient == -1 ? 1 : 0),
			  MPFR_PREC(r), rnd_mode);
    }
  else {
    /* Use the remainder to find out the correct rounding */
    /* Note that at this point the division has been done */
    /* EXACTLY. */
    if ((rnd_mode == GMP_RNDD && sign_quotient == -1) 
	|| (rnd_mode == GMP_RNDU && sign_quotient == 1)
	|| (rnd_mode == GMP_RNDN))
      {
	/* We cannot round, so that the last bits of the quotient
	   have to be zero; just look if the remainder is nonzero */
	k = rsize - 1; 
	while (k >= 0) { if (tp[k]) break; k--; }
	if (k >= 0) 
	  {
	    t = MPFR_PREC(r) & (BITS_PER_MP_LIMB - 1); 
	    if (t)
	      {
		cc = mpn_add_1(rp, rp, rrsize, 
			       (mp_limb_t)1 << (BITS_PER_MP_LIMB - t)); 
	      }
	    else
	      {
		cc = mpn_add_1(rp, rp, rrsize, 1); 
	      }
	  }
	else
	  if (rnd_mode == GMP_RNDN) /* even rounding */
	    {
	      rw = (MPFR_PREC(r) + 1) & (BITS_PER_MP_LIMB - 1);
	      if (rw) { rw = BITS_PER_MP_LIMB - rw; nw = 0; } else nw = 1; 
	      if ((rw ? (rp[nw] >> (rw + 1)) & 1 : 
		   (rp[nw] >> (BITS_PER_MP_LIMB - 1)) & 1))
		{
		  cc = mpn_add_1(rp + nw, rp + nw, rrsize, 
				 ((mp_limb_t)1) << rw); 
		}
	    }
	/* cas 0111111 */
      }
    /* Warning: we computed the result on oldrrsize limbs, but we want it
       on rrsize limbs only. Both can differ, especially when the target
       precision is a multiple of the number of bits per limb, since we've
       taken an extra bit to make rounding to nearest easier. */
    rp += oldrrsize-rrsize;
  }


  if (sign_quotient * MPFR_SIGN(r) < 0) { MPFR_CHANGE_SIGN(r); } 
  r->_mp_exp = rexp;

  if (cc) {
    mpn_rshift(rp, rp, rrsize, 1);
    rp[rrsize-1] |= (mp_limb_t) 1 << (BITS_PER_MP_LIMB-1);
    r->_mp_exp++; 
  }
    
  rw = rrsize * BITS_PER_MP_LIMB - MPFR_PREC(r);
  MPN_COPY(MPFR_MANT(r), rp, rrsize); 
  MPFR_MANT(r)[0] &= ~(((mp_limb_t)1 << rw) - 1);

  TMP_FREE (marker);
}