1#include <xrpl/protocol/Quality.h>
3#include <xrpl/beast/utility/Zero.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/protocol/Asset.h>
6#include <xrpl/protocol/STAmount.h>
24 XRPL_ASSERT(
value_ > 0,
"xrpl::Quality::operator++() : minimum value");
42 "xrpl::Quality::operator--() : maximum value");
55template <STAmount (*DivRoundFunc)(STAmount const&, STAmount const&, Asset const&,
bool)>
59 if (amount.
in > limit)
61 Amounts result(limit, DivRoundFunc(limit, quality.rate(), amount.
out.
asset(), roundUp));
63 if (result.
out > amount.
out)
65 XRPL_ASSERT(result.
in == limit,
"xrpl::ceilInImpl : result matches limit");
68 XRPL_ASSERT(amount.
in <= limit,
"xrpl::ceilInImpl : result inside limit");
84template <STAmount (*MulRoundFunc)(STAmount const&, STAmount const&, Asset const&,
bool)>
88 if (amount.
out > limit)
90 Amounts result(MulRoundFunc(limit, quality.rate(), amount.
in.
asset(), roundUp), limit);
92 if (result.
in > amount.
in)
93 result.
in = amount.
in;
94 XRPL_ASSERT(result.
out == limit,
"xrpl::ceilOutImpl : result matches limit");
97 XRPL_ASSERT(amount.
out <= limit,
"xrpl::ceilOutImpl : result inside limit");
117 XRPL_ASSERT(lhsRate != beast::kZero,
"xrpl::composedQuality : nonzero left input");
120 XRPL_ASSERT(rhsRate != beast::kZero,
"xrpl::composedQuality : nonzero right input");
128 (storedExponent > 0) && (storedExponent <= 255),
"xrpl::composedQuality : valid exponent");
130 return Quality((storedExponent << (64 - 8)) | storedMantissa);
157 auto exponent =
value_ >> (64 - 8);
158 auto mantissa =
value_ & 0x00ffffffffffffffULL;
159 mantissa += kMod[digits] - 1;
160 mantissa -= (mantissa % kMod[digits]);
162 return Quality{(exponent << (64 - 8)) | mantissa};
Represents the logical ratio of output currency to input currency.
Quality & operator++()
Advances to the next higher quality level.
STAmount rate() const
Returns the quality as STAmount.
Quality round(int tickSize) const
Returns the quality rounded up to the specified number of decimal digits.
Amounts ceilOut(Amounts const &amount, STAmount const &limit) const
Returns the scaled amount with out capped.
Amounts ceilIn(Amounts const &amount, STAmount const &limit) const
Returns the scaled amount with in capped.
Amounts ceilOutStrict(Amounts const &amount, STAmount const &limit, bool roundUp) const
Amounts ceilInStrict(Amounts const &amount, STAmount const &limit, bool roundUp) const
Quality & operator--()
Advances to the next lower quality level.
Asset const & asset() const
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
TAmounts< STAmount, STAmount > Amounts
static Amounts ceilOutImpl(Amounts const &amount, STAmount const &limit, bool roundUp, Quality const &quality)
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
STAmount mulRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
static Amounts ceilInImpl(Amounts const &amount, STAmount const &limit, bool roundUp, Quality const &quality)
Quality composedQuality(Quality const &lhs, Quality const &rhs)
Calculate the quality of a two-hop path given the two hops.