Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
internal::bigint Class Reference
Collaboration diagram for internal::bigint:

Public Member Functions

 bigint (uint64_t n)
 bigint (const bigint &)=delete
void operator= (const bigint &)=delete
void assign (const bigint &other)
void assign (uint64_t n)
int num_bigits () const
bigint & operator<<= (int shift)
template<typename Int>
bigint & operator*= (Int value)
void assign_pow10 (int exp)
void square ()
int divmod_assign (const bigint &divisor)

Private Types

enum  { bigits_capacity = 32 }
using bigit = uint32_t
using double_bigit = uint64_t

Private Member Functions

void subtract_bigits (int index, bigit other, bigit &borrow)
void remove_leading_zeros ()
void subtract_aligned (const bigint &other)
void multiply (uint32_t value)
void multiply (uint64_t value)

Private Attributes

basic_memory_buffer< bigit, bigits_capacity > bigits_
int exp_

Static Private Attributes

static FMT_CONSTEXPR_DECL const int bigit_bits = bits<bigit>::value

Friends

struct formatter< bigint >
int compare (const bigint &lhs, const bigint &rhs)
int add_compare (const bigint &lhs1, const bigint &lhs2, const bigint &rhs)

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private
449381{ bigits_capacity = 32 };

Constructor & Destructor Documentation

◆ bigint() [1/2]

internal::bigint::bigint ( )
inline
449444: exp_(0) {}

◆ bigint() [2/2]

internal::bigint::bigint ( uint64_t n)
inlineexplicit
449445{ assign(n); }

◆ ~bigint()

internal::bigint::~bigint ( )
inline
449446{ assert(bigits_.capacity() <= bigits_capacity); }
#define assert(x)
Definition duckdb.cpp:486071

Member Function Documentation

◆ subtract_bigits()

void internal::bigint::subtract_bigits ( int index,
bigit other,
bigit & borrow )
inlineprivate
449389 {
449390 auto result = static_cast<double_bigit>(bigits_[index]) - other - borrow;
449391 bigits_[index] = static_cast<bigit>(result);
449392 borrow = static_cast<bigit>(result >> (bigit_bits * 2 - 1));
449393 }
index

◆ remove_leading_zeros()

void internal::bigint::remove_leading_zeros ( )
inlineprivate
449395 {
449396 int num_bigits = static_cast<int>(bigits_.size()) - 1;
449397 while (num_bigits > 0 && bigits_[num_bigits] == 0) --num_bigits;
449398 bigits_.resize(num_bigits + 1);
449399 }

◆ subtract_aligned()

void internal::bigint::subtract_aligned ( const bigint & other)
inlineprivate
449402 {
449403 FMT_ASSERT(other.exp_ >= exp_, "unaligned bigints");
449404 FMT_ASSERT(compare(*this, other) >= 0, "");
449405 bigit borrow = 0;
449406 int i = other.exp_ - exp_;
449407 for (int j = 0, n = static_cast<int>(other.bigits_.size()); j != n;
449408 ++i, ++j) {
449409 subtract_bigits(i, other.bigits_[j], borrow);
449410 }
449411 while (borrow > 0) subtract_bigits(i, 0, borrow);
449412 remove_leading_zeros();
449413 }

◆ multiply() [1/2]

void internal::bigint::multiply ( uint32_t value)
inlineprivate
449415 {
449416 const double_bigit wide_value = value;
449417 bigit carry = 0;
449418 for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
449419 double_bigit result = bigits_[i] * wide_value + carry;
449420 bigits_[i] = static_cast<bigit>(result);
449421 carry = static_cast<bigit>(result >> bigit_bits);
449422 }
449423 if (carry != 0) bigits_.push_back(carry);
449424 }

◆ multiply() [2/2]

