* support-test-*.cs: Rename from test-*-p2.cs.
[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) do { (((unsigned short *)a)[1] = (unsigned short)b, \
272 ((unsigned short *)a)[0] = (unsigned short)c); a ++; } while (0)
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
662 #ifdef Pack_32
663         for (; xb < xbe; xb++, xc0++) {
664                 if ( (y = *xb & 0xffff) ) {
665                         x = xa;
666                         xc = xc0;
667                         carry = 0;
668                         do {
669                                 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
670                                 carry = z >> 16;
671                                 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
672                                 carry = z2 >> 16;
673                                 Storeinc(xc, z2, z);
674                         } while (x < xae);
675                         *xc = carry;
676                 }
677                 if ( (y = *xb >> 16) ) {
678                         x = xa;
679                         xc = xc0;
680                         carry = 0;
681                         z2 = *xc;
682                         do {
683                                 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
684                                 carry = z >> 16;
685                                 Storeinc(xc, z, z2);
686                                 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
687                                 carry = z2 >> 16;
688                         } while (x < xae);
689                         *xc = z2;
690                 }
691         }
692 #else
693         for (; xb < xbe; xc0++) {
694                 if (y = *xb++) {
695                         x = xa;
696                         xc = xc0;
697                         carry = 0;
698                         do {
699                                 z = *x++ * y + *xc + carry;
700                                 carry = z >> 16;
701                                 *xc++ = z & 0xffff;
702                         } while (x < xae);
703                         *xc = carry;
704                 }
705         }
706 #endif
707         for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
708         c->wds = wc;
709         return c;
710 }
711
712  static Bigint *p5s;
713
714  static Bigint *
715 pow5mult
716 #ifdef KR_headers
717         (b, k) Bigint *b; int k;
718 #else
719         (Bigint *b, int k)
720 #endif
721 {
722         Bigint *b1, *p5, *p51;
723         int i;
724         static int p05[3] = { 5, 25, 125 };
725
726         if ( (i = k & 3) )
727                 b = multadd(b, p05[i-1], 0);
728
729         if (!(k >>= 2))
730                 return b;
731         if (!(p5 = p5s)) {
732                 /* first time */
733                 p5 = p5s = i2b(625);
734                 p5->next = 0;
735         }
736         for (;;) {
737                 if (k & 1) {
738                         b1 = mult(b, p5);
739                         Bfree(b);
740                         b = b1;
741                 }
742                 if (!(k >>= 1))
743                         break;
744                 if (!(p51 = p5->next)) {
745                         p51 = p5->next = mult(p5,p5);
746                         p51->next = 0;
747                 }
748                 p5 = p51;
749         }
750         return b;
751 }
752
753  static Bigint *
754 lshift
755 #ifdef KR_headers
756         (b, k) Bigint *b; int k;
757 #else
758         (Bigint *b, int k)
759 #endif
760 {
761         int i, k1, n, n1;
762         Bigint *b1;
763         ULong *x, *x1, *xe, z;
764
765 #ifdef Pack_32
766         n = k >> 5;
767 #else
768         n = k >> 4;
769 #endif
770         k1 = b->k;
771         n1 = n + b->wds + 1;
772         for (i = b->maxwds; n1 > i; i <<= 1)
773                 k1++;
774         b1 = Balloc(k1);
775         x1 = b1->x;
776         for (i = 0; i < n; i++)
777                 *x1++ = 0;
778         x = b->x;
779         xe = x + b->wds;
780 #ifdef Pack_32
781         if (k &= 0x1f) {
782                 k1 = 32 - k;
783                 z = 0;
784                 do {
785                         *x1++ = *x << k | z;
786                         z = *x++ >> k1;
787                 } while (x < xe);
788                 if ( (*x1 = z) )
789                         ++n1;
790         }
791 #else
792         if (k &= 0xf) {
793                 k1 = 16 - k;
794                 z = 0;
795                 do {
796                         *x1++ = *x << k  & 0xffff | z;
797                         z = *x++ >> k1;
798                 } while (x < xe);
799                 if (*x1 = z)
800                         ++n1;
801         }
802 #endif
803         else
804                 do
805                         *x1++ = *x++;
806                 while (x < xe);
807         b1->wds = n1 - 1;
808         Bfree(b);
809         return b1;
810 }
811
812  static int
813 cmp
814 #ifdef KR_headers
815         (a, b) Bigint *a, *b;
816 #else
817         (Bigint *a, Bigint *b)
818 #endif
819 {
820         ULong *xa, *xa0, *xb, *xb0;
821         int i, j;
822
823         i = a->wds;
824         j = b->wds;
825 #ifdef DEBUG
826         if (i > 1 && !a->x[i-1])
827                 Bug("cmp called with a->x[a->wds-1] == 0");
828         if (j > 1 && !b->x[j-1])
829                 Bug("cmp called with b->x[b->wds-1] == 0");
830 #endif
831         if (i -= j)
832                 return i;
833         xa0 = a->x;
834         xa = xa0 + j;
835         xb0 = b->x;
836         xb = xb0 + j;
837         for (;;) {
838                 if (*--xa != *--xb)
839                         return *xa < *xb ? -1 : 1;
840                 if (xa <= xa0)
841                         break;
842         }
843         return 0;
844 }
845
846  static Bigint *
847 diff
848 #ifdef KR_headers
849         (a, b) Bigint *a, *b;
850 #else
851         (Bigint *a, Bigint *b)
852 #endif
853 {
854         Bigint *c;
855         int i, wa, wb;
856         Long borrow, y; /* We need signed shifts here. */
857         ULong *xa, *xae, *xb, *xbe, *xc;
858 #ifdef Pack_32
859         Long z;
860 #endif
861
862         i = cmp(a,b);
863         if (!i) {
864                 c = Balloc(0);
865                 c->wds = 1;
866                 c->x[0] = 0;
867                 return c;
868         }
869         if (i < 0) {
870                 c = a;
871                 a = b;
872                 b = c;
873                 i = 1;
874         } else
875                 i = 0;
876         c = Balloc(a->k);
877         c->sign = i;
878         wa = a->wds;
879         xa = a->x;
880         xae = xa + wa;
881         wb = b->wds;
882         xb = b->x;
883         xbe = xb + wb;
884         xc = c->x;
885         borrow = 0;
886 #ifdef Pack_32
887         do {
888                 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
889                 borrow = y >> 16;
890                 Sign_Extend(borrow, y);
891                 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
892                 borrow = z >> 16;
893                 Sign_Extend(borrow, z);
894                 Storeinc(xc, z, y);
895         } while (xb < xbe);
896         while (xa < xae) {
897                 y = (*xa & 0xffff) + borrow;
898                 borrow = y >> 16;
899                 Sign_Extend(borrow, y);
900                 z = (*xa++ >> 16) + borrow;
901                 borrow = z >> 16;
902                 Sign_Extend(borrow, z);
903                 Storeinc(xc, z, y);
904         }
905 #else
906         do {
907                 y = *xa++ - *xb++ + borrow;
908                 borrow = y >> 16;
909                 Sign_Extend(borrow, y);
910                 *xc++ = y & 0xffff;
911         } while (xb < xbe);
912         while (xa < xae) {
913                 y = *xa++ + borrow;
914                 borrow = y >> 16;
915                 Sign_Extend(borrow, y);
916                 *xc++ = y & 0xffff;
917         }
918 #endif
919         while (!*--xc)
920                 wa--;
921         c->wds = wa;
922         return c;
923 }
924
925  static double
926 ulp
927 #ifdef KR_headers
928         (x) double x;
929 #else
930         (double x)
931 #endif
932 {
933         register Long L;
934         double a;
935
936         L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
937 #ifndef Sudden_Underflow
938         if (L > 0) {
939 #endif
940 #ifdef IBM
941                 L |= Exp_msk1 >> 4;
942 #endif
943                 word0(a) = L;
944                 word1(a) = 0;
945 #ifndef Sudden_Underflow
946         } else {
947                 L = -L >> Exp_shift;
948                 if (L < Exp_shift) {
949                         word0(a) = 0x80000 >> L;
950                         word1(a) = 0;
951                 } else {
952                         word0(a) = 0;
953                         L -= Exp_shift;
954                         word1(a) = L >= 31 ? 1 : 1 << (31 - L);
955                 }
956         }
957 #endif
958         return a;
959 }
960
961  static double
962 b2d
963 #ifdef KR_headers
964         (a, e) Bigint *a; int *e;
965 #else
966         (Bigint *a, int *e)
967 #endif
968 {
969         ULong *xa, *xa0, w, y, z;
970         int k;
971         double d;
972 #ifdef VAX
973         ULong d0, d1;
974 #else
975 #define d0 word0(d)
976 #define d1 word1(d)
977 #endif
978
979         xa0 = a->x;
980         xa = xa0 + a->wds;
981         y = *--xa;
982 #ifdef DEBUG
983         if (!y) Bug("zero y in b2d");
984 #endif
985         k = hi0bits(y);
986         *e = 32 - k;
987 #ifdef Pack_32
988         if (k < Ebits) {
989                 d0 = Exp_1 | (y >> (Ebits - k));
990                 w = xa > xa0 ? *--xa : 0;
991                 d1 = (y << ((32-Ebits) + k)) | (w >> (Ebits - k));
992                 goto ret_d;
993                 }
994         z = xa > xa0 ? *--xa : 0;
995         if (k -= Ebits) {
996                 d0 = Exp_1 | (y << k) | (z >> (32 - k));
997                 y = xa > xa0 ? *--xa : 0;
998                 d1 = (z << k) | (y >> (32 - k));
999         } else {
1000                 d0 = Exp_1 | y;
1001                 d1 = z;
1002         }
1003 #else
1004         if (k < Ebits + 16) {
1005                 z = xa > xa0 ? *--xa : 0;
1006                 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1007                 w = xa > xa0 ? *--xa : 0;
1008                 y = xa > xa0 ? *--xa : 0;
1009                 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1010                 goto ret_d;
1011         }
1012         z = xa > xa0 ? *--xa : 0;
1013         w = xa > xa0 ? *--xa : 0;
1014         k -= Ebits + 16;
1015         d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1016         y = xa > xa0 ? *--xa : 0;
1017         d1 = w << k + 16 | y << k;
1018 #endif
1019  ret_d:
1020 #ifdef VAX
1021         word0(d) = d0 >> 16 | d0 << 16;
1022         word1(d) = d1 >> 16 | d1 << 16;
1023 #else
1024 #undef d0
1025 #undef d1
1026 #endif
1027         return d;
1028 }
1029
1030  static Bigint *
1031 d2b
1032 #ifdef KR_headers
1033         (d, e, bits) double d; int *e, *bits;
1034 #else
1035         (double d, int *e, int *bits)
1036 #endif
1037 {
1038         Bigint *b;
1039         int de, i, k;
1040         ULong *x, y, z;
1041 #ifdef VAX
1042         ULong d0, d1;
1043         d0 = word0(d) >> 16 | word0(d) << 16;
1044         d1 = word1(d) >> 16 | word1(d) << 16;
1045 #else
1046 #define d0 word0(d)
1047 #define d1 word1(d)
1048 #endif
1049
1050 #ifdef Pack_32
1051         b = Balloc(1);
1052 #else
1053         b = Balloc(2);
1054 #endif
1055         x = b->x;
1056
1057         z = d0 & Frac_mask;
1058         d0 &= 0x7fffffff;       /* clear sign bit, which we ignore */
1059 #ifdef Sudden_Underflow
1060         de = (int)(d0 >> Exp_shift);
1061 #ifndef IBM
1062         z |= Exp_msk11;
1063 #endif
1064 #else
1065         if ( (de = (int)(d0 >> Exp_shift)) )
1066                 z |= Exp_msk1;
1067 #endif
1068 #ifdef Pack_32
1069         if ( (y = d1) ) {
1070                 if ( (k = lo0bits(&y)) ) {
1071                         x[0] = y | (z << (32 - k));
1072                         z >>= k;
1073                         }
1074                 else
1075                         x[0] = y;
1076                 i = b->wds = (x[1] = z) ? 2 : 1;
1077         } else {
1078 #ifdef DEBUG
1079                 if (!z)
1080                         Bug("Zero passed to d2b");
1081 #endif
1082                 k = lo0bits(&z);
1083                 x[0] = z;
1084                 i = b->wds = 1;
1085                 k += 32;
1086         }
1087 #else
1088         if (y = d1) {
1089                 if (k = lo0bits(&y))
1090                         if (k >= 16) {
1091                                 x[0] = y | z << 32 - k & 0xffff;
1092                                 x[1] = z >> k - 16 & 0xffff;
1093                                 x[2] = z >> k;
1094                                 i = 2;
1095                         } else {
1096                                 x[0] = y & 0xffff;
1097                                 x[1] = y >> 16 | z << 16 - k & 0xffff;
1098                                 x[2] = z >> k & 0xffff;
1099                                 x[3] = z >> k+16;
1100                                 i = 3;
1101                         }
1102                 else {
1103                         x[0] = y & 0xffff;
1104                         x[1] = y >> 16;
1105                         x[2] = z & 0xffff;
1106                         x[3] = z >> 16;
1107                         i = 3;
1108                 }
1109         } else {
1110 #ifdef DEBUG
1111                 if (!z)
1112                         Bug("Zero passed to d2b");
1113 #endif
1114                 k = lo0bits(&z);
1115                 if (k >= 16) {
1116                         x[0] = z;
1117                         i = 0;
1118                 } else {
1119                         x[0] = z & 0xffff;
1120                         x[1] = z >> 16;
1121                         i = 1;
1122                 }
1123                 k += 32;
1124         }
1125         while (!x[i])
1126                 --i;
1127         b->wds = i + 1;
1128 #endif
1129 #ifndef Sudden_Underflow
1130         if (de) {
1131 #endif
1132 #ifdef IBM
1133                 *e = (de - Bias - (P-1) << 2) + k;
1134                 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1135 #else
1136                 *e = de - Bias - (P-1) + k;
1137                 *bits = P - k;
1138 #endif
1139 #ifndef Sudden_Underflow
1140         } else {
1141                 *e = de - Bias - (P-1) + 1 + k;
1142 #ifdef Pack_32
1143                 *bits = 32*i - hi0bits(x[i-1]);
1144 #else
1145                 *bits = (i+2)*16 - hi0bits(x[i]);
1146 #endif
1147         }
1148 #endif
1149         return b;
1150 }
1151 #undef d0
1152 #undef d1
1153
1154  static double
1155 ratio
1156 #ifdef KR_headers
1157         (a, b) Bigint *a, *b;
1158 #else
1159         (Bigint *a, Bigint *b)
1160 #endif
1161 {
1162         double da, db;
1163         int k, ka, kb;
1164
1165         da = b2d(a, &ka);
1166         db = b2d(b, &kb);
1167 #ifdef Pack_32
1168         k = ka - kb + 32*(a->wds - b->wds);
1169 #else
1170         k = ka - kb + 16*(a->wds - b->wds);
1171 #endif
1172 #ifdef IBM
1173         if (k > 0) {
1174                 word0(da) += (k >> 2)*Exp_msk1;
1175                 if (k &= 3)
1176                         da *= 1 << k;
1177         } else {
1178                 k = -k;
1179                 word0(db) += (k >> 2)*Exp_msk1;
1180                 if (k &= 3)
1181                         db *= 1 << k;
1182         }
1183 #else
1184         if (k > 0)
1185                 word0(da) += k*Exp_msk1;
1186         else {
1187                 k = -k;
1188                 word0(db) += k*Exp_msk1;
1189         }
1190 #endif
1191         return da / db;
1192 }
1193
1194  static double
1195 tens[] = {
1196                 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1197                 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1198                 1e20, 1e21, 1e22
1199 #ifdef VAX
1200                 , 1e23, 1e24
1201 #endif
1202                 };
1203
1204  static double
1205 #ifdef IEEE_Arith
1206 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1207 static double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1208 #define n_bigtens 5
1209 #else
1210 #ifdef IBM
1211 bigtens[] = { 1e16, 1e32, 1e64 };
1212 static double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1213 #define n_bigtens 3
1214 #else
1215 bigtens[] = { 1e16, 1e32 };
1216 static double tinytens[] = { 1e-16, 1e-32 };
1217 #define n_bigtens 2
1218 #endif
1219 #endif
1220
1221  double
1222 bsd_strtod
1223 #ifdef KR_headers
1224         (s00, se) CONST char *s00; char **se;
1225 #else
1226         (CONST char *s00, char **se)
1227 #endif
1228 {
1229         int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1230                  e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1231         CONST char *s, *s0, *s1;
1232         double aadj, aadj1, adj, rv, rv0;
1233         Long L;
1234         ULong y, z;
1235         Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1236         char decimal_point = '.';
1237
1238         sign = nz0 = nz = 0;
1239         rv = 0.;
1240         for (s = s00;;s++) switch(*s) {
1241                 case '-':
1242                         sign = 1;
1243                         /* no break */
1244                 case '+':
1245                         if (*++s)
1246                                 goto break2;
1247                         /* no break */
1248                 case 0:
1249                         s = s00;
1250                         goto ret;
1251                 default:
1252                         if (isspace((unsigned char)*s))
1253                                 continue;
1254                         goto break2;
1255         }
1256  break2:
1257         if (*s == '0') {
1258                 nz0 = 1;
1259                 while (*++s == '0') ;
1260                 if (!*s)
1261                         goto ret;
1262         }
1263         s0 = s;
1264         y = z = 0;
1265         for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1266                 if (nd < 9)
1267                         y = 10*y + c - '0';
1268                 else if (nd < 16)
1269                         z = 10*z + c - '0';
1270         nd0 = nd;
1271         if ((char)c == decimal_point) {
1272                 c = *++s;
1273                 if (!nd) {
1274                         for (; c == '0'; c = *++s)
1275                                 nz++;
1276                         if (c > '0' && c <= '9') {
1277                                 s0 = s;
1278                                 nf += nz;
1279                                 nz = 0;
1280                                 goto have_dig;
1281                         }
1282                         goto dig_done;
1283                 }
1284                 for (; c >= '0' && c <= '9'; c = *++s) {
1285  have_dig:
1286                         nz++;
1287                         if (c -= '0') {
1288                                 nf += nz;
1289                                 for (i = 1; i < nz; i++)
1290                                         if (nd++ < 9)
1291                                                 y *= 10;
1292                                         else if (nd <= DBL_DIG + 1)
1293                                                 z *= 10;
1294                                 if (nd++ < 9)
1295                                         y = 10*y + c;
1296                                 else if (nd <= DBL_DIG + 1)
1297                                         z = 10*z + c;
1298                                 nz = 0;
1299                         }
1300                 }
1301         }
1302  dig_done:
1303         e = 0;
1304         if (c == 'e' || c == 'E') {
1305                 if (!nd && !nz && !nz0) {
1306                         s = s00;
1307                         goto ret;
1308                 }
1309                 s00 = s;
1310                 esign = 0;
1311                 switch(c = *++s) {
1312                         case '-':
1313                                 esign = 1;
1314                         case '+':
1315                                 c = *++s;
1316                 }
1317                 if (c >= '0' && c <= '9') {
1318                         while (c == '0')
1319                                 c = *++s;
1320                         if (c > '0' && c <= '9') {
1321                                 L = c - '0';
1322                                 s1 = s;
1323                                 while ((c = *++s) >= '0' && c <= '9')
1324                                         L = 10*L + c - '0';
1325                                 if (s - s1 > 8 || L > 19999)
1326                                         /* Avoid confusion from exponents
1327                                          * so large that e might overflow.
1328                                          */
1329                                         e = 19999; /* safe for 16 bit ints */
1330                                 else
1331                                         e = (int)L;
1332                                 if (esign)
1333                                         e = -e;
1334                         } else
1335                                 e = 0;
1336                 } else
1337                         s = s00;
1338         }
1339         if (!nd) {
1340                 if (!nz && !nz0)
1341                         s = s00;
1342                 goto ret;
1343         }
1344         e1 = e -= nf;
1345
1346         /* Now we have nd0 digits, starting at s0, followed by a
1347          * decimal point, followed by nd-nd0 digits.  The number we're
1348          * after is the integer represented by those digits times
1349          * 10**e */
1350
1351         if (!nd0)
1352                 nd0 = nd;
1353         k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1354         rv = y;
1355         if (k > 9)
1356                 rv = tens[k - 9] * rv + z;
1357         if (nd <= DBL_DIG
1358 #ifndef RND_PRODQUOT
1359                 && FLT_ROUNDS == 1
1360 #endif
1361                         ) {
1362                 if (!e)
1363                         goto ret;
1364                 if (e > 0) {
1365                         if (e <= Ten_pmax) {
1366 #ifdef VAX
1367                                 goto vax_ovfl_check;
1368 #else
1369                                 /* rv = */ rounded_product(rv, tens[e]);
1370                                 goto ret;
1371 #endif
1372                                 }
1373                         i = DBL_DIG - nd;
1374                         if (e <= Ten_pmax + i) {
1375                                 /* A fancier test would sometimes let us do
1376                                  * this for larger i values.
1377                                  */
1378                                 e -= i;
1379                                 rv *= tens[i];
1380 #ifdef VAX
1381                                 /* VAX exponent range is so narrow we must
1382                                  * worry about overflow here...
1383                                  */
1384  vax_ovfl_check:
1385                                 word0(rv) -= P*Exp_msk1;
1386                                 /* rv = */ rounded_product(rv, tens[e]);
1387                                 if ((word0(rv) & Exp_mask)
1388                                  > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1389                                         goto ovfl;
1390                                 word0(rv) += P*Exp_msk1;
1391 #else
1392                                 /* rv = */ rounded_product(rv, tens[e]);
1393 #endif
1394                                 goto ret;
1395                         }
1396                 }
1397 #ifndef Inaccurate_Divide
1398                 else if (e >= -Ten_pmax) {
1399                         /* rv = */ rounded_quotient(rv, tens[-e]);
1400                         goto ret;
1401                 }
1402 #endif
1403         }
1404         e1 += nd - k;
1405
1406         /* Get starting approximation = rv * 10**e1 */
1407
1408         if (e1 > 0) {
1409                 if ( (i = e1 & 15) )
1410                         rv *= tens[i];
1411                 if ( (e1 &= ~15) ) {
1412                         if (e1 > DBL_MAX_10_EXP) {
1413  ovfl:
1414                                 errno = ERANGE;
1415 #ifdef __STDC__
1416                                 rv = HUGE_VAL;
1417 #else
1418                                 /* Can't trust HUGE_VAL */
1419 #ifdef IEEE_Arith
1420                                 word0(rv) = Exp_mask;
1421                                 word1(rv) = 0;
1422 #else
1423                                 word0(rv) = Big0;
1424                                 word1(rv) = Big1;
1425 #endif
1426 #endif
1427                                 goto ret;
1428                         }
1429                         if (e1 >>= 4) {
1430                                 for (j = 0; e1 > 1; j++, e1 >>= 1)
1431                                         if (e1 & 1)
1432                                                 rv *= bigtens[j];
1433                         /* The last multiplication could overflow. */
1434                                 word0(rv) -= P*Exp_msk1;
1435                                 rv *= bigtens[j];
1436                                 if ((z = word0(rv) & Exp_mask)
1437                                  > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1438                                         goto ovfl;
1439                                 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1440                                         /* set to largest number */
1441                                         /* (Can't trust DBL_MAX) */
1442                                         word0(rv) = Big0;
1443                                         word1(rv) = Big1;
1444                                         }
1445                                 else
1446                                         word0(rv) += P*Exp_msk1;
1447                         }
1448                 }
1449         } else if (e1 < 0) {
1450                 e1 = -e1;
1451                 if ( (i = e1 & 15) )
1452                         rv /= tens[i];
1453                 if ( (e1 &= ~15) ) {
1454                         e1 >>= 4;
1455                         for (j = 0; e1 > 1; j++, e1 >>= 1)
1456                                 if (e1 & 1)
1457                                         rv *= tinytens[j];
1458                         /* The last multiplication could underflow. */
1459                         rv0 = rv;
1460                         rv *= tinytens[j];
1461                         if (!rv) {
1462                                 rv = 2.*rv0;
1463                                 rv *= tinytens[j];
1464                                 if (!rv) {
1465  undfl:
1466                                         rv = 0.;
1467                                         errno = ERANGE;
1468                                         goto ret;
1469                                         }
1470                                 word0(rv) = Tiny0;
1471                                 word1(rv) = Tiny1;
1472                                 /* The refinement below will clean
1473                                  * this approximation up.
1474                                  */
1475                         }
1476                 }
1477         }
1478
1479         /* Now the hard part -- adjusting rv to the correct value.*/
1480
1481         /* Put digits into bd: true value = bd * 10^e */
1482
1483         bd0 = s2b(s0, nd0, nd, y);
1484
1485         for (;;) {
1486                 bd = Balloc(bd0->k);
1487                 Bcopy(bd, bd0);
1488                 bb = d2b(rv, &bbe, &bbbits);    /* rv = bb * 2^bbe */
1489                 bs = i2b(1);
1490
1491                 if (e >= 0) {
1492                         bb2 = bb5 = 0;
1493                         bd2 = bd5 = e;
1494                 } else {
1495                         bb2 = bb5 = -e;
1496                         bd2 = bd5 = 0;
1497                 }
1498                 if (bbe >= 0)
1499                         bb2 += bbe;
1500                 else
1501                         bd2 -= bbe;
1502                 bs2 = bb2;
1503 #ifdef Sudden_Underflow
1504 #ifdef IBM
1505                 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1506 #else
1507                 j = P + 1 - bbbits;
1508 #endif
1509 #else
1510                 i = bbe + bbbits - 1;   /* logb(rv) */
1511                 if (i < Emin)   /* denormal */
1512                         j = bbe + (P-Emin);
1513                 else
1514                         j = P + 1 - bbbits;
1515 #endif
1516                 bb2 += j;
1517                 bd2 += j;
1518                 i = bb2 < bd2 ? bb2 : bd2;
1519                 if (i > bs2)
1520                         i = bs2;
1521                 if (i > 0) {
1522                         bb2 -= i;
1523                         bd2 -= i;
1524                         bs2 -= i;
1525                         }
1526                 if (bb5 > 0) {
1527                         bs = pow5mult(bs, bb5);
1528                         bb1 = mult(bs, bb);
1529                         Bfree(bb);
1530                         bb = bb1;
1531                         }
1532                 if (bb2 > 0)
1533                         bb = lshift(bb, bb2);
1534                 if (bd5 > 0)
1535                         bd = pow5mult(bd, bd5);
1536                 if (bd2 > 0)
1537                         bd = lshift(bd, bd2);
1538                 if (bs2 > 0)
1539                         bs = lshift(bs, bs2);
1540                 delta = diff(bb, bd);
1541                 dsign = delta->sign;
1542                 delta->sign = 0;
1543                 i = cmp(delta, bs);
1544                 if (i < 0) {
1545                         /* Error is less than half an ulp -- check for
1546                          * special case of mantissa a power of two.
1547                          */
1548                         if (dsign || word1(rv) || word0(rv) & Bndry_mask)
1549                                 break;
1550                         delta = lshift(delta,Log2P);
1551                         if (cmp(delta, bs) > 0)
1552                                 goto drop_down;
1553                         break;
1554                 }
1555                 if (i == 0) {
1556                         /* exactly half-way between */
1557                         if (dsign) {
1558                                 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
1559                                  &&  word1(rv) == 0xffffffff) {
1560                                         /*boundary case -- increment exponent*/
1561                                         word0(rv) = (word0(rv) & Exp_mask)
1562                                                 + Exp_msk1
1563 #ifdef IBM
1564                                                 | Exp_msk1 >> 4
1565 #endif
1566                                                 ;
1567                                         word1(rv) = 0;
1568                                         break;
1569                                 }
1570                         } else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
1571  drop_down:
1572                                 /* boundary case -- decrement exponent */
1573 #ifdef Sudden_Underflow
1574                                 L = word0(rv) & Exp_mask;
1575 #ifdef IBM
1576                                 if (L <  Exp_msk1)
1577 #else
1578                                 if (L <= Exp_msk1)
1579 #endif
1580                                         goto undfl;
1581                                 L -= Exp_msk1;
1582 #else
1583                                 L = (word0(rv) & Exp_mask) - Exp_msk1;
1584 #endif
1585                                 word0(rv) = L | Bndry_mask1;
1586                                 word1(rv) = 0xffffffff;
1587 #ifdef IBM
1588                                 goto cont;
1589 #else
1590                                 break;
1591 #endif
1592                         }
1593 #ifndef ROUND_BIASED
1594                         if (!(word1(rv) & LSB))
1595                                 break;
1596 #endif
1597                         if (dsign)
1598                                 rv += ulp(rv);
1599 #ifndef ROUND_BIASED
1600                         else {
1601                                 rv -= ulp(rv);
1602 #ifndef Sudden_Underflow
1603                                 if (!rv)
1604                                         goto undfl;
1605 #endif
1606                         }
1607 #endif
1608                         break;
1609                 }
1610                 if ((aadj = ratio(delta, bs)) <= 2.) {
1611                         if (dsign)
1612                                 aadj = aadj1 = 1.;
1613                         else if (word1(rv) || word0(rv) & Bndry_mask) {
1614 #ifndef Sudden_Underflow
1615                                 if (word1(rv) == Tiny1 && !word0(rv))
1616                                         goto undfl;
1617 #endif
1618                                 aadj = 1.;
1619                                 aadj1 = -1.;
1620                         } else {
1621                                 /* special case -- power of FLT_RADIX to be */
1622                                 /* rounded down... */
1623
1624                                 if (aadj < 2./FLT_RADIX)
1625                                         aadj = 1./FLT_RADIX;
1626                                 else
1627                                         aadj *= 0.5;
1628                                 aadj1 = -aadj;
1629                         }
1630                 } else {
1631                         aadj *= 0.5;
1632                         aadj1 = dsign ? aadj : -aadj;
1633 #ifdef Check_FLT_ROUNDS
1634                         switch(FLT_ROUNDS) {
1635                                 case 2: /* towards +infinity */
1636                                         aadj1 -= 0.5;
1637                                         break;
1638                                 case 0: /* towards 0 */
1639                                 case 3: /* towards -infinity */
1640                                         aadj1 += 0.5;
1641                         }
1642 #else
1643                         if (FLT_ROUNDS == 0)
1644                                 aadj1 += 0.5;
1645 #endif
1646                 }
1647                 y = word0(rv) & Exp_mask;
1648
1649                 /* Check for overflow */
1650
1651                 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
1652                         rv0 = rv;
1653                         word0(rv) -= P*Exp_msk1;
1654                         adj = aadj1 * ulp(rv);
1655                         rv += adj;
1656                         if ((word0(rv) & Exp_mask) >=
1657                                         Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
1658                                 if (word0(rv0) == Big0 && word1(rv0) == Big1)
1659                                         goto ovfl;
1660                                 word0(rv) = Big0;
1661                                 word1(rv) = Big1;
1662                                 goto cont;
1663                         } else
1664                                 word0(rv) += P*Exp_msk1;
1665                 } else {
1666 #ifdef Sudden_Underflow
1667                         if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
1668                                 rv0 = rv;
1669                                 word0(rv) += P*Exp_msk1;
1670                                 adj = aadj1 * ulp(rv);
1671                                 rv += adj;
1672 #ifdef IBM
1673                                 if ((word0(rv) & Exp_mask) <  P*Exp_msk1)
1674 #else
1675                                 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
1676 #endif
1677                                 {
1678                                         if (word0(rv0) == Tiny0
1679                                          && word1(rv0) == Tiny1)
1680                                                 goto undfl;
1681                                         word0(rv) = Tiny0;
1682                                         word1(rv) = Tiny1;
1683                                         goto cont;
1684                                 } else
1685                                         word0(rv) -= P*Exp_msk1;
1686                         } else {
1687                                 adj = aadj1 * ulp(rv);
1688                                 rv += adj;
1689                         }
1690 #else
1691                         /* Compute adj so that the IEEE rounding rules will
1692                          * correctly round rv + adj in some half-way cases.
1693                          * If rv * ulp(rv) is denormalized (i.e.,
1694                          * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1695                          * trouble from bits lost to denormalization;
1696                          * example: 1.2e-307 .
1697                          */
1698                         if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
1699                                 aadj1 = (double)(int)(aadj + 0.5);
1700                                 if (!dsign)
1701                                         aadj1 = -aadj1;
1702                         }
1703                         adj = aadj1 * ulp(rv);
1704                         rv += adj;
1705 #endif
1706                 }
1707                 z = word0(rv) & Exp_mask;
1708                 if (y == z) {
1709                         /* Can we stop now? */
1710                         L = aadj;
1711                         aadj -= L;
1712                         /* The tolerances below are conservative. */
1713                         if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
1714                                 if (aadj < .4999999 || aadj > .5000001)
1715                                         break;
1716                         } else if (aadj < .4999999/FLT_RADIX)
1717                                 break;
1718                 }
1719  cont:
1720                 Bfree(bb);
1721                 Bfree(bd);
1722                 Bfree(bs);
1723                 Bfree(delta);
1724         }
1725         Bfree(bb);
1726         Bfree(bd);
1727         Bfree(bs);
1728         Bfree(bd0);
1729         Bfree(delta);
1730  ret:
1731         if (se)
1732                 *se = (char *)s;
1733         return sign ? -rv : rv;
1734 }
1735
1736  static int
1737 quorem
1738 #ifdef KR_headers
1739         (b, S) Bigint *b, *S;
1740 #else
1741         (Bigint *b, Bigint *S)
1742 #endif
1743 {
1744         int n;
1745         Long borrow, y;
1746         ULong carry, q, ys;
1747         ULong *bx, *bxe, *sx, *sxe;
1748 #ifdef Pack_32
1749         Long z;
1750         ULong si, zs;
1751 #endif
1752
1753         n = S->wds;
1754 #ifdef DEBUG
1755         /*debug*/ if (b->wds > n)
1756         /*debug*/       Bug("oversize b in quorem");
1757 #endif
1758         if (b->wds < n)
1759                 return 0;
1760         sx = S->x;
1761         sxe = sx + --n;
1762         bx = b->x;
1763         bxe = bx + n;
1764         q = *bxe / (*sxe + 1);  /* ensure q <= true quotient */
1765 #ifdef DEBUG
1766         /*debug*/ if (q > 9)
1767         /*debug*/       Bug("oversized quotient in quorem");
1768 #endif
1769         if (q) {
1770                 borrow = 0;
1771                 carry = 0;
1772                 do {
1773 #ifdef Pack_32
1774                         si = *sx++;
1775                         ys = (si & 0xffff) * q + carry;
1776                         zs = (si >> 16) * q + (ys >> 16);
1777                         carry = zs >> 16;
1778                         y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1779                         borrow = y >> 16;
1780                         Sign_Extend(borrow, y);
1781                         z = (*bx >> 16) - (zs & 0xffff) + borrow;
1782                         borrow = z >> 16;
1783                         Sign_Extend(borrow, z);
1784                         Storeinc(bx, z, y);
1785 #else
1786                         ys = *sx++ * q + carry;
1787                         carry = ys >> 16;
1788                         y = *bx - (ys & 0xffff) + borrow;
1789                         borrow = y >> 16;
1790                         Sign_Extend(borrow, y);
1791                         *bx++ = y & 0xffff;
1792 #endif
1793                 } while (sx <= sxe);
1794                 if (!*bxe) {
1795                         bx = b->x;
1796                         while (--bxe > bx && !*bxe)
1797                                 --n;
1798                         b->wds = n;
1799                 }
1800         }
1801         if (cmp(b, S) >= 0) {
1802                 q++;
1803                 borrow = 0;
1804                 carry = 0;
1805                 bx = b->x;
1806                 sx = S->x;
1807                 do {
1808 #ifdef Pack_32
1809                         si = *sx++;
1810                         ys = (si & 0xffff) + carry;
1811                         zs = (si >> 16) + (ys >> 16);
1812                         carry = zs >> 16;
1813                         y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1814                         borrow = y >> 16;
1815                         Sign_Extend(borrow, y);
1816                         z = (*bx >> 16) - (zs & 0xffff) + borrow;
1817                         borrow = z >> 16;
1818                         Sign_Extend(borrow, z);
1819                         Storeinc(bx, z, y);
1820 #else
1821                         ys = *sx++ + carry;
1822                         carry = ys >> 16;
1823                         y = *bx - (ys & 0xffff) + borrow;
1824                         borrow = y >> 16;
1825                         Sign_Extend(borrow, y);
1826                         *bx++ = y & 0xffff;
1827 #endif
1828                 } while (sx <= sxe);
1829                 bx = b->x;
1830                 bxe = bx + n;
1831                 if (!*bxe) {
1832                         while (--bxe > bx && !*bxe)
1833                                 --n;
1834                         b->wds = n;
1835                 }
1836         }
1837         return q;
1838 }
1839
1840 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1841  *
1842  * Inspired by "How to Print Floating-Point Numbers Accurately" by
1843  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1844  *
1845  * Modifications:
1846  *      1. Rather than iterating, we use a simple numeric overestimate
1847  *         to determine k = floor(log10(d)).  We scale relevant
1848  *         quantities using O(log2(k)) rather than O(k) multiplications.
1849  *      2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1850  *         try to generate digits strictly left to right.  Instead, we
1851  *         compute with fewer bits and propagate the carry if necessary
1852  *         when rounding the final digit up.  This is often faster.
1853  *      3. Under the assumption that input will be rounded nearest,
1854  *         mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1855  *         That is, we allow equality in stopping tests when the
1856  *         round-nearest rule will give the same floating-point value
1857  *         as would satisfaction of the stopping test with strict
1858  *         inequality.
1859  *      4. We remove common factors of powers of 2 from relevant
1860  *         quantities.
1861  *      5. When converting floating-point integers less than 1e16,
1862  *         we use floating-point arithmetic rather than resorting
1863  *         to multiple-precision integers.
1864  *      6. When asked to produce fewer than 15 digits, we first try
1865  *         to get by with floating-point arithmetic; we resort to
1866  *         multiple-precision integer arithmetic only if we cannot
1867  *         guarantee that the floating-point calculation has given
1868  *         the correctly rounded result.  For k requested digits and
1869  *         "uniformly" distributed input, the probability is
1870  *         something like 10^(k-15) that we must resort to the long
1871  *         calculation.
1872  */
1873
1874 char *
1875 __bsd_dtoa
1876 #ifdef KR_headers
1877         (d, mode, ndigits, decpt, sign, rve, resultp)
1878         double d; int mode, ndigits, *decpt, *sign; char **rve, **resultp;
1879 #else
1880         (double d, int mode, int ndigits, int *decpt, int *sign, char **rve,
1881          char **resultp)
1882 #endif
1883 {
1884  /*     Arguments ndigits, decpt, sign are similar to those
1885         of ecvt and fcvt; trailing zeros are suppressed from
1886         the returned string.  If not null, *rve is set to point
1887         to the end of the return value.  If d is +-Infinity or NaN,
1888         then *decpt is set to 9999.
1889
1890         mode:
1891                 0 ==> shortest string that yields d when read in
1892                         and rounded to nearest.
1893                 1 ==> like 0, but with Steele & White stopping rule;
1894                         e.g. with IEEE P754 arithmetic , mode 0 gives
1895                         1e23 whereas mode 1 gives 9.999999999999999e22.
1896                 2 ==> max(1,ndigits) significant digits.  This gives a
1897                         return value similar to that of ecvt, except
1898                         that trailing zeros are suppressed.
1899                 3 ==> through ndigits past the decimal point.  This
1900                         gives a return value similar to that from fcvt,
1901                         except that trailing zeros are suppressed, and
1902                         ndigits can be negative.
1903                 4-9 should give the same return values as 2-3, i.e.,
1904                         4 <= mode <= 9 ==> same return as mode
1905                         2 + (mode & 1).  These modes are mainly for
1906                         debugging; often they run slower but sometimes
1907                         faster than modes 2-3.
1908                 4,5,8,9 ==> left-to-right digit generation.
1909                 6-9 ==> don't try fast floating-point estimate
1910                         (if applicable).
1911
1912                 Values of mode other than 0-9 are treated as mode 0.
1913
1914                 Sufficient space is allocated to the return value
1915                 to hold the suppressed trailing zeros.
1916         */
1917
1918         int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
1919                 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
1920                 spec_case, try_quick;
1921         Long L;
1922 #ifndef Sudden_Underflow
1923         int denorm;
1924         ULong x;
1925 #endif
1926         Bigint *b, *b1, *delta, *mlo, *mhi, *S;
1927         double d2, ds, eps;
1928         char *s, *s0;
1929
1930         if (word0(d) & Sign_bit) {
1931                 /* set sign for everything, including 0's and NaNs */
1932                 *sign = 1;
1933                 word0(d) &= ~Sign_bit;  /* clear sign bit */
1934         }
1935         else
1936                 *sign = 0;
1937
1938 #if defined(IEEE_Arith) + defined(VAX)
1939 #ifdef IEEE_Arith
1940         if ((word0(d) & Exp_mask) == Exp_mask)
1941 #else
1942         if (word0(d)  == 0x8000)
1943 #endif
1944         {
1945                 /* Infinity or NaN */
1946                 const char *ss;
1947                 *decpt = 9999;
1948                 ss =
1949 #ifdef IEEE_Arith
1950                         !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" :
1951 #endif
1952                                 "NaN";
1953                 *resultp = s = malloc (strlen (ss) + 1);
1954                 strcpy (s, ss);
1955                 if (rve)
1956                         *rve =
1957 #ifdef IEEE_Arith
1958                                 s[3] ? s + 8 :
1959 #endif
1960                                                 s + 3;
1961                 return s;
1962         }
1963 #endif
1964 #ifdef IBM
1965         d += 0; /* normalize */
1966 #endif
1967         if (!d) {
1968                 *decpt = 1;
1969                 *resultp = s = malloc (2);
1970                 s [0] = '0';
1971                 s [1] = 0;
1972                 if (rve)
1973                         *rve = s + 1;
1974                 return s;
1975         }
1976
1977         b = d2b(d, &be, &bbits);
1978 #ifdef Sudden_Underflow
1979         i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
1980 #else
1981         if ( (i = (int)((word0(d) >> Exp_shift1) & (Exp_mask>>Exp_shift1))) ) {
1982 #endif
1983                 d2 = d;
1984                 word0(d2) &= Frac_mask1;
1985                 word0(d2) |= Exp_11;
1986 #ifdef IBM
1987                 if ( (j = 11 - hi0bits(word0(d2) & Frac_mask)) )
1988                         d2 /= 1 << j;
1989 #endif
1990
1991                 /* log(x)       ~=~ log(1.5) + (x-1.5)/1.5
1992                  * log10(x)      =  log(x) / log(10)
1993                  *              ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1994                  * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1995                  *
1996                  * This suggests computing an approximation k to log10(d) by
1997                  *
1998                  * k = (i - Bias)*0.301029995663981
1999                  *      + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2000                  *
2001                  * We want k to be too large rather than too small.
2002                  * The error in the first-order Taylor series approximation
2003                  * is in our favor, so we just round up the constant enough
2004                  * to compensate for any error in the multiplication of
2005                  * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2006                  * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2007                  * adding 1e-13 to the constant term more than suffices.
2008                  * Hence we adjust the constant term to 0.1760912590558.
2009                  * (We could get a more accurate k by invoking log10,
2010                  *  but this is probably not worthwhile.)
2011                  */
2012
2013                 i -= Bias;
2014 #ifdef IBM
2015                 i <<= 2;
2016                 i += j;
2017 #endif
2018 #ifndef Sudden_Underflow
2019                 denorm = 0;
2020         } else {
2021                 /* d is denormalized */
2022
2023                 i = bbits + be + (Bias + (P-1) - 1);
2024                 x = i > 32  ? ((word0(d) << (64 - i)) | (word1(d) >> (i - 32)))
2025                             : (word1(d) << (32 - i));
2026                 d2 = x;
2027                 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
2028                 i -= (Bias + (P-1) - 1) + 1;
2029                 denorm = 1;
2030         }
2031 #endif
2032         ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
2033         k = (int)ds;
2034         if (ds < 0. && ds != k)
2035                 k--;    /* want k = floor(ds) */
2036         k_check = 1;
2037         if (k >= 0 && k <= Ten_pmax) {
2038                 if (d < tens[k])
2039                         k--;
2040                 k_check = 0;
2041         }
2042         j = bbits - i - 1;
2043         if (j >= 0) {
2044                 b2 = 0;
2045                 s2 = j;
2046         } else {
2047                 b2 = -j;
2048                 s2 = 0;
2049         }
2050         if (k >= 0) {
2051                 b5 = 0;
2052                 s5 = k;
2053                 s2 += k;
2054         } else {
2055                 b2 -= k;
2056                 b5 = -k;
2057                 s5 = 0;
2058         }
2059         if (mode < 0 || mode > 9)
2060                 mode = 0;
2061         try_quick = 1;
2062         if (mode > 5) {
2063                 mode -= 4;
2064                 try_quick = 0;
2065         }
2066         leftright = 1;
2067         switch(mode) {
2068                 case 0:
2069                 case 1:
2070                         ilim = ilim1 = -1;
2071                         i = 18;
2072                         ndigits = 0;
2073                         break;
2074                 case 2:
2075                         leftright = 0;
2076                         /* no break */
2077                 case 4:
2078                         if (ndigits <= 0)
2079                                 ndigits = 1;
2080                         ilim = ilim1 = i = ndigits;
2081                         break;
2082                 case 3:
2083                         leftright = 0;
2084                         /* no break */
2085                 case 5:
2086                         i = ndigits + k + 1;
2087                         ilim = i;
2088                         ilim1 = i - 1;
2089                         if (i <= 0)
2090                                 i = 1;
2091         }
2092         *resultp = (char *) malloc(i + 1);
2093         s = s0 = *resultp;
2094
2095         if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2096
2097                 /* Try to get by with floating-point arithmetic. */
2098
2099                 i = 0;
2100                 d2 = d;
2101                 k0 = k;
2102                 ilim0 = ilim;
2103                 ieps = 2; /* conservative */
2104                 if (k > 0) {
2105                         ds = tens[k&0xf];
2106                         j = k >> 4;
2107                         if (j & Bletch) {
2108                                 /* prevent overflows */
2109                                 j &= Bletch - 1;
2110                                 d /= bigtens[n_bigtens-1];
2111                                 ieps++;
2112                         }
2113                         for (; j; j >>= 1, i++)
2114                                 if (j & 1) {
2115                                         ieps++;
2116                                         ds *= bigtens[i];
2117                                 }
2118                         d /= ds;
2119                 } else if ( (j1 = -k) ) {
2120                         d *= tens[j1 & 0xf];
2121                         for (j = j1 >> 4; j; j >>= 1, i++)
2122                                 if (j & 1) {
2123                                         ieps++;
2124                                         d *= bigtens[i];
2125                                 }
2126                 }
2127                 if (k_check && d < 1. && ilim > 0) {
2128                         if (ilim1 <= 0)
2129                                 goto fast_failed;
2130                         ilim = ilim1;
2131                         k--;
2132                         d *= 10.;
2133                         ieps++;
2134                 }
2135                 eps = ieps*d + 7.;
2136                 word0(eps) -= (P-1)*Exp_msk1;
2137                 if (ilim == 0) {
2138                         S = mhi = 0;
2139                         d -= 5.;
2140                         if (d > eps)
2141                                 goto one_digit;
2142                         if (d < -eps)
2143                                 goto no_digits;
2144                         goto fast_failed;
2145                 }
2146 #ifndef No_leftright
2147                 if (leftright) {
2148                         /* Use Steele & White method of only
2149                          * generating digits needed.
2150                          */
2151                         eps = 0.5/tens[ilim-1] - eps;
2152                         for (i = 0;;) {
2153                                 L = d;
2154                                 d -= L;
2155                                 *s++ = '0' + (int)L;
2156                                 if (d < eps)
2157                                         goto ret1;
2158                                 if (1. - d < eps)
2159                                         goto bump_up;
2160                                 if (++i >= ilim)
2161                                         break;
2162                                 eps *= 10.;
2163                                 d *= 10.;
2164                         }
2165                 } else {
2166 #endif
2167                         /* Generate ilim digits, then fix them up. */
2168                         eps *= tens[ilim-1];
2169                         for (i = 1;; i++, d *= 10.) {
2170                                 L = d;
2171                                 d -= L;
2172                                 *s++ = '0' + (int)L;
2173                                 if (i == ilim) {
2174                                         if (d > 0.5 + eps)
2175                                                 goto bump_up;
2176                                         else if (d < 0.5 - eps) {
2177                                                 while (*--s == '0');
2178                                                 s++;
2179                                                 goto ret1;
2180                                         }
2181                                         break;
2182                                 }
2183                         }
2184 #ifndef No_leftright
2185                 }
2186 #endif
2187  fast_failed:
2188                 s = s0;
2189                 d = d2;
2190                 k = k0;
2191                 ilim = ilim0;
2192         }
2193
2194         /* Do we have a "small" integer? */
2195
2196         if (be >= 0 && k <= Int_max) {
2197                 /* Yes. */
2198                 ds = tens[k];
2199                 if (ndigits < 0 && ilim <= 0) {
2200                         S = mhi = 0;
2201                         if (ilim < 0 || d <= 5*ds)
2202                                 goto no_digits;
2203                         goto one_digit;
2204                 }
2205                 for (i = 1;; i++) {
2206                         L = d / ds;
2207                         d -= L*ds;
2208 #ifdef Check_FLT_ROUNDS
2209                         /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2210                         if (d < 0) {
2211                                 L--;
2212                                 d += ds;
2213                         }
2214 #endif
2215                         *s++ = '0' + (int)L;
2216                         if (i == ilim) {
2217                                 d += d;
2218                                 if (d > ds || (d == ds && L & 1)) {
2219  bump_up:
2220                                         while (*--s == '9')
2221                                                 if (s == s0) {
2222                                                         k++;
2223                                                         *s = '0';
2224                                                         break;
2225                                                 }
2226                                         ++*s++;
2227                                 }
2228                                 break;
2229                         }
2230                         if (!(d *= 10.))
2231                                 break;
2232                 }
2233                 goto ret1;
2234         }
2235
2236         m2 = b2;
2237         m5 = b5;
2238         mhi = mlo = 0;
2239         if (leftright) {
2240                 if (mode < 2) {
2241                         i =
2242 #ifndef Sudden_Underflow
2243                                 denorm ? be + (Bias + (P-1) - 1 + 1) :
2244 #endif
2245 #ifdef IBM
2246                                 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
2247 #else
2248                                 1 + P - bbits;
2249 #endif
2250                 } else {
2251                         j = ilim - 1;
2252                         if (m5 >= j)
2253                                 m5 -= j;
2254                         else {
2255                                 s5 += j -= m5;
2256                                 b5 += j;
2257                                 m5 = 0;
2258                         }
2259                         if ((i = ilim) < 0) {
2260                                 m2 -= i;
2261                                 i = 0;
2262                         }
2263                 }
2264                 b2 += i;
2265                 s2 += i;
2266                 mhi = i2b(1);
2267         }
2268         if (m2 > 0 && s2 > 0) {
2269                 i = m2 < s2 ? m2 : s2;
2270                 b2 -= i;
2271                 m2 -= i;
2272                 s2 -= i;
2273         }
2274         if (b5 > 0) {
2275                 if (leftright) {
2276                         if (m5 > 0) {
2277                                 mhi = pow5mult(mhi, m5);
2278                                 b1 = mult(mhi, b);
2279                                 Bfree(b);
2280                                 b = b1;
2281                                 }
2282                         if ( (j = b5 - m5) )
2283                                 b = pow5mult(b, j);
2284                 } else
2285                         b = pow5mult(b, b5);
2286         }
2287         S = i2b(1);
2288         if (s5 > 0)
2289                 S = pow5mult(S, s5);
2290
2291         /* Check for special case that d is a normalized power of 2. */
2292
2293         if (mode < 2) {
2294                 if (!word1(d) && !(word0(d) & Bndry_mask)
2295 #ifndef Sudden_Underflow
2296                  && word0(d) & Exp_mask
2297 #endif
2298                                 ) {
2299                         /* The special case */
2300                         b2 += Log2P;
2301                         s2 += Log2P;
2302                         spec_case = 1;
2303                 } else
2304                         spec_case = 0;
2305         }
2306
2307         /* Arrange for convenient computation of quotients:
2308          * shift left if necessary so divisor has 4 leading 0 bits.
2309          *
2310          * Perhaps we should just compute leading 28 bits of S once
2311          * and for all and pass them and a shift to quorem, so it
2312          * can do shifts and ors to compute the numerator for q.
2313          */
2314 #ifdef Pack_32
2315         if ( (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) )
2316                 i = 32 - i;
2317 #else
2318         if ( (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) )
2319                 i = 16 - i;
2320 #endif
2321         if (i > 4) {
2322                 i -= 4;
2323                 b2 += i;
2324                 m2 += i;
2325                 s2 += i;
2326         } else if (i < 4) {
2327                 i += 28;
2328                 b2 += i;
2329                 m2 += i;
2330                 s2 += i;
2331         }
2332         if (b2 > 0)
2333                 b = lshift(b, b2);
2334         if (s2 > 0)
2335                 S = lshift(S, s2);
2336         if (k_check) {
2337                 if (cmp(b,S) < 0) {
2338                         k--;
2339                         b = multadd(b, 10, 0);  /* we botched the k estimate */
2340                         if (leftright)
2341                                 mhi = multadd(mhi, 10, 0);
2342                         ilim = ilim1;
2343                 }
2344         }
2345         if (ilim <= 0 && mode > 2) {
2346                 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
2347                         /* no digits, fcvt style */
2348  no_digits:
2349                         k = -1 - ndigits;
2350                         goto ret;
2351                 }
2352  one_digit:
2353                 *s++ = '1';
2354                 k++;
2355                 goto ret;
2356         }
2357         if (leftright) {
2358                 if (m2 > 0)
2359                         mhi = lshift(mhi, m2);
2360
2361                 /* Compute mlo -- check for special case
2362                  * that d is a normalized power of 2.
2363                  */
2364
2365                 mlo = mhi;
2366                 if (spec_case) {
2367                         mhi = Balloc(mhi->k);
2368                         Bcopy(mhi, mlo);
2369                         mhi = lshift(mhi, Log2P);
2370                 }
2371
2372                 for (i = 1;;i++) {
2373                         dig = quorem(b,S) + '0';
2374                         /* Do we yet have the shortest decimal string
2375                          * that will round to d?
2376                          */
2377                         j = cmp(b, mlo);
2378                         delta = diff(S, mhi);
2379                         j1 = delta->sign ? 1 : cmp(b, delta);
2380                         Bfree(delta);
2381 #ifndef ROUND_BIASED
2382                         if (j1 == 0 && !mode && !(word1(d) & 1)) {
2383                                 if (dig == '9')
2384                                         goto round_9_up;
2385                                 if (j > 0)
2386                                         dig++;
2387                                 *s++ = dig;
2388                                 goto ret;
2389                         }
2390 #endif
2391                         if (j < 0 || (j == 0 && !mode
2392 #ifndef ROUND_BIASED
2393                                                         && !(word1(d) & 1)
2394 #endif
2395                                         )) {
2396                                 if (j1 > 0) {
2397                                         b = lshift(b, 1);
2398                                         j1 = cmp(b, S);
2399                                         if ((j1 > 0 || (j1 == 0 && dig & 1))
2400                                         && dig++ == '9')
2401                                                 goto round_9_up;
2402                                 }
2403                                 *s++ = dig;
2404                                 goto ret;
2405                         }
2406                         if (j1 > 0) {
2407                                 if (dig == '9') { /* possible if i == 1 */
2408  round_9_up:
2409                                         *s++ = '9';
2410                                         goto roundoff;
2411                                 }
2412                                 *s++ = dig + 1;
2413                                 goto ret;
2414                         }
2415                         *s++ = dig;
2416                         if (i == ilim)
2417                                 break;
2418                         b = multadd(b, 10, 0);
2419                         if (mlo == mhi)
2420                                 mlo = mhi = multadd(mhi, 10, 0);
2421                         else {
2422                                 mlo = multadd(mlo, 10, 0);
2423                                 mhi = multadd(mhi, 10, 0);
2424                         }
2425                 }
2426         } else
2427                 for (i = 1;; i++) {
2428                         *s++ = dig = quorem(b,S) + '0';
2429                         if (i >= ilim)
2430                                 break;
2431                         b = multadd(b, 10, 0);
2432                 }
2433
2434         /* Round off last digit */
2435
2436         b = lshift(b, 1);
2437         j = cmp(b, S);
2438         if (j > 0 || (j == 0 && dig & 1)) {
2439  roundoff:
2440                 while (*--s == '9')
2441                         if (s == s0) {
2442                                 k++;
2443                                 *s++ = '1';
2444                                 goto ret;
2445                         }
2446                 ++*s++;
2447         } else {
2448                 while (*--s == '0');
2449                 s++;
2450         }
2451  ret:
2452         Bfree(S);
2453         if (mhi) {
2454                 if (mlo && mlo != mhi)
2455                         Bfree(mlo);
2456                 Bfree(mhi);
2457         }
2458  ret1:
2459         Bfree(b);
2460         if (s == s0) {  /* don't return empty string */
2461                 *s++ = '0';
2462                 k = 0;
2463         }
2464         *s = 0;
2465         *decpt = k + 1;
2466         if (rve)
2467                 *rve = s;
2468         return s0;
2469         }
2470 #ifdef __cplusplus
2471 }
2472 #endif