SignedMath.sol 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (utils/math/SignedMath.sol)
  3. pragma solidity ^0.8.20;
  4. /**
  5. * @dev Standard signed math utilities missing in the Solidity language.
  6. */
  7. library SignedMath {
  8. /**
  9. * @dev Returns the largest of two signed numbers.
  10. */
  11. function max(int256 a, int256 b) internal pure returns (int256) {
  12. return a > b ? a : b;
  13. }
  14. /**
  15. * @dev Returns the smallest of two signed numbers.
  16. */
  17. function min(int256 a, int256 b) internal pure returns (int256) {
  18. return a < b ? a : b;
  19. }
  20. /**
  21. * @dev Returns the average of two signed numbers without overflow.
  22. * The result is rounded towards zero.
  23. */
  24. function average(int256 a, int256 b) internal pure returns (int256) {
  25. // Formula from the book "Hacker's Delight"
  26. int256 x = (a & b) + ((a ^ b) >> 1);
  27. return x + (int256(uint256(x) >> 255) & (a ^ b));
  28. }
  29. /**
  30. * @dev Returns the absolute unsigned value of a signed value.
  31. */
  32. function abs(int256 n) internal pure returns (uint256) {
  33. unchecked {
  34. // Formula from the "Bit Twiddling Hacks" by Sean Eron Anderson.
  35. // Since `n` is a signed integer, the generated bytecode will use the SAR opcode to perform the right shift,
  36. // taking advantage of the most significant (or "sign" bit) in two's complement representation.
  37. // This opcode adds new most significant bits set to the value of the previous most significant bit. As a result,
  38. // the mask will either be `bytes(0)` (if n is positive) or `~bytes32(0)` (if n is negative).
  39. int256 mask = n >> 255;
  40. // A `bytes(0)` mask leaves the input unchanged, while a `~bytes32(0)` mask complements it.
  41. return uint256((n + mask) ^ mask);
  42. }
  43. }
  44. }