Coverage Report

Created: 2025-09-05 07:16

/src/icu/icu4c/source/i18n/double-conversion-bignum.cpp
Line
Count
Source (jump to first uncovered line)
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
16.6M
Bignum::Chunk& Bignum::RawBigit(const int index) {
51
16.6M
  DOUBLE_CONVERSION_ASSERT(static_cast<unsigned>(index) < kBigitCapacity);
52
16.6M
  return bigits_buffer_[index];
53
16.6M
}
54
55
56
4.52M
const Bignum::Chunk& Bignum::RawBigit(const int index) const {
57
4.52M
  DOUBLE_CONVERSION_ASSERT(static_cast<unsigned>(index) < kBigitCapacity);
58
4.52M
  return bigits_buffer_[index];
59
4.52M
}
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
26.4k
void Bignum::AssignUInt16(const uint16_t value) {
70
26.4k
  DOUBLE_CONVERSION_ASSERT(kBigitSize >= BitSize(value));
71
26.4k
  Zero();
72
26.4k
  if (value > 0) {
73
26.4k
    RawBigit(0) = value;
74
26.4k
    used_bigits_ = 1;
75
26.4k
  }
76
26.4k
}
77
78
79
101k
void Bignum::AssignUInt64(uint64_t value) {
80
101k
  Zero();
81
306k
  for(int i = 0; value > 0; ++i) {
82
204k
    RawBigit(i) = value & kBigitMask;
83
204k
    value >>= kBigitSize;
84
204k
    ++used_bigits_;
85
204k
  }
86
101k
}
87
88
89
15.1k
void Bignum::AssignBignum(const Bignum& other) {
90
15.1k
  exponent_ = other.exponent_;
91
78.0k
  for (int i = 0; i < other.used_bigits_; ++i) {
92
62.8k
    RawBigit(i) = other.RawBigit(i);
93
62.8k
  }
94
15.1k
  used_bigits_ = other.used_bigits_;
95
15.1k
}
96
97
98
static uint64_t ReadUInt64(const Vector<const char> buffer,
99
                           const int from,
100
50.8k
                           const int digits_to_read) {
101
50.8k
  uint64_t result = 0;
102
634k
  for (int i = from; i < from + digits_to_read; ++i) {
103
583k
    const int digit = buffer[i] - '0';
104
583k
    DOUBLE_CONVERSION_ASSERT(0 <= digit && digit <= 9);
105
583k
    result = result * 10 + digit;
106
583k
  }
107
50.8k
  return result;
108
50.8k
}
109
110
111
27.5k
void Bignum::AssignDecimalString(const Vector<const char> value) {
112
  // 2^64 = 18446744073709551616 > 10^19
113
27.5k
  static const int kMaxUint64DecimalDigits = 19;
114
27.5k
  Zero();
115
27.5k
  int length = value.length();
116
27.5k
  unsigned pos = 0;
117
  // Let's just say that each digit needs 4 bits.
118
50.8k
  while (length >= kMaxUint64DecimalDigits) {
119
23.3k
    const uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits);
120
23.3k
    pos += kMaxUint64DecimalDigits;
121
23.3k
    length -= kMaxUint64DecimalDigits;
122
23.3k
    MultiplyByPowerOfTen(kMaxUint64DecimalDigits);
123
23.3k
    AddUInt64(digits);
124
23.3k
  }
125
27.5k
  const uint64_t digits = ReadUInt64(value, pos, length);
126
27.5k
  MultiplyByPowerOfTen(length);
127
27.5k
  AddUInt64(digits);
128
27.5k
  Clamp();
129
27.5k
}
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
50.8k
void Bignum::AddUInt64(const uint64_t operand) {
171
50.8k
  if (operand == 0) {
172
917
    return;
173
917
  }
174
49.9k
  Bignum other;
175
49.9k
  other.AssignUInt64(operand);
176
49.9k
  AddBignum(other);
177
49.9k
}
178
179
180
49.9k
void Bignum::AddBignum(const Bignum& other) {
181
49.9k
  DOUBLE_CONVERSION_ASSERT(IsClamped());
182
49.9k
  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
49.9k
  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
49.9k
  EnsureCapacity(1 + (std::max)(BigitLength(), other.BigitLength()) - exponent_);
201
49.9k
  Chunk carry = 0;
202
49.9k
  int bigit_pos = other.exponent_ - exponent_;
203
49.9k
  DOUBLE_CONVERSION_ASSERT(bigit_pos >= 0);
204
49.9k
  for (int i = used_bigits_; i < bigit_pos; ++i) {
205
0
    RawBigit(i) = 0;
206
0
  }
207
152k
  for (int i = 0; i < other.used_bigits_; ++i) {
208
102k
    const Chunk my = (bigit_pos < used_bigits_) ? RawBigit(bigit_pos) : 0;
209
102k
    const Chunk sum = my + other.RawBigit(i) + carry;
210
102k
    RawBigit(bigit_pos) = sum & kBigitMask;
211
102k
    carry = sum >> kBigitSize;
212
102k
    ++bigit_pos;
213
102k
  }
214
50.3k
  while (carry != 0) {
215
337
    const Chunk my = (bigit_pos < used_bigits_) ? RawBigit(bigit_pos) : 0;
216
337
    const Chunk sum = my + carry;
217
337
    RawBigit(bigit_pos) = sum & kBigitMask;
218
337
    carry = sum >> kBigitSize;
219
337
    ++bigit_pos;
220
337
  }
221
49.9k
  used_bigits_ = static_cast<int16_t>(std::max(bigit_pos, static_cast<int>(used_bigits_)));
222
49.9k
  DOUBLE_CONVERSION_ASSERT(IsClamped());
223
49.9k
}
224
225
226
197k
void Bignum::SubtractBignum(const Bignum& other) {
227
197k
  DOUBLE_CONVERSION_ASSERT(IsClamped());
228
197k
  DOUBLE_CONVERSION_ASSERT(other.IsClamped());
229
  // We require this to be bigger than other.
230
197k
  DOUBLE_CONVERSION_ASSERT(LessEqual(other, *this));
231
232
197k
  Align(other);
233
234
197k
  const int offset = other.exponent_ - exponent_;
235
197k
  Chunk borrow = 0;
236
197k
  int i;
237
1.79M
  for (i = 0; i < other.used_bigits_; ++i) {
238
1.59M
    DOUBLE_CONVERSION_ASSERT((borrow == 0) || (borrow == 1));
239
1.59M
    const Chunk difference = RawBigit(i + offset) - other.RawBigit(i) - borrow;
240
1.59M
    RawBigit(i + offset) = difference & kBigitMask;
241
1.59M
    borrow = difference >> (kChunkSize - 1);
242
1.59M
  }
243
227k
  while (borrow != 0) {
244
30.3k
    const Chunk difference = RawBigit(i + offset) - borrow;
245
30.3k
    RawBigit(i + offset) = difference & kBigitMask;
246
30.3k
    borrow = difference >> (kChunkSize - 1);
247
30.3k
    ++i;
248
30.3k
  }
249
197k
  Clamp();
250
197k
}
251
252
253
148k
void Bignum::ShiftLeft(const int shift_amount) {
254
148k
  if (used_bigits_ == 0) {
255
0
    return;
256
0
  }
257
148k
  exponent_ += static_cast<int16_t>(shift_amount / kBigitSize);
258
148k
  const int local_shift = shift_amount % kBigitSize;
259
148k
  EnsureCapacity(used_bigits_ + 1);
260
148k
  BigitsShiftLeft(local_shift);
261
148k
}
262
263
264
518k
void Bignum::MultiplyByUInt32(const uint32_t factor) {
265
518k
  if (factor == 1) {
266
0
    return;
267
0
  }
268
518k
  if (factor == 0) {
269
0
    Zero();
270
0
    return;
271
0
  }
272
518k
  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
518k
  DOUBLE_CONVERSION_ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1);
278
518k
  DoubleChunk carry = 0;
