Skip to content

Commit

Permalink
Merge pull request #21 from axieinfinity/implement-feature/math/add-l…
Browse files Browse the repository at this point in the history
…ib-safe-range

feat(math): implement `add-lib-safe-range`
  • Loading branch information
TuDo1403 authored Dec 11, 2023
2 parents e6e431a + 7ab1afa commit edab1dd
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/math/LibSafeRange.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

library LibSafeRange {
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
unchecked {
c = a + b;
if (c < a) return type(uint256).max;
}
}

/**
* @dev Returns value of a + b; in case result is larger than upperbound, upperbound is returned.
*/
function addWithUpperbound(uint256 a, uint256 b, uint256 ceil) internal pure returns (uint256 c) {
if (a > ceil || b > ceil) return ceil;
c = add(a, b);
if (c > ceil) return ceil;
}
}

0 comments on commit edab1dd

Please sign in to comment.