Skip to content

Commit

Permalink
Remove unused rounding modes Trunc and Expand
Browse files Browse the repository at this point in the history
  • Loading branch information
barak manos committed Dec 30, 2024
1 parent 4c3ef87 commit 6a441be
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
22 changes: 10 additions & 12 deletions contracts/utils/math/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import {SafeCast} from "./SafeCast.sol";
*/
library Math {
enum Rounding {
Floor, // Toward negative infinity
Ceil, // Toward positive infinity
Trunc, // Toward zero
Expand // Away from zero
Floor, // Towards zero
Ceil // Towards infinity
}

/**
Expand Down Expand Up @@ -226,7 +224,7 @@ library Math {
* @dev Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
return mulDiv(x, y, denominator) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0);
return mulDiv(x, y, denominator) + unsignedRoundsUp(rounding, mulmod(x, y, denominator) > 0);
}

/**
Expand Down Expand Up @@ -529,7 +527,7 @@ library Math {
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && result * result < a);
return result + unsignedRoundsUp(rounding, result * result < a);
}
}

Expand Down Expand Up @@ -585,7 +583,7 @@ library Math {
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << result < value);
return result + unsignedRoundsUp(rounding, 1 << result < value);
}
}

Expand Down Expand Up @@ -634,7 +632,7 @@ library Math {
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 10 ** result < value);
return result + unsignedRoundsUp(rounding, 10 ** result < value);
}
}

Expand Down Expand Up @@ -664,14 +662,14 @@ library Math {
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << (result << 3) < value);
return result + unsignedRoundsUp(rounding, 1 << (result << 3) < value);
}
}

/**
* @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.
* @dev Returns 1 if rounding up is required and 0 if rounding up is not required.
*/
function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
return uint8(rounding) % 2 == 1;
function unsignedRoundsUp(Rounding rounding, bool cond) internal pure returns (uint256) {
return SafeCast.toUint(rounding == Rounding.Ceil && cond);
}
}
2 changes: 1 addition & 1 deletion test/helpers/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
Enum,
ProposalState: Enum('Pending', 'Active', 'Canceled', 'Defeated', 'Succeeded', 'Queued', 'Expired', 'Executed'),
VoteType: Object.assign(Enum('Against', 'For', 'Abstain'), { Parameters: 255n }),
Rounding: Enum('Floor', 'Ceil', 'Trunc', 'Expand'),
Rounding: Enum('Floor', 'Ceil'),
OperationState: Enum('Unset', 'Waiting', 'Ready', 'Done'),
RevertType: Enum('None', 'RevertWithoutMessage', 'RevertWithMessage', 'RevertWithCustomError', 'Panic'),
};
4 changes: 2 additions & 2 deletions test/utils/math/Math.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const { min, max, modExp } = require('../../helpers/math');
const { generators } = require('../../helpers/random');
const { product, range } = require('../../helpers/iterate');

const RoundingDown = [Rounding.Floor, Rounding.Trunc];
const RoundingUp = [Rounding.Ceil, Rounding.Expand];
const RoundingDown = [Rounding.Floor];
const RoundingUp = [Rounding.Ceil];

const bytes = (value, width = undefined) => ethers.Typed.bytes(ethers.toBeHex(value, width));
const uint256 = value => ethers.Typed.uint256(value);
Expand Down

0 comments on commit 6a441be

Please sign in to comment.