File indexing completed on 2025-01-18 09:55:27
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #ifndef DOUBLE_CONVERSION_BIGNUM_H_
0029 #define DOUBLE_CONVERSION_BIGNUM_H_
0030
0031 #include "utils.h"
0032
0033 namespace double_conversion {
0034
0035 class Bignum {
0036 public:
0037
0038
0039
0040 static const int kMaxSignificantBits = 3584;
0041
0042 Bignum() : used_bigits_(0), exponent_(0) {}
0043
0044 void AssignUInt16(const uint16_t value);
0045 void AssignUInt64(uint64_t value);
0046 void AssignBignum(const Bignum& other);
0047
0048 void AssignDecimalString(const Vector<const char> value);
0049 void AssignHexString(const Vector<const char> value);
0050
0051 void AssignPowerUInt16(uint16_t base, const int exponent);
0052
0053 void AddUInt64(const uint64_t operand);
0054 void AddBignum(const Bignum& other);
0055
0056 void SubtractBignum(const Bignum& other);
0057
0058 void Square();
0059 void ShiftLeft(const int shift_amount);
0060 void MultiplyByUInt32(const uint32_t factor);
0061 void MultiplyByUInt64(const uint64_t factor);
0062 void MultiplyByPowerOfTen(const int exponent);
0063 void Times10() { return MultiplyByUInt32(10); }
0064
0065
0066
0067
0068 uint16_t DivideModuloIntBignum(const Bignum& other);
0069
0070 bool ToHexString(char* buffer, const int buffer_size) const;
0071
0072
0073
0074
0075
0076 static int Compare(const Bignum& a, const Bignum& b);
0077 static bool Equal(const Bignum& a, const Bignum& b) {
0078 return Compare(a, b) == 0;
0079 }
0080 static bool LessEqual(const Bignum& a, const Bignum& b) {
0081 return Compare(a, b) <= 0;
0082 }
0083 static bool Less(const Bignum& a, const Bignum& b) {
0084 return Compare(a, b) < 0;
0085 }
0086
0087 static int PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c);
0088
0089 static bool PlusEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
0090 return PlusCompare(a, b, c) == 0;
0091 }
0092
0093 static bool PlusLessEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
0094 return PlusCompare(a, b, c) <= 0;
0095 }
0096
0097 static bool PlusLess(const Bignum& a, const Bignum& b, const Bignum& c) {
0098 return PlusCompare(a, b, c) < 0;
0099 }
0100 private:
0101 typedef uint32_t Chunk;
0102 typedef uint64_t DoubleChunk;
0103
0104 static const int kChunkSize = sizeof(Chunk) * 8;
0105 static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8;
0106
0107
0108 static const int kBigitSize = 28;
0109 static const Chunk kBigitMask = (1 << kBigitSize) - 1;
0110
0111
0112 static const int kBigitCapacity = kMaxSignificantBits / kBigitSize;
0113
0114 static void EnsureCapacity(const int size) {
0115 if (size > kBigitCapacity) {
0116 DOUBLE_CONVERSION_UNREACHABLE();
0117 }
0118 }
0119 void Align(const Bignum& other);
0120 void Clamp();
0121 bool IsClamped() const {
0122 return used_bigits_ == 0 || RawBigit(used_bigits_ - 1) != 0;
0123 }
0124 void Zero() {
0125 used_bigits_ = 0;
0126 exponent_ = 0;
0127 }
0128
0129
0130
0131 void BigitsShiftLeft(const int shift_amount);
0132
0133 int BigitLength() const { return used_bigits_ + exponent_; }
0134 Chunk& RawBigit(const int index);
0135 const Chunk& RawBigit(const int index) const;
0136 Chunk BigitOrZero(const int index) const;
0137 void SubtractTimes(const Bignum& other, const int factor);
0138
0139
0140
0141
0142
0143 int16_t used_bigits_;
0144 int16_t exponent_;
0145 Chunk bigits_buffer_[kBigitCapacity];
0146
0147 DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Bignum);
0148 };
0149
0150 }
0151
0152 #endif