V8 Project
v8::internal::Bignum Class Reference

#include <bignum.h>

+ Collaboration diagram for v8::internal::Bignum:

Public Member Functions

 Bignum ()
 
void AssignUInt16 (uint16_t value)
 
void AssignUInt64 (uint64_t value)
 
void AssignBignum (const Bignum &other)
 
void AssignDecimalString (Vector< const char > value)
 
void AssignHexString (Vector< const char > value)
 
void AssignPowerUInt16 (uint16_t base, int exponent)
 
void AddUInt16 (uint16_t operand)
 
void AddUInt64 (uint64_t operand)
 
void AddBignum (const Bignum &other)
 
void SubtractBignum (const Bignum &other)
 
void Square ()
 
void ShiftLeft (int shift_amount)
 
void MultiplyByUInt32 (uint32_t factor)
 
void MultiplyByUInt64 (uint64_t factor)
 
void MultiplyByPowerOfTen (int exponent)
 
void Times10 ()
 
uint16_t DivideModuloIntBignum (const Bignum &other)
 
bool ToHexString (char *buffer, int buffer_size) const
 

Static Public Member Functions

static int Compare (const Bignum &a, const Bignum &b)
 
static bool Equal (const Bignum &a, const Bignum &b)
 
static bool LessEqual (const Bignum &a, const Bignum &b)
 
static bool Less (const Bignum &a, const Bignum &b)
 
static int PlusCompare (const Bignum &a, const Bignum &b, const Bignum &c)
 
static bool PlusEqual (const Bignum &a, const Bignum &b, const Bignum &c)
 
static bool PlusLessEqual (const Bignum &a, const Bignum &b, const Bignum &c)
 
static bool PlusLess (const Bignum &a, const Bignum &b, const Bignum &c)
 

Static Public Attributes

static const int kMaxSignificantBits = 3584
 

Private Types

typedef uint32_t Chunk
 
typedef uint64_t DoubleChunk
 

Private Member Functions

void EnsureCapacity (int size)
 
void Align (const Bignum &other)
 
void Clamp ()
 
bool IsClamped () const
 
void Zero ()
 
void BigitsShiftLeft (int shift_amount)
 
int BigitLength () const
 
Chunk BigitAt (int index) const
 
void SubtractTimes (const Bignum &other, int factor)
 
 DISALLOW_COPY_AND_ASSIGN (Bignum)
 

Private Attributes

Chunk bigits_buffer_ [kBigitCapacity]
 
Vector< Chunkbigits_
 
int used_digits_
 
int exponent_
 

Static Private Attributes

static const int kChunkSize = sizeof(Chunk) * 8
 
static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8
 
static const int kBigitSize = 28
 
static const Chunk kBigitMask = (1 << kBigitSize) - 1
 
static const int kBigitCapacity = kMaxSignificantBits / kBigitSize
 

Detailed Description

Definition at line 11 of file bignum.h.

Member Typedef Documentation

◆ Chunk

Definition at line 74 of file bignum.h.

◆ DoubleChunk

typedef uint64_t v8::internal::Bignum::DoubleChunk
private

Definition at line 75 of file bignum.h.

Constructor & Destructor Documentation

◆ Bignum()

v8::internal::Bignum::Bignum ( )

Definition at line 13 of file bignum.cc.

15  for (int i = 0; i < kBigitCapacity; ++i) {
16  bigits_[i] = 0;
17  }
18 }
Chunk bigits_buffer_[kBigitCapacity]
Definition: bignum.h:105
static const int kBigitCapacity
Definition: bignum.h:85
Vector< Chunk > bigits_
Definition: bignum.h:108

References bigits_, and kBigitCapacity.

Member Function Documentation

◆ AddBignum()

void v8::internal::Bignum::AddBignum ( const Bignum other)

Definition at line 150 of file bignum.cc.

150  {
151  DCHECK(IsClamped());
152  DCHECK(other.IsClamped());
153 
154  // If this has a greater exponent than other append zero-bigits to this.
155  // After this call exponent_ <= other.exponent_.
156  Align(other);
157 
158  // There are two possibilities:
159  // aaaaaaaaaaa 0000 (where the 0s represent a's exponent)
160  // bbbbb 00000000
161  // ----------------
162  // ccccccccccc 0000
163  // or
164  // aaaaaaaaaa 0000
165  // bbbbbbbbb 0000000
166  // -----------------
167  // cccccccccccc 0000
168  // In both cases we might need a carry bigit.
169 
170  EnsureCapacity(1 + Max(BigitLength(), other.BigitLength()) - exponent_);
171  Chunk carry = 0;
172  int bigit_pos = other.exponent_ - exponent_;
173  DCHECK(bigit_pos >= 0);
174  for (int i = 0; i < other.used_digits_; ++i) {
175  Chunk sum = bigits_[bigit_pos] + other.bigits_[i] + carry;
176  bigits_[bigit_pos] = sum & kBigitMask;
177  carry = sum >> kBigitSize;
178  bigit_pos++;
179  }
180 
181  while (carry != 0) {
182  Chunk sum = bigits_[bigit_pos] + carry;
183  bigits_[bigit_pos] = sum & kBigitMask;
184  carry = sum >> kBigitSize;
185  bigit_pos++;
186  }
187  used_digits_ = Max(bigit_pos, used_digits_);
188  DCHECK(IsClamped());
189 }
static const int kBigitSize
Definition: bignum.h:81
void Align(const Bignum &other)
Definition: bignum.cc:676
bool IsClamped() const
Definition: bignum.cc:662
static const Chunk kBigitMask
Definition: bignum.h:82
uint32_t Chunk
Definition: bignum.h:74
int BigitLength() const
Definition: bignum.h:101
void EnsureCapacity(int size)
Definition: bignum.h:87
#define DCHECK(condition)
Definition: logging.h:205
static LifetimePosition Max(LifetimePosition a, LifetimePosition b)

