Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
dtoa.c
Go to the documentation of this file.
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 David M. Gay (dmg at acm dot org,
21 * with " at " changed at "@" and " dot " changed to "."). */
22
23/* On a machine with IEEE extended-precision registers, it is
24 * necessary to specify double-precision (53-bit) rounding precision
25 * before invoking strtod or dtoa. If the machine uses (the equivalent
26 * of) Intel 80x87 arithmetic, the call
27 * _control87(PC_53, MCW_PC);
28 * does this with many compilers. Whether this or another call is
29 * appropriate depends on the compiler; for this to work, it may be
30 * necessary to #include "float.h" or another system-dependent header
31 * file.
32 */
33
34/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
35 * (Note that IEEE arithmetic is disabled by gcc's -ffast-math flag.)
36 *
37 * This strtod returns a nearest machine number to the input decimal
38 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
39 * broken by the IEEE round-even rule. Otherwise ties are broken by
40 * biased rounding (add half and chop).
41 *
42 * Inspired loosely by William D. Clinger's paper "How to Read Floating
43 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
44 *
45 * Modifications:
46 *
47 * 1. We only require IEEE, IBM, or VAX double-precision
48 * arithmetic (not IEEE double-extended).
49 * 2. We get by with floating-point arithmetic in a case that
50 * Clinger missed -- when we're computing d * 10^n
51 * for a small integer d and the integer n is not too
52 * much larger than 22 (the maximum integer k for which
53 * we can represent 10^k exactly), we may be able to
54 * compute (d*10^k) * 10^(e-k) with just one roundoff.
55 * 3. Rather than a bit-at-a-time adjustment of the binary
56 * result in the hard case, we use floating-point
57 * arithmetic to determine the adjustment to within
58 * one bit; only in really hard cases do we need to
59 * compute a second residual.
60 * 4. Because of 3., we don't need a large table of powers of 10
61 * for ten-to-e (just some small tables, e.g. of 10^k
62 * for 0 <= k <= 22).
63 */
64
65/*
66 * #define IEEE_8087 for IEEE-arithmetic machines where the least
67 * significant byte has the lowest address.
68 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
69 * significant byte has the lowest address.
70 * #define Long int on machines with 32-bit ints and 64-bit longs.
71 * #define IBM for IBM mainframe-style floating-point arithmetic.
72 * #define VAX for VAX-style floating-point arithmetic (D_floating).
73 * #define No_leftright to omit left-right logic in fast floating-point
74 * computation of dtoa. This will cause dtoa modes 4 and 5 to be
75 * treated the same as modes 2 and 3 for some inputs.
76 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
77 * and strtod and dtoa should round accordingly. Unless Trust_FLT_ROUNDS
78 * is also #defined, fegetround() will be queried for the rounding mode.
79 * Note that both FLT_ROUNDS and fegetround() are specified by the C99
80 * standard (and are specified to be consistent, with fesetround()
81 * affecting the value of FLT_ROUNDS), but that some (Linux) systems
82 * do not work correctly in this regard, so using fegetround() is more
83 * portable than using FLT_ROUNDS directly.
84 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
85 * and Honor_FLT_ROUNDS is not #defined.
86 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
87 * that use extended-precision instructions to compute rounded
88 * products and quotients) with IBM.
89 * #define ROUND_BIASED for IEEE-format with biased rounding and arithmetic
90 * that rounds toward +Infinity.
91 * #define ROUND_BIASED_without_Round_Up for IEEE-format with biased
92 * rounding when the underlying floating-point arithmetic uses
93 * unbiased rounding. This prevent using ordinary floating-point
94 * arithmetic when the result could be computed with one rounding error.
95 * #define Inaccurate_Divide for IEEE-format with correctly rounded
96 * products but inaccurate quotients, e.g., for Intel i860.
97 * #define NO_LONG_LONG on machines that do not have a "long long"
98 * integer type (of >= 64 bits). On such machines, you can
99 * #define Just_16 to store 16 bits per 32-bit Long when doing
100 * high-precision integer arithmetic. Whether this speeds things
101 * up or slows things down depends on the machine and the number
102 * being converted. If long long is available and the name is
103 * something other than "long long", #define Llong to be the name,
104 * and if "unsigned Llong" does not work as an unsigned version of
105 * Llong, #define #ULLong to be the corresponding unsigned type.
106 * #define KR_headers for old-style C function headers.
107 * #define Bad_float_h if your system lacks a float.h or if it does not
108 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
109 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
110 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
111 * if memory is available and otherwise does something you deem
112 * appropriate. If MALLOC is undefined, malloc will be invoked
113 * directly -- and assumed always to succeed. Similarly, if you
114 * want something other than the system's free() to be called to
115 * recycle memory acquired from MALLOC, #define FREE to be the
116 * name of the alternate routine. (FREE or free is only called in
117 * pathological cases, e.g., in a dtoa call after a dtoa return in
118 * mode 3 with thousands of digits requested.)
119 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
120 * memory allocations from a private pool of memory when possible.
121 * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,
122 * unless #defined to be a different length. This default length
123 * suffices to get rid of MALLOC calls except for unusual cases,
124 * such as decimal-to-binary conversion of a very long string of
125 * digits. The longest string dtoa can return is about 751 bytes
126 * long. For conversions by strtod of strings of 800 digits and
127 * all dtoa conversions in single-threaded executions with 8-byte
128 * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
129 * pointers, PRIVATE_MEM >= 7112 appears adequate.
130 * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK
131 * #defined automatically on IEEE systems. On such systems,
132 * when INFNAN_CHECK is #defined, strtod checks
133 * for Infinity and NaN (case insensitively). On some systems
134 * (e.g., some HP systems), it may be necessary to #define NAN_WORD0
135 * appropriately -- to the most significant word of a quiet NaN.
136 * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
137 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
138 * strtod also accepts (case insensitively) strings of the form
139 * NaN(x), where x is a string of hexadecimal digits and spaces;
140 * if there is only one string of hexadecimal digits, it is taken
141 * for the 52 fraction bits of the resulting NaN; if there are two
142 * or more strings of hex digits, the first is for the high 20 bits,
143 * the second and subsequent for the low 32 bits, with intervening
144 * white space ignored; but if this results in none of the 52
145 * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
146 * and NAN_WORD1 are used instead.
147 * #define MULTIPLE_THREADS if the system offers preemptively scheduled
148 * multiple threads. In this case, you must provide (or suitably
149 * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
150 * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed
151 * in pow5mult, ensures lazy evaluation of only one copy of high
152 * powers of 5; omitting this lock would introduce a small
153 * probability of wasting memory, but would otherwise be harmless.)
154 * You must also invoke freedtoa(s) to free the value s returned by
155 * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.
156 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
157 * avoids underflows on inputs whose result does not underflow.
158 * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
159 * floating-point numbers and flushes underflows to zero rather
160 * than implementing gradual underflow, then you must also #define
161 * Sudden_Underflow.
162 * #define USE_LOCALE to use the current locale's decimal_point value.
163 * #define SET_INEXACT if IEEE arithmetic is being used and extra
164 * computation should be done to set the inexact flag when the
165 * result is inexact and avoid setting inexact when the result
166 * is exact. In this case, dtoa.c must be compiled in
167 * an environment, perhaps provided by #include "dtoa.c" in a
168 * suitable wrapper, that defines two functions,
169 * int get_inexact(void);
170 * void clear_inexact(void);
171 * such that get_inexact() returns a nonzero value if the
172 * inexact bit is already set, and clear_inexact() sets the
173 * inexact bit to 0. When SET_INEXACT is #defined, strtod
174 * also does extra computations to set the underflow and overflow
175 * flags when appropriate (i.e., when the result is tiny and
176 * inexact or when it is a numeric value rounded to +-infinity).
177 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
178 * the result overflows to +-Infinity or underflows to 0.
179 * #define NO_HEX_FP to omit recognition of hexadecimal floating-point
180 * values by strtod.
181 * #define NO_STRTOD_BIGCOMP (on IEEE-arithmetic systems only for now)
182 * to disable logic for "fast" testing of very long input strings
183 * to strtod. This testing proceeds by initially truncating the
184 * input string, then if necessary comparing the whole string with
185 * a decimal expansion to decide close cases. This logic is only
186 * used for input more than STRTOD_DIGLIM digits long (default 40).
187 */
188
189#ifndef Long
190#define Long long
191#endif
192#ifndef ULong
193typedef 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 USE_LOCALE
205#include "locale.h"
206#endif
207
208#ifdef Honor_FLT_ROUNDS
209#ifndef Trust_FLT_ROUNDS
210#include <fenv.h>
211#endif
212#endif
213
214#ifdef MALLOC
215#ifdef KR_headers
216extern char *MALLOC();
217#else
218extern void *MALLOC(size_t);
219#endif
220#else
221#define MALLOC malloc
222#endif
223
224#ifndef Omit_Private_Memory
225#ifndef PRIVATE_MEM
226#define PRIVATE_MEM 2304
227#endif
228#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
229static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
230#endif
231
232#undef IEEE_Arith
233#undef Avoid_Underflow
234#ifdef IEEE_MC68k
235#define IEEE_Arith
236#endif
237#ifdef IEEE_8087
238#define IEEE_Arith
239#endif
240
241#ifdef IEEE_Arith
242#ifndef NO_INFNAN_CHECK
243#undef INFNAN_CHECK
244#define INFNAN_CHECK
245#endif
246#else
247#undef INFNAN_CHECK
248#define NO_STRTOD_BIGCOMP
249#endif
250
251#include "errno.h"
252
253#ifdef Bad_float_h
254
255#ifdef IEEE_Arith
256#define DBL_DIG 15
257#define DBL_MAX_10_EXP 308
258#define DBL_MAX_EXP 1024
259#define FLT_RADIX 2
260#endif /*IEEE_Arith*/
261
262#ifdef IBM
263#define DBL_DIG 16
264#define DBL_MAX_10_EXP 75
265#define DBL_MAX_EXP 63
266#define FLT_RADIX 16
267#define DBL_MAX 7.2370055773322621e+75
268#endif
269
270#ifdef VAX
271#define DBL_DIG 16
272#define DBL_MAX_10_EXP 38
273#define DBL_MAX_EXP 127
274#define FLT_RADIX 2
275#define DBL_MAX 1.7014118346046923e+38
276#endif
277
278#ifndef LONG_MAX
279#define LONG_MAX 2147483647
280#endif
281
282#else /* ifndef Bad_float_h */
283#include "float.h"
284#endif /* Bad_float_h */
285
286#ifndef __MATH_H__
287#include "math.h"
288#endif
289
290#ifdef __cplusplus
291extern "C" {
292#endif
293
294#ifndef CONST
295#ifdef KR_headers
296#define CONST /* blank */
297#else
298#define CONST const
299#endif
300#endif
301
302#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
303Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
304#endif
305
306typedef union { double d; ULong L[2]; } U;
307
308#ifdef IEEE_8087
309#define word0(x) (x)->L[1]
310#define word1(x) (x)->L[0]
311#else
312#define word0(x) (x)->L[0]
313#define word1(x) (x)->L[1]
314#endif
315#define dval(x) (x)->d
316
317#ifndef STRTOD_DIGLIM
318#define STRTOD_DIGLIM 40
319#endif
320
321#ifdef DIGLIM_DEBUG
322extern int strtod_diglim;
323#else
324#define strtod_diglim STRTOD_DIGLIM
325#endif
326
327/* The following definition of Storeinc is appropriate for MIPS processors.
328 * An alternative that might be better on some machines is
329 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
330 */
331#if defined(IEEE_8087) + defined(VAX)
332#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
333((unsigned short *)a)[0] = (unsigned short)c, a++)
334#else
335#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
336((unsigned short *)a)[1] = (unsigned short)c, a++)
337#endif
338
339/* #define P DBL_MANT_DIG */
340/* Ten_pmax = floor(P*log(2)/log(5)) */
341/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
342/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
343/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
344
345#ifdef IEEE_Arith
346#define Exp_shift 20
347#define Exp_shift1 20
348#define Exp_msk1 0x100000
349#define Exp_msk11 0x100000
350#define Exp_mask 0x7ff00000
351#define P 53
352#define Nbits 53
353#define Bias 1023
354#define Emax 1023
355#define Emin (-1022)
356#define Exp_1 0x3ff00000
357#define Exp_11 0x3ff00000
358#define Ebits 11
359#define Frac_mask 0xfffff
360#define Frac_mask1 0xfffff
361#define Ten_pmax 22
362#define Bletch 0x10
363#define Bndry_mask 0xfffff
364#define Bndry_mask1 0xfffff
365#define LSB 1
366#define Sign_bit 0x80000000
367#define Log2P 1
368#define Tiny0 0
369#define Tiny1 1
370#define Quick_max 14
371#define Int_max 14
372#ifndef NO_IEEE_Scale
373#define Avoid_Underflow
374#ifdef Flush_Denorm /* debugging option */
375#undef Sudden_Underflow
376#endif
377#endif
378
379#ifndef Flt_Rounds
380#ifdef FLT_ROUNDS
381#define Flt_Rounds FLT_ROUNDS
382#else
383#define Flt_Rounds 1
384#endif
385#endif /*Flt_Rounds*/
386
387#ifdef Honor_FLT_ROUNDS
388#undef Check_FLT_ROUNDS
389#define Check_FLT_ROUNDS
390#else
391#define Rounding Flt_Rounds
392#endif
393
394#else /* ifndef IEEE_Arith */
395#undef Check_FLT_ROUNDS
396#undef Honor_FLT_ROUNDS
397#undef SET_INEXACT
398#undef Sudden_Underflow
399#define Sudden_Underflow
400#ifdef IBM
401#undef Flt_Rounds
402#define Flt_Rounds 0
403#define Exp_shift 24
404#define Exp_shift1 24
405#define Exp_msk1 0x1000000
406#define Exp_msk11 0x1000000
407#define Exp_mask 0x7f000000
408#define P 14
409#define Nbits 56
410#define Bias 65
411#define Emax 248
412#define Emin (-260)
413#define Exp_1 0x41000000
414#define Exp_11 0x41000000
415#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
416#define Frac_mask 0xffffff
417#define Frac_mask1 0xffffff
418#define Bletch 4
419#define Ten_pmax 22
420#define Bndry_mask 0xefffff
421#define Bndry_mask1 0xffffff
422#define LSB 1
423#define Sign_bit 0x80000000
424#define Log2P 4
425#define Tiny0 0x100000
426#define Tiny1 0
427#define Quick_max 14
428#define Int_max 15
429#else /* VAX */
430#undef Flt_Rounds
431#define Flt_Rounds 1
432#define Exp_shift 23
433#define Exp_shift1 7
434#define Exp_msk1 0x80
435#define Exp_msk11 0x800000
436#define Exp_mask 0x7f80
437#define P 56
438#define Nbits 56
439#define Bias 129
440#define Emax 126
441#define Emin (-129)
442#define Exp_1 0x40800000
443#define Exp_11 0x4080
444#define Ebits 8
445#define Frac_mask 0x7fffff
446#define Frac_mask1 0xffff007f
447#define Ten_pmax 24
448#define Bletch 2
449#define Bndry_mask 0xffff007f
450#define Bndry_mask1 0xffff007f
451#define LSB 0x10000
452#define Sign_bit 0x8000
453#define Log2P 1
454#define Tiny0 0x80
455#define Tiny1 0
456#define Quick_max 15
457#define Int_max 15
458#endif /* IBM, VAX */
459#endif /* IEEE_Arith */
460
461#ifndef IEEE_Arith
462#define ROUND_BIASED
463#else
464#ifdef ROUND_BIASED_without_Round_Up
465#undef ROUND_BIASED
466#define ROUND_BIASED
467#endif
468#endif
469
470#ifdef RND_PRODQUOT
471#define rounded_product(a,b) a = rnd_prod(a, b)
472#define rounded_quotient(a,b) a = rnd_quot(a, b)
473#ifdef KR_headers
474extern double rnd_prod(), rnd_quot();
475#else
476extern double rnd_prod(double, double), rnd_quot(double, double);
477#endif
478#else
479#define rounded_product(a,b) a *= b
480#define rounded_quotient(a,b) a /= b
481#endif
482
483#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
484#define Big1 0xffffffff
485
486#ifndef Pack_32
487#define Pack_32
488#endif
489
490typedef struct BCinfo BCinfo;
493
494#ifdef KR_headers
495#define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
496#else
497#define FFFFFFFF 0xffffffffUL
498#endif
499
500#ifdef NO_LONG_LONG
501#undef ULLong
502#ifdef Just_16
503#undef Pack_32
504/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
505 * This makes some inner loops simpler and sometimes saves work
506 * during multiplications, but it often seems to make things slightly
507 * slower. Hence the default is now to store 32 bits per Long.
508 */
509#endif
510#else /* long long available */
511#ifndef Llong
512#define Llong long long
513#endif
514#ifndef ULLong
515#define ULLong unsigned Llong
516#endif
517#endif /* NO_LONG_LONG */
518
519#ifndef MULTIPLE_THREADS
520#define ACQUIRE_DTOA_LOCK(n) /*nothing*/
521#define FREE_DTOA_LOCK(n) /*nothing*/
522#endif
523
524#define Kmax 7
525
526#ifdef __cplusplus
527extern "C" double strtod(const char *s00, char **se);
528extern "C" char *dtoa(double d, int mode, int ndigits,
529 int *decpt, int *sign, char **rve);
530#endif
531
532 struct
533Bigint {
534 struct Bigint *next;
535 int k, maxwds, sign, wds;
537 };
538
539 typedef struct Bigint Bigint;
540
541 static Bigint *freelist[Kmax+1];
542
543 static Bigint *
544Balloc
545#ifdef KR_headers
546 (k) int k;
547#else
548 (int k)
549#endif
550{
551 int x;
552 Bigint *rv;
553#ifndef Omit_Private_Memory
554 unsigned int len;
555#endif
556
558 /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */
559 /* but this case seems very unlikely. */
560 if (k <= Kmax && (rv = freelist[k]))
561 freelist[k] = rv->next;
562 else {
563 x = 1 << k;
564#ifdef Omit_Private_Memory
565 rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
566#else
567 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
568 /sizeof(double);
569 if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) {
570 rv = (Bigint*)pmem_next;
571 pmem_next += len;
572 }
573 else
574 rv = (Bigint*)MALLOC(len*sizeof(double));
575#endif
576 rv->k = k;
577 rv->maxwds = x;
578 }
580 rv->sign = rv->wds = 0;
581 return rv;
582 }
583
584 static void
585Bfree
586#ifdef KR_headers
587 (v) Bigint *v;
588#else
589 (Bigint *v)
590#endif
591{
592 if (v) {
593 if (v->k > Kmax)
594#ifdef FREE
595 FREE((void*)v);
596#else
597 free((void*)v);
598#endif
599 else {
601 v->next = freelist[v->k];
602 freelist[v->k] = v;
604 }
605 }
606 }
607
608#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
609y->wds*sizeof(Long) + 2*sizeof(int))
610
611 static Bigint *
612multadd
613#ifdef KR_headers
614 (b, m, a) Bigint *b; int m, a;
615#else
616 (Bigint *b, int m, int a) /* multiply by m and add a */
617#endif
618{
619 int i, wds;
620#ifdef ULLong
621 ULong *x;
622 ULLong carry, y;
623#else
624 ULong carry, *x, y;
625#ifdef Pack_32
626 ULong xi, z;
627#endif
628#endif
629 Bigint *b1;
630
631 wds = b->wds;
632 x = b->x;
633 i = 0;
634 carry = a;
635 do {
636#ifdef ULLong
637 y = *x * (ULLong)m + carry;
638 carry = y >> 32;
639 *x++ = y & FFFFFFFF;
640#else
641#ifdef Pack_32
642 xi = *x;
643 y = (xi & 0xffff) * m + carry;
644 z = (xi >> 16) * m + (y >> 16);
645 carry = z >> 16;
646 *x++ = (z << 16) + (y & 0xffff);
647#else
648 y = *x * m + carry;
649 carry = y >> 16;
650 *x++ = y & 0xffff;
651#endif
652#endif
653 }
654 while(++i < wds);
655 if (carry) {
656 if (wds >= b->maxwds) {
657 b1 = Balloc(b->k+1);
658 Bcopy(b1, b);
659 Bfree(b);
660 b = b1;
661 }
662 b->x[wds++] = carry;
663 b->wds = wds;
664 }
665 return b;
666 }
667
668 static Bigint *
669s2b
670#ifdef KR_headers
671 (s, nd0, nd, y9, dplen) CONST char *s; int nd0, nd, dplen; ULong y9;
672#else
673 (const char *s, int nd0, int nd, ULong y9, int dplen)
674#endif
675{
676 Bigint *b;
677 int i, k;
678 Long x, y;
679
680 x = (nd + 8) / 9;
681 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
682#ifdef Pack_32
683 b = Balloc(k);
684 b->x[0] = y9;
685 b->wds = 1;
686#else
687 b = Balloc(k+1);
688 b->x[0] = y9 & 0xffff;
689 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
690#endif
691
692 i = 9;
693 if (9 < nd0) {
694 s += 9;
695 do b = multadd(b, 10, *s++ - '0');
696 while(++i < nd0);
697 s += dplen;
698 }
699 else
700 s += dplen + 9;
701 for(; i < nd; i++)
702 b = multadd(b, 10, *s++ - '0');
703 return b;
704 }
705
706 static int
707hi0bits
708#ifdef KR_headers
709 (x) ULong x;
710#else
711 (ULong x)
712#endif
713{
714 int k = 0;
715
716 if (!(x & 0xffff0000)) {
717 k = 16;
718 x <<= 16;
719 }
720 if (!(x & 0xff000000)) {
721 k += 8;
722 x <<= 8;
723 }
724 if (!(x & 0xf0000000)) {
725 k += 4;
726 x <<= 4;
727 }
728 if (!(x & 0xc0000000)) {
729 k += 2;
730 x <<= 2;
731 }
732 if (!(x & 0x80000000)) {
733 k++;
734 if (!(x & 0x40000000))
735 return 32;
736 }
737 return k;
738 }
739
740 static int
741lo0bits
742#ifdef KR_headers
743 (y) ULong *y;
744#else
745 (ULong *y)
746#endif
747{
748 int k;
749 ULong x = *y;
750
751 if (x & 7) {
752 if (x & 1)
753 return 0;
754 if (x & 2) {
755 *y = x >> 1;
756 return 1;
757 }
758 *y = x >> 2;
759 return 2;
760 }
761 k = 0;
762 if (!(x & 0xffff)) {
763 k = 16;
764 x >>= 16;
765 }
766 if (!(x & 0xff)) {
767 k += 8;
768 x >>= 8;
769 }
770 if (!(x & 0xf)) {
771 k += 4;
772 x >>= 4;
773 }
774 if (!(x & 0x3)) {
775 k += 2;
776 x >>= 2;
777 }
778 if (!(x & 1)) {
779 k++;
780 x >>= 1;
781 if (!x)
782 return 32;
783 }
784 *y = x;
785 return k;
786 }
787
788 static Bigint *
789i2b
790#ifdef KR_headers
791 (i) int i;
792#else
793 (int i)
794#endif
795{
796 Bigint *b;
797
798 b = Balloc(1);
799 b->x[0] = i;
800 b->wds = 1;
801 return b;
802 }
803
804 static Bigint *
805mult
806#ifdef KR_headers
807 (a, b) Bigint *a, *b;
808#else
809 (Bigint *a, Bigint *b)
810#endif
811{
812 Bigint *c;
813 int k, wa, wb, wc;
814 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
815 ULong y;
816#ifdef ULLong
817 ULLong carry, z;
818#else
819 ULong carry, z;
820#ifdef Pack_32
821 ULong z2;
822#endif
823#endif
824
825 if (a->wds < b->wds) {
826 c = a;
827 a = b;
828 b = c;
829 }
830 k = a->k;
831 wa = a->wds;
832 wb = b->wds;
833 wc = wa + wb;
834 if (wc > a->maxwds)
835 k++;
836 c = Balloc(k);
837 for(x = c->x, xa = x + wc; x < xa; x++)
838 *x = 0;
839 xa = a->x;
840 xae = xa + wa;
841 xb = b->x;
842 xbe = xb + wb;
843 xc0 = c->x;
844#ifdef ULLong
845 for(; xb < xbe; xc0++) {
846 if ((y = *xb++)) {
847 x = xa;
848 xc = xc0;
849 carry = 0;
850 do {
851 z = *x++ * (ULLong)y + *xc + carry;
852 carry = z >> 32;
853 *xc++ = z & FFFFFFFF;
854 }
855 while(x < xae);
856 *xc = carry;
857 }
858 }
859#else
860#ifdef Pack_32
861 for(; xb < xbe; xb++, xc0++) {
862 if (y = *xb & 0xffff) {
863 x = xa;
864 xc = xc0;
865 carry = 0;
866 do {
867 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
868 carry = z >> 16;
869 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
870 carry = z2 >> 16;
871 Storeinc(xc, z2, z);
872 }
873 while(x < xae);
874 *xc = carry;
875 }
876 if (y = *xb >> 16) {
877 x = xa;
878 xc = xc0;
879 carry = 0;
880 z2 = *xc;
881 do {
882 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
883 carry = z >> 16;
884 Storeinc(xc, z, z2);
885 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
886 carry = z2 >> 16;
887 }
888 while(x < xae);
889 *xc = z2;
890 }
891 }
892#else
893 for(; xb < xbe; xc0++) {
894 if (y = *xb++) {
895 x = xa;
896 xc = xc0;
897 carry = 0;
898 do {
899 z = *x++ * y + *xc + carry;
900 carry = z >> 16;
901 *xc++ = z & 0xffff;
902 }
903 while(x < xae);
904 *xc = carry;
905 }
906 }
907#endif
908#endif
909 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
910 c->wds = wc;
911 return c;
912 }
913
914 static Bigint *p5s;
915
916 static Bigint *
917pow5mult
918#ifdef KR_headers
919 (b, k) Bigint *b; int k;
920#else
921 (Bigint *b, int k)
922#endif
923{
924 Bigint *b1, *p5, *p51;
925 int i;
926 static int p05[3] = { 5, 25, 125 };
927
928 if ((i = k & 3))
929 b = multadd(b, p05[i-1], 0);
930
931 if (!(k >>= 2))
932 return b;
933 if (!(p5 = p5s)) {
934 /* first time */
935#ifdef MULTIPLE_THREADS
937 if (!(p5 = p5s)) {
938 p5 = p5s = i2b(625);
939 p5->next = 0;
940 }
942#else
943 p5 = p5s = i2b(625);
944 p5->next = 0;
945#endif
946 }
947 for(;;) {
948 if (k & 1) {
949 b1 = mult(b, p5);
950 Bfree(b);
951 b = b1;
952 }
953 if (!(k >>= 1))
954 break;
955 if (!(p51 = p5->next)) {
956#ifdef MULTIPLE_THREADS
958 if (!(p51 = p5->next)) {
959 p51 = p5->next = mult(p5,p5);
960 p51->next = 0;
961 }
963#else
964 p51 = p5->next = mult(p5,p5);
965 p51->next = 0;
966#endif
967 }
968 p5 = p51;
969 }
970 return b;
971 }
972
973 static Bigint *
974lshift
975#ifdef KR_headers
976 (b, k) Bigint *b; int k;
977#else
978 (Bigint *b, int k)
979#endif
980{
981 int i, k1, n, n1;
982 Bigint *b1;
983 ULong *x, *x1, *xe, z;
984
985#ifdef Pack_32
986 n = k >> 5;
987#else
988 n = k >> 4;
989#endif
990 k1 = b->k;
991 n1 = n + b->wds + 1;
992 for(i = b->maxwds; n1 > i; i <<= 1)
993 k1++;
994 b1 = Balloc(k1);
995 x1 = b1->x;
996 for(i = 0; i < n; i++)
997 *x1++ = 0;
998 x = b->x;
999 xe = x + b->wds;
1000#ifdef Pack_32
1001 if (k &= 0x1f) {
1002 k1 = 32 - k;
1003 z = 0;
1004 do {
1005 *x1++ = *x << k | z;
1006 z = *x++ >> k1;
1007 }
1008 while(x < xe);
1009 if ((*x1 = z))
1010 ++n1;
1011 }
1012#else
1013 if (k &= 0xf) {
1014 k1 = 16 - k;
1015 z = 0;
1016 do {
1017 *x1++ = *x << k & 0xffff | z;
1018 z = *x++ >> k1;
1019 }
1020 while(x < xe);
1021 if (*x1 = z)
1022 ++n1;
1023 }
1024#endif
1025 else do
1026 *x1++ = *x++;
1027 while(x < xe);
1028 b1->wds = n1 - 1;
1029 Bfree(b);
1030 return b1;
1031 }
1032
1033 static int
1034cmp
1035#ifdef KR_headers
1036 (a, b) Bigint *a, *b;
1037#else
1038 (Bigint *a, Bigint *b)
1039#endif
1040{
1041 ULong *xa, *xa0, *xb, *xb0;
1042 int i, j;
1043
1044 i = a->wds;
1045 j = b->wds;
1046#ifdef DEBUG
1047 if (i > 1 && !a->x[i-1])
1048 Bug("cmp called with a->x[a->wds-1] == 0");
1049 if (j > 1 && !b->x[j-1])
1050 Bug("cmp called with b->x[b->wds-1] == 0");
1051#endif
1052 if (i -= j)
1053 return i;
1054 xa0 = a->x;
1055 xa = xa0 + j;
1056 xb0 = b->x;
1057 xb = xb0 + j;
1058 for(;;) {
1059 if (*--xa != *--xb)
1060 return *xa < *xb ? -1 : 1;
1061 if (xa <= xa0)
1062 break;
1063 }
1064 return 0;
1065 }
1066
1067 static Bigint *
1068diff
1069#ifdef KR_headers
1070 (a, b) Bigint *a, *b;
1071#else
1072 (Bigint *a, Bigint *b)
1073#endif
1074{
1075 Bigint *c;
1076 int i, wa, wb;
1077 ULong *xa, *xae, *xb, *xbe, *xc;
1078#ifdef ULLong
1079 ULLong borrow, y;
1080#else
1081 ULong borrow, y;
1082#ifdef Pack_32
1083 ULong z;
1084#endif
1085#endif
1086
1087 i = cmp(a,b);
1088 if (!i) {
1089 c = Balloc(0);
1090 c->wds = 1;
1091 c->x[0] = 0;
1092 return c;
1093 }
1094 if (i < 0) {
1095 c = a;
1096 a = b;
1097 b = c;
1098 i = 1;
1099 }
1100 else
1101 i = 0;
1102 c = Balloc(a->k);
1103 c->sign = i;
1104 wa = a->wds;
1105 xa = a->x;
1106 xae = xa + wa;
1107 wb = b->wds;
1108 xb = b->x;
1109 xbe = xb + wb;
1110 xc = c->x;
1111 borrow = 0;
1112#ifdef ULLong
1113 do {
1114 y = (ULLong)*xa++ - *xb++ - borrow;
1115 borrow = y >> 32 & (ULong)1;
1116 *xc++ = y & FFFFFFFF;
1117 }
1118 while(xb < xbe);
1119 while(xa < xae) {
1120 y = *xa++ - borrow;
1121 borrow = y >> 32 & (ULong)1;
1122 *xc++ = y & FFFFFFFF;
1123 }
1124#else
1125#ifdef Pack_32
1126 do {
1127 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
1128 borrow = (y & 0x10000) >> 16;
1129 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
1130 borrow = (z & 0x10000) >> 16;
1131 Storeinc(xc, z, y);
1132 }
1133 while(xb < xbe);
1134 while(xa < xae) {
1135 y = (*xa & 0xffff) - borrow;
1136 borrow = (y & 0x10000) >> 16;
1137 z = (*xa++ >> 16) - borrow;
1138 borrow = (z & 0x10000) >> 16;
1139 Storeinc(xc, z, y);
1140 }
1141#else
1142 do {
1143 y = *xa++ - *xb++ - borrow;
1144 borrow = (y & 0x10000) >> 16;
1145 *xc++ = y & 0xffff;
1146 }
1147 while(xb < xbe);
1148 while(xa < xae) {
1149 y = *xa++ - borrow;
1150 borrow = (y & 0x10000) >> 16;
1151 *xc++ = y & 0xffff;
1152 }
1153#endif
1154#endif
1155 while(!*--xc)
1156 wa--;
1157 c->wds = wa;
1158 return c;
1159 }
1160
1161 static double
1162ulp
1163#ifdef KR_headers
1164 (x) U *x;
1165#else
1166 (U *x)
1167#endif
1168{
1169 Long L;
1170 U u;
1171
1172 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1173#ifndef Avoid_Underflow
1174#ifndef Sudden_Underflow
1175 if (L > 0) {
1176#endif
1177#endif
1178#ifdef IBM
1179 L |= Exp_msk1 >> 4;
1180#endif
1181 word0(&u) = L;
1182 word1(&u) = 0;
1183#ifndef Avoid_Underflow
1184#ifndef Sudden_Underflow
1185 }
1186 else {
1187 L = -L >> Exp_shift;
1188 if (L < Exp_shift) {
1189 word0(&u) = 0x80000 >> L;
1190 word1(&u) = 0;
1191 }
1192 else {
1193 word0(&u) = 0;
1194 L -= Exp_shift;
1195 word1(&u) = L >= 31 ? 1 : 1 << 31 - L;
1196 }
1197 }
1198#endif
1199#endif
1200 return dval(&u);
1201 }
1202
1203 static double
1204b2d
1205#ifdef KR_headers
1206 (a, e) Bigint *a; int *e;
1207#else
1208 (Bigint *a, int *e)
1209#endif
1210{
1211 ULong *xa, *xa0, w, y, z;
1212 int k;
1213 U d;
1214#ifdef VAX
1215 ULong d0, d1;
1216#else
1217#define d0 word0(&d)
1218#define d1 word1(&d)
1219#endif
1220
1221 xa0 = a->x;
1222 xa = xa0 + a->wds;
1223 y = *--xa;
1224#ifdef DEBUG
1225 if (!y) Bug("zero y in b2d");
1226#endif
1227 k = hi0bits(y);
1228 *e = 32 - k;
1229#ifdef Pack_32
1230 if (k < Ebits) {
1231 d0 = Exp_1 | y >> (Ebits - k);
1232 w = xa > xa0 ? *--xa : 0;
1233 d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
1234 goto ret_d;
1235 }
1236 z = xa > xa0 ? *--xa : 0;
1237 if (k -= Ebits) {
1238 d0 = Exp_1 | y << k | z >> (32 - k);
1239 y = xa > xa0 ? *--xa : 0;
1240 d1 = z << k | y >> (32 - k);
1241 }
1242 else {
1243 d0 = Exp_1 | y;
1244 d1 = z;
1245 }
1246#else
1247 if (k < Ebits + 16) {
1248 z = xa > xa0 ? *--xa : 0;
1249 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1250 w = xa > xa0 ? *--xa : 0;
1251 y = xa > xa0 ? *--xa : 0;
1252 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1253 goto ret_d;
1254 }
1255 z = xa > xa0 ? *--xa : 0;
1256 w = xa > xa0 ? *--xa : 0;
1257 k -= Ebits + 16;
1258 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1259 y = xa > xa0 ? *--xa : 0;
1260 d1 = w << k + 16 | y << k;
1261#endif
1262 ret_d:
1263#ifdef VAX
1264 word0(&d) = d0 >> 16 | d0 << 16;
1265 word1(&d) = d1 >> 16 | d1 << 16;
1266#else
1267#undef d0
1268#undef d1
1269#endif
1270 return dval(&d);
1271 }
1272
1273 static Bigint *
1274d2b
1275#ifdef KR_headers
1276 (d, e, bits) U *d; int *e, *bits;
1277#else
1278 (U *d, int *e, int *bits)
1279#endif
1280{
1281 Bigint *b;
1282 int de, k;
1283 ULong *x, y, z;
1284#ifndef Sudden_Underflow
1285 int i;
1286#endif
1287#ifdef VAX
1288 ULong d0, d1;
1289 d0 = word0(d) >> 16 | word0(d) << 16;
1290 d1 = word1(d) >> 16 | word1(d) << 16;
1291#else
1292#define d0 word0(d)
1293#define d1 word1(d)
1294#endif
1295
1296#ifdef Pack_32
1297 b = Balloc(1);
1298#else
1299 b = Balloc(2);
1300#endif
1301 x = b->x;
1302
1303 z = d0 & Frac_mask;
1304 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1305#ifdef Sudden_Underflow
1306 de = (int)(d0 >> Exp_shift);
1307#ifndef IBM
1308 z |= Exp_msk11;
1309#endif
1310#else
1311 if ((de = (int)(d0 >> Exp_shift)))
1312 z |= Exp_msk1;
1313#endif
1314#ifdef Pack_32
1315 if ((y = d1)) {
1316 if ((k = lo0bits(&y))) {
1317 x[0] = y | z << (32 - k);
1318 z >>= k;
1319 }
1320 else
1321 x[0] = y;
1322#ifndef Sudden_Underflow
1323 i =
1324#endif
1325 b->wds = (x[1] = z) ? 2 : 1;
1326 }
1327 else {
1328 k = lo0bits(&z);
1329 x[0] = z;
1330#ifndef Sudden_Underflow
1331 i =
1332#endif
1333 b->wds = 1;
1334 k += 32;
1335 }
1336#else
1337 if (y = d1) {
1338 if (k = lo0bits(&y))
1339 if (k >= 16) {
1340 x[0] = y | z << 32 - k & 0xffff;
1341 x[1] = z >> k - 16 & 0xffff;
1342 x[2] = z >> k;
1343 i = 2;
1344 }
1345 else {
1346 x[0] = y & 0xffff;
1347 x[1] = y >> 16 | z << 16 - k & 0xffff;
1348 x[2] = z >> k & 0xffff;
1349 x[3] = z >> k+16;
1350 i = 3;
1351 }
1352 else {
1353 x[0] = y & 0xffff;
1354 x[1] = y >> 16;
1355 x[2] = z & 0xffff;
1356 x[3] = z >> 16;
1357 i = 3;
1358 }
1359 }
1360 else {
1361#ifdef DEBUG
1362 if (!z)
1363 Bug("Zero passed to d2b");
1364#endif
1365 k = lo0bits(&z);
1366 if (k >= 16) {
1367 x[0] = z;
1368 i = 0;
1369 }
1370 else {
1371 x[0] = z & 0xffff;
1372 x[1] = z >> 16;
1373 i = 1;
1374 }
1375 k += 32;
1376 }
1377 while(!x[i])
1378 --i;
1379 b->wds = i + 1;
1380#endif
1381#ifndef Sudden_Underflow
1382 if (de) {
1383#endif
1384#ifdef IBM
1385 *e = (de - Bias - (P-1) << 2) + k;
1386 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1387#else
1388 *e = de - Bias - (P-1) + k;
1389 *bits = P - k;
1390#endif
1391#ifndef Sudden_Underflow
1392 }
1393 else {
1394 *e = de - Bias - (P-1) + 1 + k;
1395#ifdef Pack_32
1396 *bits = 32*i - hi0bits(x[i-1]);
1397#else
1398 *bits = (i+2)*16 - hi0bits(x[i]);
1399#endif
1400 }
1401#endif
1402 return b;
1403 }
1404#undef d0
1405#undef d1
1406
1407 static double
1408ratio
1409#ifdef KR_headers
1410 (a, b) Bigint *a, *b;
1411#else
1412 (Bigint *a, Bigint *b)
1413#endif
1414{
1415 U da, db;
1416 int k, ka, kb;
1417
1418 dval(&da) = b2d(a, &ka);
1419 dval(&db) = b2d(b, &kb);
1420#ifdef Pack_32
1421 k = ka - kb + 32*(a->wds - b->wds);
1422#else
1423 k = ka - kb + 16*(a->wds - b->wds);
1424#endif
1425#ifdef IBM
1426 if (k > 0) {
1427 word0(&da) += (k >> 2)*Exp_msk1;
1428 if (k &= 3)
1429 dval(&da) *= 1 << k;
1430 }
1431 else {
1432 k = -k;
1433 word0(&db) += (k >> 2)*Exp_msk1;
1434 if (k &= 3)
1435 dval(&db) *= 1 << k;
1436 }
1437#else
1438 if (k > 0)
1439 word0(&da) += k*Exp_msk1;
1440 else {
1441 k = -k;
1442 word0(&db) += k*Exp_msk1;
1443 }
1444#endif
1445 return dval(&da) / dval(&db);
1446 }
1447
1448 static CONST double
1449tens[] = {
1450 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1451 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1452 1e20, 1e21, 1e22
1453#ifdef VAX
1454 , 1e23, 1e24
1455#endif
1456 };
1457
1458 static CONST double
1459#ifdef IEEE_Arith
1460bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1461static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1462#ifdef Avoid_Underflow
1463 9007199254740992.*9007199254740992.e-256
1464 /* = 2^106 * 1e-256 */
1465#else
1466 1e-256
1467#endif
1468 };
1469/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1470/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1471#define Scale_Bit 0x10
1472#define n_bigtens 5
1473#else
1474#ifdef IBM
1475bigtens[] = { 1e16, 1e32, 1e64 };
1476static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1477#define n_bigtens 3
1478#else
1479bigtens[] = { 1e16, 1e32 };
1480static CONST double tinytens[] = { 1e-16, 1e-32 };
1481#define n_bigtens 2
1482#endif
1483#endif
1484
1485#undef Need_Hexdig
1486#ifdef INFNAN_CHECK
1487#ifndef No_Hex_NaN
1488#define Need_Hexdig
1489#endif
1490#endif
1491
1492#ifndef Need_Hexdig
1493#ifndef NO_HEX_FP
1494#define Need_Hexdig
1495#endif
1496#endif
1497
1498#ifdef Need_Hexdig /*{*/
1499#if 0
1500static unsigned char hexdig[256];
1501
1502 static void
1503htinit(unsigned char *h, unsigned char *s, int inc)
1504{
1505 int i, j;
1506 for(i = 0; (j = s[i]) !=0; i++)
1507 h[j] = i + inc;
1508 }
1509
1510 static void
1511hexdig_init(void) /* Use of hexdig_init omitted 20121220 to avoid a */
1512 /* race condition when multiple threads are used. */
1513{
1514#define USC (unsigned char *)
1515 htinit(hexdig, USC "0123456789", 0x10);
1516 htinit(hexdig, USC "abcdef", 0x10 + 10);
1517 htinit(hexdig, USC "ABCDEF", 0x10 + 10);
1518 }
1519#else
1520static unsigned char hexdig[256] = {
1521 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1522 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1523 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1524 16,17,18,19,20,21,22,23,24,25,0,0,0,0,0,0,
1525 0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
1526 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1527 0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
1528 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1529 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1530 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1531 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1532 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1533 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1534 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1535 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1536 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1537 };
1538#endif
1539#endif /* } Need_Hexdig */
1540
1541#ifdef INFNAN_CHECK
1542
1543#ifndef NAN_WORD0
1544#define NAN_WORD0 0x7ff80000
1545#endif
1546
1547#ifndef NAN_WORD1
1548#define NAN_WORD1 0
1549#endif
1550
1551 static int
1552match
1553#ifdef KR_headers
1554 (sp, t) char **sp, *t;
1555#else
1556 (const char **sp, const char *t)
1557#endif
1558{
1559 int c, d;
1560 CONST char *s = *sp;
1561
1562 while((d = *t++)) {
1563 if ((c = *++s) >= 'A' && c <= 'Z')
1564 c += 'a' - 'A';
1565 if (c != d)
1566 return 0;
1567 }
1568 *sp = s + 1;
1569 return 1;
1570 }
1571
1572#ifndef No_Hex_NaN
1573 static void
1574hexnan
1575#ifdef KR_headers
1576 (rvp, sp) U *rvp; CONST char **sp;
1577#else
1578 (U *rvp, const char **sp)
1579#endif
1580{
1581 ULong c, x[2];
1582 CONST char *s;
1583 int c1, havedig, udx0, xshift;
1584
1585 /**** if (!hexdig['0']) hexdig_init(); ****/
1586 x[0] = x[1] = 0;
1587 havedig = xshift = 0;
1588 udx0 = 1;
1589 s = *sp;
1590 /* allow optional initial 0x or 0X */
1591 while((c = *(CONST unsigned char*)(s+1)) && c <= ' ')
1592 ++s;
1593 if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X'))
1594 s += 2;
1595 while((c = *(CONST unsigned char*)++s)) {
1596 if ((c1 = hexdig[c]))
1597 c = c1 & 0xf;
1598 else if (c <= ' ') {
1599 if (udx0 && havedig) {
1600 udx0 = 0;
1601 xshift = 1;
1602 }
1603 continue;
1604 }
1605#ifdef GDTOA_NON_PEDANTIC_NANCHECK
1606 else if (/*(*/ c == ')' && havedig) {
1607 *sp = s + 1;
1608 break;
1609 }
1610 else
1611 return; /* invalid form: don't change *sp */
1612#else
1613 else {
1614 do {
1615 if (/*(*/ c == ')') {
1616 *sp = s + 1;
1617 break;
1618 }
1619 } while((c = *++s));
1620 break;
1621 }
1622#endif
1623 havedig = 1;
1624 if (xshift) {
1625 xshift = 0;
1626 x[0] = x[1];
1627 x[1] = 0;
1628 }
1629 if (udx0)
1630 x[0] = (x[0] << 4) | (x[1] >> 28);
1631 x[1] = (x[1] << 4) | c;
1632 }
1633 if ((x[0] &= 0xfffff) || x[1]) {
1634 word0(rvp) = Exp_mask | x[0];
1635 word1(rvp) = x[1];
1636 }
1637 }
1638#endif /*No_Hex_NaN*/
1639#endif /* INFNAN_CHECK */
1640
1641#ifdef Pack_32
1642#define ULbits 32
1643#define kshift 5
1644#define kmask 31
1645#else
1646#define ULbits 16
1647#define kshift 4
1648#define kmask 15
1649#endif
1650
1651#if !defined(NO_HEX_FP) || defined(Honor_FLT_ROUNDS) /*{*/
1652 static Bigint *
1653#ifdef KR_headers
1654increment(b) Bigint *b;
1655#else
1656increment(Bigint *b)
1657#endif
1658{
1659 ULong *x, *xe;
1660 Bigint *b1;
1661
1662 x = b->x;
1663 xe = x + b->wds;
1664 do {
1665 if (*x < (ULong)0xffffffffL) {
1666 ++*x;
1667 return b;
1668 }
1669 *x++ = 0;
1670 } while(x < xe);
1671 {
1672 if (b->wds >= b->maxwds) {
1673 b1 = Balloc(b->k+1);
1674 Bcopy(b1,b);
1675 Bfree(b);
1676 b = b1;
1677 }
1678 b->x[b->wds++] = 1;
1679 }
1680 return b;
1681 }
1682
1683#endif /*}*/
1684
1685#ifndef NO_HEX_FP /*{*/
1686
1687 static void
1688#ifdef KR_headers
1689rshift(b, k) Bigint *b; int k;
1690#else
1691rshift(Bigint *b, int k)
1692#endif
1693{
1694 ULong *x, *x1, *xe, y;
1695 int n;
1696
1697 x = x1 = b->x;
1698 n = k >> kshift;
1699 if (n < b->wds) {
1700 xe = x + b->wds;
1701 x += n;
1702 if (k &= kmask) {
1703 n = 32 - k;
1704 y = *x++ >> k;
1705 while(x < xe) {
1706 *x1++ = (y | (*x << n)) & 0xffffffff;
1707 y = *x++ >> k;
1708 }
1709 if ((*x1 = y) !=0)
1710 x1++;
1711 }
1712 else
1713 while(x < xe)
1714 *x1++ = *x++;
1715 }
1716 if ((b->wds = x1 - b->x) == 0)
1717 b->x[0] = 0;
1718 }
1719
1720 static ULong
1721#ifdef KR_headers
1722any_on(b, k) Bigint *b; int k;
1723#else
1724any_on(Bigint *b, int k)
1725#endif
1726{
1727 int n, nwds;
1728 ULong *x, *x0, x1, x2;
1729
1730 x = b->x;
1731 nwds = b->wds;
1732 n = k >> kshift;
1733 if (n > nwds)
1734 n = nwds;
1735 else if (n < nwds && (k &= kmask)) {
1736 x1 = x2 = x[n];
1737 x1 >>= k;
1738 x1 <<= k;
1739 if (x1 != x2)
1740 return 1;
1741 }
1742 x0 = x;
1743 x += n;
1744 while(x > x0)
1745 if (*--x)
1746 return 1;
1747 return 0;
1748 }
1749
1750enum { /* rounding values: same as FLT_ROUNDS */
1754 Round_down = 3
1756
1757 void
1758#ifdef KR_headers
1759gethex(sp, rvp, rounding, sign)
1760 CONST char **sp; U *rvp; int rounding, sign;
1761#else
1762gethex( CONST char **sp, U *rvp, int rounding, int sign)
1763#endif
1764{
1765 Bigint *b;
1766 CONST unsigned char *decpt, *s0, *s, *s1;
1767 Long e, e1;
1768 ULong L, lostbits, *x;
1769 int big, denorm, esign, havedig, k, n, nbits, up, zret;
1770#ifdef IBM
1771 int j;
1772#endif
1773 enum {
1774#ifdef IEEE_Arith /*{{*/
1775 emax = 0x7fe - Bias - P + 1,
1776 emin = Emin - P + 1
1777#else /*}{*/
1778 emin = Emin - P,
1779#ifdef VAX
1780 emax = 0x7ff - Bias - P + 1
1781#endif
1782#ifdef IBM
1783 emax = 0x7f - Bias - P
1784#endif
1785#endif /*}}*/
1786 };
1787#ifdef USE_LOCALE
1788 int i;
1789#ifdef NO_LOCALE_CACHE
1790 const unsigned char *decimalpoint = (unsigned char*)
1791 localeconv()->decimal_point;
1792#else
1793 const unsigned char *decimalpoint;
1794 static unsigned char *decimalpoint_cache;
1795 if (!(s0 = decimalpoint_cache)) {
1796 s0 = (unsigned char*)localeconv()->decimal_point;
1797 if ((decimalpoint_cache = (unsigned char*)
1798 MALLOC(strlen((CONST char*)s0) + 1))) {
1799 strcpy((char*)decimalpoint_cache, (CONST char*)s0);
1800 s0 = decimalpoint_cache;
1801 }
1802 }
1803 decimalpoint = s0;
1804#endif
1805#endif
1806
1807 /**** if (!hexdig['0']) hexdig_init(); ****/
1808 havedig = 0;
1809 s0 = *(CONST unsigned char **)sp + 2;
1810 while(s0[havedig] == '0')
1811 havedig++;
1812 s0 += havedig;
1813 s = s0;
1814 decpt = 0;
1815 zret = 0;
1816 e = 0;
1817 if (hexdig[*s])
1818 havedig++;
1819 else {
1820 zret = 1;
1821#ifdef USE_LOCALE
1822 for(i = 0; decimalpoint[i]; ++i) {
1823 if (s[i] != decimalpoint[i])
1824 goto pcheck;
1825 }
1826 decpt = s += i;
1827#else
1828 if (*s != '.')
1829 goto pcheck;
1830 decpt = ++s;
1831#endif
1832 if (!hexdig[*s])
1833 goto pcheck;
1834 while(*s == '0')
1835 s++;
1836 if (hexdig[*s])
1837 zret = 0;
1838 havedig = 1;
1839 s0 = s;
1840 }
1841 while(hexdig[*s])
1842 s++;
1843#ifdef USE_LOCALE
1844 if (*s == *decimalpoint && !decpt) {
1845 for(i = 1; decimalpoint[i]; ++i) {
1846 if (s[i] != decimalpoint[i])
1847 goto pcheck;
1848 }
1849 decpt = s += i;
1850#else
1851 if (*s == '.' && !decpt) {
1852 decpt = ++s;
1853#endif
1854 while(hexdig[*s])
1855 s++;
1856 }/*}*/
1857 if (decpt)
1858 e = -(((Long)(s-decpt)) << 2);
1859 pcheck:
1860 s1 = s;
1861 big = esign = 0;
1862 switch(*s) {
1863 case 'p':
1864 case 'P':
1865 switch(*++s) {
1866 case '-':
1867 esign = 1;
1868 /* no break */
1869 case '+':
1870 s++;
1871 }
1872 if ((n = hexdig[*s]) == 0 || n > 0x19) {
1873 s = s1;
1874 break;
1875 }
1876 e1 = n - 0x10;
1877 while((n = hexdig[*++s]) !=0 && n <= 0x19) {
1878 if (e1 & 0xf8000000)
1879 big = 1;
1880 e1 = 10*e1 + n - 0x10;
1881 }
1882 if (esign)
1883 e1 = -e1;
1884 e += e1;
1885 }
1886 *sp = (char*)s;
1887 if (!havedig)
1888 *sp = (char*)s0 - 1;
1889 if (zret)
1890 goto retz1;
1891 if (big) {
1892 if (esign) {
1893#ifdef IEEE_Arith
1894 switch(rounding) {
1895 case Round_up:
1896 if (sign)
1897 break;
1898 goto ret_tiny;
1899 case Round_down:
1900 if (!sign)
1901 break;
1902 goto ret_tiny;
1903 }
1904#endif
1905 goto retz;
1906#ifdef IEEE_Arith
1907 ret_tinyf:
1908 Bfree(b);
1909 ret_tiny:
1910#ifndef NO_ERRNO
1911 errno = ERANGE;
1912#endif
1913 word0(rvp) = 0;
1914 word1(rvp) = 1;
1915 return;
1916#endif /* IEEE_Arith */
1917 }
1918 switch(rounding) {
1919 case Round_near:
1920 goto ovfl1;
1921 case Round_up:
1922 if (!sign)
1923 goto ovfl1;
1924 goto ret_big;
1925 case Round_down:
1926 if (sign)
1927 goto ovfl1;
1928 goto ret_big;
1929 }
1930 ret_big:
1931 word0(rvp) = Big0;
1932 word1(rvp) = Big1;
1933 return;
1934 }
1935 n = s1 - s0 - 1;
1936 for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1)
1937 k++;
1938 b = Balloc(k);
1939 x = b->x;
1940 n = 0;
1941 L = 0;
1942#ifdef USE_LOCALE
1943 for(i = 0; decimalpoint[i+1]; ++i);
1944#endif
1945 while(s1 > s0) {
1946#ifdef USE_LOCALE
1947 if (*--s1 == decimalpoint[i]) {
1948 s1 -= i;
1949 continue;
1950 }
1951#else
1952 if (*--s1 == '.')
1953 continue;
1954#endif
1955 if (n == ULbits) {
1956 *x++ = L;
1957 L = 0;
1958 n = 0;
1959 }
1960 L |= (hexdig[*s1] & 0x0f) << n;
1961 n += 4;
1962 }
1963 *x++ = L;
1964 b->wds = n = x - b->x;
1965 n = ULbits*n - hi0bits(L);
1966 nbits = Nbits;
1967 lostbits = 0;
1968 x = b->x;
1969 if (n > nbits) {
1970 n -= nbits;
1971 if (any_on(b,n)) {
1972 lostbits = 1;
1973 k = n - 1;
1974 if (x[k>>kshift] & 1 << (k & kmask)) {
1975 lostbits = 2;
1976 if (k > 0 && any_on(b,k))
1977 lostbits = 3;
1978 }
1979 }
1980 rshift(b, n);
1981 e += n;
1982 }
1983 else if (n < nbits) {
1984 n = nbits - n;
1985 b = lshift(b, n);
1986 e -= n;
1987 x = b->x;
1988 }
1989 if (e > Emax) {
1990 ovfl:
1991 Bfree(b);
1992 ovfl1:
1993#ifndef NO_ERRNO
1994 errno = ERANGE;
1995#endif
1996 word0(rvp) = Exp_mask;
1997 word1(rvp) = 0;
1998 return;
1999 }
2000 denorm = 0;
2001 if (e < emin) {
2002 denorm = 1;
2003 n = emin - e;
2004 if (n >= nbits) {
2005#ifdef IEEE_Arith /*{*/
2006 switch (rounding) {
2007 case Round_near:
2008 if (n == nbits && (n < 2 || any_on(b,n-1)))
2009 goto ret_tinyf;
2010 break;
2011 case Round_up:
2012 if (!sign)
2013 goto ret_tinyf;
2014 break;
2015 case Round_down:
2016 if (sign)
2017 goto ret_tinyf;
2018 }
2019#endif /* } IEEE_Arith */
2020 Bfree(b);
2021 retz:
2022#ifndef NO_ERRNO
2023 errno = ERANGE;
2024#endif
2025 retz1:
2026 rvp->d = 0.;
2027 return;
2028 }
2029 k = n - 1;
2030 if (lostbits)
2031 lostbits = 1;
2032 else if (k > 0)
2033 lostbits = any_on(b,k);
2034 if (x[k>>kshift] & 1 << (k & kmask))
2035 lostbits |= 2;
2036 nbits -= n;
2037 rshift(b,n);
2038 e = emin;
2039 }
2040 if (lostbits) {
2041 up = 0;
2042 switch(rounding) {
2043 case Round_zero:
2044 break;
2045 case Round_near:
2046 if (lostbits & 2
2047 && (lostbits & 1) | (x[0] & 1))
2048 up = 1;
2049 break;
2050 case Round_up:
2051 up = 1 - sign;
2052 break;
2053 case Round_down:
2054 up = sign;
2055 }
2056 if (up) {
2057 k = b->wds;
2058 b = increment(b);
2059 x = b->x;
2060 if (denorm) {
2061#if 0
2062 if (nbits == Nbits - 1
2063 && x[nbits >> kshift] & 1 << (nbits & kmask))
2064 denorm = 0; /* not currently used */
2065#endif
2066 }
2067 else if (b->wds > k
2068 || ((n = nbits & kmask) !=0
2069 && hi0bits(x[k-1]) < 32-n)) {
2070 rshift(b,1);
2071 if (++e > Emax)
2072 goto ovfl;
2073 }
2074 }
2075 }
2076#ifdef IEEE_Arith
2077 if (denorm)
2078 word0(rvp) = b->wds > 1 ? b->x[1] & ~0x100000 : 0;
2079 else
2080 word0(rvp) = (b->x[1] & ~0x100000) | ((e + 0x3ff + 52) << 20);
2081 word1(rvp) = b->x[0];
2082#endif
2083#ifdef IBM
2084 if ((j = e & 3)) {
2085 k = b->x[0] & ((1 << j) - 1);
2086 rshift(b,j);
2087 if (k) {
2088 switch(rounding) {
2089 case Round_up:
2090 if (!sign)
2091 increment(b);
2092 break;
2093 case Round_down:
2094 if (sign)
2095 increment(b);
2096 break;
2097 case Round_near:
2098 j = 1 << (j-1);
2099 if (k & j && ((k & (j-1)) | lostbits))
2100 increment(b);
2101 }
2102 }
2103 }
2104 e >>= 2;
2105 word0(rvp) = b->x[1] | ((e + 65 + 13) << 24);
2106 word1(rvp) = b->x[0];
2107#endif
2108#ifdef VAX
2109 /* The next two lines ignore swap of low- and high-order 2 bytes. */
2110 /* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */
2111 /* word1(rvp) = b->x[0]; */
2112 word0(rvp) = ((b->x[1] & ~0x800000) >> 16) | ((e + 129 + 55) << 7) | (b->x[1] << 16);
2113 word1(rvp) = (b->x[0] >> 16) | (b->x[0] << 16);
2114#endif
2115 Bfree(b);
2116 }
2117#endif
2119 static int
2120#ifdef KR_headers
2121dshift(b, p2) Bigint *b; int p2;
2122#else
2123dshift(Bigint *b, int p2)
2124#endif
2125{
2126 int rv = hi0bits(b->x[b->wds-1]) - 4;
2127 if (p2 > 0)
2128 rv -= p2;
2129 return rv & kmask;
2130 }
2131
2132 static int
2133quorem
2134#ifdef KR_headers
2135 (b, S) Bigint *b, *S;
2136#else
2137 (Bigint *b, Bigint *S)
2138#endif
2139{
2140 int n;
2141 ULong *bx, *bxe, q, *sx, *sxe;
2142#ifdef ULLong
2143 ULLong borrow, carry, y, ys;
2144#else
2145 ULong borrow, carry, y, ys;
2146#ifdef Pack_32
2147 ULong si, z, zs;
2148#endif
2149#endif
2150
2151 n = S->wds;
2152#ifdef DEBUG
2153 /*debug*/ if (b->wds > n)
2154 /*debug*/ Bug("oversize b in quorem");
2155#endif
2156 if (b->wds < n)
2157 return 0;
2158 sx = S->x;
2159 sxe = sx + --n;
2160 bx = b->x;
2161 bxe = bx + n;
2162 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
2163#ifdef DEBUG
2164#ifdef NO_STRTOD_BIGCOMP
2165 /*debug*/ if (q > 9)
2166#else
2167 /* An oversized q is possible when quorem is called from bigcomp and */
2168 /* the input is near, e.g., twice the smallest denormalized number. */
2169 /*debug*/ if (q > 15)
2170#endif
2171 /*debug*/ Bug("oversized quotient in quorem");
2172#endif
2173 if (q) {
2174 borrow = 0;
2175 carry = 0;
2176 do {
2177#ifdef ULLong
2178 ys = *sx++ * (ULLong)q + carry;
2179 carry = ys >> 32;
2180 y = *bx - (ys & FFFFFFFF) - borrow;
2181 borrow = y >> 32 & (ULong)1;
2182 *bx++ = y & FFFFFFFF;
2183#else
2184#ifdef Pack_32
2185 si = *sx++;
2186 ys = (si & 0xffff) * q + carry;
2187 zs = (si >> 16) * q + (ys >> 16);
2188 carry = zs >> 16;
2189 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2190 borrow = (y & 0x10000) >> 16;
2191 z = (*bx >> 16) - (zs & 0xffff) - borrow;
2192 borrow = (z & 0x10000) >> 16;
2193 Storeinc(bx, z, y);
2194#else
2195 ys = *sx++ * q + carry;
2196 carry = ys >> 16;
2197 y = *bx - (ys & 0xffff) - borrow;
2198 borrow = (y & 0x10000) >> 16;
2199 *bx++ = y & 0xffff;
2200#endif
2201#endif
2202 }
2203 while(sx <= sxe);
2204 if (!*bxe) {
2205 bx = b->x;
2206 while(--bxe > bx && !*bxe)
2207 --n;
2208 b->wds = n;
2209 }
2210 }
2211 if (cmp(b, S) >= 0) {
2212 q++;
2213 borrow = 0;
2214 carry = 0;
2215 bx = b->x;
2216 sx = S->x;
2217 do {
2218#ifdef ULLong
2219 ys = *sx++ + carry;
2220 carry = ys >> 32;
2221 y = *bx - (ys & FFFFFFFF) - borrow;
2222 borrow = y >> 32 & (ULong)1;
2223 *bx++ = y & FFFFFFFF;
2224#else
2225#ifdef Pack_32
2226 si = *sx++;
2227 ys = (si & 0xffff) + carry;
2228 zs = (si >> 16) + (ys >> 16);
2229 carry = zs >> 16;
2230 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2231 borrow = (y & 0x10000) >> 16;
2232 z = (*bx >> 16) - (zs & 0xffff) - borrow;
2233 borrow = (z & 0x10000) >> 16;
2234 Storeinc(bx, z, y);
2235#else
2236 ys = *sx++ + carry;
2237 carry = ys >> 16;
2238 y = *bx - (ys & 0xffff) - borrow;
2239 borrow = (y & 0x10000) >> 16;
2240 *bx++ = y & 0xffff;
2241#endif
2242#endif
2243 }
2244 while(sx <= sxe);
2245 bx = b->x;
2246 bxe = bx + n;
2247 if (!*bxe) {
2248 while(--bxe > bx && !*bxe)
2249 --n;
2250 b->wds = n;
2251 }
2252 }
2253 return q;
2254 }
2255
2256#if defined(Avoid_Underflow) || !defined(NO_STRTOD_BIGCOMP) /*{*/
2257 static double
2258sulp
2259#ifdef KR_headers
2260 (x, bc) U *x; BCinfo *bc;
2261#else
2262 (U *x, BCinfo *bc)
2263#endif
2264{
2265 U u;
2266 double rv;
2267 int i;
2268
2269 rv = ulp(x);
2270 if (!bc->scale || (i = 2*P + 1 - ((word0(x) & Exp_mask) >> Exp_shift)) <= 0)
2271 return rv; /* Is there an example where i <= 0 ? */
2272 word0(&u) = Exp_1 + (i << Exp_shift);
2273 word1(&u) = 0;
2274 return rv * u.d;
2275 }
2276#endif /*}*/
2277
2278#ifndef NO_STRTOD_BIGCOMP
2279 static void
2280bigcomp
2281#ifdef KR_headers
2282 (rv, s0, bc)
2283 U *rv; CONST char *s0; BCinfo *bc;
2284#else
2285 (U *rv, const char *s0, BCinfo *bc)
2286#endif
2287{
2288 Bigint *b, *d;
2289 int b2, bbits, d2, dd, dig, dsign, i, j, nd, nd0, p2, p5, speccase;
2290
2291 dsign = bc->dsign;
2292 nd = bc->nd;
2293 nd0 = bc->nd0;
2294 p5 = nd + bc->e0 - 1;
2295 speccase = 0;
2296#ifndef Sudden_Underflow
2297 if (rv->d == 0.) { /* special case: value near underflow-to-zero */
2298 /* threshold was rounded to zero */
2299 b = i2b(1);
2300 p2 = Emin - P + 1;
2301 bbits = 1;
2302#ifdef Avoid_Underflow
2303 word0(rv) = (P+2) << Exp_shift;
2304#else
2305 word1(rv) = 1;
2306#endif
2307 i = 0;
2308#ifdef Honor_FLT_ROUNDS
2309 if (bc->rounding == 1)
2310#endif
2311 {
2312 speccase = 1;
2313 --p2;
2314 dsign = 0;
2315 goto have_i;
2316 }
2317 }
2318 else
2319#endif
2320 b = d2b(rv, &p2, &bbits);
2321#ifdef Avoid_Underflow
2322 p2 -= bc->scale;
2323#endif
2324 /* floor(log2(rv)) == bbits - 1 + p2 */
2325 /* Check for denormal case. */
2326 i = P - bbits;
2327 if (i > (j = P - Emin - 1 + p2)) {
2328#ifdef Sudden_Underflow
2329 Bfree(b);
2330 b = i2b(1);
2331 p2 = Emin;
2332 i = P - 1;
2333#ifdef Avoid_Underflow
2334 word0(rv) = (1 + bc->scale) << Exp_shift;
2335#else
2336 word0(rv) = Exp_msk1;
2337#endif
2338 word1(rv) = 0;
2339#else
2340 i = j;
2341#endif
2342 }
2343#ifdef Honor_FLT_ROUNDS
2344 if (bc->rounding != 1) {
2345 if (i > 0)
2346 b = lshift(b, i);
2347 if (dsign)
2348 b = increment(b);
2349 }
2350 else
2351#endif
2352 {
2353 b = lshift(b, ++i);
2354 b->x[0] |= 1;
2355 }
2356#ifndef Sudden_Underflow
2357 have_i:
2358#endif
2359 p2 -= p5 + i;
2360 d = i2b(1);
2361 /* Arrange for convenient computation of quotients:
2362 * shift left if necessary so divisor has 4 leading 0 bits.
2363 */
2364 if (p5 > 0)
2365 d = pow5mult(d, p5);
2366 else if (p5 < 0)
2367 b = pow5mult(b, -p5);
2368 if (p2 > 0) {
2369 b2 = p2;
2370 d2 = 0;
2371 }
2372 else {
2373 b2 = 0;
2374 d2 = -p2;
2375 }
2376 i = dshift(d, d2);
2377 if ((b2 += i) > 0)
2378 b = lshift(b, b2);
2379 if ((d2 += i) > 0)
2380 d = lshift(d, d2);
2381
2382 /* Now b/d = exactly half-way between the two floating-point values */
2383 /* on either side of the input string. Compute first digit of b/d. */
2384
2385 if (!(dig = quorem(b,d))) {
2386 b = multadd(b, 10, 0); /* very unlikely */
2387 dig = quorem(b,d);
2388 }
2389
2390 /* Compare b/d with s0 */
2391
2392 for(i = 0; i < nd0; ) {
2393 if ((dd = s0[i++] - '0' - dig))
2394 goto ret;
2395 if (!b->x[0] && b->wds == 1) {
2396 if (i < nd)
2397 dd = 1;
2398 goto ret;
2399 }
2400 b = multadd(b, 10, 0);
2401 dig = quorem(b,d);
2402 }
2403 for(j = bc->dp1; i++ < nd;) {
2404 if ((dd = s0[j++] - '0' - dig))
2405 goto ret;
2406 if (!b->x[0] && b->wds == 1) {
2407 if (i < nd)
2408 dd = 1;
2409 goto ret;
2410 }
2411 b = multadd(b, 10, 0);
2412 dig = quorem(b,d);
2413 }
2414 if (dig > 0 || b->x[0] || b->wds > 1)
2415 dd = -1;
2416 ret:
2417 Bfree(b);
2418 Bfree(d);
2419#ifdef Honor_FLT_ROUNDS
2420 if (bc->rounding != 1) {
2421 if (dd < 0) {
2422 if (bc->rounding == 0) {
2423 if (!dsign)
2424 goto retlow1;
2425 }
2426 else if (dsign)
2427 goto rethi1;
2428 }
2429 else if (dd > 0) {
2430 if (bc->rounding == 0) {
2431 if (dsign)
2432 goto rethi1;
2433 goto ret1;
2434 }
2435 if (!dsign)
2436 goto rethi1;
2437 dval(rv) += 2.*sulp(rv,bc);
2438 }
2439 else {
2440 bc->inexact = 0;
2441 if (dsign)
2442 goto rethi1;
2443 }
2444 }
2445 else
2446#endif
2447 if (speccase) {
2448 if (dd <= 0)
2449 rv->d = 0.;
2450 }
2451 else if (dd < 0) {
2452 if (!dsign) /* does not happen for round-near */
2453retlow1:
2454 dval(rv) -= sulp(rv,bc);
2455 }
2456 else if (dd > 0) {
2457 if (dsign) {
2458 rethi1:
2459 dval(rv) += sulp(rv,bc);
2460 }
2461 }
2462 else {
2463 /* Exact half-way case: apply round-even rule. */
2464 if ((j = ((word0(rv) & Exp_mask) >> Exp_shift) - bc->scale) <= 0) {
2465 i = 1 - j;
2466 if (i <= 31) {
2467 if (word1(rv) & (0x1 << i))
2468 goto odd;
2469 }
2470 else if (word0(rv) & (0x1 << (i-32)))
2471 goto odd;
2472 }
2473 else if (word1(rv) & 1) {
2474 odd:
2475 if (dsign)
2476 goto rethi1;
2477 goto retlow1;
2478 }
2479 }
2480
2481#ifdef Honor_FLT_ROUNDS
2482 ret1:
2483#endif
2484 return;
2485 }
2486#endif /* NO_STRTOD_BIGCOMP */
2487
2488 double
2490#ifdef KR_headers
2491 (s00, se) CONST char *s00; char **se;
2492#else
2493 (const char *s00, char **se)
2494#endif
2495{
2496 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1;
2497 int esign, i, j, k, nd, nd0, nf, nz, nz0, nz1, sign;
2498 CONST char *s, *s0, *s1;
2499 double aadj, aadj1;
2500 Long L;
2501 U aadj2, adj, rv, rv0;
2502 ULong y, z;
2503 BCinfo bc;
2504 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
2505#ifdef Avoid_Underflow
2506 ULong Lsb, Lsb1;
2507#endif
2508#ifdef SET_INEXACT
2509 int oldinexact;
2510#endif
2511#ifndef NO_STRTOD_BIGCOMP
2512 int req_bigcomp = 0;
2513#endif
2514#ifdef Honor_FLT_ROUNDS /*{*/
2515#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
2516 bc.rounding = Flt_Rounds;
2517#else /*}{*/
2518 bc.rounding = 1;
2519 switch(fegetround()) {
2520 case FE_TOWARDZERO: bc.rounding = 0; break;
2521 case FE_UPWARD: bc.rounding = 2; break;
2522 case FE_DOWNWARD: bc.rounding = 3;
2523 }
2524#endif /*}}*/
2525#endif /*}*/
2526#ifdef USE_LOCALE
2527 CONST char *s2;
2528#endif
2529
2530 sign = nz0 = nz1 = nz = bc.dplen = bc.uflchk = 0;
2531 dval(&rv) = 0.;
2532 for(s = s00;;s++) switch(*s) {
2533 case '-':
2534 sign = 1;
2535 /* no break */
2536 case '+':
2537 if (*++s)
2538 goto break2;
2539 /* no break */
2540 case 0:
2541 goto ret0;
2542 case '\t':
2543 case '\n':
2544 case '\v':
2545 case '\f':
2546 case '\r':
2547 case ' ':
2548 continue;
2549 default:
2550 goto break2;
2551 }
2552 break2:
2553 if (*s == '0') {
2554#ifndef NO_HEX_FP /*{*/
2555 switch(s[1]) {
2556 case 'x':
2557 case 'X':
2558#ifdef Honor_FLT_ROUNDS
2559 gethex(&s, &rv, bc.rounding, sign);
2560#else
2561 gethex(&s, &rv, 1, sign);
2562#endif
2563 goto ret;
2564 }
2565#endif /*}*/
2566 nz0 = 1;
2567 while(*++s == '0') ;
2568 if (!*s)
2569 goto ret;
2570 }
2571 s0 = s;
2572 y = z = 0;
2573 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
2574 if (nd < 9)
2575 y = 10*y + c - '0';
2576 else if (nd < DBL_DIG + 2)
2577 z = 10*z + c - '0';
2578 nd0 = nd;
2579 bc.dp0 = bc.dp1 = s - s0;
2580 for(s1 = s; s1 > s0 && *--s1 == '0'; )
2581 ++nz1;
2582#ifdef USE_LOCALE
2583 s1 = localeconv()->decimal_point;
2584 if (c == *s1) {
2585 c = '.';
2586 if (*++s1) {
2587 s2 = s;
2588 for(;;) {
2589 if (*++s2 != *s1) {
2590 c = 0;
2591 break;
2592 }
2593 if (!*++s1) {
2594 s = s2;
2595 break;
2596 }
2597 }
2598 }
2599 }
2600#endif
2601 if (c == '.') {
2602 c = *++s;
2603 bc.dp1 = s - s0;
2604 bc.dplen = bc.dp1 - bc.dp0;
2605 if (!nd) {
2606 for(; c == '0'; c = *++s)
2607 nz++;
2608 if (c > '0' && c <= '9') {
2609 bc.dp0 = s0 - s;
2610 bc.dp1 = bc.dp0 + bc.dplen;
2611 s0 = s;
2612 nf += nz;
2613 nz = 0;
2614 goto have_dig;
2615 }
2616 goto dig_done;
2617 }
2618 for(; c >= '0' && c <= '9'; c = *++s) {
2619 have_dig:
2620 nz++;
2621 if (c -= '0') {
2622 nf += nz;
2623 for(i = 1; i < nz; i++)
2624 if (nd++ < 9)
2625 y *= 10;
2626 else if (nd <= DBL_DIG + 2)
2627 z *= 10;
2628 if (nd++ < 9)
2629 y = 10*y + c;
2630 else if (nd <= DBL_DIG + 2)
2631 z = 10*z + c;
2632 nz = nz1 = 0;
2633 }
2634 }
2635 }
2636 dig_done:
2637 e = 0;
2638 if (c == 'e' || c == 'E') {
2639 if (!nd && !nz && !nz0) {
2640 goto ret0;
2641 }
2642 s00 = s;
2643 esign = 0;
2644 switch(c = *++s) {
2645 case '-':
2646 esign = 1;
2647 case '+':
2648 c = *++s;
2649 }
2650 if (c >= '0' && c <= '9') {
2651 while(c == '0')
2652 c = *++s;
2653 if (c > '0' && c <= '9') {
2654 L = c - '0';
2655 s1 = s;
2656 while((c = *++s) >= '0' && c <= '9')
2657 L = 10*L + c - '0';
2658 if (s - s1 > 8 || L > 19999)
2659 /* Avoid confusion from exponents
2660 * so large that e might overflow.
2661 */
2662 e = 19999; /* safe for 16 bit ints */
2663 else
2664 e = (int)L;
2665 if (esign)
2666 e = -e;
2667 }
2668 else
2669 e = 0;
2670 }
2671 else
2672 s = s00;
2673 }
2674 if (!nd) {
2675 if (!nz && !nz0) {
2676#ifdef INFNAN_CHECK
2677 /* Check for Nan and Infinity */
2678 if (!bc.dplen)
2679 switch(c) {
2680 case 'i':
2681 case 'I':
2682 if (match(&s,"nf")) {
2683 --s;
2684 if (!match(&s,"inity"))
2685 ++s;
2686 word0(&rv) = 0x7ff00000;
2687 word1(&rv) = 0;
2688 goto ret;
2689 }
2690 break;
2691 case 'n':
2692 case 'N':
2693 if (match(&s, "an")) {
2694 word0(&rv) = NAN_WORD0;
2695 word1(&rv) = NAN_WORD1;
2696#ifndef No_Hex_NaN
2697 if (*s == '(') /*)*/
2698 hexnan(&rv, &s);
2699#endif
2700 goto ret;
2701 }
2702 }
2703#endif /* INFNAN_CHECK */
2704 ret0:
2705 s = s00;
2706 sign = 0;
2707 }
2708 goto ret;
2709 }
2710 bc.e0 = e1 = e -= nf;
2711
2712 /* Now we have nd0 digits, starting at s0, followed by a
2713 * decimal point, followed by nd-nd0 digits. The number we're
2714 * after is the integer represented by those digits times
2715 * 10**e */
2716
2717 if (!nd0)
2718 nd0 = nd;
2719 k = nd < DBL_DIG + 2 ? nd : DBL_DIG + 2;
2720 dval(&rv) = y;
2721 if (k > 9) {
2722#ifdef SET_INEXACT
2723 if (k > DBL_DIG)
2724 oldinexact = get_inexact();
2725#endif
2726 dval(&rv) = tens[k - 9] * dval(&rv) + z;
2727 }
2728 bd0 = 0;
2729 if (nd <= DBL_DIG
2730#ifndef RND_PRODQUOT
2731#ifndef Honor_FLT_ROUNDS
2732 && Flt_Rounds == 1
2733#endif
2734#endif
2735 ) {
2736 if (!e)
2737 goto ret;
2738#ifndef ROUND_BIASED_without_Round_Up
2739 if (e > 0) {
2740 if (e <= Ten_pmax) {
2741#ifdef VAX
2742 goto vax_ovfl_check;
2743#else
2744#ifdef Honor_FLT_ROUNDS
2745 /* round correctly FLT_ROUNDS = 2 or 3 */
2746 if (sign) {
2747 rv.d = -rv.d;
2748 sign = 0;
2749 }
2750#endif
2751 /* rv = */ rounded_product(dval(&rv), tens[e]);
2752 goto ret;
2753#endif
2754 }
2755 i = DBL_DIG - nd;
2756 if (e <= Ten_pmax + i) {
2757 /* A fancier test would sometimes let us do
2758 * this for larger i values.
2759 */
2760#ifdef Honor_FLT_ROUNDS
2761 /* round correctly FLT_ROUNDS = 2 or 3 */
2762 if (sign) {
2763 rv.d = -rv.d;
2764 sign = 0;
2765 }
2766#endif
2767 e -= i;
2768 dval(&rv) *= tens[i];
2769#ifdef VAX
2770 /* VAX exponent range is so narrow we must
2771 * worry about overflow here...
2772 */
2773 vax_ovfl_check:
2774 word0(&rv) -= P*Exp_msk1;
2775 /* rv = */ rounded_product(dval(&rv), tens[e]);
2776 if ((word0(&rv) & Exp_mask)
2777 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
2778 goto ovfl;
2779 word0(&rv) += P*Exp_msk1;
2780#else
2781 /* rv = */ rounded_product(dval(&rv), tens[e]);
2782#endif
2783 goto ret;
2784 }
2785 }
2786#ifndef Inaccurate_Divide
2787 else if (e >= -Ten_pmax) {
2788#ifdef Honor_FLT_ROUNDS
2789 /* round correctly FLT_ROUNDS = 2 or 3 */
2790 if (sign) {
2791 rv.d = -rv.d;
2792 sign = 0;
2793 }
2794#endif
2795 /* rv = */ rounded_quotient(dval(&rv), tens[-e]);
2796 goto ret;
2797 }
2798#endif
2799#endif /* ROUND_BIASED_without_Round_Up */
2800 }
2801 e1 += nd - k;
2802
2803#ifdef IEEE_Arith
2804#ifdef SET_INEXACT
2805 bc.inexact = 1;
2806 if (k <= DBL_DIG)
2807 oldinexact = get_inexact();
2808#endif
2809#ifdef Avoid_Underflow
2810 bc.scale = 0;
2811#endif
2812#ifdef Honor_FLT_ROUNDS
2813 if (bc.rounding >= 2) {
2814 if (sign)
2815 bc.rounding = bc.rounding == 2 ? 0 : 2;
2816 else
2817 if (bc.rounding != 2)
2818 bc.rounding = 0;
2819 }
2820#endif
2821#endif /*IEEE_Arith*/
2822
2823 /* Get starting approximation = rv * 10**e1 */
2824
2825 if (e1 > 0) {
2826 if ((i = e1 & 15))
2827 dval(&rv) *= tens[i];
2828 if (e1 &= ~15) {
2829 if (e1 > DBL_MAX_10_EXP) {
2830 ovfl:
2831 /* Can't trust HUGE_VAL */
2832#ifdef IEEE_Arith
2833#ifdef Honor_FLT_ROUNDS
2834 switch(bc.rounding) {
2835 case 0: /* toward 0 */
2836 case 3: /* toward -infinity */
2837 word0(&rv) = Big0;
2838 word1(&rv) = Big1;
2839 break;
2840 default:
2841 word0(&rv) = Exp_mask;
2842 word1(&rv) = 0;
2843 }
2844#else /*Honor_FLT_ROUNDS*/
2845 word0(&rv) = Exp_mask;
2846 word1(&rv) = 0;
2847#endif /*Honor_FLT_ROUNDS*/
2848#ifdef SET_INEXACT
2849 /* set overflow bit */
2850 dval(&rv0) = 1e300;
2851 dval(&rv0) *= dval(&rv0);
2852#endif
2853#else /*IEEE_Arith*/
2854 word0(&rv) = Big0;
2855 word1(&rv) = Big1;
2856#endif /*IEEE_Arith*/
2857 range_err:
2858 if (bd0) {
2859 Bfree(bb);
2860 Bfree(bd);
2861 Bfree(bs);
2862 Bfree(bd0);
2863 Bfree(delta);
2864 }
2865#ifndef NO_ERRNO
2866 errno = ERANGE;
2867#endif
2868 goto ret;
2869 }
2870 e1 >>= 4;
2871 for(j = 0; e1 > 1; j++, e1 >>= 1)
2872 if (e1 & 1)
2873 dval(&rv) *= bigtens[j];
2874 /* The last multiplication could overflow. */
2875 word0(&rv) -= P*Exp_msk1;
2876 dval(&rv) *= bigtens[j];
2877 if ((z = word0(&rv) & Exp_mask)
2878 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
2879 goto ovfl;
2880 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
2881 /* set to largest number */
2882 /* (Can't trust DBL_MAX) */
2883 word0(&rv) = Big0;
2884 word1(&rv) = Big1;
2885 }
2886 else
2887 word0(&rv) += P*Exp_msk1;
2888 }
2889 }
2890 else if (e1 < 0) {
2891 e1 = -e1;
2892 if ((i = e1 & 15))
2893 dval(&rv) /= tens[i];
2894 if (e1 >>= 4) {
2895 if (e1 >= 1 << n_bigtens)
2896 goto undfl;
2897#ifdef Avoid_Underflow
2898 if (e1 & Scale_Bit)
2899 bc.scale = 2*P;
2900 for(j = 0; e1 > 0; j++, e1 >>= 1)
2901 if (e1 & 1)
2902 dval(&rv) *= tinytens[j];
2903 if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
2904 >> Exp_shift)) > 0) {
2905 /* scaled rv is denormal; clear j low bits */
2906 if (j >= 32) {
2907 if (j > 54)
2908 goto undfl;
2909 word1(&rv) = 0;
2910 if (j >= 53)
2911 word0(&rv) = (P+2)*Exp_msk1;
2912 else
2913 word0(&rv) &= 0xffffffff << (j-32);
2914 }
2915 else
2916 word1(&rv) &= 0xffffffff << j;
2917 }
2918#else
2919 for(j = 0; e1 > 1; j++, e1 >>= 1)
2920 if (e1 & 1)
2921 dval(&rv) *= tinytens[j];
2922 /* The last multiplication could underflow. */
2923 dval(&rv0) = dval(&rv);
2924 dval(&rv) *= tinytens[j];
2925 if (!dval(&rv)) {
2926 dval(&rv) = 2.*dval(&rv0);
2927 dval(&rv) *= tinytens[j];
2928#endif
2929 if (!dval(&rv)) {
2930 undfl:
2931 dval(&rv) = 0.;
2932 goto range_err;
2933 }
2934#ifndef Avoid_Underflow
2935 word0(&rv) = Tiny0;
2936 word1(&rv) = Tiny1;
2937 /* The refinement below will clean
2938 * this approximation up.
2939 */
2940 }
2941#endif
2942 }
2943 }
2944
2945 /* Now the hard part -- adjusting rv to the correct value.*/
2946
2947 /* Put digits into bd: true value = bd * 10^e */
2948
2949 bc.nd = nd - nz1;
2950#ifndef NO_STRTOD_BIGCOMP
2951 bc.nd0 = nd0; /* Only needed if nd > strtod_diglim, but done here */
2952 /* to silence an erroneous warning about bc.nd0 */
2953 /* possibly not being initialized. */
2954 if (nd > strtod_diglim) {
2955 /* ASSERT(strtod_diglim >= 18); 18 == one more than the */
2956 /* minimum number of decimal digits to distinguish double values */
2957 /* in IEEE arithmetic. */
2958 i = j = 18;
2959 if (i > nd0)
2960 j += bc.dplen;
2961 for(;;) {
2962 if (--j < bc.dp1 && j >= bc.dp0)
2963 j = bc.dp0 - 1;
2964 if (s0[j] != '0')
2965 break;
2966 --i;
2967 }
2968 e += nd - i;
2969 nd = i;
2970 if (nd0 > nd)
2971 nd0 = nd;
2972 if (nd < 9) { /* must recompute y */
2973 y = 0;
2974 for(i = 0; i < nd0; ++i)
2975 y = 10*y + s0[i] - '0';
2976 for(j = bc.dp1; i < nd; ++i)
2977 y = 10*y + s0[j++] - '0';
2978 }
2979 }
2980#endif
2981 bd0 = s2b(s0, nd0, nd, y, bc.dplen);
2982
2983 for(;;) {
2984 bd = Balloc(bd0->k);
2985 Bcopy(bd, bd0);
2986 bb = d2b(&rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
2987 bs = i2b(1);
2988
2989 if (e >= 0) {
2990 bb2 = bb5 = 0;
2991 bd2 = bd5 = e;
2992 }
2993 else {
2994 bb2 = bb5 = -e;
2995 bd2 = bd5 = 0;
2996 }
2997 if (bbe >= 0)
2998 bb2 += bbe;
2999 else
3000 bd2 -= bbe;
3001 bs2 = bb2;
3002#ifdef Honor_FLT_ROUNDS
3003 if (bc.rounding != 1)
3004 bs2++;
3005#endif
3006#ifdef Avoid_Underflow
3007 Lsb = LSB;
3008 Lsb1 = 0;
3009 j = bbe - bc.scale;
3010 i = j + bbbits - 1; /* logb(rv) */
3011 j = P + 1 - bbbits;
3012 if (i < Emin) { /* denormal */
3013 i = Emin - i;
3014 j -= i;
3015 if (i < 32)
3016 Lsb <<= i;
3017 else if (i < 52)
3018 Lsb1 = Lsb << (i-32);
3019 else
3020 Lsb1 = Exp_mask;
3021 }
3022#else /*Avoid_Underflow*/
3023#ifdef Sudden_Underflow
3024#ifdef IBM
3025 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
3026#else
3027 j = P + 1 - bbbits;
3028#endif
3029#else /*Sudden_Underflow*/
3030 j = bbe;
3031 i = j + bbbits - 1; /* logb(rv) */
3032 if (i < Emin) /* denormal */
3033 j += P - Emin;
3034 else
3035 j = P + 1 - bbbits;
3036#endif /*Sudden_Underflow*/
3037#endif /*Avoid_Underflow*/
3038 bb2 += j;
3039 bd2 += j;
3040#ifdef Avoid_Underflow
3041 bd2 += bc.scale;
3042#endif
3043 i = bb2 < bd2 ? bb2 : bd2;
3044 if (i > bs2)
3045 i = bs2;
3046 if (i > 0) {
3047 bb2 -= i;
3048 bd2 -= i;
3049 bs2 -= i;
3050 }
3051 if (bb5 > 0) {
3052 bs = pow5mult(bs, bb5);
3053 bb1 = mult(bs, bb);
3054 Bfree(bb);
3055 bb = bb1;
3056 }
3057 if (bb2 > 0)
3058 bb = lshift(bb, bb2);
3059 if (bd5 > 0)
3060 bd = pow5mult(bd, bd5);
3061 if (bd2 > 0)
3062 bd = lshift(bd, bd2);
3063 if (bs2 > 0)
3064 bs = lshift(bs, bs2);
3065 delta = diff(bb, bd);
3066 bc.dsign = delta->sign;
3067 delta->sign = 0;
3068 i = cmp(delta, bs);
3069#ifndef NO_STRTOD_BIGCOMP /*{*/
3070 if (bc.nd > nd && i <= 0) {
3071 if (bc.dsign) {
3072 /* Must use bigcomp(). */
3073 req_bigcomp = 1;
3074 break;
3075 }
3076#ifdef Honor_FLT_ROUNDS
3077 if (bc.rounding != 1) {
3078 if (i < 0) {
3079 req_bigcomp = 1;
3080 break;
3081 }
3082 }
3083 else
3084#endif
3085 i = -1; /* Discarded digits make delta smaller. */
3086 }
3087#endif /*}*/
3088#ifdef Honor_FLT_ROUNDS /*{*/
3089 if (bc.rounding != 1) {
3090 if (i < 0) {
3091 /* Error is less than an ulp */
3092 if (!delta->x[0] && delta->wds <= 1) {
3093 /* exact */
3094#ifdef SET_INEXACT
3095 bc.inexact = 0;
3096#endif
3097 break;
3098 }
3099 if (bc.rounding) {
3100 if (bc.dsign) {
3101 adj.d = 1.;
3102 goto apply_adj;
3103 }
3104 }
3105 else if (!bc.dsign) {
3106 adj.d = -1.;
3107 if (!word1(&rv)
3108 && !(word0(&rv) & Frac_mask)) {
3109 y = word0(&rv) & Exp_mask;
3110#ifdef Avoid_Underflow
3111 if (!bc.scale || y > 2*P*Exp_msk1)
3112#else
3113 if (y)
3114#endif
3115 {
3116 delta = lshift(delta,Log2P);
3117 if (cmp(delta, bs) <= 0)
3118 adj.d = -0.5;
3119 }
3120 }
3121 apply_adj:
3122#ifdef Avoid_Underflow /*{*/
3123 if (bc.scale && (y = word0(&rv) & Exp_mask)
3124 <= 2*P*Exp_msk1)
3125 word0(&adj) += (2*P+1)*Exp_msk1 - y;
3126#else
3127#ifdef Sudden_Underflow
3128 if ((word0(&rv) & Exp_mask) <=
3129 P*Exp_msk1) {
3130 word0(&rv) += P*Exp_msk1;
3131 dval(&rv) += adj.d*ulp(dval(&rv));
3132 word0(&rv) -= P*Exp_msk1;
3133 }
3134 else
3135#endif /*Sudden_Underflow*/
3136#endif /*Avoid_Underflow}*/
3137 dval(&rv) += adj.d*ulp(&rv);
3138 }
3139 break;
3140 }
3141 adj.d = ratio(delta, bs);
3142 if (adj.d < 1.)
3143 adj.d = 1.;
3144 if (adj.d <= 0x7ffffffe) {
3145 /* adj = rounding ? ceil(adj) : floor(adj); */
3146 y = adj.d;
3147 if (y != adj.d) {
3148 if (!((bc.rounding>>1) ^ bc.dsign))
3149 y++;
3150 adj.d = y;
3151 }
3152 }
3153#ifdef Avoid_Underflow /*{*/
3154 if (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
3155 word0(&adj) += (2*P+1)*Exp_msk1 - y;
3156#else
3157#ifdef Sudden_Underflow
3158 if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
3159 word0(&rv) += P*Exp_msk1;
3160 adj.d *= ulp(dval(&rv));
3161 if (bc.dsign)
3162 dval(&rv) += adj.d;
3163 else
3164 dval(&rv) -= adj.d;
3165 word0(&rv) -= P*Exp_msk1;
3166 goto cont;
3167 }
3168#endif /*Sudden_Underflow*/
3169#endif /*Avoid_Underflow}*/
3170 adj.d *= ulp(&rv);
3171 if (bc.dsign) {
3172 if (word0(&rv) == Big0 && word1(&rv) == Big1)
3173 goto ovfl;
3174 dval(&rv) += adj.d;
3175 }
3176 else
3177 dval(&rv) -= adj.d;
3178 goto cont;
3179 }
3180#endif /*}Honor_FLT_ROUNDS*/
3181
3182 if (i < 0) {
3183 /* Error is less than half an ulp -- check for
3184 * special case of mantissa a power of two.
3185 */
3186 if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask
3187#ifdef IEEE_Arith /*{*/
3188#ifdef Avoid_Underflow
3189 || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
3190#else
3191 || (word0(&rv) & Exp_mask) <= Exp_msk1
3192#endif
3193#endif /*}*/
3194 ) {
3195#ifdef SET_INEXACT
3196 if (!delta->x[0] && delta->wds <= 1)
3197 bc.inexact = 0;
3198#endif
3199 break;
3200 }
3201 if (!delta->x[0] && delta->wds <= 1) {
3202 /* exact result */
3203#ifdef SET_INEXACT
3204 bc.inexact = 0;
3205#endif
3206 break;
3207 }
3208 delta = lshift(delta,Log2P);
3209 if (cmp(delta, bs) > 0)
3210 goto drop_down;
3211 break;
3212 }
3213 if (i == 0) {
3214 /* exactly half-way between */
3215 if (bc.dsign) {
3216 if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
3217 && word1(&rv) == (
3218#ifdef Avoid_Underflow
3219 (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
3220 ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
3221#endif
3222 0xffffffff)) {
3223 /*boundary case -- increment exponent*/
3224 if (word0(&rv) == Big0 && word1(&rv) == Big1)
3225 goto ovfl;
3226 word0(&rv) = (word0(&rv) & Exp_mask)
3227 + Exp_msk1
3228#ifdef IBM
3229 | Exp_msk1 >> 4
3230#endif
3231 ;
3232 word1(&rv) = 0;
3233#ifdef Avoid_Underflow
3234 bc.dsign = 0;
3235#endif
3236 break;
3237 }
3238 }
3239 else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
3240 drop_down:
3241 /* boundary case -- decrement exponent */
3242#ifdef Sudden_Underflow /*{{*/
3243 L = word0(&rv) & Exp_mask;
3244#ifdef IBM
3245 if (L < Exp_msk1)
3246#else
3247#ifdef Avoid_Underflow
3248 if (L <= (bc.scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
3249#else
3250 if (L <= Exp_msk1)
3251#endif /*Avoid_Underflow*/
3252#endif /*IBM*/
3253 {
3254 if (bc.nd >nd) {
3255 bc.uflchk = 1;
3256 break;
3257 }
3258 goto undfl;
3259 }
3260 L -= Exp_msk1;
3261#else /*Sudden_Underflow}{*/
3262#ifdef Avoid_Underflow
3263 if (bc.scale) {
3264 L = word0(&rv) & Exp_mask;
3265 if (L <= (2*P+1)*Exp_msk1) {
3266 if (L > (P+2)*Exp_msk1)
3267 /* round even ==> */
3268 /* accept rv */
3269 break;
3270 /* rv = smallest denormal */
3271 if (bc.nd >nd) {
3272 bc.uflchk = 1;
3273 break;
3274 }
3275 goto undfl;
3276 }
3277 }
3278#endif /*Avoid_Underflow*/
3279 L = (word0(&rv) & Exp_mask) - Exp_msk1;
3280#endif /*Sudden_Underflow}}*/
3281 word0(&rv) = L | Bndry_mask1;
3282 word1(&rv) = 0xffffffff;
3283#ifdef IBM
3284 goto cont;
3285#else
3286#ifndef NO_STRTOD_BIGCOMP
3287 if (bc.nd > nd)
3288 goto cont;
3289#endif
3290 break;
3291#endif
3292 }
3293#ifndef ROUND_BIASED
3294#ifdef Avoid_Underflow
3295 if (Lsb1) {
3296 if (!(word0(&rv) & Lsb1))
3297 break;
3298 }
3299 else if (!(word1(&rv) & Lsb))
3300 break;
3301#else
3302 if (!(word1(&rv) & LSB))
3303 break;
3304#endif
3305#endif
3306 if (bc.dsign)
3307#ifdef Avoid_Underflow
3308 dval(&rv) += sulp(&rv, &bc);
3309#else
3310 dval(&rv) += ulp(&rv);
3311#endif
3312#ifndef ROUND_BIASED
3313 else {
3314#ifdef Avoid_Underflow
3315 dval(&rv) -= sulp(&rv, &bc);
3316#else
3317 dval(&rv) -= ulp(&rv);
3318#endif
3319#ifndef Sudden_Underflow
3320 if (!dval(&rv)) {
3321 if (bc.nd >nd) {
3322 bc.uflchk = 1;
3323 break;
3324 }
3325 goto undfl;
3326 }
3327#endif
3328 }
3329#ifdef Avoid_Underflow
3330 bc.dsign = 1 - bc.dsign;
3331#endif
3332#endif
3333 break;
3334 }
3335 if ((aadj = ratio(delta, bs)) <= 2.) {
3336 if (bc.dsign)
3337 aadj = aadj1 = 1.;
3338 else if (word1(&rv) || word0(&rv) & Bndry_mask) {
3339#ifndef Sudden_Underflow
3340 if (word1(&rv) == Tiny1 && !word0(&rv)) {
3341 if (bc.nd >nd) {
3342 bc.uflchk = 1;
3343 break;
3344 }
3345 goto undfl;
3346 }
3347#endif
3348 aadj = 1.;
3349 aadj1 = -1.;
3350 }
3351 else {
3352 /* special case -- power of FLT_RADIX to be */
3353 /* rounded down... */
3354
3355 if (aadj < 2./FLT_RADIX)
3356 aadj = 1./FLT_RADIX;
3357 else
3358 aadj *= 0.5;
3359 aadj1 = -aadj;
3360 }
3361 }
3362 else {
3363 aadj *= 0.5;
3364 aadj1 = bc.dsign ? aadj : -aadj;
3365#ifdef Check_FLT_ROUNDS
3366 switch(bc.rounding) {
3367 case 2: /* towards +infinity */
3368 aadj1 -= 0.5;
3369 break;
3370 case 0: /* towards 0 */
3371 case 3: /* towards -infinity */
3372 aadj1 += 0.5;
3373 }
3374#else
3375 if (Flt_Rounds == 0)
3376 aadj1 += 0.5;
3377#endif /*Check_FLT_ROUNDS*/
3378 }
3379 y = word0(&rv) & Exp_mask;
3380
3381 /* Check for overflow */
3382
3383 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
3384 dval(&rv0) = dval(&rv);
3385 word0(&rv) -= P*Exp_msk1;
3386 adj.d = aadj1 * ulp(&rv);
3387 dval(&rv) += adj.d;
3388 if ((word0(&rv) & Exp_mask) >=
3389 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
3390 if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
3391 goto ovfl;
3392 word0(&rv) = Big0;
3393 word1(&rv) = Big1;
3394 goto cont;
3395 }
3396 else
3397 word0(&rv) += P*Exp_msk1;
3398 }
3399 else {
3400#ifdef Avoid_Underflow
3401 if (bc.scale && y <= 2*P*Exp_msk1) {
3402 if (aadj <= 0x7fffffff) {
3403 if ((z = aadj) <= 0)
3404 z = 1;
3405 aadj = z;
3406 aadj1 = bc.dsign ? aadj : -aadj;
3407 }
3408 dval(&aadj2) = aadj1;
3409 word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
3410 aadj1 = dval(&aadj2);
3411 adj.d = aadj1 * ulp(&rv);
3412 dval(&rv) += adj.d;
3413 if (rv.d == 0.)
3414#ifdef NO_STRTOD_BIGCOMP
3415 goto undfl;
3416#else
3417 {
3418 req_bigcomp = 1;
3419 break;
3420 }
3421#endif
3422 }
3423 else {
3424 adj.d = aadj1 * ulp(&rv);
3425 dval(&rv) += adj.d;
3426 }
3427#else
3428#ifdef Sudden_Underflow
3429 if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
3430 dval(&rv0) = dval(&rv);
3431 word0(&rv) += P*Exp_msk1;
3432 adj.d = aadj1 * ulp(&rv);
3433 dval(&rv) += adj.d;
3434#ifdef IBM
3435 if ((word0(&rv) & Exp_mask) < P*Exp_msk1)
3436#else
3437 if ((word0(&rv) & Exp_mask) <= P*Exp_msk1)
3438#endif
3439 {
3440 if (word0(&rv0) == Tiny0
3441 && word1(&rv0) == Tiny1) {
3442 if (bc.nd >nd) {
3443 bc.uflchk = 1;
3444 break;
3445 }
3446 goto undfl;
3447 }
3448 word0(&rv) = Tiny0;
3449 word1(&rv) = Tiny1;
3450 goto cont;
3451 }
3452 else
3453 word0(&rv) -= P*Exp_msk1;
3454 }
3455 else {
3456 adj.d = aadj1 * ulp(&rv);
3457 dval(&rv) += adj.d;
3458 }
3459#else /*Sudden_Underflow*/
3460 /* Compute adj so that the IEEE rounding rules will
3461 * correctly round rv + adj in some half-way cases.
3462 * If rv * ulp(rv) is denormalized (i.e.,
3463 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
3464 * trouble from bits lost to denormalization;
3465 * example: 1.2e-307 .
3466 */
3467 if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
3468 aadj1 = (double)(int)(aadj + 0.5);
3469 if (!bc.dsign)
3470 aadj1 = -aadj1;
3471 }
3472 adj.d = aadj1 * ulp(&rv);
3473 dval(&rv) += adj.d;
3474#endif /*Sudden_Underflow*/
3475#endif /*Avoid_Underflow*/
3476 }
3477 z = word0(&rv) & Exp_mask;
3478#ifndef SET_INEXACT
3479 if (bc.nd == nd) {
3480#ifdef Avoid_Underflow
3481 if (!bc.scale)
3482#endif
3483 if (y == z) {
3484 /* Can we stop now? */
3485 L = (Long)aadj;
3486 aadj -= L;
3487 /* The tolerances below are conservative. */
3488 if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
3489 if (aadj < .4999999 || aadj > .5000001)
3490 break;
3491 }
3492 else if (aadj < .4999999/FLT_RADIX)
3493 break;
3494 }
3495 }
3496#endif
3497 cont:
3498 Bfree(bb);
3499 Bfree(bd);
3500 Bfree(bs);
3501 Bfree(delta);
3502 }
3503 Bfree(bb);
3504 Bfree(bd);
3505 Bfree(bs);
3506 Bfree(bd0);
3507 Bfree(delta);
3508#ifndef NO_STRTOD_BIGCOMP
3509 if (req_bigcomp) {
3510 bd0 = 0;
3511 bc.e0 += nz1;
3512 bigcomp(&rv, s0, &bc);
3513 y = word0(&rv) & Exp_mask;
3514 if (y == Exp_mask)
3515 goto ovfl;
3516 if (y == 0 && rv.d == 0.)
3517 goto undfl;
3518 }
3519#endif
3520#ifdef SET_INEXACT
3521 if (bc.inexact) {
3522 if (!oldinexact) {
3523 word0(&rv0) = Exp_1 + (70 << Exp_shift);
3524 word1(&rv0) = 0;
3525 dval(&rv0) += 1.;
3526 }
3527 }
3528 else if (!oldinexact)
3529 clear_inexact();
3530#endif
3531#ifdef Avoid_Underflow
3532 if (bc.scale) {
3533 word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
3534 word1(&rv0) = 0;
3535 dval(&rv) *= dval(&rv0);
3536#ifndef NO_ERRNO
3537 /* try to avoid the bug of testing an 8087 register value */
3538#ifdef IEEE_Arith
3539 if (!(word0(&rv) & Exp_mask))
3540#else
3541 if (word0(&rv) == 0 && word1(&rv) == 0)
3542#endif
3543 errno = ERANGE;
3544#endif
3545 }
3546#endif /* Avoid_Underflow */
3547#ifdef SET_INEXACT
3548 if (bc.inexact && !(word0(&rv) & Exp_mask)) {
3549 /* set underflow bit */
3550 dval(&rv0) = 1e-300;
3551 dval(&rv0) *= dval(&rv0);
3552 }
3553#endif
3554 ret:
3555 if (se)
3556 *se = (char *)s;
3557 return sign ? -dval(&rv) : dval(&rv);
3558 }
3559
3560#ifndef MULTIPLE_THREADS
3561 static char *dtoa_result;
3562#endif
3563
3564 static char *
3565#ifdef KR_headers
3566rv_alloc(i) int i;
3567#else
3568rv_alloc(int i)
3569#endif
3570{
3571 int j, k, *r;
3572
3573 j = sizeof(ULong);
3574 for(k = 0;
3575 sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
3576 j <<= 1)
3577 k++;
3578 r = (int*)Balloc(k);
3579 *r = k;
3580 return
3581#ifndef MULTIPLE_THREADS
3582 dtoa_result =
3583#endif
3584 (char *)(r+1);
3585 }
3586
3587 static char *
3588#ifdef KR_headers
3589nrv_alloc(s, rve, n) char *s, **rve; int n;
3590#else
3591nrv_alloc(const char *s, char **rve, int n)
3592#endif
3593{
3594 char *rv, *t;
3595
3596 t = rv = rv_alloc(n);
3597 while((*t = *s++)) t++;
3598 if (rve)
3599 *rve = t;
3600 return rv;
3601 }
3602
3603/* freedtoa(s) must be used to free values s returned by dtoa
3604 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
3605 * but for consistency with earlier versions of dtoa, it is optional
3606 * when MULTIPLE_THREADS is not defined.
3607 */
3608
3609 void
3610#ifdef KR_headers
3611freedtoa(s) char *s;
3612#else
3614#endif
3615{
3616 Bigint *b = (Bigint *)((int *)s - 1);
3617 b->maxwds = 1 << (b->k = *(int*)b);
3618 Bfree(b);
3619#ifndef MULTIPLE_THREADS
3620 if (s == dtoa_result)
3621 dtoa_result = 0;
3622#endif
3623 }
3624
3625/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
3626 *
3627 * Inspired by "How to Print Floating-Point Numbers Accurately" by
3628 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
3629 *
3630 * Modifications:
3631 * 1. Rather than iterating, we use a simple numeric overestimate
3632 * to determine k = floor(log10(d)). We scale relevant
3633 * quantities using O(log2(k)) rather than O(k) multiplications.
3634 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
3635 * try to generate digits strictly left to right. Instead, we
3636 * compute with fewer bits and propagate the carry if necessary
3637 * when rounding the final digit up. This is often faster.
3638 * 3. Under the assumption that input will be rounded nearest,
3639 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
3640 * That is, we allow equality in stopping tests when the
3641 * round-nearest rule will give the same floating-point value
3642 * as would satisfaction of the stopping test with strict
3643 * inequality.
3644 * 4. We remove common factors of powers of 2 from relevant
3645 * quantities.
3646 * 5. When converting floating-point integers less than 1e16,
3647 * we use floating-point arithmetic rather than resorting
3648 * to multiple-precision integers.
3649 * 6. When asked to produce fewer than 15 digits, we first try
3650 * to get by with floating-point arithmetic; we resort to
3651 * multiple-precision integer arithmetic only if we cannot
3652 * guarantee that the floating-point calculation has given
3653 * the correctly rounded result. For k requested digits and
3654 * "uniformly" distributed input, the probability is
3655 * something like 10^(k-15) that we must resort to the Long
3656 * calculation.
3657 */
3658
3659 char *
3661#ifdef KR_headers
3662 (dd, mode, ndigits, decpt, sign, rve)
3663 double dd; int mode, ndigits, *decpt, *sign; char **rve;
3664#else
3665 (double dd, int mode, int ndigits, int *decpt, int *sign, char **rve)
3666#endif
3667{
3668 /* Arguments ndigits, decpt, sign are similar to those
3669 of ecvt and fcvt; trailing zeros are suppressed from
3670 the returned string. If not null, *rve is set to point
3671 to the end of the return value. If d is +-Infinity or NaN,
3672 then *decpt is set to 9999.
3673
3674 mode:
3675 0 ==> shortest string that yields d when read in
3676 and rounded to nearest.
3677 1 ==> like 0, but with Steele & White stopping rule;
3678 e.g. with IEEE P754 arithmetic , mode 0 gives
3679 1e23 whereas mode 1 gives 9.999999999999999e22.
3680 2 ==> max(1,ndigits) significant digits. This gives a
3681 return value similar to that of ecvt, except
3682 that trailing zeros are suppressed.
3683 3 ==> through ndigits past the decimal point. This
3684 gives a return value similar to that from fcvt,
3685 except that trailing zeros are suppressed, and
3686 ndigits can be negative.
3687 4,5 ==> similar to 2 and 3, respectively, but (in
3688 round-nearest mode) with the tests of mode 0 to
3689 possibly return a shorter string that rounds to d.
3690 With IEEE arithmetic and compilation with
3691 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
3692 as modes 2 and 3 when FLT_ROUNDS != 1.
3693 6-9 ==> Debugging modes similar to mode - 4: don't try
3694 fast floating-point estimate (if applicable).
3695
3696 Values of mode other than 0-9 are treated as mode 0.
3697
3698 Sufficient space is allocated to the return value
3699 to hold the suppressed trailing zeros.
3700 */
3701
3702 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
3703 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
3704 spec_case, try_quick;
3705 Long L;
3706#ifndef Sudden_Underflow
3707 int denorm;
3708 ULong x;
3709#endif
3710 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
3711 U d2, eps, u;
3712 double ds;
3713 char *s, *s0;
3714#ifndef No_leftright
3715#ifdef IEEE_Arith
3716 U eps1;
3717#endif
3718#endif
3719#ifdef SET_INEXACT
3720 int inexact, oldinexact;
3721#endif
3722#ifdef Honor_FLT_ROUNDS /*{*/
3723 int Rounding;
3724#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
3725 Rounding = Flt_Rounds;
3726#else /*}{*/
3727 Rounding = 1;
3728 switch(fegetround()) {
3729 case FE_TOWARDZERO: Rounding = 0; break;
3730 case FE_UPWARD: Rounding = 2; break;
3731 case FE_DOWNWARD: Rounding = 3;
3732 }
3733#endif /*}}*/
3734#endif /*}*/
3735
3736#ifndef MULTIPLE_THREADS
3737 if (dtoa_result) {
3738 freedtoa(dtoa_result);
3739 dtoa_result = 0;
3740 }
3741#endif
3742
3743 u.d = dd;
3744 if (word0(&u) & Sign_bit) {
3745 /* set sign for everything, including 0's and NaNs */
3746 *sign = 1;
3747 word0(&u) &= ~Sign_bit; /* clear sign bit */
3748 }
3749 else
3750 *sign = 0;
3751
3752#if defined(IEEE_Arith) + defined(VAX)
3753#ifdef IEEE_Arith
3754 if ((word0(&u) & Exp_mask) == Exp_mask)
3755#else
3756 if (word0(&u) == 0x8000)
3757#endif
3758 {
3759 /* Infinity or NaN */
3760 *decpt = 9999;
3761#ifdef IEEE_Arith
3762 if (!word1(&u) && !(word0(&u) & 0xfffff))
3763 return nrv_alloc("Infinity", rve, 8);
3764#endif
3765 return nrv_alloc("NaN", rve, 3);
3766 }
3767#endif
3768#ifdef IBM
3769 dval(&u) += 0; /* normalize */
3770#endif
3771 if (!dval(&u)) {
3772 *decpt = 1;
3773 return nrv_alloc("0", rve, 1);
3774 }
3775
3776#ifdef SET_INEXACT
3777 try_quick = oldinexact = get_inexact();
3778 inexact = 1;
3779#endif
3780#ifdef Honor_FLT_ROUNDS
3781 if (Rounding >= 2) {
3782 if (*sign)
3783 Rounding = Rounding == 2 ? 0 : 2;
3784 else
3785 if (Rounding != 2)
3786 Rounding = 0;
3787 }
3788#endif
3789
3790 b = d2b(&u, &be, &bbits);
3791#ifdef Sudden_Underflow
3792 i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
3793#else
3794 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
3795#endif
3796 dval(&d2) = dval(&u);
3797 word0(&d2) &= Frac_mask1;
3798 word0(&d2) |= Exp_11;
3799#ifdef IBM
3800 if (j = 11 - hi0bits(word0(&d2) & Frac_mask))
3801 dval(&d2) /= 1 << j;
3802#endif
3803
3804 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
3805 * log10(x) = log(x) / log(10)
3806 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
3807 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
3808 *
3809 * This suggests computing an approximation k to log10(d) by
3810 *
3811 * k = (i - Bias)*0.301029995663981
3812 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
3813 *
3814 * We want k to be too large rather than too small.
3815 * The error in the first-order Taylor series approximation
3816 * is in our favor, so we just round up the constant enough
3817 * to compensate for any error in the multiplication of
3818 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
3819 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
3820 * adding 1e-13 to the constant term more than suffices.
3821 * Hence we adjust the constant term to 0.1760912590558.
3822 * (We could get a more accurate k by invoking log10,
3823 * but this is probably not worthwhile.)
3824 */
3825
3826 i -= Bias;
3827#ifdef IBM
3828 i <<= 2;
3829 i += j;
3830#endif
3831#ifndef Sudden_Underflow
3832 denorm = 0;
3833 }
3834 else {
3835 /* d is denormalized */
3836
3837 i = bbits + be + (Bias + (P-1) - 1);
3838 x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
3839 : word1(&u) << (32 - i);
3840 dval(&d2) = x;
3841 word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
3842 i -= (Bias + (P-1) - 1) + 1;
3843 denorm = 1;
3844 }
3845#endif
3846 ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
3847 k = (int)ds;
3848 if (ds < 0. && ds != k)
3849 k--; /* want k = floor(ds) */
3850 k_check = 1;
3851 if (k >= 0 && k <= Ten_pmax) {
3852 if (dval(&u) < tens[k])
3853 k--;
3854 k_check = 0;
3855 }
3856 j = bbits - i - 1;
3857 if (j >= 0) {
3858 b2 = 0;
3859 s2 = j;
3860 }
3861 else {
3862 b2 = -j;
3863 s2 = 0;
3864 }
3865 if (k >= 0) {
3866 b5 = 0;
3867 s5 = k;
3868 s2 += k;
3869 }
3870 else {
3871 b2 -= k;
3872 b5 = -k;
3873 s5 = 0;
3874 }
3875 if (mode < 0 || mode > 9)
3876 mode = 0;
3877
3878#ifndef SET_INEXACT
3879#ifdef Check_FLT_ROUNDS
3880 try_quick = Rounding == 1;
3881#else
3882 try_quick = 1;
3883#endif
3884#endif /*SET_INEXACT*/
3885
3886 if (mode > 5) {
3887 mode -= 4;
3888 try_quick = 0;
3889 }
3890 leftright = 1;
3891 ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
3892 /* silence erroneous "gcc -Wall" warning. */
3893 switch(mode) {
3894 case 0:
3895 case 1:
3896 i = 18;
3897 ndigits = 0;
3898 break;
3899 case 2:
3900 leftright = 0;
3901 /* no break */
3902 case 4:
3903 if (ndigits <= 0)
3904 ndigits = 1;
3905 ilim = ilim1 = i = ndigits;
3906 break;
3907 case 3:
3908 leftright = 0;
3909 /* no break */
3910 case 5:
3911 i = ndigits + k + 1;
3912 ilim = i;
3913 ilim1 = i - 1;
3914 if (i <= 0)
3915 i = 1;
3916 }
3917 s = s0 = rv_alloc(i);
3918
3919#ifdef Honor_FLT_ROUNDS
3920 if (mode > 1 && Rounding != 1)
3921 leftright = 0;
3922#endif
3923
3924 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
3925
3926 /* Try to get by with floating-point arithmetic. */
3927
3928 i = 0;
3929 dval(&d2) = dval(&u);
3930 k0 = k;
3931 ilim0 = ilim;
3932 ieps = 2; /* conservative */
3933 if (k > 0) {
3934 ds = tens[k&0xf];
3935 j = k >> 4;
3936 if (j & Bletch) {
3937 /* prevent overflows */
3938 j &= Bletch - 1;
3939 dval(&u) /= bigtens[n_bigtens-1];
3940 ieps++;
3941 }
3942 for(; j; j >>= 1, i++)
3943 if (j & 1) {
3944 ieps++;
3945 ds *= bigtens[i];
3946 }
3947 dval(&u) /= ds;
3948 }
3949 else if ((j1 = -k)) {
3950 dval(&u) *= tens[j1 & 0xf];
3951 for(j = j1 >> 4; j; j >>= 1, i++)
3952 if (j & 1) {
3953 ieps++;
3954 dval(&u) *= bigtens[i];
3955 }
3956 }
3957 if (k_check && dval(&u) < 1. && ilim > 0) {
3958 if (ilim1 <= 0)
3959 goto fast_failed;
3960 ilim = ilim1;
3961 k--;
3962 dval(&u) *= 10.;
3963 ieps++;
3964 }
3965 dval(&eps) = ieps*dval(&u) + 7.;
3966 word0(&eps) -= (P-1)*Exp_msk1;
3967 if (ilim == 0) {
3968 S = mhi = 0;
3969 dval(&u) -= 5.;
3970 if (dval(&u) > dval(&eps))
3971 goto one_digit;
3972 if (dval(&u) < -dval(&eps))
3973 goto no_digits;
3974 goto fast_failed;
3975 }
3976#ifndef No_leftright
3977 if (leftright) {
3978 /* Use Steele & White method of only
3979 * generating digits needed.
3980 */
3981 dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
3982#ifdef IEEE_Arith
3983 if (k0 < 0 && j1 >= 307) {
3984 eps1.d = 1.01e256; /* 1.01 allows roundoff in the next few lines */
3985 word0(&eps1) -= Exp_msk1 * (Bias+P-1);
3986 dval(&eps1) *= tens[j1 & 0xf];
3987 for(i = 0, j = (j1-256) >> 4; j; j >>= 1, i++)
3988 if (j & 1)
3989 dval(&eps1) *= bigtens[i];
3990 if (eps.d < eps1.d)
3991 eps.d = eps1.d;
3992 }
3993#endif
3994 for(i = 0;;) {
3995 L = dval(&u);
3996 dval(&u) -= L;
3997 *s++ = '0' + (int)L;
3998 if (1. - dval(&u) < dval(&eps))
3999 goto bump_up;
4000 if (dval(&u) < dval(&eps))
4001 goto ret1;
4002 if (++i >= ilim)
4003 break;
4004 dval(&eps) *= 10.;
4005 dval(&u) *= 10.;
4006 }
4007 }
4008 else {
4009#endif
4010 /* Generate ilim digits, then fix them up. */
4011 dval(&eps) *= tens[ilim-1];
4012 for(i = 1;; i++, dval(&u) *= 10.) {
4013 L = (Long)(dval(&u));
4014 if (!(dval(&u) -= L))
4015 ilim = i;
4016 *s++ = '0' + (int)L;
4017 if (i == ilim) {
4018 if (dval(&u) > 0.5 + dval(&eps))
4019 goto bump_up;
4020 else if (dval(&u) < 0.5 - dval(&eps)) {
4021 while(*--s == '0');
4022 s++;
4023 goto ret1;
4024 }
4025 break;
4026 }
4027 }
4028#ifndef No_leftright
4029 }
4030#endif
4031 fast_failed:
4032 s = s0;
4033 dval(&u) = dval(&d2);
4034 k = k0;
4035 ilim = ilim0;
4036 }
4037
4038 /* Do we have a "small" integer? */
4039
4040 if (be >= 0 && k <= Int_max) {
4041 /* Yes. */
4042 ds = tens[k];
4043 if (ndigits < 0 && ilim <= 0) {
4044 S = mhi = 0;
4045 if (ilim < 0 || dval(&u) <= 5*ds)
4046 goto no_digits;
4047 goto one_digit;
4048 }
4049 for(i = 1;; i++, dval(&u) *= 10.) {
4050 L = (Long)(dval(&u) / ds);
4051 dval(&u) -= L*ds;
4052#ifdef Check_FLT_ROUNDS
4053 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
4054 if (dval(&u) < 0) {
4055 L--;
4056 dval(&u) += ds;
4057 }
4058#endif
4059 *s++ = '0' + (int)L;
4060 if (!dval(&u)) {
4061#ifdef SET_INEXACT
4062 inexact = 0;
4063#endif
4064 break;
4065 }
4066 if (i == ilim) {
4067#ifdef Honor_FLT_ROUNDS
4068 if (mode > 1)
4069 switch(Rounding) {
4070 case 0: goto ret1;
4071 case 2: goto bump_up;
4072 }
4073#endif
4074 dval(&u) += dval(&u);
4075#ifdef ROUND_BIASED
4076 if (dval(&u) >= ds)
4077#else
4078 if (dval(&u) > ds || (dval(&u) == ds && L & 1))
4079#endif
4080 {
4081 bump_up:
4082 while(*--s == '9')
4083 if (s == s0) {
4084 k++;
4085 *s = '0';
4086 break;
4087 }
4088 ++*s++;
4089 }
4090 break;
4091 }
4092 }
4093 goto ret1;
4094 }
4095
4096 m2 = b2;
4097 m5 = b5;
4098 mhi = mlo = 0;
4099 if (leftright) {
4100 i =
4101#ifndef Sudden_Underflow
4102 denorm ? be + (Bias + (P-1) - 1 + 1) :
4103#endif
4104#ifdef IBM
4105 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
4106#else
4107 1 + P - bbits;
4108#endif
4109 b2 += i;
4110 s2 += i;
4111 mhi = i2b(1);
4112 }
4113 if (m2 > 0 && s2 > 0) {
4114 i = m2 < s2 ? m2 : s2;
4115 b2 -= i;
4116 m2 -= i;
4117 s2 -= i;
4118 }
4119 if (b5 > 0) {
4120 if (leftright) {
4121 if (m5 > 0) {
4122 mhi = pow5mult(mhi, m5);
4123 b1 = mult(mhi, b);
4124 Bfree(b);
4125 b = b1;
4126 }
4127 if ((j = b5 - m5))
4128 b = pow5mult(b, j);
4129 }
4130 else
4131 b = pow5mult(b, b5);
4132 }
4133 S = i2b(1);
4134 if (s5 > 0)
4135 S = pow5mult(S, s5);
4136
4137 /* Check for special case that d is a normalized power of 2. */
4138
4139 spec_case = 0;
4140 if ((mode < 2 || leftright)
4141#ifdef Honor_FLT_ROUNDS
4142 && Rounding == 1
4143#endif
4144 ) {
4145 if (!word1(&u) && !(word0(&u) & Bndry_mask)
4146#ifndef Sudden_Underflow
4147 && word0(&u) & (Exp_mask & ~Exp_msk1)
4148#endif
4149 ) {
4150 /* The special case */
4151 b2 += Log2P;
4152 s2 += Log2P;
4153 spec_case = 1;
4154 }
4155 }
4156
4157 /* Arrange for convenient computation of quotients:
4158 * shift left if necessary so divisor has 4 leading 0 bits.
4159 *
4160 * Perhaps we should just compute leading 28 bits of S once
4161 * and for all and pass them and a shift to quorem, so it
4162 * can do shifts and ors to compute the numerator for q.
4163 */
4164 i = dshift(S, s2);
4165 b2 += i;
4166 m2 += i;
4167 s2 += i;
4168 if (b2 > 0)
4169 b = lshift(b, b2);
4170 if (s2 > 0)
4171 S = lshift(S, s2);
4172 if (k_check) {
4173 if (cmp(b,S) < 0) {
4174 k--;
4175 b = multadd(b, 10, 0); /* we botched the k estimate */
4176 if (leftright)
4177 mhi = multadd(mhi, 10, 0);
4178 ilim = ilim1;
4179 }
4180 }
4181 if (ilim <= 0 && (mode == 3 || mode == 5)) {
4182 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
4183 /* no digits, fcvt style */
4184 no_digits:
4185 k = -1 - ndigits;
4186 goto ret;
4187 }
4188 one_digit:
4189 *s++ = '1';
4190 k++;
4191 goto ret;
4192 }
4193 if (leftright) {
4194 if (m2 > 0)
4195 mhi = lshift(mhi, m2);
4196
4197 /* Compute mlo -- check for special case
4198 * that d is a normalized power of 2.
4199 */
4200
4201 mlo = mhi;
4202 if (spec_case) {
4203 mhi = Balloc(mhi->k);
4204 Bcopy(mhi, mlo);
4205 mhi = lshift(mhi, Log2P);
4206 }
4207
4208 for(i = 1;;i++) {
4209 dig = quorem(b,S) + '0';
4210 /* Do we yet have the shortest decimal string
4211 * that will round to d?
4212 */
4213 j = cmp(b, mlo);
4214 delta = diff(S, mhi);
4215 j1 = delta->sign ? 1 : cmp(b, delta);
4216 Bfree(delta);
4217#ifndef ROUND_BIASED
4218 if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
4219#ifdef Honor_FLT_ROUNDS
4220 && Rounding >= 1
4221#endif
4222 ) {
4223 if (dig == '9')
4224 goto round_9_up;
4225 if (j > 0)
4226 dig++;
4227#ifdef SET_INEXACT
4228 else if (!b->x[0] && b->wds <= 1)
4229 inexact = 0;
4230#endif
4231 *s++ = dig;
4232 goto ret;
4233 }
4234#endif
4235 if (j < 0 || (j == 0 && mode != 1
4236#ifndef ROUND_BIASED
4237 && !(word1(&u) & 1)
4238#endif
4239 )) {
4240 if (!b->x[0] && b->wds <= 1) {
4241#ifdef SET_INEXACT
4242 inexact = 0;
4243#endif
4244 goto accept_dig;
4245 }
4246#ifdef Honor_FLT_ROUNDS
4247 if (mode > 1)
4248 switch(Rounding) {
4249 case 0: goto accept_dig;
4250 case 2: goto keep_dig;
4251 }
4252#endif /*Honor_FLT_ROUNDS*/
4253 if (j1 > 0) {
4254 b = lshift(b, 1);
4255 j1 = cmp(b, S);
4256#ifdef ROUND_BIASED
4257 if (j1 >= 0 /*)*/
4258#else
4259 if ((j1 > 0 || (j1 == 0 && dig & 1))
4260#endif
4261 && dig++ == '9')
4262 goto round_9_up;
4263 }
4264 accept_dig:
4265 *s++ = dig;
4266 goto ret;
4267 }
4268 if (j1 > 0) {
4269#ifdef Honor_FLT_ROUNDS
4270 if (!Rounding)
4271 goto accept_dig;
4272#endif
4273 if (dig == '9') { /* possible if i == 1 */
4274 round_9_up:
4275 *s++ = '9';
4276 goto roundoff;
4277 }
4278 *s++ = dig + 1;
4279 goto ret;
4280 }
4281#ifdef Honor_FLT_ROUNDS
4282 keep_dig:
4283#endif
4284 *s++ = dig;
4285 if (i == ilim)
4286 break;
4287 b = multadd(b, 10, 0);
4288 if (mlo == mhi)
4289 mlo = mhi = multadd(mhi, 10, 0);
4290 else {
4291 mlo = multadd(mlo, 10, 0);
4292 mhi = multadd(mhi, 10, 0);
4293 }
4294 }
4295 }
4296 else
4297 for(i = 1;; i++) {
4298 *s++ = dig = quorem(b,S) + '0';
4299 if (!b->x[0] && b->wds <= 1) {
4300#ifdef SET_INEXACT
4301 inexact = 0;
4302#endif
4303 goto ret;
4304 }
4305 if (i >= ilim)
4306 break;
4307 b = multadd(b, 10, 0);
4308 }
4309
4310 /* Round off last digit */
4311
4312#ifdef Honor_FLT_ROUNDS
4313 switch(Rounding) {
4314 case 0: goto trimzeros;
4315 case 2: goto roundoff;
4316 }
4317#endif
4318 b = lshift(b, 1);
4319 j = cmp(b, S);
4320#ifdef ROUND_BIASED
4321 if (j >= 0)
4322#else
4323 if (j > 0 || (j == 0 && dig & 1))
4324#endif
4325 {
4326 roundoff:
4327 while(*--s == '9')
4328 if (s == s0) {
4329 k++;
4330 *s++ = '1';
4331 goto ret;
4332 }
4333 ++*s++;
4334 }
4335 else {
4336#ifdef Honor_FLT_ROUNDS
4337 trimzeros:
4338#endif
4339 while(*--s == '0');
4340 s++;
4341 }
4342 ret:
4343 Bfree(S);
4344 if (mhi) {
4345 if (mlo && mlo != mhi)
4346 Bfree(mlo);
4347 Bfree(mhi);
4348 }
4349 ret1:
4350#ifdef SET_INEXACT
4351 if (inexact) {
4352 if (!oldinexact) {
4353 word0(&u) = Exp_1 + (70 << Exp_shift);
4354 word1(&u) = 0;
4355 dval(&u) += 1.;
4356 }
4357 }
4358 else if (!oldinexact)
4359 clear_inexact();
4360#endif
4361 Bfree(b);
4362 *s = 0;
4363 *decpt = k + 1;
4364 if (rve)
4365 *rve = s;
4366 return s0;
4367 }
4368#ifdef __cplusplus
4369}
4370#endif
std::string one()
#define strtod
#define dtoa
wasm_allocator wa
Definition main.cpp:10
const mie::Vuint & r
Definition bn.cpp:28
void freedtoa(char *s)
Definition dtoa.c:3613
#define Emax
Definition dtoa.c:440
#define ULLong
Definition dtoa.c:515
#define Exp_shift
Definition dtoa.c:432
#define rounded_product(a, b)
Definition dtoa.c:479
#define Exp_msk11
Definition dtoa.c:435
@ Round_zero
Definition dtoa.c:1751
@ Round_up
Definition dtoa.c:1753
@ Round_near
Definition dtoa.c:1752
@ Round_down
Definition dtoa.c:1754
#define CONST
Definition dtoa.c:298
#define Tiny0
Definition dtoa.c:454
#define Big1
Definition dtoa.c:484
#define Exp_msk1
Definition dtoa.c:434
#define FREE_DTOA_LOCK(n)
Definition dtoa.c:521
#define d1
#define P
Definition dtoa.c:437
#define Nbits
Definition dtoa.c:438
#define Bndry_mask
Definition dtoa.c:449
#define Ebits
Definition dtoa.c:444
#define rounded_quotient(a, b)
Definition dtoa.c:480
#define word1(x)
Definition dtoa.c:313
#define MALLOC
Definition dtoa.c:221
#define Frac_mask
Definition dtoa.c:445
#define Tiny1
Definition dtoa.c:455
#define Frac_mask1
Definition dtoa.c:446
#define Long
Definition dtoa.c:190
#define Quick_max
Definition dtoa.c:456
#define Bias
Definition dtoa.c:439
void gethex(CONST char **sp, U *rvp, int rounding, int sign)
Definition dtoa.c:1762
#define PRIVATE_mem
Definition dtoa.c:228
#define n_bigtens
Definition dtoa.c:1481
#define LSB
Definition dtoa.c:451
#define dval(x)
Definition dtoa.c:315
Exactly one of IEEE_MC68k
Definition dtoa.c:303
#define Exp_mask
Definition dtoa.c:436
#define Kmax
Definition dtoa.c:524
#define Ten_pmax
Definition dtoa.c:447
#define Sign_bit
Definition dtoa.c:452
Exactly one of VAX
Definition dtoa.c:303
#define kmask
Definition dtoa.c:1644
#define Bletch
Definition dtoa.c:448
#define strtod_diglim
Definition dtoa.c:324
#define Flt_Rounds
Definition dtoa.c:431
#define Storeinc(a, b, c)
Definition dtoa.c:335
#define word0(x)
Definition dtoa.c:312
#define ACQUIRE_DTOA_LOCK(n)
Definition dtoa.c:520
#define Exp_1
Definition dtoa.c:442
#define Exp_shift1
Definition dtoa.c:433
#define kshift
Definition dtoa.c:1643
#define Sudden_Underflow
Definition dtoa.c:399
#define Emin
Definition dtoa.c:441
#define Bcopy(x, y)
Definition dtoa.c:608
#define Bndry_mask1
Definition dtoa.c:450
#define FFFFFFFF
Definition dtoa.c:497
#define ROUND_BIASED
Definition dtoa.c:462
#define d0
#define Log2P
Definition dtoa.c:453
#define Big0
Definition dtoa.c:483
#define Exp_11
Definition dtoa.c:443
#define Int_max
Definition dtoa.c:457
struct Bigint Bigint
Definition dtoa.c:539
#define ULbits
Definition dtoa.c:1642
unsigned Long ULong
Definition dtoa.c:193
Exactly one of IEEE_8087
Definition dtoa.c:303
void diff(const std::string &a, const std::string &b)
Definition jmp.cpp:18
static const Reg16 sp(Operand::SP)
static const Opmask k1(1)
uint64_t y
Definition sha3.cpp:34
impl::ratio< uint64_t > ratio
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
Definition dtoa.c:492
int e0
Definition dtoa.c:492
int dp0
Definition dtoa.c:492
int scale
Definition dtoa.c:492
int uflchk
Definition dtoa.c:492
int dplen
Definition dtoa.c:492
int nd0
Definition dtoa.c:492
int dp1
Definition dtoa.c:492
int nd
Definition dtoa.c:492
int inexact
Definition dtoa.c:492
int dsign
Definition dtoa.c:492
int rounding
Definition dtoa.c:492
Definition dtoa.c:533
int k
Definition dtoa.c:535
struct Bigint * next
Definition dtoa.c:534
int sign
Definition dtoa.c:535
int maxwds
Definition dtoa.c:535
int wds
Definition dtoa.c:535
ULong x[1]
Definition dtoa.c:536
Definition dtoa.c:306
double d
Definition dtoa.c:306
void cmp(const Operand &op, uint32 imm)
void inc(const Operand &op)
CK_ULONG d
CK_RV ret
char * s
uint16_t j
size_t len
CK_RV rv