CUBRID Engine  latest
dtoa.c
Go to the documentation of this file.
1 /****************************************************************
2  *
3  * The author of this software is David M. Gay.
4  *
5  * Copyright (c) 1991, 2006 by AT&T.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose without fee is hereby granted, provided that this entire notice
9  * is included in all copies of any software which is or includes a copy
10  * or modification of this software and in all copies of the supporting
11  * documentation for such software.
12  *
13  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14  * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
15  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17  *
18  ***************************************************************/
19 
20 /* Please send bug reports to
21  David M. Gay
22  AT&T Bell Laboratories, Room 2C-463
23  600 Mountain Avenue
24  Murray Hill, NJ 07974-2070
25  U.S.A.
26  dmg@research.att.com or research!dmg
27  */
28 
29 #include <string.h>
30 #include <stdlib.h>
31 #include "mprec.h"
32 #include <stdlib.h>
33 
34 void free_Bigints (struct _Jv_Bigint *p);
35 
36 static int
37 _DEFUN (quorem, (b, S), _Jv_Bigint * b _AND _Jv_Bigint * S)
38 {
39  int n;
40  long borrow, y;
41  unsigned long carry, q, ys;
42  unsigned long *bx, *bxe, *sx, *sxe;
43 #ifdef Pack_32
44  long z;
45  unsigned long si, zs;
46 #endif
47 
48  n = S->_wds;
49 #ifdef DEBUG
50  /* debug */ if (b->_wds > n)
51  /* debug */ Bug ("oversize b in quorem");
52 #endif
53  if (b->_wds < n)
54  return 0;
55  sx = S->_x;
56  sxe = sx + --n;
57  bx = b->_x;
58  bxe = bx + n;
59  q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
60 #ifdef DEBUG
61  /* debug */ if (q > 9)
62  /* debug */ Bug ("oversized quotient in quorem");
63 #endif
64  if (q)
65  {
66  borrow = 0;
67  carry = 0;
68  do
69  {
70 #ifdef Pack_32
71  si = *sx++;
72  ys = (si & 0xffff) * q + carry;
73  zs = (si >> 16) * q + (ys >> 16);
74  carry = zs >> 16;
75  y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
76  borrow = y >> 16;
77  Sign_Extend (borrow, y);
78  z = (*bx >> 16) - (zs & 0xffff) + borrow;
79  borrow = z >> 16;
80  Sign_Extend (borrow, z);
81  Storeinc (bx, z, y);
82 #else
83  ys = *sx++ * q + carry;
84  carry = ys >> 16;
85  y = *bx - (ys & 0xffff) + borrow;
86  borrow = y >> 16;
87  Sign_Extend (borrow, y);
88  *bx++ = y & 0xffff;
89 #endif
90  }
91  while (sx <= sxe);
92  if (!*bxe)
93  {
94  bx = b->_x;
95  while (--bxe > bx && !*bxe)
96  --n;
97  b->_wds = n;
98  }
99  }
100  if (cmp (b, S) >= 0)
101  {
102  q++;
103  borrow = 0;
104  carry = 0;
105  bx = b->_x;
106  sx = S->_x;
107  do
108  {
109 #ifdef Pack_32
110  si = *sx++;
111  ys = (si & 0xffff) + carry;
112  zs = (si >> 16) + (ys >> 16);
113  carry = zs >> 16;
114  y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
115  borrow = y >> 16;
116  Sign_Extend (borrow, y);
117  z = (*bx >> 16) - (zs & 0xffff) + borrow;
118  borrow = z >> 16;
119  Sign_Extend (borrow, z);
120  Storeinc (bx, z, y);
121 #else
122  ys = *sx++ + carry;
123  carry = ys >> 16;
124  y = *bx - (ys & 0xffff) + borrow;
125  borrow = y >> 16;
126  Sign_Extend (borrow, y);
127  *bx++ = y & 0xffff;
128 #endif
129  }
130  while (sx <= sxe);
131  bx = b->_x;
132  bxe = bx + n;
133  if (!*bxe)
134  {
135  while (--bxe > bx && !*bxe)
136  --n;
137  b->_wds = n;
138  }
139  }
140  return q;
141 }
142 
143 #ifdef DEBUG
144 #include <stdio.h>
145 
146 void
147 print (_Jv_Bigint * b)
148 {
149  int i, wds;
150  unsigned long *x, y;
151  wds = b->_wds;
152  x = b->_x + wds;
153  i = 0;
154  do
155  {
156  x--;
157  fprintf (stderr, "%08x", *x);
158  }
159  while (++i < wds);
160  fprintf (stderr, "\n");
161 }
162 #endif
163 
164 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
165  *
166  * Inspired by "How to Print Floating-Point Numbers Accurately" by
167  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
168  *
169  * Modifications:
170  * 1. Rather than iterating, we use a simple numeric overestimate
171  * to determine k = floor(log10(d)). We scale relevant
172  * quantities using O(log2(k)) rather than O(k) multiplications.
173  * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
174  * try to generate digits strictly left to right. Instead, we
175  * compute with fewer bits and propagate the carry if necessary
176  * when rounding the final digit up. This is often faster.
177  * 3. Under the assumption that input will be rounded nearest,
178  * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
179  * That is, we allow equality in stopping tests when the
180  * round-nearest rule will give the same floating-point value
181  * as would satisfaction of the stopping test with strict
182  * inequality.
183  * 4. We remove common factors of powers of 2 from relevant
184  * quantities.
185  * 5. When converting floating-point integers less than 1e16,
186  * we use floating-point arithmetic rather than resorting
187  * to multiple-precision integers.
188  * 6. When asked to produce fewer than 15 digits, we first try
189  * to get by with floating-point arithmetic; we resort to
190  * multiple-precision integer arithmetic only if we cannot
191  * guarantee that the floating-point calculation has given
192  * the correctly rounded result. For k requested digits and
193  * "uniformly" distributed input, the probability is
194  * something like 10^(k-15) that we must resort to the long
195  * calculation.
196  */
197 
198 
199 char *
200 _DEFUN (_dtoa_r, (ptr, _d, mode, ndigits, decpt, sign, rve, float_type),
201  struct _Jv_reent *ptr _AND double _d _AND int mode _AND int ndigits _AND int *decpt _AND int *sign _AND char
202  **rve _AND int float_type)
203 {
204  /*
205  * float_type == 0 for double precision, 1 for float.
206  *
207  * Arguments ndigits, decpt, sign are similar to those of ecvt and fcvt; trailing zeros are suppressed from the
208  * returned string. If not null, *rve is set to point to the end of the return value. If d is +-Infinity or NaN,
209  * then *decpt is set to 9999.
210  *
211  * mode: 0 ==> shortest string that yields d when read in and rounded to nearest. 1 ==> like 0, but with Steele &
212  * White stopping rule; e.g. with IEEE P754 arithmetic , mode 0 gives 1e23 whereas mode 1 gives 9.999999999999999e22.
213  * 2 ==> max(1,ndigits) significant digits. This gives a return value similar to that of ecvt, except that trailing
214  * zeros are suppressed. 3 ==> through ndigits past the decimal point. This gives a return value similar to that from
215  * fcvt, except that trailing zeros are suppressed, and ndigits can be negative. 4-9 should give the same return
216  * values as 2-3, i.e., 4 <= mode <= 9 ==> same return as mode 2 + (mode & 1). These modes are mainly for debugging;
217  * often they run slower but sometimes faster than modes 2-3. 4,5,8,9 ==> left-to-right digit generation. 6-9 ==>
218  * don't try fast floating-point estimate (if applicable).
219  *
220  * > 16 ==> Floating-point arg is treated as single precision.
221  *
222  * Values of mode other than 0-9 are treated as mode 0.
223  *
224  * Sufficient space is allocated to the return value to hold the suppressed trailing zeros. */
225 
226  int bbits, b2, b5, be, dig, i, ieps, ilim0, j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, try_quick;
227  int ilim = 0, ilim1 = 0, spec_case = 0;
228  union double_union d, d2, eps;
229  long L;
230 #ifndef Sudden_Underflow
231  int denorm;
232  unsigned long x;
233 #endif
234  _Jv_Bigint *b, *b1, *delta, *mlo = NULL, *mhi, *S;
235  double ds;
236  char *s, *s0;
237  static char infinity_p[] = "Infinity", NaN_p[] = "NaN", zero_p[] = "0";
238 
239 
240  d.d = _d;
241 
242  if (ptr->_result)
243  {
244  ptr->_result->_k = ptr->_result_k;
245  ptr->_result->_maxwds = 1 << ptr->_result_k;
246  Bfree (ptr, ptr->_result);
247  ptr->_result = 0;
248  }
249 
250  if (word0 (d) & Sign_bit)
251  {
252  /* set sign for everything, including 0's and NaNs */
253  *sign = 1;
254  word0 (d) &= ~Sign_bit; /* clear sign bit */
255  }
256  else
257  *sign = 0;
258 
259 #if defined(IEEE_Arith) + defined(VAX)
260 #ifdef IEEE_Arith
261  if ((word0 (d) & Exp_mask) == Exp_mask)
262 #else
263  if (word0 (d) == 0x8000)
264 #endif
265  {
266  /* Infinity or NaN */
267  *decpt = 9999;
268  s =
269 #ifdef IEEE_Arith
270  !word1 (d) && !(word0 (d) & 0xfffff) ? infinity_p :
271 #endif
272  NaN_p;
273  if (rve)
274  *rve =
275 #ifdef IEEE_Arith
276  s[3] ? s + 8 :
277 #endif
278  s + 3;
279  return s;
280  }
281 #endif
282 #ifdef IBM
283  d.d += 0; /* normalize */
284 #endif
285  if (!d.d)
286  {
287  *decpt = 1;
288  s = zero_p; /* "0" */
289  if (rve)
290  *rve = s + 1;
291  return s;
292  }
293 
294  b = d2b (ptr, d.d, &be, &bbits);
295 #ifdef Sudden_Underflow
296  i = (int) (word0 (d) >> Exp_shift1 & (Exp_mask >> Exp_shift1));
297 #else
298  if ((i = (int) (word0 (d) >> Exp_shift1 & (Exp_mask >> Exp_shift1))))
299  {
300 #endif
301  d2.d = d.d;
302  word0 (d2) &= Frac_mask1;
303  word0 (d2) |= Exp_11;
304 #ifdef IBM
305  if (j = 11 - hi0bits (word0 (d2) & Frac_mask))
306  d2.d /= 1 << j;
307 #endif
308 
309  /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 log10(x) = log(x) / log(10) ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
310  * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) This suggests computing an approximation k to log10(d) by k =
311  * (i - Bias)*0.301029995663981 + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); We want k to be too large
312  * rather than too small. The error in the first-order Taylor series approximation is in our favor, so we just
313  * round up the constant enough to compensate for any error in the multiplication of (i - Bias) by
314  * 0.301029995663981; since |i - Bias| <= 1077, and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, adding 1e-13 to the
315  * constant term more than suffices. Hence we adjust the constant term to 0.1760912590558. (We could get a more
316  * accurate k by invoking log10, but this is probably not worthwhile.) */
317 
318  i -= Bias;
319 #ifdef IBM
320  i <<= 2;
321  i += j;
322 #endif
323 #ifndef Sudden_Underflow
324  denorm = 0;
325  }
326  else
327  {
328  /* d is denormalized */
329 
330  i = bbits + be + (Bias + (PREC - 1) - 1);
331  x = i > 32 ? word0 (d) << (64 - i) | word1 (d) >> (i - 32) : word1 (d) << (32 - i);
332  d2.d = x;
333  word0 (d2) -= 31 * Exp_msk1; /* adjust exponent */
334  i -= (Bias + (PREC - 1) - 1) + 1;
335  denorm = 1;
336  }
337 #endif
338  ds = (d2.d - 1.5) * 0.289529654602168 + 0.1760912590558 + i * 0.301029995663981;
339  k = (int) ds;
340  if (ds < 0. && ds != k)
341  k--; /* want k = floor(ds) */
342  k_check = 1;
343  if (k >= 0 && k <= Ten_pmax)
344  {
345  if (d.d < tens[k])
346  k--;
347  k_check = 0;
348  }
349  j = bbits - i - 1;
350  if (j >= 0)
351  {
352  b2 = 0;
353  s2 = j;
354  }
355  else
356  {
357  b2 = -j;
358  s2 = 0;
359  }
360  if (k >= 0)
361  {
362  b5 = 0;
363  s5 = k;
364  s2 += k;
365  }
366  else
367  {
368  b2 -= k;
369  b5 = -k;
370  s5 = 0;
371  }
372  if (mode < 0 || mode > 9)
373  mode = 0;
374  try_quick = 1;
375  if (mode > 5)
376  {
377  mode -= 4;
378  try_quick = 0;
379  }
380  leftright = 1;
381  switch (mode)
382  {
383  case 0:
384  case 1:
385  ilim = ilim1 = -1;
386  i = 18;
387  ndigits = 0;
388  break;
389  case 2:
390  leftright = 0;
391  /* FALLTHRU */
392  case 4:
393  if (ndigits <= 0)
394  ndigits = 1;
395  ilim = ilim1 = i = ndigits;
396  break;
397  case 3:
398  leftright = 0;
399  /* FALLTHRU */
400  case 5:
401  i = ndigits + k + 1;
402  ilim = i;
403  ilim1 = i - 1;
404  if (i <= 0)
405  i = 1;
406  }
407  j = sizeof (unsigned long);
408  for (ptr->_result_k = 0; (int) (sizeof (_Jv_Bigint) - sizeof (unsigned long)) + j <= i; j <<= 1)
409  ptr->_result_k++;
410  ptr->_result = Balloc (ptr, ptr->_result_k);
411  s = s0 = (char *) ptr->_result;
412 
413  if (ilim >= 0 && ilim <= Quick_max && try_quick)
414  {
415  /* Try to get by with floating-point arithmetic. */
416 
417  i = 0;
418  d2.d = d.d;
419  k0 = k;
420  ilim0 = ilim;
421  ieps = 2; /* conservative */
422  if (k > 0)
423  {
424  ds = tens[k & 0xf];
425  j = k >> 4;
426  if (j & Bletch)
427  {
428  /* prevent overflows */
429  j &= Bletch - 1;
430  d.d /= bigtens[n_bigtens - 1];
431  ieps++;
432  }
433  for (; j; j >>= 1, i++)
434  if (j & 1)
435  {
436  ieps++;
437  ds *= bigtens[i];
438  }
439  d.d /= ds;
440  }
441  else if ((j1 = -k))
442  {
443  d.d *= tens[j1 & 0xf];
444  for (j = j1 >> 4; j; j >>= 1, i++)
445  if (j & 1)
446  {
447  ieps++;
448  d.d *= bigtens[i];
449  }
450  }
451  if (k_check && d.d < 1. && ilim > 0)
452  {
453  if (ilim1 <= 0)
454  goto fast_failed;
455  ilim = ilim1;
456  k--;
457  d.d *= 10.;
458  ieps++;
459  }
460  eps.d = ieps * d.d + 7.;
461  word0 (eps) -= (PREC - 1) * Exp_msk1;
462  if (ilim == 0)
463  {
464  S = mhi = 0;
465  d.d -= 5.;
466  if (d.d > eps.d)
467  goto one_digit;
468  if (d.d < -eps.d)
469  goto no_digits;
470  goto fast_failed;
471  }
472 #ifndef No_leftright
473  if (leftright)
474  {
475  /* Use Steele & White method of only generating digits needed. */
476  eps.d = 0.5 / tens[ilim - 1] - eps.d;
477  for (i = 0;;)
478  {
479  L = (long) d.d;
480  d.d -= L;
481  *s++ = '0' + (int) L;
482  if (d.d < eps.d)
483  goto ret1;
484  if (1. - d.d < eps.d)
485  goto bump_up;
486  if (++i >= ilim)
487  break;
488  eps.d *= 10.;
489  d.d *= 10.;
490  }
491  }
492  else
493  {
494 #endif
495  /* Generate ilim digits, then fix them up. */
496  eps.d *= tens[ilim - 1];
497  for (i = 1;; i++, d.d *= 10.)
498  {
499  L = (long) d.d;
500  d.d -= L;
501  *s++ = '0' + (int) L;
502  if (i == ilim)
503  {
504  if (d.d > 0.5 + eps.d)
505  goto bump_up;
506  else if (d.d < 0.5 - eps.d)
507  {
508  while (*--s == '0');
509  s++;
510  goto ret1;
511  }
512  break;
513  }
514  }
515 #ifndef No_leftright
516  }
517 #endif
518  fast_failed:
519  s = s0;
520  d.d = d2.d;
521  k = k0;
522  ilim = ilim0;
523  }
524 
525  /* Do we have a "small" integer? */
526 
527  if (be >= 0 && k <= Int_max)
528  {
529  /* Yes. */
530  ds = tens[k];
531  if (ndigits < 0 && ilim <= 0)
532  {
533  S = mhi = 0;
534  if (ilim < 0 || d.d <= 5 * ds)
535  goto no_digits;
536  goto one_digit;
537  }
538  for (i = 1;; i++)
539  {
540  L = (long) (d.d / ds);
541  d.d -= L * ds;
542 #ifdef Check_FLT_ROUNDS
543  /* If FLT_ROUNDS == 2, L will usually be high by 1 */
544  if (d.d < 0)
545  {
546  L--;
547  d.d += ds;
548  }
549 #endif
550  *s++ = '0' + (int) L;
551  if (i == ilim)
552  {
553  d.d += d.d;
554  if (d.d > ds || (d.d == ds && L & 1))
555  {
556  bump_up:
557  while (*--s == '9')
558  if (s == s0)
559  {
560  k++;
561  *s = '0';
562  break;
563  }
564  ++*s++;
565  }
566  break;
567  }
568  if (!(d.d *= 10.))
569  break;
570  }
571  goto ret1;
572  }
573 
574  m2 = b2;
575  m5 = b5;
576  mhi = mlo = 0;
577  if (leftright)
578  {
579  if (mode < 2)
580  {
581  i =
582 #ifndef Sudden_Underflow
583  denorm ? be + (Bias + (PREC - 1) - 1 + 1) :
584 #endif
585 #ifdef IBM
586  1 + 4 * PREC - 3 - bbits + ((bbits + be - 1) & 3);
587 #else
588  1 + PREC - bbits;
589 #endif
590  }
591  else
592  {
593  j = ilim - 1;
594  if (m5 >= j)
595  m5 -= j;
596  else
597  {
598  s5 += j -= m5;
599  b5 += j;
600  m5 = 0;
601  }
602  if ((i = ilim) < 0)
603  {
604  m2 -= i;
605  i = 0;
606  }
607  }
608  b2 += i;
609  s2 += i;
610  mhi = i2b (ptr, 1);
611  }
612  if (m2 > 0 && s2 > 0)
613  {
614  i = m2 < s2 ? m2 : s2;
615  b2 -= i;
616  m2 -= i;
617  s2 -= i;
618  }
619  if (b5 > 0)
620  {
621  if (leftright)
622  {
623  if (m5 > 0)
624  {
625  mhi = pow5mult (ptr, mhi, m5);
626  b1 = mult (ptr, mhi, b);
627  Bfree (ptr, b);
628  b = b1;
629  }
630  if ((j = b5 - m5))
631  b = pow5mult (ptr, b, j);
632  }
633  else
634  b = pow5mult (ptr, b, b5);
635  }
636  S = i2b (ptr, 1);
637  if (s5 > 0)
638  S = pow5mult (ptr, S, s5);
639 
640  /* Check for special case that d is a normalized power of 2. */
641 
642  if (mode < 2)
643  {
644  if (!word1 (d) && !(word0 (d) & Bndry_mask)
645 #ifndef Sudden_Underflow
646  && word0 (d) & Exp_mask
647 #endif
648  )
649  {
650  /* The special case */
651  b2 += Log2P;
652  s2 += Log2P;
653  spec_case = 1;
654  }
655  else
656  spec_case = 0;
657  }
658 
659  /* Arrange for convenient computation of quotients: shift left if necessary so divisor has 4 leading 0 bits. Perhaps
660  * we should just compute leading 28 bits of S once and for all and pass them and a shift to quorem, so it can do
661  * shifts and ors to compute the numerator for q. */
662 
663 #ifdef Pack_32
664  if ((i = ((s5 ? 32 - hi0bits (S->_x[S->_wds - 1]) : 1) + s2) & 0x1f))
665  i = 32 - i;
666 #else
667  if ((i = ((s5 ? 32 - hi0bits (S->_x[S->_wds - 1]) : 1) + s2) & 0xf))
668  i = 16 - i;
669 #endif
670  if (i > 4)
671  {
672  i -= 4;
673  b2 += i;
674  m2 += i;
675  s2 += i;
676  }
677  else if (i < 4)
678  {
679  i += 28;
680  b2 += i;
681  m2 += i;
682  s2 += i;
683  }
684  if (b2 > 0)
685  b = lshift (ptr, b, b2);
686  if (s2 > 0)
687  S = lshift (ptr, S, s2);
688  if (k_check)
689  {
690  if (cmp (b, S) < 0)
691  {
692  k--;
693  b = multadd (ptr, b, 10, 0); /* we botched the k estimate */
694  if (leftright)
695  mhi = multadd (ptr, mhi, 10, 0);
696  ilim = ilim1;
697  }
698  }
699  if (ilim <= 0 && mode > 2)
700  {
701  if (ilim < 0 || cmp (b, S = multadd (ptr, S, 5, 0)) <= 0)
702  {
703  /* no digits, fcvt style */
704  no_digits:
705  k = -1 - ndigits;
706  goto ret;
707  }
708  one_digit:
709  *s++ = '1';
710  k++;
711  goto ret;
712  }
713  if (leftright)
714  {
715  if (m2 > 0)
716  mhi = lshift (ptr, mhi, m2);
717 
718  /* Single precision case, */
719  if (float_type)
720  mhi = lshift (ptr, mhi, 29);
721 
722  /* Compute mlo -- check for special case that d is a normalized power of 2. */
723 
724  mlo = mhi;
725  if (spec_case)
726  {
727  mhi = Balloc (ptr, mhi->_k);
728  Bcopy (mhi, mlo);
729  mhi = lshift (ptr, mhi, Log2P);
730  }
731 
732  for (i = 1;; i++)
733  {
734  dig = quorem (b, S) + '0';
735  /* Do we yet have the shortest decimal string that will round to d? */
736  j = cmp (b, mlo);
737  delta = diff (ptr, S, mhi);
738  j1 = delta->_sign ? 1 : cmp (b, delta);
739  Bfree (ptr, delta);
740 #ifndef ROUND_BIASED
741  if (j1 == 0 && !mode && !(word1 (d) & 1))
742  {
743  if (dig == '9')
744  goto round_9_up;
745  if (j > 0)
746  dig++;
747  *s++ = dig;
748  goto ret;
749  }
750 #endif
751  if (j < 0 || (j == 0 && !mode
752 #ifndef ROUND_BIASED
753  && !(word1 (d) & 1)
754 #endif
755  ))
756  {
757  if (j1 > 0)
758  {
759  b = lshift (ptr, b, 1);
760  j1 = cmp (b, S);
761  if ((j1 > 0 || (j1 == 0 && dig & 1)) && dig++ == '9')
762  goto round_9_up;
763  }
764  *s++ = dig;
765  goto ret;
766  }
767  if (j1 > 0)
768  {
769  if (dig == '9')
770  { /* possible if i == 1 */
771  round_9_up:
772  *s++ = '9';
773  goto roundoff;
774  }
775  *s++ = dig + 1;
776  goto ret;
777  }
778  *s++ = dig;
779  if (i == ilim)
780  break;
781  b = multadd (ptr, b, 10, 0);
782  if (mlo == mhi)
783  mlo = mhi = multadd (ptr, mhi, 10, 0);
784  else
785  {
786  mlo = multadd (ptr, mlo, 10, 0);
787  mhi = multadd (ptr, mhi, 10, 0);
788  }
789  }
790  }
791  else
792  for (i = 1;; i++)
793  {
794  *s++ = dig = quorem (b, S) + '0';
795  if (i >= ilim)
796  break;
797  b = multadd (ptr, b, 10, 0);
798  }
799 
800  /* Round off last digit */
801 
802  b = lshift (ptr, b, 1);
803  j = cmp (b, S);
804  if (j > 0 || (j == 0 && dig & 1))
805  {
806  roundoff:
807  while (*--s == '9')
808  if (s == s0)
809  {
810  k++;
811  *s++ = '1';
812  goto ret;
813  }
814  ++*s++;
815  }
816  else
817  {
818  while (*--s == '0');
819  s++;
820  }
821 ret:
822  Bfree (ptr, S);
823  if (mhi)
824  {
825  if (mlo && mlo != mhi)
826  Bfree (ptr, mlo);
827  Bfree (ptr, mhi);
828  }
829 ret1:
830  Bfree (ptr, b);
831  *s = 0;
832  *decpt = k + 1;
833  if (rve)
834  *rve = s;
835  return s0;
836 }
837 
838 void
840 {
841  struct _Jv_Bigint *l = p;
842  while (l)
843  {
844  struct _Jv_Bigint *next = l->_next;
845  free (l);
846  l = next;
847  }
848 }
849 
850 _VOID
851 _DEFUN (_dtoa, (_d, mode, ndigits, decpt, sign, rve, buf, float_type),
852  double _d _AND int mode _AND int ndigits _AND int *decpt _AND int *sign _AND char **rve _AND char *buf _AND int
853  float_type)
854 {
855  struct _Jv_reent reent;
856  char *p;
857  int i;
858 
859  memset (&reent, 0, sizeof reent);
860 
861  p = _dtoa_r (&reent, _d, mode, ndigits, decpt, sign, rve, float_type);
862  strcpy (buf, p);
863 
864  for (i = 0; i < reent._max_k; ++i)
865  {
866  free_Bigints (reent._freelist[i]);
867  }
868  if (reent._freelist)
869  {
870  free (reent._freelist);
871  }
872 
873  if (reent._result)
874  {
875  free (reent._result);
876  }
877 
878  free_Bigints (reent._p5s);
879 }
#define Exp_11
Definition: mprec.h:239
#define Bcopy(x, y)
Definition: mprec.h:390
#define _AND
Definition: mprec.h:309
#define Sign_Extend(a, b)
Definition: mprec.h:94
#define word1(x)
Definition: mprec.h:115
#define Bletch
Definition: mprec.h:244
#define Balloc
Definition: mprec.h:341
#define ROUND_BIASED
Definition: mprec.h:258
int _wds
Definition: mprec.h:303
#define Bndry_mask
Definition: mprec.h:245
#define Sudden_Underflow
Definition: mprec.h:205
#define i2b
Definition: mprec.h:347
#define diff
Definition: mprec.h:352
struct _Jv_Bigint * _p5s
Definition: mprec.h:332
static int roundoff(const INTL_LANG lang, char *src_string, int flag, int *cipher, char *format)
double d
Definition: mprec.h:106
struct _Jv_Bigint _Jv_Bigint
Definition: mprec.h:339
struct _Jv_Bigint ** _freelist
Definition: mprec.h:334
#define Sign_bit
Definition: mprec.h:248
#define Int_max
Definition: mprec.h:253
int _max_k
Definition: mprec.h:335
static int _DEFUN(quorem,(b, S), _Jv_Bigint *b _AND _Jv_Bigint *S)
Definition: dtoa.c:37
#define pow5mult
Definition: mprec.h:349
#define hi0bits
Definition: mprec.h:346
#define Exp_shift1
Definition: mprec.h:232
#define lshift
Definition: mprec.h:350
#define _dtoa
Definition: mprec.h:362
#define Log2P
Definition: mprec.h:249
#define d2b
Definition: mprec.h:355
static enum scanner_mode mode
#define Ten_pmax
Definition: mprec.h:243
void free_Bigints(struct _Jv_Bigint *p)
Definition: dtoa.c:839
#define NULL
Definition: freelistheap.h:34
#define PREC
Definition: mprec.h:236
if(extra_options)
Definition: dynamic_load.c:958
#define mult
Definition: mprec.h:348
#define Bias
Definition: mprec.h:237
#define Frac_mask
Definition: mprec.h:241
#define Exp_mask
Definition: mprec.h:235
#define cmp
Definition: mprec.h:351
#define Exp_msk1
Definition: mprec.h:233
struct _Jv_Bigint * _next
Definition: mprec.h:302
#define n_bigtens
Definition: mprec.h:395
#define tens
Definition: mprec.h:358
int _sign
Definition: mprec.h:303
#define Storeinc(a, b, c)
Definition: mprec.h:126
#define multadd
Definition: mprec.h:343
int i
Definition: dynamic_load.c:954
#define bigtens
Definition: mprec.h:359
#define word0(x)
Definition: mprec.h:114
#define _dtoa_r
Definition: mprec.h:363
#define Bfree
Definition: mprec.h:342
#define Frac_mask1
Definition: mprec.h:242
struct _Jv_Bigint * _result
Definition: mprec.h:330
#define _VOID
Definition: mprec.h:315
const char ** p
Definition: dynamic_load.c:945
unsigned long _x[1]
Definition: mprec.h:304
#define Quick_max
Definition: mprec.h:252