References Align(), BigitLength(), bigits_, v8::internal::carry, DCHECK, EnsureCapacity(), exponent_, IsClamped(), kBigitMask, kBigitSize, v8::internal::Max(), and used_digits_.

Referenced by AddUInt64().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ AddUInt16()

void v8::internal::Bignum::AddUInt16 ( uint16_t  operand)

◆ AddUInt64()

void v8::internal::Bignum::AddUInt64 ( uint64_t  operand)

Definition at line 142 of file bignum.cc.

142  {
143  if (operand == 0) return;
144  Bignum other;
145  other.AssignUInt64(operand);
146  AddBignum(other);
147 }
void AddBignum(const Bignum &other)
Definition: bignum.cc:150

References AddBignum(), and AssignUInt64().

Referenced by AssignDecimalString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Align()

void v8::internal::Bignum::Align ( const Bignum other)
private

Definition at line 676 of file bignum.cc.

676  {
677  if (exponent_ > other.exponent_) {
678  // If "X" represents a "hidden" digit (by the exponent) then we are in the
679  // following case (a == this, b == other):
680  // a: aaaaaaXXXX or a: aaaaaXXX
681  // b: bbbbbbX b: bbbbbbbbXX
682  // We replace some of the hidden digits (X) of a with 0 digits.
683  // a: aaaaaa000X or a: aaaaa0XX
684  int zero_digits = exponent_ - other.exponent_;
685  EnsureCapacity(used_digits_ + zero_digits);
686  for (int i = used_digits_ - 1; i >= 0; --i) {
687  bigits_[i + zero_digits] = bigits_[i];
688  }
689  for (int i = 0; i < zero_digits; ++i) {
690  bigits_[i] = 0;
691  }
692  used_digits_ += zero_digits;
693  exponent_ -= zero_digits;
694  DCHECK(used_digits_ >= 0);
695  DCHECK(exponent_ >= 0);
696  }
697 }

References bigits_, DCHECK, EnsureCapacity(), exponent_, and used_digits_.

Referenced by AddBignum(), DivideModuloIntBignum(), and SubtractBignum().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ AssignBignum()

void v8::internal::Bignum::AssignBignum ( const Bignum other)

Definition at line 56 of file bignum.cc.

56  {
57  exponent_ = other.exponent_;
58  for (int i = 0; i < other.used_digits_; ++i) {
59  bigits_[i] = other.bigits_[i];
60  }
61  // Clear the excess digits (if there were any).
62  for (int i = other.used_digits_; i < used_digits_; ++i) {
63  bigits_[i] = 0;
64  }
65  used_digits_ = other.used_digits_;
66 }

References bigits_, exponent_, and used_digits_.

Referenced by v8::internal::FixupMultiply10(), v8::internal::InitialScaledStartValuesNegativeExponentNegativePower(), and SubtractTimes().

+ Here is the caller graph for this function:

◆ AssignDecimalString()

void v8::internal::Bignum::AssignDecimalString ( Vector< const char >  value)

Definition at line 82 of file bignum.cc.

82  {
83  // 2^64 = 18446744073709551616 > 10^19
84  const int kMaxUint64DecimalDigits = 19;
85  Zero();
86  int length = value.length();
87  int pos = 0;
88  // Let's just say that each digit needs 4 bits.
89  while (length >= kMaxUint64DecimalDigits) {
90  uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits);
92  length -= kMaxUint64DecimalDigits;
94  AddUInt64(digits);
95  }
96  uint64_t digits = ReadUInt64(value, pos, length);
97  MultiplyByPowerOfTen(length);
98  AddUInt64(digits);
99  Clamp();
100 }
void MultiplyByPowerOfTen(int exponent)
Definition: bignum.cc:281
void AddUInt64(uint64_t operand)
Definition: bignum.cc:142
int length() const
Definition: vector.h:41
static const int kMaxUint64DecimalDigits
Definition: strtod.cc:25
static uint64_t ReadUInt64(Vector< const char > buffer, int from, int digits_to_read)
Definition: bignum.cc:69

References AddUInt64(), Clamp(), v8::internal::kMaxUint64DecimalDigits, v8::internal::Vector< T >::length(), MultiplyByPowerOfTen(), v8::internal::ReadUInt64(), and Zero().

Referenced by v8::internal::BignumStrtod().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ AssignHexString()

void v8::internal::Bignum::AssignHexString ( Vector< const char >  value)

Definition at line 112 of file bignum.cc.

112  {
113  Zero();
114  int length = value.length();
115 
116  int needed_bigits = length * 4 / kBigitSize + 1;
117  EnsureCapacity(needed_bigits);
118  int string_index = length - 1;
119  for (int i = 0; i < needed_bigits - 1; ++i) {
120  // These bigits are guaranteed to be "full".
121  Chunk current_bigit = 0;
122  for (int j = 0; j < kBigitSize / 4; j++) {
123  current_bigit += HexCharValue(value[string_index--]) << (j * 4);
124  }
125  bigits_[i] = current_bigit;
126  }
127  used_digits_ = needed_bigits - 1;
128 
129  Chunk most_significant_bigit = 0; // Could be = 0;
130  for (int j = 0; j <= string_index; ++j) {
131  most_significant_bigit <<= 4;
132  most_significant_bigit += HexCharValue(value[j]);
133  }
134  if (most_significant_bigit != 0) {
135  bigits_[used_digits_] = most_significant_bigit;
136  used_digits_++;
137  }
138  Clamp();
139 }
static int HexCharValue(char c)
Definition: bignum.cc:103

