Documentation Index
Fetch the complete documentation index at: https://mintlify.com/morpho-org/vault-v2/llms.txt
Use this file to discover all available pages before exploring further.
The MathLib library provides gas-optimized mathematical utility functions used throughout the Morpho Vault V2 system.
Functions
mulDivDown
function mulDivDown(uint256 x, uint256 y, uint256 d) internal pure returns (uint256)
Returns (x * y) / d rounded down.
Returns: The result of (x * y) / d rounded down
mulDivUp
function mulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256)
Returns (x * y) / d rounded up.
Returns: The result of (x * y) / d rounded up
zeroFloorSub
function zeroFloorSub(uint256 x, uint256 y) internal pure returns (uint256 z)
Returns max(0, x - y), preventing underflow by returning 0 if y > x.
Returns: The result of x - y if x > y, otherwise 0
toUint128
function toUint128(uint256 x) internal pure returns (uint128)
Casts from uint256 to uint128, reverting if the input number is too large.
Returns: The value cast to uint128
Reverts: ErrorsLib.CastOverflow() if x > type(uint128).max
toUint256
function toUint256(int256 x) internal pure returns (uint256)
Casts from int256 to uint256, reverting if the input number is negative.
Returns: The value cast to uint256
Reverts: ErrorsLib.CastOverflow() if x < 0
min
function min(uint256 x, uint256 y) internal pure returns (uint256 z)
Returns the minimum of two values.
Returns: The smaller of x and y
Source code
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (c) 2025 Morpho Association
pragma solidity ^0.8.0;
import {ErrorsLib} from "./ErrorsLib.sol";
library MathLib {
/// @dev Returns (x * y) / d rounded down.
function mulDivDown(uint256 x, uint256 y, uint256 d) internal pure returns (uint256) {
return (x * y) / d;
}
/// @dev Returns (x * y) / d rounded up.
function mulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256) {
return (x * y + (d - 1)) / d;
}
/// @dev Returns max(0, x - y).
function zeroFloorSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
assembly {
z := mul(gt(x, y), sub(x, y))
}
}
/// @dev Casts from uint256 to uint128, reverting if input number is too large.
function toUint128(uint256 x) internal pure returns (uint128) {
require(x <= type(uint128).max, ErrorsLib.CastOverflow());
// forge-lint: disable-next-item(unsafe-typecast) safe because x <= type(uint128).max.
return uint128(x);
}
/// @dev Casts from int256 to uint256, reverting if input number is negative.
function toUint256(int256 x) internal pure returns (uint256) {
require(x >= 0, ErrorsLib.CastOverflow());
// forge-lint: disable-next-item(unsafe-typecast) safe because x >= 0.
return uint256(x);
}
/// @dev Returns min(x, y).
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
assembly {
z := xor(x, mul(xor(x, y), lt(y, x)))
}
}
}