279
3.31M
  for (int i = 0; i < used_bigits_; ++i) {
280
2.79M
    const DoubleChunk product = static_cast<DoubleChunk>(factor) * RawBigit(i) + carry;
281
2.79M
    RawBigit(i) = static_cast<Chunk>(product & kBigitMask);
282
2.79M
    carry = (product >> kBigitSize);
283
2.79M
  }
284
642k
  while (carry != 0) {
285
123k
    EnsureCapacity(used_bigits_ + 1);
286
123k
    RawBigit(used_bigits_) = carry & kBigitMask;
287
123k
    used_bigits_++;
288
123k
    carry >>= kBigitSize;
289
123k
  }
290
518k
}
291
292
293
22.9k
void Bignum::MultiplyByUInt64(const uint64_t factor) {
294
22.9k
  if (factor == 1) {
295
0
    return;
296
0
  }
297
22.9k
  if (factor == 0) {
298
0
    Zero();
299
0
    return;
300
0
  }
301
22.9k
  if (used_bigits_ == 0) {
302
0
    return;
303
0
  }
304
22.9k
  DOUBLE_CONVERSION_ASSERT(kBigitSize < 32);
305
22.9k
  uint64_t carry = 0;
306
22.9k
  const uint64_t low = factor & 0xFFFFFFFF;
307
22.9k
  const uint64_t high = factor >> 32;
308
258k
  for (int i = 0; i < used_bigits_; ++i) {
309
235k
    const uint64_t product_low = low * RawBigit(i);
310
235k
    const uint64_t product_high = high * RawBigit(i);
311
235k
    const uint64_t tmp = (carry & kBigitMask) + product_low;
312
235k
    RawBigit(i) = tmp & kBigitMask;
313
235k
    carry = (carry >> kBigitSize) + (tmp >> kBigitSize) +
314
235k
        (product_high << (32 - kBigitSize));
315
235k
  }
316
75.0k
  while (carry != 0) {
317
52.0k
    EnsureCapacity(used_bigits_ + 1);
318
52.0k
    RawBigit(used_bigits_) = carry & kBigitMask;
319
52.0k
    used_bigits_++;
320
52.0k
    carry >>= kBigitSize;
321
52.0k
  }
322
22.9k
}
323
324
325
78.3k
void Bignum::MultiplyByPowerOfTen(const int exponent) {
326
78.3k
  static const uint64_t kFive27 = DOUBLE_CONVERSION_UINT64_2PART_C(0x6765c793, fa10079d);
327
78.3k
  static const uint16_t kFive1 = 5;
328
78.3k
  static const uint16_t kFive2 = kFive1 * 5;
329
78.3k
  static const uint16_t kFive3 = kFive2 * 5;
330
78.3k
  static const uint16_t kFive4 = kFive3 * 5;
331
78.3k
  static const uint16_t kFive5 = kFive4 * 5;
332
78.3k
  static const uint16_t kFive6 = kFive5 * 5;
333
78.3k
  static const uint32_t kFive7 = kFive6 * 5;
334
78.3k
  static const uint32_t kFive8 = kFive7 * 5;
335
78.3k
  static const uint32_t kFive9 = kFive8 * 5;
336
78.3k
  static const uint32_t kFive10 = kFive9 * 5;
337
78.3k
  static const uint32_t kFive11 = kFive10 * 5;
338
78.3k
  static const uint32_t kFive12 = kFive11 * 5;
339
78.3k
  static const uint32_t kFive13 = kFive12 * 5;
340
78.3k
  static const uint32_t kFive1_to_12[] =
341
78.3k
      { kFive1, kFive2, kFive3, kFive4, kFive5, kFive6,
342
78.3k
        kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 };
343
344
78.3k
  DOUBLE_CONVERSION_ASSERT(exponent >= 0);
345
346
78.3k
  if (exponent == 0) {
347
2.90k
    return;
348
2.90k
  }
349
75.4k
  if (used_bigits_ == 0) {
350
27.5k
    return;
351
27.5k
  }
352
  // We shift by exponent at the end just before returning.
353
47.9k
  int remaining_exponent = exponent;
354
69.0k
  while (remaining_exponent >= 27) {
355
21.1k
    MultiplyByUInt64(kFive27);
356
21.1k
    remaining_exponent -= 27;
357
21.1k
  }
358
53.4k
  while (remaining_exponent >= 13) {
359
5.47k
    MultiplyByUInt32(kFive13);
360
5.47k
    remaining_exponent -= 13;
361
5.47k
  }
362
47.9k
  if (remaining_exponent > 0) {
363
45.6k
    MultiplyByUInt32(kFive1_to_12[remaining_exponent - 1]);
364
45.6k
  }
365
47.9k
  ShiftLeft(exponent);
366
47.9k
}
367
368
369
15.9k
void Bignum::Square() {
370
15.9k
  DOUBLE_CONVERSION_ASSERT(IsClamped());
371
15.9k
  const int product_length = 2 * used_bigits_;
372
15.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
15.9k
  if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_bigits_) {
387
0
    DOUBLE_CONVERSION_UNIMPLEMENTED();
388
0
  }
