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