15 #ifndef ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_ 16 #define ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_ 28 namespace strings_internal {
35 extern const uint32_t
kFiveToNth[kMaxSmallPowerOfFive + 1];
36 extern const uint32_t
kTenToNth[kMaxSmallPowerOfTen + 1];
53 template <
int max_words>
56 static_assert(max_words == 4 || max_words == 84,
57 "unsupported max_words value");
61 :
size_((v >> 32) ? 2 : v ? 1 : 0),
62 words_{
static_cast<uint32_t
>(v & 0xffffffffu),
63 static_cast<uint32_t>(v >> 32)} {}
71 if (std::find_if_not(sv.begin(), sv.end(),
ascii_isdigit) != sv.end() ||
77 if (exponent_adjust > 0) {
95 return static_cast<uint64_t
>(max_words) * 9975007 / 1035508;
101 const int word_shift = count / 32;
102 if (word_shift >= max_words) {
106 size_ = (std::min)(
size_ + word_shift, max_words);
111 for (
int i = (std::min)(
size_, max_words - 1);
i > word_shift; --
i) {
113 (
words_[
i - word_shift - 1] >> (32 - count));
128 if (
size_ == 0 || v == 1) {
135 const uint64_t factor =
v;
139 words_[
i] = window & 0xffffffff;
143 if (window && size_ < max_words) {
151 words[0] =
static_cast<uint32_t
>(
v);
152 words[1] =
static_cast<uint32_t
>(v >> 32);
162 while (n >= kMaxSmallPowerOfFive) {
173 if (n > kMaxSmallPowerOfTen) {
204 if (index < 0 || index >=
size_) {
264 void MultiplyStep(
int original_size,
const uint32_t* other_words,
265 int other_size,
int step);
267 void MultiplyBy(
int other_size,
const uint32_t* other_words) {
268 const int original_size =
size_;
269 const int first_step =
270 (std::min)(original_size + other_size - 2, max_words - 1);
271 for (
int step = first_step; step >= 0; --
step) {
272 MultiplyStep(original_size, other_words, other_size, step);
279 while (index < max_words && value > 0) {
282 if (value >
words_[index]) {
289 size_ = (std::min)(max_words, (std::max)(index + 1,
size_));
294 if (value && index < max_words) {
295 uint32_t high = value >> 32;
296 uint32_t low = value & 0xffffffff;
298 if (
words_[index] < low) {
312 size_ = (std::min)(max_words, (std::max)(index + 1,
size_));
319 template <u
int32_t divisor>
321 uint64_t accumulator = 0;
322 for (
int i =
size_ - 1;
i >= 0; --
i) {
326 words_[
i] =
static_cast<uint32_t
>(accumulator / divisor);
327 accumulator = accumulator % divisor;
332 return static_cast<uint32_t
>(accumulator);
349 template <
int N,
int M>
351 int limit = (std::max)(lhs.
size(), rhs.
size());
352 for (
int i = limit - 1;
i >= 0; --
i) {
353 const uint32_t lhs_word = lhs.
GetWord(
i);
354 const uint32_t rhs_word = rhs.
GetWord(
i);
355 if (lhs_word < rhs_word) {
357 }
else if (lhs_word > rhs_word) {
364 template <
int N,
int M>
366 int limit = (std::max)(lhs.
size(), rhs.
size());
367 for (
int i = 0;
i < limit; ++
i) {
375 template <
int N,
int M>
377 return !(lhs == rhs);
380 template <
int N,
int M>
382 return Compare(lhs, rhs) == -1;
385 template <
int N,
int M>
389 template <
int N,
int M>
393 template <
int N,
int M>
400 std::ostream& operator<<(std::ostream& os, const BigUnsigned<N>& num) {
401 return os << num.ToString();
419 #endif // ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_
void MultiplyBy(const BigUnsigned< M > &other)
const uint32_t * words() const
uint32_t GetWord(int index) const
bool operator==(const BigUnsigned< N > &lhs, const BigUnsigned< M > &rhs)
static constexpr int Digits10()
void ShiftLeft(int count)
const uint32_t kTenToNth[10]
CONSTEXPR_F fields step(second_tag, fields f, diff_t n) noexcept
bool operator>(const BigUnsigned< N > &lhs, const BigUnsigned< M > &rhs)
int ReadDigits(const char *begin, const char *end, int significant_digits)
constexpr int kMaxSmallPowerOfFive
uint32_t words_[max_words]
void AddWithCarry(int index, uint64_t value)
constexpr int kMaxSmallPowerOfTen
constexpr BigUnsigned(uint64_t v)
bool operator>=(const BigUnsigned< N > &lhs, const BigUnsigned< M > &rhs)
void MultiplyStep(int original_size, const uint32_t *other_words, int other_size, int step)
const uint32_t kFiveToNth[14]
BigUnsigned(absl::string_view sv)
void AddWithCarry(int index, uint32_t value)
void MultiplyByTenToTheNth(int n)
std::string ToString() const
void MultiplyBy(uint64_t v)
static BigUnsigned FiveToTheNth(int n)
bool ascii_isdigit(unsigned char c)
int Compare(const BigUnsigned< N > &lhs, const BigUnsigned< M > &rhs)
int ReadFloatMantissa(const ParsedFloat &fp, int significant_digits)
void MultiplyBy(uint32_t v)
void MultiplyByFiveToTheNth(int n)
bool operator!=(const BigUnsigned< N > &lhs, const BigUnsigned< M > &rhs)
void MultiplyBy(int other_size, const uint32_t *other_words)