]>
git.saurik.com Git - apple/javascriptcore.git/blob - runtime/BigInteger.h
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.
29 #include <wtf/MathExtras.h>
33 // This is used in converting the integer part of a number to a string.
36 BigInteger(double number
)
38 ASSERT(isfinite(number
) && !signbit(number
));
39 ASSERT(number
== floor(number
));
44 decomposeDouble(number
, sign
, exponent
, mantissa
);
45 ASSERT(!sign
&& exponent
>= 0);
47 int32_t zeroBits
= exponent
- 52;
50 mantissa
>>= -zeroBits
;
54 while (zeroBits
>= 32) {
59 // Left align the 53 bits of the mantissa within 96 bits.
61 values
[0] = static_cast<uint32_t>(mantissa
);
62 values
[1] = static_cast<uint32_t>(mantissa
>> 32);
64 // Shift based on the remainder of the exponent.
66 values
[2] = values
[1] >> (32 - zeroBits
);
67 values
[1] = (values
[1] << zeroBits
) | (values
[0] >> (32 - zeroBits
));
68 values
[0] = (values
[0] << zeroBits
);
70 m_values
.append(values
[0]);
71 m_values
.append(values
[1]);
72 m_values
.append(values
[2]);
74 // Canonicalize; remove all trailing zeros.
75 while (m_values
.size() && !m_values
.last())
76 m_values
.removeLast();
79 uint32_t divide(uint32_t divisor
)
83 for (size_t i
= m_values
.size(); i
; ) {
85 uint64_t dividend
= (static_cast<uint64_t>(carry
) << 32) + static_cast<uint64_t>(m_values
[i
]);
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
));
92 m_values
[i
] = static_cast<uint32_t>(result
);
93 carry
= static_cast<uint32_t>(remainder
);
96 // Canonicalize; remove all trailing zeros.
97 while (m_values
.size() && !m_values
.last())
98 m_values
.removeLast();
103 bool operator!() { return !m_values
.size(); }
106 Vector
<uint32_t, 36> m_values
;