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