rippled
Loading...
Searching...
No Matches
Quality.cpp
1#include <xrpl/beast/utility/Zero.h>
2#include <xrpl/beast/utility/instrumentation.h>
3#include <xrpl/protocol/Asset.h>
4#include <xrpl/protocol/Quality.h>
5#include <xrpl/protocol/STAmount.h>
6
7#include <cstdint>
8#include <limits>
9
10namespace xrpl {
11
12Quality::Quality(std::uint64_t value) : m_value(value)
13{
14}
15
16Quality::Quality(Amounts const& amount) : m_value(getRate(amount.out, amount.in))
17{
18}
19
20Quality&
21Quality::operator++()
22{
23 XRPL_ASSERT(m_value > 0, "xrpl::Quality::operator++() : minimum value");
24 --m_value;
25 return *this;
26}
27
28Quality
29Quality::operator++(int)
30{
31 Quality prev(*this);
32 ++*this;
33 return prev;
34}
35
36Quality&
37Quality::operator--()
38{
39 XRPL_ASSERT(
41 "xrpl::Quality::operator--() : maximum value");
42 ++m_value;
43 return *this;
44}
45
46Quality
47Quality::operator--(int)
48{
49 Quality prev(*this);
50 --*this;
51 return prev;
52}
53
54template <STAmount (*DivRoundFunc)(STAmount const&, STAmount const&, Asset const&, bool)>
55static Amounts
56ceil_in_impl(Amounts const& amount, STAmount const& limit, bool roundUp, Quality const& quality)
57{
58 if (amount.in > limit)
59 {
60 Amounts result(limit, DivRoundFunc(limit, quality.rate(), amount.out.asset(), roundUp));
61 // Clamp out
62 if (result.out > amount.out)
63 result.out = amount.out;
64 XRPL_ASSERT(result.in == limit, "xrpl::ceil_in_impl : result matches limit");
65 return result;
66 }
67 XRPL_ASSERT(amount.in <= limit, "xrpl::ceil_in_impl : result inside limit");
68 return amount;
69}
70
71Amounts
72Quality::ceil_in(Amounts const& amount, STAmount const& limit) const
73{
74 return ceil_in_impl<divRound>(amount, limit, /* roundUp */ true, *this);
75}
76
77Amounts
78Quality::ceil_in_strict(Amounts const& amount, STAmount const& limit, bool roundUp) const
79{
80 return ceil_in_impl<divRoundStrict>(amount, limit, roundUp, *this);
81}
82
83template <STAmount (*MulRoundFunc)(STAmount const&, STAmount const&, Asset const&, bool)>
84static Amounts
85ceil_out_impl(Amounts const& amount, STAmount const& limit, bool roundUp, Quality const& quality)
86{
87 if (amount.out > limit)
88 {
89 Amounts result(MulRoundFunc(limit, quality.rate(), amount.in.asset(), roundUp), limit);
90 // Clamp in
91 if (result.in > amount.in)
92 result.in = amount.in;
93 XRPL_ASSERT(result.out == limit, "xrpl::ceil_out_impl : result matches limit");
94 return result;
95 }
96 XRPL_ASSERT(amount.out <= limit, "xrpl::ceil_out_impl : result inside limit");
97 return amount;
98}
99
100Amounts
101Quality::ceil_out(Amounts const& amount, STAmount const& limit) const
102{
103 return ceil_out_impl<mulRound>(amount, limit, /* roundUp */ true, *this);
104}
105
106Amounts
107Quality::ceil_out_strict(Amounts const& amount, STAmount const& limit, bool roundUp) const
108{
109 return ceil_out_impl<mulRoundStrict>(amount, limit, roundUp, *this);
110}
111
112Quality
113composed_quality(Quality const& lhs, Quality const& rhs)
114{
115 STAmount const lhs_rate(lhs.rate());
116 XRPL_ASSERT(lhs_rate != beast::zero, "xrpl::composed_quality : nonzero left input");
117
118 STAmount const rhs_rate(rhs.rate());
119 XRPL_ASSERT(rhs_rate != beast::zero, "xrpl::composed_quality : nonzero right input");
120
121 STAmount const rate(mulRound(lhs_rate, rhs_rate, lhs_rate.asset(), true));
122
123 std::uint64_t const stored_exponent(rate.exponent() + 100);
124 std::uint64_t const stored_mantissa(rate.mantissa());
125
126 XRPL_ASSERT(
127 (stored_exponent > 0) && (stored_exponent <= 255),
128 "xrpl::composed_quality : valid exponent");
129
130 return Quality((stored_exponent << (64 - 8)) | stored_mantissa);
131}
132
133Quality
134Quality::round(int digits) const
135{
136 // Modulus for mantissa
137 static std::uint64_t const mod[17] = {
138 /* 0 */ 10000000000000000,
139 /* 1 */ 1000000000000000,
140 /* 2 */ 100000000000000,
141 /* 3 */ 10000000000000,
142 /* 4 */ 1000000000000,
143 /* 5 */ 100000000000,
144 /* 6 */ 10000000000,
145 /* 7 */ 1000000000,
146 /* 8 */ 100000000,
147 /* 9 */ 10000000,
148 /* 10 */ 1000000,
149 /* 11 */ 100000,
150 /* 12 */ 10000,
151 /* 13 */ 1000,
152 /* 14 */ 100,
153 /* 15 */ 10,
154 /* 16 */ 1,
155 };
156
157 auto exponent = m_value >> (64 - 8);
158 auto mantissa = m_value & 0x00ffffffffffffffULL;
159 mantissa += mod[digits] - 1;
160 mantissa -= (mantissa % mod[digits]);
161
162 return Quality{(exponent << (64 - 8)) | mantissa};
163}
164
165} // namespace xrpl
Asset const & asset() const
Definition STAmount.h:457
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Quality composed_quality(Quality const &lhs, Quality const &rhs)
Definition Quality.cpp:113
static Amounts ceil_in_impl(Amounts const &amount, STAmount const &limit, bool roundUp, Quality const &quality)
Definition Quality.cpp:56
static Amounts ceil_out_impl(Amounts const &amount, STAmount const &limit, bool roundUp, Quality const &quality)
Definition Quality.cpp:85
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
Definition STAmount.cpp:450
STAmount mulRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
T prev(T... args)