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