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