References bigits_, Clamp(), EnsureCapacity(), v8::internal::HexCharValue(), kBigitSize, v8::internal::Vector< T >::length(), used_digits_, and Zero().

+ Here is the call graph for this function:

◆ AssignPowerUInt16()

void v8::internal::Bignum::AssignPowerUInt16 ( uint16_t  base,
int  exponent 
)

Definition at line 393 of file bignum.cc.

393  {
394  DCHECK(base != 0);
395  DCHECK(power_exponent >= 0);
396  if (power_exponent == 0) {
397  AssignUInt16(1);
398  return;
399  }
400  Zero();
401  int shifts = 0;
402  // We expect base to be in range 2-32, and most often to be 10.
403  // It does not make much sense to implement different algorithms for counting
404  // the bits.
405  while ((base & 1) == 0) {
406  base >>= 1;
407  shifts++;
408  }
409  int bit_size = 0;
410  int tmp_base = base;
411  while (tmp_base != 0) {
412  tmp_base >>= 1;
413  bit_size++;
414  }
415  int final_size = bit_size * power_exponent;
416  // 1 extra bigit for the shifting, and one for rounded final_size.
417  EnsureCapacity(final_size / kBigitSize + 2);
418 
419  // Left to Right exponentiation.
420  int mask = 1;
421  while (power_exponent >= mask) mask <<= 1;
422 
423  // The mask is now pointing to the bit above the most significant 1-bit of
424  // power_exponent.
425  // Get rid of first 1-bit;
426  mask >>= 2;
427  uint64_t this_value = base;
428 
429  bool delayed_multipliciation = false;
430  const uint64_t max_32bits = 0xFFFFFFFF;
431  while (mask != 0 && this_value <= max_32bits) {
432  this_value = this_value * this_value;
433  // Verify that there is enough space in this_value to perform the
434  // multiplication. The first bit_size bits must be 0.
435  if ((power_exponent & mask) != 0) {
436  uint64_t base_bits_mask =
437  ~((static_cast<uint64_t>(1) << (64 - bit_size)) - 1);
438  bool high_bits_zero = (this_value & base_bits_mask) == 0;
439  if (high_bits_zero) {
440  this_value *= base;
441  } else {
442  delayed_multipliciation = true;
443  }
444  }
445  mask >>= 1;
446  }
447  AssignUInt64(this_value);
448  if (delayed_multipliciation) {
449  MultiplyByUInt32(base);
450  }
451 
452  // Now do the same thing as a bignum.
453  while (mask != 0) {
454  Square();
455  if ((power_exponent & mask) != 0) {
456  MultiplyByUInt32(base);
457  }
458  mask >>= 1;
459  }
460 
461  // And finally add the saved shifts.
462  ShiftLeft(shifts * power_exponent);
463 }
void ShiftLeft(int shift_amount)
Definition: bignum.cc:219
void AssignUInt16(uint16_t value)
Definition: bignum.cc:28
void AssignUInt64(uint64_t value)
Definition: bignum.cc:39
void MultiplyByUInt32(uint32_t factor)
Definition: bignum.cc:228

References AssignUInt16(), AssignUInt64(), DCHECK, EnsureCapacity(), kBigitSize, MultiplyByUInt32(), ShiftLeft(), Square(), and Zero().

Referenced by v8::internal::InitialScaledStartValuesNegativeExponentNegativePower(), v8::internal::InitialScaledStartValuesNegativeExponentPositivePower(), and v8::internal::InitialScaledStartValuesPositiveExponent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ AssignUInt16()

void v8::internal::Bignum::AssignUInt16 ( uint16_t  value)

Definition at line 28 of file bignum.cc.

28  {
29  DCHECK(kBigitSize >= BitSize(value));
30  Zero();
31  if (value == 0) return;
32 
33  EnsureCapacity(1);
34  bigits_[0] = value;
35  used_digits_ = 1;
36 }
static int BitSize(S value)
Definition: bignum.cc:22

References bigits_, v8::internal::BitSize(), DCHECK, EnsureCapacity(), kBigitSize, used_digits_, and Zero().

Referenced by AssignPowerUInt16(), v8::internal::InitialScaledStartValuesNegativeExponentNegativePower(), v8::internal::InitialScaledStartValuesNegativeExponentPositivePower(), and v8::internal::InitialScaledStartValuesPositiveExponent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ AssignUInt64()

void v8::internal::Bignum::AssignUInt64 ( uint64_t  value)

Definition at line 39 of file bignum.cc.

39  {
40  const int kUInt64Size = 64;
41 
42  Zero();
43  if (value == 0) return;
44 
45  int needed_bigits = kUInt64Size / kBigitSize + 1;
46  EnsureCapacity(needed_bigits);
47  for (int i = 0; i < needed_bigits; ++i) {
48  bigits_[i] = static_cast<Chunk>(value & kBigitMask);
49  value = value >> kBigitSize;
50  }
51  used_digits_ = needed_bigits;
52  Clamp();
53 }

References bigits_, Clamp(), EnsureCapacity(), kBigitMask, kBigitSize, used_digits_, and Zero().

Referenced by AddUInt64(), AssignPowerUInt16(), v8::internal::BignumStrtod(), v8::internal::InitialScaledStartValuesNegativeExponentPositivePower(), and v8::internal::InitialScaledStartValuesPositiveExponent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ BigitAt()

Bignum::Chunk v8::internal::Bignum::BigitAt ( int  index) const
private

Definition at line 589 of file bignum.cc.

589  {
590  if (index >= BigitLength()) return 0;
591  if (index < exponent_) return 0;
592  return bigits_[index - exponent_];
593 }

References BigitLength(), bigits_, and exponent_.

Referenced by Compare(), and PlusCompare().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ BigitLength()

