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 "xrpl::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 "xrpl::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 "xrpl::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,
"xrpl::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,
"xrpl::to_string(Number) : first distance check");
580 pre_from =
std::find_if(pre_from, pre_to, [](
char c) {
return c !=
'0'; });
585 post_to -= pad_suffix;
588 post_to >= post_from,
589 "xrpl::to_string(Number) : second distance check");
594 [](
char c) {
return c !=
'0'; })
603 if (pre_from == pre_to)
606 ret.
append(pre_from, pre_to);
608 if (post_to != post_from)
611 ret.
append(post_from, post_to);
627 auto r =
power(f, n / 2);
646 if (f ==
one || d == 1)
656 if (f <
Number{} && d % 2 == 0)
663 auto const di =
static_cast<int>(d);
664 auto ex = [e = e, di = di]()
666 int k = (e >= 0 ? e : e - (di - 1)) / di;
682 auto const D = ((6 * di + 11) * di + 6) * di + 1;
683 auto const a0 = 3 * di * ((2 * di - 3) * di + 1);
684 auto const a1 = 24 * di * (2 * di - 1);
685 auto const a2 = -30 * (di - 1) * di;
702 }
while (r != rm1 && r != rm2);
739 r = (r + f / r) /
Number(2);
740 }
while (r != rm1 && r != rm2);
769 if ((n % 2) == 1 && (d % 2) == 0 && f <
Number{})
void set_positive() noexcept
void push(unsigned d) noexcept
void doRoundDown(rep &mantissa, int &exponent)
bool is_negative() const noexcept
void set_negative() noexcept
void doRoundUp(rep &mantissa, int &exponent, std::string location)
static rounding_mode getround()
constexpr rep mantissa() const noexcept
static rounding_mode setround(rounding_mode mode)
Number & operator/=(Number const &x)
constexpr bool isnormal() const noexcept
Number & operator+=(Number const &x)
Number truncate() const noexcept
static thread_local rounding_mode mode_
static constexpr Number max() noexcept
static constexpr int minExponent
static constexpr std::int64_t maxMantissa
constexpr int exponent() const noexcept
static constexpr int maxExponent
static constexpr std::int64_t minMantissa
Number & operator*=(Number const &x)
constexpr Number()=default
T make_reverse_iterator(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string to_string(base_uint< Bits, Tag > const &a)
Number root(Number f, unsigned d)
Number power(Number const &f, unsigned n)
constexpr Number abs(Number x) noexcept
static unsigned divu10(uint128_t &u)