Update docs
This commit is contained in:
@ -1,24 +1,42 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/SignedMath.sol)
|
||||
// OpenZeppelin Contracts (last updated v5.1.0) (utils/math/SignedMath.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {SafeCast} from "./SafeCast.sol";
|
||||
|
||||
/**
|
||||
* @dev Standard signed math utilities missing in the Solidity language.
|
||||
*/
|
||||
library SignedMath {
|
||||
/**
|
||||
* @dev Branchless ternary evaluation for `a ? b : c`. Gas costs are constant.
|
||||
*
|
||||
* IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone.
|
||||
* However, the compiler may optimize Solidity ternary operations (i.e. `a ? b : c`) to only compute
|
||||
* one branch when needed, making this function more expensive.
|
||||
*/
|
||||
function ternary(bool condition, int256 a, int256 b) internal pure returns (int256) {
|
||||
unchecked {
|
||||
// branchless ternary works because:
|
||||
// b ^ (a ^ b) == a
|
||||
// b ^ 0 == b
|
||||
return b ^ ((a ^ b) * int256(SafeCast.toUint(condition)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the largest of two signed numbers.
|
||||
*/
|
||||
function max(int256 a, int256 b) internal pure returns (int256) {
|
||||
return a > b ? a : b;
|
||||
return ternary(a > b, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the smallest of two signed numbers.
|
||||
*/
|
||||
function min(int256 a, int256 b) internal pure returns (int256) {
|
||||
return a < b ? a : b;
|
||||
return ternary(a < b, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -36,8 +54,15 @@ library SignedMath {
|
||||
*/
|
||||
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);
|
||||
// Formula from the "Bit Twiddling Hacks" by Sean Eron Anderson.
|
||||
// Since `n` is a signed integer, the generated bytecode will use the SAR opcode to perform the right shift,
|
||||
// taking advantage of the most significant (or "sign" bit) in two's complement representation.
|
||||
// This opcode adds new most significant bits set to the value of the previous most significant bit. As a result,
|
||||
// the mask will either be `bytes32(0)` (if n is positive) or `~bytes32(0)` (if n is negative).
|
||||
int256 mask = n >> 255;
|
||||
|
||||
// A `bytes32(0)` mask leaves the input unchanged, while a `~bytes32(0)` mask complements it.
|
||||
return uint256((n + mask) ^ mask);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user