389
15.9k
  DoubleChunk accumulator = 0;
390
  // First shift the digits so we don't overwrite them.
391
15.9k
  const int copy_offset = used_bigits_;
392
105k
  for (int i = 0; i < used_bigits_; ++i) {
393
89.1k
    RawBigit(copy_offset + i) = RawBigit(i);
394
89.1k
  }
395
  // We have two loops to avoid some 'if's in the loop.
396
105k
  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
89.1k
    int bigit_index1 = i;
400
89.1k
    int bigit_index2 = 0;
401
    // Sum all of the sub-products.
402
492k
    while (bigit_index1 >= 0) {
403
403k
      const Chunk chunk1 = RawBigit(copy_offset + bigit_index1);
404
403k
      const Chunk chunk2 = RawBigit(copy_offset + bigit_index2);
405
403k
      accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
406
403k
      bigit_index1--;
407
403k
      bigit_index2++;
408
403k
    }
409
89.1k
    RawBigit(i) = static_cast<Chunk>(accumulator) & kBigitMask;
410
89.1k
    accumulator >>= kBigitSize;
411
89.1k
  }
412
105k
  for (int i = used_bigits_; i < product_length; ++i) {
413
89.1k
    int bigit_index1 = used_bigits_ - 1;
414
89.1k
    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
403k
    while (bigit_index2 < used_bigits_) {
418
313k
      const Chunk chunk1 = RawBigit(copy_offset + bigit_index1);
419
313k
      const Chunk chunk2 = RawBigit(copy_offset + bigit_index2);
420
313k
      accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
421
313k
      bigit_index1--;
422
313k
      bigit_index2++;
423
313k
    }
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
89.1k
    RawBigit(i) = static_cast<Chunk>(accumulator) & kBigitMask;
428
89.1k
    accumulator >>= kBigitSize;
429
89.1k
  }
430
  // Since the result was guaranteed to lie inside the number the
431
  // accumulator must be 0 now.
432
15.9k
  DOUBLE_CONVERSION_ASSERT(accumulator == 0);
433
434
  // Don't forget to update the used_digits and the exponent.
435
15.9k
  used_bigits_ = static_cast<int16_t>(product_length);
436
15.9k
  exponent_ *= 2;
437
15.9k
  Clamp();
