Move abs(int256) from Math to SafeMath (#3110)
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
(cherry picked from commit 3eb2d43b06)
Signed-off-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
@ -20,8 +20,4 @@ contract MathMock {
|
||||
function ceilDiv(uint256 a, uint256 b) public pure returns (uint256) {
|
||||
return Math.ceilDiv(a, b);
|
||||
}
|
||||
|
||||
function abs(int256 n) public pure returns (uint256) {
|
||||
return Math.abs(n);
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,4 +16,8 @@ contract SignedMathMock {
|
||||
function average(int256 a, int256 b) public pure returns (int256) {
|
||||
return SignedMath.average(a, b);
|
||||
}
|
||||
|
||||
function abs(int256 n) public pure returns (uint256) {
|
||||
return SignedMath.abs(n);
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,14 +40,4 @@ library Math {
|
||||
// (a + b - 1) / b can overflow on addition, so we distribute.
|
||||
return a / b + (a % b == 0 ? 0 : 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the absolute unsigned value of a signed value.
|
||||
*/
|
||||
function abs(int256 n) internal pure returns (uint256) {
|
||||
unchecked {
|
||||
// must be unchecked in order to support `n = type(int256).min`
|
||||
return uint256(n >= 0 ? n : -n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,4 +30,14 @@ library SignedMath {
|
||||
int256 x = (a & b) + ((a ^ b) >> 1);
|
||||
return x + (int256(uint256(x) >> 255) & (a ^ b));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the absolute unsigned value of a signed value.
|
||||
*/
|
||||
function abs(int256 n) internal pure returns (uint256) {
|
||||
unchecked {
|
||||
// must be unchecked in order to support `n = type(int256).min`
|
||||
return uint256(n >= 0 ? n : -n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user