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