OSDN Git Service

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