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