Coverage Report

Created: 2026-02-05 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/i18n/double-conversion-bignum.cpp
Line
Count
Source
1
// © 2018 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
//
4
// From the double-conversion library. Original license:
5
//
6
// Copyright 2010 the V8 project authors. All rights reserved.
7
// Redistribution and use in source and binary forms, with or without
8
// modification, are permitted provided that the following conditions are
9
// met:
10
//
11
//     * Redistributions of source code must retain the above copyright
12
//       notice, this list of conditions and the following disclaimer.
13
//     * Redistributions in binary form must reproduce the above
14
//       copyright notice, this list of conditions and the following
15
//       disclaimer in the documentation and/or other materials provided
16
//       with the distribution.
17
//     * Neither the name of Google Inc. nor the names of its
18
//       contributors may be used to endorse or promote products derived
19
//       from this software without specific prior written permission.
20
//
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33
// ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
34
#include "unicode/utypes.h"
35
#if !UCONFIG_NO_FORMATTING
36
37
#include <algorithm>
38
#include <cstring>
39
40
// ICU PATCH: Customize header file paths for ICU.
41
42
#include "double-conversion-bignum.h"
43
#include "double-conversion-utils.h"
44
45
// ICU PATCH: Wrap in ICU namespace
46
U_NAMESPACE_BEGIN
47
48
namespace double_conversion {
49
50
15.8M
Bignum::Chunk& Bignum::RawBigit(const int index) {
51
15.8M
  DOUBLE_CONVERSION_ASSERT(static_cast<unsigned>(index) < kBigitCapacity);
52
15.8M
  return bigits_buffer_[index];
53
15.8M
}
54
55
56
4.17M
const Bignum::Chunk& Bignum::RawBigit(const int index) const {
57
4.17M
  DOUBLE_CONVERSION_ASSERT(static_cast<unsigned>(index) < kBigitCapacity);
58
4.17M
  return bigits_buffer_[index];
59
4.17M
}
60
61
62
template<typename S>
63
static int BitSize(const S value) {
64
  (void) value;  // Mark variable as used.
65
  return 8 * sizeof(value);
66
}
67
68
// Guaranteed to lie in one Bigit.
69
24.8k
void Bignum::AssignUInt16(const uint16_t value) {
70
24.8k
  DOUBLE_CONVERSION_ASSERT(kBigitSize >= BitSize(value));
71
24.8k
  Zero();
72
24.8k
  if (value > 0) {
73
24.8k
    RawBigit(0) = value;
74
24.8k
    used_bigits_ = 1;
75
24.8k
  }
76
24.8k
}
77
78
79
50.6k
void Bignum::AssignUInt64(uint64_t value) {
80
50.6k
  Zero();
81
152k
  for(int i = 0; value > 0; ++i) {
82
101k
    RawBigit(i) = value & kBigitMask;
83
101k
    value >>= kBigitSize;
84
101k
    ++used_bigits_;
85
101k
  }
86
50.6k
}
87
88
89
12.3k
void Bignum::AssignBignum(const Bignum& other) {
90
12.3k
  exponent_ = other.exponent_;
91
70.7k
  for (int i = 0; i < other.used_bigits_; ++i) {
92
58.3k
    RawBigit(i) = other.RawBigit(i);
93
58.3k
  }
94
12.3k
  used_bigits_ = other.used_bigits_;
95
12.3k
}
96
97
98
static uint64_t ReadUInt64(const Vector<const char> buffer,
99
                           const int from,
100
18.1k
                           const int digits_to_read) {
101
18.1k
  uint64_t result = 0;
102
276k
  for (int i = from; i < from + digits_to_read; ++i) {
103
258k
    const int digit = buffer[i] - '0';
104
258k
    DOUBLE_CONVERSION_ASSERT(0 <= digit && digit <= 9);
105
258k
    result = result * 10 + digit;
106
258k
  }
107
18.1k
  return result;
108
18.1k
}
109
110
111
11.2k
void Bignum::AssignDecimalString(const Vector<const char> value) {
112
  // 2^64 = 18446744073709551616 > 10^19
113
11.2k
  static const int kMaxUint64DecimalDigits = 19;
114
11.2k
  Zero();
115
11.2k
  int length = value.length();
116
11.2k
  unsigned pos = 0;
117
  // Let's just say that each digit needs 4 bits.
118
18.1k
  while (length >= kMaxUint64DecimalDigits) {
119
6.92k
    const uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits);
120
6.92k
    pos += kMaxUint64DecimalDigits;
121
6.92k
    length -= kMaxUint64DecimalDigits;
122
6.92k
    MultiplyByPowerOfTen(kMaxUint64DecimalDigits);
123
6.92k
    AddUInt64(digits);
124
6.92k
  }
125
11.2k
  const uint64_t digits = ReadUInt64(value, pos, length);
126
11.2k
  MultiplyByPowerOfTen(length);
127
11.2k
  AddUInt64(digits);
128
11.2k
  Clamp();
129
11.2k
}
130
131
132
0
static uint64_t HexCharValue(const int c) {
133
0
  if ('0' <= c && c <= '9') {
134
0
    return c - '0';
135
0
  }
136
0
  if ('a' <= c && c <= 'f') {
137
0
    return 10 + c - 'a';
138
0
  }
139
0
  DOUBLE_CONVERSION_ASSERT('A' <= c && c <= 'F');
140
0
  return 10 + c - 'A';
141
0
}
142
143
144
// Unlike AssignDecimalString(), this function is "only" used
145
// for unit-tests and therefore not performance critical.
146
0
void Bignum::AssignHexString(Vector<const char> value) {
147
0
  Zero();
148
  // Required capacity could be reduced by ignoring leading zeros.
149
0
  EnsureCapacity(((value.length() * 4) + kBigitSize - 1) / kBigitSize);
150
0
  DOUBLE_CONVERSION_ASSERT(sizeof(uint64_t) * 8 >= kBigitSize + 4);  // TODO: static_assert
151
  // Accumulates converted hex digits until at least kBigitSize bits.
152
  // Works with non-factor-of-four kBigitSizes.
153
0
  uint64_t tmp = 0;
154
0
  for (int cnt = 0; !value.is_empty(); value.pop_back()) {
155
0
    tmp |= (HexCharValue(value.last()) << cnt);
156
0
    if ((cnt += 4) >= kBigitSize) {
157
0
      RawBigit(used_bigits_++) = (tmp & kBigitMask);
158
0
      cnt -= kBigitSize;
159
0
      tmp >>= kBigitSize;
160
0
    }
161
0
  }
162
0
  if (tmp > 0) {
163
0
    DOUBLE_CONVERSION_ASSERT(tmp <= kBigitMask);
164
0
    RawBigit(used_bigits_++) = static_cast<Bignum::Chunk>(tmp & kBigitMask);
165
0
  }
166
0
  Clamp();
167
0
}
168
169
170
18.1k
void Bignum::AddUInt64(const uint64_t operand) {
171
18.1k
  if (operand == 0) {
172
1.74k
    return;
173
1.74k
  }
174
16.4k
  Bignum other;
175
16.4k
  other.AssignUInt64(operand);
176
16.4k
  AddBignum(other);
177
16.4k
}
178
179
180
16.4k
void Bignum::AddBignum(const Bignum& other) {
181
16.4k
  DOUBLE_CONVERSION_ASSERT(IsClamped());
182
16.4k
  DOUBLE_CONVERSION_ASSERT(other.IsClamped());
183
184
  // If this has a greater exponent than other append zero-bigits to this.
185
  // After this call exponent_ <= other.exponent_.
186
16.4k
  Align(other);
187
188
  // There are two possibilities:
189
  //   aaaaaaaaaaa 0000  (where the 0s represent a's exponent)
190
  //     bbbbb 00000000
191
  //   ----------------
192
  //   ccccccccccc 0000
193
  // or
194
  //    aaaaaaaaaa 0000
195
  //  bbbbbbbbb 0000000
196
  //  -----------------
197
  //  cccccccccccc 0000
198
  // In both cases we might need a carry bigit.
199
200
16.4k
  EnsureCapacity(1 + (std::max)(BigitLength(), other.BigitLength()) - exponent_);
201
16.4k
  Chunk carry = 0;
202
16.4k
  int bigit_pos = other.exponent_ - exponent_;
203
16.4k
  DOUBLE_CONVERSION_ASSERT(bigit_pos >= 0);
204
16.4k
  for (int i = used_bigits_; i < bigit_pos; ++i) {
205
0
    RawBigit(i) = 0;
206
0
  }
207
51.2k
  for (int i = 0; i < other.used_bigits_; ++i) {
208
34.8k
    const Chunk my = (bigit_pos < used_bigits_) ? RawBigit(bigit_pos) : 0;
209
34.8k
    const Chunk sum = my + other.RawBigit(i) + carry;
210
34.8k
    RawBigit(bigit_pos) = sum & kBigitMask;
211
34.8k
    carry = sum >> kBigitSize;
212
34.8k
    ++bigit_pos;
213
34.8k
  }
214
17.5k
  while (carry != 0) {
215
1.17k
    const Chunk my = (bigit_pos < used_bigits_) ? RawBigit(bigit_pos) : 0;
216
1.17k
    const Chunk sum = my + carry;
217
1.17k
    RawBigit(bigit_pos) = sum & kBigitMask;
218
1.17k
    carry = sum >> kBigitSize;
219
1.17k
    ++bigit_pos;
220
1.17k
  }
221
16.4k
  used_bigits_ = static_cast<int16_t>(std::max(bigit_pos, static_cast<int>(used_bigits_)));
222
16.4k
  DOUBLE_CONVERSION_ASSERT(IsClamped());
223
16.4k
}
224
225
226
192k
void Bignum::SubtractBignum(const Bignum& other) {
227
192k
  DOUBLE_CONVERSION_ASSERT(IsClamped());
228
192k
  DOUBLE_CONVERSION_ASSERT(other.IsClamped());
229
  // We require this to be bigger than other.
230
192k
  DOUBLE_CONVERSION_ASSERT(LessEqual(other, *this));
231
232
192k
  Align(other);
233
234
192k
  const int offset = other.exponent_ - exponent_;
235
192k
  Chunk borrow = 0;
236
192k
  int i;
237
1.92M
  for (i = 0; i < other.used_bigits_; ++i) {
238
1.73M
    DOUBLE_CONVERSION_ASSERT((borrow == 0) || (borrow == 1));
239
1.73M
    const Chunk difference = RawBigit(i + offset) - other.RawBigit(i) - borrow;
240
1.73M
    RawBigit(i + offset) = difference & kBigitMask;
241
1.73M
    borrow = difference >> (kChunkSize - 1);
242
1.73M
  }
243
221k
  while (borrow != 0) {
244
28.4k
    const Chunk difference = RawBigit(i + offset) - borrow;
245
28.4k
    RawBigit(i + offset) = difference & kBigitMask;
246
28.4k
    borrow = difference >> (kChunkSize - 1);
247
28.4k
    ++i;
248
28.4k
  }
249
192k
  Clamp();
250
192k
}
251
252
253
94.8k
void Bignum::ShiftLeft(const int shift_amount) {
254
94.8k
  if (used_bigits_ == 0) {
255
0
    return;
256
0
  }
257
94.8k
  exponent_ += static_cast<int16_t>(shift_amount / kBigitSize);
258
94.8k
  const int local_shift = shift_amount % kBigitSize;
259
94.8k
  EnsureCapacity(used_bigits_ + 1);
260
94.8k
  BigitsShiftLeft(local_shift);
261
94.8k
}
262
263
264
443k
void Bignum::MultiplyByUInt32(const uint32_t factor) {
265
443k
  if (factor == 1) {
266
0
    return;
267
0
  }
268
443k
  if (factor == 0) {
269
0
    Zero();
270
0
    return;
271
0
  }
272
443k
  if (used_bigits_ == 0) {
273
0
    return;
274
0
  }
275
  // The product of a bigit with the factor is of size kBigitSize + 32.
276
  // Assert that this number + 1 (for the carry) fits into double chunk.
277
443k
  DOUBLE_CONVERSION_ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1);