int v8::internal::Bignum::BigitLength ( ) const
inlineprivate

Definition at line 101 of file bignum.h.

101 { return used_digits_ + exponent_; }

References exponent_, and used_digits_.

Referenced by AddBignum(), BigitAt(), Compare(), DivideModuloIntBignum(), PlusCompare(), and ToHexString().

+ Here is the caller graph for this function:

◆ BigitsShiftLeft()

void v8::internal::Bignum::BigitsShiftLeft ( int  shift_amount)
private

Definition at line 700 of file bignum.cc.

700  {
701  DCHECK(shift_amount < kBigitSize);
702  DCHECK(shift_amount >= 0);
703  Chunk carry = 0;
704  for (int i = 0; i < used_digits_; ++i) {
705  Chunk new_carry = bigits_[i] >> (kBigitSize - shift_amount);
706  bigits_[i] = ((bigits_[i] << shift_amount) + carry) & kBigitMask;
707  carry = new_carry;
708  }
709  if (carry != 0) {
711  used_digits_++;
712  }
713 }

References bigits_, v8::internal::carry, DCHECK, kBigitMask, kBigitSize, and used_digits_.

Referenced by ShiftLeft().

+ Here is the caller graph for this function:

◆ Clamp()

void v8::internal::Bignum::Clamp ( )
private

Definition at line 651 of file bignum.cc.

651  {
652  while (used_digits_ > 0 && bigits_[used_digits_ - 1] == 0) {
653  used_digits_--;
654  }
655  if (used_digits_ == 0) {
656  // Zero.
657  exponent_ = 0;
658  }
659 }

References bigits_, exponent_, and used_digits_.

Referenced by AssignDecimalString(), AssignHexString(), AssignUInt64(), DivideModuloIntBignum(), Square(), SubtractBignum(), and SubtractTimes().

+ Here is the caller graph for this function:

◆ Compare()

int v8::internal::Bignum::Compare ( const Bignum a,
const Bignum b 
)
static

Definition at line 596 of file bignum.cc.

596  {
597  DCHECK(a.IsClamped());
598  DCHECK(b.IsClamped());
599  int bigit_length_a = a.BigitLength();
600  int bigit_length_b = b.BigitLength();
601  if (bigit_length_a < bigit_length_b) return -1;
602  if (bigit_length_a > bigit_length_b) return +1;
603  for (int i = bigit_length_a - 1; i >= Min(a.exponent_, b.exponent_); --i) {
604  Chunk bigit_a = a.BigitAt(i);
605  Chunk bigit_b = b.BigitAt(i);
606  if (bigit_a < bigit_b) return -1;
607  if (bigit_a > bigit_b) return +1;
608  // Otherwise they are equal up to this digit. Try the next digit.
609  }
610  return 0;
611 }
static LifetimePosition Min(LifetimePosition a, LifetimePosition b)

References BigitAt(), BigitLength(), DCHECK, exponent_, IsClamped(), and v8::internal::Min().

Referenced by v8::internal::BignumStrtod(), Equal(), Less(), and LessEqual().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DISALLOW_COPY_AND_ASSIGN()

v8::internal::Bignum::DISALLOW_COPY_AND_ASSIGN ( Bignum  )
private

◆ DivideModuloIntBignum()

uint16_t v8::internal::Bignum::DivideModuloIntBignum ( const Bignum other)

Definition at line 467 of file bignum.cc.

467  {
468  DCHECK(IsClamped());
469  DCHECK(other.IsClamped());
470  DCHECK(other.used_digits_ > 0);
471 
472  // Easy case: if we have less digits than the divisor than the result is 0.
473  // Note: this handles the case where this == 0, too.
474  if (BigitLength() < other.BigitLength()) {
475  return 0;
476  }
477 
478  Align(other);
479 
480  uint16_t result = 0;
481 
482  // Start by removing multiples of 'other' until both numbers have the same
483  // number of digits.
484  while (BigitLength() > other.BigitLength()) {
485  // This naive approach is extremely inefficient if the this divided other
486  // might be big. This function is implemented for doubleToString where
487  // the result should be small (less than 10).
488  DCHECK(other.bigits_[other.used_digits_ - 1] >= ((1 << kBigitSize) / 16));
489  // Remove the multiples of the first digit.
490  // Example this = 23 and other equals 9. -> Remove 2 multiples.
491  result += bigits_[used_digits_ - 1];
492  SubtractTimes(other, bigits_[used_digits_ - 1]);
493  }
494 
495  DCHECK(BigitLength() == other.BigitLength());
496 
497  // Both bignums are at the same length now.
498  // Since other has more than 0 digits we know that the access to
499  // bigits_[used_digits_ - 1] is safe.
500  Chunk this_bigit = bigits_[used_digits_ - 1];
501  Chunk other_bigit = other.bigits_[other.used_digits_ - 1];
502 
503  if (other.used_digits_ == 1) {
504  // Shortcut for easy (and common) case.
505  int quotient = this_bigit / other_bigit;
506  bigits_[used_digits_ - 1] = this_bigit - other_bigit * quotient;
507  result += quotient;
508  Clamp();
509  return result;
510  }
511 
512  int division_estimate = this_bigit / (other_bigit + 1);
513  result += division_estimate;
514  SubtractTimes(other, division_estimate);
515 
516  if (other_bigit * (division_estimate + 1) > this_bigit) {
517  // No need to even try to subtract. Even if other's remaining digits were 0
518  // another subtraction would be too much.
519  return result;
520  }
521 
522  while (LessEqual(other, *this)) {
523  SubtractBignum(other);
524  result++;
525  }
526  return result;
527 }
void SubtractBignum(const Bignum &other)
Definition: bignum.cc:192
static bool LessEqual(const Bignum &a, const Bignum &b)
Definition: bignum.h:52
void SubtractTimes(const Bignum &other, int factor)
Definition: bignum.cc:716
unsigned short uint16_t
Definition: unicode.cc:23