438
15.9k
}
439
440
441
13.6k
void Bignum::AssignPowerUInt16(uint16_t base, const int power_exponent) {
442
13.6k
  DOUBLE_CONVERSION_ASSERT(base != 0);
443
13.6k
  DOUBLE_CONVERSION_ASSERT(power_exponent >= 0);
444
13.6k
  if (power_exponent == 0) {
445
955
    AssignUInt16(1);
446
955
    return;
447
955
  }
448
12.6k
  Zero();
449
12.6k
  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
25.3k
  while ((base & 1) == 0) {
454
12.6k
    base >>= 1;
455
12.6k
    shifts++;
456
12.6k
  }
457
12.6k
  int bit_size = 0;
458
12.6k
  int tmp_base = base;
459
50.7k
  while (tmp_base != 0) {
460
38.0k
    tmp_base >>= 1;
461
38.0k
    bit_size++;
462
38.0k
  }
463
12.6k
  const int final_size = bit_size * power_exponent;
464
  // 1 extra bigit for the shifting, and one for rounded final_size.
465
12.6k
  EnsureCapacity(final_size / kBigitSize + 2);
466
467
  // Left to Right exponentiation.
468
12.6k
  int mask = 1;
469
84.2k
  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
12.6k
  mask >>= 2;
475
12.6k
  uint64_t this_value = base;
476
477
12.6k
  bool delayed_multiplication = false;
478
12.6k
  const uint64_t max_32bits = 0xFFFFFFFF;
479
55.5k
  while (mask != 0 && this_value <= max_32bits) {
480
42.9k
    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
42.9k
    if ((power_exponent & mask) != 0) {
484
16.6k
      DOUBLE_CONVERSION_ASSERT(bit_size > 0);
485
16.6k
      const uint64_t base_bits_mask =
486
16.6k
        ~((static_cast<uint64_t>(1) << (64 - bit_size)) - 1);
487
16.6k
      const bool high_bits_zero = (this_value & base_bits_mask) == 0;
488
16.6k
      if (high_bits_zero) {
489
16.6k
        this_value *= base;
490
16.6k
      } else {
491
0
        delayed_multiplication = true;
492
0
      }
493
16.6k
    }
494
42.9k
    mask >>= 1;
495
42.9k
  }
496
12.6k
  AssignUInt64(this_value);
497
12.6k
  if (delayed_multiplication) {
498
0
    MultiplyByUInt32(base);
499
0
  }
500
501
  // Now do the same thing as a bignum.
502
28.6k
  while (mask != 0) {
503
15.9k
    Square();
504
15.9k
    if ((power_exponent & mask) != 0) {
505
8.40k
      MultiplyByUInt32(base);
506
8.40k
    }
507
15.9k
    mask >>= 1;
508
15.9k
  }
509
510
  // And finally add the saved shifts.
511
12.6k
  ShiftLeft(shifts * power_exponent);
512
12.6k
}
513
514
515
// Precondition: this/other < 16bit.
516
224k
uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) {
517
224k
  DOUBLE_CONVERSION_ASSERT(IsClamped());
518
224k
  DOUBLE_CONVERSION_ASSERT(other.IsClamped());
519
224k
  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
224k
  if (BigitLength() < other.BigitLength()) {
524
10.7k
    return 0;
525
10.7k
  }
526
527
214k
  Align(other);
528
529
214k
  uint16_t result = 0;
530
531
  // Start by removing multiples of 'other' until both numbers have the same
532
  // number of digits.
533
270k
  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
56.4k
    DOUBLE_CONVERSION_ASSERT(other.RawBigit(other.used_bigits_ - 1) >= ((1 << kBigitSize) / 16));
538
56.4k
    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
56.4k
    result += static_cast<uint16_t>(RawBigit(used_bigits_ - 1));
542
56.4k
    SubtractTimes(other, RawBigit(used_bigits_ - 1));
543
56.4k
  }
544
545
214k
  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
214k
  const Chunk this_bigit = RawBigit(used_bigits_ - 1);
551
214k
  const Chunk other_bigit = other.RawBigit(other.used_bigits_ - 1);
552
553
214k
  if (other.used_bigits_ == 1) {
554
    // Shortcut for easy (and common) case.
555
35.9k
    int quotient = this_bigit / other_bigit;
556
35.9k
    RawBigit(used_bigits_ - 1) = this_bigit - other_bigit * quotient;
557
35.9k
    DOUBLE_CONVERSION_ASSERT(quotient < 0x10000);
558
35.9k
    result += static_cast<uint16_t>(quotient);
559
35.9k
    Clamp();
560
35.9k
    return result;
561
35.9k
  }