278
443k
  DoubleChunk carry = 0;
279
3.00M
  for (int i = 0; i < used_bigits_; ++i) {
280
2.56M
    const DoubleChunk product = static_cast<DoubleChunk>(factor) * RawBigit(i) + carry;
281
2.56M
    RawBigit(i) = static_cast<Chunk>(product & kBigitMask);
282
2.56M
    carry = (product >> kBigitSize);
283
2.56M
  }
284
549k
  while (carry != 0) {
285
106k
    EnsureCapacity(used_bigits_ + 1);
286
106k
    RawBigit(used_bigits_) = carry & kBigitMask;
287
106k
    used_bigits_++;
288
106k
    carry >>= kBigitSize;
289
106k
  }
290
443k
}
291
292
293
36.0k
void Bignum::MultiplyByUInt64(const uint64_t factor) {
294
36.0k
  if (factor == 1) {
295
0
    return;
296
0
  }
297
36.0k
  if (factor == 0) {
298
0
    Zero();
299
0
    return;
300
0
  }
301
36.0k
  if (used_bigits_ == 0) {
302
0
    return;
303
0
  }
304
36.0k
  DOUBLE_CONVERSION_ASSERT(kBigitSize < 32);
305
36.0k
  uint64_t carry = 0;
306
36.0k
  const uint64_t low = factor & 0xFFFFFFFF;
307
36.0k
  const uint64_t high = factor >> 32;
308
411k
  for (int i = 0; i < used_bigits_; ++i) {
309
375k
    const uint64_t product_low = low * RawBigit(i);
310
375k
    const uint64_t product_high = high * RawBigit(i);
311
375k
    const uint64_t tmp = (carry & kBigitMask) + product_low;
312
375k
    RawBigit(i) = tmp & kBigitMask;
313
375k
    carry = (carry >> kBigitSize) + (tmp >> kBigitSize) +
314
375k
        (product_high << (32 - kBigitSize));
315
375k
  }
316
118k
  while (carry != 0) {
317
82.8k
    EnsureCapacity(used_bigits_ + 1);
318
82.8k
    RawBigit(used_bigits_) = carry & kBigitMask;
319
82.8k
    used_bigits_++;
320
82.8k
    carry >>= kBigitSize;
321
82.8k
  }
322
36.0k
}
323
324
325
29.3k
void Bignum::MultiplyByPowerOfTen(const int exponent) {
326
29.3k
  static const uint64_t kFive27 = DOUBLE_CONVERSION_UINT64_2PART_C(0x6765c793, fa10079d);
327
29.3k
  static const uint16_t kFive1 = 5;
328
29.3k
  static const uint16_t kFive2 = kFive1 * 5;
329
29.3k
  static const uint16_t kFive3 = kFive2 * 5;
330
29.3k
  static const uint16_t kFive4 = kFive3 * 5;
331
29.3k
  static const uint16_t kFive5 = kFive4 * 5;
332
29.3k
  static const uint16_t kFive6 = kFive5 * 5;
333
29.3k
  static const uint32_t kFive7 = kFive6 * 5;
334
29.3k
  static const uint32_t kFive8 = kFive7 * 5;
335
29.3k
  static const uint32_t kFive9 = kFive8 * 5;
336
29.3k
  static const uint32_t kFive10 = kFive9 * 5;
337
29.3k
  static const uint32_t kFive11 = kFive10 * 5;
338
29.3k
  static const uint32_t kFive12 = kFive11 * 5;
339
29.3k
  static const uint32_t kFive13 = kFive12 * 5;
340
29.3k
  static const uint32_t kFive1_to_12[] =
341
29.3k
      { kFive1, kFive2, kFive3, kFive4, kFive5, kFive6,
342
29.3k
        kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 };
343
344
29.3k
  DOUBLE_CONVERSION_ASSERT(exponent >= 0);
345
346
29.3k
  if (exponent == 0) {
347
2.65k
    return;
348
2.65k
  }
349
26.7k
  if (used_bigits_ == 0) {
350
11.2k
    return;
351
11.2k
  }
352
  // We shift by exponent at the end just before returning.
353
15.4k
  int remaining_exponent = exponent;
354
50.0k
  while (remaining_exponent >= 27) {
355
34.6k
    MultiplyByUInt64(kFive27);
356
34.6k
    remaining_exponent -= 27;
357
34.6k
  }
358
23.9k
  while (remaining_exponent >= 13) {
359
8.50k
    MultiplyByUInt32(kFive13);
360
8.50k
    remaining_exponent -= 13;
361
8.50k
  }
362
15.4k
  if (remaining_exponent > 0) {
363
12.1k
    MultiplyByUInt32(kFive1_to_12[remaining_exponent - 1]);
364
12.1k
  }
365
15.4k
  ShiftLeft(exponent);
366
15.4k
}
367
368
369
14.9k
void Bignum::Square() {
370
14.9k
  DOUBLE_CONVERSION_ASSERT(IsClamped());
371
14.9k
  const int product_length = 2 * used_bigits_;
372
14.9k
  EnsureCapacity(product_length);
373
374
  // Comba multiplication: compute each column separately.
375
  // Example: r = a2a1a0 * b2b1b0.
376
  //    r =  1    * a0b0 +
377
  //        10    * (a1b0 + a0b1) +
378
  //        100   * (a2b0 + a1b1 + a0b2) +
379
  //        1000  * (a2b1 + a1b2) +
380
  //        10000 * a2b2
381
  //
382
  // In the worst case we have to accumulate nb-digits products of digit*digit.
383
  //
384
  // Assert that the additional number of bits in a DoubleChunk are enough to
385
  // sum up used_digits of Bigit*Bigit.
386
14.9k
  if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_bigits_) {
387
0
    DOUBLE_CONVERSION_UNIMPLEMENTED();
388
0
  }
