* strtod.c: added missing Long define for big endian systems
[mono.git] / mono / utils / strtod.c
1 /*
2  * This strtod has been modified to not use values from the locale,
3  * but to hardcode the `.' as the separator.  Our class libraries will
4  * make sure that only the dot is passed.
5  *
6  * This is so we do not call `setlocale' from our runtime before doing
7  * a strtod, because this could have unwanted effects in code that is
8  * co-hosted with the Mono runtime
9  *
10  * The entry point has been renamed `bsd_strtod'.
11  *
12  * Taken from the FreeBSD distribution.
13  */
14 #include "strtod.h"
15
16 /*-
17  * Copyright (c) 1993
18  *      The Regents of the University of California.  All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright
26  *    notice, this list of conditions and the following disclaimer in the
27  *    documentation and/or other materials provided with the distribution.
28  * 3. All advertising materials mentioning features or use of this software
29  *    must display the following acknowledgement:
30  *      This product includes software developed by the University of
31  *      California, Berkeley and its contributors.
32  * 4. Neither the name of the University nor the names of its contributors
33  *    may be used to endorse or promote products derived from this software
34  *    without specific prior written permission.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  *
48  * $FreeBSD: src/lib/libc/stdlib/strtod.c,v 1.3.8.3 2002/04/17 12:01:21 ache Exp $
49  */
50
51 #if defined(LIBC_SCCS) && !defined(lint)
52 static char sccsid[] = "@(#)strtod.c    8.1 (Berkeley) 6/4/93";
53 #endif /* LIBC_SCCS and not lint */
54
55 /****************************************************************
56  *
57  * The author of this software is David M. Gay.
58  *
59  * Copyright (c) 1991 by AT&T.
60  *
61  * Permission to use, copy, modify, and distribute this software for any
62  * purpose without fee is hereby granted, provided that this entire notice
63  * is included in all copies of any software which is or includes a copy
64  * or modification of this software and in all copies of the supporting
65  * documentation for such software.
66  *
67  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
68  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
69  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
70  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
71  *
72  ***************************************************************/
73
74 /* Please send bug reports to
75         David M. Gay
76         AT&T Bell Laboratories, Room 2C-463
77         600 Mountain Avenue
78         Murray Hill, NJ 07974-2070
79         U.S.A.
80         dmg@research.att.com or research!dmg
81  */
82
83 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
84  *
85  * This strtod returns a nearest machine number to the input decimal
86  * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
87  * broken by the IEEE round-even rule.  Otherwise ties are broken by
88  * biased rounding (add half and chop).
89  *
90  * Inspired loosely by William D. Clinger's paper "How to Read Floating
91  * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
92  *
93  * Modifications:
94  *
95  *      1. We only require IEEE, IBM, or VAX double-precision
96  *              arithmetic (not IEEE double-extended).
97  *      2. We get by with floating-point arithmetic in a case that
98  *              Clinger missed -- when we're computing d * 10^n
99  *              for a small integer d and the integer n is not too
100  *              much larger than 22 (the maximum integer k for which
101  *              we can represent 10^k exactly), we may be able to
102  *              compute (d*10^k) * 10^(e-k) with just one roundoff.
103  *      3. Rather than a bit-at-a-time adjustment of the binary
104  *              result in the hard case, we use floating-point
105  *              arithmetic to determine the adjustment to within
106  *              one bit; only in really hard cases do we need to
107  *              compute a second residual.
108  *      4. Because of 3., we don't need a large table of powers of 10
109  *              for ten-to-e (just some small tables, e.g. of 10^k
110  *              for 0 <= k <= 22).
111  */
112
113 /*
114  * #define IEEE_8087 for IEEE-arithmetic machines where the least
115  *      significant byte has the lowest address.
116  * #define IEEE_MC68k for IEEE-arithmetic machines where the most
117  *      significant byte has the lowest address.
118  * #define Sudden_Underflow for IEEE-format machines without gradual
119  *      underflow (i.e., that flush to zero on underflow).
120  * #define IBM for IBM mainframe-style floating-point arithmetic.
121  * #define VAX for VAX-style floating-point arithmetic.
122  * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
123  * #define No_leftright to omit left-right logic in fast floating-point
124  *      computation of dtoa.
125  * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
126  * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
127  *      that use extended-precision instructions to compute rounded
128  *      products and quotients) with IBM.
129  * #define ROUND_BIASED for IEEE-format with biased rounding.
130  * #define Inaccurate_Divide for IEEE-format with correctly rounded
131  *      products but inaccurate quotients, e.g., for Intel i860.
132  * #define Just_16 to store 16 bits per 32-bit long when doing high-precision
133  *      integer arithmetic.  Whether this speeds things up or slows things
134  *      down depends on the machine and the number being converted.
135  * #define KR_headers for old-style C function headers.
136  * #define Bad_float_h if your system lacks a float.h or if it does not
137  *      define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
138  *      FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
139  */
140
141 #if defined(i386) || defined(mips) && defined(MIPSEL) || defined (__arm__)
142
143 #define IEEE_8087
144 #define Long long
145
146 #elif defined(__ia64)
147
148 # ifndef __LP64__
149 #  define Long long
150 # else
151 #  define Long int
152 # endif
153 # ifdef __hpux
154 #  define IEEE_MC68k
155 # else
156 #  define IEEE_8087
157 # endif
158
159 #elif defined(__hppa)
160
161 # define IEEE_MC68k
162 # ifndef __LP64__
163 #  define Long long
164 # else
165 #  define Long int
166 # endif
167
168 #else
169 #define IEEE_MC68k
170 #define Long long
171 #endif
172
173 #define ULong unsigned Long
174
175 #ifdef DEBUG
176 #include "stdio.h"
177 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
178 #endif
179
180 #include <locale.h>
181 #ifdef __cplusplus
182 #include "malloc.h"
183 #include "memory.h"
184 #else
185 #ifndef KR_headers
186 #include "stdlib.h"
187 #include "string.h"
188 #else
189 #include "malloc.h"
190 #include "memory.h"
191 #endif
192 #endif
193
194 #include "errno.h"
195 #include <ctype.h>
196 #ifdef Bad_float_h
197 #undef __STDC__
198 #ifdef IEEE_MC68k
199 #define IEEE_ARITHMETIC
200 #endif
201 #ifdef IEEE_8087
202 #define IEEE_ARITHMETIC
203 #endif
204 #ifdef IEEE_ARITHMETIC
205 #define DBL_DIG 15
206 #define DBL_MAX_10_EXP 308
207 #define DBL_MAX_EXP 1024
208 #define FLT_RADIX 2
209 #define FLT_ROUNDS 1
210 #define DBL_MAX 1.7976931348623157e+308
211 #endif
212
213 #ifdef IBM
214 #define DBL_DIG 16
215 #define DBL_MAX_10_EXP 75
216 #define DBL_MAX_EXP 63
217 #define FLT_RADIX 16
218 #define FLT_ROUNDS 0
219 #define DBL_MAX 7.2370055773322621e+75
220 #endif
221
222 #ifdef VAX
223 #define DBL_DIG 16
224 #define DBL_MAX_10_EXP 38
225 #define DBL_MAX_EXP 127
226 #define FLT_RADIX 2
227 #define FLT_ROUNDS 1
228 #define DBL_MAX 1.7014118346046923e+38
229 #endif
230
231 #ifndef LONG_MAX
232 #define LONG_MAX 2147483647
233 #endif
234 #else
235 #include "float.h"
236 #endif
237 #ifndef __MATH_H__
238 #include "math.h"
239 #endif
240
241 #ifdef __cplusplus
242 extern "C" {
243 #endif
244
245 #ifndef CONST
246 #ifdef KR_headers
247 #define CONST /* blank */
248 #else
249 #define CONST const
250 #endif
251 #endif
252
253 #ifdef Unsigned_Shifts
254 #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
255 #else
256 #define Sign_Extend(a,b) /*no-op*/
257 #endif
258
259 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
260 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
261 #endif
262
263 #ifdef IEEE_8087
264 #define word0(x) ((ULong *)&x)[1]
265 #define word1(x) ((ULong *)&x)[0]
266 #else
267 #define word0(x) ((ULong *)&x)[0]
268 #define word1(x) ((ULong *)&x)[1]
269 #endif
270
271 /* The following definition of Storeinc is appropriate for MIPS processors.
272  * An alternative that might be better on some machines is
273  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
274  */
275 #if defined(IEEE_8087) + defined(VAX)
276 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
277 ((unsigned short *)a)[0] = (unsigned short)c, a++)
278 #else
279 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
280 ((unsigned short *)a)[1] = (unsigned short)c, a++)
281 #endif
282
283 /* #define P DBL_MANT_DIG */
284 /* Ten_pmax = floor(P*log(2)/log(5)) */
285 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
286 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
287 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
288
289 #if defined(IEEE_8087) + defined(IEEE_MC68k)
290 #define Exp_shift  20
291 #define Exp_shift1 20
292 #define Exp_msk1    0x100000
293 #define Exp_msk11   0x100000
294 #define Exp_mask  0x7ff00000
295 #define P 53
296 #define Bias 1023
297 #define IEEE_Arith
298 #define Emin (-1022)
299 #define Exp_1  0x3ff00000
300 #define Exp_11 0x3ff00000
301 #define Ebits 11
302 #define Frac_mask  0xfffff
303 #define Frac_mask1 0xfffff
304 #define Ten_pmax 22
305 #define Bletch 0x10
306 #define Bndry_mask  0xfffff
307 #define Bndry_mask1 0xfffff
308 #define LSB 1
309 #define Sign_bit 0x80000000
310 #define Log2P 1
311 #define Tiny0 0
312 #define Tiny1 1
313 #define Quick_max 14
314 #define Int_max 14
315 #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
316 #else
317 #undef  Sudden_Underflow
318 #define Sudden_Underflow
319 #ifdef IBM
320 #define Exp_shift  24
321 #define Exp_shift1 24
322 #define Exp_msk1   0x1000000
323 #define Exp_msk11  0x1000000
324 #define Exp_mask  0x7f000000
325 #define P 14
326 #define Bias 65
327 #define Exp_1  0x41000000
328 #define Exp_11 0x41000000
329 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
330 #define Frac_mask  0xffffff
331 #define Frac_mask1 0xffffff
332 #define Bletch 4
333 #define Ten_pmax 22
334 #define Bndry_mask  0xefffff
335 #define Bndry_mask1 0xffffff
336 #define LSB 1
337 #define Sign_bit 0x80000000
338 #define Log2P 4
339 #define Tiny0 0x100000
340 #define Tiny1 0
341 #define Quick_max 14
342 #define Int_max 15
343 #else /* VAX */
344 #define Exp_shift  23
345 #define Exp_shift1 7
346 #define Exp_msk1    0x80
347 #define Exp_msk11   0x800000
348 #define Exp_mask  0x7f80
349 #define P 56
350 #define Bias 129
351 #define Exp_1  0x40800000
352 #define Exp_11 0x4080
353 #define Ebits 8
354 #define Frac_mask  0x7fffff
355 #define Frac_mask1 0xffff007f
356 #define Ten_pmax 24
357 #define Bletch 2
358 #define Bndry_mask  0xffff007f
359 #define Bndry_mask1 0xffff007f
360 #define LSB 0x10000
361 #define Sign_bit 0x8000
362 #define Log2P 1
363 #define Tiny0 0x80
364 #define Tiny1 0
365 #define Quick_max 15
366 #define Int_max 15
367 #endif
368 #endif
369
370 #ifndef IEEE_Arith
371 #define ROUND_BIASED
372 #endif
373
374 #ifdef RND_PRODQUOT
375 #define rounded_product(a,b) a = rnd_prod(a, b)
376 #define rounded_quotient(a,b) a = rnd_quot(a, b)
377 #ifdef KR_headers
378 extern double rnd_prod(), rnd_quot();
379 #else
380 extern double rnd_prod(double, double), rnd_quot(double, double);
381 #endif
382 #else
383 #define rounded_product(a,b) a *= b
384 #define rounded_quotient(a,b) a /= b
385 #endif
386
387 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
388 #define Big1 0xffffffff
389
390 #ifndef Just_16
391 /* When Pack_32 is not defined, we store 16 bits per 32-bit long.
392  * This makes some inner loops simpler and sometimes saves work
393  * during multiplications, but it often seems to make things slightly
394  * slower.  Hence the default is now to store 32 bits per long.
395  */
396 #ifndef Pack_32
397 #define Pack_32
398 #endif
399 #endif
400
401 #define Kmax 15
402
403 #ifdef __cplusplus
404 extern "C" double bsd_strtod(const char *s00, char **se);
405 extern "C" char *__dtoa(double d, int mode, int ndigits,
406                         int *decpt, int *sign, char **rve, char **resultp);
407 #endif
408
409  struct
410 Bigint {
411         struct Bigint *next;
412         int k, maxwds, sign, wds;
413         ULong x[1];
414 };
415
416  typedef struct Bigint Bigint;
417
418  static Bigint *
419 Balloc
420 #ifdef KR_headers
421         (k) int k;
422 #else
423         (int k)
424 #endif
425 {
426         int x;
427         Bigint *rv;
428
429         x = 1 << k;
430         rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(Long));
431         rv->k = k;
432         rv->maxwds = x;
433         rv->sign = rv->wds = 0;
434         return rv;
435 }
436
437  static void
438 Bfree
439 #ifdef KR_headers
440         (v) Bigint *v;
441 #else
442         (Bigint *v)
443 #endif
444 {
445         free(v);
446 }
447
448 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
449 y->wds*sizeof(Long) + 2*sizeof(int))
450
451  static Bigint *
452 multadd
453 #ifdef KR_headers
454         (b, m, a) Bigint *b; int m, a;
455 #else
456         (Bigint *b, int m, int a)       /* multiply by m and add a */
457 #endif
458 {
459         int i, wds;
460         ULong *x, y;
461 #ifdef Pack_32
462         ULong xi, z;
463 #endif
464         Bigint *b1;
465
466         wds = b->wds;
467         x = b->x;
468         i = 0;
469         do {
470 #ifdef Pack_32
471                 xi = *x;
472                 y = (xi & 0xffff) * m + a;
473                 z = (xi >> 16) * m + (y >> 16);
474                 a = (int)(z >> 16);
475                 *x++ = (z << 16) + (y & 0xffff);
476 #else
477                 y = *x * m + a;
478                 a = (int)(y >> 16);
479                 *x++ = y & 0xffff;
480 #endif
481         } while (++i < wds);
482         if (a) {
483                 if (wds >= b->maxwds) {
484                         b1 = Balloc(b->k+1);
485                         Bcopy(b1, b);
486                         Bfree(b);
487                         b = b1;
488                         }
489                 b->x[wds++] = a;
490                 b->wds = wds;
491         }
492         return b;
493 }
494
495  static Bigint *
496 s2b
497 #ifdef KR_headers
498         (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9;
499 #else
500         (CONST char *s, int nd0, int nd, ULong y9)
501 #endif
502 {
503         Bigint *b;
504         int i, k;
505         Long x, y;
506
507         x = (nd + 8) / 9;
508         for (k = 0, y = 1; x > y; y <<= 1, k++) ;
509 #ifdef Pack_32
510         b = Balloc(k);
511         b->x[0] = y9;
512         b->wds = 1;
513 #else
514         b = Balloc(k+1);
515         b->x[0] = y9 & 0xffff;
516         b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
517 #endif
518
519         i = 9;
520         if (9 < nd0) {
521                 s += 9;
522                 do
523                         b = multadd(b, 10, *s++ - '0');
524                 while (++i < nd0);
525                 s++;
526         } else
527                 s += 10;
528         for (; i < nd; i++)
529                 b = multadd(b, 10, *s++ - '0');
530         return b;
531 }
532
533  static int
534 hi0bits
535 #ifdef KR_headers
536         (x) register ULong x;
537 #else
538         (register ULong x)
539 #endif
540 {
541         register int k = 0;
542
543         if (!(x & 0xffff0000)) {
544                 k = 16;
545                 x <<= 16;
546         }
547         if (!(x & 0xff000000)) {
548                 k += 8;
549                 x <<= 8;
550         }
551         if (!(x & 0xf0000000)) {
552                 k += 4;
553                 x <<= 4;
554         }
555         if (!(x & 0xc0000000)) {
556                 k += 2;
557                 x <<= 2;
558         }
559         if (!(x & 0x80000000)) {
560                 k++;
561                 if (!(x & 0x40000000))
562                         return 32;
563         }
564         return k;
565 }
566
567  static int
568 lo0bits
569 #ifdef KR_headers
570         (y) ULong *y;
571 #else
572         (ULong *y)
573 #endif
574 {
575         register int k;
576         register ULong x = *y;
577
578         if (x & 7) {
579                 if (x & 1)
580                         return 0;
581                 if (x & 2) {
582                         *y = x >> 1;
583                         return 1;
584                 }
585                 *y = x >> 2;
586                 return 2;
587         }
588         k = 0;
589         if (!(x & 0xffff)) {
590                 k = 16;
591                 x >>= 16;
592         }
593         if (!(x & 0xff)) {
594                 k += 8;
595                 x >>= 8;
596         }
597         if (!(x & 0xf)) {
598                 k += 4;
599                 x >>= 4;
600         }
601         if (!(x & 0x3)) {
602                 k += 2;
603                 x >>= 2;
604         }
605         if (!(x & 1)) {
606                 k++;
607                 x >>= 1;
608                 if (!x & 1)
609                         return 32;
610         }
611         *y = x;
612         return k;
613 }
614
615  static Bigint *
616 i2b
617 #ifdef KR_headers
618         (i) int i;
619 #else
620         (int i)
621 #endif
622 {
623         Bigint *b;
624
625         b = Balloc(1);
626         b->x[0] = i;
627         b->wds = 1;
628         return b;
629         }
630
631  static Bigint *
632 mult
633 #ifdef KR_headers
634         (a, b) Bigint *a, *b;
635 #else
636         (Bigint *a, Bigint *b)
637 #endif
638 {
639         Bigint *c;
640         int k, wa, wb, wc;
641         ULong carry, y, z;
642         ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
643 #ifdef Pack_32
644         ULong z2;
645 #endif
646
647         if (a->wds < b->wds) {
648                 c = a;
649                 a = b;
650                 b = c;
651         }
652         k = a->k;
653         wa = a->wds;
654         wb = b->wds;
655         wc = wa + wb;
656         if (wc > a->maxwds)
657                 k++;
658         c = Balloc(k);
659         for (x = c->x, xa = x + wc; x < xa; x++)
660                 *x = 0;
661         xa = a->x;
662         xae = xa + wa;
663         xb = b->x;
664         xbe = xb + wb;
665         xc0 = c->x;
666 #ifdef Pack_32
667         for (; xb < xbe; xb++, xc0++) {
668                 if ( (y = *xb & 0xffff) ) {
669                         x = xa;
670                         xc = xc0;
671                         carry = 0;
672                         do {
673                                 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
674                                 carry = z >> 16;
675                                 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
676                                 carry = z2 >> 16;
677                                 Storeinc(xc, z2, z);
678                         } while (x < xae);
679                         *xc = carry;
680                 }
681                 if ( (y = *xb >> 16) ) {
682                         x = xa;
683                         xc = xc0;
684                         carry = 0;
685                         z2 = *xc;
686                         do {
687                                 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
688                                 carry = z >> 16;
689                                 Storeinc(xc, z, z2);
690                                 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
691                                 carry = z2 >> 16;
692                         } while (x < xae);
693                         *xc = z2;
694                 }
695         }
696 #else
697         for (; xb < xbe; xc0++) {
698                 if (y = *xb++) {
699                         x = xa;
700                         xc = xc0;
701                         carry = 0;
702                         do {
703                                 z = *x++ * y + *xc + carry;
704                                 carry = z >> 16;
705                                 *xc++ = z & 0xffff;
706                         } while (x < xae);
707                         *xc = carry;
708                 }
709         }
710 #endif
711         for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
712         c->wds = wc;
713         return c;
714 }
715
716  static Bigint *p5s;
717
718  static Bigint *
719 pow5mult
720 #ifdef KR_headers
721         (b, k) Bigint *b; int k;
722 #else
723         (Bigint *b, int k)
724 #endif
725 {
726         Bigint *b1, *p5, *p51;
727         int i;
728         static int p05[3] = { 5, 25, 125 };
729
730         if ( (i = k & 3) )
731                 b = multadd(b, p05[i-1], 0);
732
733         if (!(k >>= 2))
734                 return b;
735         if (!(p5 = p5s)) {
736                 /* first time */
737                 p5 = p5s = i2b(625);
738                 p5->next = 0;
739         }
740         for (;;) {
741                 if (k & 1) {
742                         b1 = mult(b, p5);
743                         Bfree(b);
744                         b = b1;
745                 }
746                 if (!(k >>= 1))
747                         break;
748                 if (!(p51 = p5->next)) {
749                         p51 = p5->next = mult(p5,p5);
750                         p51->next = 0;
751                 }
752                 p5 = p51;
753         }
754         return b;
755 }
756
757  static Bigint *
758 lshift
759 #ifdef KR_headers
760         (b, k) Bigint *b; int k;
761 #else
762         (Bigint *b, int k)
763 #endif
764 {
765         int i, k1, n, n1;
766         Bigint *b1;
767         ULong *x, *x1, *xe, z;
768
769 #ifdef Pack_32
770         n = k >> 5;
771 #else
772         n = k >> 4;
773 #endif
774         k1 = b->k;
775         n1 = n + b->wds + 1;
776         for (i = b->maxwds; n1 > i; i <<= 1)
777                 k1++;
778         b1 = Balloc(k1);
779         x1 = b1->x;
780         for (i = 0; i < n; i++)
781                 *x1++ = 0;
782         x = b->x;
783         xe = x + b->wds;
784 #ifdef Pack_32
785         if (k &= 0x1f) {
786                 k1 = 32 - k;
787                 z = 0;
788                 do {
789                         *x1++ = *x << k | z;
790                         z = *x++ >> k1;
791                 } while (x < xe);
792                 if ( (*x1 = z) )
793                         ++n1;
794         }
795 #else
796         if (k &= 0xf) {
797                 k1 = 16 - k;
798                 z = 0;
799                 do {
800                         *x1++ = *x << k  & 0xffff | z;
801                         z = *x++ >> k1;
802                 } while (x < xe);
803                 if (*x1 = z)
804                         ++n1;
805         }
806 #endif
807         else
808                 do
809                         *x1++ = *x++;
810                 while (x < xe);
811         b1->wds = n1 - 1;
812         Bfree(b);
813         return b1;
814 }
815
816  static int
817 cmp
818 #ifdef KR_headers
819         (a, b) Bigint *a, *b;
820 #else
821         (Bigint *a, Bigint *b)
822 #endif
823 {
824         ULong *xa, *xa0, *xb, *xb0;
825         int i, j;
826
827         i = a->wds;
828         j = b->wds;
829 #ifdef DEBUG
830         if (i > 1 && !a->x[i-1])
831                 Bug("cmp called with a->x[a->wds-1] == 0");
832         if (j > 1 && !b->x[j-1])
833                 Bug("cmp called with b->x[b->wds-1] == 0");
834 #endif
835         if (i -= j)
836                 return i;
837         xa0 = a->x;
838         xa = xa0 + j;
839         xb0 = b->x;
840         xb = xb0 + j;
841         for (;;) {
842                 if (*--xa != *--xb)
843                         return *xa < *xb ? -1 : 1;
844                 if (xa <= xa0)
845                         break;
846         }
847         return 0;
848 }
849
850  static Bigint *
851 diff
852 #ifdef KR_headers
853         (a, b) Bigint *a, *b;
854 #else
855         (Bigint *a, Bigint *b)
856 #endif
857 {
858         Bigint *c;
859         int i, wa, wb;
860         Long borrow, y; /* We need signed shifts here. */
861         ULong *xa, *xae, *xb, *xbe, *xc;
862 #ifdef Pack_32
863         Long z;
864 #endif
865
866         i = cmp(a,b);
867         if (!i) {
868                 c = Balloc(0);
869                 c->wds = 1;
870                 c->x[0] = 0;
871                 return c;
872         }
873         if (i < 0) {
874                 c = a;
875                 a = b;
876                 b = c;
877                 i = 1;
878         } else
879                 i = 0;
880         c = Balloc(a->k);
881         c->sign = i;
882         wa = a->wds;
883         xa = a->x;
884         xae = xa + wa;
885         wb = b->wds;
886         xb = b->x;
887         xbe = xb + wb;
888         xc = c->x;
889         borrow = 0;
890 #ifdef Pack_32
891         do {
892                 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
893                 borrow = y >> 16;
894                 Sign_Extend(borrow, y);
895                 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
896                 borrow = z >> 16;
897                 Sign_Extend(borrow, z);
898                 Storeinc(xc, z, y);
899         } while (xb < xbe);
900         while (xa < xae) {
901                 y = (*xa & 0xffff) + borrow;
902                 borrow = y >> 16;
903                 Sign_Extend(borrow, y);
904                 z = (*xa++ >> 16) + borrow;
905                 borrow = z >> 16;
906                 Sign_Extend(borrow, z);
907                 Storeinc(xc, z, y);
908         }
909 #else
910         do {
911                 y = *xa++ - *xb++ + borrow;
912                 borrow = y >> 16;
913                 Sign_Extend(borrow, y);
914                 *xc++ = y & 0xffff;
915         } while (xb < xbe);
916         while (xa < xae) {
917                 y = *xa++ + borrow;
918                 borrow = y >> 16;
919                 Sign_Extend(borrow, y);
920                 *xc++ = y & 0xffff;
921         }
922 #endif
923         while (!*--xc)
924                 wa--;
925         c->wds = wa;
926         return c;
927 }
928
929  static double
930 ulp
931 #ifdef KR_headers
932         (x) double x;
933 #else
934         (double x)
935 #endif
936 {
937         register Long L;
938         double a;
939
940         L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
941 #ifndef Sudden_Underflow
942         if (L > 0) {
943 #endif
944 #ifdef IBM
945                 L |= Exp_msk1 >> 4;
946 #endif
947                 word0(a) = L;
948                 word1(a) = 0;
949 #ifndef Sudden_Underflow
950         } else {
951                 L = -L >> Exp_shift;
952                 if (L < Exp_shift) {
953                         word0(a) = 0x80000 >> L;
954                         word1(a) = 0;
955                 } else {
956                         word0(a) = 0;
957                         L -= Exp_shift;
958                         word1(a) = L >= 31 ? 1 : 1 << (31 - L);
959                 }
960         }
961 #endif
962         return a;
963 }
964
965  static double
966 b2d
967 #ifdef KR_headers
968         (a, e) Bigint *a; int *e;
969 #else
970         (Bigint *a, int *e)
971 #endif
972 {
973         ULong *xa, *xa0, w, y, z;
974         int k;
975         double d;
976 #ifdef VAX
977         ULong d0, d1;
978 #else
979 #define d0 word0(d)
980 #define d1 word1(d)
981 #endif
982
983         xa0 = a->x;
984         xa = xa0 + a->wds;
985         y = *--xa;
986 #ifdef DEBUG
987         if (!y) Bug("zero y in b2d");
988 #endif
989         k = hi0bits(y);
990         *e = 32 - k;
991 #ifdef Pack_32
992         if (k < Ebits) {
993                 d0 = Exp_1 | (y >> (Ebits - k));
994                 w = xa > xa0 ? *--xa : 0;
995                 d1 = (y << ((32-Ebits) + k)) | (w >> (Ebits - k));
996                 goto ret_d;
997                 }
998         z = xa > xa0 ? *--xa : 0;
999         if (k -= Ebits) {
1000                 d0 = Exp_1 | (y << k) | (z >> (32 - k));
1001                 y = xa > xa0 ? *--xa : 0;
1002                 d1 = (z << k) | (y >> (32 - k));
1003         } else {
1004                 d0 = Exp_1 | y;
1005                 d1 = z;
1006         }
1007 #else
1008         if (k < Ebits + 16) {
1009                 z = xa > xa0 ? *--xa : 0;
1010                 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1011                 w = xa > xa0 ? *--xa : 0;
1012                 y = xa > xa0 ? *--xa : 0;
1013                 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1014                 goto ret_d;
1015         }
1016         z = xa > xa0 ? *--xa : 0;
1017         w = xa > xa0 ? *--xa : 0;
1018         k -= Ebits + 16;
1019         d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1020         y = xa > xa0 ? *--xa : 0;
1021         d1 = w << k + 16 | y << k;
1022 #endif
1023  ret_d:
1024 #ifdef VAX
1025         word0(d) = d0 >> 16 | d0 << 16;
1026         word1(d) = d1 >> 16 | d1 << 16;
1027 #else
1028 #undef d0
1029 #undef d1
1030 #endif
1031         return d;
1032 }
1033
1034  static Bigint *
1035 d2b
1036 #ifdef KR_headers
1037         (d, e, bits) double d; int *e, *bits;
1038 #else
1039         (double d, int *e, int *bits)
1040 #endif
1041 {
1042         Bigint *b;
1043         int de, i, k;
1044         ULong *x, y, z;
1045 #ifdef VAX
1046         ULong d0, d1;
1047         d0 = word0(d) >> 16 | word0(d) << 16;
1048         d1 = word1(d) >> 16 | word1(d) << 16;
1049 #else
1050 #define d0 word0(d)
1051 #define d1 word1(d)
1052 #endif
1053
1054 #ifdef Pack_32
1055         b = Balloc(1);
1056 #else
1057         b = Balloc(2);
1058 #endif
1059         x = b->x;
1060
1061         z = d0 & Frac_mask;
1062         d0 &= 0x7fffffff;       /* clear sign bit, which we ignore */
1063 #ifdef Sudden_Underflow
1064         de = (int)(d0 >> Exp_shift);
1065 #ifndef IBM
1066         z |= Exp_msk11;
1067 #endif
1068 #else
1069         if ( (de = (int)(d0 >> Exp_shift)) )
1070                 z |= Exp_msk1;
1071 #endif
1072 #ifdef Pack_32
1073         if ( (y = d1) ) {
1074                 if ( (k = lo0bits(&y)) ) {
1075                         x[0] = y | (z << (32 - k));
1076                         z >>= k;
1077                         }
1078                 else
1079                         x[0] = y;
1080                 i = b->wds = (x[1] = z) ? 2 : 1;
1081         } else {
1082 #ifdef DEBUG
1083                 if (!z)
1084                         Bug("Zero passed to d2b");
1085 #endif
1086                 k = lo0bits(&z);
1087                 x[0] = z;
1088                 i = b->wds = 1;
1089                 k += 32;
1090         }
1091 #else
1092         if (y = d1) {
1093                 if (k = lo0bits(&y))
1094                         if (k >= 16) {
1095                                 x[0] = y | z << 32 - k & 0xffff;
1096                                 x[1] = z >> k - 16 & 0xffff;
1097                                 x[2] = z >> k;
1098                                 i = 2;
1099                         } else {
1100                                 x[0] = y & 0xffff;
1101                                 x[1] = y >> 16 | z << 16 - k & 0xffff;
1102                                 x[2] = z >> k & 0xffff;
1103                                 x[3] = z >> k+16;
1104                                 i = 3;
1105                         }
1106                 else {
1107                         x[0] = y & 0xffff;
1108                         x[1] = y >> 16;
1109                         x[2] = z & 0xffff;
1110                         x[3] = z >> 16;
1111                         i = 3;
1112                 }
1113         } else {
1114 #ifdef DEBUG
1115                 if (!z)
1116                         Bug("Zero passed to d2b");
1117 #endif
1118                 k = lo0bits(&z);
1119                 if (k >= 16) {
1120                         x[0] = z;
1121                         i = 0;
1122                 } else {
1123                         x[0] = z & 0xffff;
1124                         x[1] = z >> 16;
1125                         i = 1;
1126                 }
1127                 k += 32;
1128         }
1129         while (!x[i])
1130                 --i;
1131         b->wds = i + 1;
1132 #endif
1133 #ifndef Sudden_Underflow
1134         if (de) {
1135 #endif
1136 #ifdef IBM
1137                 *e = (de - Bias - (P-1) << 2) + k;
1138                 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1139 #else
1140                 *e = de - Bias - (P-1) + k;
1141                 *bits = P - k;
1142 #endif
1143 #ifndef Sudden_Underflow
1144         } else {
1145                 *e = de - Bias - (P-1) + 1 + k;
1146 #ifdef Pack_32
1147                 *bits = 32*i - hi0bits(x[i-1]);
1148 #else
1149                 *bits = (i+2)*16 - hi0bits(x[i]);
1150 #endif
1151         }
1152 #endif
1153         return b;
1154 }
1155 #undef d0
1156 #undef d1
1157
1158  static double
1159 ratio
1160 #ifdef KR_headers
1161         (a, b) Bigint *a, *b;
1162 #else
1163         (Bigint *a, Bigint *b)
1164 #endif
1165 {
1166         double da, db;
1167         int k, ka, kb;
1168
1169         da = b2d(a, &ka);
1170         db = b2d(b, &kb);
1171 #ifdef Pack_32
1172         k = ka - kb + 32*(a->wds - b->wds);
1173 #else
1174         k = ka - kb + 16*(a->wds - b->wds);
1175 #endif
1176 #ifdef IBM
1177         if (k > 0) {
1178                 word0(da) += (k >> 2)*Exp_msk1;
1179                 if (k &= 3)
1180                         da *= 1 << k;
1181         } else {
1182                 k = -k;
1183                 word0(db) += (k >> 2)*Exp_msk1;
1184                 if (k &= 3)
1185                         db *= 1 << k;
1186         }
1187 #else
1188         if (k > 0)
1189                 word0(da) += k*Exp_msk1;
1190         else {
1191                 k = -k;
1192                 word0(db) += k*Exp_msk1;
1193         }
1194 #endif
1195         return da / db;
1196 }
1197
1198  static double
1199 tens[] = {
1200                 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1201                 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1202                 1e20, 1e21, 1e22
1203 #ifdef VAX
1204                 , 1e23, 1e24
1205 #endif
1206                 };
1207
1208  static double
1209 #ifdef IEEE_Arith
1210 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1211 static double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1212 #define n_bigtens 5
1213 #else
1214 #ifdef IBM
1215 bigtens[] = { 1e16, 1e32, 1e64 };
1216 static double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1217 #define n_bigtens 3
1218 #else
1219 bigtens[] = { 1e16, 1e32 };
1220 static double tinytens[] = { 1e-16, 1e-32 };
1221 #define n_bigtens 2
1222 #endif
1223 #endif
1224
1225  double
1226 bsd_strtod
1227 #ifdef KR_headers
1228         (s00, se) CONST char *s00; char **se;
1229 #else
1230         (CONST char *s00, char **se)
1231 #endif
1232 {
1233         int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1234                  e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1235         CONST char *s, *s0, *s1;
1236         double aadj, aadj1, adj, rv, rv0;
1237         Long L;
1238         ULong y, z;
1239         Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1240         char decimal_point = '.';
1241
1242         sign = nz0 = nz = 0;
1243         rv = 0.;
1244         for (s = s00;;s++) switch(*s) {
1245                 case '-':
1246                         sign = 1;
1247                         /* no break */
1248                 case '+':
1249                         if (*++s)
1250                                 goto break2;
1251                         /* no break */
1252                 case 0:
1253                         s = s00;
1254                         goto ret;
1255                 default:
1256                         if (isspace((unsigned char)*s))
1257                                 continue;
1258                         goto break2;
1259         }
1260  break2:
1261         if (*s == '0') {
1262                 nz0 = 1;
1263                 while (*++s == '0') ;
1264                 if (!*s)
1265                         goto ret;
1266         }
1267         s0 = s;
1268         y = z = 0;
1269         for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1270                 if (nd < 9)
1271                         y = 10*y + c - '0';
1272                 else if (nd < 16)
1273                         z = 10*z + c - '0';
1274         nd0 = nd;
1275         if ((char)c == decimal_point) {
1276                 c = *++s;
1277                 if (!nd) {
1278                         for (; c == '0'; c = *++s)
1279                                 nz++;
1280                         if (c > '0' && c <= '9') {
1281                                 s0 = s;
1282                                 nf += nz;
1283                                 nz = 0;
1284                                 goto have_dig;
1285                         }
1286                         goto dig_done;
1287                 }
1288                 for (; c >= '0' && c <= '9'; c = *++s) {
1289  have_dig:
1290                         nz++;
1291                         if (c -= '0') {
1292                                 nf += nz;
1293                                 for (i = 1; i < nz; i++)
1294                                         if (nd++ < 9)
1295                                                 y *= 10;
1296                                         else if (nd <= DBL_DIG + 1)
1297                                                 z *= 10;
1298                                 if (nd++ < 9)
1299                                         y = 10*y + c;
1300                                 else if (nd <= DBL_DIG + 1)
1301                                         z = 10*z + c;
1302                                 nz = 0;
1303                         }
1304                 }
1305         }
1306  dig_done:
1307         e = 0;
1308         if (c == 'e' || c == 'E') {
1309                 if (!nd && !nz && !nz0) {
1310                         s = s00;
1311                         goto ret;
1312                 }
1313                 s00 = s;
1314                 esign = 0;
1315                 switch(c = *++s) {
1316                         case '-':
1317                                 esign = 1;
1318                         case '+':
1319                                 c = *++s;
1320                 }
1321                 if (c >= '0' && c <= '9') {
1322                         while (c == '0')
1323                                 c = *++s;
1324                         if (c > '0' && c <= '9') {
1325                                 L = c - '0';
1326                                 s1 = s;
1327                                 while ((c = *++s) >= '0' && c <= '9')
1328                                         L = 10*L + c - '0';
1329                                 if (s - s1 > 8 || L > 19999)
1330                                         /* Avoid confusion from exponents
1331                                          * so large that e might overflow.
1332                                          */
1333                                         e = 19999; /* safe for 16 bit ints */
1334                                 else
1335                                         e = (int)L;
1336                                 if (esign)
1337                                         e = -e;
1338                         } else
1339                                 e = 0;
1340                 } else
1341                         s = s00;
1342         }
1343         if (!nd) {
1344                 if (!nz && !nz0)
1345                         s = s00;
1346                 goto ret;
1347         }
1348         e1 = e -= nf;
1349
1350         /* Now we have nd0 digits, starting at s0, followed by a
1351          * decimal point, followed by nd-nd0 digits.  The number we're
1352          * after is the integer represented by those digits times
1353          * 10**e */
1354
1355         if (!nd0)
1356                 nd0 = nd;
1357         k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1358         rv = y;
1359         if (k > 9)
1360                 rv = tens[k - 9] * rv + z;
1361         if (nd <= DBL_DIG
1362 #ifndef RND_PRODQUOT
1363                 && FLT_ROUNDS == 1
1364 #endif
1365                         ) {
1366                 if (!e)
1367                         goto ret;
1368                 if (e > 0) {
1369                         if (e <= Ten_pmax) {
1370 #ifdef VAX
1371                                 goto vax_ovfl_check;
1372 #else
1373                                 /* rv = */ rounded_product(rv, tens[e]);
1374                                 goto ret;
1375 #endif
1376                                 }
1377                         i = DBL_DIG - nd;
1378                         if (e <= Ten_pmax + i) {
1379                                 /* A fancier test would sometimes let us do
1380                                  * this for larger i values.
1381                                  */
1382                                 e -= i;
1383                                 rv *= tens[i];
1384 #ifdef VAX
1385                                 /* VAX exponent range is so narrow we must
1386                                  * worry about overflow here...
1387                                  */
1388  vax_ovfl_check:
1389                                 word0(rv) -= P*Exp_msk1;
1390                                 /* rv = */ rounded_product(rv, tens[e]);
1391                                 if ((word0(rv) & Exp_mask)
1392                                  > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1393                                         goto ovfl;
1394                                 word0(rv) += P*Exp_msk1;
1395 #else
1396                                 /* rv = */ rounded_product(rv, tens[e]);
1397 #endif
1398                                 goto ret;
1399                         }
1400                 }
1401 #ifndef Inaccurate_Divide
1402                 else if (e >= -Ten_pmax) {
1403                         /* rv = */ rounded_quotient(rv, tens[-e]);
1404                         goto ret;
1405                 }
1406 #endif
1407         }
1408         e1 += nd - k;
1409
1410         /* Get starting approximation = rv * 10**e1 */
1411
1412         if (e1 > 0) {
1413                 if ( (i = e1 & 15) )
1414                         rv *= tens[i];
1415                 if ( (e1 &= ~15) ) {
1416                         if (e1 > DBL_MAX_10_EXP) {
1417  ovfl:
1418                                 errno = ERANGE;
1419 #ifdef __STDC__
1420                                 rv = HUGE_VAL;
1421 #else
1422                                 /* Can't trust HUGE_VAL */
1423 #ifdef IEEE_Arith
1424                                 word0(rv) = Exp_mask;
1425                                 word1(rv) = 0;
1426 #else
1427                                 word0(rv) = Big0;
1428                                 word1(rv) = Big1;
1429 #endif
1430 #endif
1431                                 goto ret;
1432                         }
1433                         if (e1 >>= 4) {
1434                                 for (j = 0; e1 > 1; j++, e1 >>= 1)
1435                                         if (e1 & 1)
1436                                                 rv *= bigtens[j];
1437                         /* The last multiplication could overflow. */
1438                                 word0(rv) -= P*Exp_msk1;
1439                                 rv *= bigtens[j];
1440                                 if ((z = word0(rv) & Exp_mask)
1441                                  > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1442                                         goto ovfl;
1443                                 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1444                                         /* set to largest number */
1445                                         /* (Can't trust DBL_MAX) */
1446                                         word0(rv) = Big0;
1447                                         word1(rv) = Big1;
1448                                         }
1449                                 else
1450                                         word0(rv) += P*Exp_msk1;
1451                         }
1452                 }
1453         } else if (e1 < 0) {
1454                 e1 = -e1;
1455                 if ( (i = e1 & 15) )
1456                         rv /= tens[i];
1457                 if ( (e1 &= ~15) ) {
1458                         e1 >>= 4;
1459                         for (j = 0; e1 > 1; j++, e1 >>= 1)
1460                                 if (e1 & 1)
1461                                         rv *= tinytens[j];
1462                         /* The last multiplication could underflow. */
1463                         rv0 = rv;
1464                         rv *= tinytens[j];
1465                         if (!rv) {
1466                                 rv = 2.*rv0;
1467                                 rv *= tinytens[j];
1468                                 if (!rv) {
1469  undfl:
1470                                         rv = 0.;
1471                                         errno = ERANGE;
1472                                         goto ret;
1473                                         }
1474                                 word0(rv) = Tiny0;
1475                                 word1(rv) = Tiny1;
1476                                 /* The refinement below will clean
1477                                  * this approximation up.
1478                                  */
1479                         }
1480                 }
1481         }
1482
1483         /* Now the hard part -- adjusting rv to the correct value.*/
1484
1485         /* Put digits into bd: true value = bd * 10^e */
1486
1487         bd0 = s2b(s0, nd0, nd, y);
1488
1489         for (;;) {
1490                 bd = Balloc(bd0->k);
1491                 Bcopy(bd, bd0);
1492                 bb = d2b(rv, &bbe, &bbbits);    /* rv = bb * 2^bbe */
1493                 bs = i2b(1);
1494
1495                 if (e >= 0) {
1496                         bb2 = bb5 = 0;
1497                         bd2 = bd5 = e;
1498                 } else {
1499                         bb2 = bb5 = -e;
1500                         bd2 = bd5 = 0;
1501                 }
1502                 if (bbe >= 0)
1503                         bb2 += bbe;
1504                 else
1505                         bd2 -= bbe;
1506                 bs2 = bb2;
1507 #ifdef Sudden_Underflow
1508 #ifdef IBM
1509                 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1510 #else
1511                 j = P + 1 - bbbits;
1512 #endif
1513 #else
1514                 i = bbe + bbbits - 1;   /* logb(rv) */
1515                 if (i < Emin)   /* denormal */
1516                         j = bbe + (P-Emin);
1517                 else
1518                         j = P + 1 - bbbits;
1519 #endif
1520                 bb2 += j;
1521                 bd2 += j;
1522                 i = bb2 < bd2 ? bb2 : bd2;
1523                 if (i > bs2)
1524                         i = bs2;
1525                 if (i > 0) {
1526                         bb2 -= i;
1527                         bd2 -= i;
1528                         bs2 -= i;
1529                         }
1530                 if (bb5 > 0) {
1531                         bs = pow5mult(bs, bb5);
1532                         bb1 = mult(bs, bb);
1533                         Bfree(bb);
1534                         bb = bb1;
1535                         }
1536                 if (bb2 > 0)
1537                         bb = lshift(bb, bb2);
1538                 if (bd5 > 0)
1539                         bd = pow5mult(bd, bd5);
1540                 if (bd2 > 0)
1541                         bd = lshift(bd, bd2);
1542                 if (bs2 > 0)
1543                         bs = lshift(bs, bs2);
1544                 delta = diff(bb, bd);
1545                 dsign = delta->sign;
1546                 delta->sign = 0;
1547                 i = cmp(delta, bs);
1548                 if (i < 0) {
1549                         /* Error is less than half an ulp -- check for
1550                          * special case of mantissa a power of two.
1551                          */
1552                         if (dsign || word1(rv) || word0(rv) & Bndry_mask)
1553                                 break;
1554                         delta = lshift(delta,Log2P);
1555                         if (cmp(delta, bs) > 0)
1556                                 goto drop_down;
1557                         break;
1558                 }
1559                 if (i == 0) {
1560                         /* exactly half-way between */
1561                         if (dsign) {
1562                                 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
1563                                  &&  word1(rv) == 0xffffffff) {
1564                                         /*boundary case -- increment exponent*/
1565                                         word0(rv) = (word0(rv) & Exp_mask)
1566                                                 + Exp_msk1
1567 #ifdef IBM
1568                                                 | Exp_msk1 >> 4
1569 #endif
1570                                                 ;
1571                                         word1(rv) = 0;
1572                                         break;
1573                                 }
1574                         } else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
1575  drop_down:
1576                                 /* boundary case -- decrement exponent */
1577 #ifdef Sudden_Underflow
1578                                 L = word0(rv) & Exp_mask;
1579 #ifdef IBM
1580                                 if (L <  Exp_msk1)
1581 #else
1582                                 if (L <= Exp_msk1)
1583 #endif
1584                                         goto undfl;
1585                                 L -= Exp_msk1;
1586 #else
1587                                 L = (word0(rv) & Exp_mask) - Exp_msk1;
1588 #endif
1589                                 word0(rv) = L | Bndry_mask1;
1590                                 word1(rv) = 0xffffffff;
1591 #ifdef IBM
1592                                 goto cont;
1593 #else
1594                                 break;
1595 #endif
1596                         }
1597 #ifndef ROUND_BIASED
1598                         if (!(word1(rv) & LSB))
1599                                 break;
1600 #endif
1601                         if (dsign)
1602                                 rv += ulp(rv);
1603 #ifndef ROUND_BIASED
1604                         else {
1605                                 rv -= ulp(rv);
1606 #ifndef Sudden_Underflow
1607                                 if (!rv)
1608                                         goto undfl;
1609 #endif
1610                         }
1611 #endif
1612                         break;
1613                 }
1614                 if ((aadj = ratio(delta, bs)) <= 2.) {
1615                         if (dsign)
1616                                 aadj = aadj1 = 1.;
1617                         else if (word1(rv) || word0(rv) & Bndry_mask) {
1618 #ifndef Sudden_Underflow
1619                                 if (word1(rv) == Tiny1 && !word0(rv))
1620                                         goto undfl;
1621 #endif
1622                                 aadj = 1.;
1623                                 aadj1 = -1.;
1624                         } else {
1625                                 /* special case -- power of FLT_RADIX to be */
1626                                 /* rounded down... */
1627
1628                                 if (aadj < 2./FLT_RADIX)
1629                                         aadj = 1./FLT_RADIX;
1630                                 else
1631                                         aadj *= 0.5;
1632                                 aadj1 = -aadj;
1633                         }
1634                 } else {
1635                         aadj *= 0.5;
1636                         aadj1 = dsign ? aadj : -aadj;
1637 #ifdef Check_FLT_ROUNDS
1638                         switch(FLT_ROUNDS) {
1639                                 case 2: /* towards +infinity */
1640                                         aadj1 -= 0.5;
1641                                         break;
1642                                 case 0: /* towards 0 */
1643                                 case 3: /* towards -infinity */
1644                                         aadj1 += 0.5;
1645                         }
1646 #else
1647                         if (FLT_ROUNDS == 0)
1648                                 aadj1 += 0.5;
1649 #endif
1650                 }
1651                 y = word0(rv) & Exp_mask;
1652
1653                 /* Check for overflow */
1654
1655                 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
1656                         rv0 = rv;
1657                         word0(rv) -= P*Exp_msk1;
1658                         adj = aadj1 * ulp(rv);
1659                         rv += adj;
1660                         if ((word0(rv) & Exp_mask) >=
1661                                         Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
1662                                 if (word0(rv0) == Big0 && word1(rv0) == Big1)
1663                                         goto ovfl;
1664                                 word0(rv) = Big0;
1665                                 word1(rv) = Big1;
1666                                 goto cont;
1667                         } else
1668                                 word0(rv) += P*Exp_msk1;
1669                 } else {
1670 #ifdef Sudden_Underflow
1671                         if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
1672                                 rv0 = rv;
1673                                 word0(rv) += P*Exp_msk1;
1674                                 adj = aadj1 * ulp(rv);
1675                                 rv += adj;
1676 #ifdef IBM
1677                                 if ((word0(rv) & Exp_mask) <  P*Exp_msk1)
1678 #else
1679                                 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
1680 #endif
1681                                 {
1682                                         if (word0(rv0) == Tiny0
1683                                          && word1(rv0) == Tiny1)
1684                                                 goto undfl;
1685                                         word0(rv) = Tiny0;
1686                                         word1(rv) = Tiny1;
1687                                         goto cont;
1688                                 } else
1689                                         word0(rv) -= P*Exp_msk1;
1690                         } else {
1691                                 adj = aadj1 * ulp(rv);
1692                                 rv += adj;
1693                         }
1694 #else
1695                         /* Compute adj so that the IEEE rounding rules will
1696                          * correctly round rv + adj in some half-way cases.
1697                          * If rv * ulp(rv) is denormalized (i.e.,
1698                          * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1699                          * trouble from bits lost to denormalization;
1700                          * example: 1.2e-307 .
1701                          */
1702                         if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
1703                                 aadj1 = (double)(int)(aadj + 0.5);
1704                                 if (!dsign)
1705                                         aadj1 = -aadj1;
1706                         }
1707                         adj = aadj1 * ulp(rv);
1708                         rv += adj;
1709 #endif
1710                 }
1711                 z = word0(rv) & Exp_mask;
1712                 if (y == z) {
1713                         /* Can we stop now? */
1714                         L = aadj;
1715                         aadj -= L;
1716                         /* The tolerances below are conservative. */
1717                         if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
1718                                 if (aadj < .4999999 || aadj > .5000001)
1719                                         break;
1720                         } else if (aadj < .4999999/FLT_RADIX)
1721                                 break;
1722                 }
1723  cont:
1724                 Bfree(bb);
1725                 Bfree(bd);
1726                 Bfree(bs);
1727                 Bfree(delta);
1728         }
1729         Bfree(bb);
1730         Bfree(bd);
1731         Bfree(bs);
1732         Bfree(bd0);
1733         Bfree(delta);
1734  ret:
1735         if (se)
1736                 *se = (char *)s;
1737         return sign ? -rv : rv;
1738 }
1739
1740  static int
1741 quorem
1742 #ifdef KR_headers
1743         (b, S) Bigint *b, *S;
1744 #else
1745         (Bigint *b, Bigint *S)
1746 #endif
1747 {
1748         int n;
1749         Long borrow, y;
1750         ULong carry, q, ys;
1751         ULong *bx, *bxe, *sx, *sxe;
1752 #ifdef Pack_32
1753         Long z;
1754         ULong si, zs;
1755 #endif
1756
1757         n = S->wds;
1758 #ifdef DEBUG
1759         /*debug*/ if (b->wds > n)
1760         /*debug*/       Bug("oversize b in quorem");
1761 #endif
1762         if (b->wds < n)
1763                 return 0;
1764         sx = S->x;
1765         sxe = sx + --n;
1766         bx = b->x;
1767         bxe = bx + n;
1768         q = *bxe / (*sxe + 1);  /* ensure q <= true quotient */
1769 #ifdef DEBUG
1770         /*debug*/ if (q > 9)
1771         /*debug*/       Bug("oversized quotient in quorem");
1772 #endif
1773         if (q) {
1774                 borrow = 0;
1775                 carry = 0;
1776                 do {
1777 #ifdef Pack_32
1778                         si = *sx++;
1779                         ys = (si & 0xffff) * q + carry;
1780                         zs = (si >> 16) * q + (ys >> 16);
1781                         carry = zs >> 16;
1782                         y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1783                         borrow = y >> 16;
1784                         Sign_Extend(borrow, y);
1785                         z = (*bx >> 16) - (zs & 0xffff) + borrow;
1786                         borrow = z >> 16;
1787                         Sign_Extend(borrow, z);
1788                         Storeinc(bx, z, y);
1789 #else
1790                         ys = *sx++ * q + carry;
1791                         carry = ys >> 16;
1792                         y = *bx - (ys & 0xffff) + borrow;
1793                         borrow = y >> 16;
1794                         Sign_Extend(borrow, y);
1795                         *bx++ = y & 0xffff;
1796 #endif
1797                 } while (sx <= sxe);
1798                 if (!*bxe) {
1799                         bx = b->x;
1800                         while (--bxe > bx && !*bxe)
1801                                 --n;
1802                         b->wds = n;
1803                 }
1804         }
1805         if (cmp(b, S) >= 0) {
1806                 q++;
1807                 borrow = 0;
1808                 carry = 0;
1809                 bx = b->x;
1810                 sx = S->x;
1811                 do {
1812 #ifdef Pack_32
1813                         si = *sx++;
1814                         ys = (si & 0xffff) + carry;
1815                         zs = (si >> 16) + (ys >> 16);
1816                         carry = zs >> 16;
1817                         y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1818                         borrow = y >> 16;
1819                         Sign_Extend(borrow, y);
1820                         z = (*bx >> 16) - (zs & 0xffff) + borrow;
1821                         borrow = z >> 16;
1822                         Sign_Extend(borrow, z);
1823                         Storeinc(bx, z, y);
1824 #else
1825                         ys = *sx++ + carry;
1826                         carry = ys >> 16;
1827                         y = *bx - (ys & 0xffff) + borrow;
1828                         borrow = y >> 16;
1829                         Sign_Extend(borrow, y);
1830                         *bx++ = y & 0xffff;
1831 #endif
1832                 } while (sx <= sxe);
1833                 bx = b->x;
1834                 bxe = bx + n;
1835                 if (!*bxe) {
1836                         while (--bxe > bx && !*bxe)
1837                                 --n;
1838                         b->wds = n;
1839                 }
1840         }
1841         return q;
1842 }
1843
1844 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1845  *
1846  * Inspired by "How to Print Floating-Point Numbers Accurately" by
1847  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1848  *
1849  * Modifications:
1850  *      1. Rather than iterating, we use a simple numeric overestimate
1851  *         to determine k = floor(log10(d)).  We scale relevant
1852  *         quantities using O(log2(k)) rather than O(k) multiplications.
1853  *      2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1854  *         try to generate digits strictly left to right.  Instead, we
1855  *         compute with fewer bits and propagate the carry if necessary
1856  *         when rounding the final digit up.  This is often faster.
1857  *      3. Under the assumption that input will be rounded nearest,
1858  *         mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1859  *         That is, we allow equality in stopping tests when the
1860  *         round-nearest rule will give the same floating-point value
1861  *         as would satisfaction of the stopping test with strict
1862  *         inequality.
1863  *      4. We remove common factors of powers of 2 from relevant
1864  *         quantities.
1865  *      5. When converting floating-point integers less than 1e16,
1866  *         we use floating-point arithmetic rather than resorting
1867  *         to multiple-precision integers.
1868  *      6. When asked to produce fewer than 15 digits, we first try
1869  *         to get by with floating-point arithmetic; we resort to
1870  *         multiple-precision integer arithmetic only if we cannot
1871  *         guarantee that the floating-point calculation has given
1872  *         the correctly rounded result.  For k requested digits and
1873  *         "uniformly" distributed input, the probability is
1874  *         something like 10^(k-15) that we must resort to the long
1875  *         calculation.
1876  */
1877
1878 char *
1879 __bsd_dtoa
1880 #ifdef KR_headers
1881         (d, mode, ndigits, decpt, sign, rve, resultp)
1882         double d; int mode, ndigits, *decpt, *sign; char **rve, **resultp;
1883 #else
1884         (double d, int mode, int ndigits, int *decpt, int *sign, char **rve,
1885          char **resultp)
1886 #endif
1887 {
1888  /*     Arguments ndigits, decpt, sign are similar to those
1889         of ecvt and fcvt; trailing zeros are suppressed from
1890         the returned string.  If not null, *rve is set to point
1891         to the end of the return value.  If d is +-Infinity or NaN,
1892         then *decpt is set to 9999.
1893
1894         mode:
1895                 0 ==> shortest string that yields d when read in
1896                         and rounded to nearest.
1897                 1 ==> like 0, but with Steele & White stopping rule;
1898                         e.g. with IEEE P754 arithmetic , mode 0 gives
1899                         1e23 whereas mode 1 gives 9.999999999999999e22.
1900                 2 ==> max(1,ndigits) significant digits.  This gives a
1901                         return value similar to that of ecvt, except
1902                         that trailing zeros are suppressed.
1903                 3 ==> through ndigits past the decimal point.  This
1904                         gives a return value similar to that from fcvt,
1905                         except that trailing zeros are suppressed, and
1906                         ndigits can be negative.
1907                 4-9 should give the same return values as 2-3, i.e.,
1908                         4 <= mode <= 9 ==> same return as mode
1909                         2 + (mode & 1).  These modes are mainly for
1910                         debugging; often they run slower but sometimes
1911                         faster than modes 2-3.
1912                 4,5,8,9 ==> left-to-right digit generation.
1913                 6-9 ==> don't try fast floating-point estimate
1914                         (if applicable).
1915
1916                 Values of mode other than 0-9 are treated as mode 0.
1917
1918                 Sufficient space is allocated to the return value
1919                 to hold the suppressed trailing zeros.
1920         */
1921
1922         int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
1923                 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
1924                 spec_case, try_quick;
1925         Long L;
1926 #ifndef Sudden_Underflow
1927         int denorm;
1928         ULong x;
1929 #endif
1930         Bigint *b, *b1, *delta, *mlo, *mhi, *S;
1931         double d2, ds, eps;
1932         char *s, *s0;
1933
1934         if (word0(d) & Sign_bit) {
1935                 /* set sign for everything, including 0's and NaNs */
1936                 *sign = 1;
1937                 word0(d) &= ~Sign_bit;  /* clear sign bit */
1938         }
1939         else
1940                 *sign = 0;
1941
1942 #if defined(IEEE_Arith) + defined(VAX)
1943 #ifdef IEEE_Arith
1944         if ((word0(d) & Exp_mask) == Exp_mask)
1945 #else
1946         if (word0(d)  == 0x8000)
1947 #endif
1948         {
1949                 /* Infinity or NaN */
1950                 const char *ss;
1951                 *decpt = 9999;
1952                 ss =
1953 #ifdef IEEE_Arith
1954                         !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" :
1955 #endif
1956                                 "NaN";
1957                 *resultp = s = malloc (strlen (ss) + 1);
1958                 strcpy (s, ss);
1959                 if (rve)
1960                         *rve =
1961 #ifdef IEEE_Arith
1962                                 s[3] ? s + 8 :
1963 #endif
1964                                                 s + 3;
1965                 return s;
1966         }
1967 #endif
1968 #ifdef IBM
1969         d += 0; /* normalize */
1970 #endif
1971         if (!d) {
1972                 *decpt = 1;
1973                 *resultp = s = malloc (2);
1974                 s [0] = '0';
1975                 s [1] = 0;
1976                 if (rve)
1977                         *rve = s + 1;
1978                 return s;
1979         }
1980
1981         b = d2b(d, &be, &bbits);
1982 #ifdef Sudden_Underflow
1983         i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
1984 #else
1985         if ( (i = (int)((word0(d) >> Exp_shift1) & (Exp_mask>>Exp_shift1))) ) {
1986 #endif
1987                 d2 = d;
1988                 word0(d2) &= Frac_mask1;
1989                 word0(d2) |= Exp_11;
1990 #ifdef IBM
1991                 if ( (j = 11 - hi0bits(word0(d2) & Frac_mask)) )
1992                         d2 /= 1 << j;
1993 #endif
1994
1995                 /* log(x)       ~=~ log(1.5) + (x-1.5)/1.5
1996                  * log10(x)      =  log(x) / log(10)
1997                  *              ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1998                  * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1999                  *
2000                  * This suggests computing an approximation k to log10(d) by
2001                  *
2002                  * k = (i - Bias)*0.301029995663981
2003                  *      + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2004                  *
2005                  * We want k to be too large rather than too small.
2006                  * The error in the first-order Taylor series approximation
2007                  * is in our favor, so we just round up the constant enough
2008                  * to compensate for any error in the multiplication of
2009                  * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2010                  * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2011                  * adding 1e-13 to the constant term more than suffices.
2012                  * Hence we adjust the constant term to 0.1760912590558.
2013                  * (We could get a more accurate k by invoking log10,
2014                  *  but this is probably not worthwhile.)
2015                  */
2016
2017                 i -= Bias;
2018 #ifdef IBM
2019                 i <<= 2;
2020                 i += j;
2021 #endif
2022 #ifndef Sudden_Underflow
2023                 denorm = 0;
2024         } else {
2025                 /* d is denormalized */
2026
2027                 i = bbits + be + (Bias + (P-1) - 1);
2028                 x = i > 32  ? ((word0(d) << (64 - i)) | (word1(d) >> (i - 32)))
2029                             : (word1(d) << (32 - i));
2030                 d2 = x;
2031                 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
2032                 i -= (Bias + (P-1) - 1) + 1;
2033                 denorm = 1;
2034         }
2035 #endif
2036         ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
2037         k = (int)ds;
2038         if (ds < 0. && ds != k)
2039                 k--;    /* want k = floor(ds) */
2040         k_check = 1;
2041         if (k >= 0 && k <= Ten_pmax) {
2042                 if (d < tens[k])
2043                         k--;
2044                 k_check = 0;
2045         }
2046         j = bbits - i - 1;
2047         if (j >= 0) {
2048                 b2 = 0;
2049                 s2 = j;
2050         } else {
2051                 b2 = -j;
2052                 s2 = 0;
2053         }
2054         if (k >= 0) {
2055                 b5 = 0;
2056                 s5 = k;
2057                 s2 += k;
2058         } else {
2059                 b2 -= k;
2060                 b5 = -k;
2061                 s5 = 0;
2062         }
2063         if (mode < 0 || mode > 9)
2064                 mode = 0;
2065         try_quick = 1;
2066         if (mode > 5) {
2067                 mode -= 4;
2068                 try_quick = 0;
2069         }
2070         leftright = 1;
2071         switch(mode) {
2072                 case 0:
2073                 case 1:
2074                         ilim = ilim1 = -1;
2075                         i = 18;
2076                         ndigits = 0;
2077                         break;
2078                 case 2:
2079                         leftright = 0;
2080                         /* no break */
2081                 case 4:
2082                         if (ndigits <= 0)
2083                                 ndigits = 1;
2084                         ilim = ilim1 = i = ndigits;
2085                         break;
2086                 case 3:
2087                         leftright = 0;
2088                         /* no break */
2089                 case 5:
2090                         i = ndigits + k + 1;
2091                         ilim = i;
2092                         ilim1 = i - 1;
2093                         if (i <= 0)
2094                                 i = 1;
2095         }
2096         *resultp = (char *) malloc(i + 1);
2097         s = s0 = *resultp;
2098
2099         if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2100
2101                 /* Try to get by with floating-point arithmetic. */
2102
2103                 i = 0;
2104                 d2 = d;
2105                 k0 = k;
2106                 ilim0 = ilim;
2107                 ieps = 2; /* conservative */
2108                 if (k > 0) {
2109                         ds = tens[k&0xf];
2110                         j = k >> 4;
2111                         if (j & Bletch) {
2112                                 /* prevent overflows */
2113                                 j &= Bletch - 1;
2114                                 d /= bigtens[n_bigtens-1];
2115                                 ieps++;
2116                         }
2117                         for (; j; j >>= 1, i++)
2118                                 if (j & 1) {
2119                                         ieps++;
2120                                         ds *= bigtens[i];
2121                                 }
2122                         d /= ds;
2123                 } else if ( (j1 = -k) ) {
2124                         d *= tens[j1 & 0xf];
2125                         for (j = j1 >> 4; j; j >>= 1, i++)
2126                                 if (j & 1) {
2127                                         ieps++;
2128                                         d *= bigtens[i];
2129                                 }
2130                 }
2131                 if (k_check && d < 1. && ilim > 0) {
2132                         if (ilim1 <= 0)
2133                                 goto fast_failed;
2134                         ilim = ilim1;
2135                         k--;
2136                         d *= 10.;
2137                         ieps++;
2138                 }
2139                 eps = ieps*d + 7.;
2140                 word0(eps) -= (P-1)*Exp_msk1;
2141                 if (ilim == 0) {
2142                         S = mhi = 0;
2143                         d -= 5.;
2144                         if (d > eps)
2145                                 goto one_digit;
2146                         if (d < -eps)
2147                                 goto no_digits;
2148                         goto fast_failed;
2149                 }
2150 #ifndef No_leftright
2151                 if (leftright) {
2152                         /* Use Steele & White method of only
2153                          * generating digits needed.
2154                          */
2155                         eps = 0.5/tens[ilim-1] - eps;
2156                         for (i = 0;;) {
2157                                 L = d;
2158                                 d -= L;
2159                                 *s++ = '0' + (int)L;
2160                                 if (d < eps)
2161                                         goto ret1;
2162                                 if (1. - d < eps)
2163                                         goto bump_up;
2164                                 if (++i >= ilim)
2165                                         break;
2166                                 eps *= 10.;
2167                                 d *= 10.;
2168                         }
2169                 } else {
2170 #endif
2171                         /* Generate ilim digits, then fix them up. */
2172                         eps *= tens[ilim-1];
2173                         for (i = 1;; i++, d *= 10.) {
2174                                 L = d;
2175                                 d -= L;
2176                                 *s++ = '0' + (int)L;
2177                                 if (i == ilim) {
2178                                         if (d > 0.5 + eps)
2179                                                 goto bump_up;
2180                                         else if (d < 0.5 - eps) {
2181                                                 while (*--s == '0');
2182                                                 s++;
2183                                                 goto ret1;
2184                                         }
2185                                         break;
2186                                 }
2187                         }
2188 #ifndef No_leftright
2189                 }
2190 #endif
2191  fast_failed:
2192                 s = s0;
2193                 d = d2;
2194                 k = k0;
2195                 ilim = ilim0;
2196         }
2197
2198         /* Do we have a "small" integer? */
2199
2200         if (be >= 0 && k <= Int_max) {
2201                 /* Yes. */
2202                 ds = tens[k];
2203                 if (ndigits < 0 && ilim <= 0) {
2204                         S = mhi = 0;
2205                         if (ilim < 0 || d <= 5*ds)
2206                                 goto no_digits;
2207                         goto one_digit;
2208                 }
2209                 for (i = 1;; i++) {
2210                         L = d / ds;
2211                         d -= L*ds;
2212 #ifdef Check_FLT_ROUNDS
2213                         /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2214                         if (d < 0) {
2215                                 L--;
2216                                 d += ds;
2217                         }
2218 #endif
2219                         *s++ = '0' + (int)L;
2220                         if (i == ilim) {
2221                                 d += d;
2222                                 if (d > ds || (d == ds && L & 1)) {
2223  bump_up:
2224                                         while (*--s == '9')
2225                                                 if (s == s0) {
2226                                                         k++;
2227                                                         *s = '0';
2228                                                         break;
2229                                                 }
2230                                         ++*s++;
2231                                 }
2232                                 break;
2233                         }
2234                         if (!(d *= 10.))
2235                                 break;
2236                 }
2237                 goto ret1;
2238         }
2239
2240         m2 = b2;
2241         m5 = b5;
2242         mhi = mlo = 0;
2243         if (leftright) {
2244                 if (mode < 2) {
2245                         i =
2246 #ifndef Sudden_Underflow
2247                                 denorm ? be + (Bias + (P-1) - 1 + 1) :
2248 #endif
2249 #ifdef IBM
2250                                 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
2251 #else
2252                                 1 + P - bbits;
2253 #endif
2254                 } else {
2255                         j = ilim - 1;
2256                         if (m5 >= j)
2257                                 m5 -= j;
2258                         else {
2259                                 s5 += j -= m5;
2260                                 b5 += j;
2261                                 m5 = 0;
2262                         }
2263                         if ((i = ilim) < 0) {
2264                                 m2 -= i;
2265                                 i = 0;
2266                         }
2267                 }
2268                 b2 += i;
2269                 s2 += i;
2270                 mhi = i2b(1);
2271         }
2272         if (m2 > 0 && s2 > 0) {
2273                 i = m2 < s2 ? m2 : s2;
2274                 b2 -= i;
2275                 m2 -= i;
2276                 s2 -= i;
2277         }
2278         if (b5 > 0) {
2279                 if (leftright) {
2280                         if (m5 > 0) {
2281                                 mhi = pow5mult(mhi, m5);
2282                                 b1 = mult(mhi, b);
2283                                 Bfree(b);
2284                                 b = b1;
2285                                 }
2286                         if ( (j = b5 - m5) )
2287                                 b = pow5mult(b, j);
2288                 } else
2289                         b = pow5mult(b, b5);
2290         }
2291         S = i2b(1);
2292         if (s5 > 0)
2293                 S = pow5mult(S, s5);
2294
2295         /* Check for special case that d is a normalized power of 2. */
2296
2297         if (mode < 2) {
2298                 if (!word1(d) && !(word0(d) & Bndry_mask)
2299 #ifndef Sudden_Underflow
2300                  && word0(d) & Exp_mask
2301 #endif
2302                                 ) {
2303                         /* The special case */
2304                         b2 += Log2P;
2305                         s2 += Log2P;
2306                         spec_case = 1;
2307                 } else
2308                         spec_case = 0;
2309         }
2310
2311         /* Arrange for convenient computation of quotients:
2312          * shift left if necessary so divisor has 4 leading 0 bits.
2313          *
2314          * Perhaps we should just compute leading 28 bits of S once
2315          * and for all and pass them and a shift to quorem, so it
2316          * can do shifts and ors to compute the numerator for q.
2317          */
2318 #ifdef Pack_32
2319         if ( (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) )
2320                 i = 32 - i;
2321 #else
2322         if ( (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) )
2323                 i = 16 - i;
2324 #endif
2325         if (i > 4) {
2326                 i -= 4;
2327                 b2 += i;
2328                 m2 += i;
2329                 s2 += i;
2330         } else if (i < 4) {
2331                 i += 28;
2332                 b2 += i;
2333                 m2 += i;
2334                 s2 += i;
2335         }
2336         if (b2 > 0)
2337                 b = lshift(b, b2);
2338         if (s2 > 0)
2339                 S = lshift(S, s2);
2340         if (k_check) {
2341                 if (cmp(b,S) < 0) {
2342                         k--;
2343                         b = multadd(b, 10, 0);  /* we botched the k estimate */
2344                         if (leftright)
2345                                 mhi = multadd(mhi, 10, 0);
2346                         ilim = ilim1;
2347                 }
2348         }
2349         if (ilim <= 0 && mode > 2) {
2350                 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
2351                         /* no digits, fcvt style */
2352  no_digits:
2353                         k = -1 - ndigits;
2354                         goto ret;
2355                 }
2356  one_digit:
2357                 *s++ = '1';
2358                 k++;
2359                 goto ret;
2360         }
2361         if (leftright) {
2362                 if (m2 > 0)
2363                         mhi = lshift(mhi, m2);
2364
2365                 /* Compute mlo -- check for special case
2366                  * that d is a normalized power of 2.
2367                  */
2368
2369                 mlo = mhi;
2370                 if (spec_case) {
2371                         mhi = Balloc(mhi->k);
2372                         Bcopy(mhi, mlo);
2373                         mhi = lshift(mhi, Log2P);
2374                 }
2375
2376                 for (i = 1;;i++) {
2377                         dig = quorem(b,S) + '0';
2378                         /* Do we yet have the shortest decimal string
2379                          * that will round to d?
2380                          */
2381                         j = cmp(b, mlo);
2382                         delta = diff(S, mhi);
2383                         j1 = delta->sign ? 1 : cmp(b, delta);
2384                         Bfree(delta);
2385 #ifndef ROUND_BIASED
2386                         if (j1 == 0 && !mode && !(word1(d) & 1)) {
2387                                 if (dig == '9')
2388                                         goto round_9_up;
2389                                 if (j > 0)
2390                                         dig++;
2391                                 *s++ = dig;
2392                                 goto ret;
2393                         }
2394 #endif
2395                         if (j < 0 || (j == 0 && !mode
2396 #ifndef ROUND_BIASED
2397                                                         && !(word1(d) & 1)
2398 #endif
2399                                         )) {
2400                                 if (j1 > 0) {
2401                                         b = lshift(b, 1);
2402                                         j1 = cmp(b, S);
2403                                         if ((j1 > 0 || (j1 == 0 && dig & 1))
2404                                         && dig++ == '9')
2405                                                 goto round_9_up;
2406                                 }
2407                                 *s++ = dig;
2408                                 goto ret;
2409                         }
2410                         if (j1 > 0) {
2411                                 if (dig == '9') { /* possible if i == 1 */
2412  round_9_up:
2413                                         *s++ = '9';
2414                                         goto roundoff;
2415                                 }
2416                                 *s++ = dig + 1;
2417                                 goto ret;
2418                         }
2419                         *s++ = dig;
2420                         if (i == ilim)
2421                                 break;
2422                         b = multadd(b, 10, 0);
2423                         if (mlo == mhi)
2424                                 mlo = mhi = multadd(mhi, 10, 0);
2425                         else {
2426                                 mlo = multadd(mlo, 10, 0);
2427                                 mhi = multadd(mhi, 10, 0);
2428                         }
2429                 }
2430         } else
2431                 for (i = 1;; i++) {
2432                         *s++ = dig = quorem(b,S) + '0';
2433                         if (i >= ilim)
2434                                 break;
2435                         b = multadd(b, 10, 0);
2436                 }
2437
2438         /* Round off last digit */
2439
2440         b = lshift(b, 1);
2441         j = cmp(b, S);
2442         if (j > 0 || (j == 0 && dig & 1)) {
2443  roundoff:
2444                 while (*--s == '9')
2445                         if (s == s0) {
2446                                 k++;
2447                                 *s++ = '1';
2448                                 goto ret;
2449                         }
2450                 ++*s++;
2451         } else {
2452                 while (*--s == '0');
2453                 s++;
2454         }
2455  ret:
2456         Bfree(S);
2457         if (mhi) {
2458                 if (mlo && mlo != mhi)
2459                         Bfree(mlo);
2460                 Bfree(mhi);
2461         }
2462  ret1:
2463         Bfree(b);
2464         if (s == s0) {  /* don't return empty string */
2465                 *s++ = '0';
2466                 k = 0;
2467         }
2468         *s = 0;
2469         *decpt = k + 1;
2470         if (rve)
2471                 *rve = s;
2472         return s0;
2473         }
2474 #ifdef __cplusplus
2475 }
2476 #endif