Coverage Report

Created: 2023-09-25 06:27

/src/abseil-cpp/absl/strings/internal/charconv_bigint.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2018 The Abseil Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_
16
#define ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_
17
18
#include <algorithm>
19
#include <cstdint>
20
#include <iostream>
21
#include <string>
22
23
#include "absl/base/config.h"
24
#include "absl/strings/ascii.h"
25
#include "absl/strings/internal/charconv_parse.h"
26
#include "absl/strings/string_view.h"
27
28
namespace absl {
29
ABSL_NAMESPACE_BEGIN
30
namespace strings_internal {
31
32
// The largest power that 5 that can be raised to, and still fit in a uint32_t.
33
constexpr int kMaxSmallPowerOfFive = 13;
34
// The largest power that 10 that can be raised to, and still fit in a uint32_t.
35
constexpr int kMaxSmallPowerOfTen = 9;
36
37
ABSL_DLL extern const uint32_t
38
    kFiveToNth[kMaxSmallPowerOfFive + 1];
39
ABSL_DLL extern const uint32_t kTenToNth[kMaxSmallPowerOfTen + 1];
40
41
// Large, fixed-width unsigned integer.
42
//
43
// Exact rounding for decimal-to-binary floating point conversion requires very
44
// large integer math, but a design goal of absl::from_chars is to avoid
45
// allocating memory.  The integer precision needed for decimal-to-binary
46
// conversions is large but bounded, so a huge fixed-width integer class
47
// suffices.
48
//
49
// This is an intentionally limited big integer class.  Only needed operations
50
// are implemented.  All storage lives in an array data member, and all
51
// arithmetic is done in-place, to avoid requiring separate storage for operand
52
// and result.
53
//
54
// This is an internal class.  Some methods live in the .cc file, and are
55
// instantiated only for the values of max_words we need.
56
template <int max_words>
57
class BigUnsigned {
58
 public:
59
  static_assert(max_words == 4 || max_words == 84,
60
                "unsupported max_words value");
61
62
10.4k
  BigUnsigned() : size_(0), words_{} {}
absl::strings_internal::BigUnsigned<84>::BigUnsigned()
Line
Count
Source
62
10.4k
  BigUnsigned() : size_(0), words_{} {}
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::BigUnsigned()
63
  explicit constexpr BigUnsigned(uint64_t v)
64
      : size_((v >> 32) ? 2 : v ? 1 : 0),
65
        words_{static_cast<uint32_t>(v & 0xffffffffu),
66
10.4k
               static_cast<uint32_t>(v >> 32)} {}
absl::strings_internal::BigUnsigned<84>::BigUnsigned(unsigned long)
Line
Count
Source
66
10.4k
               static_cast<uint32_t>(v >> 32)} {}
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::BigUnsigned(unsigned long)
67
68
  // Constructs a BigUnsigned from the given string_view containing a decimal
69
  // value.  If the input string is not a decimal integer, constructs a 0
70
  // instead.
71
0
  explicit BigUnsigned(absl::string_view sv) : size_(0), words_{} {
72
    // Check for valid input, returning a 0 otherwise.  This is reasonable
73
    // behavior only because this constructor is for unit tests.
74
0
    if (std::find_if_not(sv.begin(), sv.end(), ascii_isdigit) != sv.end() ||
75
0
        sv.empty()) {
76
0
      return;
77
0
    }
78
0
    int exponent_adjust =
79
0
        ReadDigits(sv.data(), sv.data() + sv.size(), Digits10() + 1);
80
0
    if (exponent_adjust > 0) {
81
0
      MultiplyByTenToTheNth(exponent_adjust);
82
0
    }
83
0
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::BigUnsigned(absl::string_view)
Unexecuted instantiation: absl::strings_internal::BigUnsigned<84>::BigUnsigned(absl::string_view)
84
85
  // Loads the mantissa value of a previously-parsed float.
86
  //
87
  // Returns the associated decimal exponent.  The value of the parsed float is
88
  // exactly *this * 10**exponent.
89
  int ReadFloatMantissa(const ParsedFloat& fp, int significant_digits);
90
91
  // Returns the number of decimal digits of precision this type provides.  All
92
  // numbers with this many decimal digits or fewer are representable by this
93
  // type.
94
  //
95
  // Analogous to std::numeric_limits<BigUnsigned>::digits10.
96
7.90k
  static constexpr int Digits10() {
97
    // 9975007/1035508 is very slightly less than log10(2**32).
98
7.90k
    return static_cast<uint64_t>(max_words) * 9975007 / 1035508;
99
7.90k
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::Digits10()
absl::strings_internal::BigUnsigned<84>::Digits10()
Line
Count
Source
96
7.90k
  static constexpr int Digits10() {
97
    // 9975007/1035508 is very slightly less than log10(2**32).
98
7.90k
    return static_cast<uint64_t>(max_words) * 9975007 / 1035508;
99
7.90k
  }
100
101
  // Shifts left by the given number of bits.
102
10.4k
  void ShiftLeft(int count) {
103
10.4k
    if (count > 0) {
104
10.2k
      const int word_shift = count / 32;
105
10.2k
      if (word_shift >= max_words) {
106
201
        SetToZero();
107
201
        return;
108
201
      }
109
10.0k
      size_ = (std::min)(size_ + word_shift, max_words);
110
10.0k
      count %= 32;
111
10.0k
      if (count == 0) {
112
297
        std::copy_backward(words_, words_ + size_ - word_shift, words_ + size_);
113
9.70k
      } else {
114
168k
        for (int i = (std::min)(size_, max_words - 1); i > word_shift; --i) {
115
158k
          words_[i] = (words_[i - word_shift] << count) |
116
158k
                      (words_[i - word_shift - 1] >> (32 - count));
117
158k
        }
118
9.70k
        words_[word_shift] = words_[0] << count;
119
        // Grow size_ if necessary.
120
9.70k
        if (size_ < max_words && words_[size_]) {
121
4.38k
          ++size_;
122
4.38k
        }
123
9.70k
      }
124
10.0k
      std::fill_n(words_, word_shift, 0u);
125
10.0k
    }
126
10.4k
  }
absl::strings_internal::BigUnsigned<84>::ShiftLeft(int)
Line
Count
Source
102
10.4k
  void ShiftLeft(int count) {
103
10.4k
    if (count > 0) {
104
10.2k
      const int word_shift = count / 32;
105
10.2k
      if (word_shift >= max_words) {
106
201
        SetToZero();
107
201
        return;
108
201
      }
109
10.0k
      size_ = (std::min)(size_ + word_shift, max_words);
110
10.0k
      count %= 32;
111
10.0k
      if (count == 0) {
112
297
        std::copy_backward(words_, words_ + size_ - word_shift, words_ + size_);
113
9.70k
      } else {
114
168k
        for (int i = (std::min)(size_, max_words - 1); i > word_shift; --i) {
115
158k
          words_[i] = (words_[i - word_shift] << count) |
116
158k
                      (words_[i - word_shift - 1] >> (32 - count));
117
158k
        }
118
9.70k
        words_[word_shift] = words_[0] << count;
119
        // Grow size_ if necessary.
120
9.70k
        if (size_ < max_words && words_[size_]) {
121
4.38k
          ++size_;
122
4.38k
        }
123
9.70k
      }
124
10.0k
      std::fill_n(words_, word_shift, 0u);
125
10.0k
    }
126
10.4k
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::ShiftLeft(int)
127
128
129
  // Multiplies by v in-place.
130
498k
  void MultiplyBy(uint32_t v) {
131
498k
    if (size_ == 0 || v == 1) {
132
124k
      return;
133
124k
    }
134
374k
    if (v == 0) {
135
0
      SetToZero();
136
0
      return;
137
0
    }
138
374k
    const uint64_t factor = v;
139
374k
    uint64_t window = 0;
140
16.6M
    for (int i = 0; i < size_; ++i) {
141
16.2M
      window += factor * words_[i];
142
16.2M
      words_[i] = window & 0xffffffff;
143
16.2M
      window >>= 32;
144
16.2M
    }
145
    // If carry bits remain and there's space for them, grow size_.
146
374k
    if (window && size_ < max_words) {
147
264k
      words_[size_] = window & 0xffffffff;
148
264k
      ++size_;
149
264k
    }
150
374k
  }
absl::strings_internal::BigUnsigned<84>::MultiplyBy(unsigned int)
Line
Count
Source
130
498k
  void MultiplyBy(uint32_t v) {
131
498k
    if (size_ == 0 || v == 1) {
132
124k
      return;
133
124k
    }
134
374k
    if (v == 0) {
135
0
      SetToZero();
136
0
      return;
137
0
    }
138
374k
    const uint64_t factor = v;
139
374k
    uint64_t window = 0;
140
16.6M
    for (int i = 0; i < size_; ++i) {
141
16.2M
      window += factor * words_[i];
142
16.2M
      words_[i] = window & 0xffffffff;
143
16.2M
      window >>= 32;
144
16.2M
    }
145
    // If carry bits remain and there's space for them, grow size_.
146
374k
    if (window && size_ < max_words) {
147
264k
      words_[size_] = window & 0xffffffff;
148
264k
      ++size_;
149
264k
    }
150
374k
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::MultiplyBy(unsigned int)
151
152
6.51k
  void MultiplyBy(uint64_t v) {
153
6.51k
    uint32_t words[2];
154
6.51k
    words[0] = static_cast<uint32_t>(v);
155
6.51k
    words[1] = static_cast<uint32_t>(v >> 32);
156
6.51k
    if (words[1] == 0) {
157
0
      MultiplyBy(words[0]);
158
6.51k
    } else {
159
6.51k
      MultiplyBy(2, words);
160
6.51k
    }
161
6.51k
  }
absl::strings_internal::BigUnsigned<84>::MultiplyBy(unsigned long)
Line
Count
Source
152
6.51k
  void MultiplyBy(uint64_t v) {
153
6.51k
    uint32_t words[2];
154
6.51k
    words[0] = static_cast<uint32_t>(v);
155
6.51k
    words[1] = static_cast<uint32_t>(v >> 32);
156
6.51k
    if (words[1] == 0) {
157
0
      MultiplyBy(words[0]);
158
6.51k
    } else {
159
6.51k
      MultiplyBy(2, words);
160
6.51k
    }
161
6.51k
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::MultiplyBy(unsigned long)
162
163
  // Multiplies in place by 5 to the power of n.  n must be non-negative.
164
10.4k
  void MultiplyByFiveToTheNth(int n) {
165
203k
    while (n >= kMaxSmallPowerOfFive) {
166
193k
      MultiplyBy(kFiveToNth[kMaxSmallPowerOfFive]);
167
193k
      n -= kMaxSmallPowerOfFive;
168
193k
    }
169
10.4k
    if (n > 0) {
170
7.02k
      MultiplyBy(kFiveToNth[n]);
171
7.02k
    }
172
10.4k
  }
absl::strings_internal::BigUnsigned<84>::MultiplyByFiveToTheNth(int)
Line
Count
Source
164
10.4k
  void MultiplyByFiveToTheNth(int n) {
165
203k
    while (n >= kMaxSmallPowerOfFive) {
166
193k
      MultiplyBy(kFiveToNth[kMaxSmallPowerOfFive]);
167
193k
      n -= kMaxSmallPowerOfFive;
168
193k
    }
169
10.4k
    if (n > 0) {
170
7.02k
      MultiplyBy(kFiveToNth[n]);
171
7.02k
    }
172
10.4k
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::MultiplyByFiveToTheNth(int)
173
174
  // Multiplies in place by 10 to the power of n.  n must be non-negative.
175
0
  void MultiplyByTenToTheNth(int n) {
176
0
    if (n > kMaxSmallPowerOfTen) {
177
      // For large n, raise to a power of 5, then shift left by the same amount.
178
      // (10**n == 5**n * 2**n.)  This requires fewer multiplications overall.
179
0
      MultiplyByFiveToTheNth(n);
180
0
      ShiftLeft(n);
181
0
    } else if (n > 0) {
182
      // We can do this more quickly for very small N by using a single
183
      // multiplication.
184
0
      MultiplyBy(kTenToNth[n]);
185
0
    }
186
0
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::MultiplyByTenToTheNth(int)
Unexecuted instantiation: absl::strings_internal::BigUnsigned<84>::MultiplyByTenToTheNth(int)
187
188
  // Returns the value of 5**n, for non-negative n.  This implementation uses
189
  // a lookup table, and is faster then seeding a BigUnsigned with 1 and calling
190
  // MultiplyByFiveToTheNth().
191
  static BigUnsigned FiveToTheNth(int n);
192
193
  // Multiplies by another BigUnsigned, in-place.
194
  template <int M>
195
  void MultiplyBy(const BigUnsigned<M>& other) {
196
    MultiplyBy(other.size(), other.words());
197
  }
198
199
18.5k
  void SetToZero() {
200
18.5k
    std::fill_n(words_, size_, 0u);
201
18.5k
    size_ = 0;
202
18.5k
  }
absl::strings_internal::BigUnsigned<84>::SetToZero()
Line
Count
Source
199
18.5k
  void SetToZero() {
200
18.5k
    std::fill_n(words_, size_, 0u);
201
18.5k
    size_ = 0;
202
18.5k
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::SetToZero()
203
204
  // Returns the value of the nth word of this BigUnsigned.  This is
205
  // range-checked, and returns 0 on out-of-bounds accesses.
206
53.6k
  uint32_t GetWord(int index) const {
207
53.6k
    if (index < 0 || index >= size_) {
208
1.56k
      return 0;
209
1.56k
    }
210
52.0k
    return words_[index];
211
53.6k
  }
absl::strings_internal::BigUnsigned<84>::GetWord(int) const
Line
Count
Source
206
53.6k
  uint32_t GetWord(int index) const {
207
53.6k
    if (index < 0 || index >= size_) {
208
1.56k
      return 0;
209
1.56k
    }
210
52.0k
    return words_[index];
211
53.6k
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::GetWord(int) const
212
213
  // Returns this integer as a decimal string.  This is not used in the decimal-
214
  // to-binary conversion; it is intended to aid in testing.
215
  std::string ToString() const;
216
217
20.9k
  int size() const { return size_; }
absl::strings_internal::BigUnsigned<84>::size() const
Line
Count
Source
217
20.9k
  int size() const { return size_; }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::size() const
218
0
  const uint32_t* words() const { return words_; }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::words() const
Unexecuted instantiation: absl::strings_internal::BigUnsigned<84>::words() const
219
220
 private:
221
  // Reads the number between [begin, end), possibly containing a decimal point,
222
  // into this BigUnsigned.
223
  //
224
  // Callers are required to ensure [begin, end) contains a valid number, with
225
  // one or more decimal digits and at most one decimal point.  This routine
226
  // will behave unpredictably if these preconditions are not met.
227
  //
228
  // Only the first `significant_digits` digits are read.  Digits beyond this
229
  // limit are "sticky": If the final significant digit is 0 or 5, and if any
230
  // dropped digit is nonzero, then that final significant digit is adjusted up
231
  // to 1 or 6.  This adjustment allows for precise rounding.
232
  //
233
  // Returns `exponent_adjustment`, a power-of-ten exponent adjustment to
234
  // account for the decimal point and for dropped significant digits.  After
235
  // this function returns,
236
  //   actual_value_of_parsed_string ~= *this * 10**exponent_adjustment.
237
  int ReadDigits(const char* begin, const char* end, int significant_digits);
238
239
  // Performs a step of big integer multiplication.  This computes the full
240
  // (64-bit-wide) values that should be added at the given index (step), and
241
  // adds to that location in-place.
242
  //
243
  // Because our math all occurs in place, we must multiply starting from the
244
  // highest word working downward.  (This is a bit more expensive due to the
245
  // extra carries involved.)
246
  //
247
  // This must be called in steps, for each word to be calculated, starting from
248
  // the high end and working down to 0.  The first value of `step` should be
249
  //   `std::min(original_size + other.size_ - 2, max_words - 1)`.
250
  // The reason for this expression is that multiplying the i'th word from one
251
  // multiplicand and the j'th word of another multiplicand creates a
252
  // two-word-wide value to be stored at the (i+j)'th element.  The highest
253
  // word indices we will access are `original_size - 1` from this object, and
254
  // `other.size_ - 1` from our operand.  Therefore,
255
  // `original_size + other.size_ - 2` is the first step we should calculate,
256
  // but limited on an upper bound by max_words.
257
258
  // Working from high-to-low ensures that we do not overwrite the portions of
259
  // the initial value of *this which are still needed for later steps.
260
  //
261
  // Once called with step == 0, *this contains the result of the
262
  // multiplication.
263
  //
264
  // `original_size` is the size_ of *this before the first call to
265
  // MultiplyStep().  `other_words` and `other_size` are the contents of our
266
  // operand.  `step` is the step to perform, as described above.
267
  void MultiplyStep(int original_size, const uint32_t* other_words,
268
                    int other_size, int step);
269
270
9.01k
  void MultiplyBy(int other_size, const uint32_t* other_words) {
271
9.01k
    const int original_size = size_;
272
9.01k
    const int first_step =
273
9.01k
        (std::min)(original_size + other_size - 2, max_words - 1);
274
298k
    for (int step = first_step; step >= 0; --step) {
275
289k
      MultiplyStep(original_size, other_words, other_size, step);
276
289k
    }
277
9.01k
  }
absl::strings_internal::BigUnsigned<84>::MultiplyBy(int, unsigned int const*)
Line
Count
Source
270
9.01k
  void MultiplyBy(int other_size, const uint32_t* other_words) {
271
9.01k
    const int original_size = size_;
272
9.01k
    const int first_step =
273
9.01k
        (std::min)(original_size + other_size - 2, max_words - 1);
274
298k
    for (int step = first_step; step >= 0; --step) {
275
289k
      MultiplyStep(original_size, other_words, other_size, step);
276
289k
    }
277
9.01k
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::MultiplyBy(int, unsigned int const*)
278
279
  // Adds a 32-bit value to the index'th word, with carry.
280
441k
  void AddWithCarry(int index, uint32_t value) {
281
441k
    if (value) {
282
388k
      while (index < max_words && value > 0) {
283
196k
        words_[index] += value;
284
        // carry if we overflowed in this word:
285
196k
        if (value > words_[index]) {
286
4.40k
          value = 1;
287
4.40k
          ++index;
288
191k
        } else {
289
191k
          value = 0;
290
191k
        }
291
196k
      }
292
191k
      size_ = (std::min)(max_words, (std::max)(index + 1, size_));
293
191k
    }
294
441k
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::AddWithCarry(int, unsigned int)
absl::strings_internal::BigUnsigned<84>::AddWithCarry(int, unsigned int)
Line
Count
Source
280
441k
  void AddWithCarry(int index, uint32_t value) {
281
441k
    if (value) {
282
388k
      while (index < max_words && value > 0) {
283
196k
        words_[index] += value;
284
        // carry if we overflowed in this word:
285
196k
        if (value > words_[index]) {
286
4.40k
          value = 1;
287
4.40k
          ++index;
288
191k
        } else {
289
191k
          value = 0;
290
191k
        }
291
196k
      }
292
191k
      size_ = (std::min)(max_words, (std::max)(index + 1, size_));
293
191k
    }
294
441k
  }
295
296
289k
  void AddWithCarry(int index, uint64_t value) {
297
289k
    if (value && index < max_words) {
298
285k
      uint32_t high = value >> 32;
299
285k
      uint32_t low = value & 0xffffffff;
300
285k
      words_[index] += low;
301
285k
      if (words_[index] < low) {
302
98.7k
        ++high;
303
98.7k
        if (high == 0) {
304
          // Carry from the low word caused our high word to overflow.
305
          // Short circuit here to do the right thing.
306
0
          AddWithCarry(index + 2, static_cast<uint32_t>(1));
307
0
          return;
308
0
        }
309
98.7k
      }
310
285k
      if (high > 0) {
311
143k
        AddWithCarry(index + 1, high);
312
143k
      } else {
313
        // Normally 32-bit AddWithCarry() sets size_, but since we don't call
314
        // it when `high` is 0, do it ourselves here.
315
141k
        size_ = (std::min)(max_words, (std::max)(index + 1, size_));
316
141k
      }
317
285k
    }
318
289k
  }
Unexecuted instantiation: absl::strings_internal::BigUnsigned<4>::AddWithCarry(int, unsigned long)
absl::strings_internal::BigUnsigned<84>::AddWithCarry(int, unsigned long)
Line
Count
Source
296
289k
  void AddWithCarry(int index, uint64_t value) {
297
289k
    if (value && index < max_words) {
298
285k
      uint32_t high = value >> 32;
299
285k
      uint32_t low = value & 0xffffffff;
300
285k
      words_[index] += low;
301
285k
      if (words_[index] < low) {
302
98.7k
        ++high;
303
98.7k
        if (high == 0) {
304
          // Carry from the low word caused our high word to overflow.
305
          // Short circuit here to do the right thing.
306
0
          AddWithCarry(index + 2, static_cast<uint32_t>(1));
307
0
          return;
308
0
        }
309
98.7k
      }
310
285k
      if (high > 0) {
311
143k
        AddWithCarry(index + 1, high);
312
143k
      } else {
313
        // Normally 32-bit AddWithCarry() sets size_, but since we don't call
314
        // it when `high` is 0, do it ourselves here.
315
141k
        size_ = (std::min)(max_words, (std::max)(index + 1, size_));
316
141k
      }
317
285k
    }
318
289k
  }
319
320
  // Divide this in place by a constant divisor.  Returns the remainder of the
321
  // division.
322
  template <uint32_t divisor>
323
0
  uint32_t DivMod() {
324
0
    uint64_t accumulator = 0;
325
0
    for (int i = size_ - 1; i >= 0; --i) {
326
0
      accumulator <<= 32;
327
0
      accumulator += words_[i];
328
      // accumulator / divisor will never overflow an int32_t in this loop
329
0
      words_[i] = static_cast<uint32_t>(accumulator / divisor);
330
0
      accumulator = accumulator % divisor;
331
0
    }
332
0
    while (size_ > 0 && words_[size_ - 1] == 0) {
333
0
      --size_;
334
0
    }
335
0
    return static_cast<uint32_t>(accumulator);
336
0
  }
Unexecuted instantiation: unsigned int absl::strings_internal::BigUnsigned<4>::DivMod<10u>()
Unexecuted instantiation: unsigned int absl::strings_internal::BigUnsigned<84>::DivMod<10u>()
337
338
  // The number of elements in words_ that may carry significant values.
339
  // All elements beyond this point are 0.
340
  //
341
  // When size_ is 0, this BigUnsigned stores the value 0.
342
  // When size_ is nonzero, is *not* guaranteed that words_[size_ - 1] is
343
  // nonzero.  This can occur due to overflow truncation.
344
  // In particular, x.size_ != y.size_ does *not* imply x != y.
345
  int size_;
346
  uint32_t words_[max_words];
347
};
348
349
// Compares two big integer instances.
350
//
351
// Returns -1 if lhs < rhs, 0 if lhs == rhs, and 1 if lhs > rhs.
352
template <int N, int M>
353
10.4k
int Compare(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
354
10.4k
  int limit = (std::max)(lhs.size(), rhs.size());
355
27.8k
  for (int i = limit - 1; i >= 0; --i) {
356
26.8k
    const uint32_t lhs_word = lhs.GetWord(i);
357
26.8k
    const uint32_t rhs_word = rhs.GetWord(i);
358
26.8k
    if (lhs_word < rhs_word) {
359
6.89k
      return -1;
360
19.9k
    } else if (lhs_word > rhs_word) {
361
2.59k
      return 1;
362
2.59k
    }
363
26.8k
  }
364
1.00k
  return 0;
365
10.4k
}
366
367
template <int N, int M>
368
bool operator==(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
369
  int limit = (std::max)(lhs.size(), rhs.size());
370
  for (int i = 0; i < limit; ++i) {
371
    if (lhs.GetWord(i) != rhs.GetWord(i)) {
372
      return false;
373
    }
374
  }
375
  return true;
376
}
377
378
template <int N, int M>
379
bool operator!=(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
380
  return !(lhs == rhs);
381
}
382
383
template <int N, int M>
384
bool operator<(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
385
  return Compare(lhs, rhs) == -1;
386
}
387
388
template <int N, int M>
389
bool operator>(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
390
  return rhs < lhs;
391
}
392
template <int N, int M>
393
bool operator<=(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
394
  return !(rhs < lhs);
395
}
396
template <int N, int M>
397
bool operator>=(const BigUnsigned<N>& lhs, const BigUnsigned<M>& rhs) {
398
  return !(lhs < rhs);
399
}
400
401
// Output operator for BigUnsigned, for testing purposes only.
402
template <int N>
403
std::ostream& operator<<(std::ostream& os, const BigUnsigned<N>& num) {
404
  return os << num.ToString();
405
}
406
407
// Explicit instantiation declarations for the sizes of BigUnsigned that we
408
// are using.
409
//
410
// For now, the choices of 4 and 84 are arbitrary; 4 is a small value that is
411
// still bigger than an int128, and 84 is a large value we will want to use
412
// in the from_chars implementation.
413
//
414
// Comments justifying the use of 84 belong in the from_chars implementation,
415
// and will be added in a follow-up CL.
416
extern template class BigUnsigned<4>;
417
extern template class BigUnsigned<84>;
418
419
}  // namespace strings_internal
420
ABSL_NAMESPACE_END
421
}  // namespace absl
422
423
#endif  // ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_