summaryrefslogtreecommitdiff
path: root/div.c
blob: 589707eb3f8712dc806dc1ab169889dce724fb18 (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* 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 <stdio.h>
#include <stdlib.h>
#include "gmp.h"
#include "gmp-impl.h"
#include "longlong.h"
#include "mpfr.h"
#include "mpfr-impl.h"

// #define DEBUG

#ifdef DEBUG
void 
affiche_mp(mp_ptr z, mp_size_t length)
{
  int k; 

  if (length == 1) { printf("[%lu]\n", *z); return; }

  printf("[%lu, ", z[length - 1]); 
  for(k = length - 2; k >= 1; k--) { printf("%lu, ", z[k]); }
  printf("%lu]\n", z[0]); 
}
#endif

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, drsize, dsize;
  mp_size_t oldrsize, rsize, sign_quotient, err;
  mp_limb_t q_limb;
  mp_exp_t rexp;
  long k, t; 
  unsigned long cc = 0, rw, nw; 
  char can_round = 0, sh; 
  TMP_DECL (marker);


  /**************************************************************************
   *                                                                        *
   *              This part of the code deals with special cases            *
   *                                                                        *
   **************************************************************************/

  if (MPFR_IS_NAN(u) || MPFR_IS_NAN(v)) { MPFR_SET_NAN(r); return; }

  MPFR_CLEAR_NAN(r);

  if (MPFR_IS_INF(u)) 
    { 
      if (MPFR_IS_INF(v)) 
	MPFR_SET_NAN(r);
      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_CLEAR_INF(r);
	MPFR_SET_ZERO(r); 
	if (MPFR_SIGN(r) != MPFR_SIGN(u) * MPFR_SIGN(v)) 
	  MPFR_CHANGE_SIGN(r);
	return; 
      }

  MPFR_CLEAR_INF(r); /* clear Inf flag */

  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; }

  /**************************************************************************
   *                                                                        *
   *              End of the part concerning special values.                *
   *                                                                        *
   **************************************************************************/


#ifdef DEBUG
  printf("u = "); mpfr_out_str(stdout, 2, 0, u, GMP_RNDN); printf("\n"); 
  printf("v = "); mpfr_out_str(stdout, 2, 0, v, GMP_RNDN); printf("\n"); 
#endif

  up = MPFR_MANT(u);
  vp = MPFR_MANT(v);

  TMP_MARK (marker);

  usize = (MPFR_PREC(u) - 1)/BITS_PER_MP_LIMB + 1; 
  vsize = (MPFR_PREC(v) - 1)/BITS_PER_MP_LIMB + 1; 

#ifdef DEBUG
  printf("Entering division : "); 
  affiche_mp(up, usize); 
  affiche_mp(vp, vsize); 
#endif
      
  /**************************************************************************
   *                                                                        *
   *   First try to use only part of u, v. If this is not sufficient,       *
   *   use the full u and v, to avoid long computations eg. in the case     *
   *   u = v.                                                               *
   *                                                                        *
   **************************************************************************/

  dsize = (MPFR_PREC(r) + 3)/BITS_PER_MP_LIMB + 1; 
  drsize = MPFR_PREC(r)/BITS_PER_MP_LIMB + 1 + dsize; 

  /* Compute effective dividend */
  if (vsize < dsize) { dsize = vsize; }
  tmp = (mp_ptr) vp + vsize - dsize; 

  /* Compute effective divisor. One extra bit allowed for RNDN. */
  
  tp0 = (mp_ptr) TMP_ALLOC(drsize * BYTES_PER_MP_LIMB); 
  if (usize >= drsize) 
    MPN_COPY (tp0, up + usize - drsize, drsize); 
  else {
    MPN_COPY (tp0 + drsize - usize, up, usize); 
    MPN_ZERO (tp0, drsize - usize); 
  }

  /* Allocate limbs for quotient. */
  rp = (mp_ptr) TMP_ALLOC ((drsize - dsize + 1) * BYTES_PER_MP_LIMB);
      
