Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: runtime/third_party/double-conversion/src/bignum.h

Issue 8632010: double-conversion drop. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef DOUBLE_CONVERSION_BIGNUM_H_
29 #define DOUBLE_CONVERSION_BIGNUM_H_
30
31 #include "utils.h"
32
33 namespace double_conversion {
34
35 class Bignum {
36 public:
37 // 3584 = 128 * 28. We can represent 2^3584 > 10^1000 accurately.
38 // This bignum can encode much bigger numbers, since it contains an
39 // exponent.
40 static const int kMaxSignificantBits = 3584;
41
42 Bignum();
43 void AssignUInt16(uint16_t value);
44 void AssignUInt64(uint64_t value);
45 void AssignBignum(const Bignum& other);
46
47 void AssignDecimalString(Vector<const char> value);
48 void AssignHexString(Vector<const char> value);
49
50 void AssignPowerUInt16(uint16_t base, int exponent);
51
52 void AddUInt16(uint16_t operand);
53 void AddUInt64(uint64_t operand);
54 void AddBignum(const Bignum& other);
55 // Precondition: this >= other.
56 void SubtractBignum(const Bignum& other);
57
58 void Square();
59 void ShiftLeft(int shift_amount);
60 void MultiplyByUInt32(uint32_t factor);
61 void MultiplyByUInt64(uint64_t factor);
62 void MultiplyByPowerOfTen(int exponent);
63 void Times10() { return MultiplyByUInt32(10); }
64 // Pseudocode:
65 // int result = this / other;
66 // this = this % other;
67 // In the worst case this function is in O(this/other).
68 uint16_t DivideModuloIntBignum(const Bignum& other);
69
70 bool ToHexString(char* buffer, int buffer_size) const;
71
72 static int Compare(const Bignum& a, const Bignum& b);
73 static bool Equal(const Bignum& a, const Bignum& b) {
74 return Compare(a, b) == 0;
75 }
76 static bool LessEqual(const Bignum& a, const Bignum& b) {
77 return Compare(a, b) <= 0;
78 }
79 static bool Less(const Bignum& a, const Bignum& b) {
80 return Compare(a, b) < 0;
81 }
82 // Returns Compare(a + b, c);
83 static int PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c);
84 // Returns a + b == c
85 static bool PlusEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
86 return PlusCompare(a, b, c) == 0;
87 }
88 // Returns a + b <= c
89 static bool PlusLessEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
90 return PlusCompare(a, b, c) <= 0;
91 }
92 // Returns a + b < c
93 static bool PlusLess(const Bignum& a, const Bignum& b, const Bignum& c) {
94 return PlusCompare(a, b, c) < 0;
95 }
96 private:
97 typedef uint32_t Chunk;
98 typedef uint64_t DoubleChunk;
99
100 static const int kChunkSize = sizeof(Chunk) * 8;
101 static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8;
102 // With bigit size of 28 we loose some bits, but a double still fits easily
103 // into two chunks, and more importantly we can use the Comba multiplication.
104 static const int kBigitSize = 28;
105 static const Chunk kBigitMask = (1 << kBigitSize) - 1;
106 // Every instance allocates kBigitLength chunks on the stack. Bignums cannot
107 // grow. There are no checks if the stack-allocated space is sufficient.
108 static const int kBigitCapacity = kMaxSignificantBits / kBigitSize;
109
110 void EnsureCapacity(int size) {
111 if (size > kBigitCapacity) {
112 UNREACHABLE();
113 }
114 }
115 void Align(const Bignum& other);
116 void Clamp();
117 bool IsClamped() const;
118 void Zero();
119 // Requires this to have enough capacity (no tests done).
120 // Updates used_digits_ if necessary.
121 // shift_amount must be < kBigitSize.
122 void BigitsShiftLeft(int shift_amount);
123 // BigitLength includes the "hidden" digits encoded in the exponent.
124 int BigitLength() const { return used_digits_ + exponent_; }
125 Chunk BigitAt(int index) const;
126 void SubtractTimes(const Bignum& other, int factor);
127
128 Chunk bigits_buffer_[kBigitCapacity];
129 // A vector backed by bigits_buffer_. This way accesses to the array are
130 // checked for out-of-bounds errors.
131 Vector<Chunk> bigits_;
132 int used_digits_;
133 // The Bignum's value equals value(bigits_) * 2^(exponent_ * kBigitSize).
134 int exponent_;
135
136 DISALLOW_COPY_AND_ASSIGN(Bignum);
137 };
138
139 } // namespace double_conversion
140
141 #endif // DOUBLE_CONVERSION_BIGNUM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698