1 /* $NetBSD: gdtoa.c,v 1.4 2008/03/21 23:13:48 christos Exp $ */
3 /****************************************************************
5 The author of this software is David M. Gay.
7 Copyright (C) 1998, 1999 by Lucent Technologies
10 Permission to use, copy, modify, and distribute this software and
11 its documentation for any purpose and without fee is hereby
12 granted, provided that the above copyright notice appear in all
13 copies and that both that the copyright notice and this
14 permission notice and warranty disclaimer appear in supporting
15 documentation, and that the name of Lucent or any of its entities
16 not be used in advertising or publicity pertaining to
17 distribution of the software without specific, written prior
20 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
22 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
23 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
29 ****************************************************************/
31 /* Please send bug reports to David M. Gay (dmg at acm dot org,
32 * with " at " changed at "@" and " dot " changed to "."). */
38 bitstob(bits
, nbits
, bbits
) ULong
*bits
; int nbits
; int *bbits
;
40 bitstob(ULong
*bits
, int nbits
, int *bbits
)
60 be
= bits
+ (((unsigned int)nbits
- 1) >> kshift
);
63 *x
++ = *bits
& ALL_ON
;
65 *x
++ = (*bits
>> 16) & ALL_ON
;
67 } while(++bits
<= be
);
76 *bbits
= i
*ULbits
+ 32 - hi0bits(b
->x
[i
]);
81 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
83 * Inspired by "How to Print Floating-Point Numbers Accurately" by
84 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
87 * 1. Rather than iterating, we use a simple numeric overestimate
88 * to determine k = floor(log10(d)). We scale relevant
89 * quantities using O(log2(k)) rather than O(k) multiplications.
90 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
91 * try to generate digits strictly left to right. Instead, we
92 * compute with fewer bits and propagate the carry if necessary
93 * when rounding the final digit up. This is often faster.
94 * 3. Under the assumption that input will be rounded nearest,
95 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
96 * That is, we allow equality in stopping tests when the
97 * round-nearest rule will give the same floating-point value
98 * as would satisfaction of the stopping test with strict
100 * 4. We remove common factors of powers of 2 from relevant
102 * 5. When converting floating-point integers less than 1e16,
103 * we use floating-point arithmetic rather than resorting
104 * to multiple-precision integers.
105 * 6. When asked to produce fewer than 15 digits, we first try
106 * to get by with floating-point arithmetic; we resort to
107 * multiple-precision integer arithmetic only if we cannot
108 * guarantee that the floating-point calculation has given
109 * the correctly rounded result. For k requested digits and
110 * "uniformly" distributed input, the probability is
111 * something like 10^(k-15) that we must resort to the Long
118 (fpi
, be
, bits
, kindp
, mode
, ndigits
, decpt
, rve
)
119 FPI
*fpi
; int be
; ULong
*bits
;
120 int *kindp
, mode
, ndigits
, *decpt
; char **rve
;
122 (FPI
*fpi
, int be
, ULong
*bits
, int *kindp
, int mode
, int ndigits
, int *decpt
, char **rve
)
125 /* Arguments ndigits and decpt are similar to the second and third
126 arguments of ecvt and fcvt; trailing zeros are suppressed from
127 the returned string. If not null, *rve is set to point
128 to the end of the return value. If d is +-Infinity or NaN,
129 then *decpt is set to 9999.
132 0 ==> shortest string that yields d when read in
133 and rounded to nearest.
134 1 ==> like 0, but with Steele & White stopping rule;
135 e.g. with IEEE P754 arithmetic , mode 0 gives
136 1e23 whereas mode 1 gives 9.999999999999999e22.
137 2 ==> max(1,ndigits) significant digits. This gives a
138 return value similar to that of ecvt, except
139 that trailing zeros are suppressed.
140 3 ==> through ndigits past the decimal point. This
141 gives a return value similar to that from fcvt,
142 except that trailing zeros are suppressed, and
143 ndigits can be negative.
144 4-9 should give the same return values as 2-3, i.e.,
145 4 <= mode <= 9 ==> same return as mode
146 2 + (mode & 1). These modes are mainly for
147 debugging; often they run slower but sometimes
148 faster than modes 2-3.
149 4,5,8,9 ==> left-to-right digit generation.
150 6-9 ==> don't try fast floating-point estimate
153 Values of mode other than 0-9 are treated as mode 0.
155 Sufficient space is allocated to the return value
156 to hold the suppressed trailing zeros.
159 int bbits
, b2
, b5
, be0
, dig
, i
, ieps
, ilim
= 0, ilim0
, ilim1
= 0, inex
;
160 int j
, jj1
, k
, k0
, k_check
, kind
, leftright
, m2
, m5
, nbits
;
161 int rdir
, s2
, s5
, spec_case
, try_quick
;
163 Bigint
*b
, *b1
, *delta
, *mlo
, *mhi
, *mhi1
, *S
;
164 double d
, d2
, ds
, eps
;
167 #ifndef MULTIPLE_THREADS
169 freedtoa(dtoa_result
);
174 if (*kindp
& STRTOG_NoMemory
)
176 kind
= *kindp
&= ~STRTOG_Inexact
;
177 switch(kind
& STRTOG_Retmask
) {
181 case STRTOG_Denormal
:
183 case STRTOG_Infinite
:
185 return nrv_alloc("Infinity", rve
, 8);
188 return nrv_alloc("NaN", rve
, 3);
192 b
= bitstob(bits
, nbits
= fpi
->nbits
, &bbits
);
196 if ( (i
= trailz(b
)) !=0) {
205 return nrv_alloc("0", rve
, 1);
208 dval(d
) = b2d(b
, &i
);
210 word0(d
) &= Frac_mask1
;
213 if ( (j
= 11 - hi0bits(word0(d
) & Frac_mask
)) !=0)
217 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
218 * log10(x) = log(x) / log(10)
219 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
220 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
222 * This suggests computing an approximation k to log10(d) by
224 * k = (i - Bias)*0.301029995663981
225 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
227 * We want k to be too large rather than too small.
228 * The error in the first-order Taylor series approximation
229 * is in our favor, so we just round up the constant enough
230 * to compensate for any error in the multiplication of
231 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
232 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
233 * adding 1e-13 to the constant term more than suffices.
234 * Hence we adjust the constant term to 0.1760912590558.
235 * (We could get a more accurate k by invoking log10,
236 * but this is probably not worthwhile.)
242 ds
= (dval(d
)-1.5)*0.289529654602168 + 0.1760912590558 + i
*0.301029995663981;
244 /* correct assumption about exponent range */
251 if (ds
< 0. && ds
!= k
)
252 k
--; /* want k = floor(ds) */
256 if ( (jj1
= j
& 3) !=0)
258 word0(d
) += j
<< Exp_shift
- 2 & Exp_mask
;
260 word0(d
) += (be
+ bbits
- 1) << Exp_shift
;
262 if (k
>= 0 && k
<= Ten_pmax
) {
263 if (dval(d
) < tens
[k
])
286 if (mode
< 0 || mode
> 9)
298 i
= (int)(nbits
* .30103) + 3;
307 ilim
= ilim1
= i
= ndigits
;
319 s
= s0
= rv_alloc((size_t)i
);
323 if ( (rdir
= fpi
->rounding
- 1) !=0) {
326 if (kind
& STRTOG_Neg
)
330 /* Now rdir = 0 ==> round near, 1 ==> round up, 2 ==> round down. */
332 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
&& !rdir
333 #ifndef IMPRECISE_INEXACT
338 /* Try to get by with floating-point arithmetic. */
343 if ( (j
= 11 - hi0bits(word0(d
) & Frac_mask
)) !=0)
348 ieps
= 2; /* conservative */
351 j
= (unsigned int)k
>> 4;
353 /* prevent overflows */
355 dval(d
) /= bigtens
[n_bigtens
-1];
358 for(; j
; j
/= 2, i
++)
366 if ( (jj1
= -k
) !=0) {
367 dval(d
) *= tens
[jj1
& 0xf];
368 for(j
= jj1
>> 4; j
; j
>>= 1, i
++)
371 dval(d
) *= bigtens
[i
];
375 if (k_check
&& dval(d
) < 1. && ilim
> 0) {
383 dval(eps
) = ieps
*dval(d
) + 7.;
384 word0(eps
) -= (P
-1)*Exp_msk1
;
388 if (dval(d
) > dval(eps
))
390 if (dval(d
) < -dval(eps
))
396 /* Use Steele & White method of only
397 * generating digits needed.
399 dval(eps
) = ds
*0.5/tens
[ilim
-1] - dval(eps
);
401 L
= (Long
)(dval(d
)/ds
);
404 if (dval(d
) < dval(eps
)) {
406 inex
= STRTOG_Inexlo
;
409 if (ds
- dval(d
) < dval(eps
))
419 /* Generate ilim digits, then fix them up. */
420 dval(eps
) *= tens
[ilim
-1];
421 for(i
= 1;; i
++, dval(d
) *= 10.) {
422 if ( (L
= (Long
)(dval(d
)/ds
)) !=0)
427 if (dval(d
) > ds
+ dval(eps
))
429 else if (dval(d
) < ds
- dval(eps
)) {
433 inex
= STRTOG_Inexlo
;
449 /* Do we have a "small" integer? */
451 if (be
>= 0 && k
<= Int_max
) {
454 if (ndigits
< 0 && ilim
<= 0) {
456 if (ilim
< 0 || dval(d
) <= 5*ds
)
460 for(i
= 1;; i
++, dval(d
) *= 10.) {
463 #ifdef Check_FLT_ROUNDS
464 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
477 inex
= STRTOG_Inexlo
;
481 if (dval(d
) > ds
|| (dval(d
) == ds
&& L
& 1)) {
483 inex
= STRTOG_Inexhi
;
493 inex
= STRTOG_Inexlo
;
506 if (be
- i
++ < fpi
->emin
)
508 i
= be
- fpi
->emin
+ 1;
519 if ((i
= ilim
) < 0) {
528 if (m2
> 0 && s2
> 0) {
529 i
= m2
< s2
? m2
: s2
;
537 mhi
= pow5mult(mhi
, m5
);
546 if ( (j
= b5
- m5
) !=0) {
567 /* Check for special case that d is a normalized power of 2. */
571 if (bbits
== 1 && be0
> fpi
->emin
+ 1) {
572 /* The special case */
579 /* Arrange for convenient computation of quotients:
580 * shift left if necessary so divisor has 4 leading 0 bits.
582 * Perhaps we should just compute leading 28 bits of S once
583 * and for all and pass them and a shift to quorem, so it
584 * can do shifts and ors to compute the numerator for q.
587 if ( (i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0x1f) !=0)
590 if ( (i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0xf) !=0)
612 b
= multadd(b
, 10, 0); /* we botched the k estimate */
616 mhi
= multadd(mhi
, 10, 0);
623 if (ilim
<= 0 && mode
> 2) {
624 if (ilim
< 0 || cmp(b
,S
= multadd(S
,5,0)) <= 0) {
625 /* no digits, fcvt style */
628 inex
= STRTOG_Inexlo
;
632 inex
= STRTOG_Inexhi
;
639 mhi
= lshift(mhi
, m2
);
644 /* Compute mlo -- check for special case
645 * that d is a normalized power of 2.
650 mhi
= Balloc(mhi
->k
);
654 mhi
= lshift(mhi
, 1);
660 dig
= quorem(b
,S
) + '0';
661 /* Do we yet have the shortest decimal string
662 * that will round to d?
665 delta
= diff(S
, mhi
);
668 jj1
= delta
->sign
? 1 : cmp(b
, delta
);
671 if (jj1
== 0 && !mode
&& !(bits
[0] & 1) && !rdir
) {
675 if (b
->wds
> 1 || b
->x
[0])
676 inex
= STRTOG_Inexlo
;
680 inex
= STRTOG_Inexhi
;
686 if (j
< 0 || (j
== 0 && !mode
691 if (rdir
&& (b
->wds
> 1 || b
->x
[0])) {
693 inex
= STRTOG_Inexlo
;
696 while (cmp(S
,mhi
) > 0) {
698 mhi1
= multadd(mhi
, 10, 0);
704 b
= multadd(b
, 10, 0);
707 dig
= quorem(b
,S
) + '0';
711 inex
= STRTOG_Inexhi
;
719 if ((jj1
> 0 || (jj1
== 0 && dig
& 1))
722 inex
= STRTOG_Inexhi
;
724 if (b
->wds
> 1 || b
->x
[0])
725 inex
= STRTOG_Inexlo
;
730 if (jj1
> 0 && rdir
!= 2) {
731 if (dig
== '9') { /* possible if i == 1 */
734 inex
= STRTOG_Inexhi
;
737 inex
= STRTOG_Inexhi
;
744 b
= multadd(b
, 10, 0);
748 mlo
= mhi
= multadd(mhi
, 10, 0);
753 mlo
= multadd(mlo
, 10, 0);
756 mhi
= multadd(mhi
, 10, 0);
764 *s
++ = dig
= quorem(b
,S
) + '0';
767 b
= multadd(b
, 10, 0);
772 /* Round off last digit */
775 if (rdir
== 2 || (b
->wds
<= 1 && !b
->x
[0]))
783 if (j
> 0 || (j
== 0 && dig
& 1)) {
785 inex
= STRTOG_Inexhi
;
796 if (b
->wds
> 1 || b
->x
[0])
797 inex
= STRTOG_Inexlo
;
804 if (mlo
&& mlo
!= mhi
)