Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branchless isHealthy #24

Closed
MathisGD opened this issue Jul 4, 2023 · 3 comments
Closed

Branchless isHealthy #24

MathisGD opened this issue Jul 4, 2023 · 3 comments

Comments

@MathisGD
Copy link
Contributor

MathisGD commented Jul 4, 2023

isHealthy could be branchless:

could be branchless 🤓

    function isHealthy(Market calldata market, Id id, address user) private view returns (bool) {
        uint borrowValue = borrowShare[id][user].wMul(totalBorrow[id]).uwDiv(totalBorrowShares[id]).wMul(market.borrowableOracle.price());
        uint collateralValue = collateral[id][user].wMul(market.collateralOracle.price());
        return collateralValue.wMul(market.lLTV) >= borrowValue;
    }

with

    /// @dev Rounds towards zero.
    function uwDiv(uint x, uint y) internal pure returns (uint z) {
        // This part is "checked".
        z = x * WAD;
        // This part is "unchecked".
        assembly {
            z := div(z, y)
        }
    }

It uses the fact that div(x, 0) outputs 0.

From #4 (comment)

@Rubilmax
Copy link
Collaborator

Rubilmax commented Jul 5, 2023

uwDiv is very close to wDiv. I am against such naming.

wadDiv is better, and unsafeWadDiv is more easily distinguishable.

@MathisGD
Copy link
Contributor Author

MathisGD commented Jul 5, 2023

I really like wDiv, but you are right we should probably go for unsafeWDiv for this one

@MathisGD
Copy link
Contributor Author

I don't think that we want this kind of low-level optis finally

@MathisGD MathisGD closed this as not planned Won't fix, can't repro, duplicate, stale Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants