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, 2010 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 David M. Gay (dmg at acm dot org, 
  22  * with " at " changed at "@" and " dot " changed to ".").    */ 
  24 /* On a machine with IEEE extended-precision registers, it is 
  25  * necessary to specify double-precision (53-bit) rounding precision 
  26  * before invoking strtod or dtoa.  If the machine uses (the equivalent 
  27  * of) Intel 80x87 arithmetic, the call 
  28  *    _control87(PC_53, MCW_PC); 
  29  * does this with many compilers.  Whether this or another call is 
  30  * appropriate depends on the compiler; for this to work, it may be 
  31  * necessary to #include "float.h" or another system-dependent header 
  35 /* strtod for IEEE-arithmetic machines. 
  37  * This strtod returns a nearest machine number to the input decimal 
  38  * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are 
  39  * broken by the IEEE round-even rule.  Otherwise ties are broken by 
  40  * biased rounding (add half and chop). 
  42  * Inspired loosely by William D. Clinger's paper "How to Read Floating 
  43  * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101]. 
  47  *    1. We only require IEEE double-precision arithmetic (not IEEE double-extended). 
  48  *    2. We get by with floating-point arithmetic in a case that 
  49  *        Clinger missed -- when we're computing d * 10^n 
  50  *        for a small integer d and the integer n is not too 
  51  *        much larger than 22 (the maximum integer k for which 
  52  *        we can represent 10^k exactly), we may be able to 
  53  *        compute (d*10^k) * 10^(e-k) with just one roundoff. 
  54  *    3. Rather than a bit-at-a-time adjustment of the binary 
  55  *        result in the hard case, we use floating-point 
  56  *        arithmetic to determine the adjustment to within 
  57  *        one bit; only in really hard cases do we need to 
  58  *        compute a second residual. 
  59  *    4. Because of 3., we don't need a large table of powers of 10 
  60  *        for ten-to-e (just some small tables, e.g. of 10^k 
  76 #include <wtf/AlwaysInline.h> 
  77 #include <wtf/Assertions.h> 
  78 #include <wtf/DecimalNumber.h> 
  79 #include <wtf/FastMalloc.h> 
  80 #include <wtf/MathExtras.h> 
  81 #include <wtf/Threading.h> 
  82 #include <wtf/UnusedParam.h> 
  83 #include <wtf/Vector.h> 
  86 #pragma warning(disable: 4244) 
  87 #pragma warning(disable: 4245) 
  88 #pragma warning(disable: 4554) 
  93 #if ENABLE(WTF_MULTIPLE_THREADS) 
 102 #if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN) 
 103 #define word0(x) (x)->L[0] 
 104 #define word1(x) (x)->L[1] 
 106 #define word0(x) (x)->L[1] 
 107 #define word1(x) (x)->L[0] 
 109 #define dval(x) (x)->d 
 111 /* The following definition of Storeinc is appropriate for MIPS processors. 
 112  * An alternative that might be better on some machines is 
 113  *  *p++ = high << 16 | low & 0xffff; 
 115 static ALWAYS_INLINE 
uint32_t* storeInc(uint32_t* p
, uint16_t high
, uint16_t low
) 
 117     uint16_t* p16 
= reinterpret_cast<uint16_t*>(p
); 
 129 #define Exp_shift1 20 
 130 #define Exp_msk1    0x100000 
 131 #define Exp_msk11   0x100000 
 132 #define Exp_mask  0x7ff00000 
 136 #define Exp_1  0x3ff00000 
 137 #define Exp_11 0x3ff00000 
 139 #define Frac_mask  0xfffff 
 140 #define Frac_mask1 0xfffff 
 143 #define Bndry_mask  0xfffff 
 144 #define Bndry_mask1 0xfffff 
 146 #define Sign_bit 0x80000000 
 153 #define rounded_product(a, b) a *= b 
 154 #define rounded_quotient(a, b) a /= b 
 156 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1)) 
 157 #define Big1 0xffffffff 
 159 #if CPU(PPC64) || CPU(X86_64) 
 160 // FIXME: should we enable this on all 64-bit CPUs? 
 161 // 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware. 
 162 #define USE_LONG_LONG 
 166     BigInt() : sign(0) { } 
 177         return m_words
.size(); 
 180     void resize(size_t s
) 
 187         return m_words
.data(); 
 190     const uint32_t* words() const 
 192         return m_words
.data(); 
 195     void append(uint32_t w
) 
 200     Vector
<uint32_t, 16> m_words
; 
 203 static void multadd(BigInt
& b
, int m
, int a
)    /* multiply by m and add a */ 
 206     unsigned long long carry
; 
 212     uint32_t* x 
= b
.words(); 
 217         unsigned long long y 
= *x 
* (unsigned long long)m 
+ carry
; 
 219         *x
++ = (uint32_t)y 
& 0xffffffffUL
; 
 222         uint32_t y 
= (xi 
& 0xffff) * m 
+ carry
; 
 223         uint32_t z 
= (xi 
>> 16) * m 
+ (y 
>> 16); 
 225         *x
++ = (z 
<< 16) + (y 
& 0xffff); 
 230         b
