[runtime] Remove some unused metadata includes from utils/.
[mono.git] / mono / utils / strtod.c
1 /****************************************************************
2  *
3  * The author of this software is David M. Gay.
4  *
5  * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
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 LUCENT 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 #include "config.h"
20 #include "strtod.h"
21 #include <glib.h>
22 #define freedtoa __freedtoa
23 #define dtoa __dtoa
24
25 #define Omit_Private_Memory
26 #define MULTIPLE_THREADS 1
27 /* Lock 0 is not used because of USE_MALLOC, Lock 1 protects a lazy-initialized table */
28 #define ACQUIRE_DTOA_LOCK(n)
29 #define FREE_DTOA_LOCK(n)
30
31 /* Please send bug reports to David M. Gay (dmg at acm dot org,
32  * with " at " changed at "@" and " dot " changed to ".").      */
33
34 /* On a machine with IEEE extended-precision registers, it is
35  * necessary to specify double-precision (53-bit) rounding precision
36  * before invoking strtod or dtoa.  If the machine uses (the equivalent
37  * of) Intel 80x87 arithmetic, the call
38  *      _control87(PC_53, MCW_PC);
39  * does this with many compilers.  Whether this or another call is
40  * appropriate depends on the compiler; for this to work, it may be
41  * necessary to #include "float.h" or another system-dependent header
42  * file.
43  */
44
45 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
46  *
47  * This strtod returns a nearest machine number to the input decimal
48  * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
49  * broken by the IEEE round-even rule.  Otherwise ties are broken by
50  * biased rounding (add half and chop).
51  *
52  * Inspired loosely by William D. Clinger's paper "How to Read Floating
53  * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
54  *
55  * Modifications:
56  *
57  *      1. We only require IEEE, IBM, or VAX double-precision
58  *              arithmetic (not IEEE double-extended).
59  *      2. We get by with floating-point arithmetic in a case that
60  *              Clinger missed -- when we're computing d * 10^n
61  *              for a small integer d and the integer n is not too
62  *              much larger than 22 (the maximum integer k for which
63  *              we can represent 10^k exactly), we may be able to
64  *              compute (d*10^k) * 10^(e-k) with just one roundoff.
65  *      3. Rather than a bit-at-a-time adjustment of the binary
66  *              result in the hard case, we use floating-point
67  *              arithmetic to determine the adjustment to within
68  *              one bit; only in really hard cases do we need to
69  *              compute a second residual.
70  *      4. Because of 3., we don't need a large table of powers of 10
71  *              for ten-to-e (just some small tables, e.g. of 10^k
72  *              for 0 <= k <= 22).
73  */
74
75 /*
76  * #define IEEE_8087 for IEEE-arithmetic machines where the least
77  *      significant byte has the lowest address.
78  * #define IEEE_MC68k for IEEE-arithmetic machines where the most
79  *      significant byte has the lowest address.
80  * #define Long int on machines with 32-bit ints and 64-bit longs.
81  * #define IBM for IBM mainframe-style floating-point arithmetic.
82  * #define VAX for VAX-style floating-point arithmetic (D_floating).
83  * #define No_leftright to omit left-right logic in fast floating-point
84  *      computation of dtoa.
85  * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
86  *      and strtod and dtoa should round accordingly.
87  * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
88  *      and Honor_FLT_ROUNDS is not #defined.
89  * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
90  *      that use extended-precision instructions to compute rounded
91  *      products and quotients) with IBM.
92  * #define ROUND_BIASED for IEEE-format with biased rounding.
93  * #define Inaccurate_Divide for IEEE-format with correctly rounded
94  *      products but inaccurate quotients, e.g., for Intel i860.
95  * #define NO_LONG_LONG on machines that do not have a "long long"
96  *      integer type (of >= 64 bits).  On such machines, you can
97  *      #define Just_16 to store 16 bits per 32-bit Long when doing
98  *      high-precision integer arithmetic.  Whether this speeds things
99  *      up or slows things down depends on the machine and the number
100  *      being converted.  If long long is available and the name is
101  *      something other than "long long", #define Llong to be the name,
102  *      and if "unsigned Llong" does not work as an unsigned version of
103  *      Llong, #define #ULLong to be the corresponding unsigned type.
104  * #define KR_headers for old-style C function headers.
105  * #define Bad_float_h if your system lacks a float.h or if it does not
106  *      define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
107  *      FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
108  * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
109  *      if memory is available and otherwise does something you deem
110  *      appropriate.  If MALLOC is undefined, malloc will be invoked
111  *      directly -- and assumed always to succeed.
112  * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
113  *      memory allocations from a private pool of memory when possible.
114  *      When used, the private pool is PRIVATE_MEM bytes long:  2304 bytes,
115  *      unless #defined to be a different length.  This default length
116  *      suffices to get rid of MALLOC calls except for unusual cases,
117  *      such as decimal-to-binary conversion of a very long string of
118  *      digits.  The longest string dtoa can return is about 751 bytes
119  *      long.  For conversions by strtod of strings of 800 digits and
120  *      all dtoa conversions in single-threaded executions with 8-byte
121  *      pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
122  *      pointers, PRIVATE_MEM >= 7112 appears adequate.
123  * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
124  *      Infinity and NaN (case insensitively).  On some systems (e.g.,
125  *      some HP systems), it may be necessary to #define NAN_WORD0
126  *      appropriately -- to the most significant word of a quiet NaN.
127  *      (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
128  *      When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
129  *      strtod also accepts (case insensitively) strings of the form
130  *      NaN(x), where x is a string of hexadecimal digits and spaces;
131  *      if there is only one string of hexadecimal digits, it is taken
132  *      for the 52 fraction bits of the resulting NaN; if there are two
133  *      or more strings of hex digits, the first is for the high 20 bits,
134  *      the second and subsequent for the low 32 bits, with intervening
135  *      white space ignored; but if this results in none of the 52
136  *      fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
137  *      and NAN_WORD1 are used instead.
138  * #define MULTIPLE_THREADS if the system offers preemptively scheduled
139  *      multiple threads.  In this case, you must provide (or suitably
140  *      #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
141  *      by FREE_DTOA_LOCK(n) for n = 0 or 1.  (The second lock, accessed
142  *      in pow5mult, ensures lazy evaluation of only one copy of high
143  *      powers of 5; omitting this lock would introduce a small
144  *      probability of wasting memory, but would otherwise be harmless.)
145  *      You must also invoke freedtoa(s) to free the value s returned by
146  *      dtoa.  You may do so whether or not MULTIPLE_THREADS is #defined.
147  * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
148  *      avoids underflows on inputs whose result does not underflow.
149  *      If you #define NO_IEEE_Scale on a machine that uses IEEE-format
150  *      floating-point numbers and flushes underflows to zero rather
151  *      than implementing gradual underflow, then you must also #define
152  *      Sudden_Underflow.
153  * #define YES_ALIAS to permit aliasing certain double values with
154  *      arrays of ULongs.  This leads to slightly better code with
155  *      some compilers and was always used prior to 19990916, but it
156  *      is not strictly legal and can cause trouble with aggressively
157  *      optimizing compilers (e.g., gcc 2.95.1 under -O2).
158  * #define USE_LOCALE to use the current locale's decimal_point value.
159  * #define SET_INEXACT if IEEE arithmetic is being used and extra
160  *      computation should be done to set the inexact flag when the
161  *      result is inexact and avoid setting inexact when the result
162  *      is exact.  In this case, dtoa.c must be compiled in
163  *      an environment, perhaps provided by #include "dtoa.c" in a
164  *      suitable wrapper, that defines two functions,
165  *              int get_inexact(void);
166  *              void clear_inexact(void);
167  *      such that get_inexact() returns a nonzero value if the
168  *      inexact bit is already set, and clear_inexact() sets the
169  *      inexact bit to 0.  When SET_INEXACT is #defined, strtod
170  *      also does extra computations to set the underflow and overflow
171  *      flags when appropriate (i.e., when the result is tiny and
172  *      inexact or when it is a numeric value rounded to +-infinity).
173  * #define NO_ERRNO if strtod should not assign errno = ERANGE when
174  *      the result overflows to +-Infinity or underflows to 0.
175  */
176 #if defined(TARGET_X86) || defined(mips) && defined(MIPSEL) || defined (__arm__) || defined(__aarch64__)
177
178 #   define IEEE_8087
179
180 #elif defined(TARGET_AMD64) || defined(__alpha__)
181
182 #   define IEEE_8087
183
184 #elif defined(__ia64)
185
186 #   ifdef __hpux
187 #       define IEEE_MC68k
188 #   else
189 #       define IEEE_8087
190 #   endif
191
192 #elif defined(__hppa)
193
194 #   define IEEE_MC68k
195
196 #else
197 #define IEEE_MC68k
198 #endif
199
200 #define Long gint32
201 #define ULong guint32
202
203 #ifdef DEBUG
204 #include "stdio.h"
205 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
206 #endif
207
208 #include "stdlib.h"
209 #include "string.h"
210
211 #undef USE_LOCALE
212 #ifdef USE_LOCALE
213 #include "locale.h"
214 #endif
215
216 #ifdef MALLOC
217 #ifdef KR_headers
218 extern char *MALLOC();
219 #else
220 extern void *MALLOC(size_t);
221 #endif
222 #else
223 #define MALLOC malloc
224 #endif
225
226 #define Omit_Private_Memory
227 #ifndef Omit_Private_Memory
228 #ifndef PRIVATE_MEM
229 #define PRIVATE_MEM 2304
230 #endif
231 #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
232 static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
233 #endif
234
235 #undef IEEE_Arith
236 #undef Avoid_Underflow
237 #ifdef IEEE_MC68k
238 #define IEEE_Arith
239 #endif
240 #ifdef IEEE_8087
241 #define IEEE_Arith
242 #endif
243
244 #include "errno.h"
245
246 #ifdef Bad_float_h
247
248 #ifdef IEEE_Arith
249 #define DBL_DIG 15
250 #define DBL_MAX_10_EXP 308
251 #define DBL_MAX_EXP 1024
252 #define FLT_RADIX 2
253 #endif /*IEEE_Arith*/
254
255 #ifdef IBM
256 #define DBL_DIG 16
257 #define DBL_MAX_10_EXP 75
258 #define DBL_MAX_EXP 63
259 #define FLT_RADIX 16
260 #define DBL_MAX 7.2370055773322621e+75
261 #endif
262
263 #ifdef VAX
264 #define DBL_DIG 16
265 #define DBL_MAX_10_EXP 38
266 #define DBL_MAX_EXP 127
267 #define FLT_RADIX 2
268 #define DBL_MAX 1.7014118346046923e+38
269 #endif
270
271 #ifndef LONG_MAX
272 #define LONG_MAX 2147483647
273 #endif
274
275 #else /* ifndef Bad_float_h */
276 #include "float.h"
277 #endif /* Bad_float_h */
278
279 #ifndef __MATH_H__
280 #include "math.h"
281 #endif
282
283 #ifdef __cplusplus
284 extern "C" {
285 #endif
286
287 #ifndef CONST
288 #ifdef KR_headers
289 #define CONST /* blank */
290 #else
291 #define CONST const
292 #endif
293 #endif
294
295 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
296 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
297 #endif
298
299 typedef union { double d; ULong L[2]; } U;
300
301 #ifdef YES_ALIAS
302 #define dval(x) x
303 #ifdef IEEE_8087
304 #define word0(x) ((ULong *)&x)[1]
305 #define word1(x) ((ULong *)&x)[0]
306 #else
307 #define word0(x) ((ULong *)&x)[0]
308 #define word1(x) ((ULong *)&x)[1]
309 #endif
310 #else
311 #ifdef IEEE_8087
312 #define word0(x) ((U*)&x)->L[1]
313 #define word1(x) ((U*)&x)->L[0]
314 #else
315 #define word0(x) ((U*)&x)->L[0]
316 #define word1(x) ((U*)&x)->L[1]
317 #endif
318 #define dval(x) ((U*)&x)->d
319 #endif
320
321 /* The following definition of Storeinc is appropriate for MIPS processors.
322  * An alternative that might be better on some machines is
323  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
324  */
325 #if defined(IEEE_8087) + defined(VAX)
326 #define Storeinc(a,b,c) do { (((unsigned short *)a)[1] = (unsigned short)b, \
327 ((unsigned short *)a)[0] = (unsigned short)c, a++) } while (0)
328 #else
329 #define Storeinc(a,b,c) do { (((unsigned short *)a)[0] = (unsigned short)b, \
330 ((unsigned short *)a)[1] = (unsigned short)c, a++) } while (0)
331 #endif
332
333 /* #define P DBL_MANT_DIG */
334 /* Ten_pmax = floor(P*log(2)/log(5)) */
335 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
336 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
337 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
338
339 #ifdef IEEE_Arith
340 #define Exp_shift  20
341 #define Exp_shift1 20
342 #define Exp_msk1    0x100000
343 #define Exp_msk11   0x100000
344 #define Exp_mask  0x7ff00000
345 #define P 53
346 #define Bias 1023
347 #define Emin (-1022)
348 #define Exp_1  0x3ff00000
349 #define Exp_11 0x3ff00000
350 #define Ebits 11
351 #define Frac_mask  0xfffff
352 #define Frac_mask1 0xfffff
353 #define Ten_pmax 22
354 #define Bletch 0x10
355 #define Bndry_mask  0xfffff
356 #define Bndry_mask1 0xfffff
357 #define LSB 1
358 #define Sign_bit 0x80000000
359 #define Log2P 1
360 #define Tiny0 0
361 #define Tiny1 1
362 #define Quick_max 14
363 #define Int_max 14
364 #ifndef NO_IEEE_Scale
365 #define Avoid_Underflow
366 #ifdef Flush_Denorm     /* debugging option */
367 #undef Sudden_Underflow
368 #endif
369 #endif
370
371 #ifndef Flt_Rounds
372 #ifdef FLT_ROUNDS
373 #define Flt_Rounds FLT_ROUNDS
374 #else
375 #define Flt_Rounds 1
376 #endif
377 #endif /*Flt_Rounds*/
378
379 #ifdef Honor_FLT_ROUNDS
380 #define Rounding rounding
381 #undef Check_FLT_ROUNDS
382 #define Check_FLT_ROUNDS
383 #else
384 #define Rounding Flt_Rounds
385 #endif
386
387 #else /* ifndef IEEE_Arith */
388 #undef Check_FLT_ROUNDS
389 #undef Honor_FLT_ROUNDS
390 #undef SET_INEXACT
391 #undef  Sudden_Underflow
392 #define Sudden_Underflow
393 #ifdef IBM
394 #undef Flt_Rounds
395 #define Flt_Rounds 0
396 #define Exp_shift  24
397 #define Exp_shift1 24
398 #define Exp_msk1   0x1000000
399 #define Exp_msk11  0x1000000
400 #define Exp_mask  0x7f000000
401 #define P 14
402 #define Bias 65
403 #define Exp_1  0x41000000
404 #define Exp_11 0x41000000
405 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
406 #define Frac_mask  0xffffff
407 #define Frac_mask1 0xffffff
408 #define Bletch 4
409 #define Ten_pmax 22
410 #define Bndry_mask  0xefffff
411 #define Bndry_mask1 0xffffff
412 #define LSB 1
413 #define Sign_bit 0x80000000
414 #define Log2P 4
415 #define Tiny0 0x100000
416 #define Tiny1 0
417 #define Quick_max 14
418 #define Int_max 15
419 #else /* VAX */
420 #undef Flt_Rounds
421 #define Flt_Rounds 1
422 #define Exp_shift  23
423 #define Exp_shift1 7
424 #define Exp_msk1    0x80
425 #define Exp_msk11   0x800000
426 #define Exp_mask  0x7f80
427 #define P 56
428 #define Bias 129
429 #define Exp_1  0x40800000
430 #define Exp_11 0x4080
431 #define Ebits 8
432 #define Frac_mask  0x7fffff
433 #define Frac_mask1 0xffff007f
434 #define Ten_pmax 24
435 #define Bletch 2
436 #define Bndry_mask  0xffff007f
437 #define Bndry_mask1 0xffff007f
438 #define LSB 0x10000
439 #define Sign_bit 0x8000
440 #define Log2P 1
441 #define Tiny0 0x80
442 #define Tiny1 0
443 #define Quick_max 15
444 #define Int_max 15
445 #endif /* IBM, VAX */
446 #endif /* IEEE_Arith */
447
448 #ifndef IEEE_Arith
449 #define ROUND_BIASED
450 #endif
451
452 #ifdef RND_PRODQUOT
453 #define rounded_product(a,b) a = rnd_prod(a, b)
454 #define rounded_quotient(a,b) a = rnd_quot(a, b)
455 #ifdef KR_headers
456 extern double rnd_prod(), rnd_quot();
457 #else
458 extern double rnd_prod(double, double), rnd_quot(double, double);
459 #endif
460 #else
461 #define rounded_product(a,b) a *= b
462 #define rounded_quotient(a,b) a /= b
463 #endif
464
465 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
466 #define Big1 0xffffffff
467
468 #ifndef Pack_32
469 #define Pack_32
470 #endif
471
472 #ifdef KR_headers
473 #define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
474 #else
475 #define FFFFFFFF 0xffffffffUL
476 #endif
477
478 #ifdef NO_LONG_LONG
479 #undef ULLong
480 #ifdef Just_16
481 #undef Pack_32
482 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
483  * This makes some inner loops simpler and sometimes saves work
484  * during multiplications, but it often seems to make things slightly
485  * slower.  Hence the default is now to store 32 bits per Long.
486  */
487 #endif
488 #else   /* long long available */
489 #ifndef Llong
490 #define Llong long long
491 #endif
492 #ifndef ULLong
493 #define ULLong unsigned Llong
494 #endif
495 #endif /* NO_LONG_LONG */
496
497 #ifndef MULTIPLE_THREADS
498 #define ACQUIRE_DTOA_LOCK(n)    /*nothing*/
499 #define FREE_DTOA_LOCK(n)       /*nothing*/
500 #endif
501
502 #define Kmax 15
503
504 #ifdef __cplusplus
505 extern "C" double strtod(const char *s00, char **se);
506 extern "C" char *dtoa(double d, int mode, int ndigits,
507                         int *decpt, int *sign, char **rve);
508 #endif
509
510  struct
511 Bigint {
512         struct Bigint *next;
513         int k, maxwds, sign, wds;
514         ULong x[1];
515         };
516
517  typedef struct Bigint Bigint;
518
519  static Bigint *freelist[Kmax+1];
520
521  static Bigint *
522 Balloc
523 #ifdef KR_headers
524         (k) int k;
525 #else
526         (int k)
527 #endif
528 {
529         int x;
530         Bigint *rv;
531 #ifndef Omit_Private_Memory
532         unsigned int len;
533 #endif
534
535         ACQUIRE_DTOA_LOCK(0);
536         if ((rv = freelist[k])) {
537                 freelist[k] = rv->next;
538                 }
539         else {
540                 x = 1 << k;
541 #ifdef Omit_Private_Memory
542                 rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
543 #else
544                 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
545                         /sizeof(double);
546                 if (pmem_next - private_mem + len <= PRIVATE_mem) {
547                         rv = (Bigint*)pmem_next;
548                         pmem_next += len;
549                         }
550                 else
551                         rv = (Bigint*)MALLOC(len*sizeof(double));
552 #endif
553                 rv->k = k;
554                 rv->maxwds = x;
555                 }
556         FREE_DTOA_LOCK(0);
557         rv->sign = rv->wds = 0;
558         return rv;
559         }
560
561  static void
562 Bfree
563 #ifdef KR_headers
564         (v) Bigint *v;
565 #else
566         (Bigint *v)
567 #endif
568 {
569 #ifdef Omit_Private_Memory
570         free (v);
571 #else
572         if (v) {
573                 ACQUIRE_DTOA_LOCK(0);
574                 v->next = freelist[v->k];
575                 freelist[v->k] = v;
576                 FREE_DTOA_LOCK(0);
577         }
578 #endif
579 }
580
581 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
582 y->wds*sizeof(Long) + 2*sizeof(int))
583
584  static Bigint *
585 multadd
586 #ifdef KR_headers
587         (b, m, a) Bigint *b; int m, a;
588 #else
589         (Bigint *b, int m, int a)       /* multiply by m and add a */
590 #endif
591 {
592         int i, wds;
593 #ifdef ULLong
594         ULong *x;
595         ULLong carry, y;
596 #else
597         ULong carry, *x, y;
598 #ifdef Pack_32
599         ULong xi, z;
600 #endif
601 #endif
602         Bigint *b1;
603
604         wds = b->wds;
605         x = b->x;
606         i = 0;
607         carry = a;
608         do {
609 #ifdef ULLong
610                 y = *x * (ULLong)m + carry;
611                 carry = y >> 32;
612                 *x++ = y & FFFFFFFF;
613 #else
614 #ifdef Pack_32
615                 xi = *x;
616                 y = (xi & 0xffff) * m + carry;
617                 z = (xi >> 16) * m + (y >> 16);
618                 carry = z >> 16;
619                 *x++ = (z << 16) + (y & 0xffff);
620 #else
621                 y = *x * m + carry;
622                 carry = y >> 16;
623                 *x++ = y & 0xffff;
624 #endif
625 #endif
626                 }
627                 while(++i < wds);
628         if (carry) {
629                 if (wds >= b->maxwds) {
630                         b1 = Balloc(b->k+1);
631                         Bcopy(b1, b);
632                         Bfree(b);
633                         b = b1;
634                         }
635                 b->x[wds++] = carry;
636                 b->wds = wds;
637                 }
638         return b;
639         }
640
641  static Bigint *
642 s2b
643 #ifdef KR_headers
644         (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9;
645 #else
646         (CONST char *s, int nd0, int nd, ULong y9)
647 #endif
648 {
649         Bigint *b;
650         int i, k;
651         Long x, y;
652
653         x = (nd + 8) / 9;
654         for(k = 0, y = 1; x > y; y <<= 1, k++) ;
655 #ifdef Pack_32
656         b = Balloc(k);
657         b->x[0] = y9;
658         b->wds = 1;
659 #else
660         b = Balloc(k+1);
661         b->x[0] = y9 & 0xffff;
662         b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
663 #endif
664
665         i = 9;
666         if (9 < nd0) {
667                 s += 9;
668                 do b = multadd(b, 10, *s++ - '0');
669                         while(++i < nd0);
670                 s++;
671                 }
672         else
673                 s += 10;
674         for(; i < nd; i++)
675                 b = multadd(b, 10, *s++ - '0');
676         return b;
677         }
678
679  static int
680 hi0bits
681 #ifdef KR_headers
682         (x) register ULong x;
683 #else
684         (register ULong x)
685 #endif
686 {
687         register int k = 0;
688
689         if (!(x & 0xffff0000)) {
690                 k = 16;
691                 x <<= 16;
692                 }
693         if (!(x & 0xff000000)) {
694                 k += 8;
695                 x <<= 8;
696                 }
697         if (!(x & 0xf0000000)) {
698                 k += 4;
699                 x <<= 4;
700                 }
701         if (!(x & 0xc0000000)) {
702                 k += 2;
703                 x <<= 2;
704                 }
705         if (!(x & 0x80000000)) {
706                 k++;
707                 if (!(x & 0x40000000))
708                         return 32;
709                 }
710         return k;
711         }
712
713  static int
714 lo0bits
715 #ifdef KR_headers
716         (y) ULong *y;
717 #else
718         (ULong *y)
719 #endif
720 {
721         register int k;
722         register ULong x = *y;
723
724         if (x & 7) {
725                 if (x & 1)
726                         return 0;
727                 if (x & 2) {
728                         *y = x >> 1;
729                         return 1;
730                         }
731                 *y = x >> 2;
732                 return 2;
733                 }
734         k = 0;
735         if (!(x & 0xffff)) {
736                 k = 16;
737                 x >>= 16;
738                 }
739         if (!(x & 0xff)) {
740                 k += 8;
741                 x >>= 8;
742                 }
743         if (!(x & 0xf)) {
744                 k += 4;
745                 x >>= 4;
746                 }
747         if (!(x & 0x3)) {
748                 k += 2;
749                 x >>= 2;
750                 }
751         if (!(x & 1)) {
752                 k++;
753                 x >>= 1;
754                 if (!x)
755                         return 32;
756                 }
757         *y = x;
758         return k;
759         }
760
761  static Bigint *
762 i2b
763 #ifdef KR_headers
764         (i) int i;
765 #else
766         (int i)
767 #endif
768 {
769         Bigint *b;
770
771         b = Balloc(1);
772         b->x[0] = i;
773         b->wds = 1;
774         return b;
775         }
776
777  static Bigint *
778 mult
779 #ifdef KR_headers
780         (a, b) Bigint *a, *b;
781 #else
782         (Bigint *a, Bigint *b)
783 #endif
784 {
785         Bigint *c;
786         int k, wa, wb, wc;
787         ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
788         ULong y;
789 #ifdef ULLong
790         ULLong carry, z;
791 #else
792         ULong carry, z;
793 #ifdef Pack_32
794         ULong z2;
795 #endif
796 #endif
797
798         if (a->wds < b->wds) {
799                 c = a;
800                 a = b;
801                 b = c;
802                 }
803         k = a->k;
804         wa = a->wds;
805         wb = b->wds;
806         wc = wa + wb;
807         if (wc > a->maxwds)
808                 k++;
809         c = Balloc(k);
810         for(x = c->x, xa = x + wc; x < xa; x++)
811                 *x = 0;
812         xa = a->x;
813         xae = xa + wa;
814         xb = b->x;
815         xbe = xb + wb;
816         xc0 = c->x;
817 #ifdef ULLong
818         for(; xb < xbe; xc0++) {
819                 if ((y = *xb++)) {
820                         x = xa;
821                         xc = xc0;
822                         carry = 0;
823                         do {
824                                 z = *x++ * (ULLong)y + *xc + carry;
825                                 carry = z >> 32;
826                                 *xc++ = z & FFFFFFFF;
827                                 }
828                                 while(x < xae);
829                         *xc = carry;
830                         }
831                 }
832 #else
833 #ifdef Pack_32
834         for(; xb < xbe; xb++, xc0++) {
835                 if (y = *xb & 0xffff) {
836                         x = xa;
837                         xc = xc0;
838                         carry = 0;
839                         do {
840                                 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
841                                 carry = z >> 16;
842                                 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
843                                 carry = z2 >> 16;
844                                 Storeinc(xc, z2, z);
845                                 }
846                                 while(x < xae);
847                         *xc = carry;
848                         }
849                 if (y = *xb >> 16) {
850                         x = xa;
851                         xc = xc0;
852                         carry = 0;
853                         z2 = *xc;
854                         do {
855                                 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
856                                 carry = z >> 16;
857                                 Storeinc(xc, z, z2);
858                                 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
859                                 carry = z2 >> 16;
860                                 }
861                                 while(x < xae);
862                         *xc = z2;
863                         }
864                 }
865 #else
866         for(; xb < xbe; xc0++) {
867                 if (y = *xb++) {
868                         x = xa;
869                         xc = xc0;
870                         carry = 0;
871                         do {
872                                 z = *x++ * y + *xc + carry;
873                                 carry = z >> 16;
874                                 *xc++ = z & 0xffff;
875                                 }
876                                 while(x < xae);
877                         *xc = carry;
878                         }
879                 }
880 #endif
881 #endif
882         for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
883         c->wds = wc;
884         return c;
885         }
886
887  static Bigint *p5s;
888
889  static Bigint *
890 pow5mult
891 #ifdef KR_headers
892         (b, k) Bigint *b; int k;
893 #else
894         (Bigint *b, int k)
895 #endif
896 {
897         Bigint *b1, *p5, *p51;
898         int i;
899         static int p05[3] = { 5, 25, 125 };
900
901         if ((i = k & 3))
902                 b = multadd(b, p05[i-1], 0);
903
904         if (!(k >>= 2))
905                 return b;
906         if (!(p5 = p5s)) {
907                 /* first time */
908 #ifdef MULTIPLE_THREADS
909                 ACQUIRE_DTOA_LOCK(1);
910                 if (!(p5 = p5s)) {
911                         p5 = p5s = i2b(625);
912                         p5->next = 0;
913                         }
914                 FREE_DTOA_LOCK(1);
915 #else
916                 p5 = p5s = i2b(625);
917                 p5->next = 0;
918 #endif
919                 }
920         for(;;) {
921                 if (k & 1) {
922                         b1 = mult(b, p5);
923                         Bfree(b);
924                         b = b1;
925                         }
926                 if (!(k >>= 1))
927                         break;
928                 if (!(p51 = p5->next)) {
929 #ifdef MULTIPLE_THREADS
930                         ACQUIRE_DTOA_LOCK(1);
931                         if (!(p51 = p5->next)) {
932                                 p51 = p5->next = mult(p5,p5);
933                                 p51->next = 0;
934                                 }
935                         FREE_DTOA_LOCK(1);
936 #else
937                         p51 = p5->next = mult(p5,p5);
938                         p51->next = 0;
939 #endif
940                         }
941                 p5 = p51;
942                 }
943         return b;
944         }
945
946  static Bigint *
947 lshift
948 #ifdef KR_headers
949         (b, k) Bigint *b; int k;
950 #else
951         (Bigint *b, int k)
952 #endif
953 {
954         int i, k1, n, n1;
955         Bigint *b1;
956         ULong *x, *x1, *xe, z;
957
958 #ifdef Pack_32
959         n = k >> 5;
960 #else
961         n = k >> 4;
962 #endif
963         k1 = b->k;
964         n1 = n + b->wds + 1;
965         for(i = b->maxwds; n1 > i; i <<= 1)
966                 k1++;
967         b1 = Balloc(k1);
968         x1 = b1->x;
969         for(i = 0; i < n; i++)
970                 *x1++ = 0;
971         x = b->x;
972         xe = x + b->wds;
973 #ifdef Pack_32
974         if (k &= 0x1f) {
975                 k1 = 32 - k;
976                 z = 0;
977                 do {
978                         *x1++ = *x << k | z;
979                         z = *x++ >> k1;
980                         }
981                         while(x < xe);
982                 if ((*x1 = z))
983                         ++n1;
984                 }
985 #else
986         if (k &= 0xf) {
987                 k1 = 16 - k;
988                 z = 0;
989                 do {
990                         *x1++ = *x << k  & 0xffff | z;
991                         z = *x++ >> k1;
992                         }
993                         while(x < xe);
994                 if (*x1 = z)
995                         ++n1;
996                 }
997 #endif
998         else do
999                 *x1++ = *x++;
1000                 while(x < xe);
1001         b1->wds = n1 - 1;
1002         Bfree(b);
1003         return b1;
1004         }
1005
1006  static int
1007 cmp
1008 #ifdef KR_headers
1009         (a, b) Bigint *a, *b;
1010 #else
1011         (Bigint *a, Bigint *b)
1012 #endif
1013 {
1014         ULong *xa, *xa0, *xb, *xb0;
1015         int i, j;
1016
1017         i = a->wds;
1018         j = b->wds;
1019 #ifdef DEBUG
1020         if (i > 1 && !a->x[i-1])
1021                 Bug("cmp called with a->x[a->wds-1] == 0");
1022         if (j > 1 && !b->x[j-1])
1023                 Bug("cmp called with b->x[b->wds-1] == 0");
1024 #endif
1025         if (i -= j)
1026                 return i;
1027         xa0 = a->x;
1028         xa = xa0 + j;
1029         xb0 = b->x;
1030         xb = xb0 + j;
1031         for(;;) {
1032                 if (*--xa != *--xb)
1033                         return *xa < *xb ? -1 : 1;
1034                 if (xa <= xa0)
1035                         break;
1036                 }
1037         return 0;
1038         }
1039
1040  static Bigint *
1041 diff
1042 #ifdef KR_headers
1043         (a, b) Bigint *a, *b;
1044 #else
1045         (Bigint *a, Bigint *b)
1046 #endif
1047 {
1048         Bigint *c;
1049         int i, wa, wb;
1050         ULong *xa, *xae, *xb, *xbe, *xc;
1051 #ifdef ULLong
1052         ULLong borrow, y;
1053 #else
1054         ULong borrow, y;
1055 #ifdef Pack_32
1056         ULong z;
1057 #endif
1058 #endif
1059
1060         i = cmp(a,b);
1061         if (!i) {
1062                 c = Balloc(0);
1063                 c->wds = 1;
1064                 c->x[0] = 0;
1065                 return c;
1066                 }
1067         if (i < 0) {
1068                 c = a;
1069                 a = b;
1070                 b = c;
1071                 i = 1;
1072                 }
1073         else
1074                 i = 0;
1075         c = Balloc(a->k);
1076         c->sign = i;
1077         wa = a->wds;
1078         xa = a->x;
1079         xae = xa + wa;
1080         wb = b->wds;
1081         xb = b->x;
1082         xbe = xb + wb;
1083         xc = c->x;
1084         borrow = 0;
1085 #ifdef ULLong
1086         do {
1087                 y = (ULLong)*xa++ - *xb++ - borrow;
1088                 borrow = y >> 32 & (ULong)1;
1089                 *xc++ = y & FFFFFFFF;
1090                 }
1091                 while(xb < xbe);
1092         while(xa < xae) {
1093                 y = *xa++ - borrow;
1094                 borrow = y >> 32 & (ULong)1;
1095                 *xc++ = y & FFFFFFFF;
1096                 }
1097 #else
1098 #ifdef Pack_32
1099         do {
1100                 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
1101                 borrow = (y & 0x10000) >> 16;
1102                 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
1103                 borrow = (z & 0x10000) >> 16;
1104                 Storeinc(xc, z, y);
1105                 }
1106                 while(xb < xbe);
1107         while(xa < xae) {
1108                 y = (*xa & 0xffff) - borrow;
1109                 borrow = (y & 0x10000) >> 16;
1110                 z = (*xa++ >> 16) - borrow;
1111                 borrow = (z & 0x10000) >> 16;
1112                 Storeinc(xc, z, y);
1113                 }
1114 #else
1115         do {
1116                 y = *xa++ - *xb++ - borrow;
1117                 borrow = (y & 0x10000) >> 16;
1118                 *xc++ = y & 0xffff;
1119                 }
1120                 while(xb < xbe);
1121         while(xa < xae) {
1122                 y = *xa++ - borrow;
1123                 borrow = (y & 0x10000) >> 16;
1124                 *xc++ = y & 0xffff;
1125                 }
1126 #endif
1127 #endif
1128         while(!*--xc)
1129                 wa--;
1130         c->wds = wa;
1131         return c;
1132         }
1133
1134  static double
1135 ulp
1136 #ifdef KR_headers
1137         (x) double x;
1138 #else
1139         (double x)
1140 #endif
1141 {
1142         register Long L;
1143         double a;
1144
1145         L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1146 #ifndef Avoid_Underflow
1147 #ifndef Sudden_Underflow
1148         if (L > 0) {
1149 #endif
1150 #endif
1151 #ifdef IBM
1152                 L |= Exp_msk1 >> 4;
1153 #endif
1154                 word0(a) = L;
1155                 word1(a) = 0;
1156 #ifndef Avoid_Underflow
1157 #ifndef Sudden_Underflow
1158                 }
1159         else {
1160                 L = -L >> Exp_shift;
1161                 if (L < Exp_shift) {
1162                         word0(a) = 0x80000 >> L;
1163                         word1(a) = 0;
1164                         }
1165                 else {
1166                         word0(a) = 0;
1167                         L -= Exp_shift;
1168                         word1(a) = L >= 31 ? 1 : 1 << 31 - L;
1169                         }
1170                 }
1171 #endif
1172 #endif
1173         return dval(a);
1174         }
1175
1176  static double
1177 b2d
1178 #ifdef KR_headers
1179         (a, e) Bigint *a; int *e;
1180 #else
1181         (Bigint *a, int *e)
1182 #endif
1183 {
1184         ULong *xa, *xa0, w, y, z;
1185         int k;
1186         double d;
1187 #ifdef VAX
1188         ULong d0, d1;
1189 #else
1190 #define d0 word0(d)
1191 #define d1 word1(d)
1192 #endif
1193
1194         xa0 = a->x;
1195         xa = xa0 + a->wds;
1196         y = *--xa;
1197 #ifdef DEBUG
1198         if (!y) Bug("zero y in b2d");
1199 #endif
1200         k = hi0bits(y);
1201         *e = 32 - k;
1202 #ifdef Pack_32
1203         if (k < Ebits) {
1204                 d0 = Exp_1 | (y >> (Ebits - k));
1205                 w = xa > xa0 ? *--xa : 0;
1206                 d1 = y << ((32-Ebits) + k) | (w >> (Ebits - k));
1207                 goto ret_d;
1208                 }
1209         z = xa > xa0 ? *--xa : 0;
1210         if (k -= Ebits) {
1211                 d0 = Exp_1 | y << k | (z >> (32 - k));
1212                 y = xa > xa0 ? *--xa : 0;
1213                 d1 = z << k | (y >> (32 - k));
1214                 }
1215         else {
1216                 d0 = Exp_1 | y;
1217                 d1 = z;
1218                 }
1219 #else
1220         if (k < Ebits + 16) {
1221                 z = xa > xa0 ? *--xa : 0;
1222                 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1223                 w = xa > xa0 ? *--xa : 0;
1224                 y = xa > xa0 ? *--xa : 0;
1225                 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1226                 goto ret_d;
1227                 }
1228         z = xa > xa0 ? *--xa : 0;
1229         w = xa > xa0 ? *--xa : 0;
1230         k -= Ebits + 16;
1231         d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1232         y = xa > xa0 ? *--xa : 0;
1233         d1 = w << k + 16 | y << k;
1234 #endif
1235  ret_d:
1236 #ifdef VAX
1237         word0(d) = d0 >> 16 | d0 << 16;
1238         word1(d) = d1 >> 16 | d1 << 16;
1239 #else
1240 #undef d0
1241 #undef d1
1242 #endif
1243         return dval(d);
1244         }
1245
1246  static Bigint *
1247 d2b
1248 #ifdef KR_headers
1249         (d, e, bits) double d; int *e, *bits;
1250 #else
1251         (double d, int *e, int *bits)
1252 #endif
1253 {
1254         Bigint *b;
1255         int de, k;
1256         ULong *x, y, z;
1257 #ifndef Sudden_Underflow
1258         int i;
1259 #endif
1260 #ifdef VAX
1261         ULong d0, d1;
1262         d0 = word0(d) >> 16 | word0(d) << 16;
1263         d1 = word1(d) >> 16 | word1(d) << 16;
1264 #else
1265 #define d0 word0(d)
1266 #define d1 word1(d)
1267 #endif
1268
1269 #ifdef Pack_32
1270         b = Balloc(1);
1271 #else
1272         b = Balloc(2);
1273 #endif
1274         x = b->x;
1275
1276         z = d0 & Frac_mask;
1277         d0 &= 0x7fffffff;       /* clear sign bit, which we ignore */
1278 #ifdef Sudden_Underflow
1279         de = (int)(d0 >> Exp_shift);
1280 #ifndef IBM
1281         z |= Exp_msk11;
1282 #endif
1283 #else
1284         if ((de = (int)(d0 >> Exp_shift)))
1285                 z |= Exp_msk1;
1286 #endif
1287 #ifdef Pack_32
1288         if ((y = d1)) {
1289                 if ((k = lo0bits(&y))) {
1290                         x[0] = y | (z << (32 - k));
1291                         z >>= k;
1292                         }
1293                 else
1294                         x[0] = y;
1295 #ifndef Sudden_Underflow
1296                 i =
1297 #endif
1298                     b->wds = (x[1] = z) ? 2 : 1;
1299                 }
1300         else {
1301 #ifdef DEBUG
1302                 if (!z)
1303                         Bug("Zero passed to d2b");
1304 #endif
1305                 k = lo0bits(&z);
1306                 x[0] = z;
1307 #ifndef Sudden_Underflow
1308                 i =
1309 #endif
1310                     b->wds = 1;
1311                 k += 32;
1312                 }
1313 #else
1314         if (y = d1) {
1315                 if (k = lo0bits(&y))
1316                         if (k >= 16) {
1317                                 x[0] = y | z << 32 - k & 0xffff;
1318                                 x[1] = z >> k - 16 & 0xffff;
1319                                 x[2] = z >> k;
1320                                 i = 2;
1321                                 }
1322                         else {
1323                                 x[0] = y & 0xffff;
1324                                 x[1] = y >> 16 | z << 16 - k & 0xffff;
1325                                 x[2] = z >> k & 0xffff;
1326                                 x[3] = z >> k+16;
1327                                 i = 3;
1328                                 }
1329                 else {
1330                         x[0] = y & 0xffff;
1331                         x[1] = y >> 16;
1332                         x[2] = z & 0xffff;
1333                         x[3] = z >> 16;
1334                         i = 3;
1335                         }
1336                 }
1337         else {
1338 #ifdef DEBUG
1339                 if (!z)
1340                         Bug("Zero passed to d2b");
1341 #endif
1342                 k = lo0bits(&z);
1343                 if (k >= 16) {
1344                         x[0] = z;
1345                         i = 0;
1346                         }
1347                 else {
1348                         x[0] = z & 0xffff;
1349                         x[1] = z >> 16;
1350                         i = 1;
1351                         }
1352                 k += 32;
1353                 }
1354         while(!x[i])
1355                 --i;
1356         b->wds = i + 1;
1357 #endif
1358 #ifndef Sudden_Underflow
1359         if (de) {
1360 #endif
1361 #ifdef IBM
1362                 *e = (de - Bias - (P-1) << 2) + k;
1363                 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1364 #else
1365                 *e = de - Bias - (P-1) + k;
1366                 *bits = P - k;
1367 #endif
1368 #ifndef Sudden_Underflow
1369                 }
1370         else {
1371                 *e = de - Bias - (P-1) + 1 + k;
1372 #ifdef Pack_32
1373                 *bits = 32*i - hi0bits(x[i-1]);
1374 #else
1375                 *bits = (i+2)*16 - hi0bits(x[i]);
1376 #endif
1377                 }
1378 #endif
1379         return b;
1380         }
1381 #undef d0
1382 #undef d1
1383
1384  static double
1385 ratio
1386 #ifdef KR_headers
1387         (a, b) Bigint *a, *b;
1388 #else
1389         (Bigint *a, Bigint *b)
1390 #endif
1391 {
1392         double da, db;
1393         int k, ka, kb;
1394
1395         dval(da) = b2d(a, &ka);
1396         dval(db) = b2d(b, &kb);
1397 #ifdef Pack_32
1398         k = ka - kb + 32*(a->wds - b->wds);
1399 #else
1400         k = ka - kb + 16*(a->wds - b->wds);
1401 #endif
1402 #ifdef IBM
1403         if (k > 0) {
1404                 word0(da) += (k >> 2)*Exp_msk1;
1405                 if (k &= 3)
1406                         dval(da) *= 1 << k;
1407                 }
1408         else {
1409                 k = -k;
1410                 word0(db) += (k >> 2)*Exp_msk1;
1411                 if (k &= 3)
1412                         dval(db) *= 1 << k;
1413                 }
1414 #else
1415         if (k > 0)
1416                 word0(da) += k*Exp_msk1;
1417         else {
1418                 k = -k;
1419                 word0(db) += k*Exp_msk1;
1420                 }
1421 #endif
1422         return dval(da) / dval(db);
1423         }
1424
1425  static CONST double
1426 tens[] = {
1427                 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1428                 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1429                 1e20, 1e21, 1e22
1430 #ifdef VAX
1431                 , 1e23, 1e24
1432 #endif
1433                 };
1434
1435  static CONST double
1436 #ifdef IEEE_Arith
1437 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1438 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1439 #ifdef Avoid_Underflow
1440                 9007199254740992.*9007199254740992.e-256
1441                 /* = 2^106 * 1e-53 */
1442 #else
1443                 1e-256
1444 #endif
1445                 };
1446 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1447 /* flag unnecessarily.  It leads to a song and dance at the end of strtod. */
1448 #define Scale_Bit 0x10
1449 #define n_bigtens 5
1450 #else
1451 #ifdef IBM
1452 bigtens[] = { 1e16, 1e32, 1e64 };
1453 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1454 #define n_bigtens 3
1455 #else
1456 bigtens[] = { 1e16, 1e32 };
1457 static CONST double tinytens[] = { 1e-16, 1e-32 };
1458 #define n_bigtens 2
1459 #endif
1460 #endif
1461
1462 #ifndef IEEE_Arith
1463 #undef INFNAN_CHECK
1464 #endif
1465
1466 #ifdef INFNAN_CHECK
1467
1468 #ifndef NAN_WORD0
1469 #define NAN_WORD0 0x7ff80000
1470 #endif
1471
1472 #ifndef NAN_WORD1
1473 #define NAN_WORD1 0
1474 #endif
1475
1476  static int
1477 match
1478 #ifdef KR_headers
1479         (sp, t) char **sp, *t;
1480 #else
1481         (CONST char **sp, char *t)
1482 #endif
1483 {
1484         int c, d;
1485         CONST char *s = *sp;
1486
1487         while(d = *t++) {
1488                 if ((c = *++s) >= 'A' && c <= 'Z')
1489                         c += 'a' - 'A';
1490                 if (c != d)
1491                         return 0;
1492                 }
1493         *sp = s + 1;
1494         return 1;
1495         }
1496
1497 #ifndef No_Hex_NaN
1498  static void
1499 hexnan
1500 #ifdef KR_headers
1501         (rvp, sp) double *rvp; CONST char **sp;
1502 #else
1503         (double *rvp, CONST char **sp)
1504 #endif
1505 {
1506         ULong c, x[2];
1507         CONST char *s;
1508         int havedig, udx0, xshift;
1509
1510         x[0] = x[1] = 0;
1511         havedig = xshift = 0;
1512         udx0 = 1;
1513         s = *sp;
1514         while(c = *(CONST unsigned char*)++s) {
1515                 if (c >= '0' && c <= '9')
1516                         c -= '0';
1517                 else if (c >= 'a' && c <= 'f')
1518                         c += 10 - 'a';
1519                 else if (c >= 'A' && c <= 'F')
1520                         c += 10 - 'A';
1521                 else if (c <= ' ') {
1522                         if (udx0 && havedig) {
1523                                 udx0 = 0;
1524                                 xshift = 1;
1525                                 }
1526                         continue;
1527                         }
1528                 else if (/*(*/ c == ')' && havedig) {
1529                         *sp = s + 1;
1530                         break;
1531                         }
1532                 else
1533                         return; /* invalid form: don't change *sp */
1534                 havedig = 1;
1535                 if (xshift) {
1536                         xshift = 0;
1537                         x[0] = x[1];
1538                         x[1] = 0;
1539                         }
1540                 if (udx0)
1541                         x[0] = (x[0] << 4) | (x[1] >> 28);
1542                 x[1] = (x[1] << 4) | c;
1543                 }
1544         if ((x[0] &= 0xfffff) || x[1]) {
1545                 word0(*rvp) = Exp_mask | x[0];
1546                 word1(*rvp) = x[1];
1547                 }
1548         }
1549 #endif /*No_Hex_NaN*/
1550 #endif /* INFNAN_CHECK */
1551
1552         /*
1553          * LOCKING: This is not thread-safe, since the locking macros are defined as no-ops,
1554          * the caller should lock.
1555          */
1556
1557  double
1558 mono_strtod
1559 #ifdef KR_headers
1560         (s00, se) CONST char *s00; char **se;
1561 #else
1562         (CONST char *s00, char **se)
1563 #endif
1564 {
1565 #ifdef Avoid_Underflow
1566         int scale;
1567 #endif
1568         int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1569                  e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1570         CONST char *s, *s0, *s1;
1571         double aadj, aadj1, adj, rv, rv0;
1572         Long L;
1573         ULong y, z;
1574         Bigint *bb = NULL, *bb1, *bd = NULL, *bd0, *bs = NULL, *delta = NULL;
1575 #ifdef SET_INEXACT
1576         int inexact, oldinexact;
1577 #endif
1578 #ifdef Honor_FLT_ROUNDS
1579         int rounding;
1580 #endif
1581 #ifdef USE_LOCALE
1582         CONST char *s2;
1583 #endif
1584
1585         sign = nz0 = nz = 0;
1586         dval(rv) = 0.;
1587         for(s = s00;;s++) switch(*s) {
1588                 case '-':
1589                         sign = 1;
1590                         /* no break */
1591                 case '+':
1592                         if (*++s)
1593                                 goto break2;
1594                         /* no break */
1595                 case 0:
1596                         goto ret0;
1597                 case '\t':
1598                 case '\n':
1599                 case '\v':
1600                 case '\f':
1601                 case '\r':
1602                 case ' ':
1603                         continue;
1604                 default:
1605                         goto break2;
1606                 }
1607  break2:
1608         if (*s == '0') {
1609                 nz0 = 1;
1610                 while(*++s == '0') ;
1611                 if (!*s)
1612                         goto ret;
1613                 }
1614         s0 = s;
1615         y = z = 0;
1616         for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1617                 if (nd < 9)
1618                         y = 10*y + c - '0';
1619                 else if (nd < 16)
1620                         z = 10*z + c - '0';
1621         nd0 = nd;
1622 #ifdef USE_LOCALE
1623         s1 = localeconv()->decimal_point;
1624         if (c == *s1) {
1625                 c = '.';
1626                 if (*++s1) {
1627                         s2 = s;
1628                         for(;;) {
1629                                 if (*++s2 != *s1) {
1630                                         c = 0;
1631                                         break;
1632                                         }
1633                                 if (!*++s1) {
1634                                         s = s2;
1635                                         break;
1636                                         }
1637                                 }
1638                         }
1639                 }
1640 #endif
1641         if (c == '.') {
1642                 c = *++s;
1643                 if (!nd) {
1644                         for(; c == '0'; c = *++s)
1645                                 nz++;
1646                         if (c > '0' && c <= '9') {
1647                                 s0 = s;
1648                                 nf += nz;
1649                                 nz = 0;
1650                                 goto have_dig;
1651                                 }
1652                         goto dig_done;
1653                         }
1654                 for(; c >= '0' && c <= '9'; c = *++s) {
1655  have_dig:
1656                         nz++;
1657                         if (c -= '0') {
1658                                 nf += nz;
1659                                 for(i = 1; i < nz; i++)
1660                                         if (nd++ < 9)
1661                                                 y *= 10;
1662                                         else if (nd <= DBL_DIG + 1)
1663                                                 z *= 10;
1664                                 if (nd++ < 9)
1665                                         y = 10*y + c;
1666                                 else if (nd <= DBL_DIG + 1)
1667                                         z = 10*z + c;
1668                                 nz = 0;
1669                                 }
1670                         }
1671                 }
1672  dig_done:
1673         e = 0;
1674         if (c == 'e' || c == 'E') {
1675                 if (!nd && !nz && !nz0) {
1676                         goto ret0;
1677                         }
1678                 s00 = s;
1679                 esign = 0;
1680                 switch(c = *++s) {
1681                         case '-':
1682                                 esign = 1;
1683                         case '+':
1684                                 c = *++s;
1685                         }
1686                 if (c >= '0' && c <= '9') {
1687                         while(c == '0')
1688                                 c = *++s;
1689                         if (c > '0' && c <= '9') {
1690                                 L = c - '0';
1691                                 s1 = s;
1692                                 while((c = *++s) >= '0' && c <= '9')
1693                                         L = 10*L + c - '0';
1694                                 if (s - s1 > 8 || L > 19999)
1695                                         /* Avoid confusion from exponents
1696                                          * so large that e might overflow.
1697                                          */
1698                                         e = 19999; /* safe for 16 bit ints */
1699                                 else
1700                                         e = (int)L;
1701                                 if (esign)
1702                                         e = -e;
1703                                 }
1704                         else
1705                                 e = 0;
1706                         }
1707                 else
1708                         s = s00;
1709                 }
1710         if (!nd) {
1711                 if (!nz && !nz0) {
1712 #ifdef INFNAN_CHECK
1713                         /* Check for Nan and Infinity */
1714                         switch(c) {
1715                           case 'i':
1716                           case 'I':
1717                                 if (match(&s,"nf")) {
1718                                         --s;
1719                                         if (!match(&s,"inity"))
1720                                                 ++s;
1721                                         word0(rv) = 0x7ff00000;
1722                                         word1(rv) = 0;
1723                                         goto ret;
1724                                         }
1725                                 break;
1726                           case 'n':
1727                           case 'N':
1728                                 if (match(&s, "an")) {
1729                                         word0(rv) = NAN_WORD0;
1730                                         word1(rv) = NAN_WORD1;
1731 #ifndef No_Hex_NaN
1732                                         if (*s == '(') /*)*/
1733                                                 hexnan(&rv, &s);
1734 #endif
1735                                         goto ret;
1736                                         }
1737                           }
1738 #endif /* INFNAN_CHECK */
1739  ret0:
1740                         s = s00;
1741                         sign = 0;
1742                         }
1743                 goto ret;
1744                 }
1745         e1 = e -= nf;
1746
1747         /* Now we have nd0 digits, starting at s0, followed by a
1748          * decimal point, followed by nd-nd0 digits.  The number we're
1749          * after is the integer represented by those digits times
1750          * 10**e */
1751
1752         if (!nd0)
1753                 nd0 = nd;
1754         k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1755         dval(rv) = y;
1756         if (k > 9) {
1757 #ifdef SET_INEXACT
1758                 if (k > DBL_DIG)
1759                         oldinexact = get_inexact();
1760 #endif
1761                 dval(rv) = tens[k - 9] * dval(rv) + z;
1762                 }
1763         bd0 = 0;
1764         if (nd <= DBL_DIG
1765 #ifndef RND_PRODQUOT
1766 #ifndef Honor_FLT_ROUNDS
1767                 && Flt_Rounds == 1
1768 #endif
1769 #endif
1770                         ) {
1771                 if (!e)
1772                         goto ret;
1773                 if (e > 0) {
1774                         if (e <= Ten_pmax) {
1775 #ifdef VAX
1776                                 goto vax_ovfl_check;
1777 #else
1778 #ifdef Honor_FLT_ROUNDS
1779                                 /* round correctly FLT_ROUNDS = 2 or 3 */
1780                                 if (sign) {
1781                                         rv = -rv;
1782                                         sign = 0;
1783                                         }
1784 #endif
1785                                 /* rv = */ rounded_product(dval(rv), tens[e]);
1786                                 goto ret;
1787 #endif
1788                                 }
1789                         i = DBL_DIG - nd;
1790                         if (e <= Ten_pmax + i) {
1791                                 /* A fancier test would sometimes let us do
1792                                  * this for larger i values.
1793                                  */
1794 #ifdef Honor_FLT_ROUNDS
1795                                 /* round correctly FLT_ROUNDS = 2 or 3 */
1796                                 if (sign) {
1797                                         rv = -rv;
1798                                         sign = 0;
1799                                         }
1800 #endif
1801                                 e -= i;
1802                                 dval(rv) *= tens[i];
1803 #ifdef VAX
1804                                 /* VAX exponent range is so narrow we must
1805                                  * worry about overflow here...
1806                                  */
1807  vax_ovfl_check:
1808                                 word0(rv) -= P*Exp_msk1;
1809                                 /* rv = */ rounded_product(dval(rv), tens[e]);
1810                                 if ((word0(rv) & Exp_mask)
1811                                  > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1812                                         goto ovfl;
1813                                 word0(rv) += P*Exp_msk1;
1814 #else
1815                                 /* rv = */ rounded_product(dval(rv), tens[e]);
1816 #endif
1817                                 goto ret;
1818                                 }
1819                         }
1820 #ifndef Inaccurate_Divide
1821                 else if (e >= -Ten_pmax) {
1822 #ifdef Honor_FLT_ROUNDS
1823                         /* round correctly FLT_ROUNDS = 2 or 3 */
1824                         if (sign) {
1825                                 rv = -rv;
1826                                 sign = 0;
1827                                 }
1828 #endif
1829                         /* rv = */ rounded_quotient(dval(rv), tens[-e]);
1830                         goto ret;
1831                         }
1832 #endif
1833                 }
1834         e1 += nd - k;
1835
1836 #ifdef IEEE_Arith
1837 #ifdef SET_INEXACT
1838         inexact = 1;
1839         if (k <= DBL_DIG)
1840                 oldinexact = get_inexact();
1841 #endif
1842 #ifdef Avoid_Underflow
1843         scale = 0;
1844 #endif
1845 #ifdef Honor_FLT_ROUNDS
1846         if ((rounding = Flt_Rounds) >= 2) {
1847                 if (sign)
1848                         rounding = rounding == 2 ? 0 : 2;
1849                 else
1850                         if (rounding != 2)
1851                                 rounding = 0;
1852                 }
1853 #endif
1854 #endif /*IEEE_Arith*/
1855
1856         /* Get starting approximation = rv * 10**e1 */
1857
1858         if (e1 > 0) {
1859                 if ((i = e1 & 15))
1860                         dval(rv) *= tens[i];
1861                 if (e1 &= ~15) {
1862                         if (e1 > DBL_MAX_10_EXP) {
1863  ovfl:
1864 #ifndef NO_ERRNO
1865                                 errno = ERANGE;
1866 #endif
1867                                 /* Can't trust HUGE_VAL */
1868 #ifdef IEEE_Arith
1869 #ifdef Honor_FLT_ROUNDS
1870                                 switch(rounding) {
1871                                   case 0: /* toward 0 */
1872                                   case 3: /* toward -infinity */
1873                                         word0(rv) = Big0;
1874                                         word1(rv) = Big1;
1875                                         break;
1876                                   default:
1877                                         word0(rv) = Exp_mask;
1878                                         word1(rv) = 0;
1879                                   }
1880 #else /*Honor_FLT_ROUNDS*/
1881                                 word0(rv) = Exp_mask;
1882                                 word1(rv) = 0;
1883 #endif /*Honor_FLT_ROUNDS*/
1884 #ifdef SET_INEXACT
1885                                 /* set overflow bit */
1886                                 dval(rv0) = 1e300;
1887                                 dval(rv0) *= dval(rv0);
1888 #endif
1889 #else /*IEEE_Arith*/
1890                                 word0(rv) = Big0;
1891                                 word1(rv) = Big1;
1892 #endif /*IEEE_Arith*/
1893                                 if (bd0)
1894                                         goto retfree;
1895                                 goto ret;
1896                                 }
1897                         e1 >>= 4;
1898                         for(j = 0; e1 > 1; j++, e1 >>= 1)
1899                                 if (e1 & 1)
1900                                         dval(rv) *= bigtens[j];
1901                 /* The last multiplication could overflow. */
1902                         word0(rv) -= P*Exp_msk1;
1903                         dval(rv) *= bigtens[j];
1904                         if ((z = word0(rv) & Exp_mask)
1905                          > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1906                                 goto ovfl;
1907                         if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1908                                 /* set to largest number */
1909                                 /* (Can't trust DBL_MAX) */
1910                                 word0(rv) = Big0;
1911                                 word1(rv) = Big1;
1912                                 }
1913                         else
1914                                 word0(rv) += P*Exp_msk1;
1915                         }
1916                 }
1917         else if (e1 < 0) {
1918                 e1 = -e1;
1919                 if ((i = e1 & 15))
1920                         dval(rv) /= tens[i];
1921                 if (e1 >>= 4) {
1922                         if (e1 >= 1 << n_bigtens)
1923                                 goto undfl;
1924 #ifdef Avoid_Underflow
1925                         if (e1 & Scale_Bit)
1926                                 scale = 2*P;
1927                         for(j = 0; e1 > 0; j++, e1 >>= 1)
1928                                 if (e1 & 1)
1929                                         dval(rv) *= tinytens[j];
1930                         if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask)
1931                                                 >> Exp_shift)) > 0) {
1932                                 /* scaled rv is denormal; zap j low bits */
1933                                 if (j >= 32) {
1934                                         word1(rv) = 0;
1935                                         if (j >= 53)
1936                                          word0(rv) = (P+2)*Exp_msk1;
1937                                         else
1938                                          word0(rv) &= 0xffffffff << (j-32);
1939                                         }
1940                                 else
1941                                         word1(rv) &= 0xffffffff << j;
1942                                 }
1943 #else
1944                         for(j = 0; e1 > 1; j++, e1 >>= 1)
1945                                 if (e1 & 1)
1946                                         dval(rv) *= tinytens[j];
1947                         /* The last multiplication could underflow. */
1948                         dval(rv0) = dval(rv);
1949                         dval(rv) *= tinytens[j];
1950                         if (!dval(rv)) {
1951                                 dval(rv) = 2.*dval(rv0);
1952                                 dval(rv) *= tinytens[j];
1953 #endif
1954                                 if (!dval(rv)) {
1955  undfl:
1956                                         dval(rv) = 0.;
1957 #ifndef NO_ERRNO
1958                                         errno = ERANGE;
1959 #endif
1960                                         if (bd0)
1961                                                 goto retfree;
1962                                         goto ret;
1963                                         }
1964 #ifndef Avoid_Underflow
1965                                 word0(rv) = Tiny0;
1966                                 word1(rv) = Tiny1;
1967                                 /* The refinement below will clean
1968                                  * this approximation up.
1969                                  */
1970                                 }
1971 #endif
1972                         }
1973                 }
1974
1975         /* Now the hard part -- adjusting rv to the correct value.*/
1976
1977         /* Put digits into bd: true value = bd * 10^e */
1978
1979         bd0 = s2b(s0, nd0, nd, y);
1980
1981         for(;;) {
1982                 bd = Balloc(bd0->k);
1983                 Bcopy(bd, bd0);
1984                 bb = d2b(dval(rv), &bbe, &bbbits);      /* rv = bb * 2^bbe */
1985                 bs = i2b(1);
1986
1987                 if (e >= 0) {
1988                         bb2 = bb5 = 0;
1989                         bd2 = bd5 = e;
1990                         }
1991                 else {
1992                         bb2 = bb5 = -e;
1993                         bd2 = bd5 = 0;
1994                         }
1995                 if (bbe >= 0)
1996                         bb2 += bbe;
1997                 else
1998                         bd2 -= bbe;
1999                 bs2 = bb2;
2000 #ifdef Honor_FLT_ROUNDS
2001                 if (rounding != 1)
2002                         bs2++;
2003 #endif
2004 #ifdef Avoid_Underflow
2005                 j = bbe - scale;
2006                 i = j + bbbits - 1;     /* logb(rv) */
2007                 if (i < Emin)   /* denormal */
2008                         j += P - Emin;
2009                 else
2010                         j = P + 1 - bbbits;
2011 #else /*Avoid_Underflow*/
2012 #ifdef Sudden_Underflow
2013 #ifdef IBM
2014                 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
2015 #else
2016                 j = P + 1 - bbbits;
2017 #endif
2018 #else /*Sudden_Underflow*/
2019                 j = bbe;
2020                 i = j + bbbits - 1;     /* logb(rv) */
2021                 if (i < Emin)   /* denormal */
2022                         j += P - Emin;
2023                 else
2024                         j = P + 1 - bbbits;
2025 #endif /*Sudden_Underflow*/
2026 #endif /*Avoid_Underflow*/
2027                 bb2 += j;
2028                 bd2 += j;
2029 #ifdef Avoid_Underflow
2030                 bd2 += scale;
2031 #endif
2032                 i = bb2 < bd2 ? bb2 : bd2;
2033                 if (i > bs2)
2034                         i = bs2;
2035                 if (i > 0) {
2036                         bb2 -= i;
2037                         bd2 -= i;
2038                         bs2 -= i;
2039                         }
2040                 if (bb5 > 0) {
2041                         bs = pow5mult(bs, bb5);
2042                         bb1 = mult(bs, bb);
2043                         Bfree(bb);
2044                         bb = bb1;
2045                         }
2046                 if (bb2 > 0)
2047                         bb = lshift(bb, bb2);
2048                 if (bd5 > 0)
2049                         bd = pow5mult(bd, bd5);
2050                 if (bd2 > 0)
2051                         bd = lshift(bd, bd2);
2052                 if (bs2 > 0)
2053                         bs = lshift(bs, bs2);
2054                 delta = diff(bb, bd);
2055                 dsign = delta->sign;
2056                 delta->sign = 0;
2057                 i = cmp(delta, bs);
2058 #ifdef Honor_FLT_ROUNDS
2059                 if (rounding != 1) {
2060                         if (i < 0) {
2061                                 /* Error is less than an ulp */
2062                                 if (!delta->x[0] && delta->wds <= 1) {
2063                                         /* exact */
2064 #ifdef SET_INEXACT
2065                                         inexact = 0;
2066 #endif
2067                                         break;
2068                                         }
2069                                 if (rounding) {
2070                                         if (dsign) {
2071                                                 adj = 1.;
2072                                                 goto apply_adj;
2073                                                 }
2074                                         }
2075                                 else if (!dsign) {
2076                                         adj = -1.;
2077                                         if (!word1(rv)
2078                                          && !(word0(rv) & Frac_mask)) {
2079                                                 y = word0(rv) & Exp_mask;
2080 #ifdef Avoid_Underflow
2081                                                 if (!scale || y > 2*P*Exp_msk1)
2082 #else
2083                                                 if (y)
2084 #endif
2085                                                   {
2086                                                   delta = lshift(delta,Log2P);
2087                                                   if (cmp(delta, bs) <= 0)
2088                                                         adj = -0.5;
2089                                                   }
2090                                                 }
2091  apply_adj:
2092 #ifdef Avoid_Underflow
2093                                         if (scale && (y = word0(rv) & Exp_mask)
2094                                                 <= 2*P*Exp_msk1)
2095                                           word0(adj) += (2*P+1)*Exp_msk1 - y;
2096 #else
2097 #ifdef Sudden_Underflow
2098                                         if ((word0(rv) & Exp_mask) <=
2099                                                         P*Exp_msk1) {
2100                                                 word0(rv) += P*Exp_msk1;
2101                                                 dval(rv) += adj*ulp(dval(rv));
2102                                                 word0(rv) -= P*Exp_msk1;
2103                                                 }
2104                                         else
2105 #endif /*Sudden_Underflow*/
2106 #endif /*Avoid_Underflow*/
2107                                         dval(rv) += adj*ulp(dval(rv));
2108                                         }
2109                                 break;
2110                                 }
2111                         adj = ratio(delta, bs);
2112                         if (adj < 1.)
2113                                 adj = 1.;
2114                         if (adj <= 0x7ffffffe) {
2115                                 /* adj = rounding ? ceil(adj) : floor(adj); */
2116                                 y = adj;
2117                                 if (y != adj) {
2118                                         if (!((rounding>>1) ^ dsign))
2119                                                 y++;
2120                                         adj = y;
2121                                         }
2122                                 }
2123 #ifdef Avoid_Underflow
2124                         if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2125                                 word0(adj) += (2*P+1)*Exp_msk1 - y;
2126 #else
2127 #ifdef Sudden_Underflow
2128                         if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2129                                 word0(rv) += P*Exp_msk1;
2130                                 adj *= ulp(dval(rv));
2131                                 if (dsign)
2132                                         dval(rv) += adj;
2133                                 else
2134                                         dval(rv) -= adj;
2135                                 word0(rv) -= P*Exp_msk1;
2136                                 goto cont;
2137                                 }
2138 #endif /*Sudden_Underflow*/
2139 #endif /*Avoid_Underflow*/
2140                         adj *= ulp(dval(rv));
2141                         if (dsign)
2142                                 dval(rv) += adj;
2143                         else
2144                                 dval(rv) -= adj;
2145                         goto cont;
2146                         }
2147 #endif /*Honor_FLT_ROUNDS*/
2148
2149                 if (i < 0) {
2150                         /* Error is less than half an ulp -- check for
2151                          * special case of mantissa a power of two.
2152                          */
2153                         if (dsign || word1(rv) || word0(rv) & Bndry_mask
2154 #ifdef IEEE_Arith
2155 #ifdef Avoid_Underflow
2156                          || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1
2157 #else
2158                          || (word0(rv) & Exp_mask) <= Exp_msk1
2159 #endif
2160 #endif
2161                                 ) {
2162 #ifdef SET_INEXACT
2163                                 if (!delta->x[0] && delta->wds <= 1)
2164                                         inexact = 0;
2165 #endif
2166                                 break;
2167                                 }
2168                         if (!delta->x[0] && delta->wds <= 1) {
2169                                 /* exact result */
2170 #ifdef SET_INEXACT
2171                                 inexact = 0;
2172 #endif
2173                                 break;
2174                                 }
2175                         delta = lshift(delta,Log2P);
2176                         if (cmp(delta, bs) > 0)
2177                                 goto drop_down;
2178                         break;
2179                         }
2180                 if (i == 0) {
2181                         /* exactly half-way between */
2182                         if (dsign) {
2183                                 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
2184                                  &&  word1(rv) == (
2185 #ifdef Avoid_Underflow
2186                         (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2187                 ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
2188 #endif
2189                                                    0xffffffff)) {
2190                                         /*boundary case -- increment exponent*/
2191                                         word0(rv) = (word0(rv) & Exp_mask)
2192                                                 + Exp_msk1
2193 #ifdef IBM
2194                                                 | Exp_msk1 >> 4
2195 #endif
2196                                                 ;
2197                                         word1(rv) = 0;
2198 #ifdef Avoid_Underflow
2199                                         dsign = 0;
2200 #endif
2201                                         break;
2202                                         }
2203                                 }
2204                         else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
2205  drop_down:
2206                                 /* boundary case -- decrement exponent */
2207 #ifdef Sudden_Underflow /*{{*/
2208                                 L = word0(rv) & Exp_mask;
2209 #ifdef IBM
2210                                 if (L <  Exp_msk1)
2211 #else
2212 #ifdef Avoid_Underflow
2213                                 if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
2214 #else
2215                                 if (L <= Exp_msk1)
2216 #endif /*Avoid_Underflow*/
2217 #endif /*IBM*/
2218                                         goto undfl;
2219                                 L -= Exp_msk1;
2220 #else /*Sudden_Underflow}{*/
2221 #ifdef Avoid_Underflow
2222                                 if (scale) {
2223                                         L = word0(rv) & Exp_mask;
2224                                         if (L <= (2*P+1)*Exp_msk1) {
2225                                                 if (L > (P+2)*Exp_msk1)
2226                                                         /* round even ==> */
2227                                                         /* accept rv */
2228                                                         break;
2229                                                 /* rv = smallest denormal */
2230                                                 goto undfl;
2231                                                 }
2232                                         }
2233 #endif /*Avoid_Underflow*/
2234                                 L = (word0(rv) & Exp_mask) - Exp_msk1;
2235 #endif /*Sudden_Underflow}}*/
2236                                 word0(rv) = L | Bndry_mask1;
2237                                 word1(rv) = 0xffffffff;
2238 #ifdef IBM
2239                                 goto cont;
2240 #else
2241                                 break;
2242 #endif
2243                                 }
2244 #ifndef ROUND_BIASED
2245                         if (!(word1(rv) & LSB))
2246                                 break;
2247 #endif
2248                         if (dsign)
2249                                 dval(rv) += ulp(dval(rv));
2250 #ifndef ROUND_BIASED
2251                         else {
2252                                 dval(rv) -= ulp(dval(rv));
2253 #ifndef Sudden_Underflow
2254                                 if (!dval(rv))
2255                                         goto undfl;
2256 #endif
2257                                 }
2258 #ifdef Avoid_Underflow
2259                         dsign = 1 - dsign;
2260 #endif
2261 #endif
2262                         break;
2263                         }
2264                 if ((aadj = ratio(delta, bs)) <= 2.) {
2265                         if (dsign)
2266                                 aadj = aadj1 = 1.;
2267                         else if (word1(rv) || word0(rv) & Bndry_mask) {
2268 #ifndef Sudden_Underflow
2269                                 if (word1(rv) == Tiny1 && !word0(rv))
2270                                         goto undfl;
2271 #endif
2272                                 aadj = 1.;
2273                                 aadj1 = -1.;
2274                                 }
2275                         else {
2276                                 /* special case -- power of FLT_RADIX to be */
2277                                 /* rounded down... */
2278
2279                                 if (aadj < 2./FLT_RADIX)
2280                                         aadj = 1./FLT_RADIX;
2281                                 else
2282                                         aadj *= 0.5;
2283                                 aadj1 = -aadj;
2284                                 }
2285                         }
2286                 else {
2287                         aadj *= 0.5;
2288                         aadj1 = dsign ? aadj : -aadj;
2289 #ifdef Check_FLT_ROUNDS
2290                         switch(Rounding) {
2291                                 case 2: /* towards +infinity */
2292                                         aadj1 -= 0.5;
2293                                         break;
2294                                 case 0: /* towards 0 */
2295                                 case 3: /* towards -infinity */
2296                                         aadj1 += 0.5;
2297                                 }
2298 #else
2299                         if (Flt_Rounds == 0)
2300                                 aadj1 += 0.5;
2301 #endif /*Check_FLT_ROUNDS*/
2302                         }
2303                 y = word0(rv) & Exp_mask;
2304
2305                 /* Check for overflow */
2306
2307                 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2308                         dval(rv0) = dval(rv);
2309                         word0(rv) -= P*Exp_msk1;
2310                         adj = aadj1 * ulp(dval(rv));
2311                         dval(rv) += adj;
2312                         if ((word0(rv) & Exp_mask) >=
2313                                         Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2314                                 if (word0(rv0) == Big0 && word1(rv0) == Big1)
2315                                         goto ovfl;
2316                                 word0(rv) = Big0;
2317                                 word1(rv) = Big1;
2318                                 goto cont;
2319                                 }
2320                         else
2321                                 word0(rv) += P*Exp_msk1;
2322                         }
2323                 else {
2324 #ifdef Avoid_Underflow
2325                         if (scale && y <= 2*P*Exp_msk1) {
2326                                 if (aadj <= 0x7fffffff) {
2327                                         if ((z = aadj) <= 0)
2328                                                 z = 1;
2329                                         aadj = z;
2330                                         aadj1 = dsign ? aadj : -aadj;
2331                                         }
2332                                 word0(aadj1) += (2*P+1)*Exp_msk1 - y;
2333                                 }
2334                         adj = aadj1 * ulp(dval(rv));
2335                         dval(rv) += adj;
2336 #else
2337 #ifdef Sudden_Underflow
2338                         if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2339                                 dval(rv0) = dval(rv);
2340                                 word0(rv) += P*Exp_msk1;
2341                                 adj = aadj1 * ulp(dval(rv));
2342                                 dval(rv) += adj;
2343 #ifdef IBM
2344                                 if ((word0(rv) & Exp_mask) <  P*Exp_msk1)
2345 #else
2346                                 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
2347 #endif
2348                                         {
2349                                         if (word0(rv0) == Tiny0
2350                                          && word1(rv0) == Tiny1)
2351                                                 goto undfl;
2352                                         word0(rv) = Tiny0;
2353                                         word1(rv) = Tiny1;
2354                                         goto cont;
2355                                         }
2356                                 else
2357                                         word0(rv) -= P*Exp_msk1;
2358                                 }
2359                         else {
2360                                 adj = aadj1 * ulp(dval(rv));
2361                                 dval(rv) += adj;
2362                                 }
2363 #else /*Sudden_Underflow*/
2364                         /* Compute adj so that the IEEE rounding rules will
2365                          * correctly round rv + adj in some half-way cases.
2366                          * If rv * ulp(rv) is denormalized (i.e.,
2367                          * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2368                          * trouble from bits lost to denormalization;
2369                          * example: 1.2e-307 .
2370                          */
2371                         if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
2372                                 aadj1 = (double)(int)(aadj + 0.5);
2373                                 if (!dsign)
2374                                         aadj1 = -aadj1;
2375                                 }
2376                         adj = aadj1 * ulp(dval(rv));
2377                         dval(rv) += adj;
2378 #endif /*Sudden_Underflow*/
2379 #endif /*Avoid_Underflow*/
2380                         }
2381                 z = word0(rv) & Exp_mask;
2382 #ifndef SET_INEXACT
2383 #ifdef Avoid_Underflow
2384                 if (!scale)
2385 #endif
2386                 if (y == z) {
2387                         /* Can we stop now? */
2388                         L = (Long)aadj;
2389                         aadj -= L;
2390                         /* The tolerances below are conservative. */
2391                         if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
2392                                 if (aadj < .4999999 || aadj > .5000001)
2393                                         break;
2394                                 }
2395                         else if (aadj < .4999999/FLT_RADIX)
2396                                 break;
2397                         }
2398 #endif
2399  cont:
2400                 Bfree(bb);
2401                 Bfree(bd);
2402                 Bfree(bs);
2403                 Bfree(delta);
2404                 }
2405 #ifdef SET_INEXACT
2406         if (inexact) {
2407                 if (!oldinexact) {
2408                         word0(rv0) = Exp_1 + (70 << Exp_shift);
2409                         word1(rv0) = 0;
2410                         dval(rv0) += 1.;
2411                         }
2412                 }
2413         else if (!oldinexact)
2414                 clear_inexact();
2415 #endif
2416 #ifdef Avoid_Underflow
2417         if (scale) {
2418                 word0(rv0) = Exp_1 - 2*P*Exp_msk1;
2419                 word1(rv0) = 0;
2420                 dval(rv) *= dval(rv0);
2421 #ifndef NO_ERRNO
2422                 /* try to avoid the bug of testing an 8087 register value */
2423                 if (word0(rv) == 0 && word1(rv) == 0)
2424                         errno = ERANGE;
2425 #endif
2426                 }
2427 #endif /* Avoid_Underflow */
2428 #ifdef SET_INEXACT
2429         if (inexact && !(word0(rv) & Exp_mask)) {
2430                 /* set underflow bit */
2431                 dval(rv0) = 1e-300;
2432                 dval(rv0) *= dval(rv0);
2433                 }
2434 #endif
2435  retfree:
2436         Bfree(bb);
2437         Bfree(bd);
2438         Bfree(bs);
2439         Bfree(bd0);
2440         Bfree(delta);
2441  ret:
2442         if (se)
2443                 *se = (char *)s;
2444         return sign ? -dval(rv) : dval(rv);
2445         }
2446
2447 #if 0
2448  static int
2449 quorem
2450 #ifdef KR_headers
2451         (b, S) Bigint *b, *S;
2452 #else
2453         (Bigint *b, Bigint *S)
2454 #endif
2455 {
2456         int n;
2457         ULong *bx, *bxe, q, *sx, *sxe;
2458 #ifdef ULLong
2459         ULLong borrow, carry, y, ys;
2460 #else
2461         ULong borrow, carry, y, ys;
2462 #ifdef Pack_32
2463         ULong si, z, zs;
2464 #endif
2465 #endif
2466
2467         n = S->wds;
2468 #ifdef DEBUG
2469         /*debug*/ if (b->wds > n)
2470         /*debug*/       Bug("oversize b in quorem");
2471 #endif
2472         if (b->wds < n)
2473                 return 0;
2474         sx = S->x;
2475         sxe = sx + --n;
2476         bx = b->x;
2477         bxe = bx + n;
2478         q = *bxe / (*sxe + 1);  /* ensure q <= true quotient */
2479 #ifdef DEBUG
2480         /*debug*/ if (q > 9)
2481         /*debug*/       Bug("oversized quotient in quorem");
2482 #endif
2483         if (q) {
2484                 borrow = 0;
2485                 carry = 0;
2486                 do {
2487 #ifdef ULLong
2488                         ys = *sx++ * (ULLong)q + carry;
2489                         carry = ys >> 32;
2490                         y = *bx - (ys & FFFFFFFF) - borrow;
2491                         borrow = y >> 32 & (ULong)1;
2492                         *bx++ = y & FFFFFFFF;
2493 #else
2494 #ifdef Pack_32
2495                         si = *sx++;
2496                         ys = (si & 0xffff) * q + carry;
2497                         zs = (si >> 16) * q + (ys >> 16);
2498                         carry = zs >> 16;
2499                         y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2500                         borrow = (y & 0x10000) >> 16;
2501                         z = (*bx >> 16) - (zs & 0xffff) - borrow;
2502                         borrow = (z & 0x10000) >> 16;
2503                         Storeinc(bx, z, y);
2504 #else
2505                         ys = *sx++ * q + carry;
2506                         carry = ys >> 16;
2507                         y = *bx - (ys & 0xffff) - borrow;
2508                         borrow = (y & 0x10000) >> 16;
2509                         *bx++ = y & 0xffff;
2510 #endif
2511 #endif
2512                         }
2513                         while(sx <= sxe);
2514                 if (!*bxe) {
2515                         bx = b->x;
2516                         while(--bxe > bx && !*bxe)
2517                                 --n;
2518                         b->wds = n;
2519                         }
2520                 }
2521         if (cmp(b, S) >= 0) {
2522                 q++;
2523                 borrow = 0;
2524                 carry = 0;
2525                 bx = b->x;
2526                 sx = S->x;
2527                 do {
2528 #ifdef ULLong
2529                         ys = *sx++ + carry;
2530                         carry = ys >> 32;
2531                         y = *bx - (ys & FFFFFFFF) - borrow;
2532                         borrow = y >> 32 & (ULong)1;
2533                         *bx++ = y & FFFFFFFF;
2534 #else
2535 #ifdef Pack_32
2536                         si = *sx++;
2537                         ys = (si & 0xffff) + carry;
2538                         zs = (si >> 16) + (ys >> 16);
2539                         carry = zs >> 16;
2540                         y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2541                         borrow = (y & 0x10000) >> 16;
2542                         z = (*bx >> 16) - (zs & 0xffff) - borrow;
2543                         borrow = (z & 0x10000) >> 16;
2544                         Storeinc(bx, z, y);
2545 #else
2546                         ys = *sx++ + carry;
2547                         carry = ys >> 16;
2548                         y = *bx - (ys & 0xffff) - borrow;
2549                         borrow = (y & 0x10000) >> 16;
2550                         *bx++ = y & 0xffff;
2551 #endif
2552 #endif
2553                         }
2554                         while(sx <= sxe);
2555                 bx = b->x;
2556                 bxe = bx + n;
2557                 if (!*bxe) {
2558                         while(--bxe > bx && !*bxe)
2559                                 --n;
2560                         b->wds = n;
2561                         }
2562                 }
2563         return q;
2564         }
2565 #endif
2566
2567 #ifndef MULTIPLE_THREADS
2568  static char *dtoa_result;
2569 #endif
2570
2571 #if 0
2572  static char *
2573 #ifdef KR_headers
2574 rv_alloc(i) int i;
2575 #else
2576 rv_alloc(int i)
2577 #endif
2578 {
2579         int j, k, *r;
2580
2581         j = sizeof(ULong);
2582         for(k = 0;
2583                 sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
2584                 j <<= 1)
2585                         k++;
2586         r = (int*)Balloc(k);
2587         *r = k;
2588         return
2589 #ifndef MULTIPLE_THREADS
2590         dtoa_result =
2591 #endif
2592                 (char *)(r+1);
2593         }
2594
2595  static char *
2596 #ifdef KR_headers
2597 nrv_alloc(s, rve, n) char *s, **rve; int n;
2598 #else
2599 nrv_alloc(char *s, char **rve, int n)
2600 #endif
2601 {
2602         char *rv, *t;
2603
2604         t = rv = rv_alloc(n);
2605         while((*t = *s++)) t++;
2606         if (rve)
2607                 *rve = t;
2608         return rv;
2609         }
2610
2611 /* freedtoa(s) must be used to free values s returned by dtoa
2612  * when MULTIPLE_THREADS is #defined.  It should be used in all cases,
2613  * but for consistency with earlier versions of dtoa, it is optional
2614  * when MULTIPLE_THREADS is not defined.
2615  */
2616
2617 static void freedtoa (char *s);
2618
2619 static void
2620 #ifdef KR_headers
2621 freedtoa(s) char *s;
2622 #else
2623 freedtoa(char *s)
2624 #endif
2625 {
2626         Bigint *b = (Bigint *)((int *)s - 1);
2627         b->maxwds = 1 << (b->k = *(int*)b);
2628         Bfree(b);
2629 #ifndef MULTIPLE_THREADS
2630         if (s == dtoa_result)
2631                 dtoa_result = 0;
2632 #endif
2633         }
2634
2635 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2636  *
2637  * Inspired by "How to Print Floating-Point Numbers Accurately" by
2638  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2639  *
2640  * Modifications:
2641  *      1. Rather than iterating, we use a simple numeric overestimate
2642  *         to determine k = floor(log10(d)).  We scale relevant
2643  *         quantities using O(log2(k)) rather than O(k) multiplications.
2644  *      2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2645  *         try to generate digits strictly left to right.  Instead, we
2646  *         compute with fewer bits and propagate the carry if necessary
2647  *         when rounding the final digit up.  This is often faster.
2648  *      3. Under the assumption that input will be rounded nearest,
2649  *         mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2650  *         That is, we allow equality in stopping tests when the
2651  *         round-nearest rule will give the same floating-point value
2652  *         as would satisfaction of the stopping test with strict
2653  *         inequality.
2654  *      4. We remove common factors of powers of 2 from relevant
2655  *         quantities.
2656  *      5. When converting floating-point integers less than 1e16,
2657  *         we use floating-point arithmetic rather than resorting
2658  *         to multiple-precision integers.
2659  *      6. When asked to produce fewer than 15 digits, we first try
2660  *         to get by with floating-point arithmetic; we resort to
2661  *         multiple-precision integer arithmetic only if we cannot
2662  *         guarantee that the floating-point calculation has given
2663  *         the correctly rounded result.  For k requested digits and
2664  *         "uniformly" distributed input, the probability is
2665  *         something like 10^(k-15) that we must resort to the Long
2666  *         calculation.
2667  */
2668
2669  char *
2670 dtoa
2671 #ifdef KR_headers
2672         (d, mode, ndigits, decpt, sign, rve)
2673         double d; int mode, ndigits, *decpt, *sign; char **rve;
2674 #else
2675         (double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
2676 #endif
2677 {
2678  /*     Arguments ndigits, decpt, sign are similar to those
2679         of ecvt and fcvt; trailing zeros are suppressed from
2680         the returned string.  If not null, *rve is set to point
2681         to the end of the return value.  If d is +-Infinity or NaN,
2682         then *decpt is set to 9999.
2683
2684         mode:
2685                 0 ==> shortest string that yields d when read in
2686                         and rounded to nearest.
2687                 1 ==> like 0, but with Steele & White stopping rule;
2688                         e.g. with IEEE P754 arithmetic , mode 0 gives
2689                         1e23 whereas mode 1 gives 9.999999999999999e22.
2690                 2 ==> max(1,ndigits) significant digits.  This gives a
2691                         return value similar to that of ecvt, except
2692                         that trailing zeros are suppressed.
2693                 3 ==> through ndigits past the decimal point.  This
2694                         gives a return value similar to that from fcvt,
2695                         except that trailing zeros are suppressed, and
2696                         ndigits can be negative.
2697                 4,5 ==> similar to 2 and 3, respectively, but (in
2698                         round-nearest mode) with the tests of mode 0 to
2699                         possibly return a shorter string that rounds to d.
2700                         With IEEE arithmetic and compilation with
2701                         -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2702                         as modes 2 and 3 when FLT_ROUNDS != 1.
2703                 6-9 ==> Debugging modes similar to mode - 4:  don't try
2704                         fast floating-point estimate (if applicable).
2705
2706                 Values of mode other than 0-9 are treated as mode 0.
2707
2708                 Sufficient space is allocated to the return value
2709                 to hold the suppressed trailing zeros.
2710         */
2711
2712         int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2713                 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2714                 spec_case, try_quick;
2715         Long L;
2716 #ifndef Sudden_Underflow
2717         int denorm;
2718         ULong x;
2719 #endif
2720         Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2721         double d2, ds, eps;
2722         char *s, *s0;
2723 #ifdef Honor_FLT_ROUNDS
2724         int rounding;
2725 #endif
2726 #ifdef SET_INEXACT
2727         int inexact, oldinexact;
2728 #endif
2729
2730 #ifndef MULTIPLE_THREADS
2731         if (dtoa_result) {
2732                 freedtoa(dtoa_result);
2733                 dtoa_result = 0;
2734                 }
2735 #endif
2736
2737         if (word0(d) & Sign_bit) {
2738                 /* set sign for everything, including 0's and NaNs */
2739                 *sign = 1;
2740                 word0(d) &= ~Sign_bit;  /* clear sign bit */
2741                 }
2742         else
2743                 *sign = 0;
2744
2745 #if defined(IEEE_Arith) + defined(VAX)
2746 #ifdef IEEE_Arith
2747         if ((word0(d) & Exp_mask) == Exp_mask)
2748 #else
2749         if (word0(d)  == 0x8000)
2750 #endif
2751                 {
2752                 /* Infinity or NaN */
2753                 *decpt = 9999;
2754 #ifdef IEEE_Arith
2755                 if (!word1(d) && !(word0(d) & 0xfffff))
2756                         return nrv_alloc("Infinity", rve, 8);
2757 #endif
2758                 return nrv_alloc("NaN", rve, 3);
2759                 }
2760 #endif
2761 #ifdef IBM
2762         dval(d) += 0; /* normalize */
2763 #endif
2764         if (!dval(d)) {
2765                 *decpt = 1;
2766                 return nrv_alloc("0", rve, 1);
2767                 }
2768
2769 #ifdef SET_INEXACT
2770         try_quick = oldinexact = get_inexact();
2771         inexact = 1;
2772 #endif
2773 #ifdef Honor_FLT_ROUNDS
2774         if ((rounding = Flt_Rounds) >= 2) {
2775                 if (*sign)
2776                         rounding = rounding == 2 ? 0 : 2;
2777                 else
2778                         if (rounding != 2)
2779                                 rounding = 0;
2780                 }
2781 #endif
2782
2783         b = d2b(dval(d), &be, &bbits);
2784 #ifdef Sudden_Underflow
2785         i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
2786 #else
2787         if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) {
2788 #endif
2789                 dval(d2) = dval(d);
2790                 word0(d2) &= Frac_mask1;
2791                 word0(d2) |= Exp_11;
2792 #ifdef IBM
2793                 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
2794                         dval(d2) /= 1 << j;
2795 #endif
2796
2797                 /* log(x)       ~=~ log(1.5) + (x-1.5)/1.5
2798                  * log10(x)      =  log(x) / log(10)
2799                  *              ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2800                  * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2801                  *
2802                  * This suggests computing an approximation k to log10(d) by
2803                  *
2804                  * k = (i - Bias)*0.301029995663981
2805                  *      + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2806                  *
2807                  * We want k to be too large rather than too small.
2808                  * The error in the first-order Taylor series approximation
2809                  * is in our favor, so we just round up the constant enough
2810                  * to compensate for any error in the multiplication of
2811                  * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2812                  * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2813                  * adding 1e-13 to the constant term more than suffices.
2814                  * Hence we adjust the constant term to 0.1760912590558.
2815                  * (We could get a more accurate k by invoking log10,
2816                  *  but this is probably not worthwhile.)
2817                  */
2818
2819                 i -= Bias;
2820 #ifdef IBM
2821                 i <<= 2;
2822                 i += j;
2823 #endif
2824 #ifndef Sudden_Underflow
2825                 denorm = 0;
2826                 }
2827         else {
2828                 /* d is denormalized */
2829
2830                 i = bbits + be + (Bias + (P-1) - 1);
2831                 x = i > 32  ? word0(d) << 64 - i | word1(d) >> i - 32
2832                             : word1(d) << 32 - i;
2833                 dval(d2) = x;
2834                 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
2835                 i -= (Bias + (P-1) - 1) + 1;
2836                 denorm = 1;
2837                 }
2838 #endif
2839         ds = (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
2840         k = (int)ds;
2841         if (ds < 0. && ds != k)
2842                 k--;    /* want k = floor(ds) */
2843         k_check = 1;
2844         if (k >= 0 && k <= Ten_pmax) {
2845                 if (dval(d) < tens[k])
2846                         k--;
2847                 k_check = 0;
2848                 }
2849         j = bbits - i - 1;
2850         if (j >= 0) {
2851                 b2 = 0;
2852                 s2 = j;
2853                 }
2854         else {
2855                 b2 = -j;
2856                 s2 = 0;
2857                 }
2858         if (k >= 0) {
2859                 b5 = 0;
2860                 s5 = k;
2861                 s2 += k;
2862                 }
2863         else {
2864                 b2 -= k;
2865                 b5 = -k;
2866                 s5 = 0;
2867                 }
2868         if (mode < 0 || mode > 9)
2869                 mode = 0;
2870
2871 #ifndef SET_INEXACT
2872 #ifdef Check_FLT_ROUNDS
2873         try_quick = Rounding == 1;
2874 #else
2875         try_quick = 1;
2876 #endif
2877 #endif /*SET_INEXACT*/
2878
2879         if (mode > 5) {
2880                 mode -= 4;
2881                 try_quick = 0;
2882                 }
2883         leftright = 1;
2884         switch(mode) {
2885                 case 0:
2886                 case 1:
2887                         ilim = ilim1 = -1;
2888                         i = 18;
2889                         ndigits = 0;
2890                         break;
2891                 case 2:
2892                         leftright = 0;
2893                         /* no break */
2894                 case 4:
2895                         if (ndigits <= 0)
2896                                 ndigits = 1;
2897                         ilim = ilim1 = i = ndigits;
2898                         break;
2899                 case 3:
2900                         leftright = 0;
2901                         /* no break */
2902                 case 5:
2903                         i = ndigits + k + 1;
2904                         ilim = i;
2905                         ilim1 = i - 1;
2906                         if (i <= 0)
2907                                 i = 1;
2908                 }
2909         s = s0 = rv_alloc(i);
2910
2911 #ifdef Honor_FLT_ROUNDS
2912         if (mode > 1 && rounding != 1)
2913                 leftright = 0;
2914 #endif
2915
2916         if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2917
2918                 /* Try to get by with floating-point arithmetic. */
2919
2920                 i = 0;
2921                 dval(d2) = dval(d);
2922                 k0 = k;
2923                 ilim0 = ilim;
2924                 ieps = 2; /* conservative */
2925                 if (k > 0) {
2926                         ds = tens[k&0xf];
2927                         j = k >> 4;
2928                         if (j & Bletch) {
2929                                 /* prevent overflows */
2930                                 j &= Bletch - 1;
2931                                 dval(d) /= bigtens[n_bigtens-1];
2932                                 ieps++;
2933                                 }
2934                         for(; j; j >>= 1, i++)
2935                                 if (j & 1) {
2936                                         ieps++;
2937                                         ds *= bigtens[i];
2938                                         }
2939                         dval(d) /= ds;
2940                         }
2941                 else if (j1 = -k) {
2942                         dval(d) *= tens[j1 & 0xf];
2943                         for(j = j1 >> 4; j; j >>= 1, i++)
2944                                 if (j & 1) {
2945                                         ieps++;
2946                                         dval(d) *= bigtens[i];
2947                                         }
2948                         }
2949                 if (k_check && dval(d) < 1. && ilim > 0) {
2950                         if (ilim1 <= 0)
2951                                 goto fast_failed;
2952                         ilim = ilim1;
2953                         k--;
2954                         dval(d) *= 10.;
2955                         ieps++;
2956                         }
2957                 dval(eps) = ieps*dval(d) + 7.;
2958                 word0(eps) -= (P-1)*Exp_msk1;
2959                 if (ilim == 0) {
2960                         S = mhi = 0;
2961                         dval(d) -= 5.;
2962                         if (dval(d) > dval(eps))
2963                                 goto one_digit;
2964                         if (dval(d) < -dval(eps))
2965                                 goto no_digits;
2966                         goto fast_failed;
2967                         }
2968 #ifndef No_leftright
2969                 if (leftright) {
2970                         /* Use Steele & White method of only
2971                          * generating digits needed.
2972                          */
2973                         dval(eps) = 0.5/tens[ilim-1] - dval(eps);
2974                         for(i = 0;;) {
2975                                 L = dval(d);
2976                                 dval(d) -= L;
2977                                 *s++ = '0' + (int)L;
2978                                 if (dval(d) < dval(eps))
2979                                         goto ret1;
2980                                 if (1. - dval(d) < dval(eps))
2981                                         goto bump_up;
2982                                 if (++i >= ilim)
2983                                         break;
2984                                 dval(eps) *= 10.;
2985                                 dval(d) *= 10.;
2986                                 }
2987                         }
2988                 else {
2989 #endif
2990                         /* Generate ilim digits, then fix them up. */
2991                         dval(eps) *= tens[ilim-1];
2992                         for(i = 1;; i++, dval(d) *= 10.) {
2993                                 L = (Long)(dval(d));
2994                                 if (!(dval(d) -= L))
2995                                         ilim = i;
2996                                 *s++ = '0' + (int)L;
2997                                 if (i == ilim) {
2998                                         if (dval(d) > 0.5 + dval(eps))
2999                                                 goto bump_up;
3000                                         else if (dval(d) < 0.5 - dval(eps)) {
3001                                                 while(*--s == '0');
3002                                                 s++;
3003                                                 goto ret1;
3004                                                 }
3005                                         break;
3006                                         }
3007                                 }
3008 #ifndef No_leftright
3009                         }
3010 #endif
3011  fast_failed:
3012                 s = s0;
3013                 dval(d) = dval(d2);
3014                 k = k0;
3015                 ilim = ilim0;
3016                 }
3017
3018         /* Do we have a "small" integer? */
3019
3020         if (be >= 0 && k <= Int_max) {
3021                 /* Yes. */
3022                 ds = tens[k];
3023                 if (ndigits < 0 && ilim <= 0) {
3024                         S = mhi = 0;
3025                         if (ilim < 0 || dval(d) <= 5*ds)
3026                                 goto no_digits;
3027                         goto one_digit;
3028                         }
3029                 for(i = 1;; i++, dval(d) *= 10.) {
3030                         L = (Long)(dval(d) / ds);
3031                         dval(d) -= L*ds;
3032 #ifdef Check_FLT_ROUNDS
3033                         /* If FLT_ROUNDS == 2, L will usually be high by 1 */
3034                         if (dval(d) < 0) {
3035                                 L--;
3036                                 dval(d) += ds;
3037                                 }
3038 #endif
3039                         *s++ = '0' + (int)L;
3040                         if (!dval(d)) {
3041 #ifdef SET_INEXACT
3042                                 inexact = 0;
3043 #endif
3044                                 break;
3045                                 }
3046                         if (i == ilim) {
3047 #ifdef Honor_FLT_ROUNDS
3048                                 if (mode > 1)
3049                                 switch(rounding) {
3050                                   case 0: goto ret1;
3051                                   case 2: goto bump_up;
3052                                   }
3053 #endif
3054                                 dval(d) += dval(d);
3055                                 if (dval(d) > ds || dval(d) == ds && L & 1) {
3056  bump_up:
3057                                         while(*--s == '9')
3058                                                 if (s == s0) {
3059                                                         k++;
3060                                                         *s = '0';
3061                                                         break;
3062                                                         }
3063                                         ++*s++;
3064                                         }
3065                                 break;
3066                                 }
3067                         }
3068                 goto ret1;
3069                 }
3070
3071         m2 = b2;
3072         m5 = b5;
3073         mhi = mlo = 0;
3074         if (leftright) {
3075                 i =
3076 #ifndef Sudden_Underflow
3077                         denorm ? be + (Bias + (P-1) - 1 + 1) :
3078 #endif
3079 #ifdef IBM
3080                         1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
3081 #else
3082                         1 + P - bbits;
3083 #endif
3084                 b2 += i;
3085                 s2 += i;
3086                 mhi = i2b(1);
3087                 }
3088         if (m2 > 0 && s2 > 0) {
3089                 i = m2 < s2 ? m2 : s2;
3090                 b2 -= i;
3091                 m2 -= i;
3092                 s2 -= i;
3093                 }
3094         if (b5 > 0) {
3095                 if (leftright) {
3096                         if (m5 > 0) {
3097                                 mhi = pow5mult(mhi, m5);
3098                                 b1 = mult(mhi, b);
3099                                 Bfree(b);
3100                                 b = b1;
3101                                 }
3102                         if (j = b5 - m5)
3103                                 b = pow5mult(b, j);
3104                         }
3105                 else
3106                         b = pow5mult(b, b5);
3107                 }
3108         S = i2b(1);
3109         if (s5 > 0)
3110                 S = pow5mult(S, s5);
3111
3112         /* Check for special case that d is a normalized power of 2. */
3113
3114         spec_case = 0;
3115         if ((mode < 2 || leftright)
3116 #ifdef Honor_FLT_ROUNDS
3117                         && rounding == 1
3118 #endif
3119                                 ) {
3120                 if (!word1(d) && !(word0(d) & Bndry_mask)
3121 #ifndef Sudden_Underflow
3122                  && word0(d) & (Exp_mask & ~Exp_msk1)
3123 #endif
3124                                 ) {
3125                         /* The special case */
3126                         b2 += Log2P;
3127                         s2 += Log2P;
3128                         spec_case = 1;
3129                         }
3130                 }
3131
3132         /* Arrange for convenient computation of quotients:
3133          * shift left if necessary so divisor has 4 leading 0 bits.
3134          *
3135          * Perhaps we should just compute leading 28 bits of S once
3136          * and for all and pass them and a shift to quorem, so it
3137          * can do shifts and ors to compute the numerator for q.
3138          */
3139 #ifdef Pack_32
3140         if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f)
3141                 i = 32 - i;
3142 #else
3143         if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
3144                 i = 16 - i;
3145 #endif
3146         if (i > 4) {
3147                 i -= 4;
3148                 b2 += i;
3149                 m2 += i;
3150                 s2 += i;
3151                 }
3152         else if (i < 4) {
3153                 i += 28;
3154                 b2 += i;
3155                 m2 += i;
3156                 s2 += i;
3157                 }
3158         if (b2 > 0)
3159                 b = lshift(b, b2);
3160         if (s2 > 0)
3161                 S = lshift(S, s2);
3162         if (k_check) {
3163                 if (cmp(b,S) < 0) {
3164                         k--;
3165                         b = multadd(b, 10, 0);  /* we botched the k estimate */
3166                         if (leftright)
3167                                 mhi = multadd(mhi, 10, 0);
3168                         ilim = ilim1;
3169                         }
3170                 }
3171         if (ilim <= 0 && (mode == 3 || mode == 5)) {
3172                 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
3173                         /* no digits, fcvt style */
3174  no_digits:
3175                         k = -1 - ndigits;
3176                         goto ret;
3177                         }
3178  one_digit:
3179                 *s++ = '1';
3180                 k++;
3181                 goto ret;
3182                 }
3183         if (leftright) {
3184                 if (m2 > 0)
3185                         mhi = lshift(mhi, m2);
3186
3187                 /* Compute mlo -- check for special case
3188                  * that d is a normalized power of 2.
3189                  */
3190
3191                 mlo = mhi;
3192                 if (spec_case) {
3193                         mhi = Balloc(mhi->k);
3194                         Bcopy(mhi, mlo);
3195                         mhi = lshift(mhi, Log2P);
3196                         }
3197
3198                 for(i = 1;;i++) {
3199                         dig = quorem(b,S) + '0';
3200                         /* Do we yet have the shortest decimal string
3201                          * that will round to d?
3202                          */
3203                         j = cmp(b, mlo);
3204                         delta = diff(S, mhi);
3205                         j1 = delta->sign ? 1 : cmp(b, delta);
3206                         Bfree(delta);
3207 #ifndef ROUND_BIASED
3208                         if (j1 == 0 && mode != 1 && !(word1(d) & 1)
3209 #ifdef Honor_FLT_ROUNDS
3210                                 && rounding >= 1
3211 #endif
3212                                                                    ) {
3213                                 if (dig == '9')
3214                                         goto round_9_up;
3215                                 if (j > 0)
3216                                         dig++;
3217 #ifdef SET_INEXACT
3218                                 else if (!b->x[0] && b->wds <= 1)
3219                                         inexact = 0;
3220 #endif
3221                                 *s++ = dig;
3222                                 goto ret;
3223                                 }
3224 #endif
3225                         if (j < 0 || j == 0 && mode != 1
3226 #ifndef ROUND_BIASED
3227                                                         && !(word1(d) & 1)
3228 #endif
3229                                         ) {
3230                                 if (!b->x[0] && b->wds <= 1) {
3231 #ifdef SET_INEXACT
3232                                         inexact = 0;
3233 #endif
3234                                         goto accept_dig;
3235                                         }
3236 #ifdef Honor_FLT_ROUNDS
3237                                 if (mode > 1)
3238                                  switch(rounding) {
3239                                   case 0: goto accept_dig;
3240                                   case 2: goto keep_dig;
3241                                   }
3242 #endif /*Honor_FLT_ROUNDS*/
3243                                 if (j1 > 0) {
3244                                         b = lshift(b, 1);
3245                                         j1 = cmp(b, S);
3246                                         if ((j1 > 0 || j1 == 0 && dig & 1)
3247                                         && dig++ == '9')
3248                                                 goto round_9_up;
3249                                         }
3250  accept_dig:
3251                                 *s++ = dig;
3252                                 goto ret;
3253                                 }
3254                         if (j1 > 0) {
3255 #ifdef Honor_FLT_ROUNDS
3256                                 if (!rounding)
3257                                         goto accept_dig;
3258 #endif
3259                                 if (dig == '9') { /* possible if i == 1 */
3260  round_9_up:
3261                                         *s++ = '9';
3262                                         goto roundoff;
3263                                         }
3264                                 *s++ = dig + 1;
3265                                 goto ret;
3266                                 }
3267 #ifdef Honor_FLT_ROUNDS
3268  keep_dig:
3269 #endif
3270                         *s++ = dig;
3271                         if (i == ilim)
3272                                 break;
3273                         b = multadd(b, 10, 0);
3274                         if (mlo == mhi)
3275                                 mlo = mhi = multadd(mhi, 10, 0);
3276                         else {
3277                                 mlo = multadd(mlo, 10, 0);
3278                                 mhi = multadd(mhi, 10, 0);
3279                                 }
3280                         }
3281                 }
3282         else
3283                 for(i = 1;; i++) {
3284                         *s++ = dig = quorem(b,S) + '0';
3285                         if (!b->x[0] && b->wds <= 1) {
3286 #ifdef SET_INEXACT
3287                                 inexact = 0;
3288 #endif
3289                                 goto ret;
3290                                 }
3291                         if (i >= ilim)
3292                                 break;
3293                         b = multadd(b, 10, 0);
3294                         }
3295
3296         /* Round off last digit */
3297
3298 #ifdef Honor_FLT_ROUNDS
3299         switch(rounding) {
3300           case 0: goto trimzeros;
3301           case 2: goto roundoff;
3302           }
3303 #endif
3304         b = lshift(b, 1);
3305         j = cmp(b, S);
3306         if (j > 0 || j == 0 && dig & 1) {
3307  roundoff:
3308                 while(*--s == '9')
3309                         if (s == s0) {
3310                                 k++;
3311                                 *s++ = '1';
3312                                 goto ret;
3313                                 }
3314                 ++*s++;
3315                 }
3316         else {
3317  trimzeros:
3318                 while(*--s == '0');
3319                 s++;
3320                 }
3321  ret:
3322         Bfree(S);
3323         if (mhi) {
3324                 if (mlo && mlo != mhi)
3325                         Bfree(mlo);
3326                 Bfree(mhi);
3327                 }
3328  ret1:
3329 #ifdef SET_INEXACT
3330         if (inexact) {
3331                 if (!oldinexact) {
3332                         word0(d) = Exp_1 + (70 << Exp_shift);
3333                         word1(d) = 0;
3334                         dval(d) += 1.;
3335                         }
3336                 }
3337         else if (!oldinexact)
3338                 clear_inexact();
3339 #endif
3340         Bfree(b);
3341         *s = 0;
3342         *decpt = k + 1;
3343         if (rve)
3344                 *rve = s;
3345         return s0;
3346         }
3347 #endif
3348
3349 #ifdef __cplusplus
3350 }
3351 #endif