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