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);
50 ASSERT(kBigitSize >= BitSize(value));
52 if (value == 0)
return;
61 const int kUInt64Size = 64;
64 if (value == 0)
return;
66 int needed_bigits = kUInt64Size / kBigitSize + 1;
67 EnsureCapacity(needed_bigits);
68 for (
int i = 0; i < needed_bigits; ++i) {
69 bigits_[i] =
static_cast<Chunk
>(value & kBigitMask);
70 value = value >> kBigitSize;
72 used_digits_ = needed_bigits;
78 exponent_ = other.exponent_;
79 for (
int i = 0; i < other.used_digits_; ++i) {
80 bigits_[i] = other.bigits_[i];
83 for (
int i = other.used_digits_; i < used_digits_; ++i) {
86 used_digits_ = other.used_digits_;
94 for (
int i = from; i < from + digits_to_read; ++i) {
95 int digit = buffer[i] -
'0';
96 ASSERT(0 <= digit && digit <= 9);
97 result = result * 10 + digit;
105 const int kMaxUint64DecimalDigits = 19;
107 int length = value.
length();
110 while (length >= kMaxUint64DecimalDigits) {
111 uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits);
112 pos += kMaxUint64DecimalDigits;
113 length -= kMaxUint64DecimalDigits;
117 uint64_t digits = ReadUInt64(value, pos, length);
124 static int HexCharValue(
char c) {
125 if (
'0' <= c && c <=
'9')
return c -
'0';
126 if (
'a' <= c && c <=
'f')
return 10 + c -
'a';
127 if (
'A' <= c && c <=
'F')
return 10 + c -
'A';
135 int length = value.
length();
137 int needed_bigits = length * 4 / kBigitSize + 1;
138 EnsureCapacity(needed_bigits);
139 int string_index = length - 1;
140 for (
int i = 0; i < needed_bigits - 1; ++i) {
142 Chunk current_bigit = 0;
143 for (
int j = 0; j < kBigitSize / 4; j++) {
144 current_bigit += HexCharValue(value[string_index--]) << (j * 4);
146 bigits_[i] = current_bigit;
148 used_digits_ = needed_bigits - 1;
150 Chunk most_significant_bigit = 0;
151 for (
int j = 0; j <= string_index; ++j) {
152 most_significant_bigit <<= 4;
153 most_significant_bigit += HexCharValue(value[j]);
155 if (most_significant_bigit != 0) {
156 bigits_[used_digits_] = most_significant_bigit;
164 if (operand == 0)
return;
173 ASSERT(other.IsClamped());
191 EnsureCapacity(1 +
Max(BigitLength(), other.BigitLength()) - exponent_);
193 int bigit_pos = other.exponent_ - exponent_;
195 for (
int i = 0; i < other.used_digits_; ++i) {
196 Chunk sum = bigits_[bigit_pos] + other.bigits_[i] +
carry;
197 bigits_[bigit_pos] = sum & kBigitMask;
198 carry = sum >> kBigitSize;
203 Chunk sum = bigits_[bigit_pos] +
carry;
204 bigits_[bigit_pos] = sum & kBigitMask;
205 carry = sum >> kBigitSize;
208 used_digits_ =
Max(bigit_pos, used_digits_);
215 ASSERT(other.IsClamped());
221 int offset = other.exponent_ - exponent_;
224 for (i = 0; i < other.used_digits_; ++i) {
225 ASSERT((borrow == 0) || (borrow == 1));
226 Chunk difference = bigits_[i + offset] - other.bigits_[i] - borrow;
227 bigits_[i + offset] = difference & kBigitMask;
228 borrow = difference >> (kChunkSize - 1);
230 while (borrow != 0) {
231 Chunk difference = bigits_[i + offset] - borrow;
232 bigits_[i + offset] = difference & kBigitMask;
233 borrow = difference >> (kChunkSize - 1);
241 if (used_digits_ == 0)
return;
242 exponent_ += shift_amount / kBigitSize;
243 int local_shift = shift_amount % kBigitSize;
244 EnsureCapacity(used_digits_ + 1);
245 BigitsShiftLeft(local_shift);
250 if (factor == 1)
return;
255 if (used_digits_ == 0)
return;
259 ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1);
260 DoubleChunk
carry = 0;
261 for (
int i = 0; i < used_digits_; ++i) {
262 DoubleChunk product =
static_cast<DoubleChunk
>(factor) * bigits_[i] + carry;
263 bigits_[i] =
static_cast<Chunk
>(product & kBigitMask);
264 carry = (product >> kBigitSize);
267 EnsureCapacity(used_digits_ + 1);
268 bigits_[used_digits_] =
static_cast<Chunk
>(carry & kBigitMask);
270 carry >>= kBigitSize;
276 if (factor == 1)
return;
283 uint64_t low = factor & 0xFFFFFFFF;
284 uint64_t high = factor >> 32;
285 for (
int i = 0; i < used_digits_; ++i) {
286 uint64_t product_low = low * bigits_[i];
287 uint64_t product_high = high * bigits_[i];
288 uint64_t tmp = (carry & kBigitMask) + product_low;
289 bigits_[i] =
static_cast<Chunk
>(tmp & kBigitMask);
290 carry = (carry >> kBigitSize) + (tmp >> kBigitSize) +
291 (product_high << (32 - kBigitSize));
294 EnsureCapacity(used_digits_ + 1);
295 bigits_[used_digits_] =
static_cast<Chunk
>(carry & kBigitMask);
297 carry >>= kBigitSize;
310 const uint32_t kFive7 = kFive6 * 5;
311 const uint32_t kFive8 = kFive7 * 5;
312 const uint32_t kFive9 = kFive8 * 5;
313 const uint32_t kFive10 = kFive9 * 5;
314 const uint32_t kFive11 = kFive10 * 5;
315 const uint32_t kFive12 = kFive11 * 5;
316 const uint32_t kFive13 = kFive12 * 5;
317 const uint32_t kFive1_to_12[] =
318 { kFive1, kFive2, kFive3, kFive4, kFive5, kFive6,
319 kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 };
322 if (exponent == 0)
return;
323 if (used_digits_ == 0)
return;
326 int remaining_exponent = exponent;
327 while (remaining_exponent >= 27) {
329 remaining_exponent -= 27;
331 while (remaining_exponent >= 13) {
333 remaining_exponent -= 13;
335 if (remaining_exponent > 0) {
344 int product_length = 2 * used_digits_;
345 EnsureCapacity(product_length);
359 if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_digits_) {
362 DoubleChunk accumulator = 0;
364 int copy_offset = used_digits_;
365 for (
int i = 0; i < used_digits_; ++i) {
366 bigits_[copy_offset + i] = bigits_[i];
369 for (
int i = 0; i < used_digits_; ++i) {
372 int bigit_index1 = i;
373 int bigit_index2 = 0;
375 while (bigit_index1 >= 0) {
376 Chunk chunk1 = bigits_[copy_offset + bigit_index1];
377 Chunk chunk2 = bigits_[copy_offset + bigit_index2];
378 accumulator +=
static_cast<DoubleChunk
>(chunk1) * chunk2;
382 bigits_[i] =
static_cast<Chunk
>(accumulator) & kBigitMask;
383 accumulator >>= kBigitSize;
385 for (
int i = used_digits_; i < product_length; ++i) {
386 int bigit_index1 = used_digits_ - 1;
387 int bigit_index2 = i - bigit_index1;
390 while (bigit_index2 < used_digits_) {
391 Chunk chunk1 = bigits_[copy_offset + bigit_index1];
392 Chunk chunk2 = bigits_[copy_offset + bigit_index2];
393 accumulator +=
static_cast<DoubleChunk
>(chunk1) * chunk2;
400 bigits_[i] =
static_cast<Chunk
>(accumulator) & kBigitMask;
401 accumulator >>= kBigitSize;
408 used_digits_ = product_length;
416 ASSERT(power_exponent >= 0);
417 if (power_exponent == 0) {
426 while ((base & 1) == 0) {
432 while (tmp_base != 0) {
436 int final_size = bit_size * power_exponent;
438 EnsureCapacity(final_size / kBigitSize + 2);
442 while (power_exponent >= mask) mask <<= 1;
448 uint64_t this_value = base;
450 bool delayed_multipliciation =
false;
451 const uint64_t max_32bits = 0xFFFFFFFF;
452 while (mask != 0 && this_value <= max_32bits) {
453 this_value = this_value * this_value;
456 if ((power_exponent & mask) != 0) {
457 uint64_t base_bits_mask =
458 ~((
static_cast<uint64_t
>(1) << (64 - bit_size)) - 1);
459 bool high_bits_zero = (this_value & base_bits_mask) == 0;
460 if (high_bits_zero) {
463 delayed_multipliciation =
true;
469 if (delayed_multipliciation) {
476 if ((power_exponent & mask) != 0) {
490 ASSERT(other.IsClamped());
491 ASSERT(other.used_digits_ > 0);
495 if (BigitLength() < other.BigitLength()) {
505 while (BigitLength() > other.BigitLength()) {
509 ASSERT(other.bigits_[other.used_digits_ - 1] >= ((1 << kBigitSize) / 16));
512 result += bigits_[used_digits_ - 1];
513 SubtractTimes(other, bigits_[used_digits_ - 1]);
516 ASSERT(BigitLength() == other.BigitLength());
521 Chunk this_bigit = bigits_[used_digits_ - 1];
522 Chunk other_bigit = other.bigits_[other.used_digits_ - 1];
524 if (other.used_digits_ == 1) {
526 int quotient = this_bigit / other_bigit;
527 bigits_[used_digits_ - 1] = this_bigit - other_bigit * quotient;
533 int division_estimate = this_bigit / (other_bigit + 1);
534 result += division_estimate;
535 SubtractTimes(other, division_estimate);
537 if (other_bigit * (division_estimate + 1) > this_bigit) {
552 static int SizeInHexChars(
S number) {
555 while (number != 0) {
563 static char HexCharOfValue(
int value) {
564 ASSERT(0 <= value && value <= 16);
565 if (value < 10)
return value +
'0';
566 return value - 10 +
'A';
573 ASSERT(kBigitSize % 4 == 0);
574 const int kHexCharsPerBigit = kBigitSize / 4;
576 if (used_digits_ == 0) {
577 if (buffer_size < 2)
return false;
583 int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit +
584 SizeInHexChars(bigits_[used_digits_ - 1]) + 1;
585 if (needed_chars > buffer_size)
return false;
586 int string_index = needed_chars - 1;
587 buffer[string_index--] =
'\0';
588 for (
int i = 0; i < exponent_; ++i) {
589 for (
int j = 0; j < kHexCharsPerBigit; ++j) {
590 buffer[string_index--] =
'0';
593 for (
int i = 0; i < used_digits_ - 1; ++i) {
594 Chunk current_bigit = bigits_[i];
595 for (
int j = 0; j < kHexCharsPerBigit; ++j) {
596 buffer[string_index--] = HexCharOfValue(current_bigit & 0xF);
601 Chunk most_significant_bigit = bigits_[used_digits_ - 1];
602 while (most_significant_bigit != 0) {
603 buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF);
604 most_significant_bigit >>= 4;
610 Bignum::Chunk Bignum::BigitAt(
int index)
const {
611 if (index >= BigitLength())
return 0;
612 if (index < exponent_)
return 0;
613 return bigits_[index - exponent_];
620 int bigit_length_a = a.BigitLength();
621 int bigit_length_b = b.BigitLength();
622 if (bigit_length_a < bigit_length_b)
return -1;
623 if (bigit_length_a > bigit_length_b)
return +1;
624 for (
int i = bigit_length_a - 1; i >=
Min(a.exponent_, b.exponent_); --i) {
625 Chunk bigit_a = a.BigitAt(i);
626 Chunk bigit_b = b.BigitAt(i);
627 if (bigit_a < bigit_b)
return -1;
628 if (bigit_a > bigit_b)
return +1;
639 if (a.BigitLength() < b.BigitLength()) {
642 if (a.BigitLength() + 1 < c.BigitLength())
return -1;
643 if (a.BigitLength() > c.BigitLength())
return +1;
647 if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) {
653 int min_exponent =
Min(
Min(a.exponent_, b.exponent_), c.exponent_);
654 for (
int i = c.BigitLength() - 1; i >= min_exponent; --i) {
655 Chunk chunk_a = a.BigitAt(i);
656 Chunk chunk_b = b.BigitAt(i);
657 Chunk chunk_c = c.BigitAt(i);
658 Chunk sum = chunk_a + chunk_b;
659 if (sum > chunk_c + borrow) {
662 borrow = chunk_c + borrow - sum;
663 if (borrow > 1)
return -1;
664 borrow <<= kBigitSize;
667 if (borrow == 0)
return 0;
672 void Bignum::Clamp() {
673 while (used_digits_ > 0 && bigits_[used_digits_ - 1] == 0) {
676 if (used_digits_ == 0) {
683 bool Bignum::IsClamped()
const {
684 return used_digits_ == 0 || bigits_[used_digits_ - 1] != 0;
688 void Bignum::Zero() {
689 for (
int i = 0; i < used_digits_; ++i) {
697 void Bignum::Align(
const Bignum& other) {
698 if (exponent_ > other.exponent_) {
705 int zero_digits = exponent_ - other.exponent_;
706 EnsureCapacity(used_digits_ + zero_digits);
707 for (
int i = used_digits_ - 1; i >= 0; --i) {
708 bigits_[i + zero_digits] = bigits_[i];
710 for (
int i = 0; i < zero_digits; ++i) {
713 used_digits_ += zero_digits;
714 exponent_ -= zero_digits;
715 ASSERT(used_digits_ >= 0);
721 void Bignum::BigitsShiftLeft(
int shift_amount) {
722 ASSERT(shift_amount < kBigitSize);
723 ASSERT(shift_amount >= 0);
725 for (
int i = 0; i < used_digits_; ++i) {
726 Chunk new_carry = bigits_[i] >> (kBigitSize - shift_amount);
727 bigits_[i] = ((bigits_[i] << shift_amount) + carry) & kBigitMask;
731 bigits_[used_digits_] =
carry;
737 void Bignum::SubtractTimes(
const Bignum& other,
int factor) {
738 ASSERT(exponent_ <= other.exponent_);
740 for (
int i = 0; i < factor; ++i) {
746 int exponent_diff = other.exponent_ - exponent_;
747 for (
int i = 0; i < other.used_digits_; ++i) {
748 DoubleChunk product =
static_cast<DoubleChunk
>(factor) * other.bigits_[i];
749 DoubleChunk
remove = borrow + product;
751 bigits_[i + exponent_diff] - static_cast<Chunk>(
remove & kBigitMask);
752 bigits_[i + exponent_diff] = difference & kBigitMask;
753 borrow =
static_cast<Chunk
>((difference >> (kChunkSize - 1)) +
754 (
remove >> kBigitSize));
756 for (
int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) {
757 if (borrow == 0)
return;
758 Chunk difference = bigits_[i] - borrow;
759 bigits_[i] = difference & kBigitMask;
760 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)
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)