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