389
14.9k
  DoubleChunk accumulator = 0;
390
  // First shift the digits so we don't overwrite them.
391
14.9k
  const int copy_offset = used_bigits_;
392
96.9k
  for (int i = 0; i < used_bigits_; ++i) {
393
81.9k
    RawBigit(copy_offset + i) = RawBigit(i);
394
81.9k
  }
395
  // We have two loops to avoid some 'if's in the loop.
396
96.9k
  for (int i = 0; i < used_bigits_; ++i) {
397
    // Process temporary digit i with power i.
398
    // The sum of the two indices must be equal to i.
399
81.9k
    int bigit_index1 = i;
400
81.9k
    int bigit_index2 = 0;
401
    // Sum all of the sub-products.
402
448k
    while (bigit_index1 >= 0) {
403
366k
      const Chunk chunk1 = RawBigit(copy_offset + bigit_index1);
404
366k
      const Chunk chunk2 = RawBigit(copy_offset + bigit_index2);
405
366k
      accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
406
366k
      bigit_index1--;
407
366k
      bigit_index2++;
408
366k
    }
409
81.9k
    RawBigit(i) = static_cast<Chunk>(accumulator) & kBigitMask;
410
81.9k
    accumulator >>= kBigitSize;
411
81.9k
  }
412
96.9k
  for (int i = used_bigits_; i < product_length; ++i) {
413
81.9k
    int bigit_index1 = used_bigits_ - 1;
414
81.9k
    int bigit_index2 = i - bigit_index1;
415
    // Invariant: sum of both indices is again equal to i.
416
    // Inner loop runs 0 times on last iteration, emptying accumulator.
417
366k
    while (bigit_index2 < used_bigits_) {
418
284k
      const Chunk chunk1 = RawBigit(copy_offset + bigit_index1);
419
284k
      const Chunk chunk2 = RawBigit(copy_offset + bigit_index2);
420
284k
      accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
421
284k
      bigit_index1--;
422
284k
      bigit_index2++;
423
284k
    }
424
    // The overwritten RawBigit(i) will never be read in further loop iterations,
425
    // because bigit_index1 and bigit_index2 are always greater
426
    // than i - used_bigits_.
427
81.9k
    RawBigit(i) = static_cast<Chunk>(accumulator) & kBigitMask;
428
81.9k
    accumulator >>= kBigitSize;
429
81.9k
  }
