1#include <xrpl/basics/Number.h>
2#include <xrpl/beast/utility/instrumentation.h>
16#pragma message("Using boost::multiprecision::uint128_t")
17#include <boost/multiprecision/cpp_int.hpp>
18using uint128_t = boost::multiprecision::uint128_t;
20using uint128_t = __uint128_t;
66 push(
unsigned d) noexcept;
114 digits_ |= (d & 0x0000'0000'0000'000FULL) << 60;
120 unsigned d = (
digits_ & 0xF000'0000'0000'0000) >> 60;
157 if (
digits_ > 0x5000'0000'0000'0000)
159 if (
digits_ < 0x5000'0000'0000'0000)
170 if (r == 1 || (r == 0 && (
mantissa & 1) == 1))
192 if (r == 1 || (r == 0 && (
mantissa & 1) == 1))
213 if (r == 1 || (r == 0 && (drops & 1) == 1))
283 "ripple::Number::operator+=(Number) : is normal");
332 g.
doRoundUp(xm, xe,
"Number::addition overflow");
365static inline unsigned
369 auto q = (u >> 1) + (u >> 2);
379 auto r =
static_cast<unsigned>(u - ((q << 3) + (q << 1)));
381 auto c = (r + 6) >> 4;
399 "ripple::Number::operator*=(Number) : is normal");
416 auto zm = uint128_t(xm) * uint128_t(ym);
430 xm =
static_cast<rep>(zm);
435 "Number::multiplication overflow : exponent is " +
std::to_string(xe));
440 "ripple::Number::operator*=(Number) : result is normal");
469 uint128_t
const f = 100'000'000'000'000'000;
477Number::operator
rep()
const
489 for (; offset < 0; ++offset)
494 for (; offset > 0; --offset)
530 auto const exponent = amount.exponent();
542 bool negative =
false;
551 exponent + 43 > 0,
"ripple::to_string(Number) : minimum exponent");
553 ptrdiff_t
const pad_prefix = 27;
554 ptrdiff_t
const pad_suffix = 23;
560 val.
append(pad_prefix,
'0');
562 val.
append(pad_suffix,
'0');
564 ptrdiff_t
const offset(
exponent + 43);
566 auto pre_from(val.
begin());
567 auto const pre_to(val.
begin() + offset);
569 auto const post_from(val.
begin() + offset);
570 auto post_to(val.
end());
575 pre_from += pad_prefix;
578 post_to >= post_from,
579 "ripple::to_string(Number) : first distance check");
581 pre_from =
std::find_if(pre_from, pre_to, [](
char c) {
return c !=
'0'; });
586 post_to -= pad_suffix;
589 post_to >= post_from,
590 "ripple::to_string(Number) : second distance check");
595 [](
char c) {
return c !=
'0'; })
604 if (pre_from == pre_to)
607 ret.
append(pre_from, pre_to);
609 if (post_to != post_from)
612 ret.
append(post_from, post_to);
628 auto r =
power(f, n / 2);
647 if (f ==
one || d == 1)
657 if (f <
Number{} && d % 2 == 0)
664 auto const di =
static_cast<int>(d);
665 auto ex = [e = e, di = di]()
667 int k = (e >= 0 ? e : e - (di - 1)) / di;
683 auto const D = ((6 * di + 11) * di + 6) * di + 1;
684 auto const a0 = 3 * di * ((2 * di - 3) * di + 1);
685 auto const a1 = 24 * di * (2 * di - 1);
686 auto const a2 = -30 * (di - 1) * di;
703 }
while (r != rm1 && r != rm2);
740 r = (r + f / r) /
Number(2);
741 }
while (r != rm1 && r != rm2);
770 if ((n % 2) == 1 && (d % 2) == 0 && f <
Number{})
void set_negative() noexcept
void doRoundDown(rep &mantissa, int &exponent)
void doRoundUp(rep &mantissa, int &exponent, std::string location)
void push(unsigned d) noexcept
bool is_negative() const noexcept
void set_positive() noexcept
constexpr bool isnormal() const noexcept
Number & operator+=(Number const &x)
static constexpr std::int64_t maxMantissa
static constexpr int maxExponent
Number & operator/=(Number const &x)
Number truncate() const noexcept
static constexpr std::int64_t minMantissa
constexpr int exponent() const noexcept
static thread_local rounding_mode mode_
static constexpr Number max() noexcept
static rounding_mode getround()
Number & operator*=(Number const &x)
static constexpr int minExponent
static rounding_mode setround(rounding_mode mode)
constexpr rep mantissa() const noexcept
constexpr Number()=default
T make_reverse_iterator(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static unsigned divu10(uint128_t &u)
Number power(Number const &f, unsigned n)
std::string to_string(base_uint< Bits, Tag > const &a)
Number root(Number f, unsigned d)
constexpr Number abs(Number x) noexcept