.append((uint32_t)carry
); 
 233 static void s2b(BigInt
& b
, const char* s
, int nd0
, int nd
, uint32_t y9
) 
 243             multadd(b
, 10, *s
++ - '0'); 
 249         multadd(b
, 10, *s
++ - '0'); 
 252 static int hi0bits(uint32_t x
) 
 256     if (!(x 
& 0xffff0000)) { 
 260     if (!(x 
& 0xff000000)) { 
 264     if (!(x 
& 0xf0000000)) { 
 268     if (!(x 
& 0xc0000000)) { 
 272     if (!(x 
& 0x80000000)) { 
 274         if (!(x 
& 0x40000000)) 
 280 static int lo0bits(uint32_t* y
) 
 322 static void i2b(BigInt
& b
, int i
) 
 329 static void mult(BigInt
& aRef
, const BigInt
& bRef
) 
 331     const BigInt
* a 
= &aRef
; 
 332     const BigInt
* b 
= &bRef
; 
 335     const uint32_t* x 
= 0; 
 344     unsigned long long carry
, z
; 
 349     if (a
->size() < b
->size()) { 
 350         const BigInt
* tmp 
= a
; 
 360     for (xc 
= c
.words(), xa 
= xc 
+ wc
; xc 
< xa
; xc
++) 
 368     for (; xb 
< xbe
; xc0
++) { 
 374                 z 
= *x
++ * (unsigned long long)y 
+ *xc 
+ carry
; 
 376                 *xc
++ = (uint32_t)z 
& 0xffffffffUL
; 
 378             *xc 
= (uint32_t)carry
; 
 382     for (; xb 
< xbe
; xb
++, xc0
++) { 
 383         if ((y 
= *xb 
& 0xffff)) { 
 388                 z 
= (*x 
& 0xffff) * y 
+ (*xc 
& 0xffff) + carry
; 
 390                 uint32_t z2 
= (*x
++ >> 16) * y 
+ (*xc 
>> 16) + carry
; 
 392                 xc 
= storeInc(xc
, z2
, z
); 
 396         if ((y 
= *xb 
>> 16)) { 
 402                 z 
= (*x 
& 0xffff) * y 
+ (*xc 
>> 16) + carry
; 
 404                 xc 
= storeInc(xc
, z
, z2
); 
 405                 z2 
= (*x
++ >> 16) * y 
+ (*xc 
& 0xffff) + carry
; 
 412     for (xc0 
= c
.words(), xc 
= xc0 
+ wc
; wc 
> 0 && !*--xc
; --wc
) { } 
 418     WTF_MAKE_NONCOPYABLE(P5Node
); WTF_MAKE_FAST_ALLOCATED
; 
 428 static ALWAYS_INLINE 
void pow5mult(BigInt
& b
, int k
) 
 430     static int p05
[3] = { 5, 25, 125 }; 
 433         multadd(b
, p05
[i 
- 1], 0); 
 438 #if ENABLE(WTF_MULTIPLE_THREADS) 
 439     s_dtoaP5Mutex
->lock(); 
 452     int p5sCountLocal 
= p5sCount
; 
 453 #if ENABLE(WTF_MULTIPLE_THREADS) 
 454     s_dtoaP5Mutex
->unlock(); 
 465         if (++p5sUsed 
== p5sCountLocal
) { 
 466 #if ENABLE(WTF_MULTIPLE_THREADS) 
 467             s_dtoaP5Mutex
->lock(); 
 469             if (p5sUsed 
== p5sCount
) { 
 471                 p5
->next 
= new P5Node
; 
 473                 p5
->next
->val 
= p5
->val
; 
 474                 mult(p5
->next
->val
, p5
->next
->val
); 
 478             p5sCountLocal 
= p5sCount
; 
 479 #if ENABLE(WTF_MULTIPLE_THREADS) 
 480             s_dtoaP5Mutex
->unlock(); 
 487 static ALWAYS_INLINE 
void lshift(BigInt
& b
, int k
) 
 491     int origSize 
= b
.size(); 
 492     int n1 
= n 
+ origSize 
+ 1; 
 495         b
.resize(b
.size() + n 
+ 1); 
 497         b
.resize(b
.size() + n
); 
 499     const uint32_t* srcStart 
= b
.words(); 
 500     uint32_t* dstStart 
= b
.words(); 
 501     const uint32_t* src 
= srcStart 
+ origSize 
- 1; 
 502     uint32_t* dst 
= dstStart 
+ n1 
- 1; 
 504         uint32_t hiSubword 
= 0; 
 506         for (; src 
>= srcStart
; --src
) { 
 507             *dst
-- = hiSubword 
| *src 
>> s
; 
 508             hiSubword 
= *src 
<< k
; 
 511         ASSERT(dst 
== dstStart 
+ n
); 
 513         b
.resize(origSize 
+ n 
+ !!b
.words()[n1 
- 1]); 
 518         } while (src 
>= srcStart
); 
 520     for (dst 
= dstStart 
+ n
; dst 
!= dstStart
; ) 
 523     ASSERT(b
.size() <= 1 || b
.words()[b
.size() - 1]); 
 526 static int cmp(const BigInt
& a
, const BigInt
& b
) 
 528     const uint32_t *xa
, *xa0
, *xb
, *xb0
; 
 533     ASSERT(i 
<= 1 || a
.words()[i 
- 1]); 
 534     ASSERT(j 
<= 1 || b
.words()[j 
- 1]); 
 543             return *xa 
< *xb 
? -1 : 1; 
 550 static ALWAYS_INLINE 
void diff(BigInt
& c
, const BigInt
& aRef
, const BigInt
& bRef
) 
 552     const BigInt
* a 
= &aRef
; 
 553     const BigInt
* b 
= &bRef
; 
 565         const BigInt
* tmp 
= a
; 
 573     const uint32_t* xa 
= a
->words(); 
 574     const uint32_t* xae 
= xa 
+ wa
; 
 576     const uint32_t* xb 
= b
->words(); 
 577     const uint32_t* xbe 
= xb 
+ wb
; 
 583     unsigned long long borrow 
= 0; 
 585         unsigned long long y 
= (unsigned long long)*xa
++ - *xb
++ - borrow
; 
 586         borrow 
= y 
>> 32 & (uint32_t)1; 
 587         *xc
++ = (uint32_t)y 
& 0xffffffffUL
; 
 590         unsigned long long y 
= *xa
++ - borrow
; 
 591         borrow 
= y 
>> 32 & (uint32_t)1; 
 592         *xc
++ = (uint32_t)y 
& 0xffffffffUL
; 
 597         uint32_t y 
= (*xa 
& 0xffff) - (*xb 
& 0xffff) - borrow
; 
 598         borrow 
= (y 
& 0x10000) >> 16; 
 599         uint32_t z 
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
; 
 600         borrow 
= (z 
& 0x10000) >> 16; 
 601         xc 
= storeInc(xc
, z
, y
); 
 604         uint32_t y 
= (*xa 
& 0xffff) - borrow
; 
 605         borrow 
= (y 
& 0x10000) >> 16; 
 606         uint32_t z 
= (*xa
++ >> 16) - borrow
; 
 607         borrow 
= (z 
& 0x10000) >> 16; 
 608         xc 
= storeInc(xc
, z
, y
); 
 616 static double ulp(U 
*x
) 
 621     L 
= (word0(x
) & Exp_mask
) - (P 
- 1) * Exp_msk1
; 
 627 static double b2d(const BigInt
& a
, int* e
) 
 647         d0 
= Exp_1 
| (y 
>> (Ebits 
- k
)); 
 648         w 
= xa 
> xa0 
? *--xa 
: 0; 
 649         d1 
= (y 
<< (32 - Ebits 
+ k
)) | (w 
>> (Ebits 
- k
)); 
 652     z 
= xa 
> xa0 
? *--xa 
: 0; 
 654         d0 
= Exp_1 
| (y 
<< k
) | (z 
>> (32 - k
)); 
 655         y 
= xa 
> xa0 
? *--xa 
: 0; 
 656         d1 
= (z 
<< k
) | (y 
>> (32 - k
)); 
 667 static ALWAYS_INLINE 
void d2b(BigInt
& b
, U
* d
, int* e
, int* bits
) 
 681     d0 
&= 0x7fffffff;    /* clear sign bit, which we ignore */ 
 682     if ((de 
= (int)(d0 
>> Exp_shift
))) 
 685         if ((k 
= lo0bits(&y
))) { 
 686             x
[0] = y 
| (z 
<< (32 - k
)); 
 704         *e 
= de 
- Bias 
- (P 
- 1) + k
; 
 707         *e 
= de 
- Bias 
- (P 
- 1) + 1 + k
; 
 708         *bits 
= (32 * i
) - hi0bits(x
[i 
- 1]); 
 714 static double ratio(const BigInt
& a
, const BigInt
& b
) 
 719     dval(&da
) = b2d(a
, &ka
); 
 720     dval(&db
) = b2d(b
, &kb
); 
 721     k 
= ka 
- kb 
+ 32 * (a
.size() - b
.size()); 
 723         word0(&da
) += k 
* Exp_msk1
; 
 726         word0(&db
) += k 
* Exp_msk1
; 
 728     return dval(&da
) / dval(&db
); 
 731 static const double tens
[] = { 
 732     1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
, 
 733     1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
, 
 737 static const double bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256 
}; 
 738 static const double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128, 
 739     9007199254740992. * 9007199254740992.e
-256 
 740     /* = 2^106 * 1e-256 */ 
 743 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */ 
 744 /* flag unnecessarily.  It leads to a song and dance at the end of strtod. */ 
 745 #define Scale_Bit 0x10 
 748 double strtod(const char* s00
, char** se
) 
 751     int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
, 
 752         e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
; 
 753     const char *s
, *s0
, *s1
; 
 755     U aadj2
, adj
, rv
, rv0
; 
 758     BigInt bb
, bb1
, bd
, bd0
, bs
, delta
; 
 762     for (s 
= s00
; ; s
++) { 
 787         while (*++s 
== '0') { } 
 793     for (nd 
= nf 
= 0; (c 
= *s
) >= '0' && c 
<= '9'; nd
++, s
++) 
 795             y 
= (10 * y
) + c 
- '0'; 
 797             z 
= (10 * z
) + c 
- '0'; 
 802             for (; c 
== '0'; c 
= *++s
) 
 804             if (c 
> '0' && c 
<= '9') { 
 812         for (; c 
>= '0' && c 
<= '9'; c 
= *++s
) { 
 817                 for (i 
= 1; i 
< nz
; i
++) 
 820                     else if (nd 
<= DBL_DIG 
+ 1) 
 824                 else if (nd 
<= DBL_DIG 
+ 1) 
 832     if (c 
== 'e' || c 
== 'E') { 
 833         if (!nd 
&& !nz 
&& !nz0
) 
 843         if (c 
>= '0' && c 
<= '9') { 
 846             if (c 
> '0' && c 
<= '9') { 
 849                 while ((c 
= *++s
) >= '0' && c 
<= '9') 
 850                     L 
= (10 * L
) + c 
- '0'; 
 851                 if (s 
- s1 
> 8 || L 
> 19999) 
 852                     /* Avoid confusion from exponents 
 853                      * so large that e might overflow. 
 855                     e 
= 19999; /* safe for 16 bit ints */ 
 875     /* Now we have nd0 digits, starting at s0, followed by a 
 876      * decimal point, followed by nd-nd0 digits.  The number we're 
 877      * after is the integer represented by those digits times 
 882     k 
= nd 
< DBL_DIG 
+ 1 ? nd 
: DBL_DIG 
+ 1; 
 885         dval(&rv
) = tens
[k 
- 9] * dval(&rv
) + z
; 
 891                 /* rv = */ rounded_product(dval(&rv
), tens
[e
]); 
 895             if (e 
<= Ten_pmax 
+ i
) { 
 896                 /* A fancier test would sometimes let us do 
 897                  * this for larger i values. 
 900                 dval(&rv
) *= tens
[i
]; 
 901                 /* rv = */ rounded_product(dval(&rv
), tens
[e
]); 
 904         } else if (e 
>= -Ten_pmax
) { 
 905             /* rv = */ rounded_quotient(dval(&rv
), tens
[-e
]); 
 913     /* Get starting approximation = rv * 10**e1 */ 
 917             dval(&rv
) *= tens
[i
]; 
 919             if (e1 
> DBL_MAX_10_EXP
) { 
 924                 /* Can't trust HUGE_VAL */ 
 925                 word0(&rv
) = Exp_mask
; 
 930             for (j 
= 0; e1 
> 1; j
++, e1 
>>= 1) 
 932                     dval(&rv
) *= bigtens
[j
]; 
 933         /* The last multiplication could overflow. */ 
 934             word0(&rv
) -= P 
* Exp_msk1
; 
 935             dval(&rv
) *= bigtens
[j
]; 
 936             if ((z 
= word0(&rv
) & Exp_mask
) > Exp_msk1 
* (DBL_MAX_EXP 
+ Bias 
- P
)) 
 938             if (z 
> Exp_msk1 
* (DBL_MAX_EXP 
+ Bias 
- 1 - P
)) { 
 939                 /* set to largest number */ 
 940                 /* (Can't trust DBL_MAX) */ 
 944                 word0(&rv
) += P 
* Exp_msk1
; 
 949             dval(&rv
) /= tens
[i
]; 
 951             if (e1 
>= 1 << n_bigtens
) 
 955             for (j 
= 0; e1 
> 0; j
++, e1 
>>= 1) 
 957                     dval(&rv
) *= tinytens
[j
]; 
 958             if (scale 
&& (j 
= (2 * P
) + 1 - ((word0(&rv
) & Exp_mask
) >> Exp_shift
)) > 0) { 
 959                 /* scaled rv is denormal; clear j low bits */ 
 963                         word0(&rv
) = (P 
+ 2) * Exp_msk1
; 
 965                         word0(&rv
) &= 0xffffffff << (j 
- 32); 
 967                     word1(&rv
) &= 0xffffffff << j
; 
 980     /* Now the hard part -- adjusting rv to the correct value.*/ 
 982     /* Put digits into bd: true value = bd * 10^e */ 
 984     s2b(bd0
, s0
, nd0
, nd
, y
); 
 988         d2b(bb
, &rv
, &bbe
, &bbbits
);    /* rv = bb * 2^bbe */ 
1004         i 
= j 
+ bbbits 
- 1;    /* logb(rv) */ 
1005         if (i 
< Emin
)    /* denormal */ 
1012         i 
= bb2 
< bd2 
? bb2 
: bd2
; 
1032         diff(delta
, bb
, bd
); 
1038             /* Error is less than half an ulp -- check for 
1039              * special case of mantissa a power of two. 
1041             if (dsign 
|| word1(&rv
) || word0(&rv
) & Bndry_mask
 
1042              || (word0(&rv
) & Exp_mask
) <= (2 * P 
+ 1) * Exp_msk1
 
1046             if (!delta
.words()[0] && delta
.size() <= 1) { 
1050             lshift(delta
, Log2P
); 
1051             if (cmp(delta
, bs
) > 0) 
1056             /* exactly half-way between */ 
1058                 if ((word0(&rv
) & Bndry_mask1
) == Bndry_mask1
 
1060             (scale 
&& (y 
= word0(&rv
) & Exp_mask
) <= 2 * P 
* Exp_msk1
) 
1061         ? (0xffffffff & (0xffffffff << (2 * P 
+ 1 - (y 
>> Exp_shift
)))) : 
1063                     /*boundary case -- increment exponent*/ 
1064                     word0(&rv
) = (word0(&rv
) & Exp_mask
) + Exp_msk1
; 
1069             } else if (!(word0(&rv
) & Bndry_mask
) && !word1(&rv
)) { 
1071                 /* boundary case -- decrement exponent */ 
1073                     L 
= word0(&rv
) & Exp_mask
; 
1074                     if (L 
<= (2 * P 
+ 1) * Exp_msk1
) { 
1075                         if (L 
> (P 
+ 2) * Exp_msk1
) 
1076                             /* round even ==> */ 
1079                         /* rv = smallest denormal */ 
1083                 L 
= (word0(&rv
) & Exp_mask
) - Exp_msk1
; 
1084                 word0(&rv
) = L 
| Bndry_mask1
; 
1085                 word1(&rv
) = 0xffffffff; 
1088             if (!(word1(&rv
) & LSB
)) 
1091                 dval(&rv
) += ulp(&rv
); 
1093                 dval(&rv
) -= ulp(&rv
); 
1100         if ((aadj 
= ratio(delta
, bs
)) <= 2.) { 
1103             else if (word1(&rv
) || word0(&rv
) & Bndry_mask
) { 
1104                 if (word1(&rv
) == Tiny1 
&& !word0(&rv
)) 
1109                 /* special case -- power of FLT_RADIX to be */ 
1110                 /* rounded down... */ 
1112                 if (aadj 
< 2. / FLT_RADIX
) 
1113                     aadj 
= 1. / FLT_RADIX
; 
1120             aadj1 
= dsign 
? aadj 
: -aadj
; 
1122         y 
= word0(&rv
) & Exp_mask
; 
1124         /* Check for overflow */ 
1126         if (y 
== Exp_msk1 
* (DBL_MAX_EXP 
+ Bias 
- 1)) { 
1127             dval(&rv0
) = dval(&rv
); 
1128             word0(&rv
) -= P 
* Exp_msk1
; 
1129             adj
.d 
= aadj1 
* ulp(&rv
); 
1131             if ((word0(&rv
) & Exp_mask
) >= Exp_msk1 
* (DBL_MAX_EXP 
+ Bias 
- P
)) { 
1132                 if (word0(&rv0
) == Big0 
&& word1(&rv0
) == Big1
) 
1138             word0(&rv
) += P 
* Exp_msk1
; 
1140             if (scale 
&& y 
<= 2 * P 
* Exp_msk1
) { 
1141                 if (aadj 
<= 0x7fffffff) { 
1142                     if ((z 
= (uint32_t)aadj
) <= 0) 
1145                     aadj1 
= dsign 
? aadj 
: -aadj
; 
1147                 dval(&aadj2
) = aadj1
; 
1148                 word0(&aadj2
) += (2 * P 
+ 1) * Exp_msk1 
- y
; 
1149                 aadj1 
= dval(&aadj2
); 
1151             adj
.d 
= aadj1 
* ulp(&rv
); 
1154         z 
= word0(&rv
) & Exp_mask
; 
1155         if (!scale 
&& y 
== z
) { 
1156             /* Can we stop now? */ 
1159             /* The tolerances below are conservative. */ 
1160             if (dsign 
|| word1(&rv
) || word0(&rv
) & Bndry_mask
) { 
1161                 if (aadj 
< .4999999 || aadj 
> .5000001) 
1163             } else if (aadj 
< .4999999 / FLT_RADIX
) 
1170         word0(&rv0
) = Exp_1 
- 2 * P 
* Exp_msk1
; 
1172         dval(&rv
) *= dval(&rv0
); 
1174         /* try to avoid the bug of testing an 8087 register value */ 
1175         if (!word0(&rv
) && !word1(&rv
)) 
1181         *se 
= const_cast<char*>(s
); 
1182     return sign 
? -dval(&rv
) : dval(&rv
); 
1185 static ALWAYS_INLINE 
int quorem(BigInt
& b
, BigInt
& S
) 
1193 #ifdef USE_LONG_LONG 
1194     unsigned long long borrow
, carry
, y
, ys
; 
1196     uint32_t borrow
, carry
, y
, ys
; 
1199     ASSERT(b
.size() <= 1 || b
.words()[b
.size() - 1]); 
1200     ASSERT(S
.size() <= 1 || S
.words()[S
.size() - 1]); 
1203     ASSERT_WITH_MESSAGE(b
.size() <= n
, "oversize b in quorem"); 
1210     q 
= *bxe 
/ (*sxe 
+ 1);    /* ensure q <= true quotient */ 
1211     ASSERT_WITH_MESSAGE(q 
<= 9, "oversized quotient in quorem"); 
1216 #ifdef USE_LONG_LONG 
1217             ys 
= *sx
++ * (unsigned long long)q 
+ carry
; 
1219             y 
= *bx 
- (ys 
& 0xffffffffUL
) - borrow
; 
1220             borrow 
= y 
>> 32 & (uint32_t)1; 
1221             *bx
++ = (uint32_t)y 
& 0xffffffffUL
; 
1224             ys 
= (si 
& 0xffff) * q 
+ carry
; 
1225             zs 
= (si 
>> 16) * q 
+ (ys 
>> 16); 
1227             y 
= (*bx 
& 0xffff) - (ys 
& 0xffff) - borrow
; 
1228             borrow 
= (y 
& 0x10000) >> 16; 
1229             z 
= (*bx 
>> 16) - (zs 
& 0xffff) - borrow
; 
1230             borrow 
= (z 
& 0x10000) >> 16; 
1231             bx 
= storeInc(bx
, z
, y
); 
1233         } while (sx 
<= sxe
); 
1236             while (--bxe 
> bx 
&& !*bxe
) 
1241     if (cmp(b
, S
) >= 0) { 
1248 #ifdef USE_LONG_LONG 
1251             y 
= *bx 
- (ys 
& 0xffffffffUL
) - borrow
; 
1252             borrow 
= y 
>> 32 & (uint32_t)1; 
1253             *bx
++ = (uint32_t)y 
& 0xffffffffUL
; 
1256             ys 
= (si 
& 0xffff) + carry
; 
1257             zs 
= (si 
>> 16) + (ys 
>> 16); 
1259             y 
= (*bx 
& 0xffff) - (ys 
& 0xffff) - borrow
; 
1260             borrow 
= (y 
& 0x10000) >> 16; 
1261             z 
= (*bx 
>> 16) - (zs 
& 0xffff) - borrow
; 
1262             borrow 
= (z 
& 0x10000) >> 16; 
1263             bx 
= storeInc(bx
, z
, y
); 
1265         } while (sx 
<= sxe
); 
1269             while (--bxe 
> bx 
&& !*bxe
) 
1277 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. 
1279  * Inspired by "How to Print Floating-Point Numbers Accurately" by 
1280  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126]. 
1283  *    1. Rather than iterating, we use a simple numeric overestimate 
1284  *       to determine k = floor(log10(d)).  We scale relevant 
1285  *       quantities using O(log2(k)) rather than O(k) multiplications. 
1286  *    2. For some modes > 2 (corresponding to ecvt and fcvt), we don't 
1287  *       try to generate digits strictly left to right.  Instead, we 
1288  *       compute with fewer bits and propagate the carry if necessary 
1289  *       when rounding the final digit up.  This is often faster. 
1290  *    3. Under the assumption that input will be rounded nearest, 
1291  *       mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. 
1292  *       That is, we allow equality in stopping tests when the 
1293  *       round-nearest rule will give the same floating-point value 
1294  *       as would satisfaction of the stopping test with strict 
1296  *    4. We remove common factors of powers of 2 from relevant 
1298  *    5. When converting floating-point integers less than 1e16, 
1299  *       we use floating-point arithmetic rather than resorting 
1300  *       to multiple-precision integers. 
1301  *    6. When asked to produce fewer than 15 digits, we first try 
1302  *       to get by with floating-point arithmetic; we resort to 
1303  *       multiple-precision integer arithmetic only if we cannot 
1304  *       guarantee that the floating-point calculation has given 
1305  *       the correctly rounded result.  For k requested digits and 
1306  *       "uniformly" distributed input, the probability is 
1307  *       something like 10^(k-15) that we must resort to the int32_t 
1310  * Note: 'leftright' translates to 'generate shortest possible string'. 
1312 template<bool roundingNone
, bool roundingSignificantFigures
, bool roundingDecimalPlaces
, bool leftright
> 
1313 void dtoa(DtoaBuffer result
, double dd
, int ndigits
, bool& signOut
, int& exponentOut
, unsigned& precisionOut
) 
1315     // Exactly one rounding mode must be specified. 
1316     ASSERT(roundingNone 
+ roundingSignificantFigures 
+ roundingDecimalPlaces 
== 1); 
1317     // roundingNone only allowed (only sensible?) with leftright set. 
1318     ASSERT(!roundingNone 
|| leftright
); 
1320     ASSERT(!isnan(dd
) && !isinf(dd
)); 
1322     int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim 
= 0, ilim0
, ilim1 
= 0, 
1323         j
, j1
, k
, k0
, k_check
, m2
, m5
, s2
, s5
, 
1328     BigInt b
, delta
, mlo
, mhi
, S
; 
1336     /* Infinity or NaN */ 
1337     ASSERT((word0(&u
) & Exp_mask
) != Exp_mask
); 
1339     // JavaScript toString conversion treats -0 as 0. 
1349     if (word0(&u
) & Sign_bit
) { 
1351         word0(&u
) &= ~Sign_bit
; // clear sign bit 
1355     d2b(b
, &u
, &be
, &bbits
); 
1356     if ((i 
= (int)(word0(&u
) >> Exp_shift1 
& (Exp_mask 
>> Exp_shift1
)))) { 
1357         dval(&d2
) = dval(&u
); 
1358         word0(&d2
) &= Frac_mask1
; 
1359         word0(&d2
) |= Exp_11
; 
1361         /* log(x)    ~=~ log(1.5) + (x-1.5)/1.5 
1362          * log10(x)     =  log(x) / log(10) 
1363          *        ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) 
1364          * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) 
1366          * This suggests computing an approximation k to log10(d) by 
1368          * k = (i - Bias)*0.301029995663981 
1369          *    + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); 
1371          * We want k to be too large rather than too small. 
1372          * The error in the first-order Taylor series approximation 
1373          * is in our favor, so we just round up the constant enough 
1374          * to compensate for any error in the multiplication of 
1375          * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, 
1376          * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, 
1377          * adding 1e-13 to the constant term more than suffices. 
1378          * Hence we adjust the constant term to 0.1760912590558. 
1379          * (We could get a more accurate k by invoking log10, 
1380          *  but this is probably not worthwhile.) 
1386         /* d is denormalized */ 
1388         i 
= bbits 
+ be 
+ (Bias 
+ (P 
- 1) - 1); 
1389         x 
= (i 
> 32) ? (word0(&u
) << (64 - i
)) | (word1(&u
) >> (i 
- 32)) 
1390                 : word1(&u
) << (32 - i
); 
1392         word0(&d2
) -= 31 * Exp_msk1
; /* adjust exponent */ 
1393         i 
-= (Bias 
+ (P 
- 1) - 1) + 1; 
1396     ds 
= (dval(&d2
) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i 
* 0.301029995663981); 
1398     if (ds 
< 0. && ds 
!= k
) 
1399         k
--;    /* want k = floor(ds) */ 
1401     if (k 
>= 0 && k 
<= Ten_pmax
) { 
1402         if (dval(&u
) < tens
[k
]) 
1429     if (roundingSignificantFigures
) { 
1432         ilim 
= ilim1 
= i 
= ndigits
; 
1434     if (roundingDecimalPlaces
) { 
1435         i 
= ndigits 
+ k 
+ 1; 
1444     if (ilim 
>= 0 && ilim 
<= Quick_max
) { 
1445         /* Try to get by with floating-point arithmetic. */ 
1448         dval(&d2
) = dval(&u
); 
1451         ieps 
= 2; /* conservative */ 
1456                 /* prevent overflows */ 
1458                 dval(&u
) /= bigtens
[n_bigtens 
- 1]; 
1461             for (; j
; j 
>>= 1, i
++) { 
1468         } else if ((j1 
= -k
)) { 
1469             dval(&u
) *= tens
[j1 
& 0xf]; 
1470             for (j 
= j1 
>> 4; j
; j 
>>= 1, i
++) { 
1473                     dval(&u
) *= bigtens
[i
]; 
1477         if (k_check 
&& dval(&u
) < 1. && ilim 
> 0) { 
1485         dval(&eps
) = (ieps 
* dval(&u
)) + 7.; 
1486         word0(&eps
) -= (P 
- 1) * Exp_msk1
; 
1491             if (dval(&u
) > dval(&eps
)) 
1493             if (dval(&u
) < -dval(&eps
)) 
1498             /* Use Steele & White method of only 
1499              * generating digits needed. 
1501             dval(&eps
) = (0.5 / tens
[ilim 
- 1]) - dval(&eps
); 
1503                 L 
= (long int)dval(&u
); 
1505                 *s
++ = '0' + (int)L
; 
1506                 if (dval(&u
) < dval(&eps
)) 
1508                 if (1. - dval(&u
) < dval(&eps
)) 
1516             /* Generate ilim digits, then fix them up. */ 
1517             dval(&eps
) *= tens
[ilim 
- 1]; 
1518             for (i 
= 1;; i
++, dval(&u
) *= 10.) { 
1519                 L 
= (int32_t)(dval(&u
)); 
1520                 if (!(dval(&u
) -= L
)) 
1522                 *s
++ = '0' + (int)L
; 
1524                     if (dval(&u
) > 0.5 + dval(&eps
)) 
1526                     if (dval(&u
) < 0.5 - dval(&eps
)) { 
1527                         while (*--s 
== '0') { } 
1537         dval(&u
) = dval(&d2
); 
1542     /* Do we have a "small" integer? */ 
1544     if (be 
>= 0 && k 
<= Int_max
) { 
1547         if (ndigits 
< 0 && ilim 
<= 0) { 
1550             if (ilim 
< 0 || dval(&u
) <= 5 * ds
) 
1554         for (i 
= 1;; i
++, dval(&u
) *= 10.) { 
1555             L 
= (int32_t)(dval(&u
) / ds
); 
1557             *s
++ = '0' + (int)L
; 
1562                 dval(&u
) += dval(&u
); 
1563                 if (dval(&u
) > ds 
|| (dval(&u
) == ds 
&& (L 
& 1))) { 
1584         i 
= denorm 
? be 
+ (Bias 
+ (P 
- 1) - 1 + 1) : 1 + P 
- bbits
; 
1589     if (m2 
> 0 && s2 
> 0) { 
1590         i 
= m2 
< s2 
? m2 
: s2
; 
1610     /* Check for special case that d is a normalized power of 2. */ 
1613     if ((roundingNone 
|| leftright
) && (!word1(&u
) && !(word0(&u
) & Bndry_mask
) && word0(&u
) & (Exp_mask 
& ~Exp_msk1
))) { 
1614         /* The special case */ 
1620     /* Arrange for convenient computation of quotients: 
1621      * shift left if necessary so divisor has 4 leading 0 bits. 
1623      * Perhaps we should just compute leading 28 bits of S once 
1624      * and for all and pass them and a shift to quorem, so it 
1625      * can do shifts and ors to compute the numerator for q. 
1627     if ((i 
= ((s5 
? 32 - hi0bits(S
.words()[S
.size() - 1]) : 1) + s2
) & 0x1f)) 
1645         if (cmp(b
, S
) < 0) { 
1647             multadd(b
, 10, 0);    /* we botched the k estimate */ 
1649                 multadd(mhi
, 10, 0); 
1653     if (ilim 
<= 0 && roundingDecimalPlaces
) { 
1657         // For IEEE-754 unbiased rounding this check should be <=, such that 0.5 would flush to zero. 
1666         /* Compute mlo -- check for special case 
1667          * that d is a normalized power of 2. 
1675             dig 
= quorem(b
, S
) + '0'; 
1676             /* Do we yet have the shortest decimal string 
1677              * that will round to d? 
1680             diff(delta
, S
, mhi
); 
1681             j1 
= delta
.sign 
? 1 : cmp(b
, delta
); 
1682 #ifdef DTOA_ROUND_BIASED 
1685             // FIXME: ECMA-262 specifies that equidistant results round away from 
1686             // zero, which probably means we shouldn't be on the unbiased code path 
1687             // (the (word1(&u) & 1) clause is looking highly suspicious). I haven't 
1688             // yet understood this code well enough to make the call, but we should 
1689             // probably be enabling DTOA_ROUND_BIASED. I think the interesting corner 
1690             // case to understand is probably "Math.pow(0.5, 24).toString()". 
1691             // I believe this value is interesting because I think it is precisely 
1692             // representable in binary floating point, and its decimal representation 
1693             // has a single digit that Steele & White reduction can remove, with the 
1694             // value 5 (thus equidistant from the next numbers above and below). 
1695             // We produce the correct answer using either codepath, and I don't as 
1696             // yet understand why. :-) 
1697             if (!j1 
&& !(word1(&u
) & 1)) { 
1705             if (j 
< 0 || (!j 
&& !(word1(&u
) & 1))) { 
1707                 if ((b
.words()[0] || b
.size() > 1) && (j1 
> 0)) { 
1710                     // For IEEE-754 round-to-even, this check should be (j1 > 0 || (!j1 && (dig & 1))), 
1711                     // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) should 
1712                     // be rounded away from zero. 
1723                 if (dig 
== '9') { /* possible if i == 1 */ 
1735             multadd(mlo
, 10, 0); 
1736             multadd(mhi
, 10, 0); 
1740             *s
++ = dig 
= quorem(b
, S
) + '0'; 
1741             if (!b
.words()[0] && b
.size() <= 1) 
1749     /* Round off last digit */ 
1753     // For IEEE-754 round-to-even, this check should be (j > 0 || (!j && (dig & 1))), 
1754     // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) should 
1755     // be rounded away from zero. 
1766         while (*--s 
== '0') { } 
1784     precisionOut 
= s 
- result
; 
1787 void dtoa(DtoaBuffer result
, double dd
, bool& sign
, int& exponent
, unsigned& precision
) 
1789     // flags are roundingNone, leftright. 
1790     dtoa
<true, false, false, true>(result
, dd
, 0, sign
, exponent
, precision
); 
1793 void dtoaRoundSF(DtoaBuffer result
, double dd
, int ndigits
, bool& sign
, int& exponent
, unsigned& precision
) 
1795     // flag is roundingSignificantFigures. 
1796     dtoa
<false, true, false, false>(result
, dd
, ndigits
, sign
, exponent
, precision
); 
1799 void dtoaRoundDP(DtoaBuffer result
, double dd
, int ndigits
, bool& sign
, int& exponent
, unsigned& precision
) 
1801     // flag is roundingDecimalPlaces. 
1802     dtoa
<false, false, true, false>(result
, dd
, ndigits
, sign
, exponent
, precision
); 
1805 static ALWAYS_INLINE 
void copyAsciiToUTF16(UChar
* next
, const char* src
, unsigned size
) 
1807     for (unsigned i 
= 0; i 
< size
; ++i
) 
1811 unsigned numberToString(double d
, NumberToStringBuffer buffer
) 
1813     // Handle NaN and Infinity. 
1814     if (isnan(d
) || isinf(d
)) { 
1816             copyAsciiToUTF16(buffer
, "NaN", 3); 
1820             copyAsciiToUTF16(buffer
, "Infinity", 8); 
1823         copyAsciiToUTF16(buffer
, "-Infinity", 9); 
1827     // Convert to decimal with rounding. 
1828     DecimalNumber 
number(d
); 
1829     return number
.exponent() >= -6 && number
.exponent() < 21 
1830         ? number
.toStringDecimal(buffer
, NumberToStringBufferLength
) 
1831         : number
.toStringExponential(buffer
, NumberToStringBufferLength
);