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