]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/BigInteger.h
JavaScriptCore-1097.13.tar.gz
[apple/javascriptcore.git] / runtime / BigInteger.h
1 /*
2 * Copyright (C) 2011 Apple 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
6 * are met:
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.
12 *
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.
24 */
25
26 #ifndef BigInteger_h
27 #define BigInteger_h
28
29 #include <wtf/MathExtras.h>
30
31 namespace JSC {
32
33 // This is used in converting the integer part of a number to a string.
34 class BigInteger {
35 public:
36 BigInteger(double number)
37 {
38 ASSERT(isfinite(number) && !signbit(number));
39 ASSERT(number == floor(number));
40
41 bool sign;
42 int32_t exponent;
43 uint64_t mantissa;
44 decomposeDouble(number, sign, exponent, mantissa);
45 ASSERT(!sign && exponent >= 0);
46
47 int32_t zeroBits = exponent - 52;
48
49 if (zeroBits < 0) {
50 mantissa >>= -zeroBits;
51 zeroBits = 0;
52 }
53
54 while (zeroBits >= 32) {
55 m_values.append(0);
56 zeroBits -= 32;
57 }
58
59 // Left align the 53 bits of the mantissa within 96 bits.
60 uint32_t values[3];
61 values[0] = static_cast<uint32_t>(mantissa);
62 values[1] = static_cast<uint32_t>(mantissa >> 32);
63 values[2] = 0;
64 // Shift based on the remainder of the exponent.
65 if (zeroBits) {
66 values[2] = values[1] >> (32 - zeroBits);
67 values[1] = (values[1] << zeroBits) | (values[0] >> (32 - zeroBits));
68 values[0] = (values[0] << zeroBits);
69 }
70 m_values.append(values[0]);
71 m_values.append(values[1]);
72 m_values.append(values[2]);
73
74 // Canonicalize; remove all trailing zeros.
75 while (m_values.size() && !m_values.last())
76 m_values.removeLast();
77 }
78
79 uint32_t divide(uint32_t divisor)
80 {
81 uint32_t carry = 0;
82
83 for (size_t i = m_values.size(); i; ) {
84 --i;
85 uint64_t dividend = (static_cast<uint64_t>(carry) << 32) + static_cast<uint64_t>(m_values[i]);
86
87 uint64_t result = dividend / static_cast<uint64_t>(divisor);
88 ASSERT(result == static_cast<uint32_t>(result));
89 uint64_t remainder = dividend % static_cast<uint64_t>(divisor);
90 ASSERT(remainder == static_cast<uint32_t>(remainder));
91
92 m_values[i] = static_cast<uint32_t>(result);
93 carry = static_cast<uint32_t>(remainder);
94 }
95
96 // Canonicalize; remove all trailing zeros.
97 while (m_values.size() && !m_values.last())
98 m_values.removeLast();
99
100 return carry;
101 }
102
103 bool operator!() { return !m_values.size(); }
104
105 private:
106 Vector<uint32_t, 36> m_values;
107 };
108
109 }
110
111 #endif
112