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
duckdb::NumericHelper Class Reference

NumericHelper is a static class that holds helper functions for integers/doubles. More...

Public Member Functions

template<>
int UnsignedLength (uint8_t value)
template<>
int UnsignedLength (uint16_t value)
template<>
int UnsignedLength (uint32_t value)
template<>
int UnsignedLength (uint64_t value)
template<>
int UnsignedLength (hugeint_t value)
template<>
char * FormatUnsigned (hugeint_t value, char *ptr)
template<>
std::string ToString (hugeint_t value)
template<>
std::string ToString (uhugeint_t value)
template<>
string_t FormatSigned (hugeint_t value, Vector &vector)
template<>
int UnsignedLength (uint8_t value)
template<>
int UnsignedLength (uint16_t value)
template<>
int UnsignedLength (uint32_t value)
template<>
int UnsignedLength (uint64_t value)
template<>
int UnsignedLength (hugeint_t value)
template<>
string_t FormatSigned (hugeint_t value, Vector &vector)
template<>
std::string ToString (hugeint_t value)
template<>
std::string ToString (uhugeint_t value)
template<>
char * FormatUnsigned (hugeint_t value, char *ptr)

Static Public Member Functions

template<class T>
static int UnsignedLength (T value)
template<class SIGNED, class UNSIGNED>
static int SignedLength (SIGNED value)
template<class T>
static char * FormatUnsigned (T value, char *ptr)
template<class T>
static string_t FormatSigned (T value, Vector &vector)
template<class T>
static std::string ToString (T value)

Static Public Attributes

static constexpr uint8_t CACHED_POWERS_OF_TEN = 19
static const int64_t POWERS_OF_TEN [CACHED_POWERS_OF_TEN]
static const double DOUBLE_POWERS_OF_TEN [40]

Detailed Description

NumericHelper is a static class that holds helper functions for integers/doubles.

Member Function Documentation

◆ SignedLength()

template<class SIGNED, class UNSIGNED>
int duckdb::NumericHelper::SignedLength ( SIGNED value)
inlinestatic
19721 {
19722 int sign = -(value < 0);
19723 UNSIGNED unsigned_value = UnsafeNumericCast<UNSIGNED>((value ^ sign) - sign);
19724 return UnsignedLength(unsigned_value) - sign;
19725 }

◆ FormatUnsigned() [1/2]

template<class T>
char * duckdb::NumericHelper::FormatUnsigned ( T value,
char * ptr )
inlinestatic
19729 {
19730 while (value >= 100) {
19731 // Integer division is slow so do it for a group of two digits instead
19732 // of for every digit. The idea comes from the talk by Alexandrescu
19733 // "Three Optimization Tips for C++".
19734 auto index = NumericCast<unsigned>((value % 100) * 2);
19735 value /= 100;
19736 *--ptr = duckdb_fmt::internal::data::digits[index + 1];
19737 *--ptr = duckdb_fmt::internal::data::digits[index];
19738 }
19739 if (value < 10) {
19740 *--ptr = NumericCast<char>('0' + value);
19741 return ptr;
19742 }
19743 auto index = NumericCast<unsigned>(value * 2);
19744 *--ptr = duckdb_fmt::internal::data::digits[index + 1];
19745 *--ptr = duckdb_fmt::internal::data::digits[index];
19746 return ptr;
19747 }
index

◆ FormatSigned() [1/2]

template<class T>
string_t duckdb::NumericHelper::FormatSigned ( T value,
Vector & vector )
inlinestatic
19750 {
19751 typedef typename MakeUnsigned<T>::type unsigned_t;
19752 int8_t sign = -(value < 0);
19753 unsigned_t unsigned_value = unsigned_t(value ^ T(sign)) + unsigned_t(AbsValue(sign));
19754 auto length = UnsafeNumericCast<idx_t>(UnsignedLength<unsigned_t>(unsigned_value) + AbsValue(sign));
19755 string_t result = StringVector::EmptyString(vector, length);
19756 auto dataptr = result.GetDataWriteable();
19757 auto endptr = dataptr + length;
19758 endptr = FormatUnsigned(unsigned_value, endptr);
19759 if (sign) {
19760 *--endptr = '-';
19761 }
19762 result.Finalize();
19763 return result;
19764 }
::int8_t int8_t
static DUCKDB_API string_t EmptyString(Vector &vector, idx_t len)
Definition duckdb.cpp:137942

◆ ToString() [1/3]

template<class T>
std::string duckdb::NumericHelper::ToString ( T value)
inlinestatic
19767 {
19768 return std::to_string(value);
19769 }