562
563
178k
  const int division_estimate = this_bigit / (other_bigit + 1);
564
178k
  DOUBLE_CONVERSION_ASSERT(division_estimate < 0x10000);
565
178k
  result += static_cast<uint16_t>(division_estimate);
566
178k
  SubtractTimes(other, division_estimate);
567
568
178k
  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
87.6k
    return result;
572
87.6k
  }
573
574
178k
  while (LessEqual(other, *this)) {
575
88.3k
    SubtractBignum(other);
576
88.3k
    result++;
577
88.3k
  }
578
90.6k
  return result;
579
178k
}
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.86M
Bignum::Chunk Bignum::BigitOrZero(const int index) const {
648
1.86M
  if (index >= BigitLength()) {
649
292k
    return 0;
650
292k
  }
651
1.57M
  if (index < exponent_) {
652
18.2k
    return 0;
653
18.2k
  }
654
1.55M
  return RawBigit(index - exponent_);
655
1.57M
}
656
657
658
457k
int Bignum::Compare(const Bignum& a, const Bignum& b) {
659
457k
  DOUBLE_CONVERSION_ASSERT(a.IsClamped());
660
457k
  DOUBLE_CONVERSION_ASSERT(b.IsClamped());
661
457k
  const int bigit_length_a = a.BigitLength();
662
457k
  const int bigit_length_b = b.BigitLength();
663
457k
  if (bigit_length_a < bigit_length_b) {
664
5.37k
    return -1;
665
5.37k
  }
666
451k
  if (bigit_length_a > bigit_length_b) {
667
188k
    return +1;
668
188k
  }
669
441k
  for (int i = bigit_length_a - 1; i >= (std::min)(a.exponent_, b.exponent_); --i) {
670
410k
    const Chunk bigit_a = a.BigitOrZero(i);
671
410k
    const Chunk bigit_b = b.BigitOrZero(i);
672
410k
    if (bigit_a < bigit_b) {
673
94.9k
      return -1;
674
94.9k
    }
675
316k
    if (bigit_a > bigit_b) {
676
137k
      return +1;
677
137k
    }
678
    // Otherwise they are equal up to this digit. Try the next digit.
679
316k
  }
680
30.7k
  return 0;
681
263k
}
682
683
684
252k
int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) {
685
252k
  DOUBLE_CONVERSION_ASSERT(a.IsClamped());
686
252k
  DOUBLE_CONVERSION_ASSERT(b.IsClamped());
687
252k
  DOUBLE_CONVERSION_ASSERT(c.IsClamped());
688
252k
  if (a.BigitLength() < b.BigitLength()) {
689
5.07k
    return PlusCompare(b, a, c);
690
5.07k
  }
691
247k
  if (a.BigitLength() + 1 < c.BigitLength()) {
692
3.72k
    return -1;
693
3.72k
  }
694
243k
  if (a.BigitLength() > c.BigitLength()) {
695
1.27k
    return +1;
696
1.27k
  }
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
242k
  if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) {
701
0
    return -1;
702
0
  }
703
704
242k
  Chunk borrow = 0;
705
  // Starting at min_exponent all digits are == 0. So no need to compare them.
706
242k
  const int min_exponent = (std::min)((std::min)(a.exponent_, b.exponent_), c.exponent_);
707
354k
  for (int i = c.BigitLength() - 1; i >= min_exponent; --i) {
708
348k
    const Chunk chunk_a = a.BigitOrZero(i);
709
348k
    const Chunk chunk_b = b.BigitOrZero(i);
710
348k
    const Chunk chunk_c = c.BigitOrZero(i);
711
348k
    const Chunk sum = chunk_a + chunk_b;
712
348k
    if (sum > chunk_c + borrow) {
713
12.9k
      return +1;
714
335k
    } else {
715
335k
      borrow = chunk_c + borrow - sum;
716
335k
      if (borrow > 1) {
717
223k
        return -1;
718
223k
      }
719
111k
      borrow <<= kBigitSize;
720
111k
    }
721
348k
  }
