JavaScriptCore/wtf/dtoa.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /****************************************************************
       
     2  *
       
     3  * The author of this software is David M. Gay.
       
     4  *
       
     5  * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
       
     6  * Copyright (C) 2002, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
       
     7  *
       
     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.
       
    13  *
       
    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.
       
    18  *
       
    19  ***************************************************************/
       
    20 
       
    21 /* Please send bug reports to
       
    22     David M. Gay
       
    23     Bell Laboratories, Room 2C-463
       
    24     600 Mountain Avenue
       
    25     Murray Hill, NJ 07974-0636
       
    26     U.S.A.
       
    27     dmg@bell-labs.com
       
    28  */
       
    29 
       
    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
       
    38  * file.
       
    39  */
       
    40 
       
    41 /* strtod for IEEE-arithmetic machines.
       
    42  *
       
    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).
       
    47  *
       
    48  * Inspired loosely by William D. Clinger's paper "How to Read Floating
       
    49  * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
       
    50  *
       
    51  * Modifications:
       
    52  *
       
    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
       
    67  *        for 0 <= k <= 22).
       
    68  */
       
    69 
       
    70 /*
       
    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
       
   111  *    Sudden_Underflow.
       
   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.
       
   133  */
       
   134 
       
   135 #include "config.h"
       
   136 #include "dtoa.h"
       
   137 
       
   138 #if HAVE(ERRNO_H)
       
   139 #include <errno.h>
       
   140 #else
       
   141 #define NO_ERRNO
       
   142 #endif
       
   143 #include <math.h>
       
   144 #include <stdint.h>
       
   145 #include <stdio.h>
       
   146 #include <stdlib.h>
       
   147 #include <string.h>
       
   148 #include <wtf/AlwaysInline.h>
       
   149 #include <wtf/Assertions.h>
       
   150 #include <wtf/FastMalloc.h>
       
   151 #include <wtf/MathExtras.h>
       
   152 #include <wtf/Threading.h>
       
   153 #include <wtf/Vector.h>
       
   154 
       
   155 #if COMPILER(MSVC)
       
   156 #pragma warning(disable: 4244)
       
   157 #pragma warning(disable: 4245)
       
   158 #pragma warning(disable: 4554)
       
   159 #endif
       
   160 
       
   161 #if CPU(BIG_ENDIAN)
       
   162 #define IEEE_MC68k
       
   163 #elif CPU(MIDDLE_ENDIAN)
       
   164 #define IEEE_ARM
       
   165 #else
       
   166 #define IEEE_8087
       
   167 #endif
       
   168 
       
   169 #define INFNAN_CHECK
       
   170 
       
   171 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_ARM) != 1
       
   172 Exactly one of IEEE_8087, IEEE_ARM or IEEE_MC68k should be defined.
       
   173 #endif
       
   174 
       
   175 namespace WTF {
       
   176 
       
   177 #if ENABLE(JSC_MULTIPLE_THREADS)
       
   178 Mutex* s_dtoaP5Mutex;
       
   179 #endif
       
   180 
       
   181 typedef union {
       
   182     double d;
       
   183     uint32_t L[2];
       
   184 } U;
       
   185 
       
   186 #ifdef YES_ALIAS
       
   187 #define dval(x) x
       
   188 #ifdef IEEE_8087
       
   189 #define word0(x) ((uint32_t*)&x)[1]
       
   190 #define word1(x) ((uint32_t*)&x)[0]
       
   191 #else
       
   192 #define word0(x) ((uint32_t*)&x)[0]
       
   193 #define word1(x) ((uint32_t*)&x)[1]
       
   194 #endif
       
   195 #else
       
   196 #ifdef IEEE_8087
       
   197 #define word0(x) (x)->L[1]
       
   198 #define word1(x) (x)->L[0]
       
   199 #else
       
   200 #define word0(x) (x)->L[0]
       
   201 #define word1(x) (x)->L[1]
       
   202 #endif
       
   203 #define dval(x) (x)->d
       
   204 #endif
       
   205 
       
   206 /* The following definition of Storeinc is appropriate for MIPS processors.
       
   207  * An alternative that might be better on some machines is
       
   208  *  *p++ = high << 16 | low & 0xffff;
       
   209  */
       
   210 static ALWAYS_INLINE uint32_t* storeInc(uint32_t* p, uint16_t high, uint16_t low)
       
   211 {
       
   212     uint16_t* p16 = reinterpret_cast<uint16_t*>(p);
       
   213 #if defined(IEEE_8087) || defined(IEEE_ARM)
       
   214     p16[1] = high;
       
   215     p16[0] = low;
       
   216 #else
       
   217     p16[0] = high;
       
   218     p16[1] = low;
       
   219 #endif
       
   220     return p + 1;
       
   221 }
       
   222 
       
   223 #define Exp_shift  20
       
   224 #define Exp_shift1 20
       
   225 #define Exp_msk1    0x100000
       
   226 #define Exp_msk11   0x100000
       
   227 #define Exp_mask  0x7ff00000
       
   228 #define P 53
       
   229 #define Bias 1023
       
   230 #define Emin (-1022)
       
   231 #define Exp_1  0x3ff00000
       
   232 #define Exp_11 0x3ff00000
       
   233 #define Ebits 11
       
   234 #define Frac_mask  0xfffff
       
   235 #define Frac_mask1 0xfffff
       
   236 #define Ten_pmax 22
       
   237 #define Bletch 0x10
       
   238 #define Bndry_mask  0xfffff
       
   239 #define Bndry_mask1 0xfffff
       
   240 #define LSB 1
       
   241 #define Sign_bit 0x80000000
       
   242 #define Log2P 1
       
   243 #define Tiny0 0
       
   244 #define Tiny1 1
       
   245 #define Quick_max 14
       
   246 #define Int_max 14
       
   247 
       
   248 #if !defined(NO_IEEE_Scale)
       
   249 #undef Avoid_Underflow
       
   250 #define Avoid_Underflow
       
   251 #endif
       
   252 
       
   253 #if !defined(Flt_Rounds)
       
   254 #if defined(FLT_ROUNDS)
       
   255 #define Flt_Rounds FLT_ROUNDS
       
   256 #else
       
   257 #define Flt_Rounds 1
       
   258 #endif
       
   259 #endif /* Flt_Rounds */
       
   260 
       
   261 
       
   262 #define rounded_product(a, b) a *= b
       
   263 #define rounded_quotient(a, b) a /= b
       
   264 
       
   265 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
       
   266 #define Big1 0xffffffff
       
   267 
       
   268 
       
   269 // FIXME: we should remove non-Pack_32 mode since it is unused and unmaintained
       
   270 #ifndef Pack_32
       
   271 #define Pack_32
       
   272 #endif
       
   273 
       
   274 #if CPU(PPC64) || CPU(X86_64)
       
   275 // FIXME: should we enable this on all 64-bit CPUs?
       
   276 // 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware.
       
   277 #define USE_LONG_LONG
       
   278 #endif
       
   279 
       
   280 #ifndef USE_LONG_LONG
       
   281 #ifdef Just_16
       
   282 #undef Pack_32
       
   283 /* When Pack_32 is not defined, we store 16 bits per 32-bit int32_t.
       
   284  * This makes some inner loops simpler and sometimes saves work
       
   285  * during multiplications, but it often seems to make things slightly
       
   286  * slower.  Hence the default is now to store 32 bits per int32_t.
       
   287  */
       
   288 #endif
       
   289 #endif
       
   290 
       
   291 #define Kmax 15
       
   292 
       
   293 struct BigInt {
       
   294     BigInt() : sign(0) { }
       
   295     int sign;
       
   296 
       
   297     void clear()
       
   298     {
       
   299         sign = 0;
       
   300         m_words.clear();
       
   301     }
       
   302     
       
   303     size_t size() const
       
   304     {
       
   305         return m_words.size();
       
   306     }
       
   307 
       
   308     void resize(size_t s)
       
   309     {
       
   310         m_words.resize(s);
       
   311     }
       
   312             
       
   313     uint32_t* words()
       
   314     {
       
   315         return m_words.data();
       
   316     }
       
   317 
       
   318     const uint32_t* words() const
       
   319     {
       
   320         return m_words.data();
       
   321     }
       
   322     
       
   323     void append(uint32_t w)
       
   324     {
       
   325         m_words.append(w);
       
   326     }
       
   327     
       
   328     Vector<uint32_t, 16> m_words;
       
   329 };
       
   330 
       
   331 static void multadd(BigInt& b, int m, int a)    /* multiply by m and add a */
       
   332 {
       
   333 #ifdef USE_LONG_LONG
       
   334     unsigned long long carry;
       
   335 #else
       
   336     uint32_t carry;
       
   337 #endif
       
   338 
       
   339     int wds = b.size();
       
   340     uint32_t* x = b.words();
       
   341     int i = 0;
       
   342     carry = a;
       
   343     do {
       
   344 #ifdef USE_LONG_LONG
       
   345         unsigned long long y = *x * (unsigned long long)m + carry;
       
   346         carry = y >> 32;
       
   347         *x++ = (uint32_t)y & 0xffffffffUL;
       
   348 #else
       
   349 #ifdef Pack_32
       
   350         uint32_t xi = *x;
       
   351         uint32_t y = (xi & 0xffff) * m + carry;
       
   352         uint32_t z = (xi >> 16) * m + (y >> 16);
       
   353         carry = z >> 16;
       
   354         *x++ = (z << 16) + (y & 0xffff);
       
   355 #else
       
   356         uint32_t y = *x * m + carry;
       
   357         carry = y >> 16;
       
   358         *x++ = y & 0xffff;
       
   359 #endif
       
   360 #endif
       
   361     } while (++i < wds);
       
   362 
       
   363     if (carry)
       
   364         b.append((uint32_t)carry);
       
   365 }
       
   366 
       
   367 static void s2b(BigInt& b, const char* s, int nd0, int nd, uint32_t y9)
       
   368 {
       
   369     int k;
       
   370     int32_t y;
       
   371     int32_t x = (nd + 8) / 9;
       
   372 
       
   373     for (k = 0, y = 1; x > y; y <<= 1, k++) { }
       
   374 #ifdef Pack_32
       
   375     b.sign = 0;
       
   376     b.resize(1);
       
   377     b.words()[0] = y9;
       
   378 #else
       
   379     b.sign = 0;
       
   380     b.resize((b->x[1] = y9 >> 16) ? 2 : 1);
       
   381     b.words()[0] = y9 & 0xffff;
       
   382 #endif
       
   383 
       
   384     int i = 9;
       
   385     if (9 < nd0) {
       
   386         s += 9;
       
   387         do {
       
   388             multadd(b, 10, *s++ - '0');
       
   389         } while (++i < nd0);
       
   390         s++;
       
   391     } else
       
   392         s += 10;
       
   393     for (; i < nd; i++)
       
   394         multadd(b, 10, *s++ - '0');
       
   395 }
       
   396 
       
   397 static int hi0bits(uint32_t x)
       
   398 {
       
   399     int k = 0;
       
   400 
       
   401     if (!(x & 0xffff0000)) {
       
   402         k = 16;
       
   403         x <<= 16;
       
   404     }
       
   405     if (!(x & 0xff000000)) {
       
   406         k += 8;
       
   407         x <<= 8;
       
   408     }
       
   409     if (!(x & 0xf0000000)) {
       
   410         k += 4;
       
   411         x <<= 4;
       
   412     }
       
   413     if (!(x & 0xc0000000)) {
       
   414         k += 2;
       
   415         x <<= 2;
       
   416     }
       
   417     if (!(x & 0x80000000)) {
       
   418         k++;
       
   419         if (!(x & 0x40000000))
       
   420             return 32;
       
   421     }
       
   422     return k;
       
   423 }
       
   424 
       
   425 static int lo0bits(uint32_t* y)
       
   426 {
       
   427     int k;
       
   428     uint32_t x = *y;
       
   429 
       
   430     if (x & 7) {
       
   431         if (x & 1)
       
   432             return 0;
       
   433         if (x & 2) {
       
   434             *y = x >> 1;
       
   435             return 1;
       
   436         }
       
   437         *y = x >> 2;
       
   438         return 2;
       
   439     }
       
   440     k = 0;
       
   441     if (!(x & 0xffff)) {
       
   442         k = 16;
       
   443         x >>= 16;
       
   444     }
       
   445     if (!(x & 0xff)) {
       
   446         k += 8;
       
   447         x >>= 8;
       
   448     }
       
   449     if (!(x & 0xf)) {
       
   450         k += 4;
       
   451         x >>= 4;
       
   452     }
       
   453     if (!(x & 0x3)) {
       
   454         k += 2;
       
   455         x >>= 2;
       
   456     }
       
   457     if (!(x & 1)) {
       
   458         k++;
       
   459         x >>= 1;
       
   460         if (!x & 1)
       
   461             return 32;
       
   462     }
       
   463     *y = x;
       
   464     return k;
       
   465 }
       
   466 
       
   467 static void i2b(BigInt& b, int i)
       
   468 {
       
   469     b.sign = 0;
       
   470     b.resize(1);
       
   471     b.words()[0] = i;
       
   472 }
       
   473 
       
   474 static void mult(BigInt& aRef, const BigInt& bRef)
       
   475 {
       
   476     const BigInt* a = &aRef;
       
   477     const BigInt* b = &bRef;
       
   478     BigInt c;
       
   479     int wa, wb, wc;
       
   480     const uint32_t* x = 0;
       
   481     const uint32_t* xa;
       
   482     const uint32_t* xb;
       
   483     const uint32_t* xae;
       
   484     const uint32_t* xbe;
       
   485     uint32_t* xc;
       
   486     uint32_t* xc0;
       
   487     uint32_t y;
       
   488 #ifdef USE_LONG_LONG
       
   489     unsigned long long carry, z;
       
   490 #else
       
   491     uint32_t carry, z;
       
   492 #endif
       
   493 
       
   494     if (a->size() < b->size()) {
       
   495         const BigInt* tmp = a;
       
   496         a = b;
       
   497         b = tmp;
       
   498     }
       
   499     
       
   500     wa = a->size();
       
   501     wb = b->size();
       
   502     wc = wa + wb;
       
   503     c.resize(wc);
       
   504 
       
   505     for (xc = c.words(), xa = xc + wc; xc < xa; xc++)
       
   506         *xc = 0;
       
   507     xa = a->words();
       
   508     xae = xa + wa;
       
   509     xb = b->words();
       
   510     xbe = xb + wb;
       
   511     xc0 = c.words();
       
   512 #ifdef USE_LONG_LONG
       
   513     for (; xb < xbe; xc0++) {
       
   514         if ((y = *xb++)) {
       
   515             x = xa;
       
   516             xc = xc0;
       
   517             carry = 0;
       
   518             do {
       
   519                 z = *x++ * (unsigned long long)y + *xc + carry;
       
   520                 carry = z >> 32;
       
   521                 *xc++ = (uint32_t)z & 0xffffffffUL;
       
   522             } while (x < xae);
       
   523             *xc = (uint32_t)carry;
       
   524         }
       
   525     }
       
   526 #else
       
   527 #ifdef Pack_32
       
   528     for (; xb < xbe; xb++, xc0++) {
       
   529         if ((y = *xb & 0xffff)) {
       
   530             x = xa;
       
   531             xc = xc0;
       
   532             carry = 0;
       
   533             do {
       
   534                 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
       
   535                 carry = z >> 16;
       
   536                 uint32_t z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
       
   537                 carry = z2 >> 16;
       
   538                 xc = storeInc(xc, z2, z);
       
   539             } while (x < xae);
       
   540             *xc = carry;
       
   541         }
       
   542         if ((y = *xb >> 16)) {
       
   543             x = xa;
       
   544             xc = xc0;
       
   545             carry = 0;
       
   546             uint32_t z2 = *xc;
       
   547             do {
       
   548                 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
       
   549                 carry = z >> 16;
       
   550                 xc = storeInc(xc, z, z2);
       
   551                 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
       
   552                 carry = z2 >> 16;
       
   553             } while (x < xae);
       
   554             *xc = z2;
       
   555         }
       
   556     }
       
   557 #else
       
   558     for (; xb < xbe; xc0++) {
       
   559         if ((y = *xb++)) {
       
   560             x = xa;
       
   561             xc = xc0;
       
   562             carry = 0;
       
   563             do {
       
   564                 z = *x++ * y + *xc + carry;
       
   565                 carry = z >> 16;
       
   566                 *xc++ = z & 0xffff;
       
   567             } while (x < xae);
       
   568             *xc = carry;
       
   569         }
       
   570     }
       
   571 #endif
       
   572 #endif
       
   573     for (xc0 = c.words(), xc = xc0 + wc; wc > 0 && !*--xc; --wc) { }
       
   574     c.resize(wc);
       
   575     aRef = c;
       
   576 }
       
   577 
       
   578 struct P5Node : Noncopyable {
       
   579     BigInt val;
       
   580     P5Node* next;
       
   581 };
       
   582     
       
   583 static P5Node* p5s;
       
   584 static int p5sCount;
       
   585 
       
   586 static ALWAYS_INLINE void pow5mult(BigInt& b, int k)
       
   587 {
       
   588     static int p05[3] = { 5, 25, 125 };
       
   589 
       
   590     if (int i = k & 3)
       
   591         multadd(b, p05[i - 1], 0);
       
   592 
       
   593     if (!(k >>= 2))
       
   594         return;
       
   595 
       
   596 #if ENABLE(JSC_MULTIPLE_THREADS)
       
   597     s_dtoaP5Mutex->lock();
       
   598 #endif
       
   599     P5Node* p5 = p5s;
       
   600 
       
   601     if (!p5) {
       
   602         /* first time */
       
   603         p5 = new P5Node;
       
   604         i2b(p5->val, 625);
       
   605         p5->next = 0;
       
   606         p5s = p5;
       
   607         p5sCount = 1;
       
   608     }
       
   609 
       
   610     int p5sCountLocal = p5sCount;
       
   611 #if ENABLE(JSC_MULTIPLE_THREADS)
       
   612     s_dtoaP5Mutex->unlock();
       
   613 #endif
       
   614     int p5sUsed = 0;
       
   615 
       
   616     for (;;) {
       
   617         if (k & 1)
       
   618             mult(b, p5->val);
       
   619 
       
   620         if (!(k >>= 1))
       
   621             break;
       
   622 
       
   623         if (++p5sUsed == p5sCountLocal) {
       
   624 #if ENABLE(JSC_MULTIPLE_THREADS)
       
   625             s_dtoaP5Mutex->lock();
       
   626 #endif
       
   627             if (p5sUsed == p5sCount) {
       
   628                 ASSERT(!p5->next);
       
   629                 p5->next = new P5Node;
       
   630                 p5->next->next = 0;
       
   631                 p5->next->val = p5->val;
       
   632                 mult(p5->next->val, p5->next->val);
       
   633                 ++p5sCount;
       
   634             }
       
   635             
       
   636             p5sCountLocal = p5sCount;
       
   637 #if ENABLE(JSC_MULTIPLE_THREADS)
       
   638             s_dtoaP5Mutex->unlock();
       
   639 #endif
       
   640         }
       
   641         p5 = p5->next;
       
   642     }
       
   643 }
       
   644 
       
   645 static ALWAYS_INLINE void lshift(BigInt& b, int k)
       
   646 {
       
   647 #ifdef Pack_32
       
   648     int n = k >> 5;
       
   649 #else
       
   650     int n = k >> 4;
       
   651 #endif
       
   652 
       
   653     int origSize = b.size();
       
   654     int n1 = n + origSize + 1;
       
   655 
       
   656     if (k &= 0x1f)
       
   657         b.resize(b.size() + n + 1);
       
   658     else
       
   659         b.resize(b.size() + n);
       
   660 
       
   661     const uint32_t* srcStart = b.words();
       
   662     uint32_t* dstStart = b.words();
       
   663     const uint32_t* src = srcStart + origSize - 1;
       
   664     uint32_t* dst = dstStart + n1 - 1;
       
   665 #ifdef Pack_32
       
   666     if (k) {
       
   667         uint32_t hiSubword = 0;
       
   668         int s = 32 - k;
       
   669         for (; src >= srcStart; --src) {
       
   670             *dst-- = hiSubword | *src >> s;
       
   671             hiSubword = *src << k;
       
   672         }
       
   673         *dst = hiSubword;
       
   674         ASSERT(dst == dstStart + n);
       
   675 
       
   676         b.resize(origSize + n + !!b.words()[n1 - 1]);
       
   677     }
       
   678 #else
       
   679     if (k &= 0xf) {
       
   680         uint32_t hiSubword = 0;
       
   681         int s = 16 - k;
       
   682         for (; src >= srcStart; --src) {
       
   683             *dst-- = hiSubword | *src >> s;
       
   684             hiSubword = (*src << k) & 0xffff;
       
   685         }
       
   686         *dst = hiSubword;
       
   687         ASSERT(dst == dstStart + n);
       
   688         result->wds = b->wds + n + !!result->x[n1 - 1];
       
   689      }
       
   690 #endif
       
   691     else {
       
   692         do {
       
   693             *--dst = *src--;
       
   694         } while (src >= srcStart);
       
   695     }
       
   696     for (dst = dstStart + n; dst != dstStart; )
       
   697         *--dst = 0;
       
   698 
       
   699     ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
       
   700 }
       
   701 
       
   702 static int cmp(const BigInt& a, const BigInt& b)
       
   703 {
       
   704     const uint32_t *xa, *xa0, *xb, *xb0;
       
   705     int i, j;
       
   706 
       
   707     i = a.size();
       
   708     j = b.size();
       
   709     ASSERT(i <= 1 || a.words()[i - 1]);
       
   710     ASSERT(j <= 1 || b.words()[j - 1]);
       
   711     if (i -= j)
       
   712         return i;
       
   713     xa0 = a.words();
       
   714     xa = xa0 + j;
       
   715     xb0 = b.words();
       
   716     xb = xb0 + j;
       
   717     for (;;) {
       
   718         if (*--xa != *--xb)
       
   719             return *xa < *xb ? -1 : 1;
       
   720         if (xa <= xa0)
       
   721             break;
       
   722     }
       
   723     return 0;
       
   724 }
       
   725 
       
   726 static ALWAYS_INLINE void diff(BigInt& c, const BigInt& aRef, const BigInt& bRef)
       
   727 {
       
   728     const BigInt* a = &aRef;
       
   729     const BigInt* b = &bRef;
       
   730     int i, wa, wb;
       
   731     uint32_t* xc;
       
   732 
       
   733     i = cmp(*a, *b);
       
   734     if (!i) {
       
   735         c.sign = 0;
       
   736         c.resize(1);
       
   737         c.words()[0] = 0;
       
   738         return;
       
   739     }
       
   740     if (i < 0) {
       
   741         const BigInt* tmp = a;
       
   742         a = b;
       
   743         b = tmp;
       
   744         i = 1;
       
   745     } else
       
   746         i = 0;
       
   747 
       
   748     wa = a->size();
       
   749     const uint32_t* xa = a->words();
       
   750     const uint32_t* xae = xa + wa;
       
   751     wb = b->size();
       
   752     const uint32_t* xb = b->words();
       
   753     const uint32_t* xbe = xb + wb;
       
   754 
       
   755     c.resize(wa);
       
   756     c.sign = i;
       
   757     xc = c.words();
       
   758 #ifdef USE_LONG_LONG
       
   759     unsigned long long borrow = 0;
       
   760     do {
       
   761         unsigned long long y = (unsigned long long)*xa++ - *xb++ - borrow;
       
   762         borrow = y >> 32 & (uint32_t)1;
       
   763         *xc++ = (uint32_t)y & 0xffffffffUL;
       
   764     } while (xb < xbe);
       
   765     while (xa < xae) {
       
   766         unsigned long long y = *xa++ - borrow;
       
   767         borrow = y >> 32 & (uint32_t)1;
       
   768         *xc++ = (uint32_t)y & 0xffffffffUL;
       
   769     }
       
   770 #else
       
   771     uint32_t borrow = 0;
       
   772 #ifdef Pack_32
       
   773     do {
       
   774         uint32_t y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
       
   775         borrow = (y & 0x10000) >> 16;
       
   776         uint32_t z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
       
   777         borrow = (z & 0x10000) >> 16;
       
   778         xc = storeInc(xc, z, y);
       
   779     } while (xb < xbe);
       
   780     while (xa < xae) {
       
   781         uint32_t y = (*xa & 0xffff) - borrow;
       
   782         borrow = (y & 0x10000) >> 16;
       
   783         uint32_t z = (*xa++ >> 16) - borrow;
       
   784         borrow = (z & 0x10000) >> 16;
       
   785         xc = storeInc(xc, z, y);
       
   786     }
       
   787 #else
       
   788     do {
       
   789         uint32_t y = *xa++ - *xb++ - borrow;
       
   790         borrow = (y & 0x10000) >> 16;
       
   791         *xc++ = y & 0xffff;
       
   792     } while (xb < xbe);
       
   793     while (xa < xae) {
       
   794         uint32_t y = *xa++ - borrow;
       
   795         borrow = (y & 0x10000) >> 16;
       
   796         *xc++ = y & 0xffff;
       
   797     }
       
   798 #endif
       
   799 #endif
       
   800     while (!*--xc)
       
   801         wa--;
       
   802     c.resize(wa);
       
   803 }
       
   804 
       
   805 static double ulp(U *x)
       
   806 {
       
   807     register int32_t L;
       
   808     U u;
       
   809 
       
   810     L = (word0(x) & Exp_mask) - (P - 1) * Exp_msk1;
       
   811 #ifndef Avoid_Underflow
       
   812 #ifndef Sudden_Underflow
       
   813     if (L > 0) {
       
   814 #endif
       
   815 #endif
       
   816         word0(&u) = L;
       
   817         word1(&u) = 0;
       
   818 #ifndef Avoid_Underflow
       
   819 #ifndef Sudden_Underflow
       
   820     } else {
       
   821         L = -L >> Exp_shift;
       
   822         if (L < Exp_shift) {
       
   823             word0(&u) = 0x80000 >> L;
       
   824             word1(&u) = 0;
       
   825         } else {
       
   826             word0(&u) = 0;
       
   827             L -= Exp_shift;
       
   828             word1(&u) = L >= 31 ? 1 : 1 << 31 - L;
       
   829         }
       
   830     }
       
   831 #endif
       
   832 #endif
       
   833     return dval(&u);
       
   834 }
       
   835 
       
   836 static double b2d(const BigInt& a, int* e)
       
   837 {
       
   838     const uint32_t* xa;
       
   839     const uint32_t* xa0;
       
   840     uint32_t w;
       
   841     uint32_t y;
       
   842     uint32_t z;
       
   843     int k;
       
   844     U d;
       
   845 
       
   846 #define d0 word0(&d)
       
   847 #define d1 word1(&d)
       
   848 
       
   849     xa0 = a.words();
       
   850     xa = xa0 + a.size();
       
   851     y = *--xa;
       
   852     ASSERT(y);
       
   853     k = hi0bits(y);
       
   854     *e = 32 - k;
       
   855 #ifdef Pack_32
       
   856     if (k < Ebits) {
       
   857         d0 = Exp_1 | (y >> (Ebits - k));
       
   858         w = xa > xa0 ? *--xa : 0;
       
   859         d1 = (y << (32 - Ebits + k)) | (w >> (Ebits - k));
       
   860         goto returnD;
       
   861     }
       
   862     z = xa > xa0 ? *--xa : 0;
       
   863     if (k -= Ebits) {
       
   864         d0 = Exp_1 | (y << k) | (z >> (32 - k));
       
   865         y = xa > xa0 ? *--xa : 0;
       
   866         d1 = (z << k) | (y >> (32 - k));
       
   867     } else {
       
   868         d0 = Exp_1 | y;
       
   869         d1 = z;
       
   870     }
       
   871 #else
       
   872     if (k < Ebits + 16) {
       
   873         z = xa > xa0 ? *--xa : 0;
       
   874         d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
       
   875         w = xa > xa0 ? *--xa : 0;
       
   876         y = xa > xa0 ? *--xa : 0;
       
   877         d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
       
   878         goto returnD;
       
   879     }
       
   880     z = xa > xa0 ? *--xa : 0;
       
   881     w = xa > xa0 ? *--xa : 0;
       
   882     k -= Ebits + 16;
       
   883     d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
       
   884     y = xa > xa0 ? *--xa : 0;
       
   885     d1 = w << k + 16 | y << k;
       
   886 #endif
       
   887 returnD:
       
   888 #undef d0
       
   889 #undef d1
       
   890     return dval(&d);
       
   891 }
       
   892 
       
   893 static ALWAYS_INLINE void d2b(BigInt& b, U* d, int* e, int* bits)
       
   894 {
       
   895     int de, k;
       
   896     uint32_t* x;
       
   897     uint32_t y, z;
       
   898 #ifndef Sudden_Underflow
       
   899     int i;
       
   900 #endif
       
   901 #define d0 word0(d)
       
   902 #define d1 word1(d)
       
   903 
       
   904     b.sign = 0;
       
   905 #ifdef Pack_32
       
   906     b.resize(1);
       
   907 #else
       
   908     b.resize(2);
       
   909 #endif
       
   910     x = b.words();
       
   911 
       
   912     z = d0 & Frac_mask;
       
   913     d0 &= 0x7fffffff;    /* clear sign bit, which we ignore */
       
   914 #ifdef Sudden_Underflow
       
   915     de = (int)(d0 >> Exp_shift);
       
   916 #else
       
   917     if ((de = (int)(d0 >> Exp_shift)))
       
   918         z |= Exp_msk1;
       
   919 #endif
       
   920 #ifdef Pack_32
       
   921     if ((y = d1)) {
       
   922         if ((k = lo0bits(&y))) {
       
   923             x[0] = y | (z << (32 - k));
       
   924             z >>= k;
       
   925         } else
       
   926             x[0] = y;
       
   927             if (z) {
       
   928                 b.resize(2);
       
   929                 x[1] = z;
       
   930             }
       
   931 
       
   932 #ifndef Sudden_Underflow
       
   933         i = b.size();
       
   934 #endif
       
   935     } else {
       
   936         k = lo0bits(&z);
       
   937         x[0] = z;
       
   938 #ifndef Sudden_Underflow
       
   939         i = 1;
       
   940 #endif
       
   941         b.resize(1);
       
   942         k += 32;
       
   943     }
       
   944 #else
       
   945     if ((y = d1)) {
       
   946         if ((k = lo0bits(&y))) {
       
   947             if (k >= 16) {
       
   948                 x[0] = y | z << 32 - k & 0xffff;
       
   949                 x[1] = z >> k - 16 & 0xffff;
       
   950                 x[2] = z >> k;
       
   951                 i = 2;
       
   952             } else {
       
   953                 x[0] = y & 0xffff;
       
   954                 x[1] = y >> 16 | z << 16 - k & 0xffff;
       
   955                 x[2] = z >> k & 0xffff;
       
   956                 x[3] = z >> k + 16;
       
   957                 i = 3;
       
   958             }
       
   959         } else {
       
   960             x[0] = y & 0xffff;
       
   961             x[1] = y >> 16;
       
   962             x[2] = z & 0xffff;
       
   963             x[3] = z >> 16;
       
   964             i = 3;
       
   965         }
       
   966     } else {
       
   967         k = lo0bits(&z);
       
   968         if (k >= 16) {
       
   969             x[0] = z;
       
   970             i = 0;
       
   971         } else {
       
   972             x[0] = z & 0xffff;
       
   973             x[1] = z >> 16;
       
   974             i = 1;
       
   975         }
       
   976         k += 32;
       
   977     } while (!x[i])
       
   978         --i;
       
   979     b->resize(i + 1);
       
   980 #endif
       
   981 #ifndef Sudden_Underflow
       
   982     if (de) {
       
   983 #endif
       
   984         *e = de - Bias - (P - 1) + k;
       
   985         *bits = P - k;
       
   986 #ifndef Sudden_Underflow
       
   987     } else {
       
   988         *e = de - Bias - (P - 1) + 1 + k;
       
   989 #ifdef Pack_32
       
   990         *bits = (32 * i) - hi0bits(x[i - 1]);
       
   991 #else
       
   992         *bits = (i + 2) * 16 - hi0bits(x[i]);
       
   993 #endif
       
   994     }
       
   995 #endif
       
   996 }
       
   997 #undef d0
       
   998 #undef d1
       
   999 
       
  1000 static double ratio(const BigInt& a, const BigInt& b)
       
  1001 {
       
  1002     U da, db;
       
  1003     int k, ka, kb;
       
  1004 
       
  1005     dval(&da) = b2d(a, &ka);
       
  1006     dval(&db) = b2d(b, &kb);
       
  1007 #ifdef Pack_32
       
  1008     k = ka - kb + 32 * (a.size() - b.size());
       
  1009 #else
       
  1010     k = ka - kb + 16 * (a.size() - b.size());
       
  1011 #endif
       
  1012     if (k > 0)
       
  1013         word0(&da) += k * Exp_msk1;
       
  1014     else {
       
  1015         k = -k;
       
  1016         word0(&db) += k * Exp_msk1;
       
  1017     }
       
  1018     return dval(&da) / dval(&db);
       
  1019 }
       
  1020 
       
  1021 static const double tens[] = {
       
  1022         1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
       
  1023         1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
       
  1024         1e20, 1e21, 1e22
       
  1025 };
       
  1026 
       
  1027 static const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
       
  1028 static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
       
  1029 #ifdef Avoid_Underflow
       
  1030         9007199254740992. * 9007199254740992.e-256
       
  1031         /* = 2^106 * 1e-53 */
       
  1032 #else
       
  1033         1e-256
       
  1034 #endif
       
  1035 };
       
  1036 
       
  1037 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
       
  1038 /* flag unnecessarily.  It leads to a song and dance at the end of strtod. */
       
  1039 #define Scale_Bit 0x10
       
  1040 #define n_bigtens 5
       
  1041 
       
  1042 #if defined(INFNAN_CHECK)
       
  1043 
       
  1044 #ifndef NAN_WORD0
       
  1045 #define NAN_WORD0 0x7ff80000
       
  1046 #endif
       
  1047 
       
  1048 #ifndef NAN_WORD1
       
  1049 #define NAN_WORD1 0
       
  1050 #endif
       
  1051 
       
  1052 static int match(const char** sp, const char* t)
       
  1053 {
       
  1054     int c, d;
       
  1055     const char* s = *sp;
       
  1056 
       
  1057     while ((d = *t++)) {
       
  1058         if ((c = *++s) >= 'A' && c <= 'Z')
       
  1059             c += 'a' - 'A';
       
  1060         if (c != d)
       
  1061             return 0;
       
  1062     }
       
  1063     *sp = s + 1;
       
  1064     return 1;
       
  1065 }
       
  1066 
       
  1067 #ifndef No_Hex_NaN
       
  1068 static void hexnan(U* rvp, const char** sp)
       
  1069 {
       
  1070     uint32_t c, x[2];
       
  1071     const char* s;
       
  1072     int havedig, udx0, xshift;
       
  1073 
       
  1074     x[0] = x[1] = 0;
       
  1075     havedig = xshift = 0;
       
  1076     udx0 = 1;
       
  1077     s = *sp;
       
  1078     while ((c = *(const unsigned char*)++s)) {
       
  1079         if (c >= '0' && c <= '9')
       
  1080             c -= '0';
       
  1081         else if (c >= 'a' && c <= 'f')
       
  1082             c += 10 - 'a';
       
  1083         else if (c >= 'A' && c <= 'F')
       
  1084             c += 10 - 'A';
       
  1085         else if (c <= ' ') {
       
  1086             if (udx0 && havedig) {
       
  1087                 udx0 = 0;
       
  1088                 xshift = 1;
       
  1089             }
       
  1090             continue;
       
  1091         } else if (/*(*/ c == ')' && havedig) {
       
  1092             *sp = s + 1;
       
  1093             break;
       
  1094         } else
       
  1095             return;    /* invalid form: don't change *sp */
       
  1096         havedig = 1;
       
  1097         if (xshift) {
       
  1098             xshift = 0;
       
  1099             x[0] = x[1];
       
  1100             x[1] = 0;
       
  1101         }
       
  1102         if (udx0)
       
  1103             x[0] = (x[0] << 4) | (x[1] >> 28);
       
  1104         x[1] = (x[1] << 4) | c;
       
  1105     }
       
  1106     if ((x[0] &= 0xfffff) || x[1]) {
       
  1107         word0(rvp) = Exp_mask | x[0];
       
  1108         word1(rvp) = x[1];
       
  1109     }
       
  1110 }
       
  1111 #endif /*No_Hex_NaN*/
       
  1112 #endif /* INFNAN_CHECK */
       
  1113 
       
  1114 double strtod(const char* s00, char** se)
       
  1115 {
       
  1116 #ifdef Avoid_Underflow
       
  1117     int scale;
       
  1118 #endif
       
  1119     int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
       
  1120          e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
       
  1121     const char *s, *s0, *s1;
       
  1122     double aadj, aadj1;
       
  1123     U aadj2, adj, rv, rv0;
       
  1124     int32_t L;
       
  1125     uint32_t y, z;
       
  1126     BigInt bb, bb1, bd, bd0, bs, delta;
       
  1127 #ifdef SET_INEXACT
       
  1128     int inexact, oldinexact;
       
  1129 #endif
       
  1130 
       
  1131     sign = nz0 = nz = 0;
       
  1132     dval(&rv) = 0;
       
  1133     for (s = s00; ; s++) {
       
  1134         switch (*s) {
       
  1135         case '-':
       
  1136             sign = 1;
       
  1137             /* no break */
       
  1138         case '+':
       
  1139             if (*++s)
       
  1140                 goto break2;
       
  1141             /* no break */
       
  1142         case 0:
       
  1143             goto ret0;
       
  1144         case '\t':
       
  1145         case '\n':
       
  1146         case '\v':
       
  1147         case '\f':
       
  1148         case '\r':
       
  1149         case ' ':
       
  1150             continue;
       
  1151         default:
       
  1152             goto break2;
       
  1153         }
       
  1154     }
       
  1155 break2:
       
  1156     if (*s == '0') {
       
  1157         nz0 = 1;
       
  1158         while (*++s == '0') { }
       
  1159         if (!*s)
       
  1160             goto ret;
       
  1161     }
       
  1162     s0 = s;
       
  1163     y = z = 0;
       
  1164     for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
       
  1165         if (nd < 9)
       
  1166             y = (10 * y) + c - '0';
       
  1167         else if (nd < 16)
       
  1168             z = (10 * z) + c - '0';
       
  1169     nd0 = nd;
       
  1170     if (c == '.') {
       
  1171         c = *++s;
       
  1172         if (!nd) {
       
  1173             for (; c == '0'; c = *++s)
       
  1174                 nz++;
       
  1175             if (c > '0' && c <= '9') {
       
  1176                 s0 = s;
       
  1177                 nf += nz;
       
  1178                 nz = 0;
       
  1179                 goto haveDig;
       
  1180             }
       
  1181             goto digDone;
       
  1182         }
       
  1183         for (; c >= '0' && c <= '9'; c = *++s) {
       
  1184 haveDig:
       
  1185             nz++;
       
  1186             if (c -= '0') {
       
  1187                 nf += nz;
       
  1188                 for (i = 1; i < nz; i++)
       
  1189                     if (nd++ < 9)
       
  1190                         y *= 10;
       
  1191                     else if (nd <= DBL_DIG + 1)
       
  1192                         z *= 10;
       
  1193                 if (nd++ < 9)
       
  1194                     y = (10 * y) + c;
       
  1195                 else if (nd <= DBL_DIG + 1)
       
  1196                     z = (10 * z) + c;
       
  1197                 nz = 0;
       
  1198             }
       
  1199         }
       
  1200     }
       
  1201 digDone:
       
  1202     e = 0;
       
  1203     if (c == 'e' || c == 'E') {
       
  1204         if (!nd && !nz && !nz0)
       
  1205             goto ret0;
       
  1206         s00 = s;
       
  1207         esign = 0;
       
  1208         switch (c = *++s) {
       
  1209         case '-':
       
  1210             esign = 1;
       
  1211         case '+':
       
  1212             c = *++s;
       
  1213         }
       
  1214         if (c >= '0' && c <= '9') {
       
  1215             while (c == '0')
       
  1216                 c = *++s;
       
  1217             if (c > '0' && c <= '9') {
       
  1218                 L = c - '0';
       
  1219                 s1 = s;
       
  1220                 while ((c = *++s) >= '0' && c <= '9')
       
  1221                     L = (10 * L) + c - '0';
       
  1222                 if (s - s1 > 8 || L > 19999)
       
  1223                     /* Avoid confusion from exponents
       
  1224                      * so large that e might overflow.
       
  1225                      */
       
  1226                     e = 19999; /* safe for 16 bit ints */
       
  1227                 else
       
  1228                     e = (int)L;
       
  1229                 if (esign)
       
  1230                     e = -e;
       
  1231             } else
       
  1232                 e = 0;
       
  1233         } else
       
  1234             s = s00;
       
  1235     }
       
  1236     if (!nd) {
       
  1237         if (!nz && !nz0) {
       
  1238 #ifdef INFNAN_CHECK
       
  1239             /* Check for Nan and Infinity */
       
  1240             switch (c) {
       
  1241             case 'i':
       
  1242             case 'I':
       
  1243                 if (match(&s, "nf")) {
       
  1244                     --s;
       
  1245                     if (!match(&s, "inity"))
       
  1246                         ++s;
       
  1247                     word0(&rv) = 0x7ff00000;
       
  1248                     word1(&rv) = 0;
       
  1249                     goto ret;
       
  1250                 }
       
  1251                 break;
       
  1252             case 'n':
       
  1253             case 'N':
       
  1254                 if (match(&s, "an")) {
       
  1255                     word0(&rv) = NAN_WORD0;
       
  1256                     word1(&rv) = NAN_WORD1;
       
  1257 #ifndef No_Hex_NaN
       
  1258                     if (*s == '(') /*)*/
       
  1259                         hexnan(&rv, &s);
       
  1260 #endif
       
  1261                     goto ret;
       
  1262                 }
       
  1263             }
       
  1264 #endif /* INFNAN_CHECK */
       
  1265 ret0:
       
  1266             s = s00;
       
  1267             sign = 0;
       
  1268         }
       
  1269         goto ret;
       
  1270     }
       
  1271     e1 = e -= nf;
       
  1272 
       
  1273     /* Now we have nd0 digits, starting at s0, followed by a
       
  1274      * decimal point, followed by nd-nd0 digits.  The number we're
       
  1275      * after is the integer represented by those digits times
       
  1276      * 10**e */
       
  1277 
       
  1278     if (!nd0)
       
  1279         nd0 = nd;
       
  1280     k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
       
  1281     dval(&rv) = y;
       
  1282     if (k > 9) {
       
  1283 #ifdef SET_INEXACT
       
  1284         if (k > DBL_DIG)
       
  1285             oldinexact = get_inexact();
       
  1286 #endif
       
  1287         dval(&rv) = tens[k - 9] * dval(&rv) + z;
       
  1288     }
       
  1289     if (nd <= DBL_DIG && Flt_Rounds == 1) {
       
  1290         if (!e)
       
  1291             goto ret;
       
  1292         if (e > 0) {
       
  1293             if (e <= Ten_pmax) {
       
  1294                 /* rv = */ rounded_product(dval(&rv), tens[e]);
       
  1295                 goto ret;
       
  1296             }
       
  1297             i = DBL_DIG - nd;
       
  1298             if (e <= Ten_pmax + i) {
       
  1299                 /* A fancier test would sometimes let us do
       
  1300                  * this for larger i values.
       
  1301                  */
       
  1302                 e -= i;
       
  1303                 dval(&rv) *= tens[i];
       
  1304                 /* rv = */ rounded_product(dval(&rv), tens[e]);
       
  1305                 goto ret;
       
  1306             }
       
  1307         }
       
  1308 #ifndef Inaccurate_Divide
       
  1309         else if (e >= -Ten_pmax) {
       
  1310             /* rv = */ rounded_quotient(dval(&rv), tens[-e]);
       
  1311             goto ret;
       
  1312         }
       
  1313 #endif
       
  1314     }
       
  1315     e1 += nd - k;
       
  1316 
       
  1317 #ifdef SET_INEXACT
       
  1318     inexact = 1;
       
  1319     if (k <= DBL_DIG)
       
  1320         oldinexact = get_inexact();
       
  1321 #endif
       
  1322 #ifdef Avoid_Underflow
       
  1323     scale = 0;
       
  1324 #endif
       
  1325 
       
  1326     /* Get starting approximation = rv * 10**e1 */
       
  1327 
       
  1328     if (e1 > 0) {
       
  1329         if ((i = e1 & 15))
       
  1330             dval(&rv) *= tens[i];
       
  1331         if (e1 &= ~15) {
       
  1332             if (e1 > DBL_MAX_10_EXP) {
       
  1333 ovfl:
       
  1334 #ifndef NO_ERRNO
       
  1335                 errno = ERANGE;
       
  1336 #endif
       
  1337                 /* Can't trust HUGE_VAL */
       
  1338                 word0(&rv) = Exp_mask;
       
  1339                 word1(&rv) = 0;
       
  1340 #ifdef SET_INEXACT
       
  1341                 /* set overflow bit */
       
  1342                 dval(&rv0) = 1e300;
       
  1343                 dval(&rv0) *= dval(&rv0);
       
  1344 #endif
       
  1345                 goto ret;
       
  1346             }
       
  1347             e1 >>= 4;
       
  1348             for (j = 0; e1 > 1; j++, e1 >>= 1)
       
  1349                 if (e1 & 1)
       
  1350                     dval(&rv) *= bigtens[j];
       
  1351         /* The last multiplication could overflow. */
       
  1352             word0(&rv) -= P * Exp_msk1;
       
  1353             dval(&rv) *= bigtens[j];
       
  1354             if ((z = word0(&rv) & Exp_mask) > Exp_msk1 * (DBL_MAX_EXP + Bias - P))
       
  1355                 goto ovfl;
       
  1356             if (z > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P)) {
       
  1357                 /* set to largest number */
       
  1358                 /* (Can't trust DBL_MAX) */
       
  1359                 word0(&rv) = Big0;
       
  1360                 word1(&rv) = Big1;
       
  1361             } else
       
  1362                 word0(&rv) += P * Exp_msk1;
       
  1363         }
       
  1364     } else if (e1 < 0) {
       
  1365         e1 = -e1;
       
  1366         if ((i = e1 & 15))
       
  1367             dval(&rv) /= tens[i];
       
  1368         if (e1 >>= 4) {
       
  1369             if (e1 >= 1 << n_bigtens)
       
  1370                 goto undfl;
       
  1371 #ifdef Avoid_Underflow
       
  1372             if (e1 & Scale_Bit)
       
  1373                 scale = 2 * P;
       
  1374             for (j = 0; e1 > 0; j++, e1 >>= 1)
       
  1375                 if (e1 & 1)
       
  1376                     dval(&rv) *= tinytens[j];
       
  1377             if (scale && (j = (2 * P) + 1 - ((word0(&rv) & Exp_mask) >> Exp_shift)) > 0) {
       
  1378                 /* scaled rv is denormal; zap j low bits */
       
  1379                 if (j >= 32) {
       
  1380                     word1(&rv) = 0;
       
  1381                     if (j >= 53)
       
  1382                        word0(&rv) = (P + 2) * Exp_msk1;
       
  1383                     else
       
  1384                        word0(&rv) &= 0xffffffff << (j - 32);
       
  1385                 } else
       
  1386                     word1(&rv) &= 0xffffffff << j;
       
  1387             }
       
  1388 #else
       
  1389             for (j = 0; e1 > 1; j++, e1 >>= 1)
       
  1390                 if (e1 & 1)
       
  1391                     dval(&rv) *= tinytens[j];
       
  1392             /* The last multiplication could underflow. */
       
  1393             dval(&rv0) = dval(&rv);
       
  1394             dval(&rv) *= tinytens[j];
       
  1395             if (!dval(&rv)) {
       
  1396                 dval(&rv) = 2. * dval(&rv0);
       
  1397                 dval(&rv) *= tinytens[j];
       
  1398 #endif
       
  1399                 if (!dval(&rv)) {
       
  1400 undfl:
       
  1401                     dval(&rv) = 0.;
       
  1402 #ifndef NO_ERRNO
       
  1403                     errno = ERANGE;
       
  1404 #endif
       
  1405                     goto ret;
       
  1406                 }
       
  1407 #ifndef Avoid_Underflow
       
  1408                 word0(&rv) = Tiny0;
       
  1409                 word1(&rv) = Tiny1;
       
  1410                 /* The refinement below will clean
       
  1411                  * this approximation up.
       
  1412                  */
       
  1413             }
       
  1414 #endif
       
  1415         }
       
  1416     }
       
  1417 
       
  1418     /* Now the hard part -- adjusting rv to the correct value.*/
       
  1419 
       
  1420     /* Put digits into bd: true value = bd * 10^e */
       
  1421 
       
  1422     s2b(bd0, s0, nd0, nd, y);
       
  1423 
       
  1424     for (;;) {
       
  1425         bd = bd0;
       
  1426         d2b(bb, &rv, &bbe, &bbbits);    /* rv = bb * 2^bbe */
       
  1427         i2b(bs, 1);
       
  1428 
       
  1429         if (e >= 0) {
       
  1430             bb2 = bb5 = 0;
       
  1431             bd2 = bd5 = e;
       
  1432         } else {
       
  1433             bb2 = bb5 = -e;
       
  1434             bd2 = bd5 = 0;
       
  1435         }
       
  1436         if (bbe >= 0)
       
  1437             bb2 += bbe;
       
  1438         else
       
  1439             bd2 -= bbe;
       
  1440         bs2 = bb2;
       
  1441 #ifdef Avoid_Underflow
       
  1442         j = bbe - scale;
       
  1443         i = j + bbbits - 1;    /* logb(rv) */
       
  1444         if (i < Emin)    /* denormal */
       
  1445             j += P - Emin;
       
  1446         else
       
  1447             j = P + 1 - bbbits;
       
  1448 #else /*Avoid_Underflow*/
       
  1449 #ifdef Sudden_Underflow
       
  1450         j = P + 1 - bbbits;
       
  1451 #else /*Sudden_Underflow*/
       
  1452         j = bbe;
       
  1453         i = j + bbbits - 1;    /* logb(rv) */
       
  1454         if (i < Emin)    /* denormal */
       
  1455             j += P - Emin;
       
  1456         else
       
  1457             j = P + 1 - bbbits;
       
  1458 #endif /*Sudden_Underflow*/
       
  1459 #endif /*Avoid_Underflow*/
       
  1460         bb2 += j;
       
  1461         bd2 += j;
       
  1462 #ifdef Avoid_Underflow
       
  1463         bd2 += scale;
       
  1464 #endif
       
  1465         i = bb2 < bd2 ? bb2 : bd2;
       
  1466         if (i > bs2)
       
  1467             i = bs2;
       
  1468         if (i > 0) {
       
  1469             bb2 -= i;
       
  1470             bd2 -= i;
       
  1471             bs2 -= i;
       
  1472         }
       
  1473         if (bb5 > 0) {
       
  1474             pow5mult(bs, bb5);
       
  1475             mult(bb, bs);
       
  1476         }
       
  1477         if (bb2 > 0)
       
  1478             lshift(bb, bb2);
       
  1479         if (bd5 > 0)
       
  1480             pow5mult(bd, bd5);
       
  1481         if (bd2 > 0)
       
  1482             lshift(bd, bd2);
       
  1483         if (bs2 > 0)
       
  1484             lshift(bs, bs2);
       
  1485         diff(delta, bb, bd);
       
  1486         dsign = delta.sign;
       
  1487         delta.sign = 0;
       
  1488         i = cmp(delta, bs);
       
  1489 
       
  1490         if (i < 0) {
       
  1491             /* Error is less than half an ulp -- check for
       
  1492              * special case of mantissa a power of two.
       
  1493              */
       
  1494             if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
       
  1495 #ifdef Avoid_Underflow
       
  1496              || (word0(&rv) & Exp_mask) <= (2 * P + 1) * Exp_msk1
       
  1497 #else
       
  1498              || (word0(&rv) & Exp_mask) <= Exp_msk1
       
  1499 #endif
       
  1500                 ) {
       
  1501 #ifdef SET_INEXACT
       
  1502                 if (!delta->words()[0] && delta->size() <= 1)
       
  1503                     inexact = 0;
       
  1504 #endif
       
  1505                 break;
       
  1506             }
       
  1507             if (!delta.words()[0] && delta.size() <= 1) {
       
  1508                 /* exact result */
       
  1509 #ifdef SET_INEXACT
       
  1510                 inexact = 0;
       
  1511 #endif
       
  1512                 break;
       
  1513             }
       
  1514             lshift(delta, Log2P);
       
  1515             if (cmp(delta, bs) > 0)
       
  1516                 goto dropDown;
       
  1517             break;
       
  1518         }
       
  1519         if (!i) {
       
  1520             /* exactly half-way between */
       
  1521             if (dsign) {
       
  1522                 if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
       
  1523                  &&  word1(&rv) == (
       
  1524 #ifdef Avoid_Underflow
       
  1525             (scale && (y = word0(&rv) & Exp_mask) <= 2 * P * Exp_msk1)
       
  1526         ? (0xffffffff & (0xffffffff << (2 * P + 1 - (y >> Exp_shift)))) :
       
  1527 #endif
       
  1528                            0xffffffff)) {
       
  1529                     /*boundary case -- increment exponent*/
       
  1530                     word0(&rv) = (word0(&rv) & Exp_mask) + Exp_msk1;
       
  1531                     word1(&rv) = 0;
       
  1532 #ifdef Avoid_Underflow
       
  1533                     dsign = 0;
       
  1534 #endif
       
  1535                     break;
       
  1536                 }
       
  1537             } else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
       
  1538 dropDown:
       
  1539                 /* boundary case -- decrement exponent */
       
  1540 #ifdef Sudden_Underflow /*{{*/
       
  1541                 L = word0(&rv) & Exp_mask;
       
  1542 #ifdef Avoid_Underflow
       
  1543                 if (L <= (scale ? (2 * P + 1) * Exp_msk1 : Exp_msk1))
       
  1544 #else
       
  1545                 if (L <= Exp_msk1)
       
  1546 #endif /*Avoid_Underflow*/
       
  1547                     goto undfl;
       
  1548                 L -= Exp_msk1;
       
  1549 #else /*Sudden_Underflow}{*/
       
  1550 #ifdef Avoid_Underflow
       
  1551                 if (scale) {
       
  1552                     L = word0(&rv) & Exp_mask;
       
  1553                     if (L <= (2 * P + 1) * Exp_msk1) {
       
  1554                         if (L > (P + 2) * Exp_msk1)
       
  1555                             /* round even ==> */
       
  1556                             /* accept rv */
       
  1557                             break;
       
  1558                         /* rv = smallest denormal */
       
  1559                         goto undfl;
       
  1560                     }
       
  1561                 }
       
  1562 #endif /*Avoid_Underflow*/
       
  1563                 L = (word0(&rv) & Exp_mask) - Exp_msk1;
       
  1564 #endif /*Sudden_Underflow}}*/
       
  1565                 word0(&rv) = L | Bndry_mask1;
       
  1566                 word1(&rv) = 0xffffffff;
       
  1567                 break;
       
  1568             }
       
  1569             if (!(word1(&rv) & LSB))
       
  1570                 break;
       
  1571             if (dsign)
       
  1572                 dval(&rv) += ulp(&rv);
       
  1573             else {
       
  1574                 dval(&rv) -= ulp(&rv);
       
  1575 #ifndef Sudden_Underflow
       
  1576                 if (!dval(&rv))
       
  1577                     goto undfl;
       
  1578 #endif
       
  1579             }
       
  1580 #ifdef Avoid_Underflow
       
  1581             dsign = 1 - dsign;
       
  1582 #endif
       
  1583             break;
       
  1584         }
       
  1585         if ((aadj = ratio(delta, bs)) <= 2.) {
       
  1586             if (dsign)
       
  1587                 aadj = aadj1 = 1.;
       
  1588             else if (word1(&rv) || word0(&rv) & Bndry_mask) {
       
  1589 #ifndef Sudden_Underflow
       
  1590                 if (word1(&rv) == Tiny1 && !word0(&rv))
       
  1591                     goto undfl;
       
  1592 #endif
       
  1593                 aadj = 1.;
       
  1594                 aadj1 = -1.;
       
  1595             } else {
       
  1596                 /* special case -- power of FLT_RADIX to be */
       
  1597                 /* rounded down... */
       
  1598 
       
  1599                 if (aadj < 2. / FLT_RADIX)
       
  1600                     aadj = 1. / FLT_RADIX;
       
  1601                 else
       
  1602                     aadj *= 0.5;
       
  1603                 aadj1 = -aadj;
       
  1604             }
       
  1605         } else {
       
  1606             aadj *= 0.5;
       
  1607             aadj1 = dsign ? aadj : -aadj;
       
  1608 #ifdef Check_FLT_ROUNDS
       
  1609             switch (Rounding) {
       
  1610             case 2: /* towards +infinity */
       
  1611                 aadj1 -= 0.5;
       
  1612                 break;
       
  1613             case 0: /* towards 0 */
       
  1614             case 3: /* towards -infinity */
       
  1615                 aadj1 += 0.5;
       
  1616             }
       
  1617 #else
       
  1618             if (!Flt_Rounds)
       
  1619                 aadj1 += 0.5;
       
  1620 #endif /*Check_FLT_ROUNDS*/
       
  1621         }
       
  1622         y = word0(&rv) & Exp_mask;
       
  1623 
       
  1624         /* Check for overflow */
       
  1625 
       
  1626         if (y == Exp_msk1 * (DBL_MAX_EXP + Bias - 1)) {
       
  1627             dval(&rv0) = dval(&rv);
       
  1628             word0(&rv) -= P * Exp_msk1;
       
  1629             adj.d = aadj1 * ulp(&rv);
       
  1630             dval(&rv) += adj.d;
       
  1631             if ((word0(&rv) & Exp_mask) >= Exp_msk1 * (DBL_MAX_EXP + Bias - P)) {
       
  1632                 if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
       
  1633                     goto ovfl;
       
  1634                 word0(&rv) = Big0;
       
  1635                 word1(&rv) = Big1;
       
  1636                 goto cont;
       
  1637             }
       
  1638             word0(&rv) += P * Exp_msk1;
       
  1639         } else {
       
  1640 #ifdef Avoid_Underflow
       
  1641             if (scale && y <= 2 * P * Exp_msk1) {
       
  1642                 if (aadj <= 0x7fffffff) {
       
  1643                     if ((z = (uint32_t)aadj) <= 0)
       
  1644                         z = 1;
       
  1645                     aadj = z;
       
  1646                     aadj1 = dsign ? aadj : -aadj;
       
  1647                 }
       
  1648                 dval(&aadj2) = aadj1;
       
  1649                 word0(&aadj2) += (2 * P + 1) * Exp_msk1 - y;
       
  1650                 aadj1 = dval(&aadj2);
       
  1651             }
       
  1652             adj.d = aadj1 * ulp(&rv);
       
  1653             dval(&rv) += adj.d;
       
  1654 #else
       
  1655 #ifdef Sudden_Underflow
       
  1656             if ((word0(&rv) & Exp_mask) <= P * Exp_msk1) {
       
  1657                 dval(&rv0) = dval(&rv);
       
  1658                 word0(&rv) += P * Exp_msk1;
       
  1659                 adj.d = aadj1 * ulp(&rv);
       
  1660                 dval(&rv) += adj.d;
       
  1661                 if ((word0(&rv) & Exp_mask) <= P * Exp_msk1) {
       
  1662                     if (word0(&rv0) == Tiny0 && word1(&rv0) == Tiny1)
       
  1663                         goto undfl;
       
  1664                     word0(&rv) = Tiny0;
       
  1665                     word1(&rv) = Tiny1;
       
  1666                     goto cont;
       
  1667                 }
       
  1668                 word0(&rv) -= P * Exp_msk1;
       
  1669             } else {
       
  1670                 adj.d = aadj1 * ulp(&rv);
       
  1671                 dval(&rv) += adj.d;
       
  1672             }
       
  1673 #else /*Sudden_Underflow*/
       
  1674             /* Compute adj so that the IEEE rounding rules will
       
  1675              * correctly round rv + adj in some half-way cases.
       
  1676              * If rv * ulp(rv) is denormalized (i.e.,
       
  1677              * y <= (P - 1) * Exp_msk1), we must adjust aadj to avoid
       
  1678              * trouble from bits lost to denormalization;
       
  1679              * example: 1.2e-307 .
       
  1680              */
       
  1681             if (y <= (P - 1) * Exp_msk1 && aadj > 1.) {
       
  1682                 aadj1 = (double)(int)(aadj + 0.5);
       
  1683                 if (!dsign)
       
  1684                     aadj1 = -aadj1;
       
  1685             }
       
  1686             adj.d = aadj1 * ulp(&rv);
       
  1687             dval(&rv) += adj.d;
       
  1688 #endif /*Sudden_Underflow*/
       
  1689 #endif /*Avoid_Underflow*/
       
  1690         }
       
  1691         z = word0(&rv) & Exp_mask;
       
  1692 #ifndef SET_INEXACT
       
  1693 #ifdef Avoid_Underflow
       
  1694         if (!scale)
       
  1695 #endif
       
  1696         if (y == z) {
       
  1697             /* Can we stop now? */
       
  1698             L = (int32_t)aadj;
       
  1699             aadj -= L;
       
  1700             /* The tolerances below are conservative. */
       
  1701             if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
       
  1702                 if (aadj < .4999999 || aadj > .5000001)
       
  1703                     break;
       
  1704             } else if (aadj < .4999999 / FLT_RADIX)
       
  1705                 break;
       
  1706         }
       
  1707 #endif
       
  1708 cont:
       
  1709         {}
       
  1710     }
       
  1711 #ifdef SET_INEXACT
       
  1712     if (inexact) {
       
  1713         if (!oldinexact) {
       
  1714             word0(&rv0) = Exp_1 + (70 << Exp_shift);
       
  1715             word1(&rv0) = 0;
       
  1716             dval(&rv0) += 1.;
       
  1717         }
       
  1718     } else if (!oldinexact)
       
  1719         clear_inexact();
       
  1720 #endif
       
  1721 #ifdef Avoid_Underflow
       
  1722     if (scale) {
       
  1723         word0(&rv0) = Exp_1 - 2 * P * Exp_msk1;
       
  1724         word1(&rv0) = 0;
       
  1725         dval(&rv) *= dval(&rv0);
       
  1726 #ifndef NO_ERRNO
       
  1727         /* try to avoid the bug of testing an 8087 register value */
       
  1728         if (!word0(&rv) && !word1(&rv))
       
  1729             errno = ERANGE;
       
  1730 #endif
       
  1731     }
       
  1732 #endif /* Avoid_Underflow */
       
  1733 #ifdef SET_INEXACT
       
  1734     if (inexact && !(word0(&rv) & Exp_mask)) {
       
  1735         /* set underflow bit */
       
  1736         dval(&rv0) = 1e-300;
       
  1737         dval(&rv0) *= dval(&rv0);
       
  1738     }
       
  1739 #endif
       
  1740 ret:
       
  1741     if (se)
       
  1742         *se = const_cast<char*>(s);
       
  1743     return sign ? -dval(&rv) : dval(&rv);
       
  1744 }
       
  1745 
       
  1746 static ALWAYS_INLINE int quorem(BigInt& b, BigInt& S)
       
  1747 {
       
  1748     size_t n;
       
  1749     uint32_t* bx;
       
  1750     uint32_t* bxe;
       
  1751     uint32_t q;
       
  1752     uint32_t* sx;
       
  1753     uint32_t* sxe;
       
  1754 #ifdef USE_LONG_LONG
       
  1755     unsigned long long borrow, carry, y, ys;
       
  1756 #else
       
  1757     uint32_t borrow, carry, y, ys;
       
  1758 #ifdef Pack_32
       
  1759     uint32_t si, z, zs;
       
  1760 #endif
       
  1761 #endif
       
  1762     ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
       
  1763     ASSERT(S.size() <= 1 || S.words()[S.size() - 1]);
       
  1764 
       
  1765     n = S.size();
       
  1766     ASSERT_WITH_MESSAGE(b.size() <= n, "oversize b in quorem");
       
  1767     if (b.size() < n)
       
  1768         return 0;
       
  1769     sx = S.words();
       
  1770     sxe = sx + --n;
       
  1771     bx = b.words();
       
  1772     bxe = bx + n;
       
  1773     q = *bxe / (*sxe + 1);    /* ensure q <= true quotient */
       
  1774     ASSERT_WITH_MESSAGE(q <= 9, "oversized quotient in quorem");
       
  1775     if (q) {
       
  1776         borrow = 0;
       
  1777         carry = 0;
       
  1778         do {
       
  1779 #ifdef USE_LONG_LONG
       
  1780             ys = *sx++ * (unsigned long long)q + carry;
       
  1781             carry = ys >> 32;
       
  1782             y = *bx - (ys & 0xffffffffUL) - borrow;
       
  1783             borrow = y >> 32 & (uint32_t)1;
       
  1784             *bx++ = (uint32_t)y & 0xffffffffUL;
       
  1785 #else
       
  1786 #ifdef Pack_32
       
  1787             si = *sx++;
       
  1788             ys = (si & 0xffff) * q + carry;
       
  1789             zs = (si >> 16) * q + (ys >> 16);
       
  1790             carry = zs >> 16;
       
  1791             y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
       
  1792             borrow = (y & 0x10000) >> 16;
       
  1793             z = (*bx >> 16) - (zs & 0xffff) - borrow;
       
  1794             borrow = (z & 0x10000) >> 16;
       
  1795             bx = storeInc(bx, z, y);
       
  1796 #else
       
  1797             ys = *sx++ * q + carry;
       
  1798             carry = ys >> 16;
       
  1799             y = *bx - (ys & 0xffff) - borrow;
       
  1800             borrow = (y & 0x10000) >> 16;
       
  1801             *bx++ = y & 0xffff;
       
  1802 #endif
       
  1803 #endif
       
  1804         } while (sx <= sxe);
       
  1805         if (!*bxe) {
       
  1806             bx = b.words();
       
  1807             while (--bxe > bx && !*bxe)
       
  1808                 --n;
       
  1809             b.resize(n);
       
  1810         }
       
  1811     }
       
  1812     if (cmp(b, S) >= 0) {
       
  1813         q++;
       
  1814         borrow = 0;
       
  1815         carry = 0;
       
  1816         bx = b.words();
       
  1817         sx = S.words();
       
  1818         do {
       
  1819 #ifdef USE_LONG_LONG
       
  1820             ys = *sx++ + carry;
       
  1821             carry = ys >> 32;
       
  1822             y = *bx - (ys & 0xffffffffUL) - borrow;
       
  1823             borrow = y >> 32 & (uint32_t)1;
       
  1824             *bx++ = (uint32_t)y & 0xffffffffUL;
       
  1825 #else
       
  1826 #ifdef Pack_32
       
  1827             si = *sx++;
       
  1828             ys = (si & 0xffff) + carry;
       
  1829             zs = (si >> 16) + (ys >> 16);
       
  1830             carry = zs >> 16;
       
  1831             y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
       
  1832             borrow = (y & 0x10000) >> 16;
       
  1833             z = (*bx >> 16) - (zs & 0xffff) - borrow;
       
  1834             borrow = (z & 0x10000) >> 16;
       
  1835             bx = storeInc(bx, z, y);
       
  1836 #else
       
  1837             ys = *sx++ + carry;
       
  1838             carry = ys >> 16;
       
  1839             y = *bx - (ys & 0xffff) - borrow;
       
  1840             borrow = (y & 0x10000) >> 16;
       
  1841             *bx++ = y & 0xffff;
       
  1842 #endif
       
  1843 #endif
       
  1844         } while (sx <= sxe);
       
  1845         bx = b.words();
       
  1846         bxe = bx + n;
       
  1847         if (!*bxe) {
       
  1848             while (--bxe > bx && !*bxe)
       
  1849                 --n;
       
  1850             b.resize(n);
       
  1851         }
       
  1852     }
       
  1853     return q;
       
  1854 }
       
  1855 
       
  1856 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
       
  1857  *
       
  1858  * Inspired by "How to Print Floating-Point Numbers Accurately" by
       
  1859  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
       
  1860  *
       
  1861  * Modifications:
       
  1862  *    1. Rather than iterating, we use a simple numeric overestimate
       
  1863  *       to determine k = floor(log10(d)).  We scale relevant
       
  1864  *       quantities using O(log2(k)) rather than O(k) multiplications.
       
  1865  *    2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
       
  1866  *       try to generate digits strictly left to right.  Instead, we
       
  1867  *       compute with fewer bits and propagate the carry if necessary
       
  1868  *       when rounding the final digit up.  This is often faster.
       
  1869  *    3. Under the assumption that input will be rounded nearest,
       
  1870  *       mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
       
  1871  *       That is, we allow equality in stopping tests when the
       
  1872  *       round-nearest rule will give the same floating-point value
       
  1873  *       as would satisfaction of the stopping test with strict
       
  1874  *       inequality.
       
  1875  *    4. We remove common factors of powers of 2 from relevant
       
  1876  *       quantities.
       
  1877  *    5. When converting floating-point integers less than 1e16,
       
  1878  *       we use floating-point arithmetic rather than resorting
       
  1879  *       to multiple-precision integers.
       
  1880  *    6. When asked to produce fewer than 15 digits, we first try
       
  1881  *       to get by with floating-point arithmetic; we resort to
       
  1882  *       multiple-precision integer arithmetic only if we cannot
       
  1883  *       guarantee that the floating-point calculation has given
       
  1884  *       the correctly rounded result.  For k requested digits and
       
  1885  *       "uniformly" distributed input, the probability is
       
  1886  *       something like 10^(k-15) that we must resort to the int32_t
       
  1887  *       calculation.
       
  1888  */
       
  1889 
       
  1890 void dtoa(DtoaBuffer result, double dd, int ndigits, int* decpt, int* sign, char** rve)
       
  1891 {
       
  1892     /*
       
  1893         Arguments ndigits, decpt, sign are similar to those
       
  1894     of ecvt and fcvt; trailing zeros are suppressed from
       
  1895     the returned string.  If not null, *rve is set to point
       
  1896     to the end of the return value.  If d is +-Infinity or NaN,
       
  1897     then *decpt is set to 9999.
       
  1898 
       
  1899     */
       
  1900 
       
  1901     int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0,
       
  1902         j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
       
  1903         spec_case, try_quick;
       
  1904     int32_t L;
       
  1905 #ifndef Sudden_Underflow
       
  1906     int denorm;
       
  1907     uint32_t x;
       
  1908 #endif
       
  1909     BigInt b, b1, delta, mlo, mhi, S;
       
  1910     U d2, eps, u;
       
  1911     double ds;
       
  1912     char* s;
       
  1913     char* s0;
       
  1914 #ifdef SET_INEXACT
       
  1915     int inexact, oldinexact;
       
  1916 #endif
       
  1917 
       
  1918     u.d = dd;
       
  1919     if (word0(&u) & Sign_bit) {
       
  1920         /* set sign for everything, including 0's and NaNs */
       
  1921         *sign = 1;
       
  1922         word0(&u) &= ~Sign_bit;    /* clear sign bit */
       
  1923     } else
       
  1924         *sign = 0;
       
  1925 
       
  1926     if ((word0(&u) & Exp_mask) == Exp_mask) {
       
  1927         /* Infinity or NaN */
       
  1928         *decpt = 9999;
       
  1929         if (!word1(&u) && !(word0(&u) & 0xfffff)) {
       
  1930             strcpy(result, "Infinity");
       
  1931             if (rve)
       
  1932                 *rve = result + 8;
       
  1933         } else {
       
  1934             strcpy(result, "NaN");
       
  1935             if (rve)
       
  1936                 *rve = result + 3;
       
  1937         }
       
  1938         return;
       
  1939     }
       
  1940     if (!dval(&u)) {
       
  1941         *decpt = 1;
       
  1942         result[0] = '0';
       
  1943         result[1] = '\0';
       
  1944         if (rve)
       
  1945             *rve = result + 1;
       
  1946         return;
       
  1947     }
       
  1948 
       
  1949 #ifdef SET_INEXACT
       
  1950     try_quick = oldinexact = get_inexact();
       
  1951     inexact = 1;
       
  1952 #endif
       
  1953 
       
  1954     d2b(b, &u, &be, &bbits);
       
  1955 #ifdef Sudden_Underflow
       
  1956     i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1));
       
  1957 #else
       
  1958     if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1)))) {
       
  1959 #endif
       
  1960         dval(&d2) = dval(&u);
       
  1961         word0(&d2) &= Frac_mask1;
       
  1962         word0(&d2) |= Exp_11;
       
  1963 
       
  1964         /* log(x)    ~=~ log(1.5) + (x-1.5)/1.5
       
  1965          * log10(x)     =  log(x) / log(10)
       
  1966          *        ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
       
  1967          * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
       
  1968          *
       
  1969          * This suggests computing an approximation k to log10(d) by
       
  1970          *
       
  1971          * k = (i - Bias)*0.301029995663981
       
  1972          *    + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
       
  1973          *
       
  1974          * We want k to be too large rather than too small.
       
  1975          * The error in the first-order Taylor series approximation
       
  1976          * is in our favor, so we just round up the constant enough
       
  1977          * to compensate for any error in the multiplication of
       
  1978          * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
       
  1979          * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
       
  1980          * adding 1e-13 to the constant term more than suffices.
       
  1981          * Hence we adjust the constant term to 0.1760912590558.
       
  1982          * (We could get a more accurate k by invoking log10,
       
  1983          *  but this is probably not worthwhile.)
       
  1984          */
       
  1985 
       
  1986         i -= Bias;
       
  1987 #ifndef Sudden_Underflow
       
  1988         denorm = 0;
       
  1989     } else {
       
  1990         /* d is denormalized */
       
  1991 
       
  1992         i = bbits + be + (Bias + (P - 1) - 1);
       
  1993         x = (i > 32) ? (word0(&u) << (64 - i)) | (word1(&u) >> (i - 32))
       
  1994                 : word1(&u) << (32 - i);
       
  1995         dval(&d2) = x;
       
  1996         word0(&d2) -= 31 * Exp_msk1; /* adjust exponent */
       
  1997         i -= (Bias + (P - 1) - 1) + 1;
       
  1998         denorm = 1;
       
  1999     }
       
  2000 #endif
       
  2001     ds = (dval(&d2) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i * 0.301029995663981);
       
  2002     k = (int)ds;
       
  2003     if (ds < 0. && ds != k)
       
  2004         k--;    /* want k = floor(ds) */
       
  2005     k_check = 1;
       
  2006     if (k >= 0 && k <= Ten_pmax) {
       
  2007         if (dval(&u) < tens[k])
       
  2008             k--;
       
  2009         k_check = 0;
       
  2010     }
       
  2011     j = bbits - i - 1;
       
  2012     if (j >= 0) {
       
  2013         b2 = 0;
       
  2014         s2 = j;
       
  2015     } else {
       
  2016         b2 = -j;
       
  2017         s2 = 0;
       
  2018     }
       
  2019     if (k >= 0) {
       
  2020         b5 = 0;
       
  2021         s5 = k;
       
  2022         s2 += k;
       
  2023     } else {
       
  2024         b2 -= k;
       
  2025         b5 = -k;
       
  2026         s5 = 0;
       
  2027     }
       
  2028 
       
  2029 #ifndef SET_INEXACT
       
  2030 #ifdef Check_FLT_ROUNDS
       
  2031     try_quick = Rounding == 1;
       
  2032 #else
       
  2033     try_quick = 1;
       
  2034 #endif
       
  2035 #endif /*SET_INEXACT*/
       
  2036 
       
  2037     leftright = 1;
       
  2038     ilim = ilim1 = -1;
       
  2039     i = 18;
       
  2040     ndigits = 0;
       
  2041     s = s0 = result;
       
  2042 
       
  2043     if (ilim >= 0 && ilim <= Quick_max && try_quick) {
       
  2044 
       
  2045         /* Try to get by with floating-point arithmetic. */
       
  2046 
       
  2047         i = 0;
       
  2048         dval(&d2) = dval(&u);
       
  2049         k0 = k;
       
  2050         ilim0 = ilim;
       
  2051         ieps = 2; /* conservative */
       
  2052         if (k > 0) {
       
  2053             ds = tens[k & 0xf];
       
  2054             j = k >> 4;
       
  2055             if (j & Bletch) {
       
  2056                 /* prevent overflows */
       
  2057                 j &= Bletch - 1;
       
  2058                 dval(&u) /= bigtens[n_bigtens - 1];
       
  2059                 ieps++;
       
  2060             }
       
  2061             for (; j; j >>= 1, i++) {
       
  2062                 if (j & 1) {
       
  2063                     ieps++;
       
  2064                     ds *= bigtens[i];
       
  2065                 }
       
  2066             }
       
  2067             dval(&u) /= ds;
       
  2068         } else if ((j1 = -k)) {
       
  2069             dval(&u) *= tens[j1 & 0xf];
       
  2070             for (j = j1 >> 4; j; j >>= 1, i++) {
       
  2071                 if (j & 1) {
       
  2072                     ieps++;
       
  2073                     dval(&u) *= bigtens[i];
       
  2074                 }
       
  2075             }
       
  2076         }
       
  2077         if (k_check && dval(&u) < 1. && ilim > 0) {
       
  2078             if (ilim1 <= 0)
       
  2079                 goto fastFailed;
       
  2080             ilim = ilim1;
       
  2081             k--;
       
  2082             dval(&u) *= 10.;
       
  2083             ieps++;
       
  2084         }
       
  2085         dval(&eps) = (ieps * dval(&u)) + 7.;
       
  2086         word0(&eps) -= (P - 1) * Exp_msk1;
       
  2087         if (!ilim) {
       
  2088             S.clear();
       
  2089             mhi.clear();
       
  2090             dval(&u) -= 5.;
       
  2091             if (dval(&u) > dval(&eps))
       
  2092                 goto oneDigit;
       
  2093             if (dval(&u) < -dval(&eps))
       
  2094                 goto noDigits;
       
  2095             goto fastFailed;
       
  2096         }
       
  2097 #ifndef No_leftright
       
  2098         if (leftright) {
       
  2099             /* Use Steele & White method of only
       
  2100              * generating digits needed.
       
  2101              */
       
  2102             dval(&eps) = (0.5 / tens[ilim - 1]) - dval(&eps);
       
  2103             for (i = 0;;) {
       
  2104                 L = (long int)dval(&u);
       
  2105                 dval(&u) -= L;
       
  2106                 *s++ = '0' + (int)L;
       
  2107                 if (dval(&u) < dval(&eps))
       
  2108                     goto ret;
       
  2109                 if (1. - dval(&u) < dval(&eps))
       
  2110                     goto bumpUp;
       
  2111                 if (++i >= ilim)
       
  2112                     break;
       
  2113                 dval(&eps) *= 10.;
       
  2114                 dval(&u) *= 10.;
       
  2115             }
       
  2116         } else {
       
  2117 #endif
       
  2118             /* Generate ilim digits, then fix them up. */
       
  2119             dval(&eps) *= tens[ilim - 1];
       
  2120             for (i = 1;; i++, dval(&u) *= 10.) {
       
  2121                 L = (int32_t)(dval(&u));
       
  2122                 if (!(dval(&u) -= L))
       
  2123                     ilim = i;
       
  2124                 *s++ = '0' + (int)L;
       
  2125                 if (i == ilim) {
       
  2126                     if (dval(&u) > 0.5 + dval(&eps))
       
  2127                         goto bumpUp;
       
  2128                     if (dval(&u) < 0.5 - dval(&eps)) {
       
  2129                         while (*--s == '0') { }
       
  2130                         s++;
       
  2131                         goto ret;
       
  2132                     }
       
  2133                     break;
       
  2134                 }
       
  2135             }
       
  2136 #ifndef No_leftright
       
  2137         }
       
  2138 #endif
       
  2139 fastFailed:
       
  2140         s = s0;
       
  2141         dval(&u) = dval(&d2);
       
  2142         k = k0;
       
  2143         ilim = ilim0;
       
  2144     }
       
  2145 
       
  2146     /* Do we have a "small" integer? */
       
  2147 
       
  2148     if (be >= 0 && k <= Int_max) {
       
  2149         /* Yes. */
       
  2150         ds = tens[k];
       
  2151         if (ndigits < 0 && ilim <= 0) {
       
  2152             S.clear();
       
  2153             mhi.clear();
       
  2154             if (ilim < 0 || dval(&u) <= 5 * ds)
       
  2155                 goto noDigits;
       
  2156             goto oneDigit;
       
  2157         }
       
  2158         for (i = 1;; i++, dval(&u) *= 10.) {
       
  2159             L = (int32_t)(dval(&u) / ds);
       
  2160             dval(&u) -= L * ds;
       
  2161 #ifdef Check_FLT_ROUNDS
       
  2162             /* If FLT_ROUNDS == 2, L will usually be high by 1 */
       
  2163             if (dval(&u) < 0) {
       
  2164                 L--;
       
  2165                 dval(&u) += ds;
       
  2166             }
       
  2167 #endif
       
  2168             *s++ = '0' + (int)L;
       
  2169             if (!dval(&u)) {
       
  2170 #ifdef SET_INEXACT
       
  2171                 inexact = 0;
       
  2172 #endif
       
  2173                 break;
       
  2174             }
       
  2175             if (i == ilim) {
       
  2176                 dval(&u) += dval(&u);
       
  2177                 if (dval(&u) > ds || (dval(&u) == ds && (L & 1))) {
       
  2178 bumpUp:
       
  2179                     while (*--s == '9')
       
  2180                         if (s == s0) {
       
  2181                             k++;
       
  2182                             *s = '0';
       
  2183                             break;
       
  2184                         }
       
  2185                     ++*s++;
       
  2186                 }
       
  2187                 break;
       
  2188             }
       
  2189         }
       
  2190         goto ret;
       
  2191     }
       
  2192 
       
  2193     m2 = b2;
       
  2194     m5 = b5;
       
  2195     mhi.clear();
       
  2196     mlo.clear();
       
  2197     if (leftright) {
       
  2198         i =
       
  2199 #ifndef Sudden_Underflow
       
  2200             denorm ? be + (Bias + (P - 1) - 1 + 1) :
       
  2201 #endif
       
  2202             1 + P - bbits;
       
  2203         b2 += i;
       
  2204         s2 += i;
       
  2205         i2b(mhi, 1);
       
  2206     }
       
  2207     if (m2 > 0 && s2 > 0) {
       
  2208         i = m2 < s2 ? m2 : s2;
       
  2209         b2 -= i;
       
  2210         m2 -= i;
       
  2211         s2 -= i;
       
  2212     }
       
  2213     if (b5 > 0) {
       
  2214         if (leftright) {
       
  2215             if (m5 > 0) {
       
  2216                 pow5mult(mhi, m5);
       
  2217                 mult(b, mhi);
       
  2218             }
       
  2219             if ((j = b5 - m5))
       
  2220                 pow5mult(b, j);
       
  2221         } else
       
  2222             pow5mult(b, b5);
       
  2223         }
       
  2224     i2b(S, 1);
       
  2225     if (s5 > 0)
       
  2226         pow5mult(S, s5);
       
  2227 
       
  2228     /* Check for special case that d is a normalized power of 2. */
       
  2229 
       
  2230     spec_case = 0;
       
  2231     if (!word1(&u) && !(word0(&u) & Bndry_mask)
       
  2232 #ifndef Sudden_Underflow
       
  2233      && word0(&u) & (Exp_mask & ~Exp_msk1)
       
  2234 #endif
       
  2235             ) {
       
  2236         /* The special case */
       
  2237         b2 += Log2P;
       
  2238         s2 += Log2P;
       
  2239         spec_case = 1;
       
  2240     }
       
  2241 
       
  2242     /* Arrange for convenient computation of quotients:
       
  2243      * shift left if necessary so divisor has 4 leading 0 bits.
       
  2244      *
       
  2245      * Perhaps we should just compute leading 28 bits of S once
       
  2246      * and for all and pass them and a shift to quorem, so it
       
  2247      * can do shifts and ors to compute the numerator for q.
       
  2248      */
       
  2249 #ifdef Pack_32
       
  2250     if ((i = ((s5 ? 32 - hi0bits(S.words()[S.size() - 1]) : 1) + s2) & 0x1f))
       
  2251         i = 32 - i;
       
  2252 #else
       
  2253     if ((i = ((s5 ? 32 - hi0bits(S.words()[S.size() - 1]) : 1) + s2) & 0xf))
       
  2254         i = 16 - i;
       
  2255 #endif
       
  2256     if (i > 4) {
       
  2257         i -= 4;
       
  2258         b2 += i;
       
  2259         m2 += i;
       
  2260         s2 += i;
       
  2261     } else if (i < 4) {
       
  2262         i += 28;
       
  2263         b2 += i;
       
  2264         m2 += i;
       
  2265         s2 += i;
       
  2266     }
       
  2267     if (b2 > 0)
       
  2268         lshift(b, b2);
       
  2269     if (s2 > 0)
       
  2270         lshift(S, s2);
       
  2271     if (k_check) {
       
  2272         if (cmp(b, S) < 0) {
       
  2273             k--;
       
  2274             multadd(b, 10, 0);    /* we botched the k estimate */
       
  2275             if (leftright)
       
  2276                 multadd(mhi, 10, 0);
       
  2277             ilim = ilim1;
       
  2278         }
       
  2279     }
       
  2280 
       
  2281     if (leftright) {
       
  2282         if (m2 > 0)
       
  2283             lshift(mhi, m2);
       
  2284 
       
  2285         /* Compute mlo -- check for special case
       
  2286          * that d is a normalized power of 2.
       
  2287          */
       
  2288 
       
  2289         mlo = mhi;
       
  2290         if (spec_case) {
       
  2291             mhi = mlo;
       
  2292             lshift(mhi, Log2P);
       
  2293         }
       
  2294 
       
  2295         for (i = 1;;i++) {
       
  2296             dig = quorem(b, S) + '0';
       
  2297             /* Do we yet have the shortest decimal string
       
  2298              * that will round to d?
       
  2299              */
       
  2300             j = cmp(b, mlo);
       
  2301             diff(delta, S, mhi);
       
  2302             j1 = delta.sign ? 1 : cmp(b, delta);
       
  2303             if (!j1 && !(word1(&u) & 1)) {
       
  2304                 if (dig == '9')
       
  2305                     goto round9up;
       
  2306                 if (j > 0)
       
  2307                     dig++;
       
  2308 #ifdef SET_INEXACT
       
  2309                 else if (!b->x[0] && b->wds <= 1)
       
  2310                     inexact = 0;
       
  2311 #endif
       
  2312                 *s++ = dig;
       
  2313                 goto ret;
       
  2314             }
       
  2315             if (j < 0 || (!j && !(word1(&u) & 1))) {
       
  2316                 if (!b.words()[0] && b.size() <= 1) {
       
  2317 #ifdef SET_INEXACT
       
  2318                     inexact = 0;
       
  2319 #endif
       
  2320                     goto acceptDig;
       
  2321                 }
       
  2322                 if (j1 > 0) {
       
  2323                     lshift(b, 1);
       
  2324                     j1 = cmp(b, S);
       
  2325                     if ((j1 > 0 || (!j1 && (dig & 1))) && dig++ == '9')
       
  2326                         goto round9up;
       
  2327                 }
       
  2328 acceptDig:
       
  2329                 *s++ = dig;
       
  2330                 goto ret;
       
  2331             }
       
  2332             if (j1 > 0) {
       
  2333                 if (dig == '9') { /* possible if i == 1 */
       
  2334 round9up:
       
  2335                     *s++ = '9';
       
  2336                     goto roundoff;
       
  2337                 }
       
  2338                 *s++ = dig + 1;
       
  2339                 goto ret;
       
  2340             }
       
  2341             *s++ = dig;
       
  2342             if (i == ilim)
       
  2343                 break;
       
  2344             multadd(b, 10, 0);
       
  2345             multadd(mlo, 10, 0);
       
  2346             multadd(mhi, 10, 0);
       
  2347         }
       
  2348     } else
       
  2349         for (i = 1;; i++) {
       
  2350             *s++ = dig = quorem(b, S) + '0';
       
  2351             if (!b.words()[0] && b.size() <= 1) {
       
  2352 #ifdef SET_INEXACT
       
  2353                 inexact = 0;
       
  2354 #endif
       
  2355                 goto ret;
       
  2356             }
       
  2357             if (i >= ilim)
       
  2358                 break;
       
  2359             multadd(b, 10, 0);
       
  2360         }
       
  2361 
       
  2362     /* Round off last digit */
       
  2363 
       
  2364     lshift(b, 1);
       
  2365     j = cmp(b, S);
       
  2366     if (j > 0 || (!j && (dig & 1))) {
       
  2367 roundoff:
       
  2368         while (*--s == '9')
       
  2369             if (s == s0) {
       
  2370                 k++;
       
  2371                 *s++ = '1';
       
  2372                 goto ret;
       
  2373             }
       
  2374         ++*s++;
       
  2375     } else {
       
  2376         while (*--s == '0') { }
       
  2377         s++;
       
  2378     }
       
  2379     goto ret;
       
  2380 noDigits:
       
  2381     k = -1 - ndigits;
       
  2382     goto ret;
       
  2383 oneDigit:
       
  2384     *s++ = '1';
       
  2385     k++;
       
  2386     goto ret;
       
  2387 ret:
       
  2388 #ifdef SET_INEXACT
       
  2389     if (inexact) {
       
  2390         if (!oldinexact) {
       
  2391             word0(&u) = Exp_1 + (70 << Exp_shift);
       
  2392             word1(&u) = 0;
       
  2393             dval(&u) += 1.;
       
  2394         }
       
  2395     } else if (!oldinexact)
       
  2396         clear_inexact();
       
  2397 #endif
       
  2398     *s = 0;
       
  2399     *decpt = k + 1;
       
  2400     if (rve)
       
  2401         *rve = s;
       
  2402 }
       
  2403 
       
  2404 static ALWAYS_INLINE void append(char*& next, const char* src, unsigned size)
       
  2405 {
       
  2406     for (unsigned i = 0; i < size; ++i)
       
  2407         *next++ = *src++;
       
  2408 }
       
  2409 
       
  2410 void doubleToStringInJavaScriptFormat(double d, DtoaBuffer buffer, unsigned* resultLength)
       
  2411 {
       
  2412     ASSERT(buffer);
       
  2413 
       
  2414     // avoid ever printing -NaN, in JS conceptually there is only one NaN value
       
  2415     if (isnan(d)) {
       
  2416         append(buffer, "NaN", 3);
       
  2417         if (resultLength)
       
  2418             *resultLength = 3;
       
  2419         return;
       
  2420     }
       
  2421     // -0 -> "0"
       
  2422     if (!d) {
       
  2423         buffer[0] = '0';
       
  2424         if (resultLength)
       
  2425             *resultLength = 1;
       
  2426         return;
       
  2427     }
       
  2428 
       
  2429     int decimalPoint;
       
  2430     int sign;
       
  2431 
       
  2432     DtoaBuffer result;
       
  2433     char* resultEnd = 0;
       
  2434     WTF::dtoa(result, d, 0, &decimalPoint, &sign, &resultEnd);
       
  2435     int length = resultEnd - result;
       
  2436 
       
  2437     char* next = buffer;
       
  2438     if (sign)
       
  2439         *next++ = '-';
       
  2440 
       
  2441     if (decimalPoint <= 0 && decimalPoint > -6) {
       
  2442         *next++ = '0';
       
  2443         *next++ = '.';
       
  2444         for (int j = decimalPoint; j < 0; j++)
       
  2445             *next++ = '0';
       
  2446         append(next, result, length);
       
  2447     } else if (decimalPoint <= 21 && decimalPoint > 0) {
       
  2448         if (length <= decimalPoint) {
       
  2449             append(next, result, length);
       
  2450             for (int j = 0; j < decimalPoint - length; j++)
       
  2451                 *next++ = '0';
       
  2452         } else {
       
  2453             append(next, result, decimalPoint);
       
  2454             *next++ = '.';
       
  2455             append(next, result + decimalPoint, length - decimalPoint);
       
  2456         }
       
  2457     } else if (result[0] < '0' || result[0] > '9')
       
  2458         append(next, result, length);
       
  2459     else {
       
  2460         *next++ = result[0];
       
  2461         if (length > 1) {
       
  2462             *next++ = '.';
       
  2463             append(next, result + 1, length - 1);
       
  2464         }
       
  2465 
       
  2466         *next++ = 'e';
       
  2467         *next++ = (decimalPoint >= 0) ? '+' : '-';
       
  2468         // decimalPoint can't be more than 3 digits decimal given the
       
  2469         // nature of float representation
       
  2470         int exponential = decimalPoint - 1;
       
  2471         if (exponential < 0)
       
  2472             exponential = -exponential;
       
  2473         if (exponential >= 100)
       
  2474             *next++ = static_cast<char>('0' + exponential / 100);
       
  2475         if (exponential >= 10)
       
  2476             *next++ = static_cast<char>('0' + (exponential % 100) / 10);
       
  2477         *next++ = static_cast<char>('0' + exponential % 10);
       
  2478     }
       
  2479     if (resultLength)
       
  2480         *resultLength = next - buffer;
       
  2481 }
       
  2482 
       
  2483 } // namespace WTF