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