722
5.75k
  if (borrow == 0) {
723
5.75k
    return 0;
724
5.75k
  }
725
0
  return -1;
726
5.75k
}
727
728
729
399k
void Bignum::Clamp() {
730
482k
  while (used_bigits_ > 0 && RawBigit(used_bigits_ - 1) == 0) {
731
83.3k
    used_bigits_--;
732
83.3k
  }
733
399k
  if (used_bigits_ == 0) {
734
    // Zero.
735
2.38k
    exponent_ = 0;
736
2.38k
  }
737
399k
}
738
739
740
461k
void Bignum::Align(const Bignum& other) {
741
461k
  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.62k
    const int zero_bigits = exponent_ - other.exponent_;
749
4.62k
    EnsureCapacity(used_bigits_ + zero_bigits);
750
18.7k
    for (int i = used_bigits_ - 1; i >= 0; --i) {
751
14.1k
      RawBigit(i + zero_bigits) = RawBigit(i);
752
14.1k
    }
753
69.6k
    for (int i = 0; i < zero_bigits; ++i) {
754
65.0k
      RawBigit(i) = 0;
755
65.0k
    }
756
4.62k
    used_bigits_ += static_cast<int16_t>(zero_bigits);
757
4.62k
    exponent_ -= static_cast<int16_t>(zero_bigits);
758
759
4.62k
    DOUBLE_CONVERSION_ASSERT(used_bigits_ >= 0);
760
4.62k
    DOUBLE_CONVERSION_ASSERT(exponent_ >= 0);
761
4.62k
  }
762
461k
}
763
764
765
148k
void Bignum::BigitsShiftLeft(const int shift_amount) {
766
148k
  DOUBLE_CONVERSION_ASSERT(shift_amount < kBigitSize);
767
148k
  DOUBLE_CONVERSION_ASSERT(shift_amount >= 0);
768
148k
  Chunk carry = 0;
769
729k
  for (int i = 0; i < used_bigits_; ++i) {
770
580k
    const Chunk new_carry = RawBigit(i) >> (kBigitSize - shift_amount);
771
580k
    RawBigit(i) = ((RawBigit(i) << shift_amount) + carry) & kBigitMask;
772
580k
    carry = new_carry;
773
580k
  }
774
148k
  if (carry != 0) {
775
22.2k
    RawBigit(used_bigits_) = carry;
776
22.2k
    used_bigits_++;
777
22.2k
  }
778
148k
}
779
780
781
234k
void Bignum::SubtractTimes(const Bignum& other, const int factor) {
782
234k
  DOUBLE_CONVERSION_ASSERT(exponent_ <= other.exponent_);
783
234k
  if (factor < 3) {
784
221k
    for (int i = 0; i < factor; ++i) {
785
109k
      SubtractBignum(other);
786
109k
    }
787
112k
    return;
788
112k
  }
789
122k
  Chunk borrow = 0;
790
122k
  const int exponent_diff = other.exponent_ - exponent_;
791
1.11M
  for (int i = 0; i < other.used_bigits_; ++i) {
792
993k
    const DoubleChunk product = static_cast<DoubleChunk>(factor) * other.RawBigit(i);
793
993k
    const DoubleChunk remove = borrow + product;
794
993k
    const Chunk difference = RawBigit(i + exponent_diff) - (remove & kBigitMask);
795
993k
    RawBigit(i + exponent_diff) = difference & kBigitMask;
796
993k
    borrow = static_cast<Chunk>((difference >> (kChunkSize - 1)) +
797
993k
                                (remove >> kBigitSize));
798
993k
  }
799
135k
  for (int i = other.used_bigits_ + exponent_diff; i < used_bigits_; ++i) {
800
13.5k
    if (borrow == 0) {
801
0
      return;
802
0
    }
803
13.5k
    const Chunk difference = RawBigit(i) - borrow;
804
13.5k
    RawBigit(i) = difference & kBigitMask;
805
13.5k
    borrow = difference >> (kChunkSize - 1);
806
13.5k
  }
807
122k
  Clamp();
808
122k
}
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