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 <span.h>
9#include <util/check.h>
10#include <util/overflow.h>
11
12#include <compare>
13#include <cstdint>
14#include <vector>
15
40struct FeeFrac
41{
45 static inline std::pair<int64_t, uint32_t> MulFallback(int64_t a, int32_t b) noexcept
46 {
47 int64_t low = int64_t{static_cast<uint32_t>(a)} * b;
48 int64_t high = (a >> 32) * b;
49 return {high + (low >> 32), static_cast<uint32_t>(low)};
50 }
51
60 static inline int64_t DivFallback(std::pair<int64_t, uint32_t> n, int32_t d, bool round_down) noexcept
61 {
62 Assume(d > 0);
63 // Compute quot_high = n.first / d, so the result becomes
64 // (n.second + (n.first - quot_high * d) * 2**32) / d + (quot_high * 2**32), or
65 // (n.second + (n.first % d) * 2**32) / d + (quot_high * 2**32).
66 int64_t quot_high = n.first / d;
67 // Evaluate the parenthesized expression above, so the result becomes
68 // n_low / d + (quot_high * 2**32)
69 int64_t n_low = ((n.first % d) << 32) + n.second;
70 // Evaluate the division so the result becomes quot_low + quot_high * 2**32. It is possible
71 // that the / operator here rounds in the wrong direction (if n_low is not a multiple of
72 // size, and is (if round_down) negative, or (if !round_down) positive). If so, make a
73 // correction.
74 int64_t quot_low = n_low / d;
75 int32_t mod_low = n_low % d;
76 quot_low += (mod_low > 0) - (mod_low && round_down);
77 // Combine and return the result
78 return (quot_high << 32) + quot_low;
79 }
80
81#ifdef __SIZEOF_INT128__
84 static inline __int128 Mul(int64_t a, int32_t b) noexcept
85 {
86 return __int128{a} * b;
87 }
88
94 static inline int64_t Div(__int128 n, int32_t d, bool round_down) noexcept
95 {
96 Assume(d > 0);
97 // Compute the division.
98 int64_t quot = n / d;
99 int32_t mod = n % d;
100 // Correct result if the / operator above rounded in the wrong direction.
101 return quot + ((mod > 0) - (mod && round_down));
102 }
103#else
104 static constexpr auto Mul = MulFallback;
105 static constexpr auto Div = DivFallback;
106#endif
107
108 int64_t fee;
109 int32_t size;
110
112 constexpr inline FeeFrac() noexcept : fee{0}, size{0} {}
113
115 constexpr inline FeeFrac(int64_t f, int32_t s) noexcept : fee{f}, size{s} {}
116
117 constexpr inline FeeFrac(const FeeFrac&) noexcept = default;
118 constexpr inline FeeFrac& operator=(const FeeFrac&) noexcept = default;
119
121 bool inline IsEmpty() const noexcept {
122 return size == 0;
123 }
124
126 void inline operator+=(const FeeFrac& other) noexcept
127 {
128 fee += other.fee;
129 size += other.size;
130 }
131
133 void inline operator-=(const FeeFrac& other) noexcept
134 {
135 fee -= other.fee;
136 size -= other.size;
137 }
138
140 friend inline FeeFrac operator+(const FeeFrac& a, const FeeFrac& b) noexcept
141 {
142 return {a.fee + b.fee, a.size + b.size};
143 }
144
146 friend inline FeeFrac operator-(const FeeFrac& a, const FeeFrac& b) noexcept
147 {
148 return {a.fee - b.fee, a.size - b.size};
149 }
150
152 friend inline bool operator==(const FeeFrac& a, const FeeFrac& b) noexcept
153 {
154 return a.fee == b.fee && a.size == b.size;
155 }
156
158 friend inline std::weak_ordering FeeRateCompare(const FeeFrac& a, const FeeFrac& b) noexcept
159 {
160 auto cross_a = Mul(a.fee, b.size), cross_b = Mul(b.fee, a.size);
161 return cross_a <=> cross_b;
162 }
163
165 friend inline bool operator<<(const FeeFrac& a, const FeeFrac& b) noexcept
166 {
167 auto cross_a = Mul(a.fee, b.size), cross_b = Mul(b.fee, a.size);
168 return cross_a < cross_b;
169 }
170
172 friend inline bool operator>>(const FeeFrac& a, const FeeFrac& b) noexcept
173 {
174 auto cross_a = Mul(a.fee, b.size), cross_b = Mul(b.fee, a.size);
175 return cross_a > cross_b;
176 }
177
179 friend inline std::strong_ordering operator<=>(const FeeFrac& a, const FeeFrac& b) noexcept
180 {
181 auto cross_a = Mul(a.fee, b.size), cross_b = Mul(b.fee, a.size);
182 if (cross_a == cross_b) return b.size <=> a.size;
183 return cross_a <=> cross_b;
184 }
185
187 friend inline void swap(FeeFrac& a, FeeFrac& b) noexcept
188 {
189 std::swap(a.fee, b.fee);
190 std::swap(a.size, b.size);
191 }
192
202 template<bool RoundDown>
203 int64_t EvaluateFee(int32_t at_size) const noexcept
204 {
205 Assume(size > 0);
206 Assume(at_size >= 0);
207 if (fee >= 0 && fee < 0x200000000) [[likely]] {
208 // Common case where (this->fee * at_size) is guaranteed to fit in a uint64_t.
209 if constexpr (RoundDown) {
210 return (uint64_t(fee) * at_size) / uint32_t(size);
211 } else {
212 return CeilDiv(uint64_t(fee) * at_size, uint32_t(size));
213 }
214 } else {
215 // Otherwise, use Mul and Div.
216 return Div(Mul(fee, at_size), size, RoundDown);
217 }
218 }
219
220public:
222 int64_t EvaluateFeeDown(int32_t at_size) const noexcept { return EvaluateFee<true>(at_size); }
224 int64_t EvaluateFeeUp(int32_t at_size) const noexcept { return EvaluateFee<false>(at_size); }
225};
226
235std::partial_ordering CompareChunks(std::span<const FeeFrac> chunks0, std::span<const FeeFrac> chunks1);
236
238template<typename Tag>
239struct FeePerUnit : public FeeFrac
240{
241 // Inherit FeeFrac constructors.
243
245 static FeePerUnit FromFeeFrac(const FeeFrac& feefrac) noexcept
246 {
247 return {feefrac.fee, feefrac.size};
248 }
249};
250
251// FeePerUnit instance for satoshi / vbyte.
252struct VSizeTag {};
254
255// FeePerUnit instance for satoshi / WU.
256struct WeightTag {};
258
259#endif // BITCOIN_UTIL_FEEFRAC_H
#define Assume(val)
Assume is the identity function.
Definition: check.h:125
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:10
constexpr auto CeilDiv(const Dividend dividend, const Divisor divisor)
Integer ceiling division (for unsigned values).
Definition: overflow.h:70
Data structure storing a fee and size, ordered by increasing fee/size.
Definition: feefrac.h:41
constexpr FeeFrac(int64_t f, int32_t s) noexcept
Construct a FeeFrac with specified fee and size.
Definition: feefrac.h:115
friend std::weak_ordering FeeRateCompare(const FeeFrac &a, const FeeFrac &b) noexcept
Compare two FeeFracs just by feerate.
Definition: feefrac.h:158
friend FeeFrac operator-(const FeeFrac &a, const FeeFrac &b) noexcept
Subtract both fee and size.
Definition: feefrac.h:146
friend bool operator>>(const FeeFrac &a, const FeeFrac &b) noexcept
Check if a FeeFrac object has strictly higher feerate than another.
Definition: feefrac.h:172
friend void swap(FeeFrac &a, FeeFrac &b) noexcept
Swap two FeeFracs.
Definition: feefrac.h:187
int64_t fee
Definition: feefrac.h:108
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:222
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:203
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:60
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:224
friend std::strong_ordering operator<=>(const FeeFrac &a, const FeeFrac &b) noexcept
Compare two FeeFracs.
Definition: feefrac.h:179
constexpr FeeFrac(const FeeFrac &) noexcept=default
static constexpr auto Div
Definition: feefrac.h:105
constexpr FeeFrac & operator=(const FeeFrac &) noexcept=default
static constexpr auto Mul
Definition: feefrac.h:104
friend bool operator<<(const FeeFrac &a, const FeeFrac &b) noexcept
Check if a FeeFrac object has strictly lower feerate than another.
Definition: feefrac.h:165
void operator-=(const FeeFrac &other) noexcept
Subtract fee and size of another FeeFrac from this one.
Definition: feefrac.h:133
int32_t size
Definition: feefrac.h:109
void operator+=(const FeeFrac &other) noexcept
Add fee and size of another FeeFrac to this one.
Definition: feefrac.h:126
bool IsEmpty() const noexcept
Check if this is empty (size and fee are 0).
Definition: feefrac.h:121
constexpr FeeFrac() noexcept
Construct an IsEmpty() FeeFrac.
Definition: feefrac.h:112
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:45
friend FeeFrac operator+(const FeeFrac &a, const FeeFrac &b) noexcept
Sum fee and size.
Definition: feefrac.h:140
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:152
Tagged wrapper around FeeFrac to avoid unit confusion.
Definition: feefrac.h:240
static FeePerUnit FromFeeFrac(const FeeFrac &feefrac) noexcept
Convert a FeeFrac to a FeePerUnit.
Definition: feefrac.h:245