| 606 | } |
| 607 | |
| 608 | static bool amount_msat_mul_div(struct amount_msat *res, |
| 609 | struct amount_msat msat, u64 mul, u64 div) |
| 610 | { |
| 611 | /* We avoid overflow in the operation (a/b)*x noticing that we can |
| 612 | * decompose x = q_x * b + r_x, where q_x = floor(x/b) and |
| 613 | * r_x = x mod b, then: x*(a/b) = a*q_x + a*r_x/b, which does not |
| 614 | * overflow if a*b does not. */ |
| 615 | if (mul_overflows_u64(mul, div)) |
| 616 | return false; |
| 617 | u64 x = msat.millisatoshis; |
| 618 | res->millisatoshis = mul * (x / div) + (mul * (x % div)) / div; |
| 619 | return true; |
| 620 | } |
| 621 | |
| 622 | bool amount_msat_fee(struct amount_msat *fee, |
| 623 | struct amount_msat amt, |
no test coverage detected