Merge "Better solution for Docomo 1339 bug." into tizen_2.1
[framework/web/webkit-efl.git] / Source / WTF / wtf / dtoa.cpp
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, 2010, 2012 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 David M. Gay (dmg at acm dot org,
22  * with " at " changed at "@" and " dot " changed to ".").    */
23
24 /* On a machine with IEEE extended-precision registers, it is
25  * necessary to specify double-precision (53-bit) rounding precision
26  * before invoking strtod or dtoa.  If the machine uses (the equivalent
27  * of) Intel 80x87 arithmetic, the call
28  *    _control87(PC_53, MCW_PC);
29  * does this with many compilers.  Whether this or another call is
30  * appropriate depends on the compiler; for this to work, it may be
31  * necessary to #include "float.h" or another system-dependent header
32  * file.
33  */
34
35 #include "config.h"
36 #include "dtoa.h"
37
38 #include <stdio.h>
39 #include <wtf/AlwaysInline.h>
40 #include <wtf/MathExtras.h>
41 #include <wtf/Threading.h>
42 #include <wtf/Vector.h>
43
44 #if COMPILER(MSVC)
45 #pragma warning(disable: 4244)
46 #pragma warning(disable: 4245)
47 #pragma warning(disable: 4554)
48 #endif
49
50 namespace WTF {
51
52 Mutex* s_dtoaP5Mutex;
53
54 typedef union {
55     double d;
56     uint32_t L[2];
57 } U;
58
59 #if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN)
60 #define word0(x) (x)->L[0]
61 #define word1(x) (x)->L[1]
62 #else
63 #define word0(x) (x)->L[1]
64 #define word1(x) (x)->L[0]
65 #endif
66 #define dval(x) (x)->d
67
68 /* The following definition of Storeinc is appropriate for MIPS processors.
69  * An alternative that might be better on some machines is
70  *  *p++ = high << 16 | low & 0xffff;
71  */
72 static ALWAYS_INLINE uint32_t* storeInc(uint32_t* p, uint16_t high, uint16_t low)
73 {
74     uint16_t* p16 = reinterpret_cast<uint16_t*>(p);
75 #if CPU(BIG_ENDIAN)
76     p16[0] = high;
77     p16[1] = low;
78 #else
79     p16[1] = high;
80     p16[0] = low;
81 #endif
82     return p + 1;
83 }
84
85 #define Exp_shift  20
86 #define Exp_shift1 20
87 #define Exp_msk1    0x100000
88 #define Exp_msk11   0x100000
89 #define Exp_mask  0x7ff00000
90 #define P 53
91 #define Bias 1023
92 #define Emin (-1022)
93 #define Exp_1  0x3ff00000
94 #define Exp_11 0x3ff00000
95 #define Ebits 11
96 #define Frac_mask  0xfffff
97 #define Frac_mask1 0xfffff
98 #define Ten_pmax 22
99 #define Bletch 0x10
100 #define Bndry_mask  0xfffff
101 #define Bndry_mask1 0xfffff
102 #define LSB 1
103 #define Sign_bit 0x80000000
104 #define Log2P 1
105 #define Tiny0 0
106 #define Tiny1 1
107 #define Quick_max 14
108 #define Int_max 14
109
110 #define rounded_product(a, b) a *= b
111 #define rounded_quotient(a, b) a /= b
112
113 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
114 #define Big1 0xffffffff
115
116 #if CPU(PPC64) || CPU(X86_64)
117 // FIXME: should we enable this on all 64-bit CPUs?
118 // 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware.
119 #define USE_LONG_LONG
120 #endif
121
122 struct BigInt {
123     BigInt() : sign(0) { }
124     int sign;
125
126     void clear()
127     {
128         sign = 0;
129         m_words.clear();
130     }
131
132     size_t size() const
133     {
134         return m_words.size();
135     }
136
137     void resize(size_t s)
138     {
139         m_words.resize(s);
140     }
141
142     uint32_t* words()
143     {
144         return m_words.data();
145     }
146
147     const uint32_t* words() const
148     {
149         return m_words.data();
150     }
151
152     void append(uint32_t w)
153     {
154         m_words.append(w);
155     }
156
157     Vector<uint32_t, 16> m_words;
158 };
159
160 static void multadd(BigInt& b, int m, int a)    /* multiply by m and add a */
161 {
162 #ifdef USE_LONG_LONG
163     unsigned long long carry;
164 #else
165     uint32_t carry;
166 #endif
167
168     int wds = b.size();
169     uint32_t* x = b.words();
170     int i = 0;
171     carry = a;
172     do {
173 #ifdef USE_LONG_LONG
174         unsigned long long y = *x * (unsigned long long)m + carry;
175         carry = y >> 32;
176         *x++ = (uint32_t)y & 0xffffffffUL;
177 #else
178         uint32_t xi = *x;
179         uint32_t y = (xi & 0xffff) * m + carry;
180         uint32_t z = (xi >> 16) * m + (y >> 16);
181         carry = z >> 16;
182         *x++ = (z << 16) + (y & 0xffff);
183 #endif
184     } while (++i < wds);
185
186     if (carry)
187         b.append((uint32_t)carry);
188 }
189
190 static int hi0bits(uint32_t x)
191 {
192     int k = 0;
193
194     if (!(x & 0xffff0000)) {
195         k = 16;
196         x <<= 16;
197     }
198     if (!(x & 0xff000000)) {
199         k += 8;
200         x <<= 8;
201     }
202     if (!(x & 0xf0000000)) {
203         k += 4;
204         x <<= 4;
205     }
206     if (!(x & 0xc0000000)) {
207         k += 2;
208         x <<= 2;
209     }
210     if (!(x & 0x80000000)) {
211         k++;
212         if (!(x & 0x40000000))
213             return 32;
214     }
215     return k;
216 }
217
218 static int lo0bits(uint32_t* y)
219 {
220     int k;
221     uint32_t x = *y;
222
223     if (x & 7) {
224         if (x & 1)
225             return 0;
226         if (x & 2) {
227             *y = x >> 1;
228             return 1;
229         }
230         *y = x >> 2;
231         return 2;
232     }
233     k = 0;
234     if (!(x & 0xffff)) {
235         k = 16;
236         x >>= 16;
237     }
238     if (!(x & 0xff)) {
239         k += 8;
240         x >>= 8;
241     }
242     if (!(x & 0xf)) {
243         k += 4;
244         x >>= 4;
245     }
246     if (!(x & 0x3)) {
247         k += 2;
248         x >>= 2;
249     }
250     if (!(x & 1)) {
251         k++;
252         x >>= 1;
253         if (!x)
254             return 32;
255     }
256     *y = x;
257     return k;
258 }
259
260 static void i2b(BigInt& b, int i)
261 {
262     b.sign = 0;
263     b.resize(1);
264     b.words()[0] = i;
265 }
266
267 static void mult(BigInt& aRef, const BigInt& bRef)
268 {
269     const BigInt* a = &aRef;
270     const BigInt* b = &bRef;
271     BigInt c;
272     int wa, wb, wc;
273     const uint32_t* x = 0;
274     const uint32_t* xa;
275     const uint32_t* xb;
276     const uint32_t* xae;
277     const uint32_t* xbe;
278     uint32_t* xc;
279     uint32_t* xc0;
280     uint32_t y;
281 #ifdef USE_LONG_LONG
282     unsigned long long carry, z;
283 #else
284     uint32_t carry, z;
285 #endif
286
287     if (a->size() < b->size()) {
288         const BigInt* tmp = a;
289         a = b;
290         b = tmp;
291     }
292
293     wa = a->size();
294     wb = b->size();
295     wc = wa + wb;
296     c.resize(wc);
297
298     for (xc = c.words(), xa = xc + wc; xc < xa; xc++)
299         *xc = 0;
300     xa = a->words();
301     xae = xa + wa;
302     xb = b->words();
303     xbe = xb + wb;
304     xc0 = c.words();
305 #ifdef USE_LONG_LONG
306     for (; xb < xbe; xc0++) {
307         if ((y = *xb++)) {
308             x = xa;
309             xc = xc0;
310             carry = 0;
311             do {
312                 z = *x++ * (unsigned long long)y + *xc + carry;
313                 carry = z >> 32;
314                 *xc++ = (uint32_t)z & 0xffffffffUL;
315             } while (x < xae);
316             *xc = (uint32_t)carry;
317         }
318     }
319 #else
320     for (; xb < xbe; xb++, xc0++) {
321         if ((y = *xb & 0xffff)) {
322             x = xa;
323             xc = xc0;
324             carry = 0;
325             do {
326                 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
327                 carry = z >> 16;
328                 uint32_t z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
329                 carry = z2 >> 16;
330                 xc = storeInc(xc, z2, z);
331             } while (x < xae);
332             *xc = carry;
333         }
334         if ((y = *xb >> 16)) {
335             x = xa;
336             xc = xc0;
337             carry = 0;
338             uint32_t z2 = *xc;
339             do {
340                 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
341                 carry = z >> 16;
342                 xc = storeInc(xc, z, z2);
343                 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
344                 carry = z2 >> 16;
345             } while (x < xae);
346             *xc = z2;
347         }
348     }
349 #endif
350     for (xc0 = c.words(), xc = xc0 + wc; wc > 0 && !*--xc; --wc) { }
351     c.resize(wc);
352     aRef = c;
353 }
354
355 struct P5Node {
356     WTF_MAKE_NONCOPYABLE(P5Node); WTF_MAKE_FAST_ALLOCATED;
357 public:
358     P5Node() { }
359     BigInt val;
360     P5Node* next;
361 };
362
363 static P5Node* p5s;
364 static int p5sCount;
365
366 static ALWAYS_INLINE void pow5mult(BigInt& b, int k)
367 {
368     static int p05[3] = { 5, 25, 125 };
369
370     if (int i = k & 3)
371         multadd(b, p05[i - 1], 0);
372
373     if (!(k >>= 2))
374         return;
375
376     s_dtoaP5Mutex->lock();
377     P5Node* p5 = p5s;
378
379     if (!p5) {
380         /* first time */
381         p5 = new P5Node;
382         i2b(p5->val, 625);
383         p5->next = 0;
384         p5s = p5;
385         p5sCount = 1;
386     }
387
388     int p5sCountLocal = p5sCount;
389     s_dtoaP5Mutex->unlock();
390     int p5sUsed = 0;
391
392     for (;;) {
393         if (k & 1)
394             mult(b, p5->val);
395
396         if (!(k >>= 1))
397             break;
398
399         if (++p5sUsed == p5sCountLocal) {
400             s_dtoaP5Mutex->lock();
401             if (p5sUsed == p5sCount) {
402                 ASSERT(!p5->next);
403                 p5->next = new P5Node;
404                 p5->next->next = 0;
405                 p5->next->val = p5->val;
406                 mult(p5->next->val, p5->next->val);
407                 ++p5sCount;
408             }
409
410             p5sCountLocal = p5sCount;
411             s_dtoaP5Mutex->unlock();
412         }
413         p5 = p5->next;
414     }
415 }
416
417 static ALWAYS_INLINE void lshift(BigInt& b, int k)
418 {
419     int n = k >> 5;
420
421     int origSize = b.size();
422     int n1 = n + origSize + 1;
423
424     if (k &= 0x1f)
425         b.resize(b.size() + n + 1);
426     else
427         b.resize(b.size() + n);
428
429     const uint32_t* srcStart = b.words();
430     uint32_t* dstStart = b.words();
431     const uint32_t* src = srcStart + origSize - 1;
432     uint32_t* dst = dstStart + n1 - 1;
433     if (k) {
434         uint32_t hiSubword = 0;
435         int s = 32 - k;
436         for (; src >= srcStart; --src) {
437             *dst-- = hiSubword | *src >> s;
438             hiSubword = *src << k;
439         }
440         *dst = hiSubword;
441         ASSERT(dst == dstStart + n);
442
443         b.resize(origSize + n + !!b.words()[n1 - 1]);
444     }
445     else {
446         do {
447             *--dst = *src--;
448         } while (src >= srcStart);
449     }
450     for (dst = dstStart + n; dst != dstStart; )
451         *--dst = 0;
452
453     ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
454 }
455
456 static int cmp(const BigInt& a, const BigInt& b)
457 {
458     const uint32_t *xa, *xa0, *xb, *xb0;
459     int i, j;
460
461     i = a.size();
462     j = b.size();
463     ASSERT(i <= 1 || a.words()[i - 1]);
464     ASSERT(j <= 1 || b.words()[j - 1]);
465     if (i -= j)
466         return i;
467     xa0 = a.words();
468     xa = xa0 + j;
469     xb0 = b.words();
470     xb = xb0 + j;
471     for (;;) {
472         if (*--xa != *--xb)
473             return *xa < *xb ? -1 : 1;
474         if (xa <= xa0)
475             break;
476     }
477     return 0;
478 }
479
480 static ALWAYS_INLINE void diff(BigInt& c, const BigInt& aRef, const BigInt& bRef)
481 {
482     const BigInt* a = &aRef;
483     const BigInt* b = &bRef;
484     int i, wa, wb;
485     uint32_t* xc;
486
487     i = cmp(*a, *b);
488     if (!i) {
489         c.sign = 0;
490         c.resize(1);
491         c.words()[0] = 0;
492         return;
493     }
494     if (i < 0) {
495         const BigInt* tmp = a;
496         a = b;
497         b = tmp;
498         i = 1;
499     } else
500         i = 0;
501
502     wa = a->size();
503     const uint32_t* xa = a->words();
504     const uint32_t* xae = xa + wa;
505     wb = b->size();
506     const uint32_t* xb = b->words();
507     const uint32_t* xbe = xb + wb;
508
509     c.resize(wa);
510     c.sign = i;
511     xc = c.words();
512 #ifdef USE_LONG_LONG
513     unsigned long long borrow = 0;
514     do {
515         unsigned long long y = (unsigned long long)*xa++ - *xb++ - borrow;
516         borrow = y >> 32 & (uint32_t)1;
517         *xc++ = (uint32_t)y & 0xffffffffUL;
518     } while (xb < xbe);
519     while (xa < xae) {
520         unsigned long long y = *xa++ - borrow;
521         borrow = y >> 32 & (uint32_t)1;
522         *xc++ = (uint32_t)y & 0xffffffffUL;
523     }
524 #else
525     uint32_t borrow = 0;
526     do {
527         uint32_t y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
528         borrow = (y & 0x10000) >> 16;
529         uint32_t z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
530         borrow = (z & 0x10000) >> 16;
531         xc = storeInc(xc, z, y);
532     } while (xb < xbe);
533     while (xa < xae) {
534         uint32_t y = (*xa & 0xffff) - borrow;
535         borrow = (y & 0x10000) >> 16;
536         uint32_t z = (*xa++ >> 16) - borrow;
537         borrow = (z & 0x10000) >> 16;
538         xc = storeInc(xc, z, y);
539     }
540 #endif
541     while (!*--xc)
542         wa--;
543     c.resize(wa);
544 }
545
546 static ALWAYS_INLINE void d2b(BigInt& b, U* d, int* e, int* bits)
547 {
548     int de, k;
549     uint32_t* x;
550     uint32_t y, z;
551     int i;
552 #define d0 word0(d)
553 #define d1 word1(d)
554
555     b.sign = 0;
556     b.resize(1);
557     x = b.words();
558
559     z = d0 & Frac_mask;
560     d0 &= 0x7fffffff;    /* clear sign bit, which we ignore */
561     if ((de = (int)(d0 >> Exp_shift)))
562         z |= Exp_msk1;
563     if ((y = d1)) {
564         if ((k = lo0bits(&y))) {
565             x[0] = y | (z << (32 - k));
566             z >>= k;
567         } else
568             x[0] = y;
569         if (z) {
570             b.resize(2);
571             x[1] = z;
572         }
573
574         i = b.size();
575     } else {
576         k = lo0bits(&z);
577         x[0] = z;
578         i = 1;
579         b.resize(1);
580         k += 32;
581     }
582     if (de) {
583         *e = de - Bias - (P - 1) + k;
584         *bits = P - k;
585     } else {
586         *e = 0 - Bias - (P - 1) + 1 + k;
587         *bits = (32 * i) - hi0bits(x[i - 1]);
588     }
589 }
590 #undef d0
591 #undef d1
592
593 static const double tens[] = {
594     1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
595     1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
596     1e20, 1e21, 1e22
597 };
598
599 static const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
600 static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
601     9007199254740992. * 9007199254740992.e-256
602     /* = 2^106 * 1e-256 */
603 };
604
605 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
606 /* flag unnecessarily.  It leads to a song and dance at the end of strtod. */
607 #define Scale_Bit 0x10
608 #define n_bigtens 5
609
610 static ALWAYS_INLINE int quorem(BigInt& b, BigInt& S)
611 {
612     size_t n;
613     uint32_t* bx;
614     uint32_t* bxe;
615     uint32_t q;
616     uint32_t* sx;
617     uint32_t* sxe;
618 #ifdef USE_LONG_LONG
619     unsigned long long borrow, carry, y, ys;
620 #else
621     uint32_t borrow, carry, y, ys;
622     uint32_t si, z, zs;
623 #endif
624     ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
625     ASSERT(S.size() <= 1 || S.words()[S.size() - 1]);
626
627     n = S.size();
628     ASSERT_WITH_MESSAGE(b.size() <= n, "oversize b in quorem");
629     if (b.size() < n)
630         return 0;
631     sx = S.words();
632     sxe = sx + --n;
633     bx = b.words();
634     bxe = bx + n;
635     q = *bxe / (*sxe + 1);    /* ensure q <= true quotient */
636     ASSERT_WITH_MESSAGE(q <= 9, "oversized quotient in quorem");
637     if (q) {
638         borrow = 0;
639         carry = 0;
640         do {
641 #ifdef USE_LONG_LONG
642             ys = *sx++ * (unsigned long long)q + carry;
643             carry = ys >> 32;
644             y = *bx - (ys & 0xffffffffUL) - borrow;
645             borrow = y >> 32 & (uint32_t)1;
646             *bx++ = (uint32_t)y & 0xffffffffUL;
647 #else
648             si = *sx++;
649             ys = (si & 0xffff) * q + carry;
650             zs = (si >> 16) * q + (ys >> 16);
651             carry = zs >> 16;
652             y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
653             borrow = (y & 0x10000) >> 16;
654             z = (*bx >> 16) - (zs & 0xffff) - borrow;
655             borrow = (z & 0x10000) >> 16;
656             bx = storeInc(bx, z, y);
657 #endif
658         } while (sx <= sxe);
659         if (!*bxe) {
660             bx = b.words();
661             while (--bxe > bx && !*bxe)
662                 --n;
663             b.resize(n);
664         }
665     }
666     if (cmp(b, S) >= 0) {
667         q++;
668         borrow = 0;
669         carry = 0;
670         bx = b.words();
671         sx = S.words();
672         do {
673 #ifdef USE_LONG_LONG
674             ys = *sx++ + carry;
675             carry = ys >> 32;
676             y = *bx - (ys & 0xffffffffUL) - borrow;
677             borrow = y >> 32 & (uint32_t)1;
678             *bx++ = (uint32_t)y & 0xffffffffUL;
679 #else
680             si = *sx++;
681             ys = (si & 0xffff) + carry;
682             zs = (si >> 16) + (ys >> 16);
683             carry = zs >> 16;
684             y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
685             borrow = (y & 0x10000) >> 16;
686             z = (*bx >> 16) - (zs & 0xffff) - borrow;
687             borrow = (z & 0x10000) >> 16;
688             bx = storeInc(bx, z, y);
689 #endif
690         } while (sx <= sxe);
691         bx = b.words();
692         bxe = bx + n;
693         if (!*bxe) {
694             while (--bxe > bx && !*bxe)
695                 --n;
696             b.resize(n);
697         }
698     }
699     return q;
700 }
701
702 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
703  *
704  * Inspired by "How to Print Floating-Point Numbers Accurately" by
705  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
706  *
707  * Modifications:
708  *    1. Rather than iterating, we use a simple numeric overestimate
709  *       to determine k = floor(log10(d)).  We scale relevant
710  *       quantities using O(log2(k)) rather than O(k) multiplications.
711  *    2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
712  *       try to generate digits strictly left to right.  Instead, we
713  *       compute with fewer bits and propagate the carry if necessary
714  *       when rounding the final digit up.  This is often faster.
715  *    3. Under the assumption that input will be rounded nearest,
716  *       mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
717  *       That is, we allow equality in stopping tests when the
718  *       round-nearest rule will give the same floating-point value
719  *       as would satisfaction of the stopping test with strict
720  *       inequality.
721  *    4. We remove common factors of powers of 2 from relevant
722  *       quantities.
723  *    5. When converting floating-point integers less than 1e16,
724  *       we use floating-point arithmetic rather than resorting
725  *       to multiple-precision integers.
726  *    6. When asked to produce fewer than 15 digits, we first try
727  *       to get by with floating-point arithmetic; we resort to
728  *       multiple-precision integer arithmetic only if we cannot
729  *       guarantee that the floating-point calculation has given
730  *       the correctly rounded result.  For k requested digits and
731  *       "uniformly" distributed input, the probability is
732  *       something like 10^(k-15) that we must resort to the int32_t
733  *       calculation.
734  *
735  * Note: 'leftright' translates to 'generate shortest possible string'.
736  */
737 template<bool roundingNone, bool roundingSignificantFigures, bool roundingDecimalPlaces, bool leftright>
738 void dtoa(DtoaBuffer result, double dd, int ndigits, bool& signOut, int& exponentOut, unsigned& precisionOut)
739 {
740     // Exactly one rounding mode must be specified.
741     ASSERT(roundingNone + roundingSignificantFigures + roundingDecimalPlaces == 1);
742     // roundingNone only allowed (only sensible?) with leftright set.
743     ASSERT(!roundingNone || leftright);
744
745     ASSERT(isfinite(dd));
746
747     int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0,
748         j, j1, k, k0, k_check, m2, m5, s2, s5,
749         spec_case;
750     int32_t L;
751     int denorm;
752     uint32_t x;
753     BigInt b, delta, mlo, mhi, S;
754     U d2, eps, u;
755     double ds;
756     char* s;
757     char* s0;
758
759     u.d = dd;
760
761     /* Infinity or NaN */
762     ASSERT((word0(&u) & Exp_mask) != Exp_mask);
763
764     // JavaScript toString conversion treats -0 as 0.
765     if (!dval(&u)) {
766         signOut = false;
767         exponentOut = 0;
768         precisionOut = 1;
769         result[0] = '0';
770         result[1] = '\0';
771         return;
772     }
773
774     if (word0(&u) & Sign_bit) {
775         signOut = true;
776         word0(&u) &= ~Sign_bit; // clear sign bit
777     } else
778         signOut = false;
779
780     d2b(b, &u, &be, &bbits);
781     if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1)))) {
782         dval(&d2) = dval(&u);
783         word0(&d2) &= Frac_mask1;
784         word0(&d2) |= Exp_11;
785
786         /* log(x)    ~=~ log(1.5) + (x-1.5)/1.5
787          * log10(x)     =  log(x) / log(10)
788          *        ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
789          * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
790          *
791          * This suggests computing an approximation k to log10(d) by
792          *
793          * k = (i - Bias)*0.301029995663981
794          *    + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
795          *
796          * We want k to be too large rather than too small.
797          * The error in the first-order Taylor series approximation
798          * is in our favor, so we just round up the constant enough
799          * to compensate for any error in the multiplication of
800          * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
801          * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
802          * adding 1e-13 to the constant term more than suffices.
803          * Hence we adjust the constant term to 0.1760912590558.
804          * (We could get a more accurate k by invoking log10,
805          *  but this is probably not worthwhile.)
806          */
807
808         i -= Bias;
809         denorm = 0;
810     } else {
811         /* d is denormalized */
812
813         i = bbits + be + (Bias + (P - 1) - 1);
814         x = (i > 32) ? (word0(&u) << (64 - i)) | (word1(&u) >> (i - 32))
815                 : word1(&u) << (32 - i);
816         dval(&d2) = x;
817         word0(&d2) -= 31 * Exp_msk1; /* adjust exponent */
818         i -= (Bias + (P - 1) - 1) + 1;
819         denorm = 1;
820     }
821     ds = (dval(&d2) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i * 0.301029995663981);
822     k = (int)ds;
823     if (ds < 0. && ds != k)
824         k--;    /* want k = floor(ds) */
825     k_check = 1;
826     if (k >= 0 && k <= Ten_pmax) {
827         if (dval(&u) < tens[k])
828             k--;
829         k_check = 0;
830     }
831     j = bbits - i - 1;
832     if (j >= 0) {
833         b2 = 0;
834         s2 = j;
835     } else {
836         b2 = -j;
837         s2 = 0;
838     }
839     if (k >= 0) {
840         b5 = 0;
841         s5 = k;
842         s2 += k;
843     } else {
844         b2 -= k;
845         b5 = -k;
846         s5 = 0;
847     }
848
849     if (roundingNone) {
850         ilim = ilim1 = -1;
851         i = 18;
852         ndigits = 0;
853     }
854     if (roundingSignificantFigures) {
855         if (ndigits <= 0)
856             ndigits = 1;
857         ilim = ilim1 = i = ndigits;
858     }
859     if (roundingDecimalPlaces) {
860         i = ndigits + k + 1;
861         ilim = i;
862         ilim1 = i - 1;
863         if (i <= 0)
864             i = 1;
865     }
866
867     s = s0 = result;
868
869     if (ilim >= 0 && ilim <= Quick_max) {
870         /* Try to get by with floating-point arithmetic. */
871
872         i = 0;
873         dval(&d2) = dval(&u);
874         k0 = k;
875         ilim0 = ilim;
876         ieps = 2; /* conservative */
877         if (k > 0) {
878             ds = tens[k & 0xf];
879             j = k >> 4;
880             if (j & Bletch) {
881                 /* prevent overflows */
882                 j &= Bletch - 1;
883                 dval(&u) /= bigtens[n_bigtens - 1];
884                 ieps++;
885             }
886             for (; j; j >>= 1, i++) {
887                 if (j & 1) {
888                     ieps++;
889                     ds *= bigtens[i];
890                 }
891             }
892             dval(&u) /= ds;
893         } else if ((j1 = -k)) {
894             dval(&u) *= tens[j1 & 0xf];
895             for (j = j1 >> 4; j; j >>= 1, i++) {
896                 if (j & 1) {
897                     ieps++;
898                     dval(&u) *= bigtens[i];
899                 }
900             }
901         }
902         if (k_check && dval(&u) < 1. && ilim > 0) {
903             if (ilim1 <= 0)
904                 goto fastFailed;
905             ilim = ilim1;
906             k--;
907             dval(&u) *= 10.;
908             ieps++;
909         }
910         dval(&eps) = (ieps * dval(&u)) + 7.;
911         word0(&eps) -= (P - 1) * Exp_msk1;
912         if (!ilim) {
913             S.clear();
914             mhi.clear();
915             dval(&u) -= 5.;
916             if (dval(&u) > dval(&eps))
917                 goto oneDigit;
918             if (dval(&u) < -dval(&eps))
919                 goto noDigits;
920             goto fastFailed;
921         }
922         if (leftright) {
923             /* Use Steele & White method of only
924              * generating digits needed.
925              */
926             dval(&eps) = (0.5 / tens[ilim - 1]) - dval(&eps);
927             for (i = 0;;) {
928                 L = (long int)dval(&u);
929                 dval(&u) -= L;
930                 *s++ = '0' + (int)L;
931                 if (dval(&u) < dval(&eps))
932                     goto ret;
933                 if (1. - dval(&u) < dval(&eps))
934                     goto bumpUp;
935                 if (++i >= ilim)
936                     break;
937                 dval(&eps) *= 10.;
938                 dval(&u) *= 10.;
939             }
940         } else {
941             /* Generate ilim digits, then fix them up. */
942             dval(&eps) *= tens[ilim - 1];
943             for (i = 1;; i++, dval(&u) *= 10.) {
944                 L = (int32_t)(dval(&u));
945                 if (!(dval(&u) -= L))
946                     ilim = i;
947                 *s++ = '0' + (int)L;
948                 if (i == ilim) {
949                     if (dval(&u) > 0.5 + dval(&eps))
950                         goto bumpUp;
951                     if (dval(&u) < 0.5 - dval(&eps)) {
952                         while (*--s == '0') { }
953                         s++;
954                         goto ret;
955                     }
956                     break;
957                 }
958             }
959         }
960 fastFailed:
961         s = s0;
962         dval(&u) = dval(&d2);
963         k = k0;
964         ilim = ilim0;
965     }
966
967     /* Do we have a "small" integer? */
968
969     if (be >= 0 && k <= Int_max) {
970         /* Yes. */
971         ds = tens[k];
972         if (ndigits < 0 && ilim <= 0) {
973             S.clear();
974             mhi.clear();
975             if (ilim < 0 || dval(&u) <= 5 * ds)
976                 goto noDigits;
977             goto oneDigit;
978         }
979         for (i = 1;; i++, dval(&u) *= 10.) {
980             L = (int32_t)(dval(&u) / ds);
981             dval(&u) -= L * ds;
982             *s++ = '0' + (int)L;
983             if (!dval(&u)) {
984                 break;
985             }
986             if (i == ilim) {
987                 dval(&u) += dval(&u);
988                 if (dval(&u) > ds || (dval(&u) == ds && (L & 1))) {
989 bumpUp:
990                     while (*--s == '9')
991                         if (s == s0) {
992                             k++;
993                             *s = '0';
994                             break;
995                         }
996                     ++*s++;
997                 }
998                 break;
999             }
1000         }
1001         goto ret;
1002     }
1003
1004     m2 = b2;
1005     m5 = b5;
1006     mhi.clear();
1007     mlo.clear();
1008     if (leftright) {
1009         i = denorm ? be + (Bias + (P - 1) - 1 + 1) : 1 + P - bbits;
1010         b2 += i;
1011         s2 += i;
1012         i2b(mhi, 1);
1013     }
1014     if (m2 > 0 && s2 > 0) {
1015         i = m2 < s2 ? m2 : s2;
1016         b2 -= i;
1017         m2 -= i;
1018         s2 -= i;
1019     }
1020     if (b5 > 0) {
1021         if (leftright) {
1022             if (m5 > 0) {
1023                 pow5mult(mhi, m5);
1024                 mult(b, mhi);
1025             }
1026             if ((j = b5 - m5))
1027                 pow5mult(b, j);
1028         } else
1029             pow5mult(b, b5);
1030     }
1031     i2b(S, 1);
1032     if (s5 > 0)
1033         pow5mult(S, s5);
1034
1035     /* Check for special case that d is a normalized power of 2. */
1036
1037     spec_case = 0;
1038     if ((roundingNone || leftright) && (!word1(&u) && !(word0(&u) & Bndry_mask) && word0(&u) & (Exp_mask & ~Exp_msk1))) {
1039         /* The special case */
1040         b2 += Log2P;
1041         s2 += Log2P;
1042         spec_case = 1;
1043     }
1044
1045     /* Arrange for convenient computation of quotients:
1046      * shift left if necessary so divisor has 4 leading 0 bits.
1047      *
1048      * Perhaps we should just compute leading 28 bits of S once
1049      * and for all and pass them and a shift to quorem, so it
1050      * can do shifts and ors to compute the numerator for q.
1051      */
1052     if ((i = ((s5 ? 32 - hi0bits(S.words()[S.size() - 1]) : 1) + s2) & 0x1f))
1053         i = 32 - i;
1054     if (i > 4) {
1055         i -= 4;
1056         b2 += i;
1057         m2 += i;
1058         s2 += i;
1059     } else if (i < 4) {
1060         i += 28;
1061         b2 += i;
1062         m2 += i;
1063         s2 += i;
1064     }
1065     if (b2 > 0)
1066         lshift(b, b2);
1067     if (s2 > 0)
1068         lshift(S, s2);
1069     if (k_check) {
1070         if (cmp(b, S) < 0) {
1071             k--;
1072             multadd(b, 10, 0);    /* we botched the k estimate */
1073             if (leftright)
1074                 multadd(mhi, 10, 0);
1075             ilim = ilim1;
1076         }
1077     }
1078     if (ilim <= 0 && roundingDecimalPlaces) {
1079         if (ilim < 0)
1080             goto noDigits;
1081         multadd(S, 5, 0);
1082         // For IEEE-754 unbiased rounding this check should be <=, such that 0.5 would flush to zero.
1083         if (cmp(b, S) < 0)
1084             goto noDigits;
1085         goto oneDigit;
1086     }
1087     if (leftright) {
1088         if (m2 > 0)
1089             lshift(mhi, m2);
1090
1091         /* Compute mlo -- check for special case
1092          * that d is a normalized power of 2.
1093          */
1094
1095         mlo = mhi;
1096         if (spec_case)
1097             lshift(mhi, Log2P);
1098
1099         for (i = 1;;i++) {
1100             dig = quorem(b, S) + '0';
1101             /* Do we yet have the shortest decimal string
1102              * that will round to d?
1103              */
1104             j = cmp(b, mlo);
1105             diff(delta, S, mhi);
1106             j1 = delta.sign ? 1 : cmp(b, delta);
1107 #ifdef DTOA_ROUND_BIASED
1108             if (j < 0 || !j) {
1109 #else
1110             // FIXME: ECMA-262 specifies that equidistant results round away from
1111             // zero, which probably means we shouldn't be on the unbiased code path
1112             // (the (word1(&u) & 1) clause is looking highly suspicious). I haven't
1113             // yet understood this code well enough to make the call, but we should
1114             // probably be enabling DTOA_ROUND_BIASED. I think the interesting corner
1115             // case to understand is probably "Math.pow(0.5, 24).toString()".
1116             // I believe this value is interesting because I think it is precisely
1117             // representable in binary floating point, and its decimal representation
1118             // has a single digit that Steele & White reduction can remove, with the
1119             // value 5 (thus equidistant from the next numbers above and below).
1120             // We produce the correct answer using either codepath, and I don't as
1121             // yet understand why. :-)
1122             if (!j1 && !(word1(&u) & 1)) {
1123                 if (dig == '9')
1124                     goto round9up;
1125                 if (j > 0)
1126                     dig++;
1127                 *s++ = dig;
1128                 goto ret;
1129             }
1130             if (j < 0 || (!j && !(word1(&u) & 1))) {
1131 #endif
1132                 if ((b.words()[0] || b.size() > 1) && (j1 > 0)) {
1133                     lshift(b, 1);
1134                     j1 = cmp(b, S);
1135                     // For IEEE-754 round-to-even, this check should be (j1 > 0 || (!j1 && (dig & 1))),
1136                     // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) should
1137                     // be rounded away from zero.
1138                     if (j1 >= 0) {
1139                         if (dig == '9')
1140                             goto round9up;
1141                         dig++;
1142                     }
1143                 }
1144                 *s++ = dig;
1145                 goto ret;
1146             }
1147             if (j1 > 0) {
1148                 if (dig == '9') { /* possible if i == 1 */
1149 round9up:
1150                     *s++ = '9';
1151                     goto roundoff;
1152                 }
1153                 *s++ = dig + 1;
1154                 goto ret;
1155             }
1156             *s++ = dig;
1157             if (i == ilim)
1158                 break;
1159             multadd(b, 10, 0);
1160             multadd(mlo, 10, 0);
1161             multadd(mhi, 10, 0);
1162         }
1163     } else {
1164         for (i = 1;; i++) {
1165             *s++ = dig = quorem(b, S) + '0';
1166             if (!b.words()[0] && b.size() <= 1)
1167                 goto ret;
1168             if (i >= ilim)
1169                 break;
1170             multadd(b, 10, 0);
1171         }
1172     }
1173
1174     /* Round off last digit */
1175
1176     lshift(b, 1);
1177     j = cmp(b, S);
1178     // For IEEE-754 round-to-even, this check should be (j > 0 || (!j && (dig & 1))),
1179     // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) should
1180     // be rounded away from zero.
1181     if (j >= 0) {
1182 roundoff:
1183         while (*--s == '9')
1184             if (s == s0) {
1185                 k++;
1186                 *s++ = '1';
1187                 goto ret;
1188             }
1189         ++*s++;
1190     } else {
1191         while (*--s == '0') { }
1192         s++;
1193     }
1194     goto ret;
1195 noDigits:
1196     exponentOut = 0;
1197     precisionOut = 1;
1198     result[0] = '0';
1199     result[1] = '\0';
1200     return;
1201 oneDigit:
1202     *s++ = '1';
1203     k++;
1204     goto ret;
1205 ret:
1206     ASSERT(s > result);
1207     *s = 0;
1208     exponentOut = k;
1209     precisionOut = s - result;
1210 }
1211
1212 void dtoa(DtoaBuffer result, double dd, bool& sign, int& exponent, unsigned& precision)
1213 {
1214     // flags are roundingNone, leftright.
1215     dtoa<true, false, false, true>(result, dd, 0, sign, exponent, precision);
1216 }
1217
1218 void dtoaRoundSF(DtoaBuffer result, double dd, int ndigits, bool& sign, int& exponent, unsigned& precision)
1219 {
1220     // flag is roundingSignificantFigures.
1221     dtoa<false, true, false, false>(result, dd, ndigits, sign, exponent, precision);
1222 }
1223
1224 void dtoaRoundDP(DtoaBuffer result, double dd, int ndigits, bool& sign, int& exponent, unsigned& precision)
1225 {
1226     // flag is roundingDecimalPlaces.
1227     dtoa<false, false, true, false>(result, dd, ndigits, sign, exponent, precision);
1228 }
1229
1230 const char* numberToString(double d, NumberToStringBuffer buffer)
1231 {
1232     double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
1233     const double_conversion::DoubleToStringConverter& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter();
1234     converter.ToShortest(d, &builder);
1235     return builder.Finalize();
1236 }
1237
1238 static inline const char* formatStringTruncatingTrailingZerosIfNeeded(NumberToStringBuffer buffer, double_conversion::StringBuilder& builder)
1239 {
1240     size_t length = builder.position();
1241     size_t decimalPointPosition = 0;
1242     for (; decimalPointPosition < length; ++decimalPointPosition) {
1243         if (buffer[decimalPointPosition] == '.')
1244             break;
1245     }
1246
1247     // No decimal seperator found, early exit.
1248     if (decimalPointPosition == length)
1249         return builder.Finalize();
1250
1251     size_t truncatedLength = length - 1;
1252     for (; truncatedLength > decimalPointPosition; --truncatedLength) {
1253         if (buffer[truncatedLength] != '0')
1254             break;
1255     }
1256
1257     // No trailing zeros found to strip.
1258     if (truncatedLength == length - 1)
1259         return builder.Finalize();
1260
1261     // If we removed all trailing zeros, remove the decimal point as well.
1262     if (truncatedLength == decimalPointPosition) {
1263         ASSERT(truncatedLength > 0);
1264         --truncatedLength;
1265     }
1266
1267     // Truncate the StringBuilder, and return the final result.
1268     builder.SetPosition(truncatedLength + 1);
1269     return builder.Finalize();
1270 }
1271
1272 const char* numberToFixedPrecisionString(double d, unsigned significantFigures, NumberToStringBuffer buffer, bool truncateTrailingZeros)
1273 {
1274     // Mimic String::format("%.[precision]g", ...), but use dtoas rounding facilities.
1275     // "g": Signed value printed in f or e format, whichever is more compact for the given value and precision.
1276     // The e format is used only when the exponent of the value is less than –4 or greater than or equal to the
1277     // precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
1278     // "precision": The precision specifies the maximum number of significant digits printed.
1279     double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
1280     const double_conversion::DoubleToStringConverter& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter();
1281     converter.ToPrecision(d, significantFigures, &builder);
1282     if (!truncateTrailingZeros)
1283         return builder.Finalize();
1284     return formatStringTruncatingTrailingZerosIfNeeded(buffer, builder);
1285 }
1286
1287 const char* numberToFixedWidthString(double d, unsigned decimalPlaces, NumberToStringBuffer buffer)
1288 {
1289     // Mimic String::format("%.[precision]f", ...), but use dtoas rounding facilities.
1290     // "f": Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits.
1291     // The number of digits before the decimal point depends on the magnitude of the number, and
1292     // the number of digits after the decimal point depends on the requested precision.
1293     // "precision": The precision value specifies the number of digits after the decimal point.
1294     // If a decimal point appears, at least one digit appears before it.
1295     // The value is rounded to the appropriate number of digits.    
1296     double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
1297     const double_conversion::DoubleToStringConverter& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter();
1298     converter.ToFixed(d, decimalPlaces, &builder);
1299     return builder.Finalize();
1300 }
1301
1302 namespace Internal {
1303
1304 double parseDoubleFromLongString(const UChar* string, size_t length, size_t& parsedLength)
1305 {
1306     Vector<LChar> conversionBuffer(length);
1307     for (size_t i = 0; i < length; ++i)
1308         conversionBuffer[i] = isASCII(string[i]) ? string[i] : 0;
1309     return parseDouble(conversionBuffer.data(), length, parsedLength);
1310 }
1311
1312 } // namespace Internal
1313
1314 } // namespace WTF