SignedMath.sol 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (utils/math/SignedMath.sol)
  3. pragma solidity ^0.8.20;
  4. import {SafeCast} from "./SafeCast.sol";
  5. /**
  6. * @dev Standard signed math utilities missing in the Solidity language.
  7. */
  8. library SignedMath {
  9. /**
  10. * @dev Branchless ternary evaluation for `a ? b : c`. Gas costs are constant.
  11. *
  12. * IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone.
  13. * However, the compiler may optimize Solidity ternary operations (i.e. `a ? b : c`) to only compute
  14. * one branch when needed, making this function more expensive.
  15. */
  16. function ternary(bool condition, int256 a, int256 b) internal pure returns (int256) {
  17. unchecked {
  18. // branchless ternary works because:
  19. // b ^ (a ^ b) == a
  20. // b ^ 0 == b
  21. return b ^ ((a ^ b) * int256(SafeCast.toUint(condition)));
  22. }
  23. }
  24. /**
  25. * @dev Returns the largest of two signed numbers.
  26. */
  27. function max(int256 a, int256 b) internal pure returns (int256) {
  28. return ternary(a > b, a, b);
  29. }
  30. /**
  31. * @dev Returns the smallest of two signed numbers.
  32. */
  33. function min(int256 a, int256 b) internal pure returns (int256) {
  34. return ternary(a < b, a, b);
  35. }
  36. /**
  37. * @dev Returns the average of two signed numbers without overflow.
  38. * The result is rounded towards zero.
  39. */
  40. function average(int256 a, int256 b) internal pure returns (int256) {
  41. // Formula from the book "Hacker's Delight"
  42. int256 x = (a & b) + ((a ^ b) >> 1);
  43. return x + (int256(uint256(x) >> 255) & (a ^ b));
  44. }
  45. /**
  46. * @dev Returns the absolute unsigned value of a signed value.
  47. */
  48. function abs(int256 n) internal pure returns (uint256) {
  49. unchecked {
  50. // Formula from the "Bit Twiddling Hacks" by Sean Eron Anderson.
  51. // Since `n` is a signed integer, the generated bytecode will use the SAR opcode to perform the right shift,
  52. // taking advantage of the most significant (or "sign" bit) in two's complement representation.
  53. // This opcode adds new most significant bits set to the value of the previous most significant bit. As a result,
  54. // the mask will either be `bytes32(0)` (if n is positive) or `~bytes32(0)` (if n is negative).
  55. int256 mask = n >> 255;
  56. // A `bytes32(0)` mask leaves the input unchanged, while a `~bytes32(0)` mask complements it.
  57. return uint256((n + mask) ^ mask);
  58. }
  59. }
  60. }