References Align(), BigitLength(), bigits_, Clamp(), DCHECK, IsClamped(), kBigitSize, LessEqual(), SubtractBignum(), SubtractTimes(), and used_digits_.

Referenced by v8::internal::GenerateCountedDigits(), and v8::internal::GenerateShortestDigits().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ EnsureCapacity()

void v8::internal::Bignum::EnsureCapacity ( int  size)
inlineprivate

Definition at line 87 of file bignum.h.

87  {
88  if (size > kBigitCapacity) {
89  UNREACHABLE();
90  }
91  }
enable harmony numeric enable harmony object literal extensions Optimize object size
#define UNREACHABLE()
Definition: logging.h:30

References kBigitCapacity, size, and UNREACHABLE.

Referenced by AddBignum(), Align(), AssignHexString(), AssignPowerUInt16(), AssignUInt16(), AssignUInt64(), MultiplyByUInt32(), MultiplyByUInt64(), ShiftLeft(), and Square().

+ Here is the caller graph for this function:

◆ Equal()

static bool v8::internal::Bignum::Equal ( const Bignum a,
const Bignum b 
)
inlinestatic

Definition at line 49 of file bignum.h.

49  {
50  return Compare(a, b) == 0;
51  }
static int Compare(const Bignum &a, const Bignum &b)
Definition: bignum.cc:596

References Compare().

Referenced by v8::internal::FixupMultiply10(), v8::internal::GenerateShortestDigits(), and SubtractTimes().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IsClamped()

bool v8::internal::Bignum::IsClamped ( ) const
private

Definition at line 662 of file bignum.cc.

662  {
663  return used_digits_ == 0 || bigits_[used_digits_ - 1] != 0;
664 }

References bigits_, and used_digits_.

Referenced by AddBignum(), Compare(), DivideModuloIntBignum(), PlusCompare(), Square(), SubtractBignum(), and ToHexString().

+ Here is the caller graph for this function:

◆ Less()

static bool v8::internal::Bignum::Less ( const Bignum a,
const Bignum b 
)
inlinestatic

Definition at line 55 of file bignum.h.

55  {
56  return Compare(a, b) < 0;
57  }

References Compare().

Referenced by v8::internal::GenerateShortestDigits().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ LessEqual()

static bool v8::internal::Bignum::LessEqual ( const Bignum a,
const Bignum b 
)
inlinestatic

Definition at line 52 of file bignum.h.

52  {
53  return Compare(a, b) <= 0;
54  }

References Compare().

Referenced by DivideModuloIntBignum(), v8::internal::GenerateShortestDigits(), and SubtractBignum().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MultiplyByPowerOfTen()

void v8::internal::Bignum::MultiplyByPowerOfTen ( int  exponent)

Definition at line 281 of file bignum.cc.

281  {
282  const uint64_t kFive27 = V8_2PART_UINT64_C(0x6765c793, fa10079d);
283  const uint16_t kFive1 = 5;
284  const uint16_t kFive2 = kFive1 * 5;
285  const uint16_t kFive3 = kFive2 * 5;
286  const uint16_t kFive4 = kFive3 * 5;
287  const uint16_t kFive5 = kFive4 * 5;
288  const uint16_t kFive6 = kFive5 * 5;
289  const uint32_t kFive7 = kFive6 * 5;
290  const uint32_t kFive8 = kFive7 * 5;
291  const uint32_t kFive9 = kFive8 * 5;
292  const uint32_t kFive10 = kFive9 * 5;
293  const uint32_t kFive11 = kFive10 * 5;
294  const uint32_t kFive12 = kFive11 * 5;
295  const uint32_t kFive13 = kFive12 * 5;
296  const uint32_t kFive1_to_12[] =
297  { kFive1, kFive2, kFive3, kFive4, kFive5, kFive6,
298  kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 };
299 
300  DCHECK(exponent >= 0);
301  if (exponent == 0) return;
302  if (used_digits_ == 0) return;
303 
304  // We shift by exponent at the end just before returning.
305  int remaining_exponent = exponent;
306  while (remaining_exponent >= 27) {
307  MultiplyByUInt64(kFive27);
308  remaining_exponent -= 27;
309  }
310  while (remaining_exponent >= 13) {
311  MultiplyByUInt32(kFive13);
312  remaining_exponent -= 13;
313  }
314  if (remaining_exponent > 0) {
315  MultiplyByUInt32(kFive1_to_12[remaining_exponent - 1]);
316  }
317  ShiftLeft(exponent);
318 }
void MultiplyByUInt64(uint64_t factor)
Definition: bignum.cc:254
#define V8_2PART_UINT64_C(a, b)
Definition: macros.h:376

References DCHECK, MultiplyByUInt32(), MultiplyByUInt64(), ShiftLeft(), used_digits_, and V8_2PART_UINT64_C.

Referenced by AssignDecimalString(), and v8::internal::BignumStrtod().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MultiplyByUInt32()

void v8::internal::Bignum::MultiplyByUInt32 ( uint32_t  factor)

Definition at line 228 of file bignum.cc.

