1 /****************************************************************
3 * The author of this software is David M. Gay.
5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
6 * Copyright (C) 2002, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose without fee is hereby granted, provided that this entire notice
10 * is included in all copies of any software which is or includes a copy
11 * or modification of this software and in all copies of the supporting
12 * documentation for such software.
14 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
15 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
16 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
17 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
19 ***************************************************************/
21 /* Please send bug reports to
23 Bell Laboratories, Room 2C-463
25 Murray Hill, NJ 07974-0636
30 /* On a machine with IEEE extended-precision registers, it is
31 * necessary to specify double-precision (53-bit) rounding precision
32 * before invoking strtod or dtoa. If the machine uses (the equivalent
33 * of) Intel 80x87 arithmetic, the call
34 * _control87(PC_53, MCW_PC);
35 * does this with many compilers. Whether this or another call is
36 * appropriate depends on the compiler; for this to work, it may be
37 * necessary to #include "float.h" or another system-dependent header
41 /* strtod for IEEE-arithmetic machines.
43 * This strtod returns a nearest machine number to the input decimal
44 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
45 * broken by the IEEE round-even rule. Otherwise ties are broken by
46 * biased rounding (add half and chop).
48 * Inspired loosely by William D. Clinger's paper "How to Read Floating
49 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
53 * 1. We only require IEEE.
54 * 2. We get by with floating-point arithmetic in a case that
55 * Clinger missed -- when we're computing d * 10^n
56 * for a small integer d and the integer n is not too
57 * much larger than 22 (the maximum integer k for which
58 * we can represent 10^k exactly), we may be able to
59 * compute (d*10^k) * 10^(e-k) with just one roundoff.
60 * 3. Rather than a bit-at-a-time adjustment of the binary
61 * result in the hard case, we use floating-point
62 * arithmetic to determine the adjustment to within
63 * one bit; only in really hard cases do we need to
64 * compute a second residual.
65 * 4. Because of 3., we don't need a large table of powers of 10
66 * for ten-to-e (just some small tables, e.g. of 10^k
71 * #define IEEE_8087 for IEEE-arithmetic machines where the least
72 * significant byte has the lowest address.
73 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
74 * significant byte has the lowest address.
75 * #define No_leftright to omit left-right logic in fast floating-point
76 * computation of dtoa.
77 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
78 * and Honor_FLT_ROUNDS is not #defined.
79 * #define Inaccurate_Divide for IEEE-format with correctly rounded
80 * products but inaccurate quotients, e.g., for Intel i860.
81 * #define USE_LONG_LONG on machines that have a "long long"
82 * integer type (of >= 64 bits), and performance testing shows that
83 * it is faster than 32-bit fallback (which is often not the case
84 * on 32-bit machines). On such machines, you can #define Just_16
85 * to store 16 bits per 32-bit int32_t when doing high-precision integer
86 * arithmetic. Whether this speeds things up or slows things down
87 * depends on the machine and the number being converted.
88 * #define Bad_float_h if your system lacks a float.h or if it does not
89 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
90 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
91 * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
92 * Infinity and NaN (case insensitively). On some systems (e.g.,
93 * some HP systems), it may be necessary to #define NAN_WORD0
94 * appropriately -- to the most significant word of a quiet NaN.
95 * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
96 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
97 * strtod also accepts (case insensitively) strings of the form
98 * NaN(x), where x is a string of hexadecimal digits and spaces;
99 * if there is only one string of hexadecimal digits, it is taken
100 * for the 52 fraction bits of the resulting NaN; if there are two
101 * or more strings of hex digits, the first is for the high 20 bits,
102 * the second and subsequent for the low 32 bits, with intervening
103 * white space ignored; but if this results in none of the 52
104 * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
105 * and NAN_WORD1 are used instead.
106 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
107 * avoids underflows on inputs whose result does not underflow.
108 * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
109 * floating-point numbers and flushes underflows to zero rather
110 * than implementing gradual underflow, then you must also #define
112 * #define YES_ALIAS to permit aliasing certain double values with
113 * arrays of ULongs. This leads to slightly better code with
114 * some compilers and was always used prior to 19990916, but it
115 * is not strictly legal and can cause trouble with aggressively
116 * optimizing compilers (e.g., gcc 2.95.1 under -O2).
117 * #define SET_INEXACT if IEEE arithmetic is being used and extra
118 * computation should be done to set the inexact flag when the
119 * result is inexact and avoid setting inexact when the result
120 * is exact. In this case, dtoa.c must be compiled in
121 * an environment, perhaps provided by #include "dtoa.c" in a
122 * suitable wrapper, that defines two functions,
123 * int get_inexact(void);
124 * void clear_inexact(void);
125 * such that get_inexact() returns a nonzero value if the
126 * inexact bit is already set, and clear_inexact() sets the
127 * inexact bit to 0. When SET_INEXACT is #defined, strtod
128 * also does extra computations to set the underflow and overflow
129 * flags when appropriate (i.e., when the result is tiny and
130 * inexact or when it is a numeric value rounded to +-infinity).
131 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
132 * the result overflows to +-Infinity or underflows to 0.
148 #include <wtf/AlwaysInline.h>
149 #include <wtf/Assertions.h>
150 #include <wtf/FastMalloc.h>
151 #include <wtf/Threading.h>
154 #pragma warning(disable: 4244)
155 #pragma warning(disable: 4245)
156 #pragma warning(disable: 4554)
159 #if PLATFORM(BIG_ENDIAN)
161 #elif PLATFORM(MIDDLE_ENDIAN)
169 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_ARM) != 1
170 Exactly one of IEEE_8087
, IEEE_ARM
or IEEE_MC68k should be defined
.
175 #if ENABLE(JSC_MULTIPLE_THREADS)
176 Mutex
* s_dtoaP5Mutex
;
179 typedef union { double d
; uint32_t L
[2]; } U
;
184 #define word0(x) ((uint32_t*)&x)[1]
185 #define word1(x) ((uint32_t*)&x)[0]
187 #define word0(x) ((uint32_t*)&x)[0]
188 #define word1(x) ((uint32_t*)&x)[1]
192 #define word0(x) ((U*)&x)->L[1]
193 #define word1(x) ((U*)&x)->L[0]
195 #define word0(x) ((U*)&x)->L[0]
196 #define word1(x) ((U*)&x)->L[1]
198 #define dval(x) ((U*)&x)->d
201 /* The following definition of Storeinc is appropriate for MIPS processors.
202 * An alternative that might be better on some machines is
203 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
205 #if defined(IEEE_8087) || defined(IEEE_ARM)
206 #define Storeinc(a,b,c) (((unsigned short*)a)[1] = (unsigned short)b, ((unsigned short*)a)[0] = (unsigned short)c, a++)
208 #define Storeinc(a,b,c) (((unsigned short*)a)[0] = (unsigned short)b, ((unsigned short*)a)[1] = (unsigned short)c, a++)
212 #define Exp_shift1 20
213 #define Exp_msk1 0x100000
214 #define Exp_msk11 0x100000
215 #define Exp_mask 0x7ff00000
219 #define Exp_1 0x3ff00000
220 #define Exp_11 0x3ff00000
222 #define Frac_mask 0xfffff
223 #define Frac_mask1 0xfffff
226 #define Bndry_mask 0xfffff
227 #define Bndry_mask1 0xfffff
229 #define Sign_bit 0x80000000
236 #if !defined(NO_IEEE_Scale)
237 #undef Avoid_Underflow
238 #define Avoid_Underflow
241 #if !defined(Flt_Rounds)
242 #if defined(FLT_ROUNDS)
243 #define Flt_Rounds FLT_ROUNDS
247 #endif /*Flt_Rounds*/
250 #define rounded_product(a,b) a *= b
251 #define rounded_quotient(a,b) a /= b
253 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
254 #define Big1 0xffffffff
260 #if PLATFORM(PPC64) || PLATFORM(X86_64)
261 // 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware.
262 #define USE_LONG_LONG
265 #ifndef USE_LONG_LONG
268 /* When Pack_32 is not defined, we store 16 bits per 32-bit int32_t.
269 * This makes some inner loops simpler and sometimes saves work
270 * during multiplications, but it often seems to make things slightly
271 * slower. Hence the default is now to store 32 bits per int32_t.
280 int k
, maxwds
, sign
, wds
;
284 static Bigint
* Balloc(int k
)
287 Bigint
* rv
= (Bigint
*)fastMalloc(sizeof(Bigint
) + (x
- 1)*sizeof(uint32_t));
291 rv
->sign
= rv
->wds
= 0;
296 static void Bfree(Bigint
* v
)
301 #define Bcopy(x, y) memcpy((char*)&x->sign, (char*)&y->sign, y->wds * sizeof(int32_t) + 2 * sizeof(int))
303 static Bigint
* multadd(Bigint
* b
, int m
, int a
) /* multiply by m and add a */
306 unsigned long long carry
;
317 unsigned long long y
= *x
* (unsigned long long)m
+ carry
;
319 *x
++ = (uint32_t)y
& 0xffffffffUL
;
323 uint32_t y
= (xi
& 0xffff) * m
+ carry
;
324 uint32_t z
= (xi
>> 16) * m
+ (y
>> 16);
326 *x
++ = (z
<< 16) + (y
& 0xffff);
328 uint32_t y
= *x
* m
+ carry
;
336 if (wds
>= b
->maxwds
) {
337 Bigint
* b1
= Balloc(b
->k
+ 1);
342 b
->x
[wds
++] = (uint32_t)carry
;
348 static Bigint
* s2b(const char* s
, int nd0
, int nd
, uint32_t y9
)
352 int32_t x
= (nd
+ 8) / 9;
354 for (k
= 0, y
= 1; x
> y
; y
<<= 1, k
++) { }
356 Bigint
* b
= Balloc(k
);
360 Bigint
* b
= Balloc(k
+ 1);
361 b
->x
[0] = y9
& 0xffff;
362 b
->wds
= (b
->x
[1] = y9
>> 16) ? 2 : 1;
369 b
= multadd(b
, 10, *s
++ - '0');
375 b
= multadd(b
, 10, *s
++ - '0');
379 static int hi0bits(uint32_t x
)
383 if (!(x
& 0xffff0000)) {
387 if (!(x
& 0xff000000)) {
391 if (!(x
& 0xf0000000)) {
395 if (!(x
& 0xc0000000)) {
399 if (!(x
& 0x80000000)) {
401 if (!(x
& 0x40000000))
407 static int lo0bits (uint32_t* y
)
449 static Bigint
* i2b(int i
)
459 static Bigint
* mult(Bigint
* a
, Bigint
* b
)
463 uint32_t *x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
466 unsigned long long carry
, z
;
471 if (a
->wds
< b
->wds
) {
483 for (x
= c
->x
, xa
= x
+ wc
; x
< xa
; x
++)
491 for (; xb
< xbe
; xc0
++) {
497 z
= *x
++ * (unsigned long long)y
+ *xc
+ carry
;
499 *xc
++ = (uint32_t)z
& 0xffffffffUL
;
501 *xc
= (uint32_t)carry
;
506 for (; xb
< xbe
; xb
++, xc0
++) {
507 if ((y
= *xb
& 0xffff)) {
512 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
514 uint32_t z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
520 if ((y
= *xb
>> 16)) {
526 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
529 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
536 for(; xb
< xbe
; xc0
++) {
542 z
= *x
++ * y
+ *xc
+ carry
;
551 for (xc0
= c
->x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) { }
557 static int p5s_count
;
559 static Bigint
* pow5mult(Bigint
* b
, int k
)
561 static int p05
[3] = { 5, 25, 125 };
564 b
= multadd(b
, p05
[i
- 1], 0);
569 #if ENABLE(JSC_MULTIPLE_THREADS)
570 s_dtoaP5Mutex
->lock();
578 int p5s_count_local
= p5s_count
;
579 #if ENABLE(JSC_MULTIPLE_THREADS)
580 s_dtoaP5Mutex
->unlock();
586 Bigint
* b1
= mult(b
, p5
);
593 if (++p5s_used
== p5s_count_local
) {
594 #if ENABLE(JSC_MULTIPLE_THREADS)
595 s_dtoaP5Mutex
->lock();
597 if (p5s_used
== p5s_count
) {
599 p5
->next
= mult(p5
, p5
);
603 p5s_count_local
= p5s_count
;
604 #if ENABLE(JSC_MULTIPLE_THREADS)
605 s_dtoaP5Mutex
->unlock();
614 static Bigint
* lshift(Bigint
* b
, int k
)
625 int n1
= n
+ b
->wds
+ 1;
626 for (int i
= b
->maxwds
; n1
> i
; i
<<= 1)
631 const uint32_t* srcStart
= b
->x
;
632 uint32_t* dstStart
= result
->x
;
633 const uint32_t* src
= srcStart
+ b
->wds
- 1;
634 uint32_t* dst
= dstStart
+ n1
- 1;
637 uint32_t hiSubword
= 0;
639 for (; src
>= srcStart
; --src
) {
640 *dst
-- = hiSubword
| *src
>> s
;
641 hiSubword
= *src
<< k
;
644 ASSERT(dst
== dstStart
+ n
);
645 result
->wds
= b
->wds
+ n
+ (result
->x
[n1
- 1] != 0);
649 uint32_t hiSubword
= 0;
651 for (; src
>= srcStart
; --src
) {
652 *dst
-- = hiSubword
| *src
>> s
;
653 hiSubword
= (*src
<< k
) & 0xffff;
656 ASSERT(dst
== dstStart
+ n
);
657 result
->wds
= b
->wds
+ n
+ (result
->x
[n1
- 1] != 0);
663 } while (src
>= srcStart
);
664 result
->wds
= b
->wds
+ n
;
666 for (dst
= dstStart
+ n
; dst
!= dstStart
; )
674 static int cmp(Bigint
* a
, Bigint
* b
)
676 uint32_t *xa
, *xa0
, *xb
, *xb0
;
681 ASSERT(i
<= 1 || a
->x
[i
- 1]);
682 ASSERT(j
<= 1 || b
->x
[j
- 1]);
691 return *xa
< *xb
? -1 : 1;
698 static Bigint
* diff(Bigint
* a
, Bigint
* b
)
702 uint32_t *xa
, *xae
, *xb
, *xbe
, *xc
;
728 unsigned long long borrow
= 0;
730 unsigned long long y
= (unsigned long long)*xa
++ - *xb
++ - borrow
;
731 borrow
= y
>> 32 & (uint32_t)1;
732 *xc
++ = (uint32_t)y
& 0xffffffffUL
;
735 unsigned long long y
= *xa
++ - borrow
;
736 borrow
= y
>> 32 & (uint32_t)1;
737 *xc
++ = (uint32_t)y
& 0xffffffffUL
;
743 uint32_t y
= (*xa
& 0xffff) - (*xb
& 0xffff) - borrow
;
744 borrow
= (y
& 0x10000) >> 16;
745 uint32_t z
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
;
746 borrow
= (z
& 0x10000) >> 16;
750 uint32_t y
= (*xa
& 0xffff) - borrow
;
751 borrow
= (y
& 0x10000) >> 16;
752 uint32_t z
= (*xa
++ >> 16) - borrow
;
753 borrow
= (z
& 0x10000) >> 16;
758 uint32_t y
= *xa
++ - *xb
++ - borrow
;
759 borrow
= (y
& 0x10000) >> 16;
763 uint32_t y
= *xa
++ - borrow
;
764 borrow
= (y
& 0x10000) >> 16;
775 static double ulp(double x
)
780 L
= (word0(x
) & Exp_mask
) - (P
- 1) * Exp_msk1
;
781 #ifndef Avoid_Underflow
782 #ifndef Sudden_Underflow
788 #ifndef Avoid_Underflow
789 #ifndef Sudden_Underflow
793 word0(a
) = 0x80000 >> L
;
798 word1(a
) = L
>= 31 ? 1 : 1 << 31 - L
;
806 static double b2d(Bigint
* a
, int* e
)
827 d0
= Exp_1
| y
>> Ebits
- k
;
828 w
= xa
> xa0
? *--xa
: 0;
829 d1
= y
<< (32 - Ebits
) + k
| w
>> Ebits
- k
;
832 z
= xa
> xa0
? *--xa
: 0;
834 d0
= Exp_1
| y
<< k
| z
>> 32 - k
;
835 y
= xa
> xa0
? *--xa
: 0;
836 d1
= z
<< k
| y
>> 32 - k
;
842 if (k
< Ebits
+ 16) {
843 z
= xa
> xa0
? *--xa
: 0;
844 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
845 w
= xa
> xa0
? *--xa
: 0;
846 y
= xa
> xa0
? *--xa
: 0;
847 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
850 z
= xa
> xa0
? *--xa
: 0;
851 w
= xa
> xa0
? *--xa
: 0;
853 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
854 y
= xa
> xa0
? *--xa
: 0;
855 d1
= w
<< k
+ 16 | y
<< k
;
863 static Bigint
* d2b(double d
, int* e
, int* bits
)
868 #ifndef Sudden_Underflow
882 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
883 #ifdef Sudden_Underflow
884 de
= (int)(d0
>> Exp_shift
);
886 if ((de
= (int)(d0
>> Exp_shift
)))
891 if ((k
= lo0bits(&y
))) {
892 x
[0] = y
| z
<< 32 - k
;
896 #ifndef Sudden_Underflow
899 b
->wds
= (x
[1] = z
) ? 2 : 1;
903 #ifndef Sudden_Underflow
911 if ((k
= lo0bits(&y
))) {
913 x
[0] = y
| z
<< 32 - k
& 0xffff;
914 x
[1] = z
>> k
- 16 & 0xffff;
919 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
920 x
[2] = z
>> k
& 0xffff;
946 #ifndef Sudden_Underflow
949 *e
= de
- Bias
- (P
- 1) + k
;
951 #ifndef Sudden_Underflow
953 *e
= de
- Bias
- (P
- 1) + 1 + k
;
955 *bits
= (32 * i
) - hi0bits(x
[i
- 1]);
957 *bits
= (i
+ 2) * 16 - hi0bits(x
[i
]);
966 static double ratio(Bigint
* a
, Bigint
* b
)
971 dval(da
) = b2d(a
, &ka
);
972 dval(db
) = b2d(b
, &kb
);
974 k
= ka
- kb
+ 32 * (a
->wds
- b
->wds
);
976 k
= ka
- kb
+ 16 * (a
->wds
- b
->wds
);
979 word0(da
) += k
* Exp_msk1
;
982 word0(db
) += k
* Exp_msk1
;
984 return dval(da
) / dval(db
);
987 static const double tens
[] = {
988 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
989 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
993 static const double bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
994 static const double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128,
995 #ifdef Avoid_Underflow
996 9007199254740992. * 9007199254740992.e
-256
997 /* = 2^106 * 1e-53 */
1003 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1004 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1005 #define Scale_Bit 0x10
1008 #if defined(INFNAN_CHECK)
1011 #define NAN_WORD0 0x7ff80000
1018 static int match(const char** sp
, const char* t
)
1021 const char* s
= *sp
;
1023 while ((d
= *t
++)) {
1024 if ((c
= *++s
) >= 'A' && c
<= 'Z')
1034 static void hexnan(double* rvp
, const char** sp
)
1038 int havedig
, udx0
, xshift
;
1041 havedig
= xshift
= 0;
1044 while ((c
= *(const unsigned char*)++s
)) {
1045 if (c
>= '0' && c
<= '9')
1047 else if (c
>= 'a' && c
<= 'f')
1049 else if (c
>= 'A' && c
<= 'F')
1051 else if (c
<= ' ') {
1052 if (udx0
&& havedig
) {
1057 } else if (/*(*/ c
== ')' && havedig
) {
1061 return; /* invalid form: don't change *sp */
1069 x
[0] = (x
[0] << 4) | (x
[1] >> 28);
1070 x
[1] = (x
[1] << 4) | c
;
1072 if ((x
[0] &= 0xfffff) || x
[1]) {
1073 word0(*rvp
) = Exp_mask
| x
[0];
1077 #endif /*No_Hex_NaN*/
1078 #endif /* INFNAN_CHECK */
1080 double strtod(const char* s00
, char** se
)
1082 #ifdef Avoid_Underflow
1085 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
,
1086 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
1087 const char *s
, *s0
, *s1
;
1088 double aadj
, aadj1
, adj
, rv
, rv0
;
1091 Bigint
*bb
= NULL
, *bb1
= NULL
, *bd
= NULL
, *bd0
= NULL
, *bs
= NULL
, *delta
= NULL
;
1093 int inexact
, oldinexact
;
1096 sign
= nz0
= nz
= 0;
1098 for (s
= s00
; ; s
++)
1122 while (*++s
== '0') { }
1128 for (nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
1130 y
= (10 * y
) + c
- '0';
1132 z
= (10 * z
) + c
- '0';
1137 for (; c
== '0'; c
= *++s
)
1139 if (c
> '0' && c
<= '9') {
1147 for (; c
>= '0' && c
<= '9'; c
= *++s
) {
1152 for (i
= 1; i
< nz
; i
++)
1155 else if (nd
<= DBL_DIG
+ 1)
1159 else if (nd
<= DBL_DIG
+ 1)
1167 if (c
== 'e' || c
== 'E') {
1168 if (!nd
&& !nz
&& !nz0
) {
1179 if (c
>= '0' && c
<= '9') {
1182 if (c
> '0' && c
<= '9') {
1185 while ((c
= *++s
) >= '0' && c
<= '9')
1186 L
= (10 * L
) + c
- '0';
1187 if (s
- s1
> 8 || L
> 19999)
1188 /* Avoid confusion from exponents
1189 * so large that e might overflow.
1191 e
= 19999; /* safe for 16 bit ints */
1204 /* Check for Nan and Infinity */
1208 if (match(&s
,"nf")) {
1210 if (!match(&s
,"inity"))
1212 word0(rv
) = 0x7ff00000;
1219 if (match(&s
, "an")) {
1220 word0(rv
) = NAN_WORD0
;
1221 word1(rv
) = NAN_WORD1
;
1223 if (*s
== '(') /*)*/
1229 #endif /* INFNAN_CHECK */
1238 /* Now we have nd0 digits, starting at s0, followed by a
1239 * decimal point, followed by nd-nd0 digits. The number we're
1240 * after is the integer represented by those digits times
1245 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
1250 oldinexact
= get_inexact();
1252 dval(rv
) = tens
[k
- 9] * dval(rv
) + z
;
1255 if (nd
<= DBL_DIG
&& Flt_Rounds
== 1) {
1259 if (e
<= Ten_pmax
) {
1260 /* rv = */ rounded_product(dval(rv
), tens
[e
]);
1264 if (e
<= Ten_pmax
+ i
) {
1265 /* A fancier test would sometimes let us do
1266 * this for larger i values.
1269 dval(rv
) *= tens
[i
];
1270 /* rv = */ rounded_product(dval(rv
), tens
[e
]);
1274 #ifndef Inaccurate_Divide
1275 else if (e
>= -Ten_pmax
) {
1276 /* rv = */ rounded_quotient(dval(rv
), tens
[-e
]);
1286 oldinexact
= get_inexact();
1288 #ifdef Avoid_Underflow
1292 /* Get starting approximation = rv * 10**e1 */
1296 dval(rv
) *= tens
[i
];
1298 if (e1
> DBL_MAX_10_EXP
) {
1303 /* Can't trust HUGE_VAL */
1304 word0(rv
) = Exp_mask
;
1307 /* set overflow bit */
1309 dval(rv0
) *= dval(rv0
);
1316 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
1318 dval(rv
) *= bigtens
[j
];
1319 /* The last multiplication could overflow. */
1320 word0(rv
) -= P
* Exp_msk1
;
1321 dval(rv
) *= bigtens
[j
];
1322 if ((z
= word0(rv
) & Exp_mask
) > Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
))
1324 if (z
> Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1 - P
)) {
1325 /* set to largest number */
1326 /* (Can't trust DBL_MAX) */
1330 word0(rv
) += P
* Exp_msk1
;
1332 } else if (e1
< 0) {
1335 dval(rv
) /= tens
[i
];
1337 if (e1
>= 1 << n_bigtens
)
1339 #ifdef Avoid_Underflow
1342 for (j
= 0; e1
> 0; j
++, e1
>>= 1)
1344 dval(rv
) *= tinytens
[j
];
1345 if (scale
&& (j
= (2 * P
) + 1 - ((word0(rv
) & Exp_mask
) >> Exp_shift
)) > 0) {
1346 /* scaled rv is denormal; zap j low bits */
1350 word0(rv
) = (P
+ 2) * Exp_msk1
;
1352 word0(rv
) &= 0xffffffff << j
- 32;
1354 word1(rv
) &= 0xffffffff << j
;
1357 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
1359 dval(rv
) *= tinytens
[j
];
1360 /* The last multiplication could underflow. */
1361 dval(rv0
) = dval(rv
);
1362 dval(rv
) *= tinytens
[j
];
1364 dval(rv
) = 2. * dval(rv0
);
1365 dval(rv
) *= tinytens
[j
];
1377 #ifndef Avoid_Underflow
1380 /* The refinement below will clean
1381 * this approximation up.
1388 /* Now the hard part -- adjusting rv to the correct value.*/
1390 /* Put digits into bd: true value = bd * 10^e */
1392 bd0
= s2b(s0
, nd0
, nd
, y
);
1395 bd
= Balloc(bd0
->k
);
1397 bb
= d2b(dval(rv
), &bbe
, &bbbits
); /* rv = bb * 2^bbe */
1412 #ifdef Avoid_Underflow
1414 i
= j
+ bbbits
- 1; /* logb(rv) */
1415 if (i
< Emin
) /* denormal */
1419 #else /*Avoid_Underflow*/
1420 #ifdef Sudden_Underflow
1422 #else /*Sudden_Underflow*/
1424 i
= j
+ bbbits
- 1; /* logb(rv) */
1425 if (i
< Emin
) /* denormal */
1429 #endif /*Sudden_Underflow*/
1430 #endif /*Avoid_Underflow*/
1433 #ifdef Avoid_Underflow
1436 i
= bb2
< bd2
? bb2
: bd2
;
1445 bs
= pow5mult(bs
, bb5
);
1451 bb
= lshift(bb
, bb2
);
1453 bd
= pow5mult(bd
, bd5
);
1455 bd
= lshift(bd
, bd2
);
1457 bs
= lshift(bs
, bs2
);
1458 delta
= diff(bb
, bd
);
1459 dsign
= delta
->sign
;
1464 /* Error is less than half an ulp -- check for
1465 * special case of mantissa a power of two.
1467 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
1468 #ifdef Avoid_Underflow
1469 || (word0(rv
) & Exp_mask
) <= (2 * P
+ 1) * Exp_msk1
1471 || (word0(rv
) & Exp_mask
) <= Exp_msk1
1475 if (!delta
->x
[0] && delta
->wds
<= 1)
1480 if (!delta
->x
[0] && delta
->wds
<= 1) {
1487 delta
= lshift(delta
,Log2P
);
1488 if (cmp(delta
, bs
) > 0)
1493 /* exactly half-way between */
1495 if ((word0(rv
) & Bndry_mask1
) == Bndry_mask1
1497 #ifdef Avoid_Underflow
1498 (scale
&& (y
= word0(rv
) & Exp_mask
) <= 2 * P
* Exp_msk1
)
1499 ? (0xffffffff & (0xffffffff << (2 * P
+ 1 - (y
>> Exp_shift
)))) :
1502 /*boundary case -- increment exponent*/
1503 word0(rv
) = (word0(rv
) & Exp_mask
) + Exp_msk1
;
1505 #ifdef Avoid_Underflow
1510 } else if (!(word0(rv
) & Bndry_mask
) && !word1(rv
)) {
1512 /* boundary case -- decrement exponent */
1513 #ifdef Sudden_Underflow /*{{*/
1514 L
= word0(rv
) & Exp_mask
;
1515 #ifdef Avoid_Underflow
1516 if (L
<= (scale
? (2 * P
+ 1) * Exp_msk1
: Exp_msk1
))
1519 #endif /*Avoid_Underflow*/
1522 #else /*Sudden_Underflow}{*/
1523 #ifdef Avoid_Underflow
1525 L
= word0(rv
) & Exp_mask
;
1526 if (L
<= (2 * P
+ 1) * Exp_msk1
) {
1527 if (L
> (P
+ 2) * Exp_msk1
)
1528 /* round even ==> */
1531 /* rv = smallest denormal */
1535 #endif /*Avoid_Underflow*/
1536 L
= (word0(rv
) & Exp_mask
) - Exp_msk1
;
1537 #endif /*Sudden_Underflow}}*/
1538 word0(rv
) = L
| Bndry_mask1
;
1539 word1(rv
) = 0xffffffff;
1542 if (!(word1(rv
) & LSB
))
1545 dval(rv
) += ulp(dval(rv
));
1547 dval(rv
) -= ulp(dval(rv
));
1548 #ifndef Sudden_Underflow
1553 #ifdef Avoid_Underflow
1558 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
1561 else if (word1(rv
) || word0(rv
) & Bndry_mask
) {
1562 #ifndef Sudden_Underflow
1563 if (word1(rv
) == Tiny1
&& !word0(rv
))
1569 /* special case -- power of FLT_RADIX to be */
1570 /* rounded down... */
1572 if (aadj
< 2. / FLT_RADIX
)
1573 aadj
= 1. / FLT_RADIX
;
1580 aadj1
= dsign
? aadj
: -aadj
;
1581 #ifdef Check_FLT_ROUNDS
1583 case 2: /* towards +infinity */
1586 case 0: /* towards 0 */
1587 case 3: /* towards -infinity */
1591 if (Flt_Rounds
== 0)
1593 #endif /*Check_FLT_ROUNDS*/
1595 y
= word0(rv
) & Exp_mask
;
1597 /* Check for overflow */
1599 if (y
== Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1)) {
1600 dval(rv0
) = dval(rv
);
1601 word0(rv
) -= P
* Exp_msk1
;
1602 adj
= aadj1
* ulp(dval(rv
));
1604 if ((word0(rv
) & Exp_mask
) >= Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
)) {
1605 if (word0(rv0
) == Big0
&& word1(rv0
) == Big1
)
1611 word0(rv
) += P
* Exp_msk1
;
1613 #ifdef Avoid_Underflow
1614 if (scale
&& y
<= 2 * P
* Exp_msk1
) {
1615 if (aadj
<= 0x7fffffff) {
1616 if ((z
= (uint32_t)aadj
) <= 0)
1619 aadj1
= dsign
? aadj
: -aadj
;
1621 word0(aadj1
) += (2 * P
+ 1) * Exp_msk1
- y
;
1623 adj
= aadj1
* ulp(dval(rv
));
1626 #ifdef Sudden_Underflow
1627 if ((word0(rv
) & Exp_mask
) <= P
* Exp_msk1
) {
1628 dval(rv0
) = dval(rv
);
1629 word0(rv
) += P
* Exp_msk1
;
1630 adj
= aadj1
* ulp(dval(rv
));
1632 if ((word0(rv
) & Exp_mask
) <= P
* Exp_msk1
)
1634 if (word0(rv0
) == Tiny0
&& word1(rv0
) == Tiny1
)
1641 word0(rv
) -= P
* Exp_msk1
;
1643 adj
= aadj1
* ulp(dval(rv
));
1646 #else /*Sudden_Underflow*/
1647 /* Compute adj so that the IEEE rounding rules will
1648 * correctly round rv + adj in some half-way cases.
1649 * If rv * ulp(rv) is denormalized (i.e.,
1650 * y <= (P - 1) * Exp_msk1), we must adjust aadj to avoid
1651 * trouble from bits lost to denormalization;
1652 * example: 1.2e-307 .
1654 if (y
<= (P
- 1) * Exp_msk1
&& aadj
> 1.) {
1655 aadj1
= (double)(int)(aadj
+ 0.5);
1659 adj
= aadj1
* ulp(dval(rv
));
1661 #endif /*Sudden_Underflow*/
1662 #endif /*Avoid_Underflow*/
1664 z
= word0(rv
) & Exp_mask
;
1666 #ifdef Avoid_Underflow
1670 /* Can we stop now? */
1673 /* The tolerances below are conservative. */
1674 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
) {
1675 if (aadj
< .4999999 || aadj
> .5000001)
1677 } else if (aadj
< .4999999 / FLT_RADIX
)
1690 word0(rv0
) = Exp_1
+ (70 << Exp_shift
);
1694 } else if (!oldinexact
)
1697 #ifdef Avoid_Underflow
1699 word0(rv0
) = Exp_1
- 2 * P
* Exp_msk1
;
1701 dval(rv
) *= dval(rv0
);
1703 /* try to avoid the bug of testing an 8087 register value */
1704 if (word0(rv
) == 0 && word1(rv
) == 0)
1708 #endif /* Avoid_Underflow */
1710 if (inexact
&& !(word0(rv
) & Exp_mask
)) {
1711 /* set underflow bit */
1713 dval(rv0
) *= dval(rv0
);
1724 *se
= const_cast<char*>(s
);
1725 return sign
? -dval(rv
) : dval(rv
);
1728 static int quorem(Bigint
* b
, Bigint
* S
)
1731 uint32_t *bx
, *bxe
, q
, *sx
, *sxe
;
1732 #ifdef USE_LONG_LONG
1733 unsigned long long borrow
, carry
, y
, ys
;
1735 uint32_t borrow
, carry
, y
, ys
;
1742 ASSERT_WITH_MESSAGE(b
->wds
<= n
, "oversize b in quorem");
1749 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
1750 ASSERT_WITH_MESSAGE(q
<= 9, "oversized quotient in quorem");
1755 #ifdef USE_LONG_LONG
1756 ys
= *sx
++ * (unsigned long long)q
+ carry
;
1758 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
1759 borrow
= y
>> 32 & (uint32_t)1;
1760 *bx
++ = (uint32_t)y
& 0xffffffffUL
;
1764 ys
= (si
& 0xffff) * q
+ carry
;
1765 zs
= (si
>> 16) * q
+ (ys
>> 16);
1767 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
1768 borrow
= (y
& 0x10000) >> 16;
1769 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
1770 borrow
= (z
& 0x10000) >> 16;
1773 ys
= *sx
++ * q
+ carry
;
1775 y
= *bx
- (ys
& 0xffff) - borrow
;
1776 borrow
= (y
& 0x10000) >> 16;
1780 } while (sx
<= sxe
);
1783 while (--bxe
> bx
&& !*bxe
)
1788 if (cmp(b
, S
) >= 0) {
1795 #ifdef USE_LONG_LONG
1798 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
1799 borrow
= y
>> 32 & (uint32_t)1;
1800 *bx
++ = (uint32_t)y
& 0xffffffffUL
;
1804 ys
= (si
& 0xffff) + carry
;
1805 zs
= (si
>> 16) + (ys
>> 16);
1807 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
1808 borrow
= (y
& 0x10000) >> 16;
1809 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
1810 borrow
= (z
& 0x10000) >> 16;
1815 y
= *bx
- (ys
& 0xffff) - borrow
;
1816 borrow
= (y
& 0x10000) >> 16;
1820 } while (sx
<= sxe
);
1824 while (--bxe
> bx
&& !*bxe
)
1832 #if !ENABLE(JSC_MULTIPLE_THREADS)
1833 static char* dtoa_result
;
1836 static char* rv_alloc(int i
)
1840 int j
= sizeof(uint32_t);
1842 sizeof(Bigint
) - sizeof(uint32_t) - sizeof(int) + j
<= (unsigned)i
;
1845 int* r
= (int*)Balloc(k
);
1848 #if !ENABLE(JSC_MULTIPLE_THREADS)
1854 static char* nrv_alloc(const char* s
, char** rve
, int n
)
1856 char* rv
= rv_alloc(n
);
1866 /* freedtoa(s) must be used to free values s returned by dtoa
1867 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
1868 * but for consistency with earlier versions of dtoa, it is optional
1869 * when MULTIPLE_THREADS is not defined.
1872 void freedtoa(char* s
)
1874 Bigint
* b
= (Bigint
*)((int*)s
- 1);
1875 b
->maxwds
= 1 << (b
->k
= *(int*)b
);
1877 #if !ENABLE(JSC_MULTIPLE_THREADS)
1878 if (s
== dtoa_result
)
1883 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1885 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1886 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1889 * 1. Rather than iterating, we use a simple numeric overestimate
1890 * to determine k = floor(log10(d)). We scale relevant
1891 * quantities using O(log2(k)) rather than O(k) multiplications.
1892 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1893 * try to generate digits strictly left to right. Instead, we
1894 * compute with fewer bits and propagate the carry if necessary
1895 * when rounding the final digit up. This is often faster.
1896 * 3. Under the assumption that input will be rounded nearest,
1897 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1898 * That is, we allow equality in stopping tests when the
1899 * round-nearest rule will give the same floating-point value
1900 * as would satisfaction of the stopping test with strict
1902 * 4. We remove common factors of powers of 2 from relevant
1904 * 5. When converting floating-point integers less than 1e16,
1905 * we use floating-point arithmetic rather than resorting
1906 * to multiple-precision integers.
1907 * 6. When asked to produce fewer than 15 digits, we first try
1908 * to get by with floating-point arithmetic; we resort to
1909 * multiple-precision integer arithmetic only if we cannot
1910 * guarantee that the floating-point calculation has given
1911 * the correctly rounded result. For k requested digits and
1912 * "uniformly" distributed input, the probability is
1913 * something like 10^(k-15) that we must resort to the int32_t
1917 char* dtoa(double d
, int ndigits
, int* decpt
, int* sign
, char** rve
)
1920 Arguments ndigits, decpt, sign are similar to those
1921 of ecvt and fcvt; trailing zeros are suppressed from
1922 the returned string. If not null, *rve is set to point
1923 to the end of the return value. If d is +-Infinity or NaN,
1924 then *decpt is set to 9999.
1928 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
= 0, ilim0
, ilim1
= 0,
1929 j
, j1
, k
, k0
, k_check
, leftright
, m2
, m5
, s2
, s5
,
1930 spec_case
, try_quick
;
1932 #ifndef Sudden_Underflow
1936 Bigint
*b
, *b1
, *delta
, *mlo
= NULL
, *mhi
, *S
;
1940 int inexact
, oldinexact
;
1943 #if !ENABLE(JSC_MULTIPLE_THREADS)
1945 freedtoa(dtoa_result
);
1950 if (word0(d
) & Sign_bit
) {
1951 /* set sign for everything, including 0's and NaNs */
1953 word0(d
) &= ~Sign_bit
; /* clear sign bit */
1957 if ((word0(d
) & Exp_mask
) == Exp_mask
)
1959 /* Infinity or NaN */
1961 if (!word1(d
) && !(word0(d
) & 0xfffff))
1962 return nrv_alloc("Infinity", rve
, 8);
1963 return nrv_alloc("NaN", rve
, 3);
1967 return nrv_alloc("0", rve
, 1);
1971 try_quick
= oldinexact
= get_inexact();
1975 b
= d2b(dval(d
), &be
, &bbits
);
1976 #ifdef Sudden_Underflow
1977 i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
));
1979 if ((i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
)))) {
1982 word0(d2
) &= Frac_mask1
;
1983 word0(d2
) |= Exp_11
;
1985 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
1986 * log10(x) = log(x) / log(10)
1987 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1988 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1990 * This suggests computing an approximation k to log10(d) by
1992 * k = (i - Bias)*0.301029995663981
1993 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1995 * We want k to be too large rather than too small.
1996 * The error in the first-order Taylor series approximation
1997 * is in our favor, so we just round up the constant enough
1998 * to compensate for any error in the multiplication of
1999 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2000 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2001 * adding 1e-13 to the constant term more than suffices.
2002 * Hence we adjust the constant term to 0.1760912590558.
2003 * (We could get a more accurate k by invoking log10,
2004 * but this is probably not worthwhile.)
2008 #ifndef Sudden_Underflow
2011 /* d is denormalized */
2013 i
= bbits
+ be
+ (Bias
+ (P
- 1) - 1);
2014 x
= i
> 32 ? word0(d
) << 64 - i
| word1(d
) >> i
- 32
2015 : word1(d
) << 32 - i
;
2017 word0(d2
) -= 31 * Exp_msk1
; /* adjust exponent */
2018 i
-= (Bias
+ (P
- 1) - 1) + 1;
2022 ds
= (dval(d2
) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i
* 0.301029995663981);
2024 if (ds
< 0. && ds
!= k
)
2025 k
--; /* want k = floor(ds) */
2027 if (k
>= 0 && k
<= Ten_pmax
) {
2028 if (dval(d
) < tens
[k
])
2051 #ifdef Check_FLT_ROUNDS
2052 try_quick
= Rounding
== 1;
2056 #endif /*SET_INEXACT*/
2062 s
= s0
= rv_alloc(i
);
2064 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
2066 /* Try to get by with floating-point arithmetic. */
2072 ieps
= 2; /* conservative */
2077 /* prevent overflows */
2079 dval(d
) /= bigtens
[n_bigtens
- 1];
2082 for (; j
; j
>>= 1, i
++) {
2089 } else if ((j1
= -k
)) {
2090 dval(d
) *= tens
[j1
& 0xf];
2091 for (j
= j1
>> 4; j
; j
>>= 1, i
++) {
2094 dval(d
) *= bigtens
[i
];
2098 if (k_check
&& dval(d
) < 1. && ilim
> 0) {
2106 dval(eps
) = (ieps
* dval(d
)) + 7.;
2107 word0(eps
) -= (P
- 1) * Exp_msk1
;
2111 if (dval(d
) > dval(eps
))
2113 if (dval(d
) < -dval(eps
))
2117 #ifndef No_leftright
2119 /* Use Steele & White method of only
2120 * generating digits needed.
2122 dval(eps
) = (0.5 / tens
[ilim
- 1]) - dval(eps
);
2124 L
= (long int)dval(d
);
2126 *s
++ = '0' + (int)L
;
2127 if (dval(d
) < dval(eps
))
2129 if (1. - dval(d
) < dval(eps
))
2138 /* Generate ilim digits, then fix them up. */
2139 dval(eps
) *= tens
[ilim
- 1];
2140 for (i
= 1;; i
++, dval(d
) *= 10.) {
2141 L
= (int32_t)(dval(d
));
2142 if (!(dval(d
) -= L
))
2144 *s
++ = '0' + (int)L
;
2146 if (dval(d
) > 0.5 + dval(eps
))
2148 else if (dval(d
) < 0.5 - dval(eps
)) {
2149 while (*--s
== '0') { }
2156 #ifndef No_leftright
2166 /* Do we have a "small" integer? */
2168 if (be
>= 0 && k
<= Int_max
) {
2171 if (ndigits
< 0 && ilim
<= 0) {
2173 if (ilim
< 0 || dval(d
) <= 5 * ds
)
2177 for (i
= 1;; i
++, dval(d
) *= 10.) {
2178 L
= (int32_t)(dval(d
) / ds
);
2180 #ifdef Check_FLT_ROUNDS
2181 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2187 *s
++ = '0' + (int)L
;
2196 if (dval(d
) > ds
|| dval(d
) == ds
&& L
& 1) {
2217 #ifndef Sudden_Underflow
2218 denorm
? be
+ (Bias
+ (P
- 1) - 1 + 1) :
2225 if (m2
> 0 && s2
> 0) {
2226 i
= m2
< s2
? m2
: s2
;
2234 mhi
= pow5mult(mhi
, m5
);
2242 b
= pow5mult(b
, b5
);
2246 S
= pow5mult(S
, s5
);
2248 /* Check for special case that d is a normalized power of 2. */
2251 if (!word1(d
) && !(word0(d
) & Bndry_mask
)
2252 #ifndef Sudden_Underflow
2253 && word0(d
) & (Exp_mask
& ~Exp_msk1
)
2256 /* The special case */
2262 /* Arrange for convenient computation of quotients:
2263 * shift left if necessary so divisor has 4 leading 0 bits.
2265 * Perhaps we should just compute leading 28 bits of S once
2266 * and for all and pass them and a shift to quorem, so it
2267 * can do shifts and ors to compute the numerator for q.
2270 if ((i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
- 1]) : 1) + s2
) & 0x1f))
2273 if ((i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
- 1]) : 1) + s2
) & 0xf))
2294 b
= multadd(b
, 10, 0); /* we botched the k estimate */
2296 mhi
= multadd(mhi
, 10, 0);
2303 mhi
= lshift(mhi
, m2
);
2305 /* Compute mlo -- check for special case
2306 * that d is a normalized power of 2.
2311 mhi
= Balloc(mhi
->k
);
2313 mhi
= lshift(mhi
, Log2P
);
2317 dig
= quorem(b
,S
) + '0';
2318 /* Do we yet have the shortest decimal string
2319 * that will round to d?
2322 delta
= diff(S
, mhi
);
2323 j1
= delta
->sign
? 1 : cmp(b
, delta
);
2325 if (j1
== 0 && !(word1(d
) & 1)) {
2331 else if (!b
->x
[0] && b
->wds
<= 1)
2337 if (j
< 0 || j
== 0 && !(word1(d
) & 1)) {
2338 if (!b
->x
[0] && b
->wds
<= 1) {
2347 if ((j1
> 0 || j1
== 0 && dig
& 1) && dig
++ == '9')
2355 if (dig
== '9') { /* possible if i == 1 */
2366 b
= multadd(b
, 10, 0);
2368 mlo
= mhi
= multadd(mhi
, 10, 0);
2370 mlo
= multadd(mlo
, 10, 0);
2371 mhi
= multadd(mhi
, 10, 0);
2376 *s
++ = dig
= quorem(b
,S
) + '0';
2377 if (!b
->x
[0] && b
->wds
<= 1) {
2385 b
= multadd(b
, 10, 0);
2388 /* Round off last digit */
2392 if (j
> 0 || j
== 0 && dig
& 1) {
2402 while (*--s
== '0') { }
2416 if (mlo
&& mlo
!= mhi
)
2424 word0(d
) = Exp_1
+ (70 << Exp_shift
);
2428 } else if (!oldinexact
)