OSDN Git Service

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