Coverage Report

Created: 2025-06-24 06:43

/src/icu/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
0
Bignum::Chunk& Bignum::RawBigit(const int index) {
51
0
  DOUBLE_CONVERSION_ASSERT(static_cast<unsigned>(index) < kBigitCapacity);
52
0
  return bigits_buffer_[index];
53
0
}
54
55
56
0
const Bignum::Chunk& Bignum::RawBigit(const int index) const {
57
0
  DOUBLE_CONVERSION_ASSERT(static_cast<unsigned>(index) < kBigitCapacity);
58
0
  return bigits_buffer_[index];
59
0
}
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
0
void Bignum::AssignUInt16(const uint16_t value) {
70
0
  DOUBLE_CONVERSION_ASSERT(kBigitSize >= BitSize(value));
71
0
  Zero();
72
0
  if (value > 0) {
73
0
    RawBigit(0) = value;
74
0
    used_bigits_ = 1;
75
0
  }
76
0
}
77
78
79
0
void Bignum::AssignUInt64(uint64_t value) {
80
0
  Zero();
81
0
  for(int i = 0; value > 0; ++i) {
82
0
    RawBigit(i) = value & kBigitMask;
83
0
    value >>= kBigitSize;
84
0
    ++used_bigits_;
85
0
  }
86
0
}
87
88
89
0
void Bignum::AssignBignum(const Bignum& other) {
90
0
  exponent_ = other.exponent_;
91
0
  for (int i = 0; i < other.used_bigits_; ++i) {
92
0
    RawBigit(i) = other.RawBigit(i);
93
0
  }
94
0
  used_bigits_ = other.used_bigits_;
95
0
}
96
97
98
static uint64_t ReadUInt64(const Vector<const char> buffer,
99
                           const int from,
100
0
                           const int digits_to_read) {
101
0
  uint64_t result = 0;
102
0
  for (int i = from; i < from + digits_to_read; ++i) {
103
0
    const int digit = buffer[i] - '0';
104
0
    DOUBLE_CONVERSION_ASSERT(0 <= digit && digit <= 9);
105
0
    result = result * 10 + digit;
106
0
  }
107
0
  return result;
108
0
}
109
110
111
0
void Bignum::AssignDecimalString(const Vector<const char> value) {
112
  // 2^64 = 18446744073709551616 > 10^19
113
0
  static const int kMaxUint64DecimalDigits = 19;
114
0
  Zero();
115
0
  int length = value.length();
116
0
  unsigned pos = 0;
117
  // Let's just say that each digit needs 4 bits.
118
0
  while (length >= kMaxUint64DecimalDigits) {
119
0
    const uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits);
120
0
    pos += kMaxUint64DecimalDigits;
121
0
    length -= kMaxUint64DecimalDigits;
122
0
    MultiplyByPowerOfTen(kMaxUint64DecimalDigits);
123
0
    AddUInt64(digits);
124
0
  }
125
0
  const uint64_t digits = ReadUInt64(value, pos, length);
126
0
  MultiplyByPowerOfTen(length);
127
0
  AddUInt64(digits);
128
0
  Clamp();
129
0
}
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;  // Accumulates converted hex digits until at least
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
    RawBigit(used_bigits_++) = tmp;
164
0
  }
165
0
  Clamp();
166
0
}
167
168
169
0
void Bignum::AddUInt64(const uint64_t operand) {
170
0
  if (operand == 0) {
171
0
    return;
172
0
  }
173
0
  Bignum other;
174
0
  other.AssignUInt64(operand);
175
0
  AddBignum(other);
176
0
}
177
178
179
0
void Bignum::AddBignum(const Bignum& other) {
180
0
  DOUBLE_CONVERSION_ASSERT(IsClamped());
181
0
  DOUBLE_CONVERSION_ASSERT(other.IsClamped());
182
183
  // If this has a greater exponent than other append zero-bigits to this.
184
  // After this call exponent_ <= other.exponent_.
185
0
  Align(other);
186
187
  // There are two possibilities:
188
  //   aaaaaaaaaaa 0000  (where the 0s represent a's exponent)
189
  //     bbbbb 00000000
190
  //   ----------------
191
  //   ccccccccccc 0000
192
  // or
193
  //    aaaaaaaaaa 0000
194
  //  bbbbbbbbb 0000000
195
  //  -----------------
196
  //  cccccccccccc 0000
197
  // In both cases we might need a carry bigit.
198
199
0
  EnsureCapacity(1 + (std::max)(BigitLength(), other.BigitLength()) - exponent_);
200
0
  Chunk carry = 0;
201
0
  int bigit_pos = other.exponent_ - exponent_;
202
0
  DOUBLE_CONVERSION_ASSERT(bigit_pos >= 0);
203
0
  for (int i = used_bigits_; i < bigit_pos; ++i) {
204
0
    RawBigit(i) = 0;
205
0
  }
206
0
  for (int i = 0; i < other.used_bigits_; ++i) {
207
0
    const Chunk my = (bigit_pos < used_bigits_) ? RawBigit(bigit_pos) : 0;
208
0
    const Chunk sum = my + other.RawBigit(i) + carry;
209
0
    RawBigit(bigit_pos) = sum & kBigitMask;
210
0
    carry = sum >> kBigitSize;
211
0
    ++bigit_pos;
212
0
  }
213
0
  while (carry != 0) {
214
0
    const Chunk my = (bigit_pos < used_bigits_) ? RawBigit(bigit_pos) : 0;
215
0
    const Chunk sum = my + carry;
216
0
    RawBigit(bigit_pos) = sum & kBigitMask;
217
0
    carry = sum >> kBigitSize;
218
0
    ++bigit_pos;
219
0
  }
220
0
  used_bigits_ = (std::max)(bigit_pos, static_cast<int>(used_bigits_));
221
0
  DOUBLE_CONVERSION_ASSERT(IsClamped());
222
0
}
223
224
225
0
void Bignum::SubtractBignum(const Bignum& other) {
226
0
  DOUBLE_CONVERSION_ASSERT(IsClamped());
227
0
  DOUBLE_CONVERSION_ASSERT(other.IsClamped());
228
  // We require this to be bigger than other.
229
0
  DOUBLE_CONVERSION_ASSERT(LessEqual(other, *this));
230
231
0
  Align(other);
232
233
0
  const int offset = other.exponent_ - exponent_;
234
0
  Chunk borrow = 0;
235
0
  int i;
236
0
  for (i = 0; i < other.used_bigits_; ++i) {
237
0
    DOUBLE_CONVERSION_ASSERT((borrow == 0) || (borrow == 1));
238
0
    const Chunk difference = RawBigit(i + offset) - other.RawBigit(i) - borrow;
239
0
    RawBigit(i + offset) = difference & kBigitMask;
240
0
    borrow = difference >> (kChunkSize - 1);
241
0
  }
242
0
  while (borrow != 0) {
243
0
    const Chunk difference = RawBigit(i + offset) - borrow;
244
0
    RawBigit(i + offset) = difference & kBigitMask;
245
0
    borrow = difference >> (kChunkSize - 1);
246
0
    ++i;
247
0
  }
248
0
  Clamp();
249
0
}
250
251
252
0
void Bignum::ShiftLeft(const int shift_amount) {
253
0
  if (used_bigits_ == 0) {
254
0
    return;
255
0
  }
256
0
  exponent_ += (shift_amount / kBigitSize);
257
0
  const int local_shift = shift_amount % kBigitSize;
258
0
  EnsureCapacity(used_bigits_ + 1);
259
0
  BigitsShiftLeft(local_shift);
260
0
}
261
262
263
0
void Bignum::MultiplyByUInt32(const uint32_t factor) {
264
0
  if (factor == 1) {
265
0
    return;
266
0
  }
267
0
  if (factor == 0) {
268
0
    Zero();
269
0
    return;
270
0
  }
271
0
  if (used_bigits_ == 0) {
272
0
    return;
273
0
  }
274
  // The product of a bigit with the factor is of size kBigitSize + 32.
275
  // Assert that this number + 1 (for the carry) fits into double chunk.
276
0
  DOUBLE_CONVERSION_ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1);
277
0
  DoubleChunk carry = 0;
278
0
  for (int i = 0; i < used_bigits_; ++i) {
279
0
    const DoubleChunk product = static_cast<DoubleChunk>(factor) * RawBigit(i) + carry;
280
0
    RawBigit(i) = static_cast<Chunk>(product & kBigitMask);
281
0
    carry = (product >> kBigitSize);
282
0
  }
283
0
  while (carry != 0) {
284
0
    EnsureCapacity(used_bigits_ + 1);
285
0
    RawBigit(used_bigits_) = carry & kBigitMask;
286
0
    used_bigits_++;
287
0
    carry >>= kBigitSize;
288
0
  }
289
0
}
290
291
292
0
void Bignum::MultiplyByUInt64(const uint64_t factor) {
293
0
  if (factor == 1) {
294
0
    return;
295
0
  }
296
0
  if (factor == 0) {
297
0
    Zero();
298
0
    return;
299
0
  }
300
0
  if (used_bigits_ == 0) {
301
0
    return;
302
0
  }
303
0
  DOUBLE_CONVERSION_ASSERT(kBigitSize < 32);
304
0
  uint64_t carry = 0;
305
0
  const uint64_t low = factor & 0xFFFFFFFF;
306
0
  const uint64_t high = factor >> 32;
307
0
  for (int i = 0; i < used_bigits_; ++i) {
308
0
    const uint64_t product_low = low * RawBigit(i);
309
0
    const uint64_t product_high = high * RawBigit(i);
310
0
    const uint64_t tmp = (carry & kBigitMask) + product_low;
311
0
    RawBigit(i) = tmp & kBigitMask;
312
0
    carry = (carry >> kBigitSize) + (tmp >> kBigitSize) +
313
0
        (product_high << (32 - kBigitSize));
314
0
  }
315
0
  while (carry != 0) {
316
0
    EnsureCapacity(used_bigits_ + 1);
317
0
    RawBigit(used_bigits_) = carry & kBigitMask;
318
0
    used_bigits_++;
319
0
    carry >>= kBigitSize;
320
0
  }
321
0
}
322
323
324
0
void Bignum::MultiplyByPowerOfTen(const int exponent) {
325
0
  static const uint64_t kFive27 = DOUBLE_CONVERSION_UINT64_2PART_C(0x6765c793, fa10079d);
326
0
  static const uint16_t kFive1 = 5;
327
0
  static const uint16_t kFive2 = kFive1 * 5;
328
0
  static const uint16_t kFive3 = kFive2 * 5;
329
0
  static const uint16_t kFive4 = kFive3 * 5;
330
0
  static const uint16_t kFive5 = kFive4 * 5;
331
0
  static const uint16_t kFive6 = kFive5 * 5;
332
0
  static const uint32_t kFive7 = kFive6 * 5;
333
0
  static const uint32_t kFive8 = kFive7 * 5;
334
0
  static const uint32_t kFive9 = kFive8 * 5;
335
0
  static const uint32_t kFive10 = kFive9 * 5;
336
0
  static const uint32_t kFive11 = kFive10 * 5;
337
0
  static const uint32_t kFive12 = kFive11 * 5;
338
0
  static const uint32_t kFive13 = kFive12 * 5;
339
0
  static const uint32_t kFive1_to_12[] =
340
0
      { kFive1, kFive2, kFive3, kFive4, kFive5, kFive6,
341
0
        kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 };
342
343
0
  DOUBLE_CONVERSION_ASSERT(exponent >= 0);
344
345
0
  if (exponent == 0) {
346
0
    return;
347
0
  }
348
0
  if (used_bigits_ == 0) {
349
0
    return;
350
0
  }
351
  // We shift by exponent at the end just before returning.
352
0
  int remaining_exponent = exponent;
353
0
  while (remaining_exponent >= 27) {
354
0
    MultiplyByUInt64(kFive27);
355
0
    remaining_exponent -= 27;
356
0
  }
357
0
  while (remaining_exponent >= 13) {
358
0
    MultiplyByUInt32(kFive13);
359
0
    remaining_exponent -= 13;
360
0
  }
361
0
  if (remaining_exponent > 0) {
362
0
    MultiplyByUInt32(kFive1_to_12[remaining_exponent - 1]);
363
0
  }
364
0
  ShiftLeft(exponent);
365
0
}
366
367
368
0
void Bignum::Square() {
369
0
  DOUBLE_CONVERSION_ASSERT(IsClamped());
370
0
  const int product_length = 2 * used_bigits_;
371
0
  EnsureCapacity(product_length);
372
373
  // Comba multiplication: compute each column separately.
374
  // Example: r = a2a1a0 * b2b1b0.
375
  //    r =  1    * a0b0 +
376
  //        10    * (a1b0 + a0b1) +
377
  //        100   * (a2b0 + a1b1 + a0b2) +
378
  //        1000  * (a2b1 + a1b2) +
379
  //        10000 * a2b2
380
  //
381
  // In the worst case we have to accumulate nb-digits products of digit*digit.
382
  //
383
  // Assert that the additional number of bits in a DoubleChunk are enough to
384
  // sum up used_digits of Bigit*Bigit.
385
0
  if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_bigits_) {
386
0
    DOUBLE_CONVERSION_UNIMPLEMENTED();
387
0
  }
388
0
  DoubleChunk accumulator = 0;
389
  // First shift the digits so we don't overwrite them.
390
0
  const int copy_offset = used_bigits_;
391
0
  for (int i = 0; i < used_bigits_; ++i) {
392
0
    RawBigit(copy_offset + i) = RawBigit(i);
393
0
  }
394
  // We have two loops to avoid some 'if's in the loop.
395
0
  for (int i = 0; i < used_bigits_; ++i) {
396
    // Process temporary digit i with power i.
397
    // The sum of the two indices must be equal to i.
398
0
    int bigit_index1 = i;
399
0
    int bigit_index2 = 0;
400
    // Sum all of the sub-products.
401
0
    while (bigit_index1 >= 0) {
402
0
      const Chunk chunk1 = RawBigit(copy_offset + bigit_index1);
403
0
      const Chunk chunk2 = RawBigit(copy_offset + bigit_index2);
404
0
      accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
405
0
      bigit_index1--;
406
0
      bigit_index2++;
407
0
    }
408
0
    RawBigit(i) = static_cast<Chunk>(accumulator) & kBigitMask;
409
0
    accumulator >>= kBigitSize;
410
0
  }
411
0
  for (int i = used_bigits_; i < product_length; ++i) {
412
0
    int bigit_index1 = used_bigits_ - 1;
413
0
    int bigit_index2 = i - bigit_index1;
414
    // Invariant: sum of both indices is again equal to i.
415
    // Inner loop runs 0 times on last iteration, emptying accumulator.
416
0
    while (bigit_index2 < used_bigits_) {
417
0
      const Chunk chunk1 = RawBigit(copy_offset + bigit_index1);
418
0
      const Chunk chunk2 = RawBigit(copy_offset + bigit_index2);
419
0
      accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
420
0
      bigit_index1--;
421
0
      bigit_index2++;
422
0
    }
423
    // The overwritten RawBigit(i) will never be read in further loop iterations,
424
    // because bigit_index1 and bigit_index2 are always greater
425
    // than i - used_bigits_.
426
0
    RawBigit(i) = static_cast<Chunk>(accumulator) & kBigitMask;
427
0
    accumulator >>= kBigitSize;
428
0
  }
429
  // Since the result was guaranteed to lie inside the number the
430
  // accumulator must be 0 now.
431
0
  DOUBLE_CONVERSION_ASSERT(accumulator == 0);
432
433
  // Don't forget to update the used_digits and the exponent.
434
0
  used_bigits_ = product_length;
435
0
  exponent_ *= 2;
436
0
  Clamp();
437
0
}
438
439
440
0
void Bignum::AssignPowerUInt16(uint16_t base, const int power_exponent) {
441
0
  DOUBLE_CONVERSION_ASSERT(base != 0);
442
0
  DOUBLE_CONVERSION_ASSERT(power_exponent >= 0);
443
0
  if (power_exponent == 0) {
444
0
    AssignUInt16(1);
445
0
    return;
446
0
  }
447
0
  Zero();
448
0
  int shifts = 0;
449
  // We expect base to be in range 2-32, and most often to be 10.
450
  // It does not make much sense to implement different algorithms for counting
451
  // the bits.
452
0
  while ((base & 1) == 0) {
453
0
    base >>= 1;
454
0
    shifts++;
455
0
  }
456
0
  int bit_size = 0;
457
0
  int tmp_base = base;
458
0
  while (tmp_base != 0) {
459
0
    tmp_base >>= 1;
460
0
    bit_size++;
461
0
  }
462
0
  const int final_size = bit_size * power_exponent;
463
  // 1 extra bigit for the shifting, and one for rounded final_size.
464
0
  EnsureCapacity(final_size / kBigitSize + 2);
465
466
  // Left to Right exponentiation.
467
0
  int mask = 1;
468
0
  while (power_exponent >= mask) mask <<= 1;
469
470
  // The mask is now pointing to the bit above the most significant 1-bit of
471
  // power_exponent.
472
  // Get rid of first 1-bit;
473
0
  mask >>= 2;
474
0
  uint64_t this_value = base;
475
476
0
  bool delayed_multiplication = false;
477
0
  const uint64_t max_32bits = 0xFFFFFFFF;
478
0
  while (mask != 0 && this_value <= max_32bits) {
479
0
    this_value = this_value * this_value;
480
    // Verify that there is enough space in this_value to perform the
481
    // multiplication.  The first bit_size bits must be 0.
482
0
    if ((power_exponent & mask) != 0) {
483
0
      DOUBLE_CONVERSION_ASSERT(bit_size > 0);
484
0
      const uint64_t base_bits_mask =
485
0
        ~((static_cast<uint64_t>(1) << (64 - bit_size)) - 1);
486
0
      const bool high_bits_zero = (this_value & base_bits_mask) == 0;
487
0
      if (high_bits_zero) {
488
0
        this_value *= base;
489
0
      } else {
490
0
        delayed_multiplication = true;
491
0
      }
492
0
    }
493
0
    mask >>= 1;
494
0
  }
495
0
  AssignUInt64(this_value);
496
0
  if (delayed_multiplication) {
497
0
    MultiplyByUInt32(base);
498
0
  }
499
500
  // Now do the same thing as a bignum.
501
0
  while (mask != 0) {
502
0
    Square();
503
0
    if ((power_exponent & mask) != 0) {
504
0
      MultiplyByUInt32(base);
505
0
    }
506
0
    mask >>= 1;
507
0
  }
508
509
  // And finally add the saved shifts.
510
0
  ShiftLeft(shifts * power_exponent);
511
0
}
512
513
514
// Precondition: this/other < 16bit.
515
0
uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) {
516
0
  DOUBLE_CONVERSION_ASSERT(IsClamped());
517
0
  DOUBLE_CONVERSION_ASSERT(other.IsClamped());
518
0
  DOUBLE_CONVERSION_ASSERT(other.used_bigits_ > 0);
519
520
  // Easy case: if we have less digits than the divisor than the result is 0.
521
  // Note: this handles the case where this == 0, too.
522
0
  if (BigitLength() < other.BigitLength()) {
523
0
    return 0;
524
0
  }
525
526
0
  Align(other);
527
528
0
  uint16_t result = 0;
529
530
  // Start by removing multiples of 'other' until both numbers have the same
531
  // number of digits.
532
0
  while (BigitLength() > other.BigitLength()) {
533
    // This naive approach is extremely inefficient if `this` divided by other
534
    // is big. This function is implemented for doubleToString where
535
    // the result should be small (less than 10).
536
0
    DOUBLE_CONVERSION_ASSERT(other.RawBigit(other.used_bigits_ - 1) >= ((1 << kBigitSize) / 16));
537
0
    DOUBLE_CONVERSION_ASSERT(RawBigit(used_bigits_ - 1) < 0x10000);
538
    // Remove the multiples of the first digit.
539
    // Example this = 23 and other equals 9. -> Remove 2 multiples.
540
0
    result += static_cast<uint16_t>(RawBigit(used_bigits_ - 1));
541
0
    SubtractTimes(other, RawBigit(used_bigits_ - 1));
542
0
  }
543
544
0
  DOUBLE_CONVERSION_ASSERT(BigitLength() == other.BigitLength());
545
546
  // Both bignums are at the same length now.
547
  // Since other has more than 0 digits we know that the access to
548
  // RawBigit(used_bigits_ - 1) is safe.
549
0
  const Chunk this_bigit = RawBigit(used_bigits_ - 1);
550
0
  const Chunk other_bigit = other.RawBigit(other.used_bigits_ - 1);
551
552
0
  if (other.used_bigits_ == 1) {
553
    // Shortcut for easy (and common) case.
554
0
    int quotient = this_bigit / other_bigit;
555
0
    RawBigit(used_bigits_ - 1) = this_bigit - other_bigit * quotient;
556
0
    DOUBLE_CONVERSION_ASSERT(quotient < 0x10000);
557
0
    result += static_cast<uint16_t>(quotient);
558
0
    Clamp();
559
0
    return result;
560
0
  }
561
562
0
  const int division_estimate = this_bigit / (other_bigit + 1);
563
0
  DOUBLE_CONVERSION_ASSERT(division_estimate < 0x10000);
564
0
  result += static_cast<uint16_t>(division_estimate);
565
0
  SubtractTimes(other, division_estimate);
566
567
0
  if (other_bigit * (division_estimate + 1) > this_bigit) {
568
    // No need to even try to subtract. Even if other's remaining digits were 0
569
    // another subtraction would be too much.
570
0
    return result;
571
0
  }
572
573
0
  while (LessEqual(other, *this)) {
574
0
    SubtractBignum(other);
575
0
    result++;
576
0
  }
577
0
  return result;
578
0
}
579
580
581
template<typename S>
582
0
static int SizeInHexChars(S number) {
583
0
  DOUBLE_CONVERSION_ASSERT(number > 0);
584
0
  int result = 0;
585
0
  while (number != 0) {
586
0
    number >>= 4;
587
0
    result++;
588
0
  }
589
0
  return result;
590
0
}
591
592
593
0
static char HexCharOfValue(const int value) {
594
0
  DOUBLE_CONVERSION_ASSERT(0 <= value && value <= 16);
595
0
  if (value < 10) {
596
0
    return static_cast<char>(value + '0');
597
0
  }
598
0
  return static_cast<char>(value - 10 + 'A');
599
0
}
600
601
602
0
bool Bignum::ToHexString(char* buffer, const int buffer_size) const {
603
0
  DOUBLE_CONVERSION_ASSERT(IsClamped());
604
  // Each bigit must be printable as separate hex-character.
605
0
  DOUBLE_CONVERSION_ASSERT(kBigitSize % 4 == 0);
606
0
  static const int kHexCharsPerBigit = kBigitSize / 4;
607
608
0
  if (used_bigits_ == 0) {
609
0
    if (buffer_size < 2) {
610
0
      return false;
611
0
    }
612
0
    buffer[0] = '0';
613
0
    buffer[1] = '\0';
614
0
    return true;
615
0
  }
616
  // We add 1 for the terminating '\0' character.
617
0
  const int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit +
618
0
    SizeInHexChars(RawBigit(used_bigits_ - 1)) + 1;
619
0
  if (needed_chars > buffer_size) {
620
0
    return false;
621
0
  }
622
0
  int string_index = needed_chars - 1;
623
0
  buffer[string_index--] = '\0';
624
0
  for (int i = 0; i < exponent_; ++i) {
625
0
    for (int j = 0; j < kHexCharsPerBigit; ++j) {
626
0
      buffer[string_index--] = '0';
627
0
    }
628
0
  }
629
0
  for (int i = 0; i < used_bigits_ - 1; ++i) {
630
0
    Chunk current_bigit = RawBigit(i);
631
0
    for (int j = 0; j < kHexCharsPerBigit; ++j) {
632
0
      buffer[string_index--] = HexCharOfValue(current_bigit & 0xF);
633
0
      current_bigit >>= 4;
634
0
    }
635
0
  }
636
  // And finally the last bigit.
637
0
  Chunk most_significant_bigit = RawBigit(used_bigits_ - 1);
638
0
  while (most_significant_bigit != 0) {
639
0
    buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF);
640
0
    most_significant_bigit >>= 4;
641
0
  }
642
0
  return true;
643
0
}
644
645
646
0
Bignum::Chunk Bignum::BigitOrZero(const int index) const {
647
0
  if (index >= BigitLength()) {
648
0
    return 0;
649
0
  }
650
0
  if (index < exponent_) {
651
0
    return 0;
652
0
  }
653
0
  return RawBigit(index - exponent_);
654
0
}
655
656
657
0
int Bignum::Compare(const Bignum& a, const Bignum& b) {
658
0
  DOUBLE_CONVERSION_ASSERT(a.IsClamped());
659
0
  DOUBLE_CONVERSION_ASSERT(b.IsClamped());
660
0
  const int bigit_length_a = a.BigitLength();
661
0
  const int bigit_length_b = b.BigitLength();
662
0
  if (bigit_length_a < bigit_length_b) {
663
0
    return -1;
664
0
  }
665
0
  if (bigit_length_a > bigit_length_b) {
666
0
    return +1;
667
0
  }
668
0
  for (int i = bigit_length_a - 1; i >= (std::min)(a.exponent_, b.exponent_); --i) {
669
0
    const Chunk bigit_a = a.BigitOrZero(i);
670
0
    const Chunk bigit_b = b.BigitOrZero(i);
671
0
    if (bigit_a < bigit_b) {
672
0
      return -1;
673
0
    }
674
0
    if (bigit_a > bigit_b) {
675
0
      return +1;
676
0
    }
677
    // Otherwise they are equal up to this digit. Try the next digit.
678
0
  }
679
0
  return 0;
680
0
}
681
682
683
0
int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) {
684
0
  DOUBLE_CONVERSION_ASSERT(a.IsClamped());
685
0
  DOUBLE_CONVERSION_ASSERT(b.IsClamped());
686
0
  DOUBLE_CONVERSION_ASSERT(c.IsClamped());
687
0
  if (a.BigitLength() < b.BigitLength()) {
688
0
    return PlusCompare(b, a, c);
689
0
  }
690
0
  if (a.BigitLength() + 1 < c.BigitLength()) {
691
0
    return -1;
692
0
  }
693
0
  if (a.BigitLength() > c.BigitLength()) {
694
0
    return +1;
695
0
  }
696
  // The exponent encodes 0-bigits. So if there are more 0-digits in 'a' than
697
  // 'b' has digits, then the bigit-length of 'a'+'b' must be equal to the one
698
  // of 'a'.
699
0
  if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) {
700
0
    return -1;
701
0
  }
702
703
0
  Chunk borrow = 0;
704
  // Starting at min_exponent all digits are == 0. So no need to compare them.
705
0
  const int min_exponent = (std::min)((std::min)(a.exponent_, b.exponent_), c.exponent_);
706
0
  for (int i = c.BigitLength() - 1; i >= min_exponent; --i) {
707
0
    const Chunk chunk_a = a.BigitOrZero(i);
708
0
    const Chunk chunk_b = b.BigitOrZero(i);
709
0
    const Chunk chunk_c = c.BigitOrZero(i);
710
0
    const Chunk sum = chunk_a + chunk_b;
711
0
    if (sum > chunk_c + borrow) {
712
0
      return +1;
713
0
    } else {
714
0
      borrow = chunk_c + borrow - sum;
715
0
      if (borrow > 1) {
716
0
        return -1;
717
0
      }
718
0
      borrow <<= kBigitSize;
719
0
    }
720
0
  }
721
0
  if (borrow == 0) {
722
0
    return 0;
723
0
  }
724
0
  return -1;
725
0
}
726
727
728
0
void Bignum::Clamp() {
729
0
  while (used_bigits_ > 0 && RawBigit(used_bigits_ - 1) == 0) {
730
0
    used_bigits_--;
731
0
  }
732
0
  if (used_bigits_ == 0) {
733
    // Zero.
734
0
    exponent_ = 0;
735
0
  }
736
0
}
737
738
739
0
void Bignum::Align(const Bignum& other) {
740
0
  if (exponent_ > other.exponent_) {
741
    // If "X" represents a "hidden" bigit (by the exponent) then we are in the
742
    // following case (a == this, b == other):
743
    // a:  aaaaaaXXXX   or a:   aaaaaXXX
744
    // b:     bbbbbbX      b: bbbbbbbbXX
745
    // We replace some of the hidden digits (X) of a with 0 digits.
746
    // a:  aaaaaa000X   or a:   aaaaa0XX
747
0
    const int zero_bigits = exponent_ - other.exponent_;
748
0
    EnsureCapacity(used_bigits_ + zero_bigits);
749
0
    for (int i = used_bigits_ - 1; i >= 0; --i) {
750
0
      RawBigit(i + zero_bigits) = RawBigit(i);
751
0
    }
752
0
    for (int i = 0; i < zero_bigits; ++i) {
753
0
      RawBigit(i) = 0;
754
0
    }
755
0
    used_bigits_ += zero_bigits;
756
0
    exponent_ -= zero_bigits;
757
758
0
    DOUBLE_CONVERSION_ASSERT(used_bigits_ >= 0);
759
0
    DOUBLE_CONVERSION_ASSERT(exponent_ >= 0);
760
0
  }
761
0
}
762
763
764
0
void Bignum::BigitsShiftLeft(const int shift_amount) {
765
0
  DOUBLE_CONVERSION_ASSERT(shift_amount < kBigitSize);
766
0
  DOUBLE_CONVERSION_ASSERT(shift_amount >= 0);
767
0
  Chunk carry = 0;
768
0
  for (int i = 0; i < used_bigits_; ++i) {
769
0
    const Chunk new_carry = RawBigit(i) >> (kBigitSize - shift_amount);
770
0
    RawBigit(i) = ((RawBigit(i) << shift_amount) + carry) & kBigitMask;
771
0
    carry = new_carry;
772
0
  }
773
0
  if (carry != 0) {
774
0
    RawBigit(used_bigits_) = carry;
775
0
    used_bigits_++;
776
0
  }
777
0
}
778
779
780
0
void Bignum::SubtractTimes(const Bignum& other, const int factor) {
781
0
  DOUBLE_CONVERSION_ASSERT(exponent_ <= other.exponent_);
782
0
  if (factor < 3) {
783
0
    for (int i = 0; i < factor; ++i) {
784
0
      SubtractBignum(other);
785
0
    }
786
0
    return;
787
0
  }
788
0
  Chunk borrow = 0;
789
0
  const int exponent_diff = other.exponent_ - exponent_;
790
0
  for (int i = 0; i < other.used_bigits_; ++i) {
791
0
    const DoubleChunk product = static_cast<DoubleChunk>(factor) * other.RawBigit(i);
792
0
    const DoubleChunk remove = borrow + product;
793
0
    const Chunk difference = RawBigit(i + exponent_diff) - (remove & kBigitMask);
794
0
    RawBigit(i + exponent_diff) = difference & kBigitMask;
795
0
    borrow = static_cast<Chunk>((difference >> (kChunkSize - 1)) +
796
0
                                (remove >> kBigitSize));
797
0
  }
798
0
  for (int i = other.used_bigits_ + exponent_diff; i < used_bigits_; ++i) {
799
0
    if (borrow == 0) {
800
0
      return;
801
0
    }
802
0
    const Chunk difference = RawBigit(i) - borrow;
803
0
    RawBigit(i) = difference & kBigitMask;
804
0
    borrow = difference >> (kChunkSize - 1);
805
0
  }
806
0
  Clamp();
807
0
}
808
809
810
}  // namespace double_conversion
811
812
// ICU PATCH: Close ICU namespace
813
U_NAMESPACE_END
814
#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING