20 #include <ripple/basics/Number.h>
21 #include <boost/predef.h>
29 #ifdef BOOST_COMP_MSVC
30 #include <boost/multiprecision/cpp_int.hpp>
31 using uint128_t = boost::multiprecision::uint128_t;
32 #else // !defined(_MSVC_LANG)
33 using uint128_t = __uint128_t;
34 #endif // !defined(_MSVC_LANG)
79 push(
unsigned d) noexcept;
115 digits_ |= (d & 0x0000'0000
'0000'000
FULL) << 60;
121 unsigned d = (
digits_ & 0xF000
'0000'0000
'0000) >> 60;
127 // -1 if Guard is less than half
128 // 0 if Guard is exactly half
129 // 1 if Guard is greater than half
131 Number::Guard::round() noexcept
133 auto mode = Number::getround();
135 if (mode == towards_zero)
138 if (mode == downward)
142 if (digits_ > 0 || xbit_)
152 if (digits_ > 0 || xbit_)
157 // assume round to nearest if mode is not one of the predefined values
158 if (digits_ > 0x5000'0000
'0000'0000)
160 if (
digits_ < 0x5000
'0000'0000
'0000)
169 constexpr Number one{1000000000000000, -15, Number::unchecked{}};
179 bool const negative = (mantissa_ < 0);
180 auto m = static_cast<std::make_unsigned_t<rep>>(mantissa_);
183 while ((m < minMantissa) && (exponent_ > minExponent))
191 while (m > maxMantissa)
193 if (exponent_ >= maxExponent)
194 throw std::overflow_error("Number::normalize 1");
200 if ((exponent_ < minExponent) || (mantissa_ < minMantissa))
207 if (r == 1 || (r == 0 && (mantissa_ & 1) == 1))
210 if (mantissa_ > maxMantissa)
216 if (exponent_ > maxExponent)
217 throw std::overflow_error("Number::normalize 2");
220 mantissa_ = -mantissa_;
224 Number::operator+=(Number const& y)
228 if (*this == Number{})
238 assert(isnormal() && y.isnormal());
239 auto xm = mantissa();
240 auto xe = exponent();
247 auto ym = y.mantissa();
248 auto ye = y.exponent();
281 if (xm > maxMantissa)
288 if (r == 1 || (r == 0 && (xm & 1) == 1))
291 if (xm > maxMantissa)
297 if (xe > maxExponent)
298 throw std::overflow_error("Number::addition overflow");
312 while (xm < minMantissa)
319 if (r == 1 || (r == 0 && (xm & 1) == 1))
322 if (xm < minMantissa)
328 if (xe < minExponent)
331 xe = Number{}.exponent_;
339 // Optimization equivalent to:
340 // auto r = static_cast<unsigned>(u % 10);
343 // Derived from Hacker's Delight Second Edition Chapter 10
345 static inline unsigned
349 auto q = (u >> 1) + (u >> 2);
359 auto r =
static_cast<unsigned>(u - ((q << 3) + (q << 1)));
361 auto c = (r + 6) >> 4;
394 auto zm = uint128_t(xm) * uint128_t(ym);
408 xm =
static_cast<rep>(zm);
411 if (r == 1 || (r == 0 && (xm & 1) == 1))
427 "Number::multiplication overflow : exponent is " +
460 const uint128_t f = 100
'000'000
'000'000
'000;
461 mantissa_ = static_cast<std::int64_t>(uint128_t(nm) * f / uint128_t(dm));
462 exponent_ = ne - de - 17;
463 mantissa_ *= np * dp;
468 Number::operator rep() const
470 rep drops = mantissa_;
471 int offset = exponent_;
480 for (; offset < 0; ++offset)
485 for (; offset > 0; --offset)
487 if (drops > std::numeric_limits<decltype(drops)>::max() / 10)
488 throw std::overflow_error("Number::operator rep() overflow");
492 if (r == 1 || (r == 0 && (drops & 1) == 1))
502 Number::operator XRPAmount() const
504 return XRPAmount{static_cast<rep>(*this)};
508 to_string(Number const& amount)
510 // keep full internal accuracy, but make more human friendly if possible
511 if (amount == Number{})
514 auto const exponent = amount.exponent();
515 auto mantissa = amount.mantissa();
517 // Use scientific notation for exponents that are too small or too large
518 if (((exponent != 0) && ((exponent < -25) || (exponent > -5))))
520 std::string ret = std::to_string(mantissa);
522 ret.append(std::to_string(exponent));
526 bool negative = false;
530 mantissa = -mantissa;
534 assert(exponent + 43 > 0);
536 ptrdiff_t const pad_prefix = 27;
537 ptrdiff_t const pad_suffix = 23;
539 std::string const raw_value(std::to_string(mantissa));
542 val.reserve(raw_value.length() + pad_prefix + pad_suffix);
543 val.append(pad_prefix, '0
');
544 val.append(raw_value);
545 val.append(pad_suffix, '0
');
547 ptrdiff_t const offset(exponent + 43);
549 auto pre_from(val.begin());
550 auto const pre_to(val.begin() + offset);
552 auto const post_from(val.begin() + offset);
553 auto post_to(val.end());
555 // Crop leading zeroes. Take advantage of the fact that there's always a
558 pre_from += pad_prefix;
560 assert(post_to >= post_from);
562 pre_from =
std::find_if(pre_from, pre_to, [](
char c) {
return c !=
'0'; });
567 post_to -= pad_suffix;
569 assert(post_to >= post_from);
574 [](
char c) {
return c !=
'0'; })
583 if (pre_from == pre_to)
586 ret.
append(pre_from, pre_to);
588 if (post_to != post_from)
591 ret.
append(post_from, post_to);
607 auto r =
power(f, n / 2);
626 if (f ==
one || d == 1)
636 if (f <
Number{} && d % 2 == 0)
643 auto const di =
static_cast<int>(d);
644 auto ex = [e = e, di = di]()
646 int k = (e >= 0 ? e : e - (di - 1)) / di;
662 auto const D = ((6 * di + 11) * di + 6) * di + 1;
663 auto const a0 = 3 * di * ((2 * di - 3) * di + 1);
664 auto const a1 = 24 * di * (2 * di - 1);
665 auto const a2 = -30 * (di - 1) * di;
682 }
while (r != rm1 && r != rm2);
719 r = (r + f / r) /
Number(2);
720 }
while (r != rm1 && r != rm2);
749 if ((n % 2) == 1 && (d % 2) == 0 && f <
Number{})
static rounding_mode getround()
constexpr static std::int64_t maxMantissa
Number & operator*=(Number const &x)
void push(unsigned d) noexcept
constexpr int exponent() const noexcept
constexpr bool isnormal() const noexcept
bool is_negative() const noexcept
void set_positive() noexcept
static thread_local rounding_mode mode_
static rounding_mode setround(rounding_mode mode)
constexpr static int maxExponent
Number power(Number const &f, unsigned n)
constexpr static int minExponent
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Number & operator/=(Number const &x)
constexpr Number abs(Number x) noexcept
constexpr Number()=default
T make_reverse_iterator(T... args)
constexpr rep mantissa() const noexcept
void set_negative() noexcept
static unsigned divu10(uint128_t &u)
Number root(Number f, unsigned d)
@ FULL
we have the ledger and can even validate