Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / Decimal.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "platform/Decimal.h"
33
34 #include "wtf/MathExtras.h"
35 #include "wtf/Noncopyable.h"
36 #include "wtf/text/StringBuilder.h"
37
38 #include <algorithm>
39 #include <float.h>
40
41 namespace blink {
42
43 namespace DecimalPrivate {
44
45 static int const ExponentMax = 1023;
46 static int const ExponentMin = -1023;
47 static int const Precision = 18;
48
49 static const uint64_t MaxCoefficient = UINT64_C(0xDE0B6B3A763FFFF); // 999999999999999999 == 18 9's
50
51 // This class handles Decimal special values.
52 class SpecialValueHandler {
53     WTF_MAKE_NONCOPYABLE(SpecialValueHandler);
54 public:
55     enum HandleResult {
56         BothFinite,
57         BothInfinity,
58         EitherNaN,
59         LHSIsInfinity,
60         RHSIsInfinity,
61     };
62
63     SpecialValueHandler(const Decimal& lhs, const Decimal& rhs);
64     HandleResult handle();
65     Decimal value() const;
66
67 private:
68     enum Result {
69         ResultIsLHS,
70         ResultIsRHS,
71         ResultIsUnknown,
72     };
73
74     const Decimal& m_lhs;
75     const Decimal& m_rhs;
76     Result m_result;
77 };
78
79 SpecialValueHandler::SpecialValueHandler(const Decimal& lhs, const Decimal& rhs)
80     : m_lhs(lhs), m_rhs(rhs), m_result(ResultIsUnknown)
81 {
82 }
83
84 SpecialValueHandler::HandleResult SpecialValueHandler::handle()
85 {
86     if (m_lhs.isFinite() && m_rhs.isFinite())
87         return BothFinite;
88
89     const Decimal::EncodedData::FormatClass lhsClass = m_lhs.value().formatClass();
90     const Decimal::EncodedData::FormatClass rhsClass = m_rhs.value().formatClass();
91     if (lhsClass == Decimal::EncodedData::ClassNaN) {
92         m_result = ResultIsLHS;
93         return EitherNaN;
94     }
95
96     if (rhsClass == Decimal::EncodedData::ClassNaN) {
97         m_result = ResultIsRHS;
98         return EitherNaN;
99     }
100
101     if (lhsClass == Decimal::EncodedData::ClassInfinity)
102         return rhsClass == Decimal::EncodedData::ClassInfinity ? BothInfinity : LHSIsInfinity;
103
104     if (rhsClass == Decimal::EncodedData::ClassInfinity)
105         return RHSIsInfinity;
106
107     ASSERT_NOT_REACHED();
108     return BothFinite;
109 }
110
111 Decimal SpecialValueHandler::value() const
112 {
113     switch (m_result) {
114     case ResultIsLHS:
115         return m_lhs;
116     case ResultIsRHS:
117         return m_rhs;
118     case ResultIsUnknown:
119     default:
120         ASSERT_NOT_REACHED();
121         return m_lhs;
122     }
123 }
124
125 // This class is used for 128 bit unsigned integer arithmetic.
126 class UInt128 {
127 public:
128     UInt128(uint64_t low, uint64_t high)
129         : m_high(high), m_low(low)
130     {
131     }
132
133     UInt128& operator/=(uint32_t);
134
135     uint64_t high() const { return m_high; }
136     uint64_t low() const { return m_low; }
137
138     static UInt128 multiply(uint64_t u, uint64_t v) { return UInt128(u * v, multiplyHigh(u, v)); }
139
140 private:
141     static uint32_t highUInt32(uint64_t x) { return static_cast<uint32_t>(x >> 32); }
142     static uint32_t lowUInt32(uint64_t x) { return static_cast<uint32_t>(x & ((static_cast<uint64_t>(1) << 32) - 1)); }
143     static uint64_t makeUInt64(uint32_t low, uint32_t high) { return low | (static_cast<uint64_t>(high) << 32); }
144
145     static uint64_t multiplyHigh(uint64_t, uint64_t);
146
147     uint64_t m_high;
148     uint64_t m_low;
149 };
150
151 UInt128& UInt128::operator/=(const uint32_t divisor)
152 {
153     ASSERT(divisor);
154
155     if (!m_high) {
156         m_low /= divisor;
157         return *this;
158     }
159
160     uint32_t dividend[4];
161     dividend[0] = lowUInt32(m_low);
162     dividend[1] = highUInt32(m_low);
163     dividend[2] = lowUInt32(m_high);
164     dividend[3] = highUInt32(m_high);
165
166     uint32_t quotient[4];
167     uint32_t remainder = 0;
168     for (int i = 3; i >= 0; --i) {
169         const uint64_t work = makeUInt64(dividend[i], remainder);
170         remainder = static_cast<uint32_t>(work % divisor);
171         quotient[i] = static_cast<uint32_t>(work / divisor);
172     }
173     m_low = makeUInt64(quotient[0], quotient[1]);
174     m_high = makeUInt64(quotient[2], quotient[3]);
175     return *this;
176 }
177
178 // Returns high 64bit of 128bit product.
179 uint64_t UInt128::multiplyHigh(uint64_t u, uint64_t v)
180 {
181     const uint64_t uLow = lowUInt32(u);
182     const uint64_t uHigh = highUInt32(u);
183     const uint64_t vLow = lowUInt32(v);
184     const uint64_t vHigh = highUInt32(v);
185     const uint64_t partialProduct = uHigh * vLow + highUInt32(uLow * vLow);
186     return uHigh * vHigh + highUInt32(partialProduct) + highUInt32(uLow * vHigh + lowUInt32(partialProduct));
187 }
188
189 static int countDigits(uint64_t x)
190 {
191     int numberOfDigits = 0;
192     for (uint64_t powerOfTen = 1; x >= powerOfTen; powerOfTen *= 10) {
193         ++numberOfDigits;
194         if (powerOfTen >= std::numeric_limits<uint64_t>::max() / 10)
195             break;
196     }
197     return numberOfDigits;
198 }
199
200 static uint64_t scaleDown(uint64_t x, int n)
201 {
202     ASSERT(n >= 0);
203     while (n > 0 && x) {
204         x /= 10;
205         --n;
206     }
207     return x;
208 }
209
210 static uint64_t scaleUp(uint64_t x, int n)
211 {
212     ASSERT(n >= 0);
213     ASSERT(n < Precision);
214
215     uint64_t y = 1;
216     uint64_t z = 10;
217     for (;;) {
218         if (n & 1)
219             y = y * z;
220
221         n >>= 1;
222         if (!n)
223             return x * y;
224
225         z = z * z;
226     }
227 }
228
229 } // namespace DecimalPrivate
230
231 using namespace DecimalPrivate;
232
233 Decimal::EncodedData::EncodedData(Sign sign, FormatClass formatClass)
234     : m_coefficient(0)
235     , m_exponent(0)
236     , m_formatClass(formatClass)
237     , m_sign(sign)
238 {
239 }
240
241 Decimal::EncodedData::EncodedData(Sign sign, int exponent, uint64_t coefficient)
242     : m_formatClass(coefficient ? ClassNormal : ClassZero)
243     , m_sign(sign)
244 {
245     if (exponent >= ExponentMin && exponent <= ExponentMax) {
246         while (coefficient > MaxCoefficient) {
247             coefficient /= 10;
248             ++exponent;
249         }
250     }
251
252     if (exponent > ExponentMax) {
253         m_coefficient = 0;
254         m_exponent = 0;
255         m_formatClass = ClassInfinity;
256         return;
257     }
258
259     if (exponent < ExponentMin) {
260         m_coefficient = 0;
261         m_exponent = 0;
262         m_formatClass = ClassZero;
263         return;
264     }
265
266     m_coefficient = coefficient;
267     m_exponent = static_cast<int16_t>(exponent);
268 }
269
270 bool Decimal::EncodedData::operator==(const EncodedData& another) const
271 {
272     return m_sign == another.m_sign
273         && m_formatClass == another.m_formatClass
274         && m_exponent == another.m_exponent
275         && m_coefficient == another.m_coefficient;
276 }
277
278 Decimal::Decimal(int32_t i32)
279     : m_data(i32 < 0 ? Negative : Positive, 0, i32 < 0 ? static_cast<uint64_t>(-static_cast<int64_t>(i32)) : static_cast<uint64_t>(i32))
280 {
281 }
282
283 Decimal::Decimal(Sign sign, int exponent, uint64_t coefficient)
284     : m_data(sign, exponent, coefficient)
285 {
286 }
287
288 Decimal::Decimal(const EncodedData& data)
289     : m_data(data)
290 {
291 }
292
293 Decimal::Decimal(const Decimal& other)
294     : m_data(other.m_data)
295 {
296 }
297
298 Decimal& Decimal::operator=(const Decimal& other)
299 {
300     m_data = other.m_data;
301     return *this;
302 }
303
304 Decimal& Decimal::operator+=(const Decimal& other)
305 {
306     m_data = (*this + other).m_data;
307     return *this;
308 }
309
310 Decimal& Decimal::operator-=(const Decimal& other)
311 {
312     m_data = (*this - other).m_data;
313     return *this;
314 }
315
316 Decimal& Decimal::operator*=(const Decimal& other)
317 {
318     m_data = (*this * other).m_data;
319     return *this;
320 }
321
322 Decimal& Decimal::operator/=(const Decimal& other)
323 {
324     m_data = (*this / other).m_data;
325     return *this;
326 }
327
328 Decimal Decimal::operator-() const
329 {
330     if (isNaN())
331         return *this;
332
333     Decimal result(*this);
334     result.m_data.setSign(invertSign(m_data.sign()));
335     return result;
336 }
337
338 Decimal Decimal::operator+(const Decimal& rhs) const
339 {
340     const Decimal& lhs = *this;
341     const Sign lhsSign = lhs.sign();
342     const Sign rhsSign = rhs.sign();
343
344     SpecialValueHandler handler(lhs, rhs);
345     switch (handler.handle()) {
346     case SpecialValueHandler::BothFinite:
347         break;
348
349     case SpecialValueHandler::BothInfinity:
350         return lhsSign == rhsSign ? lhs : nan();
351
352     case SpecialValueHandler::EitherNaN:
353         return handler.value();
354
355     case SpecialValueHandler::LHSIsInfinity:
356         return lhs;
357
358     case SpecialValueHandler::RHSIsInfinity:
359         return rhs;
360     }
361
362     const AlignedOperands alignedOperands = alignOperands(lhs, rhs);
363
364     const uint64_t result = lhsSign == rhsSign
365         ? alignedOperands.lhsCoefficient + alignedOperands.rhsCoefficient
366         : alignedOperands.lhsCoefficient - alignedOperands.rhsCoefficient;
367
368     if (lhsSign == Negative && rhsSign == Positive && !result)
369         return Decimal(Positive, alignedOperands.exponent, 0);
370
371     return static_cast<int64_t>(result) >= 0
372         ? Decimal(lhsSign, alignedOperands.exponent, result)
373         : Decimal(invertSign(lhsSign), alignedOperands.exponent, -static_cast<int64_t>(result));
374 }
375
376 Decimal Decimal::operator-(const Decimal& rhs) const
377 {
378     const Decimal& lhs = *this;
379     const Sign lhsSign = lhs.sign();
380     const Sign rhsSign = rhs.sign();
381
382     SpecialValueHandler handler(lhs, rhs);
383     switch (handler.handle()) {
384     case SpecialValueHandler::BothFinite:
385         break;
386
387     case SpecialValueHandler::BothInfinity:
388         return lhsSign == rhsSign ? nan() : lhs;
389
390     case SpecialValueHandler::EitherNaN:
391         return handler.value();
392
393     case SpecialValueHandler::LHSIsInfinity:
394         return lhs;
395
396     case SpecialValueHandler::RHSIsInfinity:
397         return infinity(invertSign(rhsSign));
398     }
399
400     const AlignedOperands alignedOperands = alignOperands(lhs, rhs);
401
402     const uint64_t result = lhsSign == rhsSign
403         ? alignedOperands.lhsCoefficient - alignedOperands.rhsCoefficient
404         : alignedOperands.lhsCoefficient + alignedOperands.rhsCoefficient;
405
406     if (lhsSign == Negative && rhsSign == Negative && !result)
407         return Decimal(Positive, alignedOperands.exponent, 0);
408
409     return static_cast<int64_t>(result) >= 0
410         ? Decimal(lhsSign, alignedOperands.exponent, result)
411         : Decimal(invertSign(lhsSign), alignedOperands.exponent, -static_cast<int64_t>(result));
412 }
413
414 Decimal Decimal::operator*(const Decimal& rhs) const
415 {
416     const Decimal& lhs = *this;
417     const Sign lhsSign = lhs.sign();
418     const Sign rhsSign = rhs.sign();
419     const Sign resultSign = lhsSign == rhsSign ? Positive : Negative;
420
421     SpecialValueHandler handler(lhs, rhs);
422     switch (handler.handle()) {
423     case SpecialValueHandler::BothFinite: {
424         const uint64_t lhsCoefficient = lhs.m_data.coefficient();
425         const uint64_t rhsCoefficient = rhs.m_data.coefficient();
426         int resultExponent = lhs.exponent() + rhs.exponent();
427         UInt128 work(UInt128::multiply(lhsCoefficient, rhsCoefficient));
428         while (work.high()) {
429             work /= 10;
430             ++resultExponent;
431         }
432         return Decimal(resultSign, resultExponent, work.low());
433     }
434
435     case SpecialValueHandler::BothInfinity:
436         return infinity(resultSign);
437
438     case SpecialValueHandler::EitherNaN:
439         return handler.value();
440
441     case SpecialValueHandler::LHSIsInfinity:
442         return rhs.isZero() ? nan() : infinity(resultSign);
443
444     case SpecialValueHandler::RHSIsInfinity:
445         return lhs.isZero() ? nan() : infinity(resultSign);
446     }
447
448     ASSERT_NOT_REACHED();
449     return nan();
450 }
451
452 Decimal Decimal::operator/(const Decimal& rhs) const
453 {
454     const Decimal& lhs = *this;
455     const Sign lhsSign = lhs.sign();
456     const Sign rhsSign = rhs.sign();
457     const Sign resultSign = lhsSign == rhsSign ? Positive : Negative;
458
459     SpecialValueHandler handler(lhs, rhs);
460     switch (handler.handle()) {
461     case SpecialValueHandler::BothFinite:
462         break;
463
464     case SpecialValueHandler::BothInfinity:
465         return nan();
466
467     case SpecialValueHandler::EitherNaN:
468         return handler.value();
469
470     case SpecialValueHandler::LHSIsInfinity:
471         return infinity(resultSign);
472
473     case SpecialValueHandler::RHSIsInfinity:
474         return zero(resultSign);
475     }
476
477     ASSERT(lhs.isFinite());
478     ASSERT(rhs.isFinite());
479
480     if (rhs.isZero())
481         return lhs.isZero() ? nan() : infinity(resultSign);
482
483     int resultExponent = lhs.exponent() - rhs.exponent();
484
485     if (lhs.isZero())
486         return Decimal(resultSign, resultExponent, 0);
487
488     uint64_t remainder = lhs.m_data.coefficient();
489     const uint64_t divisor = rhs.m_data.coefficient();
490     uint64_t result = 0;
491     while (result < MaxCoefficient / 100) {
492         while (remainder < divisor) {
493             remainder *= 10;
494             result *= 10;
495             --resultExponent;
496         }
497         result += remainder / divisor;
498         remainder %= divisor;
499         if (!remainder)
500             break;
501     }
502
503     if (remainder > divisor / 2)
504         ++result;
505
506     return Decimal(resultSign, resultExponent, result);
507 }
508
509 bool Decimal::operator==(const Decimal& rhs) const
510 {
511     return m_data == rhs.m_data || compareTo(rhs).isZero();
512 }
513
514 bool Decimal::operator!=(const Decimal& rhs) const
515 {
516     if (m_data == rhs.m_data)
517         return false;
518     const Decimal result = compareTo(rhs);
519     if (result.isNaN())
520         return false;
521     return !result.isZero();
522 }
523
524 bool Decimal::operator<(const Decimal& rhs) const
525 {
526     const Decimal result = compareTo(rhs);
527     if (result.isNaN())
528         return false;
529     return !result.isZero() && result.isNegative();
530 }
531
532 bool Decimal::operator<=(const Decimal& rhs) const
533 {
534     if (m_data == rhs.m_data)
535         return true;
536     const Decimal result = compareTo(rhs);
537     if (result.isNaN())
538         return false;
539     return result.isZero() || result.isNegative();
540 }
541
542 bool Decimal::operator>(const Decimal& rhs) const
543 {
544     const Decimal result = compareTo(rhs);
545     if (result.isNaN())
546         return false;
547     return !result.isZero() && result.isPositive();
548 }
549
550 bool Decimal::operator>=(const Decimal& rhs) const
551 {
552     if (m_data == rhs.m_data)
553         return true;
554     const Decimal result = compareTo(rhs);
555     if (result.isNaN())
556         return false;
557     return result.isZero() || !result.isNegative();
558 }
559
560 Decimal Decimal::abs() const
561 {
562     Decimal result(*this);
563     result.m_data.setSign(Positive);
564     return result;
565 }
566
567 Decimal::AlignedOperands Decimal::alignOperands(const Decimal& lhs, const Decimal& rhs)
568 {
569     ASSERT(lhs.isFinite());
570     ASSERT(rhs.isFinite());
571
572     const int lhsExponent = lhs.exponent();
573     const int rhsExponent = rhs.exponent();
574     int exponent = std::min(lhsExponent, rhsExponent);
575     uint64_t lhsCoefficient = lhs.m_data.coefficient();
576     uint64_t rhsCoefficient = rhs.m_data.coefficient();
577
578     if (lhsExponent > rhsExponent) {
579         const int numberOfLHSDigits = countDigits(lhsCoefficient);
580         if (numberOfLHSDigits) {
581             const int lhsShiftAmount = lhsExponent - rhsExponent;
582             const int overflow = numberOfLHSDigits + lhsShiftAmount - Precision;
583             if (overflow <= 0) {
584                 lhsCoefficient = scaleUp(lhsCoefficient, lhsShiftAmount);
585             } else {
586                 lhsCoefficient = scaleUp(lhsCoefficient, lhsShiftAmount - overflow);
587                 rhsCoefficient = scaleDown(rhsCoefficient, overflow);
588                 exponent += overflow;
589             }
590         }
591
592     } else if (lhsExponent < rhsExponent) {
593         const int numberOfRHSDigits = countDigits(rhsCoefficient);
594         if (numberOfRHSDigits) {
595             const int rhsShiftAmount = rhsExponent - lhsExponent;
596             const int overflow = numberOfRHSDigits + rhsShiftAmount - Precision;
597             if (overflow <= 0) {
598                 rhsCoefficient = scaleUp(rhsCoefficient, rhsShiftAmount);
599             } else {
600                 rhsCoefficient = scaleUp(rhsCoefficient, rhsShiftAmount - overflow);
601                 lhsCoefficient = scaleDown(lhsCoefficient, overflow);
602                 exponent += overflow;
603             }
604         }
605     }
606
607     AlignedOperands alignedOperands;
608     alignedOperands.exponent = exponent;
609     alignedOperands.lhsCoefficient = lhsCoefficient;
610     alignedOperands.rhsCoefficient = rhsCoefficient;
611     return alignedOperands;
612 }
613
614 static bool isMultiplePowersOfTen(uint64_t coefficient, int n)
615 {
616     return !coefficient || !(coefficient % scaleUp(1, n));
617 }
618
619 // Round toward positive infinity.
620 Decimal Decimal::ceil() const
621 {
622     if (isSpecial())
623         return *this;
624
625     if (exponent() >= 0)
626         return *this;
627
628     uint64_t result = m_data.coefficient();
629     const int numberOfDigits = countDigits(result);
630     const int numberOfDropDigits = -exponent();
631     if (numberOfDigits < numberOfDropDigits)
632         return isPositive() ? Decimal(1) : zero(Positive);
633
634     result = scaleDown(result, numberOfDropDigits);
635     if (isPositive() && !isMultiplePowersOfTen(m_data.coefficient(), numberOfDropDigits))
636         ++result;
637     return Decimal(sign(), 0, result);
638 }
639
640 Decimal Decimal::compareTo(const Decimal& rhs) const
641 {
642     const Decimal result(*this - rhs);
643     switch (result.m_data.formatClass()) {
644     case EncodedData::ClassInfinity:
645         return result.isNegative() ? Decimal(-1) : Decimal(1);
646
647     case EncodedData::ClassNaN:
648     case EncodedData::ClassNormal:
649         return result;
650
651     case EncodedData::ClassZero:
652         return zero(Positive);
653
654     default:
655         ASSERT_NOT_REACHED();
656         return nan();
657     }
658 }
659
660 // Round toward negative infinity.
661 Decimal Decimal::floor() const
662 {
663     if (isSpecial())
664         return *this;
665
666     if (exponent() >= 0)
667         return *this;
668
669     uint64_t result = m_data.coefficient();
670     const int numberOfDigits = countDigits(result);
671     const int numberOfDropDigits = -exponent();
672     if (numberOfDigits < numberOfDropDigits)
673         return isPositive() ? zero(Positive) : Decimal(-1);
674
675     result = scaleDown(result, numberOfDropDigits);
676     if (isNegative() && !isMultiplePowersOfTen(m_data.coefficient(), numberOfDropDigits))
677         ++result;
678     return Decimal(sign(), 0, result);
679 }
680
681 Decimal Decimal::fromDouble(double doubleValue)
682 {
683     if (std::isfinite(doubleValue))
684         return fromString(String::numberToStringECMAScript(doubleValue));
685
686     if (std::isinf(doubleValue))
687         return infinity(doubleValue < 0 ? Negative : Positive);
688
689     return nan();
690 }
691
692 Decimal Decimal::fromString(const String& str)
693 {
694     int exponent = 0;
695     Sign exponentSign = Positive;
696     int numberOfDigits = 0;
697     int numberOfDigitsAfterDot = 0;
698     int numberOfExtraDigits = 0;
699     Sign sign = Positive;
700
701     enum {
702         StateDigit,
703         StateDot,
704         StateDotDigit,
705         StateE,
706         StateEDigit,
707         StateESign,
708         StateSign,
709         StateStart,
710         StateZero,
711     } state = StateStart;
712
713 #define HandleCharAndBreak(expected, nextState) \
714     if (ch == expected) { \
715         state = nextState; \
716         break; \
717     }
718
719 #define HandleTwoCharsAndBreak(expected1, expected2, nextState) \
720     if (ch == expected1 || ch == expected2) { \
721         state = nextState; \
722         break; \
723     }
724
725     uint64_t accumulator = 0;
726     for (unsigned index = 0; index < str.length(); ++index) {
727         const int ch = str[index];
728         switch (state) {
729         case StateDigit:
730             if (ch >= '0' && ch <= '9') {
731                 if (numberOfDigits < Precision) {
732                     ++numberOfDigits;
733                     accumulator *= 10;
734                     accumulator += ch - '0';
735                 } else {
736                     ++numberOfExtraDigits;
737                 }
738                 break;
739             }
740
741             HandleCharAndBreak('.', StateDot);
742             HandleTwoCharsAndBreak('E', 'e', StateE);
743             return nan();
744
745         case StateDot:
746         case StateDotDigit:
747             if (ch >= '0' && ch <= '9') {
748                 if (numberOfDigits < Precision) {
749                     ++numberOfDigits;
750                     ++numberOfDigitsAfterDot;
751                     accumulator *= 10;
752                     accumulator += ch - '0';
753                 }
754                 state = StateDotDigit;
755                 break;
756             }
757
758             HandleTwoCharsAndBreak('E', 'e', StateE);
759             return nan();
760
761         case StateE:
762             if (ch == '+') {
763                 exponentSign = Positive;
764                 state = StateESign;
765                 break;
766             }
767
768             if (ch == '-') {
769                 exponentSign = Negative;
770                 state = StateESign;
771                 break;
772             }
773
774             if (ch >= '0' && ch <= '9') {
775                 exponent = ch - '0';
776                 state = StateEDigit;
777                 break;
778             }
779
780             return nan();
781
782         case StateEDigit:
783             if (ch >= '0' && ch <= '9') {
784                 exponent *= 10;
785                 exponent += ch - '0';
786                 if (exponent > ExponentMax + Precision) {
787                     if (accumulator)
788                         return exponentSign == Negative ? zero(Positive) : infinity(sign);
789                     return zero(sign);
790                 }
791                 state = StateEDigit;
792                 break;
793             }
794
795             return nan();
796
797         case StateESign:
798             if (ch >= '0' && ch <= '9') {
799                 exponent = ch - '0';
800                 state = StateEDigit;
801                 break;
802             }
803
804             return nan();
805
806         case StateSign:
807             if (ch >= '1' && ch <= '9') {
808                 accumulator = ch - '0';
809                 numberOfDigits = 1;
810                 state = StateDigit;
811                 break;
812             }
813
814             HandleCharAndBreak('0', StateZero);
815             return nan();
816
817         case StateStart:
818             if (ch >= '1' && ch <= '9') {
819                 accumulator = ch - '0';
820                 numberOfDigits = 1;
821                 state = StateDigit;
822                 break;
823             }
824
825             if (ch == '-') {
826                 sign = Negative;
827                 state = StateSign;
828                 break;
829             }
830
831             if (ch == '+') {
832                 sign = Positive;
833                 state = StateSign;
834                 break;
835             }
836
837             HandleCharAndBreak('0', StateZero);
838             HandleCharAndBreak('.', StateDot);
839             return nan();
840
841         case StateZero:
842             if (ch == '0')
843                 break;
844
845             if (ch >= '1' && ch <= '9') {
846                 accumulator = ch - '0';
847                 numberOfDigits = 1;
848                 state = StateDigit;
849                 break;
850             }
851
852             HandleCharAndBreak('.', StateDot);
853             HandleTwoCharsAndBreak('E', 'e', StateE);
854             return nan();
855
856         default:
857             ASSERT_NOT_REACHED();
858             return nan();
859         }
860     }
861
862     if (state == StateZero)
863         return zero(sign);
864
865     if (state == StateDigit || state == StateEDigit || state == StateDotDigit) {
866         int resultExponent = exponent * (exponentSign == Negative ? -1 : 1) - numberOfDigitsAfterDot + numberOfExtraDigits;
867         if (resultExponent < ExponentMin)
868             return zero(Positive);
869
870         const int overflow = resultExponent - ExponentMax + 1;
871         if (overflow > 0) {
872             if (overflow + numberOfDigits - numberOfDigitsAfterDot > Precision)
873                 return infinity(sign);
874             accumulator = scaleUp(accumulator, overflow);
875             resultExponent -= overflow;
876         }
877
878         return Decimal(sign, resultExponent, accumulator);
879     }
880
881     return nan();
882 }
883
884 Decimal Decimal::infinity(const Sign sign)
885 {
886     return Decimal(EncodedData(sign, EncodedData::ClassInfinity));
887 }
888
889 Decimal Decimal::nan()
890 {
891     return Decimal(EncodedData(Positive, EncodedData::ClassNaN));
892 }
893
894 Decimal Decimal::remainder(const Decimal& rhs) const
895 {
896     const Decimal quotient = *this / rhs;
897     return quotient.isSpecial() ? quotient : *this - (quotient.isNegative() ? quotient.ceil() : quotient.floor()) * rhs;
898 }
899
900 Decimal Decimal::round() const
901 {
902     if (isSpecial())
903         return *this;
904
905     if (exponent() >= 0)
906         return *this;
907
908     uint64_t result = m_data.coefficient();
909     const int numberOfDigits = countDigits(result);
910     const int numberOfDropDigits = -exponent();
911     if (numberOfDigits < numberOfDropDigits)
912         return zero(Positive);
913
914     result = scaleDown(result, numberOfDropDigits - 1);
915     if (result % 10 >= 5)
916         result += 10;
917     result /= 10;
918     return Decimal(sign(), 0, result);
919 }
920
921 double Decimal::toDouble() const
922 {
923     if (isFinite()) {
924         bool valid;
925         const double doubleValue = toString().toDouble(&valid);
926         return valid ? doubleValue : std::numeric_limits<double>::quiet_NaN();
927     }
928
929     if (isInfinity())
930         return isNegative() ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity();
931
932     return std::numeric_limits<double>::quiet_NaN();
933 }
934
935 String Decimal::toString() const
936 {
937     switch (m_data.formatClass()) {
938     case EncodedData::ClassInfinity:
939         return sign() ? "-Infinity" : "Infinity";
940
941     case EncodedData::ClassNaN:
942         return "NaN";
943
944     case EncodedData::ClassNormal:
945     case EncodedData::ClassZero:
946         break;
947
948     default:
949         ASSERT_NOT_REACHED();
950         return "";
951     }
952
953     StringBuilder builder;
954     if (sign())
955         builder.append('-');
956
957     int originalExponent = exponent();
958     uint64_t coefficient = m_data.coefficient();
959
960     if (originalExponent < 0) {
961         const int maxDigits = DBL_DIG;
962         uint64_t lastDigit = 0;
963         while (countDigits(coefficient) > maxDigits) {
964             lastDigit = coefficient % 10;
965             coefficient /= 10;
966             ++originalExponent;
967         }
968
969         if (lastDigit >= 5)
970             ++coefficient;
971
972         while (originalExponent < 0 && coefficient && !(coefficient % 10)) {
973             coefficient /= 10;
974             ++originalExponent;
975         }
976     }
977
978     const String digits = String::number(coefficient);
979     int coefficientLength = static_cast<int>(digits.length());
980     const int adjustedExponent = originalExponent + coefficientLength - 1;
981     if (originalExponent <= 0 && adjustedExponent >= -6) {
982         if (!originalExponent) {
983             builder.append(digits);
984             return builder.toString();
985         }
986
987         if (adjustedExponent >= 0) {
988             for (int i = 0; i < coefficientLength; ++i) {
989                 builder.append(digits[i]);
990                 if (i == adjustedExponent)
991                     builder.append('.');
992             }
993             return builder.toString();
994         }
995
996         builder.appendLiteral("0.");
997         for (int i = adjustedExponent + 1; i < 0; ++i)
998             builder.append('0');
999
1000         builder.append(digits);
1001
1002     } else {
1003         builder.append(digits[0]);
1004         while (coefficientLength >= 2 && digits[coefficientLength - 1] == '0')
1005             --coefficientLength;
1006         if (coefficientLength >= 2) {
1007             builder.append('.');
1008             for (int i = 1; i < coefficientLength; ++i)
1009                 builder.append(digits[i]);
1010         }
1011
1012         if (adjustedExponent) {
1013             builder.append(adjustedExponent < 0 ? "e" : "e+");
1014             builder.appendNumber(adjustedExponent);
1015         }
1016     }
1017     return builder.toString();
1018 }
1019
1020 Decimal Decimal::zero(Sign sign)
1021 {
1022     return Decimal(EncodedData(sign, EncodedData::ClassZero));
1023 }
1024
1025 } // namespace blink