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