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)
173 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_ARM) != 1
174 Exactly one of IEEE_8087
, IEEE_ARM
or IEEE_MC68k should be defined
.
179 #if ENABLE(JSC_MULTIPLE_THREADS)
180 Mutex
* s_dtoaP5Mutex
;
183 typedef union { double d
; uint32_t L
[2]; } U
;
188 #define word0(x) ((uint32_t*)&x)[1]
189 #define word1(x) ((uint32_t*)&x)[0]
191 #define word0(x) ((uint32_t*)&x)[0]
192 #define word1(x) ((uint32_t*)&x)[1]
196 #define word0(x) (x)->L[1]
197 #define word1(x) (x)->L[0]
199 #define word0(x) (x)->L[0]
200 #define word1(x) (x)->L[1]
202 #define dval(x) (x)->d
205 /* The following definition of Storeinc is appropriate for MIPS processors.
206 * An alternative that might be better on some machines is
207 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
209 #if defined(IEEE_8087) || defined(IEEE_ARM)
210 #define Storeinc(a,b,c) (((unsigned short*)a)[1] = (unsigned short)b, ((unsigned short*)a)[0] = (unsigned short)c, a++)
212 #define Storeinc(a,b,c) (((unsigned short*)a)[0] = (unsigned short)b, ((unsigned short*)a)[1] = (unsigned short)c, a++)
216 #define Exp_shift1 20
217 #define Exp_msk1 0x100000
218 #define Exp_msk11 0x100000
219 #define Exp_mask 0x7ff00000
223 #define Exp_1 0x3ff00000
224 #define Exp_11 0x3ff00000
226 #define Frac_mask 0xfffff
227 #define Frac_mask1 0xfffff
230 #define Bndry_mask 0xfffff
231 #define Bndry_mask1 0xfffff
233 #define Sign_bit 0x80000000
240 #if !defined(NO_IEEE_Scale)
241 #undef Avoid_Underflow
242 #define Avoid_Underflow
245 #if !defined(Flt_Rounds)
246 #if defined(FLT_ROUNDS)
247 #define Flt_Rounds FLT_ROUNDS
251 #endif /*Flt_Rounds*/
254 #define rounded_product(a,b) a *= b
255 #define rounded_quotient(a,b) a /= b
257 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
258 #define Big1 0xffffffff
261 // FIXME: we should remove non-Pack_32 mode since it is unused and unmaintained
266 #if CPU(PPC64) || CPU(X86_64)
267 // FIXME: should we enable this on all 64-bit CPUs?
268 // 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware.
269 #define USE_LONG_LONG
272 #ifndef USE_LONG_LONG
275 /* When Pack_32 is not defined, we store 16 bits per 32-bit int32_t.
276 * This makes some inner loops simpler and sometimes saves work
277 * during multiplications, but it often seems to make things slightly
278 * slower. Hence the default is now to store 32 bits per int32_t.
286 BigInt() : sign(0) { }
297 return m_words
.size();
300 void resize(size_t s
)
307 return m_words
.data();
310 const uint32_t* words() const
312 return m_words
.data();
315 void append(uint32_t w
)
320 Vector
<uint32_t, 16> m_words
;
323 static void multadd(BigInt
& b
, int m
, int a
) /* multiply by m and add a */
326 unsigned long long carry
;
332 uint32_t* x
= b
.words();
337 unsigned long long y
= *x
* (unsigned long long)m
+ carry
;
339 *x
++ = (uint32_t)y
& 0xffffffffUL
;
343 uint32_t y
= (xi
& 0xffff) * m
+ carry
;
344 uint32_t z
= (xi
>> 16) * m
+ (y
>> 16);
346 *x
++ = (z
<< 16) + (y
& 0xffff);
348 uint32_t y
= *x
* m
+ carry
;
356 b
.append((uint32_t)carry
);
359 static void s2b(BigInt
& b
, const char* s
, int nd0
, int nd
, uint32_t y9
)
363 int32_t x
= (nd
+ 8) / 9;
365 for (k
= 0, y
= 1; x
> y
; y
<<= 1, k
++) { }
372 b
.resize((b
->x
[1] = y9
>> 16) ? 2 : 1);
373 b
.words()[0] = y9
& 0xffff;
380 multadd(b
, 10, *s
++ - '0');
386 multadd(b
, 10, *s
++ - '0');
389 static int hi0bits(uint32_t x
)
393 if (!(x
& 0xffff0000)) {
397 if (!(x
& 0xff000000)) {
401 if (!(x
& 0xf0000000)) {
405 if (!(x
& 0xc0000000)) {
409 if (!(x
& 0x80000000)) {
411 if (!(x
& 0x40000000))
417 static int lo0bits (uint32_t* y
)
459 static void i2b(BigInt
& b
, int i
)
466 static void mult(BigInt
& aRef
, const BigInt
& bRef
)
468 const BigInt
* a
= &aRef
;
469 const BigInt
* b
= &bRef
;
472 const uint32_t *x
= 0, *xa
, *xb
, *xae
, *xbe
;
476 unsigned long long carry
, z
;
481 if (a
->size() < b
->size()) {
482 const BigInt
* tmp
= a
;
492 for (xc
= c
.words(), xa
= xc
+ wc
; xc
< xa
; xc
++)
500 for (; xb
< xbe
; xc0
++) {
506 z
= *x
++ * (unsigned long long)y
+ *xc
+ carry
;
508 *xc
++ = (uint32_t)z
& 0xffffffffUL
;
510 *xc
= (uint32_t)carry
;
515 for (; xb
< xbe
; xb
++, xc0
++) {
516 if ((y
= *xb
& 0xffff)) {
521 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
523 uint32_t z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
529 if ((y
= *xb
>> 16)) {
535 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
538 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
545 for(; xb
< xbe
; xc0
++) {
551 z
= *x
++ * y
+ *xc
+ carry
;
560 for (xc0
= c
.words(), xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) { }
565 struct P5Node
: Noncopyable
{
571 static int p5s_count
;
573 static ALWAYS_INLINE
void pow5mult(BigInt
& b
, int k
)
575 static int p05
[3] = { 5, 25, 125 };
578 multadd(b
, p05
[i
- 1], 0);
583 #if ENABLE(JSC_MULTIPLE_THREADS)
584 s_dtoaP5Mutex
->lock();
597 int p5s_count_local
= p5s_count
;
598 #if ENABLE(JSC_MULTIPLE_THREADS)
599 s_dtoaP5Mutex
->unlock();
610 if (++p5s_used
== p5s_count_local
) {
611 #if ENABLE(JSC_MULTIPLE_THREADS)
612 s_dtoaP5Mutex
->lock();
614 if (p5s_used
== p5s_count
) {
616 p5
->next
= new P5Node
;
618 p5
->next
->val
= p5
->val
;
619 mult(p5
->next
->val
, p5
->next
->val
);
623 p5s_count_local
= p5s_count
;
624 #if ENABLE(JSC_MULTIPLE_THREADS)
625 s_dtoaP5Mutex
->unlock();
632 static ALWAYS_INLINE
void lshift(BigInt
& b
, int k
)
640 int origSize
= b
.size();
641 int n1
= n
+ origSize
+ 1;
644 b
.resize(b
.size() + n
+ 1);
646 b
.resize(b
.size() + n
);
648 const uint32_t* srcStart
= b
.words();
649 uint32_t* dstStart
= b
.words();
650 const uint32_t* src
= srcStart
+ origSize
- 1;
651 uint32_t* dst
= dstStart
+ n1
- 1;
654 uint32_t hiSubword
= 0;
656 for (; src
>= srcStart
; --src
) {
657 *dst
-- = hiSubword
| *src
>> s
;
658 hiSubword
= *src
<< k
;
661 ASSERT(dst
== dstStart
+ n
);
663 b
.resize(origSize
+ n
+ (b
.words()[n1
- 1] != 0));
667 uint32_t hiSubword
= 0;
669 for (; src
>= srcStart
; --src
) {
670 *dst
-- = hiSubword
| *src
>> s
;
671 hiSubword
= (*src
<< k
) & 0xffff;
674 ASSERT(dst
== dstStart
+ n
);
675 result
->wds
= b
->wds
+ n
+ (result
->x
[n1
- 1] != 0);
681 } while (src
>= srcStart
);
683 for (dst
= dstStart
+ n
; dst
!= dstStart
; )
686 ASSERT(b
.size() <= 1 || b
.words()[b
.size() - 1]);
689 static int cmp(const BigInt
& a
, const BigInt
& b
)
691 const uint32_t *xa
, *xa0
, *xb
, *xb0
;
696 ASSERT(i
<= 1 || a
.words()[i
- 1]);
697 ASSERT(j
<= 1 || b
.words()[j
- 1]);
706 return *xa
< *xb
? -1 : 1;
713 static ALWAYS_INLINE
void diff(BigInt
& c
, const BigInt
& aRef
, const BigInt
& bRef
)
715 const BigInt
* a
= &aRef
;
716 const BigInt
* b
= &bRef
;
728 const BigInt
* tmp
= a
;
736 const uint32_t* xa
= a
->words();
737 const uint32_t* xae
= xa
+ wa
;
739 const uint32_t* xb
= b
->words();
740 const uint32_t* xbe
= xb
+ wb
;
746 unsigned long long borrow
= 0;
748 unsigned long long y
= (unsigned long long)*xa
++ - *xb
++ - borrow
;
749 borrow
= y
>> 32 & (uint32_t)1;
750 *xc
++ = (uint32_t)y
& 0xffffffffUL
;
753 unsigned long long y
= *xa
++ - borrow
;
754 borrow
= y
>> 32 & (uint32_t)1;
755 *xc
++ = (uint32_t)y
& 0xffffffffUL
;
761 uint32_t y
= (*xa
& 0xffff) - (*xb
& 0xffff) - borrow
;
762 borrow
= (y
& 0x10000) >> 16;
763 uint32_t z
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
;
764 borrow
= (z
& 0x10000) >> 16;
768 uint32_t y
= (*xa
& 0xffff) - borrow
;
769 borrow
= (y
& 0x10000) >> 16;
770 uint32_t z
= (*xa
++ >> 16) - borrow
;
771 borrow
= (z
& 0x10000) >> 16;
776 uint32_t y
= *xa
++ - *xb
++ - borrow
;
777 borrow
= (y
& 0x10000) >> 16;
781 uint32_t y
= *xa
++ - borrow
;
782 borrow
= (y
& 0x10000) >> 16;
792 static double ulp(U
*x
)
797 L
= (word0(x
) & Exp_mask
) - (P
- 1) * Exp_msk1
;
798 #ifndef Avoid_Underflow
799 #ifndef Sudden_Underflow
805 #ifndef Avoid_Underflow
806 #ifndef Sudden_Underflow
810 word0(&u
) = 0x80000 >> L
;
815 word1(&u
) = L
>= 31 ? 1 : 1 << 31 - L
;
823 static double b2d(const BigInt
& a
, int* e
)
844 d0
= Exp_1
| (y
>> (Ebits
- k
));
845 w
= xa
> xa0
? *--xa
: 0;
846 d1
= (y
<< (32 - Ebits
+ k
)) | (w
>> (Ebits
- k
));
849 z
= xa
> xa0
? *--xa
: 0;
851 d0
= Exp_1
| (y
<< k
) | (z
>> (32 - k
));
852 y
= xa
> xa0
? *--xa
: 0;
853 d1
= (z
<< k
) | (y
>> (32 - k
));
859 if (k
< Ebits
+ 16) {
860 z
= xa
> xa0
? *--xa
: 0;
861 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
862 w
= xa
> xa0
? *--xa
: 0;
863 y
= xa
> xa0
? *--xa
: 0;
864 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
867 z
= xa
> xa0
? *--xa
: 0;
868 w
= xa
> xa0
? *--xa
: 0;
870 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
871 y
= xa
> xa0
? *--xa
: 0;
872 d1
= w
<< k
+ 16 | y
<< k
;
880 static ALWAYS_INLINE
void d2b(BigInt
& b
, U
* d
, int* e
, int* bits
)
884 #ifndef Sudden_Underflow
899 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
900 #ifdef Sudden_Underflow
901 de
= (int)(d0
>> Exp_shift
);
903 if ((de
= (int)(d0
>> Exp_shift
)))
908 if ((k
= lo0bits(&y
))) {
909 x
[0] = y
| (z
<< (32 - k
));
918 #ifndef Sudden_Underflow
924 #ifndef Sudden_Underflow
932 if ((k
= lo0bits(&y
))) {
934 x
[0] = y
| z
<< 32 - k
& 0xffff;
935 x
[1] = z
>> k
- 16 & 0xffff;
940 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
941 x
[2] = z
>> k
& 0xffff;
967 #ifndef Sudden_Underflow
970 *e
= de
- Bias
- (P
- 1) + k
;
972 #ifndef Sudden_Underflow
974 *e
= de
- Bias
- (P
- 1) + 1 + k
;
976 *bits
= (32 * i
) - hi0bits(x
[i
- 1]);
978 *bits
= (i
+ 2) * 16 - hi0bits(x
[i
]);
986 static double ratio(const BigInt
& a
, const BigInt
& b
)
991 dval(&da
) = b2d(a
, &ka
);
992 dval(&db
) = b2d(b
, &kb
);
994 k
= ka
- kb
+ 32 * (a
.size() - b
.size());
996 k
= ka
- kb
+ 16 * (a
.size() - b
.size());
999 word0(&da
) += k
* Exp_msk1
;
1002 word0(&db
) += k
* Exp_msk1
;
1004 return dval(&da
) / dval(&db
);
1007 static const double tens
[] = {
1008 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
1009 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
1013 static const double bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1014 static const double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1015 #ifdef Avoid_Underflow
1016 9007199254740992. * 9007199254740992.e
-256
1017 /* = 2^106 * 1e-53 */
1023 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1024 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1025 #define Scale_Bit 0x10
1028 #if defined(INFNAN_CHECK)
1031 #define NAN_WORD0 0x7ff80000
1038 static int match(const char** sp
, const char* t
)
1041 const char* s
= *sp
;
1043 while ((d
= *t
++)) {
1044 if ((c
= *++s
) >= 'A' && c
<= 'Z')
1054 static void hexnan(U
* rvp
, const char** sp
)
1058 int havedig
, udx0
, xshift
;
1061 havedig
= xshift
= 0;
1064 while ((c
= *(const unsigned char*)++s
)) {
1065 if (c
>= '0' && c
<= '9')
1067 else if (c
>= 'a' && c
<= 'f')
1069 else if (c
>= 'A' && c
<= 'F')
1071 else if (c
<= ' ') {
1072 if (udx0
&& havedig
) {
1077 } else if (/*(*/ c
== ')' && havedig
) {
1081 return; /* invalid form: don't change *sp */
1089 x
[0] = (x
[0] << 4) | (x
[1] >> 28);
1090 x
[1] = (x
[1] << 4) | c
;
1092 if ((x
[0] &= 0xfffff) || x
[1]) {
1093 word0(rvp
) = Exp_mask
| x
[0];
1097 #endif /*No_Hex_NaN*/
1098 #endif /* INFNAN_CHECK */
1100 double strtod(const char* s00
, char** se
)
1102 #ifdef Avoid_Underflow
1105 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
,
1106 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
1107 const char *s
, *s0
, *s1
;
1109 U aadj2
, adj
, rv
, rv0
;
1112 BigInt bb
, bb1
, bd
, bd0
, bs
, delta
;
1114 int inexact
, oldinexact
;
1117 sign
= nz0
= nz
= 0;
1119 for (s
= s00
; ; s
++)
1143 while (*++s
== '0') { }
1149 for (nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
1151 y
= (10 * y
) + c
- '0';
1153 z
= (10 * z
) + c
- '0';
1158 for (; c
== '0'; c
= *++s
)
1160 if (c
> '0' && c
<= '9') {
1168 for (; c
>= '0' && c
<= '9'; c
= *++s
) {
1173 for (i
= 1; i
< nz
; i
++)
1176 else if (nd
<= DBL_DIG
+ 1)
1180 else if (nd
<= DBL_DIG
+ 1)
1188 if (c
== 'e' || c
== 'E') {
1189 if (!nd
&& !nz
&& !nz0
) {
1200 if (c
>= '0' && c
<= '9') {
1203 if (c
> '0' && c
<= '9') {
1206 while ((c
= *++s
) >= '0' && c
<= '9')
1207 L
= (10 * L
) + c
- '0';
1208 if (s
- s1
> 8 || L
> 19999)
1209 /* Avoid confusion from exponents
1210 * so large that e might overflow.
1212 e
= 19999; /* safe for 16 bit ints */
1225 /* Check for Nan and Infinity */
1229 if (match(&s
,"nf")) {
1231 if (!match(&s
,"inity"))
1233 word0(&rv
) = 0x7ff00000;
1240 if (match(&s
, "an")) {
1241 word0(&rv
) = NAN_WORD0
;
1242 word1(&rv
) = NAN_WORD1
;
1244 if (*s
== '(') /*)*/
1250 #endif /* INFNAN_CHECK */
1259 /* Now we have nd0 digits, starting at s0, followed by a
1260 * decimal point, followed by nd-nd0 digits. The number we're
1261 * after is the integer represented by those digits times
1266 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
1271 oldinexact
= get_inexact();
1273 dval(&rv
) = tens
[k
- 9] * dval(&rv
) + z
;
1275 if (nd
<= DBL_DIG
&& Flt_Rounds
== 1) {
1279 if (e
<= Ten_pmax
) {
1280 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
1284 if (e
<= Ten_pmax
+ i
) {
1285 /* A fancier test would sometimes let us do
1286 * this for larger i values.
1289 dval(&rv
) *= tens
[i
];
1290 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
1294 #ifndef Inaccurate_Divide
1295 else if (e
>= -Ten_pmax
) {
1296 /* rv = */ rounded_quotient(dval(&rv
), tens
[-e
]);
1306 oldinexact
= get_inexact();
1308 #ifdef Avoid_Underflow
1312 /* Get starting approximation = rv * 10**e1 */
1316 dval(&rv
) *= tens
[i
];
1318 if (e1
> DBL_MAX_10_EXP
) {
1323 /* Can't trust HUGE_VAL */
1324 word0(&rv
) = Exp_mask
;
1327 /* set overflow bit */
1329 dval(&rv0
) *= dval(&rv0
);
1334 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
1336 dval(&rv
) *= bigtens
[j
];
1337 /* The last multiplication could overflow. */
1338 word0(&rv
) -= P
* Exp_msk1
;
1339 dval(&rv
) *= bigtens
[j
];
1340 if ((z
= word0(&rv
) & Exp_mask
) > Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
))
1342 if (z
> Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1 - P
)) {
1343 /* set to largest number */
1344 /* (Can't trust DBL_MAX) */
1348 word0(&rv
) += P
* Exp_msk1
;
1350 } else if (e1
< 0) {
1353 dval(&rv
) /= tens
[i
];
1355 if (e1
>= 1 << n_bigtens
)
1357 #ifdef Avoid_Underflow
1360 for (j
= 0; e1
> 0; j
++, e1
>>= 1)
1362 dval(&rv
) *= tinytens
[j
];
1363 if (scale
&& (j
= (2 * P
) + 1 - ((word0(&rv
) & Exp_mask
) >> Exp_shift
)) > 0) {
1364 /* scaled rv is denormal; zap j low bits */
1368 word0(&rv
) = (P
+ 2) * Exp_msk1
;
1370 word0(&rv
) &= 0xffffffff << (j
- 32);
1372 word1(&rv
) &= 0xffffffff << j
;
1375 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
1377 dval(&rv
) *= tinytens
[j
];
1378 /* The last multiplication could underflow. */
1379 dval(&rv0
) = dval(&rv
);
1380 dval(&rv
) *= tinytens
[j
];
1382 dval(&rv
) = 2. * dval(&rv0
);
1383 dval(&rv
) *= tinytens
[j
];
1393 #ifndef Avoid_Underflow
1396 /* The refinement below will clean
1397 * this approximation up.
1404 /* Now the hard part -- adjusting rv to the correct value.*/
1406 /* Put digits into bd: true value = bd * 10^e */
1408 s2b(bd0
, s0
, nd0
, nd
, y
);
1412 d2b(bb
, &rv
, &bbe
, &bbbits
); /* rv = bb * 2^bbe */
1427 #ifdef Avoid_Underflow
1429 i
= j
+ bbbits
- 1; /* logb(rv) */
1430 if (i
< Emin
) /* denormal */
1434 #else /*Avoid_Underflow*/
1435 #ifdef Sudden_Underflow
1437 #else /*Sudden_Underflow*/
1439 i
= j
+ bbbits
- 1; /* logb(rv) */
1440 if (i
< Emin
) /* denormal */
1444 #endif /*Sudden_Underflow*/
1445 #endif /*Avoid_Underflow*/
1448 #ifdef Avoid_Underflow
1451 i
= bb2
< bd2
? bb2
: bd2
;
1471 diff(delta
, bb
, bd
);
1477 /* Error is less than half an ulp -- check for
1478 * special case of mantissa a power of two.
1480 if (dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
1481 #ifdef Avoid_Underflow
1482 || (word0(&rv
) & Exp_mask
) <= (2 * P
+ 1) * Exp_msk1
1484 || (word0(&rv
) & Exp_mask
) <= Exp_msk1
1488 if (!delta
->words()[0] && delta
->size() <= 1)
1493 if (!delta
.words()[0] && delta
.size() <= 1) {
1500 lshift(delta
, Log2P
);
1501 if (cmp(delta
, bs
) > 0)
1506 /* exactly half-way between */
1508 if ((word0(&rv
) & Bndry_mask1
) == Bndry_mask1
1510 #ifdef Avoid_Underflow
1511 (scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2 * P
* Exp_msk1
)
1512 ? (0xffffffff & (0xffffffff << (2 * P
+ 1 - (y
>> Exp_shift
)))) :
1515 /*boundary case -- increment exponent*/
1516 word0(&rv
) = (word0(&rv
) & Exp_mask
) + Exp_msk1
;
1518 #ifdef Avoid_Underflow
1523 } else if (!(word0(&rv
) & Bndry_mask
) && !word1(&rv
)) {
1525 /* boundary case -- decrement exponent */
1526 #ifdef Sudden_Underflow /*{{*/
1527 L
= word0(&rv
) & Exp_mask
;
1528 #ifdef Avoid_Underflow
1529 if (L
<= (scale
? (2 * P
+ 1) * Exp_msk1
: Exp_msk1
))
1532 #endif /*Avoid_Underflow*/
1535 #else /*Sudden_Underflow}{*/
1536 #ifdef Avoid_Underflow
1538 L
= word0(&rv
) & Exp_mask
;
1539 if (L
<= (2 * P
+ 1) * Exp_msk1
) {
1540 if (L
> (P
+ 2) * Exp_msk1
)
1541 /* round even ==> */
1544 /* rv = smallest denormal */
1548 #endif /*Avoid_Underflow*/
1549 L
= (word0(&rv
) & Exp_mask
) - Exp_msk1
;
1550 #endif /*Sudden_Underflow}}*/
1551 word0(&rv
) = L
| Bndry_mask1
;
1552 word1(&rv
) = 0xffffffff;
1555 if (!(word1(&rv
) & LSB
))
1558 dval(&rv
) += ulp(&rv
);
1560 dval(&rv
) -= ulp(&rv
);
1561 #ifndef Sudden_Underflow
1566 #ifdef Avoid_Underflow
1571 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
1574 else if (word1(&rv
) || word0(&rv
) & Bndry_mask
) {
1575 #ifndef Sudden_Underflow
1576 if (word1(&rv
) == Tiny1
&& !word0(&rv
))
1582 /* special case -- power of FLT_RADIX to be */
1583 /* rounded down... */
1585 if (aadj
< 2. / FLT_RADIX
)
1586 aadj
= 1. / FLT_RADIX
;
1593 aadj1
= dsign
? aadj
: -aadj
;
1594 #ifdef Check_FLT_ROUNDS
1596 case 2: /* towards +infinity */
1599 case 0: /* towards 0 */
1600 case 3: /* towards -infinity */
1604 if (Flt_Rounds
== 0)
1606 #endif /*Check_FLT_ROUNDS*/
1608 y
= word0(&rv
) & Exp_mask
;
1610 /* Check for overflow */
1612 if (y
== Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1)) {
1613 dval(&rv0
) = dval(&rv
);
1614 word0(&rv
) -= P
* Exp_msk1
;
1615 adj
.d
= aadj1
* ulp(&rv
);
1617 if ((word0(&rv
) & Exp_mask
) >= Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
)) {
1618 if (word0(&rv0
) == Big0
&& word1(&rv0
) == Big1
)
1624 word0(&rv
) += P
* Exp_msk1
;
1626 #ifdef Avoid_Underflow
1627 if (scale
&& y
<= 2 * P
* Exp_msk1
) {
1628 if (aadj
<= 0x7fffffff) {
1629 if ((z
= (uint32_t)aadj
) <= 0)
1632 aadj1
= dsign
? aadj
: -aadj
;
1634 dval(&aadj2
) = aadj1
;
1635 word0(&aadj2
) += (2 * P
+ 1) * Exp_msk1
- y
;
1636 aadj1
= dval(&aadj2
);
1638 adj
.d
= aadj1
* ulp(&rv
);
1641 #ifdef Sudden_Underflow
1642 if ((word0(&rv
) & Exp_mask
) <= P
* Exp_msk1
) {
1643 dval(&rv0
) = dval(&rv
);
1644 word0(&rv
) += P
* Exp_msk1
;
1645 adj
.d
= aadj1
* ulp(&rv
);
1647 if ((word0(&rv
) & Exp_mask
) <= P
* Exp_msk1
)
1649 if (word0(&rv0
) == Tiny0
&& word1(&rv0
) == Tiny1
)
1656 word0(&rv
) -= P
* Exp_msk1
;
1658 adj
.d
= aadj1
* ulp(&rv
);
1661 #else /*Sudden_Underflow*/
1662 /* Compute adj so that the IEEE rounding rules will
1663 * correctly round rv + adj in some half-way cases.
1664 * If rv * ulp(rv) is denormalized (i.e.,
1665 * y <= (P - 1) * Exp_msk1), we must adjust aadj to avoid
1666 * trouble from bits lost to denormalization;
1667 * example: 1.2e-307 .
1669 if (y
<= (P
- 1) * Exp_msk1
&& aadj
> 1.) {
1670 aadj1
= (double)(int)(aadj
+ 0.5);
1674 adj
.d
= aadj1
* ulp(&rv
);
1676 #endif /*Sudden_Underflow*/
1677 #endif /*Avoid_Underflow*/
1679 z
= word0(&rv
) & Exp_mask
;
1681 #ifdef Avoid_Underflow
1685 /* Can we stop now? */
1688 /* The tolerances below are conservative. */
1689 if (dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
) {
1690 if (aadj
< .4999999 || aadj
> .5000001)
1692 } else if (aadj
< .4999999 / FLT_RADIX
)
1702 word0(&rv0
) = Exp_1
+ (70 << Exp_shift
);
1706 } else if (!oldinexact
)
1709 #ifdef Avoid_Underflow
1711 word0(&rv0
) = Exp_1
- 2 * P
* Exp_msk1
;
1713 dval(&rv
) *= dval(&rv0
);
1715 /* try to avoid the bug of testing an 8087 register value */
1716 if (word0(&rv
) == 0 && word1(&rv
) == 0)
1720 #endif /* Avoid_Underflow */
1722 if (inexact
&& !(word0(&rv
) & Exp_mask
)) {
1723 /* set underflow bit */
1724 dval(&rv0
) = 1e-300;
1725 dval(&rv0
) *= dval(&rv0
);
1730 *se
= const_cast<char*>(s
);
1731 return sign
? -dval(&rv
) : dval(&rv
);
1734 static ALWAYS_INLINE
int quorem(BigInt
& b
, BigInt
& S
)
1737 uint32_t *bx
, *bxe
, q
, *sx
, *sxe
;
1738 #ifdef USE_LONG_LONG
1739 unsigned long long borrow
, carry
, y
, ys
;
1741 uint32_t borrow
, carry
, y
, ys
;
1746 ASSERT(b
.size() <= 1 || b
.words()[b
.size() - 1]);
1747 ASSERT(S
.size() <= 1 || S
.words()[S
.size() - 1]);
1750 ASSERT_WITH_MESSAGE(b
.size() <= n
, "oversize b in quorem");
1757 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
1758 ASSERT_WITH_MESSAGE(q
<= 9, "oversized quotient in quorem");
1763 #ifdef USE_LONG_LONG
1764 ys
= *sx
++ * (unsigned long long)q
+ carry
;
1766 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
1767 borrow
= y
>> 32 & (uint32_t)1;
1768 *bx
++ = (uint32_t)y
& 0xffffffffUL
;
1772 ys
= (si
& 0xffff) * q
+ carry
;
1773 zs
= (si
>> 16) * q
+ (ys
>> 16);
1775 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
1776 borrow
= (y
& 0x10000) >> 16;
1777 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
1778 borrow
= (z
& 0x10000) >> 16;
1781 ys
= *sx
++ * q
+ carry
;
1783 y
= *bx
- (ys
& 0xffff) - borrow
;
1784 borrow
= (y
& 0x10000) >> 16;
1788 } while (sx
<= sxe
);
1791 while (--bxe
> bx
&& !*bxe
)
1796 if (cmp(b
, S
) >= 0) {
1803 #ifdef USE_LONG_LONG
1806 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
1807 borrow
= y
>> 32 & (uint32_t)1;
1808 *bx
++ = (uint32_t)y
& 0xffffffffUL
;
1812 ys
= (si
& 0xffff) + carry
;
1813 zs
= (si
>> 16) + (ys
>> 16);
1815 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
1816 borrow
= (y
& 0x10000) >> 16;
1817 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
1818 borrow
= (z
& 0x10000) >> 16;
1823 y
= *bx
- (ys
& 0xffff) - borrow
;
1824 borrow
= (y
& 0x10000) >> 16;
1828 } while (sx
<= sxe
);
1832 while (--bxe
> bx
&& !*bxe
)
1840 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1842 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1843 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1846 * 1. Rather than iterating, we use a simple numeric overestimate
1847 * to determine k = floor(log10(d)). We scale relevant
1848 * quantities using O(log2(k)) rather than O(k) multiplications.
1849 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1850 * try to generate digits strictly left to right. Instead, we
1851 * compute with fewer bits and propagate the carry if necessary
1852 * when rounding the final digit up. This is often faster.
1853 * 3. Under the assumption that input will be rounded nearest,
1854 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1855 * That is, we allow equality in stopping tests when the
1856 * round-nearest rule will give the same floating-point value
1857 * as would satisfaction of the stopping test with strict
1859 * 4. We remove common factors of powers of 2 from relevant
1861 * 5. When converting floating-point integers less than 1e16,
1862 * we use floating-point arithmetic rather than resorting
1863 * to multiple-precision integers.
1864 * 6. When asked to produce fewer than 15 digits, we first try
1865 * to get by with floating-point arithmetic; we resort to
1866 * multiple-precision integer arithmetic only if we cannot
1867 * guarantee that the floating-point calculation has given
1868 * the correctly rounded result. For k requested digits and
1869 * "uniformly" distributed input, the probability is
1870 * something like 10^(k-15) that we must resort to the int32_t
1874 void dtoa(DtoaBuffer result
, double dd
, int ndigits
, int* decpt
, int* sign
, char** rve
)
1877 Arguments ndigits, decpt, sign are similar to those
1878 of ecvt and fcvt; trailing zeros are suppressed from
1879 the returned string. If not null, *rve is set to point
1880 to the end of the return value. If d is +-Infinity or NaN,
1881 then *decpt is set to 9999.
1885 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
= 0, ilim0
, ilim1
= 0,
1886 j
, j1
, k
, k0
, k_check
, leftright
, m2
, m5
, s2
, s5
,
1887 spec_case
, try_quick
;
1889 #ifndef Sudden_Underflow
1893 BigInt b
, b1
, delta
, mlo
, mhi
, S
;
1898 int inexact
, oldinexact
;
1902 if (word0(&u
) & Sign_bit
) {
1903 /* set sign for everything, including 0's and NaNs */
1905 word0(&u
) &= ~Sign_bit
; /* clear sign bit */
1909 if ((word0(&u
) & Exp_mask
) == Exp_mask
)
1911 /* Infinity or NaN */
1913 if (!word1(&u
) && !(word0(&u
) & 0xfffff)) {
1914 strcpy(result
, "Infinity");
1918 strcpy(result
, "NaN");
1934 try_quick
= oldinexact
= get_inexact();
1938 d2b(b
, &u
, &be
, &bbits
);
1939 #ifdef Sudden_Underflow
1940 i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
));
1942 if ((i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
)))) {
1944 dval(&d2
) = dval(&u
);
1945 word0(&d2
) &= Frac_mask1
;
1946 word0(&d2
) |= Exp_11
;
1948 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
1949 * log10(x) = log(x) / log(10)
1950 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1951 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1953 * This suggests computing an approximation k to log10(d) by
1955 * k = (i - Bias)*0.301029995663981
1956 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1958 * We want k to be too large rather than too small.
1959 * The error in the first-order Taylor series approximation
1960 * is in our favor, so we just round up the constant enough
1961 * to compensate for any error in the multiplication of
1962 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
1963 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
1964 * adding 1e-13 to the constant term more than suffices.
1965 * Hence we adjust the constant term to 0.1760912590558.
1966 * (We could get a more accurate k by invoking log10,
1967 * but this is probably not worthwhile.)
1971 #ifndef Sudden_Underflow
1974 /* d is denormalized */
1976 i
= bbits
+ be
+ (Bias
+ (P
- 1) - 1);
1977 x
= (i
> 32) ? (word0(&u
) << (64 - i
)) | (word1(&u
) >> (i
- 32))
1978 : word1(&u
) << (32 - i
);
1980 word0(&d2
) -= 31 * Exp_msk1
; /* adjust exponent */
1981 i
-= (Bias
+ (P
- 1) - 1) + 1;
1985 ds
= (dval(&d2
) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i
* 0.301029995663981);
1987 if (ds
< 0. && ds
!= k
)
1988 k
--; /* want k = floor(ds) */
1990 if (k
>= 0 && k
<= Ten_pmax
) {
1991 if (dval(&u
) < tens
[k
])
2014 #ifdef Check_FLT_ROUNDS
2015 try_quick
= Rounding
== 1;
2019 #endif /*SET_INEXACT*/
2027 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
2029 /* Try to get by with floating-point arithmetic. */
2032 dval(&d2
) = dval(&u
);
2035 ieps
= 2; /* conservative */
2040 /* prevent overflows */
2042 dval(&u
) /= bigtens
[n_bigtens
- 1];
2045 for (; j
; j
>>= 1, i
++) {
2052 } else if ((j1
= -k
)) {
2053 dval(&u
) *= tens
[j1
& 0xf];
2054 for (j
= j1
>> 4; j
; j
>>= 1, i
++) {
2057 dval(&u
) *= bigtens
[i
];
2061 if (k_check
&& dval(&u
) < 1. && ilim
> 0) {
2069 dval(&eps
) = (ieps
* dval(&u
)) + 7.;
2070 word0(&eps
) -= (P
- 1) * Exp_msk1
;
2075 if (dval(&u
) > dval(&eps
))
2077 if (dval(&u
) < -dval(&eps
))
2081 #ifndef No_leftright
2083 /* Use Steele & White method of only
2084 * generating digits needed.
2086 dval(&eps
) = (0.5 / tens
[ilim
- 1]) - dval(&eps
);
2088 L
= (long int)dval(&u
);
2090 *s
++ = '0' + (int)L
;
2091 if (dval(&u
) < dval(&eps
))
2093 if (1. - dval(&u
) < dval(&eps
))
2102 /* Generate ilim digits, then fix them up. */
2103 dval(&eps
) *= tens
[ilim
- 1];
2104 for (i
= 1;; i
++, dval(&u
) *= 10.) {
2105 L
= (int32_t)(dval(&u
));
2106 if (!(dval(&u
) -= L
))
2108 *s
++ = '0' + (int)L
;
2110 if (dval(&u
) > 0.5 + dval(&eps
))
2112 else if (dval(&u
) < 0.5 - dval(&eps
)) {
2113 while (*--s
== '0') { }
2120 #ifndef No_leftright
2125 dval(&u
) = dval(&d2
);
2130 /* Do we have a "small" integer? */
2132 if (be
>= 0 && k
<= Int_max
) {
2135 if (ndigits
< 0 && ilim
<= 0) {
2138 if (ilim
< 0 || dval(&u
) <= 5 * ds
)
2142 for (i
= 1;; i
++, dval(&u
) *= 10.) {
2143 L
= (int32_t)(dval(&u
) / ds
);
2145 #ifdef Check_FLT_ROUNDS
2146 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2152 *s
++ = '0' + (int)L
;
2160 dval(&u
) += dval(&u
);
2161 if (dval(&u
) > ds
|| (dval(&u
) == ds
&& (L
& 1))) {
2183 #ifndef Sudden_Underflow
2184 denorm
? be
+ (Bias
+ (P
- 1) - 1 + 1) :
2191 if (m2
> 0 && s2
> 0) {
2192 i
= m2
< s2
? m2
: s2
;
2212 /* Check for special case that d is a normalized power of 2. */
2215 if (!word1(&u
) && !(word0(&u
) & Bndry_mask
)
2216 #ifndef Sudden_Underflow
2217 && word0(&u
) & (Exp_mask
& ~Exp_msk1
)
2220 /* The special case */
2226 /* Arrange for convenient computation of quotients:
2227 * shift left if necessary so divisor has 4 leading 0 bits.
2229 * Perhaps we should just compute leading 28 bits of S once
2230 * and for all and pass them and a shift to quorem, so it
2231 * can do shifts and ors to compute the numerator for q.
2234 if ((i
= ((s5
? 32 - hi0bits(S
.words()[S
.size() - 1]) : 1) + s2
) & 0x1f))
2237 if ((i
= ((s5
? 32 - hi0bits(S
.words()[S
.size() - 1]) : 1) + s2
) & 0xf))
2258 multadd(b
, 10, 0); /* we botched the k estimate */
2260 multadd(mhi
, 10, 0);
2269 /* Compute mlo -- check for special case
2270 * that d is a normalized power of 2.
2280 dig
= quorem(b
,S
) + '0';
2281 /* Do we yet have the shortest decimal string
2282 * that will round to d?
2285 diff(delta
, S
, mhi
);
2286 j1
= delta
.sign
? 1 : cmp(b
, delta
);
2287 if (j1
== 0 && !(word1(&u
) & 1)) {
2293 else if (!b
->x
[0] && b
->wds
<= 1)
2299 if (j
< 0 || (j
== 0 && !(word1(&u
) & 1))) {
2300 if (!b
.words()[0] && b
.size() <= 1) {
2309 if ((j1
> 0 || (j1
== 0 && (dig
& 1))) && dig
++ == '9')
2317 if (dig
== '9') { /* possible if i == 1 */
2329 multadd(mlo
, 10, 0);
2330 multadd(mhi
, 10, 0);
2334 *s
++ = dig
= quorem(b
,S
) + '0';
2335 if (!b
.words()[0] && b
.size() <= 1) {
2346 /* Round off last digit */
2350 if (j
> 0 || (j
== 0 && (dig
& 1))) {
2360 while (*--s
== '0') { }
2375 word0(&u
) = Exp_1
+ (70 << Exp_shift
);
2379 } else if (!oldinexact
)
2388 static ALWAYS_INLINE
void append(char*& next
, const char* src
, unsigned size
)
2390 for (unsigned i
= 0; i
< size
; ++i
)
2394 void doubleToStringInJavaScriptFormat(double d
, DtoaBuffer buffer
, unsigned* resultLength
)
2398 // avoid ever printing -NaN, in JS conceptually there is only one NaN value
2400 append(buffer
, "NaN", 3);
2417 char* resultEnd
= 0;
2418 WTF::dtoa(result
, d
, 0, &decimalPoint
, &sign
, &resultEnd
);
2419 int length
= resultEnd
- result
;
2421 char* next
= buffer
;
2425 if (decimalPoint
<= 0 && decimalPoint
> -6) {
2428 for (int j
= decimalPoint
; j
< 0; j
++)
2430 append(next
, result
, length
);
2431 } else if (decimalPoint
<= 21 && decimalPoint
> 0) {
2432 if (length
<= decimalPoint
) {
2433 append(next
, result
, length
);
2434 for (int j
= 0; j
< decimalPoint
- length
; j
++)
2437 append(next
, result
, decimalPoint
);
2439 append(next
, result
+ decimalPoint
, length
- decimalPoint
);
2441 } else if (result
[0] < '0' || result
[0] > '9')
2442 append(next
, result
, length
);
2444 *next
++ = result
[0];
2447 append(next
, result
+ 1, length
- 1);
2451 *next
++ = (decimalPoint
>= 0) ? '+' : '-';
2452 // decimalPoint can't be more than 3 digits decimal given the
2453 // nature of float representation
2454 int exponential
= decimalPoint
- 1;
2455 if (exponential
< 0)
2456 exponential
= -exponential
;
2457 if (exponential
>= 100)
2458 *next
++ = static_cast<char>('0' + exponential
/ 100);
2459 if (exponential
>= 10)
2460 *next
++ = static_cast<char>('0' + (exponential
% 100) / 10);
2461 *next
++ = static_cast<char>('0' + exponential
% 10);
2464 *resultLength
= next
- buffer
;