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