1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998, 1999 by Lucent Technologies
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
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
27 ****************************************************************/
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
33 #include <sys/config.h>
40 bitstob(ptr
, bits
, nbits
, bbits
) ULong
*bits
;
41 struct _reent ptr
, int nbits
; int *bbits
;
43 bitstob(struct _reent
*ptr
, ULong
*bits
, int nbits
, int *bbits
)
63 be
= bits
+ ((nbits
- 1) >> kshift
);
66 *x
++ = *bits
& ALL_ON
;
68 *x
++ = (*bits
>> 16) & ALL_ON
;
70 } while(++bits
<= be
);
79 *bbits
= i
*ULbits
+ 32 - hi0bits(b
->_x
[i
]);
84 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
86 * Inspired by "How to Print Floating-Point Numbers Accurately" by
87 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
90 * 1. Rather than iterating, we use a simple numeric overestimate
91 * to determine k = floor(log10(d)). We scale relevant
92 * quantities using O(log2(k)) rather than O(k) multiplications.
93 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
94 * try to generate digits strictly left to right. Instead, we
95 * compute with fewer bits and propagate the carry if necessary
96 * when rounding the final digit up. This is often faster.
97 * 3. Under the assumption that input will be rounded nearest,
98 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
99 * That is, we allow equality in stopping tests when the
100 * round-nearest rule will give the same floating-point value
101 * as would satisfaction of the stopping test with strict
103 * 4. We remove common factors of powers of 2 from relevant
105 * 5. When converting floating-point integers less than 1e16,
106 * we use floating-point arithmetic rather than resorting
107 * to multiple-precision integers.
108 * 6. When asked to produce fewer than 15 digits, we first try
109 * to get by with floating-point arithmetic; we resort to
110 * multiple-precision integer arithmetic only if we cannot
111 * guarantee that the floating-point calculation has given
112 * the correctly rounded result. For k requested digits and
113 * "uniformly" distributed input, the probability is
114 * something like 10^(k-15) that we must resort to the Long
121 (ptr
, fpi
, be
, bits
, kindp
, mode
, ndigits
, decpt
, rve
)
122 struct _reent
*ptr
, FPI
*fpi
; int be
; ULong
*bits
;
123 int *kindp
, mode
, ndigits
, *decpt
; char **rve
;
125 (struct _reent
*ptr
, FPI
*fpi
, int be
, ULong
*bits
, int *kindp
,
126 int mode
, int ndigits
, int *decpt
, char **rve
)
129 /* Arguments ndigits and decpt are similar to the second and third
130 arguments of ecvt and fcvt; trailing zeros are suppressed from
131 the returned string. If not null, *rve is set to point
132 to the end of the return value. If d is +-Infinity or NaN,
133 then *decpt is set to 9999.
134 be = exponent: value = (integer represented by bits) * (2 to the power of be).
137 0 ==> shortest string that yields d when read in
138 and rounded to nearest.
139 1 ==> like 0, but with Steele & White stopping rule;
140 e.g. with IEEE P754 arithmetic , mode 0 gives
141 1e23 whereas mode 1 gives 9.999999999999999e22.
142 2 ==> max(1,ndigits) significant digits. This gives a
143 return value similar to that of ecvt, except
144 that trailing zeros are suppressed.
145 3 ==> through ndigits past the decimal point. This
146 gives a return value similar to that from fcvt,
147 except that trailing zeros are suppressed, and
148 ndigits can be negative.
149 4-9 should give the same return values as 2-3, i.e.,
150 4 <= mode <= 9 ==> same return as mode
151 2 + (mode & 1). These modes are mainly for
152 debugging; often they run slower but sometimes
153 faster than modes 2-3.
154 4,5,8,9 ==> left-to-right digit generation.
155 6-9 ==> don't try fast floating-point estimate
158 Values of mode other than 0-9 are treated as mode 0.
160 Sufficient space is allocated to the return value
161 to hold the suppressed trailing zeros.
164 int bbits
, b2
, b5
, be0
, dig
, i
, ieps
, ilim
, ilim0
, ilim1
, inex
;
165 int j
, j1
, k
, k0
, k_check
, kind
, leftright
, m2
, m5
, nbits
;
166 int rdir
, s2
, s5
, spec_case
, try_quick
;
168 Bigint
*b
, *b1
, *delta
, *mlo
, *mhi
, *mhi1
, *S
;
173 #ifndef MULTIPLE_THREADS
175 freedtoa(ptr
, dtoa_result
);
180 kind
= *kindp
&= ~STRTOG_Inexact
;
181 switch(kind
& STRTOG_Retmask
) {
185 case STRTOG_Denormal
:
187 case STRTOG_Infinite
:
189 return nrv_alloc(ptr
, "Infinity", rve
, 8);
192 return nrv_alloc(ptr
, "NaN", rve
, 3);
196 b
= bitstob(ptr
, bits
, nbits
= fpi
->nbits
, &bbits
);
200 if ( (i
= trailz(b
)) !=0) {
209 return nrv_alloc(ptr
, "0", rve
, 1);
212 dval(d
) = b2d(b
, &i
);
214 word0(d
) &= Frac_mask1
;
217 if ( (j
= 11 - hi0bits(word0(d
) & Frac_mask
)) !=0)
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)
226 * This suggests computing an approximation k to log10(&d) by
228 * k = (i - Bias)*0.301029995663981
229 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
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.)
246 ds
= (dval(d
)-1.5)*0.289529654602168 + 0.1760912590558 + i
*0.301029995663981;
248 /* correct assumption about exponent range */
255 if (ds
< 0. && ds
!= k
)
256 k
--; /* want k = floor(ds) */
260 if ( (j1
= j
& 3) !=0)
262 word0(d
) += j
<< Exp_shift
- 2 & Exp_mask
;
264 word0(d
) += (be
+ bbits
- 1) << Exp_shift
;
266 if (k
>= 0 && k
<= Ten_pmax
) {
267 if (dval(d
) < tens
[k
])
290 if (mode
< 0 || mode
> 9)
297 else if (i
>= -4 - Emin
|| i
< Emin
)
300 ilim
= ilim1
= -1; /* Values for cases 0 and 1; done here to */
301 /* silence erroneous "gcc -Wall" warning. */
305 i
= (int)(nbits
* .30103) + 3;
314 ilim
= ilim1
= i
= ndigits
;
326 s
= s0
= rv_alloc(ptr
, i
);
330 if ( (rdir
= fpi
->rounding
- 1) !=0) {
333 if (kind
& STRTOG_Neg
)
337 /* Now rdir = 0 ==> round near, 1 ==> round up, 2 ==> round down. */
339 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
&& !rdir
340 #ifndef IMPRECISE_INEXACT
345 /* Try to get by with floating-point arithmetic. */
350 if ( (j
= 11 - hi0bits(word0(d
) & Frac_mask
)) !=0)
355 ieps
= 2; /* conservative */
360 /* prevent overflows */
362 dval(d
) /= bigtens
[n_bigtens
-1];
365 for(; j
; j
>>= 1, i
++)
373 if ( (j1
= -k
) !=0) {
374 dval(d
) *= tens
[j1
& 0xf];
375 for(j
= j1
>> 4; j
; j
>>= 1, i
++)
378 dval(d
) *= bigtens
[i
];
382 if (k_check
&& dval(d
) < 1. && ilim
> 0) {
390 dval(eps
) = ieps
*dval(d
) + 7.;
391 word0(eps
) -= (P
-1)*Exp_msk1
;
395 if (dval(d
) > dval(eps
))
397 if (dval(d
) < -dval(eps
))
403 /* Use Steele & White method of only
404 * generating digits needed.
406 dval(eps
) = ds
*0.5/tens
[ilim
-1] - dval(eps
);
408 L
= (Long
)(dval(d
)/ds
);
411 if (dval(d
) < dval(eps
)) {
413 inex
= STRTOG_Inexlo
;
416 if (ds
- dval(d
) < dval(eps
))
426 /* Generate ilim digits, then fix them up. */
427 dval(eps
) *= tens
[ilim
-1];
428 for(i
= 1;; i
++, dval(d
) *= 10.) {
429 if ( (L
= (Long
)(dval(d
)/ds
)) !=0)
434 if (dval(d
) > ds
+ dval(eps
))
436 else if (dval(d
) < ds
- dval(eps
)) {
438 inex
= STRTOG_Inexlo
;
439 goto clear_trailing0
;
454 /* Do we have a "small" integer? */
456 if (be
>= 0 && k
<= Int_max
) {
459 if (ndigits
< 0 && ilim
<= 0) {
461 if (ilim
< 0 || dval(d
) <= 5*ds
)
465 for(i
= 1;; i
++, dval(d
) *= 10.) {
468 #ifdef Check_FLT_ROUNDS
469 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
482 inex
= STRTOG_Inexlo
;
489 if (dval(d
) > ds
|| (dval(d
) == ds
&& L
& 1))
493 inex
= STRTOG_Inexhi
;
503 inex
= STRTOG_Inexlo
;
519 if (be
- i
++ < fpi
->emin
&& mode
!= 3 && mode
!= 5) {
521 i
= be
- fpi
->emin
+ 1;
522 if (mode
>= 2 && ilim
> 0 && ilim
< i
)
525 else if (mode
>= 2) {
535 if ((i
= ilim
) < 0) {
546 if (m2
> 0 && s2
> 0) {
547 i
= m2
< s2
? m2
: s2
;
555 mhi
= pow5mult(ptr
, mhi
, m5
);
558 b1
= mult(ptr
, mhi
, b
);
564 if ( (j
= b5
- m5
) !=0) {
565 b
= pow5mult(ptr
, b
, j
);
571 b
= pow5mult(ptr
, b
, b5
);
580 S
= pow5mult(ptr
, S
, s5
);
585 /* Check for special case that d is a normalized power of 2. */
589 if (bbits
== 1 && be0
> fpi
->emin
+ 1) {
590 /* The special case */
597 /* Arrange for convenient computation of quotients:
598 * shift left if necessary so divisor has 4 leading 0 bits.
600 * Perhaps we should just compute leading 28 bits of S once
601 * and for all and pass them and a shift to quorem, so it
602 * can do shifts and ors to compute the numerator for q.
604 i
= ((s5
? hi0bits(S
->_x
[S
->_wds
-1]) : ULbits
- 1) - s2
- 4) & kmask
;
607 b
= lshift(ptr
, b
, b2
);
612 S
= lshift(ptr
, S
, s2
);
619 b
= multadd(ptr
, b
, 10, 0); /* we botched the k estimate */
623 mhi
= multadd(ptr
, mhi
, 10, 0);
630 if (ilim
<= 0 && mode
> 2) {
631 S
= multadd(ptr
, S
,5,0);
634 if (ilim
< 0 || cmp(b
,S
) <= 0) {
635 /* no digits, fcvt style */
638 inex
= STRTOG_Inexlo
;
642 inex
= STRTOG_Inexhi
;
649 mhi
= lshift(ptr
, mhi
, m2
);
654 /* Compute mlo -- check for special case
655 * that d is a normalized power of 2.
660 mhi
= Balloc(ptr
, mhi
->_k
);
664 mhi
= lshift(ptr
, mhi
, 1);
670 dig
= quorem(b
,S
) + '0';
671 /* Do we yet have the shortest decimal string
672 * that will round to d?
675 delta
= diff(ptr
, S
, mhi
);
678 j1
= delta
->_sign
? 1 : cmp(b
, delta
);
681 if (j1
== 0 && !mode
&& !(bits
[0] & 1) && !rdir
) {
685 if (b
->_wds
> 1 || b
->_x
[0])
686 inex
= STRTOG_Inexlo
;
690 inex
= STRTOG_Inexhi
;
696 if (j
< 0 || (j
== 0 && !mode
701 if (rdir
&& (b
->_wds
> 1 || b
->_x
[0])) {
703 inex
= STRTOG_Inexlo
;
706 while (cmp(S
,mhi
) > 0) {
708 mhi1
= multadd(ptr
, mhi
, 10, 0);
714 b
= multadd(ptr
, b
, 10, 0);
717 dig
= quorem(b
,S
) + '0';
721 inex
= STRTOG_Inexhi
;
725 b
= lshift(ptr
, b
, 1);
732 if ((j1
> 0 || (j1
== 0 && dig
& 1))
736 inex
= STRTOG_Inexhi
;
738 if (b
->_wds
> 1 || b
->_x
[0])
739 inex
= STRTOG_Inexlo
;
744 if (j1
> 0 && rdir
!= 2) {
745 if (dig
== '9') { /* possible if i == 1 */
748 inex
= STRTOG_Inexhi
;
751 inex
= STRTOG_Inexhi
;
758 b
= multadd(ptr
, b
, 10, 0);
762 mlo
= mhi
= multadd(ptr
, mhi
, 10, 0);
767 mlo
= multadd(ptr
, mlo
, 10, 0);
770 mhi
= multadd(ptr
, mhi
, 10, 0);
778 *s
++ = dig
= quorem(b
,S
) + '0';
781 b
= multadd(ptr
, b
, 10, 0);
786 /* Round off last digit */
789 if (rdir
== 2 || (b
->_wds
<= 1 && !b
->_x
[0]))
793 b
= lshift(ptr
, b
, 1);
800 if (j
> 0 || (j
== 0 && dig
& 1))
804 inex
= STRTOG_Inexhi
;
815 if (b
->_wds
> 1 || b
->_x
[0])
816 inex
= STRTOG_Inexlo
;
823 if (mlo
&& mlo
!= mhi
)
837 #endif /* _USE_GDTOA */