// SPDX-License-Identifier: MIT pragma solidity 0.7.6; /** * @dev copy from "@openzeppelin/contracts-upgradeable/utils/SafeCastUpgradeable.sol" * and rename to avoid naming conflict with uniswap */ library PerpSafeCast { /** * @dev Returns the downcasted uint128 from uint256, reverting on * overflow (when the input is greater than largest uint128). * * Counterpart to Solidity's `uint128` operator. * * Requirements: * * - input must fit into 128 bits */ function toUint128(uint256 value) internal pure returns (uint128 returnValue) { require(((returnValue = uint128(value)) == value), "SafeCast: value doesn't fit in 128 bits"); } /** * @dev Returns the downcasted uint64 from uint256, reverting on * overflow (when the input is greater than largest uint64). * * Counterpart to Solidity's `uint64` operator. * * Requirements: * * - input must fit into 64 bits */ function toUint64(uint256 value) internal pure returns (uint64 returnValue) { require(((returnValue = uint64(value)) == value), "SafeCast: value doesn't fit in 64 bits"); } /** * @dev Returns the downcasted uint32 from uint256, reverting on * overflow (when the input is greater than largest uint32). * * Counterpart to Solidity's `uint32` operator. * * Requirements: * * - input must fit into 32 bits */ function toUint32(uint256 value) internal pure returns (uint32 returnValue) { require(((returnValue = uint32(value)) == value), "SafeCast: value doesn't fit in 32 bits"); } /** * @dev Returns the downcasted uint24 from uint256, reverting on * overflow (when the input is greater than largest uint24). * * Counterpart to Solidity's `uint24` operator. * * Requirements: * * - input must fit into 24 bits */ function toUint24(uint256 value) internal pure returns (uint24 returnValue) { require(((returnValue = uint24(value)) == value), "SafeCast: value doesn't fit in 24 bits"); } /** * @dev Returns the downcasted uint16 from uint256, reverting on * overflow (when the input is greater than largest uint16). * * Counterpart to Solidity's `uint16` operator. * * Requirements: * * - input must fit into 16 bits */ function toUint16(uint256 value) internal pure returns (uint16 returnValue) { require(((returnValue = uint16(value)) == value), "SafeCast: value doesn't fit in 16 bits"); } /** * @dev Returns the downcasted uint8 from uint256, reverting on * overflow (when the input is greater than largest uint8). * * Counterpart to Solidity's `uint8` operator. * * Requirements: * * - input must fit into 8 bits. */ function toUint8(uint256 value) internal pure returns (uint8 returnValue) { require(((returnValue = uint8(value)) == value), "SafeCast: value doesn't fit in 8 bits"); } /** * @dev Converts a signed int256 into an unsigned uint256. * * Requirements: * * - input must be greater than or equal to 0. */ function toUint256(int256 value) internal pure returns (uint256) { require(value >= 0, "SafeCast: value must be positive"); return uint256(value); } /** * @dev Returns the downcasted int128 from int256, reverting on * overflow (when the input is less than smallest int128 or * greater than largest int128). * * Counterpart to Solidity's `int128` operator. * * Requirements: * * - input must fit into 128 bits * * _Available since v3.1._ */ function toInt128(int256 value) internal pure returns (int128 returnValue) { require(((returnValue = int128(value)) == value), "SafeCast: value doesn't fit in 128 bits"); } /** * @dev Returns the downcasted int64 from int256, reverting on * overflow (when the input is less than smallest int64 or * greater than largest int64). * * Counterpart to Solidity's `int64` operator. * * Requirements: * * - input must fit into 64 bits * * _Available since v3.1._ */ function toInt64(int256 value) internal pure returns (int64 returnValue) { require(((returnValue = int64(value)) == value), "SafeCast: value doesn't fit in 64 bits"); } /** * @dev Returns the downcasted int32 from int256, reverting on * overflow (when the input is less than smallest int32 or * greater than largest int32). * * Counterpart to Solidity's `int32` operator. * * Requirements: * * - input must fit into 32 bits * * _Available since v3.1._ */ function toInt32(int256 value) internal pure returns (int32 returnValue) { require(((returnValue = int32(value)) == value), "SafeCast: value doesn't fit in 32 bits"); } /** * @dev Returns the downcasted int16 from int256, reverting on * overflow (when the input is less than smallest int16 or * greater than largest int16). * * Counterpart to Solidity's `int16` operator. * * Requirements: * * - input must fit into 16 bits * * _Available since v3.1._ */ function toInt16(int256 value) internal pure returns (int16 returnValue) { require(((returnValue = int16(value)) == value), "SafeCast: value doesn't fit in 16 bits"); } /** * @dev Returns the downcasted int8 from int256, reverting on * overflow (when the input is less than smallest int8 or * greater than largest int8). * * Counterpart to Solidity's `int8` operator. * * Requirements: * * - input must fit into 8 bits. * * _Available since v3.1._ */ function toInt8(int256 value) internal pure returns (int8 returnValue) { require(((returnValue = int8(value)) == value), "SafeCast: value doesn't fit in 8 bits"); } /** * @dev Converts an unsigned uint256 into a signed int256. * * Requirements: * * - input must be less than or equal to maxInt256. */ function toInt256(uint256 value) internal pure returns (int256) { require(value <= uint256(type(int256).max), "SafeCast: value doesn't fit in an int256"); return int256(value); } /** * @dev Returns the downcasted uint24 from int256, reverting on * overflow (when the input is greater than largest uint24). * * Counterpart to Solidity's `uint24` operator. * * Requirements: * * - input must be greater than or equal to 0 and into 24 bit. */ function toUint24(int256 value) internal pure returns (uint24 returnValue) { require( ((returnValue = uint24(value)) == value), "SafeCast: value must be positive or value doesn't fit in an 24 bits" ); } /** * @dev Returns the downcasted int24 from int256, reverting on * overflow (when the input is greater than largest int24). * * Counterpart to Solidity's `int24` operator. * * Requirements: * * - input must fit into 24 bits */ function toInt24(int256 value) internal pure returns (int24 returnValue) { require(((returnValue = int24(value)) == value), "SafeCast: value doesn't fit in an 24 bits"); } }