00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_
00016 #define ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_
00017
00018 #include <algorithm>
00019 #include <cstdint>
00020 #include <iostream>
00021 #include <string>
00022
00023 #include "absl/strings/ascii.h"
00024 #include "absl/strings/internal/charconv_parse.h"
00025 #include "absl/strings/string_view.h"
00026
00027 namespace absl {
00028 namespace strings_internal {
00029
00030
00031 constexpr int kMaxSmallPowerOfFive = 13;
00032
00033 constexpr int kMaxSmallPowerOfTen = 9;
00034
00035 extern const uint32_t kFiveToNth[kMaxSmallPowerOfFive + 1];
00036 extern const uint32_t kTenToNth[kMaxSmallPowerOfTen + 1];
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 template <int max_words>
00054 class BigUnsigned {
00055 public:
00056 static_assert(max_words == 4 || max_words == 84,
00057 "unsupported max_words value");
00058
00059 BigUnsigned() : size_(0), words_{} {}
00060 explicit constexpr BigUnsigned(uint64_t v)
00061 : size_((v >> 32) ? 2 : v ? 1 : 0),
00062 words_{static_cast<uint32_t>(v & 0xffffffffu),
00063 static_cast<uint32_t>(v >> 32)} {}
00064
00065
00066
00067
00068 explicit BigUnsigned(absl::string_view sv) : size_(0), words_{} {
00069
00070
00071 if (std::find_if_not(sv.begin(), sv.end(), ascii_isdigit) != sv.end() ||
00072 sv.empty()) {
00073 return;
00074 }
00075 int exponent_adjust =
00076 ReadDigits(sv.data(), sv.data() + sv.size(), Digits10() + 1);
00077 if (exponent_adjust > 0) {
00078 MultiplyByTenToTheNth(exponent_adjust);
00079 }
00080 }
00081
00082
00083
00084
00085
00086 int ReadFloatMantissa(const ParsedFloat& fp, int significant_digits);
00087
00088
00089
00090
00091
00092
00093 static constexpr int Digits10() {
00094
00095 return static_cast<uint64_t>(max_words) * 9975007 / 1035508;
00096 }
00097
00098
00099 void ShiftLeft(int count) {
00100 if (count > 0) {
00101 const int word_shift = count / 32;
00102 if (word_shift >= max_words) {
00103 SetToZero();
00104 return;
00105 }
00106 size_ = (std::min)(size_ + word_shift, max_words);
00107 count %= 32;
00108 if (count == 0) {
00109 std::copy_backward(words_, words_ + size_ - word_shift, words_ + size_);
00110 } else {
00111 for (int i = (std::min)(size_, max_words - 1); i > word_shift; --i) {
00112 words_[i] = (words_[i - word_shift] << count) |
00113 (words_[i - word_shift - 1] >> (32 - count));
00114 }
00115 words_[word_shift] = words_[0] << count;
00116
00117 if (size_ < max_words && words_[size_]) {
00118 ++size_;
00119 }
00120 }
00121 std::fill(words_, words_ + word_shift, 0u);
00122 }
00123 }
00124
00125
00126
00127 void MultiplyBy(uint32_t v) {
00128 if (size_ == 0 || v == 1) {
00129 return;
00130 }
00131 if (v == 0) {
00132 SetToZero();
00133 return;
00134 }
00135 const uint64_t factor = v;
00136 uint64_t window = 0;
00137 for (int i = 0; i < size_; ++i) {
00138 window += factor * words_[i];
00139 words_[i] = window & 0xffffffff;
00140 window >>= 32;
00141 }
00142
00143 if (window && size_ < max_words) {
00144 words_[size_] = window & 0xffffffff;
00145 ++size_;
00146 }
00147 }
00148
00149 void MultiplyBy(uint64_t v) {
00150 uint32_t words[2];
00151 words[0] = static_cast<uint32_t>(v);
00152 words[1] = static_cast<uint32_t>(v >> 32);
00153 if (words[1] == 0) {
00154 MultiplyBy(words[0]);
00155 } else {
00156 MultiplyBy(2, words);
00157 }
00158 }
00159
00160
00161 void MultiplyByFiveToTheNth(int n) {
00162 while (n >= kMaxSmallPowerOfFive) {
00163 MultiplyBy(kFiveToNth[kMaxSmallPowerOfFive]);
00164 n -= kMaxSmallPowerOfFive;
00165 }
00166 if (n > 0) {
00167 MultiplyBy(kFiveToNth[n]);
00168 }
00169 }
00170
00171
00172 void MultiplyByTenToTheNth(int n) {
00173 if (n > kMaxSmallPowerOfTen) {
00174
00175
00176 MultiplyByFiveToTheNth(n);
00177 ShiftLeft(n);
00178 } else if (n > 0) {
00179
00180
00181 MultiplyBy(kTenToNth[n]);
00182 }
00183 }
00184
00185
00186
00187
00188 static BigUnsigned FiveToTheNth(int n);
00189
00190
00191 template <int M>
00192 void MultiplyBy(const BigUnsigned<M>& other) {
00193 MultiplyBy(other.size(), other.words());
00194 }
00195
00196 void SetToZero() {
00197 std::fill(words_, words_ + size_, 0u);
00198 size_ = 0;
00199 }
00200
00201
00202
00203 uint32_t GetWord(int index) const {
00204 if (index < 0 || index >= size_) {
00205 return 0;
00206 }
00207 return words_[index];
00208 }
00209
00210
00211
00212 std::string ToString() const;
00213
00214 int size() const { return size_; }
00215 const uint32_t* words() const { return words_; }
00216
00217 private:
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 int ReadDigits(const char* begin, const char* end, int significant_digits);
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 void MultiplyStep(int original_size, const uint32_t* other_words,
00265 int other_size, int step);
00266
00267 void MultiplyBy(int other_size, const uint32_t* other_words) {
00268 const int original_size = size_;
00269 const int first_step =
00270 (std::min)(original_size + other_size - 2, max_words - 1);
00271 for (int step = first_step; step >= 0; --step) {
00272 MultiplyStep(original_size, other_words, other_size, step);
00273 }
00274 }
00275
00276
00277 void AddWithCarry(int index, uint32_t value) {
00278 if (value) {
00279 while (index < max_words && value > 0) {
00280 words_[index] += value;
00281
00282 if (value > words_[index]) {
00283 value = 1;
00284 ++index;
00285 } else {
00286 value = 0;
00287 }
00288 }
00289 size_ = (std::min)(max_words, (std::max)(index + 1, size_));
00290 }
00291 }
00292
00293 void AddWithCarry(int index, uint64_t value) {
00294 if (value && index < max_words) {
00295 uint32_t high = value >> 32;
00296 uint32_t low = value & 0xffffffff;
00297 words_[index] += low;
00298 if (words_[index] < low) {
00299 ++high;
00300 if (high == 0) {
00301
00302
00303 AddWithCarry(index + 2, static_cast<uint32_t>(1));
00304 return;
00305 }
00306 }
00307 if (high > 0) {
00308 AddWithCarry(index + 1, high);
00309 } else {
00310
00311
00312 size_ = (std::min)(max_words, (std::max)(index + 1, size_));
00313 }
00314 }
00315 }
00316
00317
00318
00319 template <uint32_t divisor>
00320 uint32_t DivMod() {
00321 uint64_t accumulator = 0;
00322 for (int i = size_ - 1; i >= 0; --i) {
00323 accumulator <<= 32;
00324 accumulator += words_[i];
00325
00326 words_[i] = static_cast<uint32_t>(accumulator / divisor);
00327 accumulator = accumulator % divisor;
00328 }
00329 while (size_ > 0 && words_[size_ - 1] == 0) {
00330 --size_;
00331 }
00332 return static_cast<uint32_t>(accumulator);
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342 int size_;
00343 uint32_t words_[max_words];
00344 };
00345
00346
00347
00348
00349 template <int N, int M>
00350 int Compare(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
00351 int limit = (std::max)(lhs.size(), rhs.size());
00352 for (int i = limit - 1; i >= 0; --i) {
00353 const uint32_t lhs_word = lhs.GetWord(i);
00354 const uint32_t rhs_word = rhs.GetWord(i);
00355 if (lhs_word < rhs_word) {
00356 return -1;
00357 } else if (lhs_word > rhs_word) {
00358 return 1;
00359 }
00360 }
00361 return 0;
00362 }
00363
00364 template <int N, int M>
00365 bool operator==(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
00366 int limit = (std::max)(lhs.size(), rhs.size());
00367 for (int i = 0; i < limit; ++i) {
00368 if (lhs.GetWord(i) != rhs.GetWord(i)) {
00369 return false;
00370 }
00371 }
00372 return true;
00373 }
00374
00375 template <int N, int M>
00376 bool operator!=(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
00377 return !(lhs == rhs);
00378 }
00379
00380 template <int N, int M>
00381 bool operator<(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
00382 return Compare(lhs, rhs) == -1;
00383 }
00384
00385 template <int N, int M>
00386 bool operator>(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
00387 return rhs < lhs;
00388 }
00389 template <int N, int M>
00390 bool operator<=(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
00391 return !(rhs < lhs);
00392 }
00393 template <int N, int M>
00394 bool operator>=(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
00395 return !(lhs < rhs);
00396 }
00397
00398
00399 template <int N>
00400 std::ostream& operator<<(std::ostream& os, const BigUnsigned<N>& num) {
00401 return os << num.ToString();
00402 }
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 extern template class BigUnsigned<4>;
00414 extern template class BigUnsigned<84>;
00415
00416 }
00417 }
00418
00419 #endif // ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_