Repository: bitcoin/bitcoin
File: src/util/feefrac.h (C/C++)

Code before:
    constexpr inline FeeFrac() noexcept : fee{0}, size{0} {}

    /** Construct a FeeFrac with specified fee and size. */
    constexpr inline FeeFrac(int64_t f, int32_t s) noexcept : fee{f}, size{s} {}

    constexpr inline FeeFrac(const FeeFrac&) noexcept = default;
    constexpr inline FeeFrac& operator=(const FeeFrac&) noexcept = default;

    /** Check if this is empty (size and fee are 0). */
    bool inline IsEmpty() const noexcept {
        return size == 0;
    }

    /** Add fee and size of another FeeFrac to this one. */
    void inline operator+=(const FeeFrac& other) noexcept
    {
        fee += other.fee;
        size += other.size;
    }

    /** Subtract fee and size of another FeeFrac from this one. */
    void inline operator-=(const FeeFrac& other) noexcept
    {
        fee -= other.fee;
        size -= other.size;
    }

    /** Sum fee and size. */
    friend inline FeeFrac operator+(const FeeFrac& a, const FeeFrac& b) noexcept
    {
        return {a.fee + b.fee, a.size + b.size};
    }

    /** Subtract both fee and size. */
    friend inline FeeFrac operator-(const FeeFrac& a, const FeeFrac& b) noexcept
    {
        return {a.fee - b.fee, a.size - b.size};
    }

    /** Check if two FeeFrac objects are equal (both same fee and same size). */
    friend inline bool operator==(const FeeFrac& a, const FeeFrac& b) noexcept
    {
        return a.fee == b.fee && a.size == b.size;
    }

    /** Compare two FeeFracs just by feerate. */
    friend inline std::weak_ordering FeeRateCompare(const FeeFrac& a, const FeeFrac& b) noexcept
    {
        auto cross_a = Mul(a.fee, b.size), cross_b = Mul(b.fee, a.size);
        return cross_a <=> cross_b;
    }

Review comment (suggestion):
In "feefrac: support both rounding up and down for Evaluate"  486e816bd7d68f4411b35330907ce42812c5c71b

`Div` method description needs to be updated we round toward positive infinity when round down is false.

Code after:
        int64_t n_low = ((n.first % d) << 32) + n.second;
        // Evaluate the division so the result becomes quot_low + quot_high * 2**32. It is possible
        // that the / operator here rounds in the wrong direction (if n_low is not a multiple of
        // size, and is (if round_down) negative, or (if !round_down) positive). If so, make a
        // correction.
        int64_t quot_low = n_low / d;
        int32_t mod_low = n_low % d;
        quot_low += (mod_low > 0) - (mod_low && round_down);
        // Combine and return the result
        return (quot_high << 32) + quot_low;
    }

#ifdef __SIZEOF_INT128__
    /** Helper function for 32*64 signed multiplication, returning an unspecified but totally
     *  ordered type. This is a version relying on __int128. */
    static inline __int128 Mul(int64_t a, int32_t b) noexcept
    {
        return __int128{a} * b;
    }

    /** Helper function for 96/32 signed division, rounding towards negative infinity (if
     *  round_down), or towards positive infinity (if !round_down). This is a
     *  version relying on __int128.
     *
     * The result must fit in an int64_t, and d must be strictly positive. */
    static inline int64_t Div(__int128 n, int32_t d, bool round_down) noexcept
    {
        Assume(d > 0);
        // Compute the division.
        int64_t quot = n / d;
        int32_t mod = n % d;
        // Correct result if the / operator above rounded in the wrong direction.
        return quot + (mod > 0) - (mod && round_down);
    }
#else
    static constexpr auto Mul = MulFallback;
    static constexpr auto Div = DivFallback;
#endif

    int64_t fee;
    int32_t size;

    /** Construct an IsEmpty() FeeFrac. */
    constexpr inline FeeFrac() noexcept : fee{0}, size{0} {}

    /** Construct a FeeFrac with specified fee and size. */
    constexpr inline FeeFrac(int64_t f, int32_t s) noexcept : fee{f}, size{s} {}

    constexpr inline FeeFrac(const FeeFrac&) noexcept = default;
    constexpr inline FeeFrac& operator=(const FeeFrac&) noexcept = default;