430
  // Since the result was guaranteed to lie inside the number the
431
  // accumulator must be 0 now.
432
14.9k
  DOUBLE_CONVERSION_ASSERT(accumulator == 0);
433
434
  // Don't forget to update the used_digits and the exponent.
435
14.9k
  used_bigits_ = static_cast<int16_t>(product_length);
436
14.9k
  exponent_ *= 2;
437
14.9k
  Clamp();
438
14.9k
}
439
440
441
12.7k
void Bignum::AssignPowerUInt16(uint16_t base, const int power_exponent) {
442
12.7k
  DOUBLE_CONVERSION_ASSERT(base != 0);
443
12.7k
  DOUBLE_CONVERSION_ASSERT(power_exponent >= 0);
444
12.7k
  if (power_exponent == 0) {
445
894
    AssignUInt16(1);
446
894
    return;
447
894
  }
448
11.8k
  Zero();
449
11.8k
  int shifts = 0;
450
  // We expect base to be in range 2-32, and most often to be 10.
451
  // It does not make much sense to implement different algorithms for counting
452
  // the bits.
453
23.6k
  while ((base & 1) == 0) {
454
11.8k
    base >>= 1;
455
11.8k
    shifts++;
456
11.8k
  }
457
11.8k
  int bit_size = 0;
458
11.8k
  int tmp_base = base;
459
47.2k
  while (tmp_base != 0) {
460
35.4k
    tmp_base >>= 1;
461
35.4k
    bit_size++;
462
35.4k
  }
463
11.8k
  const int final_size = bit_size * power_exponent;
464
  // 1 extra bigit for the shifting, and one for rounded final_size.
465
11.8k
  EnsureCapacity(final_size / kBigitSize + 2);
466
467
  // Left to Right exponentiation.
468
11.8k
  int mask = 1;
469
79.1k
  while (power_exponent >= mask) mask <<= 1;
470
471
  // The mask is now pointing to the bit above the most significant 1-bit of
472
  // power_exponent.
473
  // Get rid of first 1-bit;
474
11.8k
  mask >>= 2;
475
11.8k
  uint64_t this_value = base;
476
477
11.8k
  bool delayed_multiplication = false;
478
11.8k
  const uint64_t max_32bits = 0xFFFFFFFF;
479
52.3k
  while (mask != 0 && this_value <= max_32bits) {
480
40.5k
    this_value = this_value * this_value;
481
    // Verify that there is enough space in this_value to perform the
482
    // multiplication.  The first bit_size bits must be 0.
483
40.5k
    if ((power_exponent & mask) != 0) {
484
15.7k
      DOUBLE_CONVERSION_ASSERT(bit_size > 0);
485
15.7k
      const uint64_t base_bits_mask =
486
15.7k
        ~((static_cast<uint64_t>(1) << (64 - bit_size)) - 1);
487
15.7k
      const bool high_bits_zero = (this_value & base_bits_mask) == 0;
488
15.7k
      if (high_bits_zero) {
489
15.7k
        this_value *= base;
490
15.7k
      } else {
491
0
        delayed_multiplication = true;
492
0
      }
493
15.7k
    }
494
40.5k
    mask >>= 1;
495
40.5k
  }
496
11.8k
  AssignUInt64(this_value);
497
11.8k
  if (delayed_multiplication) {
498
0
    MultiplyByUInt32(base);
499
0
  }
500
501
  // Now do the same thing as a bignum.
502
26.7k
  while (mask != 0) {
503
14.9k
    Square();
504
14.9k
    if ((power_exponent & mask) != 0) {
505
7.89k
      MultiplyByUInt32(base);
506
7.89k
    }
507
14.9k
    mask >>= 1;
508
14.9k
  }
509
510
  // And finally add the saved shifts.
511
11.8k
  ShiftLeft(shifts * power_exponent);
512
11.8k
}
513
514
515
// Precondition: this/other < 16bit.
516
204k
uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) {
517
204k
  DOUBLE_CONVERSION_ASSERT(IsClamped());
518
204k
  DOUBLE_CONVERSION_ASSERT(other.IsClamped());
519
204k
  DOUBLE_CONVERSION_ASSERT(other.used_bigits_ > 0);
520
521
  // Easy case: if we have less digits than the divisor than the result is 0.
522
  // Note: this handles the case where this == 0, too.
523
204k
  if (BigitLength() < other.BigitLength()) {
524
13.3k
    return 0;
525
13.3k
  }
526
527
191k
  Align(other);
528
529
191k
  uint16_t result = 0;
530
531
  // Start by removing multiples of 'other' until both numbers have the same
532
  // number of digits.
533
243k
  while (BigitLength() > other.BigitLength()) {
534
    // This naive approach is extremely inefficient if `this` divided by other
535
    // is big. This function is implemented for doubleToString where
536
    // the result should be small (less than 10).
537
52.7k
    DOUBLE_CONVERSION_ASSERT(other.RawBigit(other.used_bigits_ - 1) >= ((1 << kBigitSize) / 16));
538
52.7k
    DOUBLE_CONVERSION_ASSERT(RawBigit(used_bigits_ - 1) < 0x10000);
539
    // Remove the multiples of the first digit.
540
    // Example this = 23 and other equals 9. -> Remove 2 multiples.
541
52.7k
    result += static_cast<uint16_t>(RawBigit(used_bigits_ - 1));
542
52.7k
    SubtractTimes(other, RawBigit(used_bigits_ - 1));
543
52.7k
  }
544
545
191k
  DOUBLE_CONVERSION_ASSERT(BigitLength() == other.BigitLength());
546
547
  // Both bignums are at the same length now.
548
  // Since other has more than 0 digits we know that the access to
549
  // RawBigit(used_bigits_ - 1) is safe.
550
191k
  const Chunk this_bigit = RawBigit(used_bigits_ - 1);
551
191k
  const Chunk other_bigit = other.RawBigit(other.used_bigits_ - 1);
552
553
191k
  if (other.used_bigits_ == 1) {
554
    // Shortcut for easy (and common) case.
555
33.3k
    int quotient = this_bigit / other_bigit;
556
33.3k
    RawBigit(used_bigits_ - 1) = this_bigit - other_bigit * quotient;
557
33.3k
    DOUBLE_CONVERSION_ASSERT(quotient < 0x10000);
558
33.3k
    result += static_cast<uint16_t>(quotient);
559
33.3k
    Clamp();
560
33.3k
    return result;
561
33.3k
  }
562
563
157k
  const int division_estimate = this_bigit / (other_bigit + 1);
564
157k
  DOUBLE_CONVERSION_ASSERT(division_estimate < 0x10000);
565
157k
  result += static_cast<uint16_t>(division_estimate);
566
157k
  SubtractTimes(other, division_estimate);
567
568
157k
  if (other_bigit * (division_estimate + 1) > this_bigit) {
569
    // No need to even try to subtract. Even if other's remaining digits were 0
570
    // another subtraction would be too much.
571
88.0k
    return result;
572
88.0k
  }
573
574
154k
  while (LessEqual(other, *this)) {
575
84.6k
    SubtractBignum(other);
576
84.6k
    result++;
577
84.6k
  }
578
69.7k
  return result;
579
157k
}
580
581
582
template<typename S>
583
0
static int SizeInHexChars(S number) {
584
0
  DOUBLE_CONVERSION_ASSERT(number > 0);
585
0
  int result = 0;
586
0
  while (number != 0) {
587
0
    number >>= 4;
588
0
    result++;
589
0
  }
590
0
  return result;
591
0
}
592
593
594
0
static char HexCharOfValue(const int value) {
595
0
  DOUBLE_CONVERSION_ASSERT(0 <= value && value <= 16);
596
0
  if (value < 10) {
597
0
    return static_cast<char>(value + '0');
598
0
  }
599
0
  return static_cast<char>(value - 10 + 'A');
600
0
}
601
602
603
0
bool Bignum::ToHexString(char* buffer, const int buffer_size) const {
604
0
  DOUBLE_CONVERSION_ASSERT(IsClamped());
605
  // Each bigit must be printable as separate hex-character.
606
0
  DOUBLE_CONVERSION_ASSERT(kBigitSize % 4 == 0);
607
0
  static const int kHexCharsPerBigit = kBigitSize / 4;
608
609
0
  if (used_bigits_ == 0) {
610
0
    if (buffer_size < 2) {
611
0
      return false;
612
0
    }
613
0
    buffer[0] = '0';
614
0
    buffer[1] = '\0';
615
0
    return true;
616
0
  }
617
  // We add 1 for the terminating '\0' character.
618
0
  const int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit +
619
0
    SizeInHexChars(RawBigit(used_bigits_ - 1)) + 1;
620
0
  if (needed_chars > buffer_size) {
621
0
    return false;
622
0
  }
623
0
  int string_index = needed_chars - 1;
624
0
  buffer[string_index--] = '\0';
625
0
  for (int i = 0; i < exponent_; ++i) {
626
0
    for (int j = 0; j < kHexCharsPerBigit; ++j) {
627
0
      buffer[string_index--] = '0';
628
0
    }
629
0
  }
630
0
  for (int i = 0; i < used_bigits_ - 1; ++i) {
631
0
    Chunk current_bigit = RawBigit(i);
632
0
    for (int j = 0; j < kHexCharsPerBigit; ++j) {
633
0
      buffer[string_index--] = HexCharOfValue(current_bigit & 0xF);
634
0
      current_bigit >>= 4;
635
0
    }
636
0
  }
637
  // And finally the last bigit.
638
0
  Chunk most_significant_bigit = RawBigit(used_bigits_ - 1);
639
0
  while (most_significant_bigit != 0) {
640
0
    buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF);
641
0
    most_significant_bigit >>= 4;
642
0
  }