228  {
229  if (factor == 1) return;
230  if (factor == 0) {
231  Zero();
232  return;
233  }
234  if (used_digits_ == 0) return;
235 
236  // The product of a bigit with the factor is of size kBigitSize + 32.
237  // Assert that this number + 1 (for the carry) fits into double chunk.
238  DCHECK(kDoubleChunkSize >= kBigitSize + 32 + 1);
239  DoubleChunk carry = 0;
240  for (int i = 0; i < used_digits_; ++i) {
241  DoubleChunk product = static_cast<DoubleChunk>(factor) * bigits_[i] + carry;
242  bigits_[i] = static_cast<Chunk>(product & kBigitMask);
243  carry = (product >> kBigitSize);
244  }
245  while (carry != 0) {
247  bigits_[used_digits_] = static_cast<Chunk>(carry & kBigitMask);
248  used_digits_++;
249  carry >>= kBigitSize;
250  }
251 }
static const int kDoubleChunkSize
Definition: bignum.h:78
uint64_t DoubleChunk
Definition: bignum.h:75

References bigits_, v8::internal::carry, DCHECK, EnsureCapacity(), kBigitMask, kBigitSize, kDoubleChunkSize, used_digits_, and Zero().

Referenced by AssignPowerUInt16(), MultiplyByPowerOfTen(), SubtractTimes(), and Times10().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MultiplyByUInt64()

void v8::internal::Bignum::MultiplyByUInt64 ( uint64_t  factor)

Definition at line 254 of file bignum.cc.

254  {
255  if (factor == 1) return;
256  if (factor == 0) {
257  Zero();
258  return;
259  }
260  DCHECK(kBigitSize < 32);
261  uint64_t carry = 0;
262  uint64_t low = factor & 0xFFFFFFFF;
263  uint64_t high = factor >> 32;
264  for (int i = 0; i < used_digits_; ++i) {
265  uint64_t product_low = low * bigits_[i];
266  uint64_t product_high = high * bigits_[i];
267  uint64_t tmp = (carry & kBigitMask) + product_low;
268  bigits_[i] = static_cast<Chunk>(tmp & kBigitMask);
269  carry = (carry >> kBigitSize) + (tmp >> kBigitSize) +
270  (product_high << (32 - kBigitSize));
271  }
272  while (carry != 0) {
274  bigits_[used_digits_] = static_cast<Chunk>(carry & kBigitMask);
275  used_digits_++;
276  carry >>= kBigitSize;
277  }
278 }

References bigits_, v8::internal::carry, DCHECK, EnsureCapacity(), kBigitMask, kBigitSize, used_digits_, and Zero().

Referenced by v8::internal::InitialScaledStartValuesNegativeExponentNegativePower(), and MultiplyByPowerOfTen().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ PlusCompare()

int v8::internal::Bignum::PlusCompare ( const Bignum a,
const Bignum b,
const Bignum c 
)
static

Definition at line 614 of file bignum.cc.

614  {
615  DCHECK(a.IsClamped());
616  DCHECK(b.IsClamped());
617  DCHECK(c.IsClamped());
618  if (a.BigitLength() < b.BigitLength()) {
619  return PlusCompare(b, a, c);
620  }
621  if (a.BigitLength() + 1 < c.BigitLength()) return -1;
622  if (a.BigitLength() > c.BigitLength()) return +1;
623  // The exponent encodes 0-bigits. So if there are more 0-digits in 'a' than
624  // 'b' has digits, then the bigit-length of 'a'+'b' must be equal to the one
625  // of 'a'.
626  if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) {
627  return -1;
628  }
629 
630  Chunk borrow = 0;
631  // Starting at min_exponent all digits are == 0. So no need to compare them.
632  int min_exponent = Min(Min(a.exponent_, b.exponent_), c.exponent_);
633  for (int i = c.BigitLength() - 1; i >= min_exponent; --i) {
634  Chunk chunk_a = a.BigitAt(i);
635  Chunk chunk_b = b.BigitAt(i);
636  Chunk chunk_c = c.BigitAt(i);
637  Chunk sum = chunk_a + chunk_b;
638  if (sum > chunk_c + borrow) {
639  return +1;
640  } else {
641  borrow = chunk_c + borrow - sum;
642  if (borrow > 1) return -1;
643  borrow <<= kBigitSize;
644  }
645  }
646  if (borrow == 0) return 0;
647  return -1;
648 }
static int PlusCompare(const Bignum &a, const Bignum &b, const Bignum &c)
Definition: bignum.cc:614

References BigitAt(), BigitLength(), DCHECK, exponent_, IsClamped(), kBigitSize, and v8::internal::Min().

Referenced by v8::internal::BignumToFixed(), v8::internal::FixupMultiply10(), v8::internal::GenerateCountedDigits(), v8::internal::GenerateShortestDigits(), PlusEqual(), PlusLess(), and PlusLessEqual().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ PlusEqual()

static bool v8::internal::Bignum::PlusEqual ( const Bignum a,
const Bignum b,
const Bignum c 
)
inlinestatic

Definition at line 61 of file bignum.h.

61  {
62  return PlusCompare(a, b, c) == 0;
63  }

References PlusCompare().

+ Here is the call graph for this function:

◆ PlusLess()

static bool v8::internal::Bignum::PlusLess ( const Bignum a,
const Bignum b,
const Bignum c 
)
inlinestatic

Definition at line 69 of file bignum.h.

69  {
70  return PlusCompare(a, b, c) < 0;
71  }

References PlusCompare().

+ Here is the call graph for this function:

◆ PlusLessEqual()

static bool v8::internal::Bignum::PlusLessEqual ( const Bignum a,
const Bignum b,
const Bignum c 
)
inlinestatic

Definition at line 65 of file bignum.h.

65  {
66  return PlusCompare(a, b, c) <= 0;
67  }

References PlusCompare().

+ Here is the call graph for this function:

◆ ShiftLeft()

void v8::internal::Bignum::ShiftLeft ( int  shift_amount)

Definition at line 219 of file bignum.cc.