◆ UnsignedLength() [1/5]

template<>
int duckdb::NumericHelper::UnsignedLength ( uint8_t value)
113436 {
113437 int length = 1;
113438 length += value >= 10;
113439 length += value >= 100;
113440 return length;
113441}

◆ UnsignedLength() [2/5]

template<>
int duckdb::NumericHelper::UnsignedLength ( uint16_t value)
113444 {
113445 int length = 1;
113446 length += value >= 10;
113447 length += value >= 100;
113448 length += value >= 1000;
113449 length += value >= 10000;
113450 return length;
113451}

◆ UnsignedLength() [3/5]

template<>
int duckdb::NumericHelper::UnsignedLength ( uint32_t value)
113454 {
113455 if (value >= 10000) {
113456 int length = 5;
113457 length += value >= 100000;
113458 length += value >= 1000000;
113459 length += value >= 10000000;
113460 length += value >= 100000000;
113461 length += value >= 1000000000;
113462 return length;
113463 } else {
113464 int length = 1;
113465 length += value >= 10;
113466 length += value >= 100;
113467 length += value >= 1000;
113468 return length;
113469 }
113470}

◆ UnsignedLength() [4/5]

template<>
int duckdb::NumericHelper::UnsignedLength ( uint64_t value)
113473 {
113474 if (value >= 10000000000ULL) {
113475 if (value >= 1000000000000000ULL) {
113476 int length = 16;
113477 length += value >= 10000000000000000ULL;
113478 length += value >= 100000000000000000ULL;
113479 length += value >= 1000000000000000000ULL;
113480 length += value >= 10000000000000000000ULL;
113481 return length;
113482 } else {
113483 int length = 11;
113484 length += value >= 100000000000ULL;
113485 length += value >= 1000000000000ULL;
113486 length += value >= 10000000000000ULL;
113487 length += value >= 100000000000000ULL;
113488 return length;
113489 }
113490 } else {
113491 if (value >= 100000ULL) {
113492 int length = 6;
113493 length += value >= 1000000ULL;
113494 length += value >= 10000000ULL;
113495 length += value >= 100000000ULL;
113496 length += value >= 1000000000ULL;
113497 return length;
113498 } else {
113499 int length = 1;
113500 length += value >= 10ULL;
113501 length += value >= 100ULL;
113502 length += value >= 1000ULL;
113503 length += value >= 10000ULL;
113504 return length;
113505 }
113506 }
113507}

◆ UnsignedLength() [5/5]

template<>
int duckdb::NumericHelper::UnsignedLength ( hugeint_t value)
113510 {
113511 D_ASSERT(value.upper >= 0);
113512 if (value.upper == 0) {
113513 return UnsignedLength<uint64_t>(value.lower);
113514 }
113515 // search the length using the POWERS_OF_TEN array
113516 // the length has to be between [17] and [38], because the hugeint is bigger than 2^63
113517 // we use the same approach as above, but split a bit more because comparisons for hugeints are more expensive
113518 if (value >= Hugeint::POWERS_OF_TEN[27]) {
113519 // [27..38]
113520 if (value >= Hugeint::POWERS_OF_TEN[32]) {
113521 if (value >= Hugeint::POWERS_OF_TEN[36]) {
113522 int length = 37;
113523 length += value >= Hugeint::POWERS_OF_TEN[37];
113524 length += value >= Hugeint::POWERS_OF_TEN[38];
113525 return length;
113526 } else {
113527 int length = 33;
113528 length += value >= Hugeint::POWERS_OF_TEN[33];
113529 length += value >= Hugeint::POWERS_OF_TEN[34];
113530 length += value >= Hugeint::POWERS_OF_TEN[35];
113531 return length;
113532 }
113533 } else {
113534 if (value >= Hugeint::POWERS_OF_TEN[30]) {
113535 int length = 31;
113536 length += value >= Hugeint::POWERS_OF_TEN[31];
113537 length += value >= Hugeint::POWERS_OF_TEN[32];
113538 return length;
113539 } else {
113540 int length = 28;
113541 length += value >= Hugeint::POWERS_OF_TEN[28];
113542 length += value >= Hugeint::POWERS_OF_TEN[29];
113543 return length;
113544 }
113545 }
113546 } else {
113547 // [17..27]
113548 if (value >= Hugeint::POWERS_OF_TEN[22]) {
113549 // [22..27]
113550 if (value >= Hugeint::POWERS_OF_TEN[25]) {
113551 int length = 26;
113552 length += value >= Hugeint::POWERS_OF_TEN[26];
113553 return length;
113554 } else {
113555 int length = 23;
113556 length += value >= Hugeint::POWERS_OF_TEN[23];
113557 length += value >= Hugeint::POWERS_OF_TEN[24];
113558 return length;
113559 }
113560 } else {
113561 // [17..22]
113562 if (value >= Hugeint::POWERS_OF_TEN[20]) {
113563 int length = 21;
113564 length += value >= Hugeint::POWERS_OF_TEN[21];
113565 return length;
113566 } else {
113567 int length = 18;
113568 length += value >= Hugeint::POWERS_OF_TEN[18];
113569 length += value >= Hugeint::POWERS_OF_TEN[19];
113570 return length;
113571 }
113572 }
113573 }
113574}

