source: trunk/libs/newlib/src/newlib/libc/stdlib/dtoa.c @ 577

Last change on this file since 577 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

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