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.
147 #include <wtf/AlwaysInline.h>
148 #include <wtf/Assertions.h>
149 #include <wtf/FastMalloc.h>
150 #include <wtf/MathExtras.h>
151 #include <wtf/Vector.h>
152 #include <wtf/Threading.h>
157 #pragma warning(disable: 4244)
158 #pragma warning(disable: 4245)
159 #pragma warning(disable: 4554)
164 #elif CPU(MIDDLE_ENDIAN)
172 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_ARM) != 1
173 Exactly one of IEEE_8087
, IEEE_ARM
or IEEE_MC68k should be defined
.
178 #if ENABLE(JSC_MULTIPLE_THREADS)
179 Mutex
* s_dtoaP5Mutex
;
182 typedef union { double d
; uint32_t L
[2]; } U
;
187 #define word0(x) ((uint32_t*)&x)[1]
188 #define word1(x) ((uint32_t*)&x)[0]
190 #define word0(x) ((uint32_t*)&x)[0]
191 #define word1(x) ((uint32_t*)&x)[1]
195 #define word0(x) (x)->L[1]
196 #define word1(x) (x)->L[0]
198 #define word0(x) (x)->L[0]
199 #define word1(x) (x)->L[1]
201 #define dval(x) (x)->d
204 /* The following definition of Storeinc is appropriate for MIPS processors.
205 * An alternative that might be better on some machines is
206 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
208 #if defined(IEEE_8087) || defined(IEEE_ARM)
209 #define Storeinc(a,b,c) (((unsigned short*)a)[1] = (unsigned short)b, ((unsigned short*)a)[0] = (unsigned short)c, a++)
211 #define Storeinc(a,b,c) (((unsigned short*)a)[0] = (unsigned short)b, ((unsigned short*)a)[1] = (unsigned short)c, a++)
215 #define Exp_shift1 20
216 #define Exp_msk1 0x100000
217 #define Exp_msk11 0x100000
218 #define Exp_mask 0x7ff00000
222 #define Exp_1 0x3ff00000
223 #define Exp_11 0x3ff00000
225 #define Frac_mask 0xfffff
226 #define Frac_mask1 0xfffff
229 #define Bndry_mask 0xfffff
230 #define Bndry_mask1 0xfffff
232 #define Sign_bit 0x80000000
239 #if !defined(NO_IEEE_Scale)
240 #undef Avoid_Underflow
241 #define Avoid_Underflow
244 #if !defined(Flt_Rounds)
245 #if defined(FLT_ROUNDS)
246 #define Flt_Rounds FLT_ROUNDS
250 #endif /*Flt_Rounds*/
253 #define rounded_product(a,b) a *= b
254 #define rounded_quotient(a,b) a /= b
256 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
257 #define Big1 0xffffffff
260 // FIXME: we should remove non-Pack_32 mode since it is unused and unmaintained
265 #if CPU(PPC64) || CPU(X86_64)
266 // FIXME: should we enable this on all 64-bit CPUs?
267 // 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware.
268 #define USE_LONG_LONG
271 #ifndef USE_LONG_LONG
274 /* When Pack_32 is not defined, we store 16 bits per 32-bit int32_t.
275 * This makes some inner loops simpler and sometimes saves work
276 * during multiplications, but it often seems to make things slightly
277 * slower. Hence the default is now to store 32 bits per int32_t.
285 BigInt() : sign(0) { }
296 return m_words
.size();
299 void resize(size_t s
)
306 return m_words
.data();
309 const uint32_t* words() const
311 return m_words
.data();
314 void append(uint32_t w
)
319 Vector
<uint32_t, 16> m_words
;
322 static void multadd(BigInt
& b
, int m
, int a
) /* multiply by m and add a */
325 unsigned long long carry
;
331 uint32_t* x
= b
.words();
336 unsigned long long y
= *x
* (unsigned long long)m
+ carry
;
338 *x
++ = (uint32_t)y
& 0xffffffffUL
;
342 uint32_t y
= (xi
& 0xffff) * m
+ carry
;
343 uint32_t z
= (xi
>> 16) * m
+ (y
>> 16);
345 *x
++ = (z
<< 16) + (y
& 0xffff);
347 uint32_t y
= *x
* m
+ carry
;
355 b
.append((uint32_t)carry
);
358 static void s2b(BigInt
& b
, const char* s
, int nd0
, int nd
, uint32_t y9
)
362 int32_t x
= (nd
+ 8) / 9;
364 for (k
= 0, y
= 1; x
> y
; y
<<= 1, k
++) { }
371 b
.resize((b
->x
[1] = y9
>> 16) ? 2 : 1);
372 b
.words()[0] = y9
& 0xffff;
379 multadd(b
, 10, *s
++ - '0');
385 multadd(b
, 10, *s
++ - '0');
388 static int hi0bits(uint32_t x
)
392 if (!(x
& 0xffff0000)) {
396 if (!(x
& 0xff000000)) {
400 if (!(x
& 0xf0000000)) {
404 if (!(x
& 0xc0000000)) {
408 if (!(x
& 0x80000000)) {
410 if (!(x
& 0x40000000))
416 static int lo0bits (uint32_t* y
)
458 static void i2b(BigInt
& b
, int i
)
465 static void mult(BigInt
& aRef
, const BigInt
& bRef
)
467 const BigInt
* a
= &aRef
;
468 const BigInt
* b
= &bRef
;
471 const uint32_t *x
= 0, *xa
, *xb
, *xae
, *xbe
;
475 unsigned long long carry
, z
;
480 if (a
->size() < b
->size()) {
481 const BigInt
* tmp
= a
;
491 for (xc
= c
.words(), xa
= xc
+ wc
; xc
< xa
; xc
++)
499 for (; xb
< xbe
; xc0
++) {
505 z
= *x
++ * (unsigned long long)y
+ *xc
+ carry
;
507 *xc
++ = (uint32_t)z
& 0xffffffffUL
;
509 *xc
= (uint32_t)carry
;
514 for (; xb
< xbe
; xb
++, xc0
++) {
515 if ((y
= *xb
& 0xffff)) {
520 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
522 uint32_t z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
528 if ((y
= *xb
>> 16)) {
534 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
537 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
544 for(; xb
< xbe
; xc0
++) {
550 z
= *x
++ * y
+ *xc
+ carry
;
559 for (xc0
= c
.words(), xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) { }
564 struct P5Node
: Noncopyable
{
570 static int p5s_count
;
572 static ALWAYS_INLINE
void pow5mult(BigInt
& b
, int k
)
574 static int p05
[3] = { 5, 25, 125 };
577 multadd(b
, p05
[i
- 1], 0);
582 #if ENABLE(JSC_MULTIPLE_THREADS)
583 s_dtoaP5Mutex
->lock();
596 int p5s_count_local
= p5s_count
;
597 #if ENABLE(JSC_MULTIPLE_THREADS)
598 s_dtoaP5Mutex
->unlock();
609 if (++p5s_used
== p5s_count_local
) {
610 #if ENABLE(JSC_MULTIPLE_THREADS)
611 s_dtoaP5Mutex
->lock();
613 if (p5s_used
== p5s_count
) {
615 p5
->next
= new P5Node
;
617 p5
->next
->val
= p5
->val
;
618 mult(p5
->next
->val
, p5
->next
->val
);
622 p5s_count_local
= p5s_count
;
623 #if ENABLE(JSC_MULTIPLE_THREADS)
624 s_dtoaP5Mutex
->unlock();
631 static ALWAYS_INLINE
void lshift(BigInt
& b
, int k
)
639 int origSize
= b
.size();
640 int n1
= n
+ origSize
+ 1;
643 b
.resize(b
.size() + n
+ 1);
645 b
.resize(b
.size() + n
);
647 const uint32_t* srcStart
= b
.words();
648 uint32_t* dstStart
= b
.words();
649 const uint32_t* src
= srcStart
+ origSize
- 1;
650 uint32_t* dst
= dstStart
+ n1
- 1;
653 uint32_t hiSubword
= 0;
655 for (; src
>= srcStart
; --src
) {
656 *dst
-- = hiSubword
| *src
>> s
;
657 hiSubword
= *src
<< k
;
660 ASSERT(dst
== dstStart
+ n
);
662 b
.resize(origSize
+ n
+ (b
.words()[n1
- 1] != 0));
666 uint32_t hiSubword
= 0;
668 for (; src
>= srcStart
; --src
) {
669 *dst
-- = hiSubword
| *src
>> s
;
670 hiSubword
= (*src
<< k
) & 0xffff;
673 ASSERT(dst
== dstStart
+ n
);
674 result
->wds
= b
->wds
+ n
+ (result
->x
[n1
- 1] != 0);
680 } while (src
>= srcStart
);
682 for (dst
= dstStart
+ n
; dst
!= dstStart
; )
685 ASSERT(b
.size() <= 1 || b
.words()[b
.size() - 1]);
688 static int cmp(const BigInt
& a
, const BigInt
& b
)
690 const uint32_t *xa
, *xa0
, *xb
, *xb0
;
695 ASSERT(i
<= 1 || a
.words()[i
- 1]);
696 ASSERT(j
<= 1 || b
.words()[j
- 1]);
705 return *xa
< *xb
? -1 : 1;
712 static ALWAYS_INLINE
void diff(BigInt
& c
, const BigInt
& aRef
, const BigInt
& bRef
)
714 const BigInt
* a
= &aRef
;
715 const BigInt
* b
= &bRef
;
727 const BigInt
* tmp
= a
;
735 const uint32_t* xa
= a
->words();
736 const uint32_t* xae
= xa
+ wa
;
738 const uint32_t* xb
= b
->words();
739 const uint32_t* xbe
= xb
+ wb
;
745 unsigned long long borrow
= 0;
747 unsigned long long y
= (unsigned long long)*xa
++ - *xb
++ - borrow
;
748 borrow
= y
>> 32 & (uint32_t)1;
749 *xc
++ = (uint32_t)y
& 0xffffffffUL
;
752 unsigned long long y
= *xa
++ - borrow
;
753 borrow
= y
>> 32 & (uint32_t)1;
754 *xc
++ = (uint32_t)y
& 0xffffffffUL
;
760 uint32_t y
= (*xa
& 0xffff) - (*xb
& 0xffff) - borrow
;
761 borrow
= (y
& 0x10000) >> 16;
762 uint32_t z
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
;
763 borrow
= (z
& 0x10000) >> 16;
767 uint32_t y
= (*xa
& 0xffff) - borrow
;
768 borrow
= (y
& 0x10000) >> 16;
769 uint32_t z
= (*xa
++ >> 16) - borrow
;
770 borrow
= (z
& 0x10000) >> 16;
775 uint32_t y
= *xa
++ - *xb
++ - borrow
;
776 borrow
= (y
& 0x10000) >> 16;
780 uint32_t y
= *xa
++ - borrow
;
781 borrow
= (y
& 0x10000) >> 16;
791 static double ulp(U
*x
)
796 L
= (word0(x
) & Exp_mask
) - (P
- 1) * Exp_msk1
;
797 #ifndef Avoid_Underflow
798 #ifndef Sudden_Underflow
804 #ifndef Avoid_Underflow
805 #ifndef Sudden_Underflow
809 word0(&u
) = 0x80000 >> L
;
814 word1(&u
) = L
>= 31 ? 1 : 1 << 31 - L
;
822 static double b2d(const BigInt
& a
, int* e
)
843 d0
= Exp_1
| (y
>> (Ebits
- k
));
844 w
= xa
> xa0
? *--xa
: 0;
845 d1
= (y
<< (32 - Ebits
+ k
)) | (w
>> (Ebits
- k
));
848 z
= xa
> xa0
? *--xa
: 0;
850 d0
= Exp_1
| (y
<< k
) | (z
>> (32 - k
));
851 y
= xa
> xa0
? *--xa
: 0;
852 d1
= (z
<< k
) | (y
>> (32 - k
));
858 if (k
< Ebits
+ 16) {
859 z
= xa
> xa0
? *--xa
: 0;
860 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
861 w
= xa
> xa0
? *--xa
: 0;
862 y
= xa
> xa0
? *--xa
: 0;
863 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
866 z
= xa
> xa0
? *--xa
: 0;
867 w
= xa
> xa0
? *--xa
: 0;
869 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
870 y
= xa
> xa0
? *--xa
: 0;
871 d1
= w
<< k
+ 16 | y
<< k
;
879 static ALWAYS_INLINE
void d2b(BigInt
& b
, U
* d
, int* e
, int* bits
)
883 #ifndef Sudden_Underflow
898 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
899 #ifdef Sudden_Underflow
900 de
= (int)(d0
>> Exp_shift
);
902 if ((de
= (int)(d0
>> Exp_shift
)))
907 if ((k
= lo0bits(&y
))) {
908 x
[0] = y
| (z
<< (32 - k
));
917 #ifndef Sudden_Underflow
923 #ifndef Sudden_Underflow
931 if ((k
= lo0bits(&y
))) {
933 x
[0] = y
| z
<< 32 - k
& 0xffff;
934 x
[1] = z
>> k
- 16 & 0xffff;
939 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
940 x
[2] = z
>> k
& 0xffff;
966 #ifndef Sudden_Underflow
969 *e
= de
- Bias
- (P
- 1) + k
;
971 #ifndef Sudden_Underflow
973 *e
= de
- Bias
- (P
- 1) + 1 + k
;
975 *bits
= (32 * i
) - hi0bits(x
[i
- 1]);
977 *bits
= (i
+ 2) * 16 - hi0bits(x
[i
]);
985 static double ratio(const BigInt
& a
, const BigInt
& b
)
990 dval(&da
) = b2d(a
, &ka
);
991 dval(&db
) = b2d(b
, &kb
);
993 k
= ka
- kb
+ 32 * (a
.size() - b
.size());
995 k
= ka
- kb
+ 16 * (a
.size() - b
.size());
998 word0(&da
) += k
* Exp_msk1
;
1001 word0(&db
) += k
* Exp_msk1
;
1003 return dval(&da
) / dval(&db
);
1006 static const double tens
[] = {
1007 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
1008 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
1012 static const double bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1013 static const double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1014 #ifdef Avoid_Underflow
1015 9007199254740992. * 9007199254740992.e
-256
1016 /* = 2^106 * 1e-53 */
1022 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1023 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1024 #define Scale_Bit 0x10
1027 #if defined(INFNAN_CHECK)
1030 #define NAN_WORD0 0x7ff80000
1037 static int match(const char** sp
, const char* t
)
1040 const char* s
= *sp
;
1042 while ((d
= *t
++)) {
1043 if ((c
= *++s
) >= 'A' && c
<= 'Z')
1053 static void hexnan(U
* rvp
, const char** sp
)
1057 int havedig
, udx0
, xshift
;
1060 havedig
= xshift
= 0;
1063 while ((c
= *(const unsigned char*)++s
)) {
1064 if (c
>= '0' && c
<= '9')
1066 else if (c
>= 'a' && c
<= 'f')
1068 else if (c
>= 'A' && c
<= 'F')
1070 else if (c
<= ' ') {
1071 if (udx0
&& havedig
) {
1076 } else if (/*(*/ c
== ')' && havedig
) {
1080 return; /* invalid form: don't change *sp */
1088 x
[0] = (x
[0] << 4) | (x
[1] >> 28);
1089 x
[1] = (x
[1] << 4) | c
;
1091 if ((x
[0] &= 0xfffff) || x
[1]) {
1092 word0(rvp
) = Exp_mask
| x
[0];
1096 #endif /*No_Hex_NaN*/
1097 #endif /* INFNAN_CHECK */
1099 double strtod(const char* s00
, char** se
)
1101 #ifdef Avoid_Underflow
1104 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
,
1105 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
1106 const char *s
, *s0
, *s1
;
1108 U aadj2
, adj
, rv
, rv0
;
1111 BigInt bb
, bb1
, bd
, bd0
, bs
, delta
;
1113 int inexact
, oldinexact
;
1116 sign
= nz0
= nz
= 0;
1118 for (s
= s00
; ; s
++)
1142 while (*++s
== '0') { }
1148 for (nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
1150 y
= (10 * y
) + c
- '0';
1152 z
= (10 * z
) + c
- '0';
1157 for (; c
== '0'; c
= *++s
)
1159 if (c
> '0' && c
<= '9') {
1167 for (; c
>= '0' && c
<= '9'; c
= *++s
) {
1172 for (i
= 1; i
< nz
; i
++)
1175 else if (nd
<= DBL_DIG
+ 1)
1179 else if (nd
<= DBL_DIG
+ 1)
1187 if (c
== 'e' || c
== 'E') {
1188 if (!nd
&& !nz
&& !nz0
) {
1199 if (c
>= '0' && c
<= '9') {
1202 if (c
> '0' && c
<= '9') {
1205 while ((c
= *++s
) >= '0' && c
<= '9')
1206 L
= (10 * L
) + c
- '0';
1207 if (s
- s1
> 8 || L
> 19999)
1208 /* Avoid confusion from exponents
1209 * so large that e might overflow.
1211 e
= 19999; /* safe for 16 bit ints */
1224 /* Check for Nan and Infinity */
1228 if (match(&s
,"nf")) {
1230 if (!match(&s
,"inity"))
1232 word0(&rv
) = 0x7ff00000;
1239 if (match(&s
, "an")) {
1240 word0(&rv
) = NAN_WORD0
;
1241 word1(&rv
) = NAN_WORD1
;
1243 if (*s
== '(') /*)*/
1249 #endif /* INFNAN_CHECK */
1258 /* Now we have nd0 digits, starting at s0, followed by a
1259 * decimal point, followed by nd-nd0 digits. The number we're
1260 * after is the integer represented by those digits times
1265 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
1270 oldinexact
= get_inexact();
1272 dval(&rv
) = tens
[k
- 9] * dval(&rv
) + z
;
1274 if (nd
<= DBL_DIG
&& Flt_Rounds
== 1) {
1278 if (e
<= Ten_pmax
) {
1279 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
1283 if (e
<= Ten_pmax
+ i
) {
1284 /* A fancier test would sometimes let us do
1285 * this for larger i values.
1288 dval(&rv
) *= tens
[i
];
1289 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
1293 #ifndef Inaccurate_Divide
1294 else if (e
>= -Ten_pmax
) {
1295 /* rv = */ rounded_quotient(dval(&rv
), tens
[-e
]);
1305 oldinexact
= get_inexact();
1307 #ifdef Avoid_Underflow
1311 /* Get starting approximation = rv * 10**e1 */
1315 dval(&rv
) *= tens
[i
];
1317 if (e1
> DBL_MAX_10_EXP
) {
1322 /* Can't trust HUGE_VAL */
1323 word0(&rv
) = Exp_mask
;
1326 /* set overflow bit */
1328 dval(&rv0
) *= dval(&rv0
);
1333 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
1335 dval(&rv
) *= bigtens
[j
];
1336 /* The last multiplication could overflow. */
1337 word0(&rv
) -= P
* Exp_msk1
;
1338 dval(&rv
) *= bigtens
[j
];
1339 if ((z
= word0(&rv
) & Exp_mask
) > Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
))
1341 if (z
> Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1 - P
)) {
1342 /* set to largest number */
1343 /* (Can't trust DBL_MAX) */
1347 word0(&rv
) += P
* Exp_msk1
;
1349 } else if (e1
< 0) {
1352 dval(&rv
) /= tens
[i
];
1354 if (e1
>= 1 << n_bigtens
)
1356 #ifdef Avoid_Underflow
1359 for (j
= 0; e1
> 0; j
++, e1
>>= 1)
1361 dval(&rv
) *= tinytens
[j
];
1362 if (scale
&& (j
= (2 * P
) + 1 - ((word0(&rv
) & Exp_mask
) >> Exp_shift
)) > 0) {
1363 /* scaled rv is denormal; zap j low bits */
1367 word0(&rv
) = (P
+ 2) * Exp_msk1
;
1369 word0(&rv
) &= 0xffffffff << (j
- 32);
1371 word1(&rv
) &= 0xffffffff << j
;
1374 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
1376 dval(&rv
) *= tinytens
[j
];
1377 /* The last multiplication could underflow. */
1378 dval(&rv0
) = dval(&rv
);
1379 dval(&rv
) *= tinytens
[j
];
1381 dval(&rv
) = 2. * dval(&rv0
);
1382 dval(&rv
) *= tinytens
[j
];
1392 #ifndef Avoid_Underflow
1395 /* The refinement below will clean
1396 * this approximation up.
1403 /* Now the hard part -- adjusting rv to the correct value.*/
1405 /* Put digits into bd: true value = bd * 10^e */
1407 s2b(bd0
, s0
, nd0
, nd
, y
);
1411 d2b(bb
, &rv
, &bbe
, &bbbits
); /* rv = bb * 2^bbe */
1426 #ifdef Avoid_Underflow
1428 i
= j
+ bbbits
- 1; /* logb(rv) */
1429 if (i
< Emin
) /* denormal */
1433 #else /*Avoid_Underflow*/
1434 #ifdef Sudden_Underflow
1436 #else /*Sudden_Underflow*/
1438 i
= j
+ bbbits
- 1; /* logb(rv) */
1439 if (i
< Emin
) /* denormal */
1443 #endif /*Sudden_Underflow*/
1444 #endif /*Avoid_Underflow*/
1447 #ifdef Avoid_Underflow
1450 i
= bb2
< bd2
? bb2
: bd2
;
1470 diff(delta
, bb
, bd
);
1476 /* Error is less than half an ulp -- check for
1477 * special case of mantissa a power of two.
1479 if (dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
1480 #ifdef Avoid_Underflow
1481 || (word0(&rv
) & Exp_mask
) <= (2 * P
+ 1) * Exp_msk1
1483 || (word0(&rv
) & Exp_mask
) <= Exp_msk1
1487 if (!delta
->words()[0] && delta
->size() <= 1)
1492 if (!delta
.words()[0] && delta
.size() <= 1) {
1499 lshift(delta
, Log2P
);
1500 if (cmp(delta
, bs
) > 0)
1505 /* exactly half-way between */
1507 if ((word0(&rv
) & Bndry_mask1
) == Bndry_mask1
1509 #ifdef Avoid_Underflow
1510 (scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2 * P
* Exp_msk1
)
1511 ? (0xffffffff & (0xffffffff << (2 * P
+ 1 - (y
>> Exp_shift
)))) :
1514 /*boundary case -- increment exponent*/
1515 word0(&rv
) = (word0(&rv
) & Exp_mask
) + Exp_msk1
;
1517 #ifdef Avoid_Underflow
1522 } else if (!(word0(&rv
) & Bndry_mask
) && !word1(&rv
)) {
1524 /* boundary case -- decrement exponent */
1525 #ifdef Sudden_Underflow /*{{*/
1526 L
= word0(&rv
) & Exp_mask
;
1527 #ifdef Avoid_Underflow
1528 if (L
<= (scale
? (2 * P
+ 1) * Exp_msk1
: Exp_msk1
))
1531 #endif /*Avoid_Underflow*/
1534 #else /*Sudden_Underflow}{*/
1535 #ifdef Avoid_Underflow
1537 L
= word0(&rv
) & Exp_mask
;
1538 if (L
<= (2 * P
+ 1) * Exp_msk1
) {
1539 if (L
> (P
+ 2) * Exp_msk1
)
1540 /* round even ==> */
1543 /* rv = smallest denormal */
1547 #endif /*Avoid_Underflow*/
1548 L
= (word0(&rv
) & Exp_mask
) - Exp_msk1
;
1549 #endif /*Sudden_Underflow}}*/
1550 word0(&rv
) = L
| Bndry_mask1
;
1551 word1(&rv
) = 0xffffffff;
1554 if (!(word1(&rv
) & LSB
))
1557 dval(&rv
) += ulp(&rv
);
1559 dval(&rv
) -= ulp(&rv
);
1560 #ifndef Sudden_Underflow
1565 #ifdef Avoid_Underflow
1570 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
1573 else if (word1(&rv
) || word0(&rv
) & Bndry_mask
) {
1574 #ifndef Sudden_Underflow
1575 if (word1(&rv
) == Tiny1
&& !word0(&rv
))
1581 /* special case -- power of FLT_RADIX to be */
1582 /* rounded down... */
1584 if (aadj
< 2. / FLT_RADIX
)
1585 aadj
= 1. / FLT_RADIX
;
1592 aadj1
= dsign
? aadj
: -aadj
;
1593 #ifdef Check_FLT_ROUNDS
1595 case 2: /* towards +infinity */
1598 case 0: /* towards 0 */
1599 case 3: /* towards -infinity */
1603 if (Flt_Rounds
== 0)
1605 #endif /*Check_FLT_ROUNDS*/
1607 y
= word0(&rv
) & Exp_mask
;
1609 /* Check for overflow */
1611 if (y
== Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1)) {
1612 dval(&rv0
) = dval(&rv
);
1613 word0(&rv
) -= P
* Exp_msk1
;
1614 adj
.d
= aadj1
* ulp(&rv
);
1616 if ((word0(&rv
) & Exp_mask
) >= Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
)) {
1617 if (word0(&rv0
) == Big0
&& word1(&rv0
) == Big1
)
1623 word0(&rv
) += P
* Exp_msk1
;
1625 #ifdef Avoid_Underflow
1626 if (scale
&& y
<= 2 * P
* Exp_msk1
) {
1627 if (aadj
<= 0x7fffffff) {
1628 if ((z
= (uint32_t)aadj
) <= 0)
1631 aadj1
= dsign
? aadj
: -aadj
;
1633 dval(&aadj2
) = aadj1
;
1634 word0(&aadj2
) += (2 * P
+ 1) * Exp_msk1
- y
;
1635 aadj1
= dval(&aadj2
);
1637 adj
.d
= aadj1
* ulp(&rv
);
1640 #ifdef Sudden_Underflow
1641 if ((word0(&rv
) & Exp_mask
) <= P
* Exp_msk1
) {
1642 dval(&rv0
) = dval(&rv
);
1643 word0(&rv
) += P
* Exp_msk1
;
1644 adj
.d
= aadj1
* ulp(&rv
);
1646 if ((word0(&rv
) & Exp_mask
) <= P
* Exp_msk1
)
1648 if (word0(&rv0
) == Tiny0
&& word1(&rv0
) == Tiny1
)
1655 word0(&rv
) -= P
* Exp_msk1
;
1657 adj
.d
= aadj1
* ulp(&rv
);
1660 #else /*Sudden_Underflow*/
1661 /* Compute adj so that the IEEE rounding rules will
1662 * correctly round rv + adj in some half-way cases.
1663 * If rv * ulp(rv) is denormalized (i.e.,
1664 * y <= (P - 1) * Exp_msk1), we must adjust aadj to avoid
1665 * trouble from bits lost to denormalization;
1666 * example: 1.2e-307 .
1668 if (y
<= (P
- 1) * Exp_msk1
&& aadj
> 1.) {
1669 aadj1
= (double)(int)(aadj
+ 0.5);
1673 adj
.d
= aadj1
* ulp(&rv
);
1675 #endif /*Sudden_Underflow*/
1676 #endif /*Avoid_Underflow*/
1678 z
= word0(&rv
) & Exp_mask
;
1680 #ifdef Avoid_Underflow
1684 /* Can we stop now? */
1687 /* The tolerances below are conservative. */
1688 if (dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
) {
1689 if (aadj
< .4999999 || aadj
> .5000001)
1691 } else if (aadj
< .4999999 / FLT_RADIX
)
1701 word0(&rv0
) = Exp_1
+ (70 << Exp_shift
);
1705 } else if (!oldinexact
)
1708 #ifdef Avoid_Underflow
1710 word0(&rv0
) = Exp_1
- 2 * P
* Exp_msk1
;
1712 dval(&rv
) *= dval(&rv0
);
1714 /* try to avoid the bug of testing an 8087 register value */
1715 if (word0(&rv
) == 0 && word1(&rv
) == 0)
1719 #endif /* Avoid_Underflow */
1721 if (inexact
&& !(word0(&rv
) & Exp_mask
)) {
1722 /* set underflow bit */
1723 dval(&rv0
) = 1e-300;
1724 dval(&rv0
) *= dval(&rv0
);
1729 *se
= const_cast<char*>(s
);
1730 return sign
? -dval(&rv
) : dval(&rv
);
1733 static ALWAYS_INLINE
int quorem(BigInt
& b
, BigInt
& S
)
1736 uint32_t *bx
, *bxe
, q
, *sx
, *sxe
;
1737 #ifdef USE_LONG_LONG
1738 unsigned long long borrow
, carry
, y
, ys
;
1740 uint32_t borrow
, carry
, y
, ys
;
1745 ASSERT(b
.size() <= 1 || b
.words()[b
.size() - 1]);
1746 ASSERT(S
.size() <= 1 || S
.words()[S
.size() - 1]);
1749 ASSERT_WITH_MESSAGE(b
.size() <= n
, "oversize b in quorem");
1756 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
1757 ASSERT_WITH_MESSAGE(q
<= 9, "oversized quotient in quorem");
1762 #ifdef USE_LONG_LONG
1763 ys
= *sx
++ * (unsigned long long)q
+ carry
;
1765 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
1766 borrow
= y
>> 32 & (uint32_t)1;
1767 *bx
++ = (uint32_t)y
& 0xffffffffUL
;
1771 ys
= (si
& 0xffff) * q
+ carry
;
1772 zs
= (si
>> 16) * q
+ (ys
>> 16);
1774 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
1775 borrow
= (y
& 0x10000) >> 16;
1776 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
1777 borrow
= (z
& 0x10000) >> 16;
1780 ys
= *sx
++ * q
+ carry
;
1782 y
= *bx
- (ys
& 0xffff) - borrow
;
1783 borrow
= (y
& 0x10000) >> 16;
1787 } while (sx
<= sxe
);
1790 while (--bxe
> bx
&& !*bxe
)
1795 if (cmp(b
, S
) >= 0) {
1802 #ifdef USE_LONG_LONG
1805 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
1806 borrow
= y
>> 32 & (uint32_t)1;
1807 *bx
++ = (uint32_t)y
& 0xffffffffUL
;
1811 ys
= (si
& 0xffff) + carry
;
1812 zs
= (si
>> 16) + (ys
>> 16);
1814 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
1815 borrow
= (y
& 0x10000) >> 16;
1816 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
1817 borrow
= (z
& 0x10000) >> 16;
1822 y
= *bx
- (ys
& 0xffff) - borrow
;
1823 borrow
= (y
& 0x10000) >> 16;
1827 } while (sx
<= sxe
);
1831 while (--bxe
> bx
&& !*bxe
)
1839 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1841 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1842 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1845 * 1. Rather than iterating, we use a simple numeric overestimate
1846 * to determine k = floor(log10(d)). We scale relevant
1847 * quantities using O(log2(k)) rather than O(k) multiplications.
1848 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1849 * try to generate digits strictly left to right. Instead, we
1850 * compute with fewer bits and propagate the carry if necessary
1851 * when rounding the final digit up. This is often faster.
1852 * 3. Under the assumption that input will be rounded nearest,
1853 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1854 * That is, we allow equality in stopping tests when the
1855 * round-nearest rule will give the same floating-point value
1856 * as would satisfaction of the stopping test with strict
1858 * 4. We remove common factors of powers of 2 from relevant
1860 * 5. When converting floating-point integers less than 1e16,
1861 * we use floating-point arithmetic rather than resorting
1862 * to multiple-precision integers.
1863 * 6. When asked to produce fewer than 15 digits, we first try
1864 * to get by with floating-point arithmetic; we resort to
1865 * multiple-precision integer arithmetic only if we cannot
1866 * guarantee that the floating-point calculation has given
1867 * the correctly rounded result. For k requested digits and
1868 * "uniformly" distributed input, the probability is
1869 * something like 10^(k-15) that we must resort to the int32_t
1873 void dtoa(DtoaBuffer result
, double dd
, int ndigits
, int* decpt
, int* sign
, char** rve
)
1876 Arguments ndigits, decpt, sign are similar to those
1877 of ecvt and fcvt; trailing zeros are suppressed from
1878 the returned string. If not null, *rve is set to point
1879 to the end of the return value. If d is +-Infinity or NaN,
1880 then *decpt is set to 9999.
1884 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
= 0, ilim0
, ilim1
= 0,
1885 j
, j1
, k
, k0
, k_check
, leftright
, m2
, m5
, s2
, s5
,
1886 spec_case
, try_quick
;
1888 #ifndef Sudden_Underflow
1892 BigInt b
, b1
, delta
, mlo
, mhi
, S
;
1897 int inexact
, oldinexact
;
1901 if (word0(&u
) & Sign_bit
) {
1902 /* set sign for everything, including 0's and NaNs */
1904 word0(&u
) &= ~Sign_bit
; /* clear sign bit */
1908 if ((word0(&u
) & Exp_mask
) == Exp_mask
)
1910 /* Infinity or NaN */
1912 if (!word1(&u
) && !(word0(&u
) & 0xfffff)) {
1913 strcpy(result
, "Infinity");
1917 strcpy(result
, "NaN");
1933 try_quick
= oldinexact
= get_inexact();
1937 d2b(b
, &u
, &be
, &bbits
);
1938 #ifdef Sudden_Underflow
1939 i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
));
1941 if ((i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
)))) {
1943 dval(&d2
) = dval(&u
);
1944 word0(&d2
) &= Frac_mask1
;
1945 word0(&d2
) |= Exp_11
;
1947 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
1948 * log10(x) = log(x) / log(10)
1949 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1950 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1952 * This suggests computing an approximation k to log10(d) by
1954 * k = (i - Bias)*0.301029995663981
1955 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1957 * We want k to be too large rather than too small.
1958 * The error in the first-order Taylor series approximation
1959 * is in our favor, so we just round up the constant enough
1960 * to compensate for any error in the multiplication of
1961 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
1962 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
1963 * adding 1e-13 to the constant term more than suffices.
1964 * Hence we adjust the constant term to 0.1760912590558.
1965 * (We could get a more accurate k by invoking log10,
1966 * but this is probably not worthwhile.)
1970 #ifndef Sudden_Underflow
1973 /* d is denormalized */
1975 i
= bbits
+ be
+ (Bias
+ (P
- 1) - 1);
1976 x
= (i
> 32) ? (word0(&u
) << (64 - i
)) | (word1(&u
) >> (i
- 32))
1977 : word1(&u
) << (32 - i
);
1979 word0(&d2
) -= 31 * Exp_msk1
; /* adjust exponent */
1980 i
-= (Bias
+ (P
- 1) - 1) + 1;
1984 ds
= (dval(&d2
) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i
* 0.301029995663981);
1986 if (ds
< 0. && ds
!= k
)
1987 k
--; /* want k = floor(ds) */
1989 if (k
>= 0 && k
<= Ten_pmax
) {
1990 if (dval(&u
) < tens
[k
])
2013 #ifdef Check_FLT_ROUNDS
2014 try_quick
= Rounding
== 1;
2018 #endif /*SET_INEXACT*/
2026 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
2028 /* Try to get by with floating-point arithmetic. */
2031 dval(&d2
) = dval(&u
);
2034 ieps
= 2; /* conservative */
2039 /* prevent overflows */
2041 dval(&u
) /= bigtens
[n_bigtens
- 1];
2044 for (; j
; j
>>= 1, i
++) {
2051 } else if ((j1
= -k
)) {
2052 dval(&u
) *= tens
[j1
& 0xf];
2053 for (j
= j1
>> 4; j
; j
>>= 1, i
++) {
2056 dval(&u
) *= bigtens
[i
];
2060 if (k_check
&& dval(&u
) < 1. && ilim
> 0) {
2068 dval(&eps
) = (ieps
* dval(&u
)) + 7.;
2069 word0(&eps
) -= (P
- 1) * Exp_msk1
;
2074 if (dval(&u
) > dval(&eps
))
2076 if (dval(&u
) < -dval(&eps
))
2080 #ifndef No_leftright
2082 /* Use Steele & White method of only
2083 * generating digits needed.
2085 dval(&eps
) = (0.5 / tens
[ilim
- 1]) - dval(&eps
);
2087 L
= (long int)dval(&u
);
2089 *s
++ = '0' + (int)L
;
2090 if (dval(&u
) < dval(&eps
))
2092 if (1. - dval(&u
) < dval(&eps
))
2101 /* Generate ilim digits, then fix them up. */
2102 dval(&eps
) *= tens
[ilim
- 1];
2103 for (i
= 1;; i
++, dval(&u
) *= 10.) {
2104 L
= (int32_t)(dval(&u
));
2105 if (!(dval(&u
) -= L
))
2107 *s
++ = '0' + (int)L
;
2109 if (dval(&u
) > 0.5 + dval(&eps
))
2111 else if (dval(&u
) < 0.5 - dval(&eps
)) {
2112 while (*--s
== '0') { }
2119 #ifndef No_leftright
2124 dval(&u
) = dval(&d2
);
2129 /* Do we have a "small" integer? */
2131 if (be
>= 0 && k
<= Int_max
) {
2134 if (ndigits
< 0 && ilim
<= 0) {
2137 if (ilim
< 0 || dval(&u
) <= 5 * ds
)
2141 for (i
= 1;; i
++, dval(&u
) *= 10.) {
2142 L
= (int32_t)(dval(&u
) / ds
);
2144 #ifdef Check_FLT_ROUNDS
2145 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2151 *s
++ = '0' + (int)L
;
2159 dval(&u
) += dval(&u
);
2160 if (dval(&u
) > ds
|| (dval(&u
) == ds
&& (L
& 1))) {
2182 #ifndef Sudden_Underflow
2183 denorm
? be
+ (Bias
+ (P
- 1) - 1 + 1) :
2190 if (m2
> 0 && s2
> 0) {
2191 i
= m2
< s2
? m2
: s2
;
2211 /* Check for special case that d is a normalized power of 2. */
2214 if (!word1(&u
) && !(word0(&u
) & Bndry_mask
)
2215 #ifndef Sudden_Underflow
2216 && word0(&u
) & (Exp_mask
& ~Exp_msk1
)
2219 /* The special case */
2225 /* Arrange for convenient computation of quotients:
2226 * shift left if necessary so divisor has 4 leading 0 bits.
2228 * Perhaps we should just compute leading 28 bits of S once
2229 * and for all and pass them and a shift to quorem, so it
2230 * can do shifts and ors to compute the numerator for q.
2233 if ((i
= ((s5
? 32 - hi0bits(S
.words()[S
.size() - 1]) : 1) + s2
) & 0x1f))
2236 if ((i
= ((s5
? 32 - hi0bits(S
.words()[S
.size() - 1]) : 1) + s2
) & 0xf))
2257 multadd(b
, 10, 0); /* we botched the k estimate */
2259 multadd(mhi
, 10, 0);
2268 /* Compute mlo -- check for special case
2269 * that d is a normalized power of 2.
2279 dig
= quorem(b
,S
) + '0';
2280 /* Do we yet have the shortest decimal string
2281 * that will round to d?
2284 diff(delta
, S
, mhi
);
2285 j1
= delta
.sign
? 1 : cmp(b
, delta
);
2286 if (j1
== 0 && !(word1(&u
) & 1)) {
2292 else if (!b
->x
[0] && b
->wds
<= 1)
2298 if (j
< 0 || (j
== 0 && !(word1(&u
) & 1))) {
2299 if (!b
.words()[0] && b
.size() <= 1) {
2308 if ((j1
> 0 || (j1
== 0 && (dig
& 1))) && dig
++ == '9')
2316 if (dig
== '9') { /* possible if i == 1 */
2328 multadd(mlo
, 10, 0);
2329 multadd(mhi
, 10, 0);
2333 *s
++ = dig
= quorem(b
,S
) + '0';
2334 if (!b
.words()[0] && b
.size() <= 1) {
2345 /* Round off last digit */
2349 if (j
> 0 || (j
== 0 && (dig
& 1))) {
2359 while (*--s
== '0') { }
2374 word0(&u
) = Exp_1
+ (70 << Exp_shift
);
2378 } else if (!oldinexact
)
2387 static ALWAYS_INLINE
void append(char*& next
, const char* src
, unsigned size
)
2389 for (unsigned i
= 0; i
< size
; ++i
)
2393 void doubleToStringInJavaScriptFormat(double d
, DtoaBuffer buffer
, unsigned* resultLength
)
2397 // avoid ever printing -NaN, in JS conceptually there is only one NaN value
2399 append(buffer
, "NaN", 3);
2416 char* resultEnd
= 0;
2417 WTF::dtoa(result
, d
, 0, &decimalPoint
, &sign
, &resultEnd
);
2418 int length
= resultEnd
- result
;
2420 char* next
= buffer
;
2424 if (decimalPoint
<= 0 && decimalPoint
> -6) {
2427 for (int j
= decimalPoint
; j
< 0; j
++)
2429 append(next
, result
, length
);
2430 } else if (decimalPoint
<= 21 && decimalPoint
> 0) {
2431 if (length
<= decimalPoint
) {
2432 append(next
, result
, length
);
2433 for (int j
= 0; j
< decimalPoint
- length
; j
++)
2436 append(next
, result
, decimalPoint
);
2438 append(next
, result
+ decimalPoint
, length
- decimalPoint
);
2440 } else if (result
[0] < '0' || result
[0] > '9')
2441 append(next
, result
, length
);
2443 *next
++ = result
[0];
2446 append(next
, result
+ 1, length
- 1);
2450 *next
++ = (decimalPoint
>= 0) ? '+' : '-';
2451 // decimalPoint can't be more than 3 digits decimal given the
2452 // nature of float representation
2453 int exponential
= decimalPoint
- 1;
2454 if (exponential
< 0)
2455 exponential
= -exponential
;
2456 if (exponential
>= 100)
2457 *next
++ = static_cast<char>('0' + exponential
/ 100);
2458 if (exponential
>= 10)
2459 *next
++ = static_cast<char>('0' + (exponential
% 100) / 10);
2460 *next
++ = static_cast<char>('0' + exponential
% 10);
2463 *resultLength
= next
- buffer
;