219  {
220  if (used_digits_ == 0) return;
221  exponent_ += shift_amount / kBigitSize;
222  int local_shift = shift_amount % kBigitSize;
224  BigitsShiftLeft(local_shift);
225 }
void BigitsShiftLeft(int shift_amount)
Definition: bignum.cc:700

References BigitsShiftLeft(), EnsureCapacity(), exponent_, kBigitSize, and used_digits_.

Referenced by AssignPowerUInt16(), v8::internal::BignumStrtod(), v8::internal::InitialScaledStartValuesNegativeExponentNegativePower(), v8::internal::InitialScaledStartValuesNegativeExponentPositivePower(), v8::internal::InitialScaledStartValuesPositiveExponent(), and MultiplyByPowerOfTen().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Square()

void v8::internal::Bignum::Square ( )

Definition at line 321 of file bignum.cc.

321  {
322  DCHECK(IsClamped());
323  int product_length = 2 * used_digits_;
324  EnsureCapacity(product_length);
325 
326  // Comba multiplication: compute each column separately.
327  // Example: r = a2a1a0 * b2b1b0.
328  // r = 1 * a0b0 +
329  // 10 * (a1b0 + a0b1) +
330  // 100 * (a2b0 + a1b1 + a0b2) +
331  // 1000 * (a2b1 + a1b2) +
332  // 10000 * a2b2
333  //
334  // In the worst case we have to accumulate nb-digits products of digit*digit.
335  //
336  // Assert that the additional number of bits in a DoubleChunk are enough to
337  // sum up used_digits of Bigit*Bigit.
338  if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_digits_) {
339  UNIMPLEMENTED();
340  }
341  DoubleChunk accumulator = 0;
342  // First shift the digits so we don't overwrite them.
343  int copy_offset = used_digits_;
344  for (int i = 0; i < used_digits_; ++i) {
345  bigits_[copy_offset + i] = bigits_[i];
346  }
347  // We have two loops to avoid some 'if's in the loop.
348  for (int i = 0; i < used_digits_; ++i) {
349  // Process temporary digit i with power i.
350  // The sum of the two indices must be equal to i.
351  int bigit_index1 = i;
352  int bigit_index2 = 0;
353  // Sum all of the sub-products.
354  while (bigit_index1 >= 0) {
355  Chunk chunk1 = bigits_[copy_offset + bigit_index1];
356  Chunk chunk2 = bigits_[copy_offset + bigit_index2];
357  accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
358  bigit_index1--;
359  bigit_index2++;
360  }
361  bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask;
362  accumulator >>= kBigitSize;
363  }
364  for (int i = used_digits_; i < product_length; ++i) {
365  int bigit_index1 = used_digits_ - 1;
366  int bigit_index2 = i - bigit_index1;
367  // Invariant: sum of both indices is again equal to i.
368  // Inner loop runs 0 times on last iteration, emptying accumulator.
369  while (bigit_index2 < used_digits_) {
370  Chunk chunk1 = bigits_[copy_offset + bigit_index1];
371  Chunk chunk2 = bigits_[copy_offset + bigit_index2];
372  accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
373  bigit_index1--;
374  bigit_index2++;
375  }
376  // The overwritten bigits_[i] will never be read in further loop iterations,
377  // because bigit_index1 and bigit_index2 are always greater
378  // than i - used_digits_.
379  bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask;
380  accumulator >>= kBigitSize;
381  }
382  // Since the result was guaranteed to lie inside the number the
383  // accumulator must be 0 now.
384  DCHECK(accumulator == 0);
385 
386  // Don't forget to update the used_digits and the exponent.
387  used_digits_ = product_length;
388  exponent_ *= 2;
389  Clamp();
390 }
static const int kChunkSize
Definition: bignum.h:77
#define UNIMPLEMENTED()
Definition: logging.h:28

References bigits_, Clamp(), DCHECK, EnsureCapacity(), exponent_, IsClamped(), kBigitMask, kBigitSize, kChunkSize, UNIMPLEMENTED, and used_digits_.

Referenced by AssignPowerUInt16().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SubtractBignum()

void v8::internal::Bignum::SubtractBignum ( const Bignum other)

Definition at line 192 of file bignum.cc.

192  {
193  DCHECK(IsClamped());
194  DCHECK(other.IsClamped());
195  // We require this to be bigger than other.
196  DCHECK(LessEqual(other, *this));
197 
198  Align(other);
199 
200  int offset = other.exponent_ - exponent_;
201  Chunk borrow = 0;
202  int i;
203  for (i = 0; i < other.used_digits_; ++i) {
204  DCHECK((borrow == 0) || (borrow == 1));
205  Chunk difference = bigits_[i + offset] - other.bigits_[i] - borrow;
206  bigits_[i + offset] = difference & kBigitMask;
207  borrow = difference >> (kChunkSize - 1);
208  }
209  while (borrow != 0) {
210  Chunk difference = bigits_[i + offset] - borrow;
211  bigits_[i + offset] = difference & kBigitMask;
212  borrow = difference >> (kChunkSize - 1);
213  ++i;
214  }
215  Clamp();
216 }

References Align(), bigits_, Clamp(), DCHECK, exponent_, IsClamped(), kBigitMask, kChunkSize, LessEqual(), and used_digits_.

Referenced by DivideModuloIntBignum(), and SubtractTimes().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SubtractTimes()

void v8::internal::Bignum::SubtractTimes ( const Bignum other,
int  factor 
)
private

Definition at line 716 of file bignum.cc.