◆ FormatSigned() [2/2]

template<>
string_t duckdb::NumericHelper::FormatSigned ( hugeint_t value,
Vector & vector )
113577 {
113578 int negative = value.upper < 0;
113579 if (negative) {
113580 if (value == NumericLimits<hugeint_t>::Minimum()) {
113581 string_t result = StringVector::AddString(vector, Hugeint::HUGEINT_MINIMUM_STRING);
113582 return result;
113583 }
113584 Hugeint::NegateInPlace(value);
113585 }
113586 int length = UnsignedLength(value) + negative;
113587 string_t result = StringVector::EmptyString(vector, NumericCast<size_t>(length));
113588 auto dataptr = result.GetDataWriteable();
113589 auto endptr = dataptr + length;
113590 if (value.upper == 0) {
113591 // small value: format as uint64_t
113592 endptr = NumericHelper::FormatUnsigned<uint64_t>(value.lower, endptr);
113593 } else {
113594 endptr = FormatUnsigned(value, endptr);
113595 }
113596 if (negative) {
113597 *--endptr = '-';
113598 }
113599 D_ASSERT(endptr == dataptr);
113600 result.Finalize();
113601 return result;
113602}
static DUCKDB_API string_t AddString(Vector &vector, const char *data, idx_t len)
Add a string to the string heap of the vector (auxiliary data).
Definition duckdb.cpp:137889

◆ ToString() [2/3]

template<>
std::string duckdb::NumericHelper::ToString ( hugeint_t value)
113605 {
113606 return Hugeint::ToString(value);
113607}
static string ToString(hugeint_t input)
Convert a hugeint object to a string.
Definition duckdb.cpp:121920

◆ ToString() [3/3]

template<>
std::string duckdb::NumericHelper::ToString ( uhugeint_t value)
113610 {
113611 return Uhugeint::ToString(value);
113612}
static string ToString(uhugeint_t input)
Convert a uhugeint object to a string.
Definition duckdb.cpp:130182

◆ FormatUnsigned() [2/2]

template<>
char * duckdb::NumericHelper::FormatUnsigned ( hugeint_t value,
char * ptr )
113654 {
113655 while (value.upper > 0) {
113656 // while integer division is slow, hugeint division is MEGA slow
113657 // we want to avoid doing as many divisions as possible
113658 // for that reason we start off doing a division by a large power of ten that uint64_t can hold
113659 // (100000000000000000) - this is the third largest
113660 // the reason we don't use the largest is because that can result in an overflow inside the division
113661 // function
113662 uint64_t remainder;
113663 value = Hugeint::DivModPositive(value, 100000000000000000ULL, remainder);
113664
113665 auto startptr = ptr;
113666 // now we format the remainder: note that we need to pad with zero's in case
113667 // the remainder is small (i.e. less than 10000000000000000)
113668 ptr = NumericHelper::FormatUnsigned<uint64_t>(remainder, ptr);
113669
113670 int format_length = UnsafeNumericCast<int>(startptr - ptr);
113671 // pad with zero
113672 for (int i = format_length; i < 17; i++) {
113673 *--ptr = '0';
113674 }
113675 }
113676 // once the value falls in the range of a uint64_t, fallback to formatting as uint64_t to avoid hugeint division
113677 return NumericHelper::FormatUnsigned<uint64_t>(value.lower, ptr);
113678}
::uint64_t uint64_t

Member Data Documentation

◆ POWERS_OF_TEN

const int64_t duckdb::NumericHelper::POWERS_OF_TEN
static
Initial value:
{1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000,
10000000000000000,
100000000000000000,
1000000000000000000}

◆ DOUBLE_POWERS_OF_TEN

const double duckdb::NumericHelper::DOUBLE_POWERS_OF_TEN
static
Initial value:
{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39}

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