#ifdef DEBUG
  printf("Dividing : "); 
  affiche_mp(tp0, drsize); 
  printf(" by "); 
  affiche_mp(tmp, dsize); 
  printf(".\n"); 
#endif
      
#if (__GNU_MP_VERSION < 3)
  q_limb = mpn_divrem (rp, 0, tp0, drsize, tmp, dsize);
  tp = tp0;
#else 
  tmp2 = (mp_ptr) TMP_ALLOC (dsize * BYTES_PER_MP_LIMB);
  mpn_tdiv_qr(rp, tmp2, 0, tp0, drsize, tmp, dsize);
  q_limb = rp[drsize - dsize];
  tp = tmp2;
#endif
  
  /* Estimate number of correct bits. */
  err = (drsize - dsize) * BITS_PER_MP_LIMB; 
  if (drsize < usize) err --; 
  if (dsize < vsize) err -= 2; 

#ifdef DEBUG
  printf("The result is : \n"); 
  printf("Quotient : "); 

  affiche_mp(rp, drsize - dsize + 1); 
 
  printf("Remainder : "); 
  affiche_mp(tp, dsize); 

  printf("Number of correct bits = %lu\n", err); 
#endif
      
  /* Compute sign and exponent. Correction might occur just below. */
  sign_quotient = ((MPFR_SIGN(u) * MPFR_SIGN(v) > 0) ? 1 : -1); 
  rexp = MPFR_EXP(u) - MPFR_EXP(v);
  rsize = drsize - dsize;
  
  /* We want to check if rounding is possible. Have to mimic normalization */
  if (q_limb) { sh = -1; } 
  else { count_leading_zeros(sh, rp[rsize - 1]); } 
  
  can_round = mpfr_can_round_raw(rp, rsize + 1, sign_quotient, err + sh + 
				 BITS_PER_MP_LIMB, GMP_RNDN, rnd_mode, 
				 MPFR_PREC(r) + sh + BITS_PER_MP_LIMB); 

  if (!can_round && (drsize < usize || dsize < vsize)) 
    {
      mp_size_t ulrsize; 
      char b = 0; 
      mp_ptr rp2, ulorem, ulorem2; 

  /**************************************************************************
   *                                                                        *
   *   The attempt to use only part of u and v failed. We first compute a   *
   *   correcting term, then perform the full division.                     *
   *   Put u = uhi + ulo, v = vhi + vlo. We have uhi = vhi * rp + tp,       *
   *   thus u - v * rp = tp + ulo - rp*vlo, that we shall divide by v.      *
   *                                                                        *
   **************************************************************************/
#ifdef DEBUG
      printf("Using the full u and v.\n");
#endif
      
      if (usize > drsize)
	  ulrsize = usize - drsize + dsize; 
	  // store the low part of u and the remainder. 
      else ulrsize = dsize; // just store the remainder. 

      if (vsize > dsize) ulrsize++; 

      ulorem = TMP_ALLOC(ulrsize * BYTES_PER_MP_LIMB);
      ulorem2 = TMP_ALLOC(ulrsize * BYTES_PER_MP_LIMB);
      ulorem[ulrsize - 1] = ulorem2 [ulrsize - 1] = 0; 

      if (dsize < vsize)
	{
	  mp_size_t lg = vsize + rsize - dsize + 1; 

	  if (rsize > vsize - dsize)
	    mpn_mul(ulorem + ulrsize - lg, rp, rsize, vp, vsize - dsize);
	  else
	    mpn_mul(ulorem + ulrsize - lg, vp, vsize - dsize, rp, rsize);

	  MPN_ZERO(ulorem, ulrsize - lg); 
	}
      else MPN_ZERO(ulorem, ulrsize); 

#ifdef DEBUG
      printf("vlo * q: "); 
      affiche_mp(ulorem, ulrsize); 
#endif 
     
      MPN_COPY(ulorem2 + ulrsize - 1 - dsize, tp, dsize);      
      if (drsize < usize) 
	MPN_COPY(ulorem2, up, usize - drsize); 

#ifdef DEBUG
      printf("(b = %d) ulo + r: ", b); 
      affiche_mp(ulorem2, ulrsize); 
#endif
      
      if (mpn_cmp(ulorem2, ulorem, ulrsize) > 0)
	mpn_sub_n(ulorem, ulorem2, ulorem, ulrsize); 
      else 
	{ 
	  ulorem2[ulrsize - 1] = 
	    mpn_add_n(ulorem2 + ulrsize - vsize - 1, 
		      ulorem2 + ulrsize - vsize - 1, vp, vsize); 

	  if (mpn_cmp(ulorem2, ulorem, ulrsize) < 0)
	    {
	      b = 2; 
	      ulorem2[ulrsize - 1] += 
		mpn_add_n(ulorem2 + ulrsize - vsize - 1, 
			  ulorem2 + ulrsize - vsize - 1, vp, vsize); 
	    }
	  else b = 1; 

	  mpn_sub_n(ulorem, ulorem2, ulorem, ulrsize); 
	}
	  
#ifdef DEBUG
  printf("(b = %d) ulo + r - vlo * q: ", b); 
  affiche_mp(ulorem, ulrsize); 
#endif
     
      rp2 = (mp_ptr) TMP_ALLOC((ulrsize - vsize + 1) * BYTES_PER_MP_LIMB); 

#if (__GNU_MP_VERSION < 3)
      q_limb = mpn_divrem (rp2, 0, ulorem, ulrsize, vp, vsize);
      tp = ulorem; 
#else 
      tmp2 = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
      mpn_tdiv_qr(rp2, tmp2, 0, ulorem, ulrsize, vp, vsize);
      tp = tmp2; 
#endif

      if (!b)
	mpn_add_1(rp, rp, rsize, rp2[ulrsize - vsize]);
      else
	mpn_sub_1(rp, rp, rsize, b); 

      dsize = vsize; /* The length of the remainder, in the end of the code */
    }

  /**************************************************************************
   *                                                                        *
   *                       Final stuff (rounding and so.)                   *
   *                                                                        *
   **************************************************************************/

  if (q_limb)
    {
      mpn_rshift(rp, rp, rsize, 1);
      rp[rsize - 1] |= 1 << (BITS_PER_MP_LIMB - 1); rexp ++; 
    }
  else
    if (sh) { mpn_lshift(rp, rp, rsize, sh); rexp -= sh; }
  
  oldrsize = rsize;
  rsize = (MPFR_PREC(r) - 1)/BITS_PER_MP_LIMB + 1;

  if (can_round) /* Lazy case. */
    {
      cc = mpfr_round_raw(rp, rp, err, (sign_quotient == -1 ? 1 : 0),
			  MPFR_PREC(r), rnd_mode, NULL);
    }
  else {
    rp += oldrsize-rsize;
    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 = dsize - 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, rsize, 
			     (mp_limb_t)1 << (BITS_PER_MP_LIMB - t)); 
	    else
	      cc = mpn_add_1(rp, rp, rsize, 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, rsize, 
				   ((mp_limb_t)1) << rw); 
		  }
	      }
	/* cas 0111111 */
	  }
      }
  }

  if (sign_quotient * MPFR_SIGN(r) < 0) { MPFR_CHANGE_SIGN(r); } 
  MPFR_EXP(r) = rexp;

  if (cc) {
    mpn_rshift(rp, rp, rsize, 1);
    rp[rsize-1] |= (mp_limb_t) 1 << (BITS_PER_MP_LIMB-1);
    MPFR_EXP(r)++; 
  }
    
  rw = rsize * BITS_PER_MP_LIMB - MPFR_PREC(r);
  MPN_COPY(MPFR_MANT(r), rp, rsize); 
  MPFR_MANT(r)[0] &= ~(((mp_limb_t)1 << rw) - 1);
#ifdef DEBUG
  printf("r = "); mpfr_out_str(stdout, 2, 0, r, GMP_RNDN); printf("\n"); 
#endif
  TMP_FREE (marker);
}