123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- pragma solidity ^0.5.7;
- /**
- * @title SignedSafeMath
- * @dev Signed math operations with safety checks that revert on error.
- */
- library SignedSafeMath {
- int256 constant private INT256_MIN = -2**255;
- /**
- * @dev Multiplies two signed integers, reverts on overflow.
- */
- function mul(int256 a, int256 b) internal pure returns (int256) {
- // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
- // benefit is lost if 'b' is also tested.
- // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
- if (a == 0) {
- return 0;
- }
- require(!(a == -1 && b == INT256_MIN)); // This is the only case of overflow not detected by the check below
- int256 c = a * b;
- require(c / a == b);
- return c;
- }
- /**
- * @dev Integer division of two signed integers truncating the quotient, reverts on division by zero.
- */
- function div(int256 a, int256 b) internal pure returns (int256) {
- require(b != 0); // Solidity only automatically asserts when dividing by 0
- require(!(b == -1 && a == INT256_MIN)); // This is the only case of overflow
- int256 c = a / b;
- return c;
- }
- /**
- * @dev Subtracts two signed integers, reverts on overflow.
- */
- function sub(int256 a, int256 b) internal pure returns (int256) {
- int256 c = a - b;
- require((b >= 0 && c <= a) || (b < 0 && c > a));
- return c;
- }
- /**
- * @dev Adds two signed integers, reverts on overflow.
- */
- function add(int256 a, int256 b) internal pure returns (int256) {
- int256 c = a + b;
- require((b >= 0 && c >= a) || (b < 0 && c < a));
- return c;
- }
- }
|