double-conversion-bignum.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. //
  4. // From the double-conversion library. Original license:
  5. //
  6. // Copyright 2010 the V8 project authors. All rights reserved.
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following
  15. // disclaimer in the documentation and/or other materials provided
  16. // with the distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
  33. #include "unicode/utypes.h"
  34. #if !UCONFIG_NO_FORMATTING
  35. #ifndef DOUBLE_CONVERSION_BIGNUM_H_
  36. #define DOUBLE_CONVERSION_BIGNUM_H_
  37. // ICU PATCH: Customize header file paths for ICU.
  38. #include "double-conversion-utils.h"
  39. // ICU PATCH: Wrap in ICU namespace
  40. U_NAMESPACE_BEGIN
  41. namespace double_conversion {
  42. class Bignum {
  43. public:
  44. // 3584 = 128 * 28. We can represent 2^3584 > 10^1000 accurately.
  45. // This bignum can encode much bigger numbers, since it contains an
  46. // exponent.
  47. static const int kMaxSignificantBits = 3584;
  48. Bignum() : used_bigits_(0), exponent_(0) {}
  49. void AssignUInt16(const uint16_t value);
  50. void AssignUInt64(uint64_t value);
  51. void AssignBignum(const Bignum& other);
  52. void AssignDecimalString(const Vector<const char> value);
  53. void AssignHexString(const Vector<const char> value);
  54. void AssignPowerUInt16(uint16_t base, const int exponent);
  55. void AddUInt64(const uint64_t operand);
  56. void AddBignum(const Bignum& other);
  57. // Precondition: this >= other.
  58. void SubtractBignum(const Bignum& other);
  59. void Square();
  60. void ShiftLeft(const int shift_amount);
  61. void MultiplyByUInt32(const uint32_t factor);
  62. void MultiplyByUInt64(const uint64_t factor);
  63. void MultiplyByPowerOfTen(const int exponent);
  64. void Times10() { return MultiplyByUInt32(10); }
  65. // Pseudocode:
  66. // int result = this / other;
  67. // this = this % other;
  68. // In the worst case this function is in O(this/other).
  69. uint16_t DivideModuloIntBignum(const Bignum& other);
  70. bool ToHexString(char* buffer, const int buffer_size) const;
  71. // Returns
  72. // -1 if a < b,
  73. // 0 if a == b, and
  74. // +1 if a > b.
  75. static int Compare(const Bignum& a, const Bignum& b);
  76. static bool Equal(const Bignum& a, const Bignum& b) {
  77. return Compare(a, b) == 0;
  78. }
  79. static bool LessEqual(const Bignum& a, const Bignum& b) {
  80. return Compare(a, b) <= 0;
  81. }
  82. static bool Less(const Bignum& a, const Bignum& b) {
  83. return Compare(a, b) < 0;
  84. }
  85. // Returns Compare(a + b, c);
  86. static int PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c);
  87. // Returns a + b == c
  88. static bool PlusEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
  89. return PlusCompare(a, b, c) == 0;
  90. }
  91. // Returns a + b <= c
  92. static bool PlusLessEqual(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 PlusLess(const Bignum& a, const Bignum& b, const Bignum& c) {
  97. return PlusCompare(a, b, c) < 0;
  98. }
  99. private:
  100. typedef uint32_t Chunk;
  101. typedef uint64_t DoubleChunk;
  102. static const int kChunkSize = sizeof(Chunk) * 8;
  103. static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8;
  104. // With bigit size of 28 we loose some bits, but a double still fits easily
  105. // into two chunks, and more importantly we can use the Comba multiplication.
  106. static const int kBigitSize = 28;
  107. static const Chunk kBigitMask = (1 << kBigitSize) - 1;
  108. // Every instance allocates kBigitLength chunks on the stack. Bignums cannot
  109. // grow. There are no checks if the stack-allocated space is sufficient.
  110. static const int kBigitCapacity = kMaxSignificantBits / kBigitSize;
  111. static void EnsureCapacity(const int size) {
  112. if (size > kBigitCapacity) {
  113. DOUBLE_CONVERSION_UNREACHABLE();
  114. }
  115. }
  116. void Align(const Bignum& other);
  117. void Clamp();
  118. bool IsClamped() const {
  119. return used_bigits_ == 0 || RawBigit(used_bigits_ - 1) != 0;
  120. }
  121. void Zero() {
  122. used_bigits_ = 0;
  123. exponent_ = 0;
  124. }
  125. // Requires this to have enough capacity (no tests done).
  126. // Updates used_bigits_ if necessary.
  127. // shift_amount must be < kBigitSize.
  128. void BigitsShiftLeft(const int shift_amount);
  129. // BigitLength includes the "hidden" bigits encoded in the exponent.
  130. int BigitLength() const { return used_bigits_ + exponent_; }
  131. Chunk& RawBigit(const int index);
  132. const Chunk& RawBigit(const int index) const;
  133. Chunk BigitOrZero(const int index) const;
  134. void SubtractTimes(const Bignum& other, const int factor);
  135. // The Bignum's value is value(bigits_buffer_) * 2^(exponent_ * kBigitSize),
  136. // where the value of the buffer consists of the lower kBigitSize bits of
  137. // the first used_bigits_ Chunks in bigits_buffer_, first chunk has lowest
  138. // significant bits.
  139. int16_t used_bigits_;
  140. int16_t exponent_;
  141. Chunk bigits_buffer_[kBigitCapacity];
  142. DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Bignum);
  143. };
  144. } // namespace double_conversion
  145. // ICU PATCH: Close ICU namespace
  146. U_NAMESPACE_END
  147. #endif // DOUBLE_CONVERSION_BIGNUM_H_
  148. #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING