28 #include "../include/v8stdint.h"
36 : bigits_(bigits_buffer_, kBigitCapacity), used_digits_(0), exponent_(0) {
37 for (
int i = 0; i < kBigitCapacity; ++i) {
44 static int BitSize(
S value) {
45 return 8 *
sizeof(value);
51 ASSERT(kBigitSize >= BitSize(value));
53 if (value == 0)
return;
62 const int kUInt64Size = 64;
65 if (value == 0)
return;
67 int needed_bigits = kUInt64Size / kBigitSize + 1;
68 EnsureCapacity(needed_bigits);
69 for (
int i = 0; i < needed_bigits; ++i) {
70 bigits_[i] =
static_cast<Chunk
>(value & kBigitMask);
71 value = value >> kBigitSize;
73 used_digits_ = needed_bigits;
79 exponent_ = other.exponent_;
80 for (
int i = 0; i < other.used_digits_; ++i) {
81 bigits_[i] = other.bigits_[i];
84 for (
int i = other.used_digits_; i < used_digits_; ++i) {
87 used_digits_ = other.used_digits_;
95 for (
int i = from; i < from + digits_to_read; ++i) {
96 int digit = buffer[i] -
'0';
97 ASSERT(0 <= digit && digit <= 9);
98 result = result * 10 + digit;
106 const int kMaxUint64DecimalDigits = 19;
108 int length = value.
length();
111 while (length >= kMaxUint64DecimalDigits) {
112 uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits);
113 pos += kMaxUint64DecimalDigits;
114 length -= kMaxUint64DecimalDigits;
118 uint64_t digits = ReadUInt64(value, pos, length);
125 static int HexCharValue(
char c) {
126 if (
'0' <= c && c <=
'9')
return c -
'0';
127 if (
'a' <= c && c <=
'f')
return 10 + c -
'a';
128 if (
'A' <= c && c <=
'F')
return 10 + c -
'A';
136 int length = value.
length();
138 int needed_bigits = length * 4 / kBigitSize + 1;
139 EnsureCapacity(needed_bigits);
140 int string_index = length - 1;
141 for (
int i = 0; i < needed_bigits - 1; ++i) {
143 Chunk current_bigit = 0;
144 for (
int j = 0; j < kBigitSize / 4; j++) {
145 current_bigit += HexCharValue(value[string_index--]) << (j * 4);
147 bigits_[i] = current_bigit;
149 used_digits_ = needed_bigits - 1;
151 Chunk most_significant_bigit = 0;
152 for (
int j = 0; j <= string_index; ++j) {
153 most_significant_bigit <<= 4;
154 most_significant_bigit += HexCharValue(value[j]);
156 if (most_significant_bigit != 0) {
157 bigits_[used_digits_] = most_significant_bigit;
165 if (operand == 0)
return;
174 ASSERT(other.IsClamped());
192 EnsureCapacity(1 +
Max(BigitLength(), other.BigitLength()) - exponent_);
194 int bigit_pos = other.exponent_ - exponent_;
196 for (
int i = 0; i < other.used_digits_; ++i) {
197 Chunk sum = bigits_[bigit_pos] + other.bigits_[i] +
carry;
198 bigits_[bigit_pos] = sum & kBigitMask;
199 carry = sum >> kBigitSize;
204 Chunk sum = bigits_[bigit_pos] +
carry;
205 bigits_[bigit_pos] = sum & kBigitMask;
206 carry = sum >> kBigitSize;
209 used_digits_ =
Max(bigit_pos, used_digits_);
216 ASSERT(other.IsClamped());
222 int offset = other.exponent_ - exponent_;
225 for (i = 0; i < other.used_digits_; ++i) {
226 ASSERT((borrow == 0) || (borrow == 1));
227 Chunk difference = bigits_[i + offset] - other.bigits_[i] - borrow;
228 bigits_[i + offset] = difference & kBigitMask;
229 borrow = difference >> (kChunkSize - 1);
231 while (borrow != 0) {
232 Chunk difference = bigits_[i + offset] - borrow;
233 bigits_[i + offset] = difference & kBigitMask;
234 borrow = difference >> (kChunkSize - 1);
242 if (used_digits_ == 0)
return;
243 exponent_ += shift_amount / kBigitSize;
244 int local_shift = shift_amount % kBigitSize;
245 EnsureCapacity(used_digits_ + 1);
246 BigitsShiftLeft(local_shift);
251 if (factor == 1)
return;
256 if (used_digits_ == 0)
return;
260 ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1);
261 DoubleChunk
carry = 0;
262 for (
int i = 0; i < used_digits_; ++i) {
263 DoubleChunk product =
static_cast<DoubleChunk
>(factor) * bigits_[i] + carry;
264 bigits_[i] =
static_cast<Chunk
>(product & kBigitMask);
265 carry = (product >> kBigitSize);
268 EnsureCapacity(used_digits_ + 1);
269 bigits_[used_digits_] =
static_cast<Chunk
>(carry & kBigitMask);
271 carry >>= kBigitSize;
277 if (factor == 1)
return;
284 uint64_t low = factor & 0xFFFFFFFF;
285 uint64_t high = factor >> 32;
286 for (
int i = 0; i < used_digits_; ++i) {
287 uint64_t product_low = low * bigits_[i];
288 uint64_t product_high = high * bigits_[i];
289 uint64_t tmp = (carry & kBigitMask) + product_low;
290 bigits_[i] =
static_cast<Chunk
>(tmp & kBigitMask);
291 carry = (carry >> kBigitSize) + (tmp >> kBigitSize) +
292 (product_high << (32 - kBigitSize));
295 EnsureCapacity(used_digits_ + 1);
296 bigits_[used_digits_] =
static_cast<Chunk
>(carry & kBigitMask);
298 carry >>= kBigitSize;
311 const uint32_t kFive7 = kFive6 * 5;
312 const uint32_t kFive8 = kFive7 * 5;
313 const uint32_t kFive9 = kFive8 * 5;
314 const uint32_t kFive10 = kFive9 * 5;
315 const uint32_t kFive11 = kFive10 * 5;
316 const uint32_t kFive12 = kFive11 * 5;
317 const uint32_t kFive13 = kFive12 * 5;
318 const uint32_t kFive1_to_12[] =
319 { kFive1, kFive2, kFive3, kFive4, kFive5, kFive6,
320 kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 };
323 if (exponent == 0)
return;
324 if (used_digits_ == 0)
return;
327 int remaining_exponent = exponent;
328 while (remaining_exponent >= 27) {
330 remaining_exponent -= 27;
332 while (remaining_exponent >= 13) {
334 remaining_exponent -= 13;
336 if (remaining_exponent > 0) {
345 int product_length = 2 * used_digits_;
346 EnsureCapacity(product_length);
360 if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_digits_) {
363 DoubleChunk accumulator = 0;
365 int copy_offset = used_digits_;
366 for (
int i = 0; i < used_digits_; ++i) {
367 bigits_[copy_offset + i] = bigits_[i];
370 for (
int i = 0; i < used_digits_; ++i) {
373 int bigit_index1 = i;
374 int bigit_index2 = 0;
376 while (bigit_index1 >= 0) {
377 Chunk chunk1 = bigits_[copy_offset + bigit_index1];
378 Chunk chunk2 = bigits_[copy_offset + bigit_index2];
379 accumulator +=
static_cast<DoubleChunk
>(chunk1) * chunk2;
383 bigits_[i] =
static_cast<Chunk
>(accumulator) & kBigitMask;
384 accumulator >>= kBigitSize;
386 for (
int i = used_digits_; i < product_length; ++i) {
387 int bigit_index1 = used_digits_ - 1;
388 int bigit_index2 = i - bigit_index1;
391 while (bigit_index2 < used_digits_) {
392 Chunk chunk1 = bigits_[copy_offset + bigit_index1];
393 Chunk chunk2 = bigits_[copy_offset + bigit_index2];
394 accumulator +=
static_cast<DoubleChunk
>(chunk1) * chunk2;
401 bigits_[i] =
static_cast<Chunk
>(accumulator) & kBigitMask;
402 accumulator >>= kBigitSize;
409 used_digits_ = product_length;
417 ASSERT(power_exponent >= 0);
418 if (power_exponent == 0) {
427 while ((base & 1) == 0) {
433 while (tmp_base != 0) {
437 int final_size = bit_size * power_exponent;
439 EnsureCapacity(final_size / kBigitSize + 2);
443 while (power_exponent >= mask) mask <<= 1;
449 uint64_t this_value = base;
451 bool delayed_multipliciation =
false;
452 const uint64_t max_32bits = 0xFFFFFFFF;
453 while (mask != 0 && this_value <= max_32bits) {
454 this_value = this_value * this_value;
457 if ((power_exponent & mask) != 0) {
458 uint64_t base_bits_mask =
459 ~((
static_cast<uint64_t
>(1) << (64 - bit_size)) - 1);
460 bool high_bits_zero = (this_value & base_bits_mask) == 0;
461 if (high_bits_zero) {
464 delayed_multipliciation =
true;
470 if (delayed_multipliciation) {
477 if ((power_exponent & mask) != 0) {
491 ASSERT(other.IsClamped());
492 ASSERT(other.used_digits_ > 0);
496 if (BigitLength() < other.BigitLength()) {
506 while (BigitLength() > other.BigitLength()) {
510 ASSERT(other.bigits_[other.used_digits_ - 1] >= ((1 << kBigitSize) / 16));
513 result += bigits_[used_digits_ - 1];
514 SubtractTimes(other, bigits_[used_digits_ - 1]);
517 ASSERT(BigitLength() == other.BigitLength());
522 Chunk this_bigit = bigits_[used_digits_ - 1];
523 Chunk other_bigit = other.bigits_[other.used_digits_ - 1];
525 if (other.used_digits_ == 1) {
527 int quotient = this_bigit / other_bigit;
528 bigits_[used_digits_ - 1] = this_bigit - other_bigit * quotient;
534 int division_estimate = this_bigit / (other_bigit + 1);
535 result += division_estimate;
536 SubtractTimes(other, division_estimate);
538 if (other_bigit * (division_estimate + 1) > this_bigit) {
553 static int SizeInHexChars(
S number) {
556 while (number != 0) {
564 static char HexCharOfValue(
int value) {
565 ASSERT(0 <= value && value <= 16);
566 if (value < 10)
return value +
'0';
567 return value - 10 +
'A';
574 ASSERT(kBigitSize % 4 == 0);
575 const int kHexCharsPerBigit = kBigitSize / 4;
577 if (used_digits_ == 0) {
578 if (buffer_size < 2)
return false;
584 int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit +
585 SizeInHexChars(bigits_[used_digits_ - 1]) + 1;
586 if (needed_chars > buffer_size)
return false;
587 int string_index = needed_chars - 1;
588 buffer[string_index--] =
'\0';
589 for (
int i = 0; i < exponent_; ++i) {
590 for (
int j = 0; j < kHexCharsPerBigit; ++j) {
591 buffer[string_index--] =
'0';
594 for (
int i = 0; i < used_digits_ - 1; ++i) {
595 Chunk current_bigit = bigits_[i];
596 for (
int j = 0; j < kHexCharsPerBigit; ++j) {
597 buffer[string_index--] = HexCharOfValue(current_bigit & 0xF);
602 Chunk most_significant_bigit = bigits_[used_digits_ - 1];
603 while (most_significant_bigit != 0) {
604 buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF);
605 most_significant_bigit >>= 4;
611 Bignum::Chunk Bignum::BigitAt(
int index)
const {
612 if (index >= BigitLength())
return 0;
613 if (index < exponent_)
return 0;
614 return bigits_[index - exponent_];
621 int bigit_length_a = a.BigitLength();
622 int bigit_length_b = b.BigitLength();
623 if (bigit_length_a < bigit_length_b)
return -1;
624 if (bigit_length_a > bigit_length_b)
return +1;
625 for (
int i = bigit_length_a - 1; i >=
Min(a.exponent_, b.exponent_); --i) {
626 Chunk bigit_a = a.BigitAt(i);
627 Chunk bigit_b = b.BigitAt(i);
628 if (bigit_a < bigit_b)
return -1;
629 if (bigit_a > bigit_b)
return +1;
640 if (a.BigitLength() < b.BigitLength()) {
643 if (a.BigitLength() + 1 < c.BigitLength())
return -1;
644 if (a.BigitLength() > c.BigitLength())
return +1;
648 if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) {
654 int min_exponent =
Min(
Min(a.exponent_, b.exponent_), c.exponent_);
655 for (
int i = c.BigitLength() - 1; i >= min_exponent; --i) {
656 Chunk chunk_a = a.BigitAt(i);
657 Chunk chunk_b = b.BigitAt(i);
658 Chunk chunk_c = c.BigitAt(i);
659 Chunk sum = chunk_a + chunk_b;
660 if (sum > chunk_c + borrow) {
663 borrow = chunk_c + borrow - sum;
664 if (borrow > 1)
return -1;
665 borrow <<= kBigitSize;
668 if (borrow == 0)
return 0;
673 void Bignum::Clamp() {
674 while (used_digits_ > 0 && bigits_[used_digits_ - 1] == 0) {
677 if (used_digits_ == 0) {
684 bool Bignum::IsClamped()
const {
685 return used_digits_ == 0 || bigits_[used_digits_ - 1] != 0;
689 void Bignum::Zero() {
690 for (
int i = 0; i < used_digits_; ++i) {
698 void Bignum::Align(
const Bignum& other) {
699 if (exponent_ > other.exponent_) {
706 int zero_digits = exponent_ - other.exponent_;
707 EnsureCapacity(used_digits_ + zero_digits);
708 for (
int i = used_digits_ - 1; i >= 0; --i) {
709 bigits_[i + zero_digits] = bigits_[i];
711 for (
int i = 0; i < zero_digits; ++i) {
714 used_digits_ += zero_digits;
715 exponent_ -= zero_digits;
716 ASSERT(used_digits_ >= 0);
722 void Bignum::BigitsShiftLeft(
int shift_amount) {
723 ASSERT(shift_amount < kBigitSize);
724 ASSERT(shift_amount >= 0);
726 for (
int i = 0; i < used_digits_; ++i) {
727 Chunk new_carry = bigits_[i] >> (kBigitSize - shift_amount);
728 bigits_[i] = ((bigits_[i] << shift_amount) + carry) & kBigitMask;
732 bigits_[used_digits_] =
carry;
738 void Bignum::SubtractTimes(
const Bignum& other,
int factor) {
741 a.AssignBignum(*
this);
742 b.AssignBignum(other);
743 b.MultiplyByUInt32(factor);
746 ASSERT(exponent_ <= other.exponent_);
748 for (
int i = 0; i < factor; ++i) {
754 int exponent_diff = other.exponent_ - exponent_;
755 for (
int i = 0; i < other.used_digits_; ++i) {
756 DoubleChunk product =
static_cast<DoubleChunk
>(factor) * other.bigits_[i];
757 DoubleChunk
remove = borrow + product;
759 bigits_[i + exponent_diff] - static_cast<Chunk>(
remove & kBigitMask);
760 bigits_[i + exponent_diff] = difference & kBigitMask;
761 borrow =
static_cast<Chunk
>((difference >> (kChunkSize - 1)) +
762 (
remove >> kBigitSize));
764 for (
int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) {
765 if (borrow == 0)
return;
766 Chunk difference = bigits_[i] - borrow;
767 bigits_[i] = difference & kBigitMask;
768 borrow = difference >> (kChunkSize - 1);
void AssignBignum(const Bignum &other)
void MultiplyByUInt64(uint64_t factor)
#define ASSERT(condition)
void AssignUInt64(uint64_t value)
void AddBignum(const Bignum &other)
void AddUInt64(uint64_t operand)
static int Compare(const Bignum &a, const Bignum &b)
void AssignHexString(Vector< const char > value)
static int PlusCompare(const Bignum &a, const Bignum &b, const Bignum &c)
static bool Equal(const Bignum &a, const Bignum &b)
void AssignPowerUInt16(uint16_t base, int exponent)
void ShiftLeft(int shift_amount)
uint16_t DivideModuloIntBignum(const Bignum &other)
void MultiplyByUInt32(uint32_t factor)
#define V8_2PART_UINT64_C(a, b)
void AssignUInt16(uint16_t value)
void SubtractBignum(const Bignum &other)
bool ToHexString(char *buffer, int buffer_size) const
static bool LessEqual(const Bignum &a, const Bignum &b)
void AssignDecimalString(Vector< const char > value)
void MultiplyByPowerOfTen(int exponent)