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