716  {
717 #ifdef DEBUG
718  Bignum a, b;
719  a.AssignBignum(*this);
720  b.AssignBignum(other);
721  b.MultiplyByUInt32(factor);
722  a.SubtractBignum(b);
723 #endif
724  DCHECK(exponent_ <= other.exponent_);
725  if (factor < 3) {
726  for (int i = 0; i < factor; ++i) {
727  SubtractBignum(other);
728  }
729  return;
730  }
731  Chunk borrow = 0;
732  int exponent_diff = other.exponent_ - exponent_;
733  for (int i = 0; i < other.used_digits_; ++i) {
734  DoubleChunk product = static_cast<DoubleChunk>(factor) * other.bigits_[i];
735  DoubleChunk remove = borrow + product;
736  Chunk difference =
737  bigits_[i + exponent_diff] - static_cast<Chunk>(remove & kBigitMask);
738  bigits_[i + exponent_diff] = difference & kBigitMask;
739  borrow = static_cast<Chunk>((difference >> (kChunkSize - 1)) +
740  (remove >> kBigitSize));
741  }
742  for (int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) {
743  if (borrow == 0) return;
744  Chunk difference = bigits_[i] - borrow;
745  bigits_[i] = difference & kBigitMask;
746  borrow = difference >> (kChunkSize - 1);
747  }
748  Clamp();
749  DCHECK(Bignum::Equal(a, *this));
750 }
static bool Equal(const Bignum &a, const Bignum &b)
Definition: bignum.h:49

References AssignBignum(), bigits_, Clamp(), DCHECK, Equal(), exponent_, kBigitMask, kBigitSize, kChunkSize, MultiplyByUInt32(), SubtractBignum(), and used_digits_.

Referenced by DivideModuloIntBignum().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Times10()

void v8::internal::Bignum::Times10 ( )
inline

Definition at line 39 of file bignum.h.

39 { return MultiplyByUInt32(10); }

References MultiplyByUInt32().

Referenced by v8::internal::BignumToFixed(), v8::internal::FixupMultiply10(), v8::internal::GenerateCountedDigits(), and v8::internal::GenerateShortestDigits().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ToHexString()

bool v8::internal::Bignum::ToHexString ( char *  buffer,
int  buffer_size 
) const

Definition at line 549 of file bignum.cc.

549  {
550  DCHECK(IsClamped());
551  // Each bigit must be printable as separate hex-character.
552  DCHECK(kBigitSize % 4 == 0);
553  const int kHexCharsPerBigit = kBigitSize / 4;
554 
555  if (used_digits_ == 0) {
556  if (buffer_size < 2) return false;
557  buffer[0] = '0';
558  buffer[1] = '\0';
559  return true;
560  }
561  // We add 1 for the terminating '\0' character.
562  int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit +
564  if (needed_chars > buffer_size) return false;
565  int string_index = needed_chars - 1;
566  buffer[string_index--] = '\0';
567  for (int i = 0; i < exponent_; ++i) {
568  for (int j = 0; j < kHexCharsPerBigit; ++j) {
569  buffer[string_index--] = '0';
570  }
571  }
572  for (int i = 0; i < used_digits_ - 1; ++i) {
573  Chunk current_bigit = bigits_[i];
574  for (int j = 0; j < kHexCharsPerBigit; ++j) {
575  buffer[string_index--] = HexCharOfValue(current_bigit & 0xF);
576  current_bigit >>= 4;
577  }
578  }
579  // And finally the last bigit.
580  Chunk most_significant_bigit = bigits_[used_digits_ - 1];
581  while (most_significant_bigit != 0) {
582  buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF);
583  most_significant_bigit >>= 4;
584  }
585  return true;
586 }
static int SizeInHexChars(S number)
Definition: bignum.cc:531
static char HexCharOfValue(int value)
Definition: bignum.cc:542

References BigitLength(), bigits_, DCHECK, exponent_, v8::internal::HexCharOfValue(), IsClamped(), kBigitSize, v8::internal::SizeInHexChars(), and used_digits_.

+ Here is the call graph for this function:

◆ Zero()

void v8::internal::Bignum::Zero ( )
private

Definition at line 667 of file bignum.cc.

667  {
668  for (int i = 0; i < used_digits_; ++i) {
669  bigits_[i] = 0;
670  }
671  used_digits_ = 0;
672  exponent_ = 0;
673 }

References bigits_, exponent_, and used_digits_.

Referenced by AssignDecimalString(), AssignHexString(), AssignPowerUInt16(), AssignUInt16(), AssignUInt64(), MultiplyByUInt32(), and MultiplyByUInt64().

+ Here is the caller graph for this function:

Member Data Documentation

◆ bigits_

◆ bigits_buffer_

Chunk v8::internal::Bignum::bigits_buffer_[kBigitCapacity]
private

Definition at line 105 of file bignum.h.

◆ exponent_

int v8::internal::Bignum::exponent_
private

◆ kBigitCapacity

const int v8::internal::Bignum::kBigitCapacity = kMaxSignificantBits / kBigitSize
staticprivate

Definition at line 85 of file bignum.h.

Referenced by Bignum(), and EnsureCapacity().

◆ kBigitMask

const Chunk v8::internal::Bignum::kBigitMask = (1 << kBigitSize) - 1
staticprivate

◆ kBigitSize

◆ kChunkSize

const int v8::internal::Bignum::kChunkSize = sizeof(Chunk) * 8
staticprivate

Definition at line 77 of file bignum.h.

Referenced by Square(), SubtractBignum(), and SubtractTimes().

◆ kDoubleChunkSize

const int v8::internal::Bignum::kDoubleChunkSize = sizeof(DoubleChunk) * 8
staticprivate

Definition at line 78 of file bignum.h.

Referenced by MultiplyByUInt32().

◆ kMaxSignificantBits

const int v8::internal::Bignum::kMaxSignificantBits = 3584
static

Definition at line 16 of file bignum.h.

Referenced by v8::internal::BignumDtoa(), and v8::internal::BignumStrtod().

◆ used_digits_


The documentation for this class was generated from the following files: