Bitcoin Core 31.99.0
P2P Digital Currency
feefrac.h
Go to the documentation of this file.
1// Copyright (c) The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_UTIL_FEEFRAC_H
6#define BITCOIN_UTIL_FEEFRAC_H
7
8#include <util/check.h>
9#include <util/overflow.h>
10
11#include <compare>
12#include <concepts>
13#include <cstdint>
14#include <span>
15#include <utility>
16
21struct FeeFrac
22{
26 static inline std::pair<int64_t, uint32_t> MulFallback(int64_t a, int32_t b) noexcept
27 {
28 int64_t low = int64_t{static_cast<uint32_t>(a)} * b;
29 int64_t high = (a >> 32) * b;
30 return {high + (low >> 32), static_cast<uint32_t>(low)};
31 }
32
41 static inline int64_t DivFallback(std::pair<int64_t, uint32_t> n, int32_t d, bool round_down) noexcept
42 {
43 Assume(d > 0);
44 // Compute quot_high = n.first / d, so the result becomes
45 // (n.second + (n.first - quot_high * d) * 2**32) / d + (quot_high * 2**32), or
46 // (n.second + (n.first % d) * 2**32) / d + (quot_high * 2**32).
47 int64_t quot_high = n.first / d;
48 // Evaluate the parenthesized expression above, so the result becomes
49 // n_low / d + (quot_high * 2**32)
50 int64_t n_low = ((n.first % d) << 32) + n.second;
51 // Evaluate the division so the result becomes quot_low + quot_high * 2**32. It is possible
52 // that the / operator here rounds in the wrong direction (if n_low is not a multiple of
53 // size, and is (if round_down) negative, or (if !round_down) positive). If so, make a
54 // correction.
55 int64_t quot_low = n_low / d;
56 int32_t mod_low = n_low % d;
57 quot_low += (mod_low > 0) - (mod_low && round_down);
58 // Combine and return the result
59 return (quot_high << 32) + quot_low;
60 }
61
62#ifdef __SIZEOF_INT128__
65 static inline __int128 Mul(int64_t a, int32_t b) noexcept
66 {
67 return __int128{a} * b;
68 }
69
75 static inline int64_t Div(__int128 n, int32_t d, bool round_down) noexcept
76 {
77 Assume(d > 0);
78 // Compute the division.
79 int64_t quot = n / d;
80 int32_t mod = n % d;
81 // Correct result if the / operator above rounded in the wrong direction.
82 return quot + ((mod > 0) - (mod && round_down));
83 }
84#else
85 static constexpr auto Mul = MulFallback;
86 static constexpr auto Div = DivFallback;
87#endif
88
89 int64_t fee;
90 int32_t size;
91
93 constexpr inline FeeFrac() noexcept : fee{0}, size{0} {}
94
96 constexpr inline FeeFrac(int64_t f, int32_t s) noexcept : fee{f}, size{s} {}
97
98 constexpr inline FeeFrac(const FeeFrac&) noexcept = default;
99 constexpr inline FeeFrac& operator=(const FeeFrac&) noexcept = default;
100
102 bool inline IsEmpty() const noexcept {
103 return size == 0;
104 }
105
107 void inline operator+=(const FeeFrac& other) noexcept
108 {
109 fee += other.fee;
110 size += other.size;
111 }
112
114 void inline operator-=(const FeeFrac& other) noexcept
115 {
116 fee -= other.fee;
117 size -= other.size;
118 }
119
121 friend inline FeeFrac operator+(const FeeFrac& a, const FeeFrac& b) noexcept
122 {
123 return {a.fee + b.fee, a.size + b.size};
124 }
125
127 friend inline FeeFrac operator-(const FeeFrac& a, const FeeFrac& b) noexcept
128 {
129 return {a.fee - b.fee, a.size - b.size};
130 }
131
133 friend inline bool operator==(const FeeFrac& a, const FeeFrac& b) noexcept
134 {
135 return a.fee == b.fee && a.size == b.size;
136 }
137
139 friend inline void swap(FeeFrac& a, FeeFrac& b) noexcept
140 {
141 std::swap(a.fee, b.fee);
142 std::swap(a.size, b.size);
143 }
144
154 template<bool RoundDown>
155 int64_t EvaluateFee(int32_t at_size) const noexcept
156 {
157 Assume(size > 0);
158 Assume(at_size >= 0);
159 if (fee >= 0 && fee < 0x200000000) [[likely]] {
160 // Common case where (this->fee * at_size) is guaranteed to fit in a uint64_t.
161 if constexpr (RoundDown) {
162 return (uint64_t(fee) * at_size) / uint32_t(size);
163 } else {
164 return CeilDiv(uint64_t(fee) * at_size, uint32_t(size));
165 }
166 } else {
167 // Otherwise, use Mul and Div.
168 return Div(Mul(fee, at_size), size, RoundDown);
169 }
170 }
171
172public:
174 int64_t EvaluateFeeDown(int32_t at_size) const noexcept { return EvaluateFee<true>(at_size); }
176 int64_t EvaluateFeeUp(int32_t at_size) const noexcept { return EvaluateFee<false>(at_size); }
177};
178
187std::partial_ordering CompareChunks(std::span<const FeeFrac> chunks0, std::span<const FeeFrac> chunks1);
188
190template<typename Tag>
191struct FeePerUnit : public FeeFrac
192{
193 // Inherit FeeFrac constructors.
195
197 static FeePerUnit FromFeeFrac(const FeeFrac& feefrac) noexcept
198 {
199 return {feefrac.fee, feefrac.size};
200 }
201};
202
203// FeePerUnit instance for satoshi / vbyte.
204struct VSizeTag {};
206
207// FeePerUnit instance for satoshi / WU.
208struct WeightTag {};
210
217template<std::derived_from<FeeFrac> T>
219{
220 const T& m_feefrac;
221
222public:
223 constexpr ByRatio(const T& feefrac) noexcept : m_feefrac{feefrac} {}
224
225 friend bool operator==(const ByRatio& a, const ByRatio& b) noexcept
226 {
227 auto cross_a = T::Mul(a.m_feefrac.fee, b.m_feefrac.size);
228 auto cross_b = T::Mul(b.m_feefrac.fee, a.m_feefrac.size);
229 return cross_a == cross_b;
230 }
231
232 // Note that we can use std::strong_ordering here, because even though FeeFrac{1,2} and
233 // FeeFrac{2,4} are distinct as FeeFracs, they are indistinguishable from ByRatio's perspective
234 // (operator== also treats them as equal).
235 friend std::strong_ordering operator<=>(const ByRatio& a, const ByRatio& b) noexcept
236 {
237 auto cross_a = T::Mul(a.m_feefrac.fee, b.m_feefrac.size);
238 auto cross_b = T::Mul(b.m_feefrac.fee, a.m_feefrac.size);
239 return cross_a <=> cross_b;
240 }
241
242 // Specialized versions for efficiency. GCC 15+ and Clang 11+ produce operator<=>-derived
243 // versions that are equally efficient as this at -O2, but earlier versions do not.
244 friend bool operator<(const ByRatio& a, const ByRatio& b) noexcept
245 {
246 auto cross_a = T::Mul(a.m_feefrac.fee, b.m_feefrac.size);
247 auto cross_b = T::Mul(b.m_feefrac.fee, a.m_feefrac.size);
248 return cross_a < cross_b;
249 }
250 friend bool operator>(const ByRatio& a, const ByRatio& b) noexcept
251 {
252 auto cross_a = T::Mul(a.m_feefrac.fee, b.m_feefrac.size);
253 auto cross_b = T::Mul(b.m_feefrac.fee, a.m_feefrac.size);
254 return cross_a > cross_b;
255 }
256 friend bool operator<=(const ByRatio& a, const ByRatio& b) noexcept
257 {
258 auto cross_a = T::Mul(a.m_feefrac.fee, b.m_feefrac.size);
259 auto cross_b = T::Mul(b.m_feefrac.fee, a.m_feefrac.size);
260 return cross_a <= cross_b;
261 }
262 friend bool operator>=(const ByRatio& a, const ByRatio& b) noexcept
263 {
264 auto cross_a = T::Mul(a.m_feefrac.fee, b.m_feefrac.size);
265 auto cross_b = T::Mul(b.m_feefrac.fee, a.m_feefrac.size);
266 return cross_a >= cross_b;
267 }
268};
269
288template<std::derived_from<FeeFrac> T>
290{
291 const T& m_feefrac;
292
293public:
294 constexpr ByRatioNegSize(const T& feefrac) noexcept : m_feefrac{feefrac} {}
295
296 friend bool operator==(const ByRatioNegSize& a, const ByRatioNegSize& b) noexcept
297 {
298 return a.m_feefrac == b.m_feefrac;
299 }
300
301 friend std::strong_ordering operator<=>(const ByRatioNegSize& a, const ByRatioNegSize& b) noexcept
302 {
303 auto cross_a = T::Mul(a.m_feefrac.fee, b.m_feefrac.size);
304 auto cross_b = T::Mul(b.m_feefrac.fee, a.m_feefrac.size);
305 auto cmp = cross_a <=> cross_b;
306 if (cmp != 0) return cmp;
307 return b.m_feefrac.size <=> a.m_feefrac.size;
308 }
309
310 // Support conversion back to underlying FeeFrac, which allows using std::max().
311 operator const T&() const noexcept { return m_feefrac; }
312};
313
314#endif // BITCOIN_UTIL_FEEFRAC_H
#define Assume(val)
Assume is the identity function.
Definition: check.h:128
Wrapper around FeeFrac & derived types, which adds a feerate-based ordering which treats equal-feerat...
Definition: feefrac.h:219
friend bool operator<(const ByRatio &a, const ByRatio &b) noexcept
Definition: feefrac.h:244
friend bool operator>=(const ByRatio &a, const ByRatio &b) noexcept
Definition: feefrac.h:262
friend bool operator<=(const ByRatio &a, const ByRatio &b) noexcept
Definition: feefrac.h:256
constexpr ByRatio(const T &feefrac) noexcept
Definition: feefrac.h:223
friend bool operator==(const ByRatio &a, const ByRatio &b) noexcept
Definition: feefrac.h:225
friend std::strong_ordering operator<=>(const ByRatio &a, const ByRatio &b) noexcept
Definition: feefrac.h:235
friend bool operator>(const ByRatio &a, const ByRatio &b) noexcept
Definition: feefrac.h:250
const T & m_feefrac
Definition: feefrac.h:220
Wrapper around FeeFrac & derived types, which adds a total ordering which first sorts by feerate and ...
Definition: feefrac.h:290
friend std::strong_ordering operator<=>(const ByRatioNegSize &a, const ByRatioNegSize &b) noexcept
Definition: feefrac.h:301
const T & m_feefrac
Definition: feefrac.h:291
constexpr ByRatioNegSize(const T &feefrac) noexcept
Definition: feefrac.h:294
friend bool operator==(const ByRatioNegSize &a, const ByRatioNegSize &b) noexcept
Definition: feefrac.h:296
std::partial_ordering CompareChunks(std::span< const FeeFrac > chunks0, std::span< const FeeFrac > chunks1)
Compare the feerate diagrams implied by the provided sorted chunks data.
Definition: feefrac.cpp:12
constexpr auto CeilDiv(const Dividend dividend, const Divisor divisor)
Integer ceiling division (for unsigned values).
Definition: overflow.h:71
Data structure storing a fee and size.
Definition: feefrac.h:22
constexpr FeeFrac(int64_t f, int32_t s) noexcept
Construct a FeeFrac with specified fee and size.
Definition: feefrac.h:96
friend FeeFrac operator-(const FeeFrac &a, const FeeFrac &b) noexcept
Subtract both fee and size.
Definition: feefrac.h:127
friend void swap(FeeFrac &a, FeeFrac &b) noexcept
Swap two FeeFracs.
Definition: feefrac.h:139
int64_t fee
Definition: feefrac.h:89
int64_t EvaluateFeeDown(int32_t at_size) const noexcept
Compute the fee for a given size at_size using this object's feerate, rounding down.
Definition: feefrac.h:174
int64_t EvaluateFee(int32_t at_size) const noexcept
Compute the fee for a given size at_size using this object's feerate.
Definition: feefrac.h:155
static int64_t DivFallback(std::pair< int64_t, uint32_t > n, int32_t d, bool round_down) noexcept
Helper function for 96/32 signed division, rounding towards negative infinity (if round_down) or posi...
Definition: feefrac.h:41
int64_t EvaluateFeeUp(int32_t at_size) const noexcept
Compute the fee for a given size at_size using this object's feerate, rounding up.
Definition: feefrac.h:176
constexpr FeeFrac(const FeeFrac &) noexcept=default
static constexpr auto Div
Definition: feefrac.h:86
constexpr FeeFrac & operator=(const FeeFrac &) noexcept=default
static constexpr auto Mul
Definition: feefrac.h:85
void operator-=(const FeeFrac &other) noexcept
Subtract fee and size of another FeeFrac from this one.
Definition: feefrac.h:114
int32_t size
Definition: feefrac.h:90
void operator+=(const FeeFrac &other) noexcept
Add fee and size of another FeeFrac to this one.
Definition: feefrac.h:107
bool IsEmpty() const noexcept
Check if this is empty (size and fee are 0).
Definition: feefrac.h:102
constexpr FeeFrac() noexcept
Construct an IsEmpty() FeeFrac.
Definition: feefrac.h:93
static std::pair< int64_t, uint32_t > MulFallback(int64_t a, int32_t b) noexcept
Helper function for 32*64 signed multiplication, returning an unspecified but totally ordered type.
Definition: feefrac.h:26
friend FeeFrac operator+(const FeeFrac &a, const FeeFrac &b) noexcept
Sum fee and size.
Definition: feefrac.h:121
friend bool operator==(const FeeFrac &a, const FeeFrac &b) noexcept
Check if two FeeFrac objects are equal (both same fee and same size).
Definition: feefrac.h:133
Tagged wrapper around FeeFrac to avoid unit confusion.
Definition: feefrac.h:192
static FeePerUnit FromFeeFrac(const FeeFrac &feefrac) noexcept
Convert a FeeFrac to a FeePerUnit.
Definition: feefrac.h:197