5 #ifndef BITCOIN_UTIL_FEEFRAC_H 6 #define BITCOIN_UTIL_FEEFRAC_H 44 static inline std::pair<int64_t, uint32_t>
MulFallback(int64_t a, int32_t b) noexcept
46 int64_t low = int64_t{
static_cast<uint32_t
>(a)} * b;
47 int64_t high = (a >> 32) * b;
48 return {high + (low >> 32), static_cast<uint32_t>(low)};
59 static inline int64_t
DivFallback(std::pair<int64_t, uint32_t> n, int32_t d,
bool round_down) noexcept
65 int64_t quot_high = n.first / d;
68 int64_t n_low = ((n.first % d) << 32) + n.second;
73 int64_t quot_low = n_low / d;
74 int32_t mod_low = n_low % d;
75 quot_low += (mod_low > 0) - (mod_low && round_down);
77 return (quot_high << 32) + quot_low;
80 #ifdef __SIZEOF_INT128__ 83 static inline __int128
Mul(int64_t a, int32_t b) noexcept
85 return __int128{a} * b;
93 static inline int64_t
Div(__int128 n, int32_t d,
bool round_down) noexcept
100 return quot + ((mod > 0) - (mod && round_down));
141 return {a.
fee + b.fee, a.size + b.size};
147 return {a.
fee - b.fee, a.size - b.size};
153 return a.fee == b.fee && a.size == b.size;
159 auto cross_a =
Mul(a.fee, b.size), cross_b =
Mul(b.fee, a.size);
160 return cross_a <=> cross_b;
166 auto cross_a =
Mul(a.fee, b.size), cross_b =
Mul(b.fee, a.size);
167 return cross_a < cross_b;
173 auto cross_a =
Mul(a.fee, b.size), cross_b =
Mul(b.fee, a.size);
174 return cross_a > cross_b;
178 friend inline std::strong_ordering operator<=>(
const FeeFrac& a,
const FeeFrac& b) noexcept
180 auto cross_a =
Mul(a.fee, b.size), cross_b =
Mul(b.fee, a.size);
181 if (cross_a == cross_b)
return b.size <=> a.size;
182 return cross_a <=> cross_b;
188 std::swap(a.fee, b.fee);
189 std::swap(a.size, b.size);
201 template<
bool RoundDown>
206 if (
fee >= 0 &&
fee < 0x200000000) [[likely]] {
208 if constexpr (RoundDown) {
209 return (uint64_t(
fee) * at_size) / uint32_t(
size);
211 return (uint64_t(
fee) * at_size +
size - 1U) / uint32_t(
size);
221 int64_t
EvaluateFeeDown(int32_t at_size)
const noexcept {
return EvaluateFee<true>(at_size); }
223 int64_t
EvaluateFeeUp(int32_t at_size)
const noexcept {
return EvaluateFee<false>(at_size); }
234 std::partial_ordering
CompareChunks(std::span<const FeeFrac> chunks0, std::span<const FeeFrac> chunks1);
237 template<
typename Tag>
246 return {feefrac.
fee, feefrac.size};
258 #endif // BITCOIN_UTIL_FEEFRAC_H
friend std::weak_ordering FeeRateCompare(const FeeFrac &a, const FeeFrac &b) noexcept
Compare two FeeFracs just by feerate.
static FeePerUnit FromFeeFrac(const FeeFrac &feefrac) noexcept
Convert a FeeFrac to a FeePerUnit.
friend void swap(FeeFrac &a, FeeFrac &b) noexcept
Swap two FeeFracs.
friend FeeFrac operator+(const FeeFrac &a, const FeeFrac &b) noexcept
Sum fee and size.
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...
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.
void operator+=(const FeeFrac &other) noexcept
Add fee and size of another FeeFrac to this one.
constexpr FeeFrac() noexcept
Construct an IsEmpty() FeeFrac.
friend FeeFrac operator-(const FeeFrac &a, const FeeFrac &b) noexcept
Subtract both fee and size.
friend bool operator<<(const FeeFrac &a, const FeeFrac &b) noexcept
Check if a FeeFrac object has strictly lower feerate than another.
Tagged wrapper around FeeFrac to avoid unit confusion.
void operator-=(const FeeFrac &other) noexcept
Subtract fee and size of another FeeFrac from this one.
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.
static constexpr auto Div
bool IsEmpty() const noexcept
Check if this is empty (size and fee are 0).
#define Assume(val)
Assume is the identity function.
constexpr FeeFrac & operator=(const FeeFrac &) noexcept=default
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...
static constexpr auto Mul
Data structure storing a fee and size, ordered by increasing fee/size.
int64_t EvaluateFee(int32_t at_size) const noexcept
Compute the fee for a given size at_size using this object's feerate.
friend bool operator==(const FeeFrac &a, const FeeFrac &b) noexcept
Check if two FeeFrac objects are equal (both same fee and same size).
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.
friend bool operator>>(const FeeFrac &a, const FeeFrac &b) noexcept
Check if a FeeFrac object has strictly higher feerate than another.