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/Feature.h>
7#include <xrpl/protocol/Rules.h>
8#include <xrpl/protocol/STAmount.h>
28 XRPL_ASSERT(
value_ > 0,
"xrpl::Quality::operator++() : minimum value");
46 "xrpl::Quality::operator--() : maximum value");
59template <STAmount (*DivRoundFunc)(STAmount const&, STAmount const&, Asset const&,
bool)>
63 if (amount.
in > limit)
65 Amounts result(limit, DivRoundFunc(limit, quality.rate(), amount.
out.
asset(), roundUp));
67 if (result.
out > amount.
out)
69 XRPL_ASSERT(result.
in == limit,
"xrpl::ceilInImpl : result matches limit");
72 XRPL_ASSERT(amount.
in <= limit,
"xrpl::ceilInImpl : result inside limit");
92template <STAmount (*MulRoundFunc)(STAmount const&, STAmount const&, Asset const&,
bool)>
98 return MulRoundFunc(v1, v2, asset, roundUp);
116template <STAmount (*MulRoundFunc)(STAmount const&, STAmount const&, Asset const&,
bool)>
120 if (amount.
out > limit)
126 .value_or(amount.
in),
129 if (result.
in > amount.
in)
130 result.
in = amount.
in;
131 XRPL_ASSERT(result.
out == limit,
"xrpl::ceilOutImpl : result matches limit");
134 XRPL_ASSERT(amount.
out <= limit,
"xrpl::ceilOutImpl : result inside limit");
154 XRPL_ASSERT(lhsRate !=
beast::kZero,
"xrpl::composedQuality : nonzero left input");
157 XRPL_ASSERT(rhsRate !=
beast::kZero,
"xrpl::composedQuality : nonzero right input");
165 (storedExponent > 0) && (storedExponent <= 255),
"xrpl::composedQuality : valid exponent");
167 return Quality((storedExponent << (64 - 8)) | storedMantissa);
194 auto const exponent =
value_ >> (64 - 8);
195 auto mantissa =
value_ & 0x00ffffffffffffffULL;
196 mantissa += kMod[digits] - 1;
197 mantissa -= (mantissa % kMod[digits]);
199 return Quality{(exponent << (64 - 8)) | mantissa};
constexpr bool native() const
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
bool isFeatureEnabled(UInt256 const &feature, bool resultIfNoRules)
Check whether a feature is enabled in the current ledger rules.
static Amounts ceilOutImpl(Amounts const &amount, STAmount const &limit, bool roundUp, Quality const &quality)
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
static std::optional< STAmount > tryMulRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
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.