void internal::bigint::multiply ( uint64_t value)
inlineprivate
449426 {
449427 const bigit mask = ~bigit(0);
449428 const double_bigit lower = value & mask;
449429 const double_bigit upper = value >> bigit_bits;
449430 double_bigit carry = 0;
449431 for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
449432 double_bigit result = bigits_[i] * lower + (carry & mask);
449433 carry =
449434 bigits_[i] * upper + (result >> bigit_bits) + (carry >> bigit_bits);
449435 bigits_[i] = static_cast<bigit>(result);
449436 }
449437 while (carry != 0) {
449438 bigits_.push_back(carry & mask);
449439 carry >>= bigit_bits;
449440 }
449441 }
GMat mask(const GMat &src, const GMat &mask)

◆ assign() [1/2]

void internal::bigint::assign ( const bigint & other)
inline
449451 {
449452 bigits_.resize(other.bigits_.size());
449453 auto data = other.bigits_.data();
449454 std::copy(data, data + other.bigits_.size(), bigits_.data());
449455 exp_ = other.exp_;
449456 }

◆ assign() [2/2]

void internal::bigint::assign ( uint64_t n)
inline
449458 {
449459 int num_bigits = 0;
449460 do {
449461 bigits_[num_bigits++] = n & ~bigit(0);
449462 n >>= bigit_bits;
449463 } while (n != 0);
449464 bigits_.resize(num_bigits);
449465 exp_ = 0;
449466 }

◆ num_bigits()

int internal::bigint::num_bigits ( ) const
inline
449468{ return static_cast<int>(bigits_.size()) + exp_; }

◆ operator<<=()

bigint & internal::bigint::operator<<= ( int shift)
inline
449470 {
449471 assert(shift >= 0);
449472 exp_ += shift / bigit_bits;
449473 shift %= bigit_bits;
449474 if (shift == 0) return *this;
449475 bigit carry = 0;
449476 for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
449477 bigit c = bigits_[i] >> (bigit_bits - shift);
449478 bigits_[i] = (bigits_[i] << shift) + carry;
449479 carry = c;
449480 }
449481 if (carry != 0) bigits_.push_back(carry);
449482 return *this;
449483 }

◆ operator*=()

template<typename Int>
bigint & internal::bigint::operator*= ( Int value)
inline
449485 {
449486 FMT_ASSERT(value > 0, "");
449487 multiply(uint32_or_64_or_128_t<Int>(value));
449488 return *this;
449489 }

◆ assign_pow10()

void internal::bigint::assign_pow10 ( int exp)
inline
449532 {
449533 assert(exp >= 0);
449534 if (exp == 0) return assign(1);
449535 // Find the top bit.
449536 int bitmask = 1;
449537 while (exp >= bitmask) bitmask <<= 1;
449538 bitmask >>= 1;
449539 // pow(10, exp) = pow(5, exp) * pow(2, exp). First compute pow(5, exp) by
449540 // repeated squaring and multiplication.
449541 assign(5);
449542 bitmask >>= 1;
449543 while (bitmask != 0) {
449544 square();
449545 if ((exp & bitmask) != 0) *this *= 5;
449546 bitmask >>= 1;
449547 }
449548 *this <<= exp; // Multiply by pow(2, exp) by shifting.
449549 }
void exp(InputArray src, OutputArray dst)

◆ square()

void internal::bigint::square ( )
inline
449551 {
449552 basic_memory_buffer<bigit, bigits_capacity> n(std::move(bigits_));
449553 int num_bigits = static_cast<int>(bigits_.size());
449554 int num_result_bigits = 2 * num_bigits;
449555 bigits_.resize(num_result_bigits);
449556#if FMT_USE_INT128
449557 using accumulator_t = __uint128_t;
449558#else
449559 using accumulator_t = accumulator;
449560#endif
449561 auto sum = accumulator_t();
449562 for (int bigit_index = 0; bigit_index < num_bigits; ++bigit_index) {
449563 // Compute bigit at position bigit_index of the result by adding
449564 // cross-product terms n[i] * n[j] such that i + j == bigit_index.
449565 for (int i = 0, j = bigit_index; j >= 0; ++i, --j) {
449566 // Most terms are multiplied twice which can be optimized in the future.
449567 sum += static_cast<double_bigit>(n[i]) * n[j];
449568 }
449569 bigits_[bigit_index] = static_cast<bigit>(sum);
449570 sum >>= bits<bigit>::value; // Compute the carry.
449571 }
449572 // Do the same for the top half.
449573 for (int bigit_index = num_bigits; bigit_index < num_result_bigits;
449574 ++bigit_index) {
449575 for (int j = num_bigits - 1, i = bigit_index - j; i < num_bigits;)
449576 sum += static_cast<double_bigit>(n[i++]) * n[j--];
449577 bigits_[bigit_index] = static_cast<bigit>(sum);
449578 sum >>= bits<bigit>::value;
449579 }
449580 --num_result_bigits;
449581 remove_leading_zeros();
449582 exp_ *= 2;
449583 }
Scalar sum(InputArray src)

