All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bignum.h
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 #include "pxr/pxr.h"
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
37 namespace pxr_double_conversion {
38 
39 class Bignum {
40  public:
41  // 3584 = 128 * 28. We can represent 2^3584 > 10^1000 accurately.
42  // This bignum can encode much bigger numbers, since it contains an
43  // exponent.
44  static const int kMaxSignificantBits = 3584;
45 
46  Bignum();
47  void AssignUInt16(uint16_t value);
48  void AssignUInt64(uint64_t value);
49  void AssignBignum(const Bignum& other);
50 
51  void AssignDecimalString(Vector<const char> value);
52  void AssignHexString(Vector<const char> value);
53 
54  void AssignPowerUInt16(uint16_t base, int exponent);
55 
56  void AddUInt64(uint64_t operand);
57  void AddBignum(const Bignum& other);
58  // Precondition: this >= other.
59  void SubtractBignum(const Bignum& other);
60 
61  void Square();
62  void ShiftLeft(int shift_amount);
63  void MultiplyByUInt32(uint32_t factor);
64  void MultiplyByUInt64(uint64_t factor);
65  void MultiplyByPowerOfTen(int exponent);
66  void Times10() { return MultiplyByUInt32(10); }
67  // Pseudocode:
68  // int result = this / other;
69  // this = this % other;
70  // In the worst case this function is in O(this/other).
71  uint16_t DivideModuloIntBignum(const Bignum& other);
72 
73  bool ToHexString(char* buffer, int buffer_size) const;
74 
75  // Returns
76  // -1 if a < b,
77  // 0 if a == b, and
78  // +1 if a > b.
79  static int Compare(const Bignum& a, const Bignum& b);
80  static bool Equal(const Bignum& a, const Bignum& b) {
81  return Compare(a, b) == 0;
82  }
83  static bool LessEqual(const Bignum& a, const Bignum& b) {
84  return Compare(a, b) <= 0;
85  }
86  static bool Less(const Bignum& a, const Bignum& b) {
87  return Compare(a, b) < 0;
88  }
89  // Returns Compare(a + b, c);
90  static int PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c);
91  // Returns a + b == c
92  static bool PlusEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
93  return PlusCompare(a, b, c) == 0;
94  }
95  // Returns a + b <= c
96  static bool PlusLessEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
97  return PlusCompare(a, b, c) <= 0;
98  }
99  // Returns a + b < c
100  static bool PlusLess(const Bignum& a, const Bignum& b, const Bignum& c) {
101  return PlusCompare(a, b, c) < 0;
102  }
103  private:
104  typedef uint32_t Chunk;
105  typedef uint64_t DoubleChunk;
106 
107  static const int kChunkSize = sizeof(Chunk) * 8;
108  static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8;
109  // With bigit size of 28 we loose some bits, but a double still fits easily
110  // into two chunks, and more importantly we can use the Comba multiplication.
111  static const int kBigitSize = 28;
112  static const Chunk kBigitMask = (1 << kBigitSize) - 1;
113  // Every instance allocates kBigitLength chunks on the stack. Bignums cannot
114  // grow. There are no checks if the stack-allocated space is sufficient.
115  static const int kBigitCapacity = kMaxSignificantBits / kBigitSize;
116 
117  void EnsureCapacity(int size) {
118  if (size > kBigitCapacity) {
119  UNREACHABLE();
120  }
121  }
122  void Align(const Bignum& other);
123  void Clamp();
124  bool IsClamped() const;
125  void Zero();
126  // Requires this to have enough capacity (no tests done).
127  // Updates used_digits_ if necessary.
128  // shift_amount must be < kBigitSize.
129  void BigitsShiftLeft(int shift_amount);
130  // BigitLength includes the "hidden" digits encoded in the exponent.
131  int BigitLength() const { return used_digits_ + exponent_; }
132  Chunk BigitAt(int index) const;
133  void SubtractTimes(const Bignum& other, int factor);
134 
135  Chunk bigits_buffer_[kBigitCapacity];
136  // A vector backed by bigits_buffer_. This way accesses to the array are
137  // checked for out-of-bounds errors.
138  Vector<Chunk> bigits_;
139  int used_digits_;
140  // The Bignum's value equals value(bigits_) * 2^(exponent_ * kBigitSize).
141  int exponent_;
142 
143  DISALLOW_COPY_AND_ASSIGN(Bignum);
144 };
145 
146 } // namespace pxr_double_conversion
147 
148 PXR_NAMESPACE_CLOSE_SCOPE
149 
150 #endif // DOUBLE_CONVERSION_BIGNUM_H_