bignum.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef DOUBLE_CONVERSION_BIGNUM_H_
  28. #define DOUBLE_CONVERSION_BIGNUM_H_
  29. #include "utils.h"
  30. namespace double_conversion {
  31. class Bignum {
  32. public:
  33. // 3584 = 128 * 28. We can represent 2^3584 > 10^1000 accurately.
  34. // This bignum can encode much bigger numbers, since it contains an
  35. // exponent.
  36. static const int kMaxSignificantBits = 3584;
  37. Bignum() : used_bigits_(0), exponent_(0) {}
  38. void AssignUInt16(const uint16_t value);
  39. void AssignUInt64(uint64_t value);
  40. void AssignBignum(const Bignum& other);
  41. void AssignDecimalString(const Vector<const char> value);
  42. void AssignHexString(const Vector<const char> value);
  43. void AssignPowerUInt16(uint16_t base, const int exponent);
  44. void AddUInt64(const uint64_t operand);
  45. void AddBignum(const Bignum& other);
  46. // Precondition: this >= other.
  47. void SubtractBignum(const Bignum& other);
  48. void Square();
  49. void ShiftLeft(const int shift_amount);
  50. void MultiplyByUInt32(const uint32_t factor);
  51. void MultiplyByUInt64(const uint64_t factor);
  52. void MultiplyByPowerOfTen(const int exponent);
  53. void Times10() { return MultiplyByUInt32(10); }
  54. // Pseudocode:
  55. // int result = this / other;
  56. // this = this % other;
  57. // In the worst case this function is in O(this/other).
  58. uint16_t DivideModuloIntBignum(const Bignum& other);
  59. bool ToHexString(char* buffer, const int buffer_size) const;
  60. // Returns
  61. // -1 if a < b,
  62. // 0 if a == b, and
  63. // +1 if a > b.
  64. static int Compare(const Bignum& a, const Bignum& b);
  65. static bool Equal(const Bignum& a, const Bignum& b) {
  66. return Compare(a, b) == 0;
  67. }
  68. static bool LessEqual(const Bignum& a, const Bignum& b) {
  69. return Compare(a, b) <= 0;
  70. }
  71. static bool Less(const Bignum& a, const Bignum& b) {
  72. return Compare(a, b) < 0;
  73. }
  74. // Returns Compare(a + b, c);
  75. static int PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c);
  76. // Returns a + b == c
  77. static bool PlusEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
  78. return PlusCompare(a, b, c) == 0;
  79. }
  80. // Returns a + b <= c
  81. static bool PlusLessEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
  82. return PlusCompare(a, b, c) <= 0;
  83. }
  84. // Returns a + b < c
  85. static bool PlusLess(const Bignum& a, const Bignum& b, const Bignum& c) {
  86. return PlusCompare(a, b, c) < 0;
  87. }
  88. private:
  89. typedef uint32_t Chunk;
  90. typedef uint64_t DoubleChunk;
  91. static const int kChunkSize = sizeof(Chunk) * 8;
  92. static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8;
  93. // With bigit size of 28 we loose some bits, but a double still fits easily
  94. // into two chunks, and more importantly we can use the Comba multiplication.
  95. static const int kBigitSize = 28;
  96. static const Chunk kBigitMask = (1 << kBigitSize) - 1;
  97. // Every instance allocates kBigitLength chunks on the stack. Bignums cannot
  98. // grow. There are no checks if the stack-allocated space is sufficient.
  99. static const int kBigitCapacity = kMaxSignificantBits / kBigitSize;
  100. static void EnsureCapacity(const int size) {
  101. if (size > kBigitCapacity) {
  102. DOUBLE_CONVERSION_UNREACHABLE();
  103. }
  104. }
  105. void Align(const Bignum& other);
  106. void Clamp();
  107. bool IsClamped() const {
  108. return used_bigits_ == 0 || RawBigit(used_bigits_ - 1) != 0;
  109. }
  110. void Zero() {
  111. used_bigits_ = 0;
  112. exponent_ = 0;
  113. }
  114. // Requires this to have enough capacity (no tests done).
  115. // Updates used_bigits_ if necessary.
  116. // shift_amount must be < kBigitSize.
  117. void BigitsShiftLeft(const int shift_amount);
  118. // BigitLength includes the "hidden" bigits encoded in the exponent.
  119. int BigitLength() const { return used_bigits_ + exponent_; }
  120. Chunk& RawBigit(const int index);
  121. const Chunk& RawBigit(const int index) const;
  122. Chunk BigitOrZero(const int index) const;
  123. void SubtractTimes(const Bignum& other, const int factor);
  124. // The Bignum's value is value(bigits_buffer_) * 2^(exponent_ * kBigitSize),
  125. // where the value of the buffer consists of the lower kBigitSize bits of
  126. // the first used_bigits_ Chunks in bigits_buffer_, first chunk has lowest
  127. // significant bits.
  128. int16_t used_bigits_;
  129. int16_t exponent_;
  130. Chunk bigits_buffer_[kBigitCapacity];
  131. DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Bignum);
  132. };
  133. } // namespace double_conversion
  134. #endif // DOUBLE_CONVERSION_BIGNUM_H_