| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include "unicode/utypes.h" |
| 35 |
#if !UCONFIG_NO_FORMATTING |
| 36 |
|
| 37 |
#ifndef DOUBLE_CONVERSION_BIGNUM_H_ |
| 38 |
#define DOUBLE_CONVERSION_BIGNUM_H_ |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#include "double-conversion-utils.h" |
| 43 |
|
| 44 |
|
| 45 |
U_NAMESPACE_BEGIN |
| 46 |
|
| 47 |
namespace double_conversion { |
| 48 |
|
| 49 |
class Bignum { |
| 50 |
public: |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static const int kMaxSignificantBits = 3584; |
| 55 |
|
| 56 |
Bignum() : used_bigits_(0), exponent_(0) {} |
| 57 |
|
| 58 |
void AssignUInt16(const uint16_t value); |
| 59 |
void AssignUInt64(uint64_t value); |
| 60 |
void AssignBignum(const Bignum& other); |
| 61 |
|
| 62 |
void AssignDecimalString(const Vector<const char> value); |
| 63 |
void AssignHexString(const Vector<const char> value); |
| 64 |
|
| 65 |
void AssignPowerUInt16(uint16_t base, const int exponent); |
| 66 |
|
| 67 |
void AddUInt64(const uint64_t operand); |
| 68 |
void AddBignum(const Bignum& other); |
| 69 |
|
| 70 |
void SubtractBignum(const Bignum& other); |
| 71 |
|
| 72 |
void Square(); |
| 73 |
void ShiftLeft(const int shift_amount); |
| 74 |
void MultiplyByUInt32(const uint32_t factor); |
| 75 |
void MultiplyByUInt64(const uint64_t factor); |
| 76 |
void MultiplyByPowerOfTen(const int exponent); |
| 77 |
void Times10() { return MultiplyByUInt32(10); } |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
uint16_t DivideModuloIntBignum(const Bignum& other); |
| 83 |
|
| 84 |
bool ToHexString(char* buffer, const int buffer_size) const; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static int Compare(const Bignum& a, const Bignum& b); |
| 91 |
static bool Equal(const Bignum& a, const Bignum& b) { |
| 92 |
return Compare(a, b) == 0; |
| 93 |
} |
| 94 |
static bool LessEqual(const Bignum& a, const Bignum& b) { |
| 95 |
return Compare(a, b) <= 0; |
| 96 |
} |
| 97 |
static bool Less(const Bignum& a, const Bignum& b) { |
| 98 |
return Compare(a, b) < 0; |
| 99 |
} |
| 100 |
|
| 101 |
static int PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c); |
| 102 |
|
| 103 |
static bool PlusEqual(const Bignum& a, const Bignum& b, const Bignum& c) { |
| 104 |
return PlusCompare(a, b, c) == 0; |
| 105 |
} |
| 106 |
|
| 107 |
static bool PlusLessEqual(const Bignum& a, const Bignum& b, const Bignum& c) { |
| 108 |
return PlusCompare(a, b, c) <= 0; |
| 109 |
} |
| 110 |
|
| 111 |
static bool PlusLess(const Bignum& a, const Bignum& b, const Bignum& c) { |
| 112 |
return PlusCompare(a, b, c) < 0; |
| 113 |
} |
| 114 |
private: |
| 115 |
typedef uint32_t Chunk; |
| 116 |
typedef uint64_t DoubleChunk; |
| 117 |
|
| 118 |
static const int kChunkSize = sizeof(Chunk) * 8; |
| 119 |
static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8; |
| 120 |
|
| 121 |
|
| 122 |
static const int kBigitSize = 28; |
| 123 |
static const Chunk kBigitMask = (1 << kBigitSize) - 1; |
| 124 |
|
| 125 |
|
| 126 |
static const int kBigitCapacity = kMaxSignificantBits / kBigitSize; |
| 127 |
|
| 128 |
static void EnsureCapacity(const int size) { |
| 129 |
if (size > kBigitCapacity) { |
| 130 |
DOUBLE_CONVERSION_UNREACHABLE(); |
| 131 |
} |
| 132 |
} |
| 133 |
void Align(const Bignum& other); |
| 134 |
void Clamp(); |
| 135 |
bool IsClamped() const { |
| 136 |
return used_bigits_ == 0 || RawBigit(used_bigits_ - 1) != 0; |
| 137 |
} |
| 138 |
void Zero() { |
| 139 |
used_bigits_ = 0; |
| 140 |
exponent_ = 0; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
void BigitsShiftLeft(const int shift_amount); |
| 146 |
|
| 147 |
int BigitLength() const { return used_bigits_ + exponent_; } |
| 148 |
Chunk& RawBigit(const int index); |
| 149 |
const Chunk& RawBigit(const int index) const; |
| 150 |
Chunk BigitOrZero(const int index) const; |
| 151 |
void SubtractTimes(const Bignum& other, const int factor); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
int16_t used_bigits_; |
| 158 |
int16_t exponent_; |
| 159 |
Chunk bigits_buffer_[kBigitCapacity]; |
| 160 |
|
| 161 |
DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Bignum); |
| 162 |
}; |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
U_NAMESPACE_END |
| 168 |
|
| 169 |
#endif |
| 170 |
#endif |