643
0
  return true;
644
0
}
645
646
647
1.56M
Bignum::Chunk Bignum::BigitOrZero(const int index) const {
648
1.56M
  if (index >= BigitLength()) {
649
263k
    return 0;
650
263k
  }
651
1.30M
  if (index < exponent_) {
652
15.6k
    return 0;
653
15.6k
  }
654
1.28M
  return RawBigit(index - exponent_);
655
1.30M
}
656
657
658
392k
int Bignum::Compare(const Bignum& a, const Bignum& b) {
659
392k
  DOUBLE_CONVERSION_ASSERT(a.IsClamped());
660
392k
  DOUBLE_CONVERSION_ASSERT(b.IsClamped());
661
392k
  const int bigit_length_a = a.BigitLength();
662
392k
  const int bigit_length_b = b.BigitLength();
663
392k
  if (bigit_length_a < bigit_length_b) {
664
4.29k
    return -1;
665
4.29k
  }
666
388k
  if (bigit_length_a > bigit_length_b) {
667
167k
    return +1;
668
167k
  }
669
354k
  for (int i = bigit_length_a - 1; i >= (std::min)(a.exponent_, b.exponent_); --i) {
670
326k
    const Chunk bigit_a = a.BigitOrZero(i);
671
326k
    const Chunk bigit_b = b.BigitOrZero(i);
672
326k
    if (bigit_a < bigit_b) {
673
94.5k
      return -1;
674
94.5k
    }
675
231k
    if (bigit_a > bigit_b) {
676
98.6k
      return +1;
677
98.6k
    }
678
    // Otherwise they are equal up to this digit. Try the next digit.
679
231k
  }
680
27.8k
  return 0;
681
220k
}
682
683
684
229k
int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) {
685
229k
  DOUBLE_CONVERSION_ASSERT(a.IsClamped());
686
229k
  DOUBLE_CONVERSION_ASSERT(b.IsClamped());
687
229k
  DOUBLE_CONVERSION_ASSERT(c.IsClamped());
688
229k
  if (a.BigitLength() < b.BigitLength()) {
689
4.03k
    return PlusCompare(b, a, c);
690
4.03k
  }
691
225k
  if (a.BigitLength() + 1 < c.BigitLength()) {
692
2.70k
    return -1;
693
2.70k
  }
694
222k
  if (a.BigitLength() > c.BigitLength()) {
695
1.04k
    return +1;
696
1.04k
  }
697
  // The exponent encodes 0-bigits. So if there are more 0-digits in 'a' than
698
  // 'b' has digits, then the bigit-length of 'a'+'b' must be equal to the one
699
  // of 'a'.
700
221k
  if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) {
701
0
    return -1;
702
0
  }
