Ruby: missing/dtoa.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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