Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
gdtoa.c
Go to the documentation of this file.
1 /****************************************************************
2 
3 The author of this software is David M. Gay.
4 
5 Copyright (C) 1998, 1999 by Lucent Technologies
6 All Rights Reserved
7 
8 Permission to use, copy, modify, and distribute this software and
9 its documentation for any purpose and without fee is hereby
10 granted, provided that the above copyright notice appear in all
11 copies and that both that the copyright notice and this
12 permission notice and warranty disclaimer appear in supporting
13 documentation, and that the name of Lucent or any of its entities
14 not be used in advertising or publicity pertaining to
15 distribution of the software without specific, written prior
16 permission.
17 
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25 THIS SOFTWARE.
26 
27 ****************************************************************/
28 
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30  * with " at " changed at "@" and " dot " changed to "."). */
31 
32 #include "gdtoaimp.h"
33 #include <float.h>
34 #include <math.h>
35 
36 static Bigint*
37 #ifdef KR_headers
38  bitstob(bits, nbits, bbits) ULong* bits;
39 int nbits;
40 int* bbits;
41 #else
42  bitstob(ULong* bits, int nbits, int* bbits)
43 #endif
44 {
45  int i, k;
46  Bigint* b;
47  ULong *be, *x, *x0;
48 
49  i = ULbits;
50  k = 0;
51  while(i < nbits)
52  {
53  i <<= 1;
54  k++;
55  }
56 #ifndef Pack_32
57  if(!k)
58  k = 1;
59 #endif
60  b = Balloc(k);
61  be = bits + ((nbits - 1) >> kshift);
62  x = x0 = b->x;
63  do
64  {
65  *x++ = *bits & ALL_ON;
66 #ifdef Pack_16
67  *x++ = (*bits >> 16) & ALL_ON;
68 #endif
69  } while(++bits <= be);
70  i = (int)(x - x0); // TODO: can we fix types here?
71  while(!x0[--i])
72  {
73  {
74  if(!i)
75  {
76  b->wds = 0;
77  *bbits = 0;
78  goto ret;
79  }
80  }
81  }
82  b->wds = i + 1;
83  *bbits = i * ULbits + 32 - hi0bits(b->x[i]);
84 ret:
85  return b;
86 }
87 
88 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
89  *
90  * Inspired by "How to Print Floating-Point Numbers Accurately" by
91  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
92  *
93  * Modifications:
94  * 1. Rather than iterating, we use a simple numeric overestimate
95  * to determine k = floor(log10(d)). We scale relevant
96  * quantities using O(log2(k)) rather than O(k) multiplications.
97  * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
98  * try to generate digits strictly left to right. Instead, we
99  * compute with fewer bits and propagate the carry if necessary
100  * when rounding the final digit up. This is often faster.
101  * 3. Under the assumption that input will be rounded nearest,
102  * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
103  * That is, we allow equality in stopping tests when the
104  * round-nearest rule will give the same floating-point value
105  * as would satisfaction of the stopping test with strict
106  * inequality.
107  * 4. We remove common factors of powers of 2 from relevant
108  * quantities.
109  * 5. When converting floating-point integers less than 1e16,
110  * we use floating-point arithmetic rather than resorting
111  * to multiple-precision integers.
112  * 6. When asked to produce fewer than 15 digits, we first try
113  * to get by with floating-point arithmetic; we resort to
114  * multiple-precision integer arithmetic only if we cannot
115  * guarantee that the floating-point calculation has given
116  * the correctly rounded result. For k requested digits and
117  * "uniformly" distributed input, the probability is
118  * something like 10^(k-15) that we must resort to the Long
119  * calculation.
120  */
121 
122 char* gdtoa
123 #ifdef KR_headers
124  (fpi, be, bits, kindp, mode, ndigits, decpt, rve) FPI* fpi;
125 int be;
126 ULong* bits;
127 int *kindp, mode, ndigits, *decpt;
128 char** rve;
129 #else
130  (FPI* fpi, int be, ULong* bits, int* kindp, int mode, int ndigits, int* decpt, char** rve)
131 #endif
132 {
133  /* Arguments ndigits and decpt are similar to the second and third
134  arguments of ecvt and fcvt; trailing zeros are suppressed from
135  the returned string. If not null, *rve is set to point
136  to the end of the return value. If d is +-Infinity or NaN,
137  then *decpt is set to 9999.
138 
139  mode:
140  0 ==> shortest string that yields d when read in
141  and rounded to nearest.
142  1 ==> like 0, but with Steele & White stopping rule;
143  e.g. with IEEE P754 arithmetic , mode 0 gives
144  1e23 whereas mode 1 gives 9.999999999999999e22.
145  2 ==> max(1,ndigits) significant digits. This gives a
146  return value similar to that of ecvt, except
147  that trailing zeros are suppressed.
148  3 ==> through ndigits past the decimal point. This
149  gives a return value similar to that from fcvt,
150  except that trailing zeros are suppressed, and
151  ndigits can be negative.
152  4-9 should give the same return values as 2-3, i.e.,
153  4 <= mode <= 9 ==> same return as mode
154  2 + (mode & 1). These modes are mainly for
155  debugging; often they run slower but sometimes
156  faster than modes 2-3.
157  4,5,8,9 ==> left-to-right digit generation.
158  6-9 ==> don't try fast floating-point estimate
159  (if applicable).
160 
161  Values of mode other than 0-9 are treated as mode 0.
162 
163  Sufficient space is allocated to the return value
164  to hold the suppressed trailing zeros.
165  */
166 
167  int bbits, b2, b5, be0, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0, inex;
168  int j, j1, k, k0, k_check, kind, leftright, m2, m5, nbits;
169  int rdir, s2, s5, spec_case, try_quick;
170  Long L;
171  Bigint *b, *b1, *delta, *mlo = NULL, *mhi, *mhi1, *S;
172  double d, d2, ds, eps;
173  char *s, *s0;
174 
175 #ifndef MULTIPLE_THREADS
176  if(dtoa_result)
177  {
179  dtoa_result = 0;
180  }
181 #endif
182  inex = 0;
183  kind = *kindp &= ~STRTOG_Inexact;
184  switch(kind & STRTOG_Retmask)
185  {
186  case STRTOG_Zero:
187  goto ret_zero;
188  case STRTOG_Normal:
189  case STRTOG_Denormal:
190  break;
191  case STRTOG_Infinite:
192  *decpt = -32768;
193  return nrv_alloc("Infinity", rve, 8);
194  case STRTOG_NaN:
195  *decpt = -32768;
196  return nrv_alloc("NaN", rve, 3);
197  default:
198  return 0;
199  }
200  b = bitstob(bits, nbits = fpi->nbits, &bbits);
201  be0 = be;
202  if((i = trailz(b)) != 0)
203  {
204  rshift(b, i);
205  be += i;
206  bbits -= i;
207  }
208  if(!b->wds)
209  {
210  Bfree(b);
211  ret_zero:
212  *decpt = 1;
213  return nrv_alloc("0", rve, 1);
214  }
215 
216  dval(d) = b2d(b, &i);
217  i = be + bbits - 1;
218  word0(d) &= Frac_mask1;
219  word0(d) |= Exp_11;
220 
221  /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
222  * log10(x) = log(x) / log(10)
223  * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
224  * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
225  *
226  * This suggests computing an approximation k to log10(d) by
227  *
228  * k = (i - Bias)*0.301029995663981
229  * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
230  *
231  * We want k to be too large rather than too small.
232  * The error in the first-order Taylor series approximation
233  * is in our favor, so we just round up the constant enough
234  * to compensate for any error in the multiplication of
235  * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
236  * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
237  * adding 1e-13 to the constant term more than suffices.
238  * Hence we adjust the constant term to 0.1760912590558.
239  * (We could get a more accurate k by invoking log10,
240  * but this is probably not worthwhile.)
241  */
242  ds = (dval(d) - 1.5) * 0.289529654602168 + 0.1760912590558 + i * 0.301029995663981;
243 
244  /* correct assumption about exponent range */
245  if((j = i) < 0)
246  {
247  j = -j;
248  }
249  if((j -= 1077) > 0)
250  {
251  ds += j * 7e-17;
252  }
253 
254  k = (int)ds;
255  if(ds < 0. && (fabs(ds - k) > DBL_EPSILON))
256  {
257  k--; /* want k = floor(ds) */
258  }
259  k_check = 1;
260  word0(d) += (unsigned)((be + bbits - 1) << Exp_shift);
261  if(k >= 0 && k <= Ten_pmax)
262  {
263  if(dval(d) < tens[k])
264  {
265  k--;
266  }
267  k_check = 0;
268  }
269  j = bbits - i - 1;
270  if(j >= 0)
271  {
272  b2 = 0;
273  s2 = j;
274  }
275  else
276  {
277  b2 = -j;
278  s2 = 0;
279  }
280  if(k >= 0)
281  {
282  b5 = 0;
283  s5 = k;
284  s2 += k;
285  }
286  else
287  {
288  b2 -= k;
289  b5 = -k;
290  s5 = 0;
291  }
292  if(mode < 0 || mode > 9)
293  {
294  mode = 0;
295  }
296  try_quick = 1;
297  if(mode > 5)
298  {
299  mode -= 4;
300  try_quick = 0;
301  }
302  leftright = 1;
303  switch(mode)
304  {
305  case 0:
306  case 1:
307  ilim = ilim1 = -1;
308  i = (int)(nbits * .30103) + 3;
309  ndigits = 0;
310  break;
311  case 2:
312  leftright = 0;
313  /* no break */
314  case 4:
315  if(ndigits <= 0)
316  {
317  {
318  ndigits = 1;
319  }
320  }
321  ilim = ilim1 = i = ndigits;
322  break;
323  case 3:
324  leftright = 0;
325  /* no break */
326  case 5:
327  i = ndigits + k + 1;
328  ilim = i;
329  ilim1 = i - 1;
330  if(i <= 0)
331  {
332  {
333  i = 1;
334  }
335  }
336  }
337  s = s0 = rv_alloc(i);
338 
339  if((rdir = fpi->rounding - 1) != 0)
340  {
341  if(rdir < 0)
342  {
343  {
344  rdir = 2;
345  }
346  }
347  if(kind & STRTOG_Neg)
348  {
349  {
350  rdir = 3 - rdir;
351  }
352  }
353  }
354 
355  /* Now rdir = 0 ==> round near, 1 ==> round up, 2 ==> round down. */
356 
357  if(ilim >= 0 && ilim <= Quick_max && try_quick && !rdir
358 #ifndef IMPRECISE_INEXACT
359  && k == 0
360 #endif
361  )
362  {
363  /* Try to get by with floating-point arithmetic. */
364 
365  i = 0;
366  d2 = dval(d);
367  k0 = k;
368  ilim0 = ilim;
369  ieps = 2; /* conservative */
370  if(k > 0)
371  {
372  ds = tens[k & 0xf];
373  j = k >> 4;
374  if(j & Bletch)
375  {
376  /* prevent overflows */
377  j &= Bletch - 1;
378  dval(d) /= bigtens[n_bigtens - 1];
379  ieps++;
380  }
381  for(; j; j >>= 1, i++)
382  {
383  {
384  if(j & 1)
385  {
386  ieps++;
387  ds *= bigtens[i];
388  }
389  }
390  }
391  }
392  else
393  {
394  ds = 1.;
395  if((j1 = -k) != 0)
396  {
397  dval(d) *= tens[j1 & 0xf];
398  for(j = j1 >> 4; j; j >>= 1, i++)
399  {
400  {
401  if(j & 1)
402  {
403  ieps++;
404  dval(d) *= bigtens[i];
405  }
406  }
407  }
408  }
409  }
410  if(k_check && dval(d) < 1. && ilim > 0)
411  {
412  if(ilim1 <= 0)
413  {
414  {
415  goto fast_failed;
416  }
417  }
418  ilim = ilim1;
419  k--;
420  dval(d) *= 10.;
421  ieps++;
422  }
423  dval(eps) = ieps * dval(d) + 7.;
424  word0(eps) -= (P - 1) * Exp_msk1;
425  if(ilim == 0)
426  {
427  S = mhi = 0;
428  dval(d) -= 5.;
429  if(dval(d) > dval(eps))
430  {
431  {
432  goto one_digit;
433  }
434  }
435  if(dval(d) < -dval(eps))
436  {
437  {
438  goto no_digits;
439  }
440  }
441  goto fast_failed;
442  }
443 #ifndef No_leftright
444  if(leftright)
445  {
446  /* Use Steele & White method of only
447  * generating digits needed.
448  */
449  dval(eps) = ds * 0.5 / tens[ilim - 1] - dval(eps);
450  for(i = 0;;)
451  {
452  L = (Long)(dval(d) / ds);
453  dval(d) -= L * ds;
454  *s++ = (char)('0' + (int)L);
455  if(dval(d) < dval(eps))
456  {
457  if(dval(d) > 0.0)
458  {
459  inex = STRTOG_Inexlo;
460  }
461  goto ret1;
462  }
463  if(ds - dval(d) < dval(eps))
464  {
465  goto bump_up;
466  }
467  if(++i >= ilim)
468  {
469  break;
470  }
471  dval(eps) *= 10.;
472  dval(d) *= 10.;
473  }
474  }
475  else
476  {
477 #endif
478  /* Generate ilim digits, then fix them up. */
479  dval(eps) *= tens[ilim - 1];
480  for(i = 1;; i++, dval(d) *= 10.)
481  {
482  if((L = (Long)(dval(d) / ds)) != 0)
483  {
484  dval(d) -= L * ds;
485  }
486  *s++ = (char)('0' + (int)L);
487  if(i == ilim)
488  {
489  ds *= 0.5;
490  if(dval(d) > ds + dval(eps))
491  {
492  goto bump_up;
493  }
494  else if(dval(d) < ds - dval(eps))
495  {
496  while(*--s == '0')
497  {
498  }
499  s++;
500  if(dval(d) > 0.0)
501  {
502  inex = STRTOG_Inexlo;
503  }
504  goto ret1;
505  }
506  break;
507  }
508  }
509 #ifndef No_leftright
510  }
511 #endif
512  fast_failed:
513  s = s0;
514  dval(d) = d2;
515  k = k0;
516  ilim = ilim0;
517  }
518 
519  /* Do we have a "small" integer? */
520 
521  if(be >= 0 && k <= Int_max)
522  {
523  /* Yes. */
524  ds = tens[k];
525  if(ndigits < 0 && ilim <= 0)
526  {
527  S = mhi = 0;
528  if(ilim < 0 || dval(d) <= 5 * ds)
529  {
530  {
531  goto no_digits;
532  }
533  }
534  goto one_digit;
535  }
536  for(i = 1;; i++, dval(d) *= 10.)
537  {
538  L = (int)(dval(d) / ds);
539  dval(d) -= L * ds;
540 #ifdef Check_FLT_ROUNDS
541  /* If FLT_ROUNDS == 2, L will usually be high by 1 */
542  if(dval(d) < 0)
543  {
544  L--;
545  dval(d) += ds;
546  }
547 #endif
548  *s++ = (char)('0' + (int)L);
549  if(dval(d) == 0.)
550  {
551  {
552  break;
553  }
554  }
555  if(i == ilim)
556  {
557  if(rdir)
558  {
559  if(rdir == 1)
560  {
561  {
562  goto bump_up;
563  }
564  }
565  inex = STRTOG_Inexlo;
566  goto ret1;
567  }
568  dval(d) += dval(d);
569  if((dval(d) > ds) || ((fabs(dval(d) - ds) <= DBL_EPSILON) && (L & 1)))
570  {
571  bump_up:
572  inex = STRTOG_Inexhi;
573  while(*--s == '9')
574  {
575  {
576  if(s == s0)
577  {
578  k++;
579  *s = '0';
580  break;
581  }
582  }
583  }
584  ++*s++;
585  }
586  else
587  {
588  {
589  inex = STRTOG_Inexlo;
590  }
591  }
592  break;
593  }
594  }
595  goto ret1;
596  }
597 
598  m2 = b2;
599  m5 = b5;
600  mhi = mlo = 0;
601  if(leftright)
602  {
603  if(mode < 2)
604  {
605  i = nbits - bbits;
606  if(be - i++ < fpi->emin)
607  {
608  {
609  /* denormal */
610  i = be - fpi->emin + 1;
611  }
612  }
613  }
614  else
615  {
616  j = ilim - 1;
617  if(m5 >= j)
618  {
619  {
620  m5 -= j;
621  }
622  }
623  else
624  {
625  s5 += j -= m5;
626  b5 += j;
627  m5 = 0;
628  }
629  if((i = ilim) < 0)
630  {
631  m2 -= i;
632  i = 0;
633  }
634  }
635  b2 += i;
636  s2 += i;
637  mhi = i2b(1);
638  }
639  if(m2 > 0 && s2 > 0)
640  {
641  i = m2 < s2 ? m2 : s2;
642  b2 -= i;
643  m2 -= i;
644  s2 -= i;
645  }
646  if(b5 > 0)
647  {
648  if(leftright)
649  {
650  if(m5 > 0)
651  {
652  mhi = pow5mult(mhi, m5);
653  b1 = mult(mhi, b);
654  Bfree(b);
655  b = b1;
656  }
657  if((j = b5 - m5) != 0)
658  {
659  {
660  b = pow5mult(b, j);
661  }
662  }
663  }
664  else
665  {
666  {
667  b = pow5mult(b, b5);
668  }
669  }
670  }
671  S = i2b(1);
672  if(s5 > 0)
673  {
674  {
675  S = pow5mult(S, s5);
676  }
677  }
678 
679  /* Check for special case that d is a normalized power of 2. */
680 
681  spec_case = 0;
682  if(mode < 2)
683  {
684  if(bbits == 1 && be0 > fpi->emin + 1)
685  {
686  /* The special case */
687  b2++;
688  s2++;
689  spec_case = 1;
690  }
691  }
692 
693  /* Arrange for convenient computation of quotients:
694  * shift left if necessary so divisor has 4 leading 0 bits.
695  *
696  * Perhaps we should just compute leading 28 bits of S once
697  * and for all and pass them and a shift to quorem, so it
698  * can do shifts and ors to compute the numerator for q.
699  */
700 #ifdef Pack_32
701  if((i = ((s5 ? 32 - hi0bits(S->x[S->wds - 1]) : 1) + s2) & 0x1f) != 0)
702  {
703  {
704  i = 32 - i;
705  }
706  }
707 #else
708  if((i = ((s5 ? 32 - hi0bits(S->x[S->wds - 1]) : 1) + s2) & 0xf) != 0)
709  i = 16 - i;
710 #endif
711  if(i > 4)
712  {
713  i -= 4;
714  b2 += i;
715  m2 += i;
716  s2 += i;
717  }
718  else if(i < 4)
719  {
720  i += 28;
721  b2 += i;
722  m2 += i;
723  s2 += i;
724  }
725  if(b2 > 0)
726  {
727  {
728  b = lshift(b, b2);
729  }
730  }
731  if(s2 > 0)
732  {
733  {
734  S = lshift(S, s2);
735  }
736  }
737  if(k_check)
738  {
739  if(cmp(b, S) < 0)
740  {
741  k--;
742  b = multadd(b, 10, 0); /* we botched the k estimate */
743  if(leftright)
744  {
745  {
746  mhi = multadd(mhi, 10, 0);
747  }
748  }
749  ilim = ilim1;
750  }
751  }
752  if(ilim <= 0 && mode > 2)
753  {
754  if(ilim < 0 || cmp(b, S = multadd(S, 5, 0)) <= 0)
755  {
756  /* no digits, fcvt style */
757  no_digits:
758  k = -1 - ndigits;
759  inex = STRTOG_Inexlo;
760  goto ret;
761  }
762  one_digit:
763  inex = STRTOG_Inexhi;
764  *s++ = '1';
765  k++;
766  goto ret;
767  }
768  if(leftright)
769  {
770  if(m2 > 0)
771  {
772  {
773  mhi = lshift(mhi, m2);
774  }
775  }
776 
777  /* Compute mlo -- check for special case
778  * that d is a normalized power of 2.
779  */
780 
781  mlo = mhi;
782  if(spec_case)
783  {
784  mhi = Balloc(mhi->k);
785  Bcopy(mhi, mlo);
786  mhi = lshift(mhi, 1);
787  }
788 
789  for(i = 1;; i++)
790  {
791  dig = quorem(b, S) + '0';
792  /* Do we yet have the shortest decimal string
793  * that will round to d?
794  */
795  j = cmp(b, mlo);
796  delta = diff(S, mhi);
797  j1 = delta->sign ? 1 : cmp(b, delta);
798  Bfree(delta);
799 #ifndef ROUND_BIASED
800  if(j1 == 0 && !mode && !(bits[0] & 1) && !rdir)
801  {
802  if(dig == '9')
803  {
804  {
805  goto round_9_up;
806  }
807  }
808  if(j <= 0)
809  {
810  if(b->wds > 1 || b->x[0])
811  {
812  {
813  inex = STRTOG_Inexlo;
814  }
815  }
816  }
817  else
818  {
819  dig++;
820  inex = STRTOG_Inexhi;
821  }
822  *s++ = (char)dig;
823  goto ret;
824  }
825 #endif
826  if((j < 0) || ((j == 0) && !mode
827 #ifndef ROUND_BIASED
828  && !(bits[0] & 1)
829 #endif
830  ))
831  {
832  if(rdir && (b->wds > 1 || b->x[0]))
833  {
834  if(rdir == 2)
835  {
836  inex = STRTOG_Inexlo;
837  goto accept;
838  }
839  while(cmp(S, mhi) > 0)
840  {
841  *s++ = (char)dig;
842  mhi1 = multadd(mhi, 10, 0);
843  if(mlo == mhi)
844  {
845  {
846  mlo = mhi1;
847  }
848  }
849  mhi = mhi1;
850  b = multadd(b, 10, 0);
851  dig = quorem(b, S) + '0';
852  }
853  if(dig++ == '9')
854  {
855  {
856  goto round_9_up;
857  }
858  }
859  inex = STRTOG_Inexhi;
860  goto accept;
861  }
862  if(j1 > 0)
863  {
864  b = lshift(b, 1);
865  j1 = cmp(b, S);
866  if(((j1 > 0) || (j1 == 0 && dig & 1)) && (dig++ == '9'))
867  {
868  {
869  goto round_9_up;
870  }
871  }
872  inex = STRTOG_Inexhi;
873  }
874  if(b->wds > 1 || b->x[0])
875  {
876  {
877  inex = STRTOG_Inexlo;
878  }
879  }
880  accept:
881  *s++ = (char)dig;
882  goto ret;
883  }
884  if(j1 > 0 && rdir != 2)
885  {
886  if(dig == '9')
887  { /* possible if i == 1 */
888  round_9_up:
889  *s++ = '9';
890  inex = STRTOG_Inexhi;
891  goto roundoff;
892  }
893  inex = STRTOG_Inexhi;
894  *s++ = (char)(dig + 1);
895  goto ret;
896  }
897  *s++ = (char)dig;
898  if(i == ilim)
899  {
900  {
901  break;
902  }
903  }
904  b = multadd(b, 10, 0);
905  if(mlo == mhi)
906  {
907  {
908  mlo = mhi = multadd(mhi, 10, 0);
909  }
910  }
911  else
912  {
913  mlo = multadd(mlo, 10, 0);
914  mhi = multadd(mhi, 10, 0);
915  }
916  }
917  }
918  else
919  {
920  {
921  for(i = 1;; i++)
922  {
923  dig = quorem(b, S) + '0';
924  *s++ = (char)dig;
925  if(i >= ilim)
926  {
927  {
928  break;
929  }
930  }
931  b = multadd(b, 10, 0);
932  }
933  }
934  }
935 
936  /* Round off last digit */
937 
938  if(rdir)
939  {
940  if((rdir == 2) || ((b->wds <= 1) && !b->x[0]))
941  {
942  {
943  goto chopzeros;
944  }
945  }
946  goto roundoff;
947  }
948  b = lshift(b, 1);
949  j = cmp(b, S);
950  if((j > 0) || ((j == 0) && (dig & 1)))
951  {
952  roundoff:
953  inex = STRTOG_Inexhi;
954  while(*--s == '9')
955  {
956  {
957  if(s == s0)
958  {
959  k++;
960  *s++ = '1';
961  goto ret;
962  }
963  }
964  }
965  ++*s++;
966  }
967  else
968  {
969  chopzeros:
970  if(b->wds > 1 || b->x[0])
971  {
972  {
973  inex = STRTOG_Inexlo;
974  }
975  }
976  while(*--s == '0')
977  {
978  }
979  s++;
980  }
981 ret:
982  Bfree(S);
983  if(mhi)
984  {
985  if(mlo && mlo != mhi)
986  {
987  {
988  Bfree(mlo);
989  }
990  }
991  Bfree(mhi);
992  }
993 ret1:
994  Bfree(b);
995  *s = 0;
996  *decpt = k + 1;
997  if(rve)
998  {
999  {
1000  *rve = s;
1001  }
1002  }
1003  *kindp |= inex;
1004  return s0;
1005 }
#define ALL_ON
Definition: gdtoaimp.h:469
#define Ten_pmax
Definition: gdtoaimp.h:406
#define bigtens
Definition: gdtoaimp.h:514
#define P
Definition: gdtoaimp.h:399
ULong x[1]
Definition: gdtoaimp.h:488
Bigint * i2b(int i)
Definition: misc.c:271
int wds
Definition: gdtoaimp.h:487
#define dtoa_result
Definition: gdtoaimp.h:520
int trailz(Bigint *b)
Definition: gmisc.c:92
Bigint * lshift(Bigint *b, int k)
Definition: misc.c:495
#define Exp_msk1
Definition: gdtoaimp.h:396
#define Bletch
Definition: gdtoaimp.h:407
void freedtoa(char *s)
Definition: dmisc.c:99
#define Exp_11
Definition: gdtoaimp.h:402
double b2d(Bigint *a, int *e)
Definition: misc.c:724
char * gdtoa(FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, int *decpt, char **rve)
Definition: gdtoa.c:130
char * rv_alloc(int i)
Definition: dmisc.c:42
int sign
Definition: gdtoaimp.h:487
Bigint * diff(Bigint *a, Bigint *b)
Definition: misc.c:617
#define Quick_max
Definition: gdtoaimp.h:415
#define Bcopy(x, y)
Definition: gdtoaimp.h:501
#define tens
Definition: gdtoaimp.h:544
#define dval(x)
Definition: gdtoaimp.h:307
int rounding
Definition: gdtoa.h:91
int nbits
Definition: gdtoa.h:88
#define word0(x)
Definition: gdtoaimp.h:304
int quorem(Bigint *b, Bigint *S)
Definition: dmisc.c:118
#define hi0bits(x)
Definition: gdtoaimp.h:525
#define NULL
Definition: stddef.h:15
static Bigint * bitstob(ULong *bits, int nbits, int *bbits)
Definition: gdtoa.c:42
char * nrv_alloc(char *s, char **rve, int n)
Definition: dmisc.c:68
#define kshift
Definition: gdtoaimp.h:467
unsigned Long ULong
Definition: gdtoa.h:41
void Bfree(Bigint *v)
Definition: misc.c:92
double fabs(double)
Definition: fabs.c:4
int emin
Definition: gdtoa.h:89
void rshift(Bigint *b, int k)
Definition: gmisc.c:39
#define Int_max
Definition: gdtoaimp.h:416
Bigint * multadd(Bigint *b, int m, int a)
Definition: misc.c:171
Bigint * pow5mult(Bigint *b, int k)
Definition: misc.c:420
int cmp(Bigint *a, Bigint *b)
Definition: misc.c:570
Definition: gdtoa.h:86
Bigint * Balloc(int k)
Definition: misc.c:47
Bigint * mult(Bigint *a, Bigint *b)
Definition: misc.c:287
#define Exp_shift
Definition: gdtoaimp.h:394
#define Frac_mask1
Definition: gdtoaimp.h:405
#define Long
Definition: gdtoa.h:38
#define DBL_EPSILON
Definition: float.h:38
#define ROUND_BIASED
Definition: gdtoaimp.h:421
#define ULbits
Definition: gdtoaimp.h:466