17std::array<uint32_t, 4> Mul128(uint64_t a, uint64_t b)
19 std::array<uint32_t, 4>
ret{0, 0, 0, 0};
22 auto add_fn = [&](uint64_t v,
int pos) {
24 for (
int i = 0; i + pos < 4; ++i) {
26 accum +=
ret[3 - pos - i];
28 if (i == 0) accum += v & 0xffffffff;
29 if (i == 1) accum += v >> 32;
31 ret[3 - pos - i] = accum & 0xffffffff;
40 add_fn((a & 0xffffffff) * (b & 0xffffffff), 0);
41 add_fn((a >> 32) * (b & 0xffffffff), 1);
42 add_fn((a & 0xffffffff) * (b >> 32), 1);
43 add_fn((a >> 32) * (b >> 32), 2);
48std::strong_ordering compare_arrays(
const std::array<uint32_t, 4>& a,
const std::array<uint32_t, 4>& b) {
49 for (
size_t i = 0; i < a.size(); ++i) {
50 if (a[i] != b[i])
return a[i] <=> b[i];
52 return std::strong_ordering::equal;
55std::strong_ordering MulCompare(int64_t a1, int64_t a2, int64_t b1, int64_t b2)
58 int sign_a = (a1 == 0 ? 0 : a1 < 0 ? -1 : 1) * (a2 == 0 ? 0 : a2 < 0 ? -1 : 1);
59 int sign_b = (b1 == 0 ? 0 : b1 < 0 ? -1 : 1) * (b2 == 0 ? 0 : b2 < 0 ? -1 : 1);
60 if (sign_a != sign_b)
return sign_a <=> sign_b;
63 uint64_t abs_a1 =
static_cast<uint64_t
>(a1), abs_a2 =
static_cast<uint64_t
>(a2);
64 uint64_t abs_b1 =
static_cast<uint64_t
>(b1), abs_b2 =
static_cast<uint64_t
>(b2);
67 if (a1 < 0) abs_a1 = ~abs_a1 + 1;
68 if (a2 < 0) abs_a2 = ~abs_a2 + 1;
69 if (b1 < 0) abs_b1 = ~abs_b1 + 1;
70 if (b2 < 0) abs_b2 = ~abs_b2 + 1;
73 auto mul_abs_a = Mul128(abs_a1, abs_a2);
74 auto mul_abs_b = Mul128(abs_b1, abs_b2);
76 return compare_arrays(mul_abs_b, mul_abs_a);
78 return compare_arrays(mul_abs_a, mul_abs_b);
101 auto cmp_feerate = MulCompare(f1, s2, f2, s1);
102 assert(FeeRateCompare(fr1, fr2) == cmp_feerate);
103 assert((fr1 << fr2) == std::is_lt(cmp_feerate));
104 assert((fr1 >> fr2) == std::is_gt(cmp_feerate));
108 assert(cmp_mul == cmp_feerate);
112 assert(cmp_fallback == cmp_feerate);
115 auto cmp_total = std::is_eq(cmp_feerate) ? (s2 <=> s1) : cmp_feerate;
116 assert((fr1 <=> fr2) == cmp_total);
117 assert((fr1 < fr2) == std::is_lt(cmp_total));
118 assert((fr1 > fr2) == std::is_gt(cmp_total));
119 assert((fr1 <= fr2) == std::is_lteq(cmp_total));
120 assert((fr1 >= fr2) == std::is_gteq(cmp_total));
121 assert((fr1 == fr2) == std::is_eq(cmp_total));
122 assert((fr1 != fr2) == std::is_neq(cmp_total));
Data structure storing a fee and size, ordered by increasing fee/size.
static constexpr auto Mul
bool IsEmpty() const noexcept
Check if this is empty (size and fee are 0).
static std::pair< int64_t, uint32_t > MulFallback(int64_t a, int32_t b) noexcept
Fallback version for Mul (see below).