703
704
221k
  Chunk borrow = 0;
705
  // Starting at min_exponent all digits are == 0. So no need to compare them.
706
221k
  const int min_exponent = (std::min)((std::min)(a.exponent_, b.exponent_), c.exponent_);
707
308k
  for (int i = c.BigitLength() - 1; i >= min_exponent; --i) {
708
303k
    const Chunk chunk_a = a.BigitOrZero(i);
709
303k
    const Chunk chunk_b = b.BigitOrZero(i);
710
303k
    const Chunk chunk_c = c.BigitOrZero(i);
711
303k
    const Chunk sum = chunk_a + chunk_b;
712
303k
    if (sum > chunk_c + borrow) {
713
13.7k
      return +1;
714
290k
    } else {
715
290k
      borrow = chunk_c + borrow - sum;
716
290k
      if (borrow > 1) {
717
202k
        return -1;
718
202k
      }
719
87.2k
      borrow <<= kBigitSize;
720
87.2k
    }
721
303k
  }
722
4.74k
  if (borrow == 0) {
723
4.74k
    return 0;
724
4.74k
  }
725
0
  return -1;
726
4.74k
}
727
728
729
350k
void Bignum::Clamp() {
730
430k
  while (used_bigits_ > 0 && RawBigit(used_bigits_ - 1) == 0) {
731
80.3k
    used_bigits_--;
732
80.3k
  }
733
350k
  if (used_bigits_ == 0) {
734
    // Zero.
735
1.31k
    exponent_ = 0;
736
1.31k
  }
737
350k
}
738
739
740
400k
void Bignum::Align(const Bignum& other) {
741
400k
  if (exponent_ > other.exponent_) {
742
    // If "X" represents a "hidden" bigit (by the exponent) then we are in the
743
    // following case (a == this, b == other):
744
    // a:  aaaaaaXXXX   or a:   aaaaaXXX
745
    // b:     bbbbbbX      b: bbbbbbbbXX
746
    // We replace some of the hidden digits (X) of a with 0 digits.
747
    // a:  aaaaaa000X   or a:   aaaaa0XX
748
4.38k
    const int zero_bigits = exponent_ - other.exponent_;
749
4.38k
    EnsureCapacity(used_bigits_ + zero_bigits);
750
17.4k
    for (int i = used_bigits_ - 1; i >= 0; --i) {
751
13.0k
      RawBigit(i + zero_bigits) = RawBigit(i);
752
13.0k
    }
753
63.3k
    for (int i = 0; i < zero_bigits; ++i) {
754
58.9k
      RawBigit(i) = 0;
755
58.9k
    }
756
4.38k
    used_bigits_ += static_cast<int16_t>(zero_bigits);
757
4.38k
    exponent_ -= static_cast<int16_t>(zero_bigits);
758
759
4.38k
    DOUBLE_CONVERSION_ASSERT(used_bigits_ >= 0);
760
4.38k
    DOUBLE_CONVERSION_ASSERT(exponent_ >= 0);
761
4.38k
  }
762
400k
}
763
764
765
94.8k
void Bignum::BigitsShiftLeft(const int shift_amount) {
766
94.8k
  DOUBLE_CONVERSION_ASSERT(shift_amount < kBigitSize);
767
94.8k
  DOUBLE_CONVERSION_ASSERT(shift_amount >= 0);
768
94.8k
  Chunk carry = 0;
769
564k
  for (int i = 0; i < used_bigits_; ++i) {
770
469k
    const Chunk new_carry = RawBigit(i) >> (kBigitSize - shift_amount);
771
469k
    RawBigit(i) = ((RawBigit(i) << shift_amount) + carry) & kBigitMask;
772
469k
    carry = new_carry;
773
469k
  }
774
94.8k
  if (carry != 0) {
775
28.7k
    RawBigit(used_bigits_) = carry;
776
28.7k
    used_bigits_++;
777
28.7k
  }
778
94.8k
}
779
780
781
210k
void Bignum::SubtractTimes(const Bignum& other, const int factor) {
782
210k
  DOUBLE_CONVERSION_ASSERT(exponent_ <= other.exponent_);
783
210k
  if (factor < 3) {
784
220k
    for (int i = 0; i < factor; ++i) {
785
107k
      SubtractBignum(other);
786
107k
    }
787
112k
    return;
788
112k
  }
789
98.1k
  Chunk borrow = 0;
790
98.1k
  const int exponent_diff = other.exponent_ - exponent_;
791
966k
  for (int i = 0; i < other.used_bigits_; ++i) {
792
868k
    const DoubleChunk product = static_cast<DoubleChunk>(factor) * other.RawBigit(i);
793
868k
    const DoubleChunk remove = borrow + product;
794
868k
    const Chunk difference = RawBigit(i + exponent_diff) - (remove & kBigitMask);
795
868k
    RawBigit(i + exponent_diff) = difference & kBigitMask;
796
868k
    borrow = static_cast<Chunk>((difference >> (kChunkSize - 1)) +
797
868k
                                (remove >> kBigitSize));
798
868k
  }
799
109k
  for (int i = other.used_bigits_ + exponent_diff; i < used_bigits_; ++i) {
800
11.7k
    if (borrow == 0) {
801
0
      return;
802
0
    }
803
11.7k
    const Chunk difference = RawBigit(i) - borrow;
804
11.7k
    RawBigit(i) = difference & kBigitMask;
805
11.7k
    borrow = difference >> (kChunkSize - 1);
806
11.7k
  }
807
98.1k
  Clamp();
808
98.1k
}
809
810
811
}  // namespace double_conversion
812
813
// ICU PATCH: Close ICU namespace
814
U_NAMESPACE_END
815
#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING