2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <wtf/MathExtras.h>
32 // This is used in converting the integer part of a number to a string.
35 BigInteger(double number)
37 ASSERT(std::isfinite(number) && !std::signbit(number));
38 ASSERT(number == floor(number));
43 decomposeDouble(number, sign, exponent, mantissa);
44 ASSERT(!sign && exponent >= 0);
46 int32_t zeroBits = exponent - 52;
49 mantissa >>= -zeroBits;
53 while (zeroBits >= 32) {
58 // Left align the 53 bits of the mantissa within 96 bits.
60 values[0] = static_cast<uint32_t>(mantissa);
61 values[1] = static_cast<uint32_t>(mantissa >> 32);
63 // Shift based on the remainder of the exponent.
65 values[2] = values[1] >> (32 - zeroBits);
66 values[1] = (values[1] << zeroBits) | (values[0] >> (32 - zeroBits));
67 values[0] = (values[0] << zeroBits);
69 m_values.append(values[0]);
70 m_values.append(values[1]);
71 m_values.append(values[2]);
73 // Canonicalize; remove all trailing zeros.
74 while (m_values.size() && !m_values.last())
75 m_values.removeLast();
78 uint32_t divide(uint32_t divisor)
82 for (size_t i = m_values.size(); i; ) {
84 uint64_t dividend = (static_cast<uint64_t>(carry) << 32) + static_cast<uint64_t>(m_values[i]);
86 uint64_t result = dividend / static_cast<uint64_t>(divisor);
87 ASSERT(result == static_cast<uint32_t>(result));
88 uint64_t remainder = dividend % static_cast<uint64_t>(divisor);
89 ASSERT(remainder == static_cast<uint32_t>(remainder));
91 m_values[i] = static_cast<uint32_t>(result);
92 carry = static_cast<uint32_t>(remainder);
95 // Canonicalize; remove all trailing zeros.
96 while (m_values.size() && !m_values.last())
97 m_values.removeLast();
102 bool operator!() { return !m_values.size(); }
105 Vector<uint32_t, 36> m_values;