Skip to content

Commit

Permalink
Attempting to avoid all overflow in diagram checks
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs committed Jan 10, 2024
1 parent 698f826 commit 100b0fb
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/policy/rbf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ inline std::pair<int64_t, uint64_t> Mul128(int64_t a, int64_t b)

inline std::pair<int64_t, uint64_t> Add128(const std::pair<int64_t, uint64_t>& a,
const std::pair<int64_t, uint64_t>& b) {
// Add the low parts
uint64_t low = a.second + b.second;
uint32_t low_a_low = (uint32_t)a.second;
uint32_t low_a_high = a.second >> 32;
uint32_t low_b_low = (uint32_t)b.second;
uint32_t low_b_high = b.second >> 32;

// Calculate carry from the low part (if the addition of low parts overflows, it will carry to the high part)
int64_t carry = (low < a.second) ? 1 : 0;
uint64_t low_sum_low = static_cast<uint64_t>(low_a_low) + low_b_low;
uint64_t carry = low_sum_low >> 32; // carry for the next 32 bits

uint64_t low_sum_high = static_cast<uint64_t>(low_a_high) + low_b_high + carry;
carry = low_sum_high >> 32; // carry for the high part

uint64_t low = (low_sum_high << 32) | (low_sum_low & 0xFFFFFFFF);

// Add the high parts and the carry
int64_t high = a.first + b.first + carry;

return {high, low};
Expand Down

0 comments on commit 100b0fb

Please sign in to comment.