◆ divmod_assign()

int internal::bigint::divmod_assign ( const bigint & divisor)
inline
449587 {
449588 FMT_ASSERT(this != &divisor, "");
449589 if (compare(*this, divisor) < 0) return 0;
449590 int num_bigits = static_cast<int>(bigits_.size());
449591 FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1] != 0, "");
449592 int exp_difference = exp_ - divisor.exp_;
449593 if (exp_difference > 0) {
449594 // Align bigints by adding trailing zeros to simplify subtraction.
449595 bigits_.resize(num_bigits + exp_difference);
449596 for (int i = num_bigits - 1, j = i + exp_difference; i >= 0; --i, --j)
449597 bigits_[j] = bigits_[i];
449598 std::uninitialized_fill_n(bigits_.data(), exp_difference, 0);
449599 exp_ -= exp_difference;
449600 }
449601 int quotient = 0;
449602 do {
449603 subtract_aligned(divisor);
449604 ++quotient;
449605 } while (compare(*this, divisor) >= 0);
449606 return quotient;
449607 }

◆ compare

int compare ( const bigint & lhs,
const bigint & rhs )
friend
449491 {
449492 int num_lhs_bigits = lhs.num_bigits(), num_rhs_bigits = rhs.num_bigits();
449493 if (num_lhs_bigits != num_rhs_bigits)
449494 return num_lhs_bigits > num_rhs_bigits ? 1 : -1;
449495 int i = static_cast<int>(lhs.bigits_.size()) - 1;
449496 int j = static_cast<int>(rhs.bigits_.size()) - 1;
449497 int end = i - j;
449498 if (end < 0) end = 0;
449499 for (; i >= end; --i, --j) {
449500 bigit lhs_bigit = lhs.bigits_[i], rhs_bigit = rhs.bigits_[j];
449501 if (lhs_bigit != rhs_bigit) return lhs_bigit > rhs_bigit ? 1 : -1;
449502 }
449503 if (i != j) return i > j ? 1 : -1;
449504 return 0;
449505 }

◆ add_compare

int add_compare ( const bigint & lhs1,
const bigint & lhs2,
const bigint & rhs )
friend
449509 {
449510 int max_lhs_bigits = (std::max)(lhs1.num_bigits(), lhs2.num_bigits());
449511 int num_rhs_bigits = rhs.num_bigits();
449512 if (max_lhs_bigits + 1 < num_rhs_bigits) return -1;
449513 if (max_lhs_bigits > num_rhs_bigits) return 1;
449514 auto get_bigit = [](const bigint& n, int i) -> bigit {
449515 return i >= n.exp_ && i < n.num_bigits() ? n.bigits_[i - n.exp_] : 0;
449516 };
449517 double_bigit borrow = 0;
449518 int min_exp = (std::min)((std::min)(lhs1.exp_, lhs2.exp_), rhs.exp_);
449519 for (int i = num_rhs_bigits - 1; i >= min_exp; --i) {
449520 double_bigit sum =
449521 static_cast<double_bigit>(get_bigit(lhs1, i)) + get_bigit(lhs2, i);
449522 bigit rhs_bigit = get_bigit(rhs, i);
449523 if (sum > rhs_bigit + borrow) return 1;
449524 borrow = rhs_bigit + borrow - sum;
449525 if (borrow > 1) return -1;
449526 borrow <<= bigit_bits;
449527 }
449528 return borrow != 0 ? -1 : 0;
449529 }

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