1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- // SPDX-License-Identifier: MIT
- // 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 ternary(a > b, a, b);
- }
- /**
- * @dev Returns the smallest of two signed numbers.
- */
- function min(int256 a, int256 b) internal pure returns (int256) {
- return ternary(a < b, a, b);
- }
- /**
- * @dev Returns the average of two signed numbers without overflow.
- * The result is rounded towards zero.
- */
- function average(int256 a, int256 b) internal pure returns (int256) {
- // Formula from the book "Hacker's Delight"
- 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 {
- // 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);
- }
- }
- }
|