1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.0.0) (utils/math/SafeCast.sol)
- // This file was procedurally generated from scripts/generate/templates/SafeCast.js.
- pragma solidity ^0.8.20;
- /**
- * @dev Wrappers over Solidity's uintXX/intXX/bool casting operators with added overflow
- * checks.
- *
- * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
- * easily result in undesired exploitation or bugs, since developers usually
- * assume that overflows raise errors. `SafeCast` restores this intuition by
- * reverting the transaction when such an operation overflows.
- *
- * Using this library instead of the unchecked operations eliminates an entire
- * class of bugs, so it's recommended to use it always.
- */
- library SafeCast {
- /**
- * @dev Value doesn't fit in an uint of `bits` size.
- */
- error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);
- /**
- * @dev An int value doesn't fit in an uint of `bits` size.
- */
- error SafeCastOverflowedIntToUint(int256 value);
- /**
- * @dev Value doesn't fit in an int of `bits` size.
- */
- error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);
- /**
- * @dev An uint value doesn't fit in an int of `bits` size.
- */
- error SafeCastOverflowedUintToInt(uint256 value);
- /**
- * @dev Returns the downcasted uint248 from uint256, reverting on
- * overflow (when the input is greater than largest uint248).
- *
- * Counterpart to Solidity's `uint248` operator.
- *
- * Requirements:
- *
- * - input must fit into 248 bits
- */
- function toUint248(uint256 value) internal pure returns (uint248) {
- if (value > type(uint248).max) {
- revert SafeCastOverflowedUintDowncast(248, value);
- }
- return uint248(value);
- }
- /**
- * @dev Returns the downcasted uint240 from uint256, reverting on
- * overflow (when the input is greater than largest uint240).
- *
- * Counterpart to Solidity's `uint240` operator.
- *
- * Requirements:
- *
- * - input must fit into 240 bits
- */
- function toUint240(uint256 value) internal pure returns (uint240) {
- if (value > type(uint240).max) {
- revert SafeCastOverflowedUintDowncast(240, value);
- }
- return uint240(value);
- }
- /**
- * @dev Returns the downcasted uint232 from uint256, reverting on
- * overflow (when the input is greater than largest uint232).
- *
- * Counterpart to Solidity's `uint232` operator.
- *
- * Requirements:
- *
- * - input must fit into 232 bits
- */
- function toUint232(uint256 value) internal pure returns (uint232) {
- if (value > type(uint232).max) {
- revert SafeCastOverflowedUintDowncast(232, value);
- }
- return uint232(value);
- }
- /**
- * @dev Returns the downcasted uint224 from uint256, reverting on
- * overflow (when the input is greater than largest uint224).
- *
- * Counterpart to Solidity's `uint224` operator.
- *
- * Requirements:
- *
- * - input must fit into 224 bits
- */
- function toUint224(uint256 value) internal pure returns (uint224) {
- if (value > type(uint224).max) {
- revert SafeCastOverflowedUintDowncast(224, value);
- }
- return uint224(value);
- }
- /**
- * @dev Returns the downcasted uint216 from uint256, reverting on
- * overflow (when the input is greater than largest uint216).
- *
- * Counterpart to Solidity's `uint216` operator.
- *
- * Requirements:
- *
- * - input must fit into 216 bits
- */
- function toUint216(uint256 value) internal pure returns (uint216) {
- if (value > type(uint216).max) {
- revert SafeCastOverflowedUintDowncast(216, value);
- }
- return uint216(value);
- }
- /**
- * @dev Returns the downcasted uint208 from uint256, reverting on
- * overflow (when the input is greater than largest uint208).
- *
- * Counterpart to Solidity's `uint208` operator.
- *
- * Requirements:
- *
- * - input must fit into 208 bits
- */
- function toUint208(uint256 value) internal pure returns (uint208) {
- if (value > type(uint208).max) {
- revert SafeCastOverflowedUintDowncast(208, value);
- }
- return uint208(value);
- }
- /**
- * @dev Returns the downcasted uint200 from uint256, reverting on
- * overflow (when the input is greater than largest uint200).
- *
- * Counterpart to Solidity's `uint200` operator.
- *
- * Requirements:
- *
- * - input must fit into 200 bits
- */
- function toUint200(uint256 value) internal pure returns (uint200) {
- if (value > type(uint200).max) {
- revert SafeCastOverflowedUintDowncast(200, value);
- }
- return uint200(value);
- }
- /**
- * @dev Returns the downcasted uint192 from uint256, reverting on
- * overflow (when the input is greater than largest uint192).
- *
- * Counterpart to Solidity's `uint192` operator.
- *
- * Requirements:
- *
- * - input must fit into 192 bits
- */
- function toUint192(uint256 value) internal pure returns (uint192) {
- if (value > type(uint192).max) {
- revert SafeCastOverflowedUintDowncast(192, value);
- }
- return uint192(value);
- }
- /**
- * @dev Returns the downcasted uint184 from uint256, reverting on
- * overflow (when the input is greater than largest uint184).
- *
- * Counterpart to Solidity's `uint184` operator.
- *
- * Requirements:
- *
- * - input must fit into 184 bits
- */
- function toUint184(uint256 value) internal pure returns (uint184) {
- if (value > type(uint184).max) {
- revert SafeCastOverflowedUintDowncast(184, value);
- }
- return uint184(value);
- }
- /**
- * @dev Returns the downcasted uint176 from uint256, reverting on
- * overflow (when the input is greater than largest uint176).
- *
- * Counterpart to Solidity's `uint176` operator.
- *
- * Requirements:
- *
- * - input must fit into 176 bits
- */
- function toUint176(uint256 value) internal pure returns (uint176) {
- if (value > type(uint176).max) {
- revert SafeCastOverflowedUintDowncast(176, value);
- }
- return uint176(value);
- }
- /**
- * @dev Returns the downcasted uint168 from uint256, reverting on
- * overflow (when the input is greater than largest uint168).
- *
- * Counterpart to Solidity's `uint168` operator.
- *
- * Requirements:
- *
- * - input must fit into 168 bits
- */
- function toUint168(uint256 value) internal pure returns (uint168) {
- if (value > type(uint168).max) {
- revert SafeCastOverflowedUintDowncast(168, value);
- }
- return uint168(value);
- }
- /**
- * @dev Returns the downcasted uint160 from uint256, reverting on
- * overflow (when the input is greater than largest uint160).
- *
- * Counterpart to Solidity's `uint160` operator.
- *
- * Requirements:
- *
- * - input must fit into 160 bits
- */
- function toUint160(uint256 value) internal pure returns (uint160) {
- if (value > type(uint160).max) {
- revert SafeCastOverflowedUintDowncast(160, value);
- }
- return uint160(value);
- }
- /**
- * @dev Returns the downcasted uint152 from uint256, reverting on
- * overflow (when the input is greater than largest uint152).
- *
- * Counterpart to Solidity's `uint152` operator.
- *
- * Requirements:
- *
- * - input must fit into 152 bits
- */
- function toUint152(uint256 value) internal pure returns (uint152) {
- if (value > type(uint152).max) {
- revert SafeCastOverflowedUintDowncast(152, value);
- }
- return uint152(value);
- }
- /**
- * @dev Returns the downcasted uint144 from uint256, reverting on
- * overflow (when the input is greater than largest uint144).
- *
- * Counterpart to Solidity's `uint144` operator.
- *
- * Requirements:
- *
- * - input must fit into 144 bits
- */
- function toUint144(uint256 value) internal pure returns (uint144) {
- if (value > type(uint144).max) {
- revert SafeCastOverflowedUintDowncast(144, value);
- }
- return uint144(value);
- }
- /**
- * @dev Returns the downcasted uint136 from uint256, reverting on
- * overflow (when the input is greater than largest uint136).
- *
- * Counterpart to Solidity's `uint136` operator.
- *
- * Requirements:
- *
- * - input must fit into 136 bits
- */
- function toUint136(uint256 value) internal pure returns (uint136) {
- if (value > type(uint136).max) {
- revert SafeCastOverflowedUintDowncast(136, value);
- }
- return uint136(value);
- }
- /**
- * @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) {
- if (value > type(uint128).max) {
- revert SafeCastOverflowedUintDowncast(128, value);
- }
- return uint128(value);
- }
- /**
- * @dev Returns the downcasted uint120 from uint256, reverting on
- * overflow (when the input is greater than largest uint120).
- *
- * Counterpart to Solidity's `uint120` operator.
- *
- * Requirements:
- *
- * - input must fit into 120 bits
- */
- function toUint120(uint256 value) internal pure returns (uint120) {
- if (value > type(uint120).max) {
- revert SafeCastOverflowedUintDowncast(120, value);
- }
- return uint120(value);
- }
- /**
- * @dev Returns the downcasted uint112 from uint256, reverting on
- * overflow (when the input is greater than largest uint112).
- *
- * Counterpart to Solidity's `uint112` operator.
- *
- * Requirements:
- *
- * - input must fit into 112 bits
- */
- function toUint112(uint256 value) internal pure returns (uint112) {
- if (value > type(uint112).max) {
- revert SafeCastOverflowedUintDowncast(112, value);
- }
- return uint112(value);
- }
- /**
- * @dev Returns the downcasted uint104 from uint256, reverting on
- * overflow (when the input is greater than largest uint104).
- *
- * Counterpart to Solidity's `uint104` operator.
- *
- * Requirements:
- *
- * - input must fit into 104 bits
- */
- function toUint104(uint256 value) internal pure returns (uint104) {
- if (value > type(uint104).max) {
- revert SafeCastOverflowedUintDowncast(104, value);
- }
- return uint104(value);
- }
- /**
- * @dev Returns the downcasted uint96 from uint256, reverting on
- * overflow (when the input is greater than largest uint96).
- *
- * Counterpart to Solidity's `uint96` operator.
- *
- * Requirements:
- *
- * - input must fit into 96 bits
- */
- function toUint96(uint256 value) internal pure returns (uint96) {
- if (value > type(uint96).max) {
- revert SafeCastOverflowedUintDowncast(96, value);
- }
- return uint96(value);
- }
- /**
- * @dev Returns the downcasted uint88 from uint256, reverting on
- * overflow (when the input is greater than largest uint88).
- *
- * Counterpart to Solidity's `uint88` operator.
- *
- * Requirements:
- *
- * - input must fit into 88 bits
- */
- function toUint88(uint256 value) internal pure returns (uint88) {
- if (value > type(uint88).max) {
- revert SafeCastOverflowedUintDowncast(88, value);
- }
- return uint88(value);
- }
- /**
- * @dev Returns the downcasted uint80 from uint256, reverting on
- * overflow (when the input is greater than largest uint80).
- *
- * Counterpart to Solidity's `uint80` operator.
- *
- * Requirements:
- *
- * - input must fit into 80 bits
- */
- function toUint80(uint256 value) internal pure returns (uint80) {
- if (value > type(uint80).max) {
- revert SafeCastOverflowedUintDowncast(80, value);
- }
- return uint80(value);
- }
- /**
- * @dev Returns the downcasted uint72 from uint256, reverting on
- * overflow (when the input is greater than largest uint72).
- *
- * Counterpart to Solidity's `uint72` operator.
- *
- * Requirements:
- *
- * - input must fit into 72 bits
- */
- function toUint72(uint256 value) internal pure returns (uint72) {
- if (value > type(uint72).max) {
- revert SafeCastOverflowedUintDowncast(72, value);
- }
- return uint72(value);
- }
- /**
- * @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) {
- if (value > type(uint64).max) {
- revert SafeCastOverflowedUintDowncast(64, value);
- }
- return uint64(value);
- }
- /**
- * @dev Returns the downcasted uint56 from uint256, reverting on
- * overflow (when the input is greater than largest uint56).
- *
- * Counterpart to Solidity's `uint56` operator.
- *
- * Requirements:
- *
- * - input must fit into 56 bits
- */
- function toUint56(uint256 value) internal pure returns (uint56) {
- if (value > type(uint56).max) {
- revert SafeCastOverflowedUintDowncast(56, value);
- }
- return uint56(value);
- }
- /**
- * @dev Returns the downcasted uint48 from uint256, reverting on
- * overflow (when the input is greater than largest uint48).
- *
- * Counterpart to Solidity's `uint48` operator.
- *
- * Requirements:
- *
- * - input must fit into 48 bits
- */
- function toUint48(uint256 value) internal pure returns (uint48) {
- if (value > type(uint48).max) {
- revert SafeCastOverflowedUintDowncast(48, value);
- }
- return uint48(value);
- }
- /**
- * @dev Returns the downcasted uint40 from uint256, reverting on
- * overflow (when the input is greater than largest uint40).
- *
- * Counterpart to Solidity's `uint40` operator.
- *
- * Requirements:
- *
- * - input must fit into 40 bits
- */
- function toUint40(uint256 value) internal pure returns (uint40) {
- if (value > type(uint40).max) {
- revert SafeCastOverflowedUintDowncast(40, value);
- }
- return uint40(value);
- }
- /**
- * @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) {
- if (value > type(uint32).max) {
- revert SafeCastOverflowedUintDowncast(32, value);
- }
- return uint32(value);
- }
- /**
- * @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) {
- if (value > type(uint24).max) {
- revert SafeCastOverflowedUintDowncast(24, value);
- }
- return uint24(value);
- }
- /**
- * @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) {
- if (value > type(uint16).max) {
- revert SafeCastOverflowedUintDowncast(16, value);
- }
- return uint16(value);
- }
- /**
- * @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) {
- if (value > type(uint8).max) {
- revert SafeCastOverflowedUintDowncast(8, value);
- }
- return uint8(value);
- }
- /**
- * @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) {
- if (value < 0) {
- revert SafeCastOverflowedIntToUint(value);
- }
- return uint256(value);
- }
- /**
- * @dev Returns the downcasted int248 from int256, reverting on
- * overflow (when the input is less than smallest int248 or
- * greater than largest int248).
- *
- * Counterpart to Solidity's `int248` operator.
- *
- * Requirements:
- *
- * - input must fit into 248 bits
- */
- function toInt248(int256 value) internal pure returns (int248 downcasted) {
- downcasted = int248(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(248, value);
- }
- }
- /**
- * @dev Returns the downcasted int240 from int256, reverting on
- * overflow (when the input is less than smallest int240 or
- * greater than largest int240).
- *
- * Counterpart to Solidity's `int240` operator.
- *
- * Requirements:
- *
- * - input must fit into 240 bits
- */
- function toInt240(int256 value) internal pure returns (int240 downcasted) {
- downcasted = int240(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(240, value);
- }
- }
- /**
- * @dev Returns the downcasted int232 from int256, reverting on
- * overflow (when the input is less than smallest int232 or
- * greater than largest int232).
- *
- * Counterpart to Solidity's `int232` operator.
- *
- * Requirements:
- *
- * - input must fit into 232 bits
- */
- function toInt232(int256 value) internal pure returns (int232 downcasted) {
- downcasted = int232(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(232, value);
- }
- }
- /**
- * @dev Returns the downcasted int224 from int256, reverting on
- * overflow (when the input is less than smallest int224 or
- * greater than largest int224).
- *
- * Counterpart to Solidity's `int224` operator.
- *
- * Requirements:
- *
- * - input must fit into 224 bits
- */
- function toInt224(int256 value) internal pure returns (int224 downcasted) {
- downcasted = int224(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(224, value);
- }
- }
- /**
- * @dev Returns the downcasted int216 from int256, reverting on
- * overflow (when the input is less than smallest int216 or
- * greater than largest int216).
- *
- * Counterpart to Solidity's `int216` operator.
- *
- * Requirements:
- *
- * - input must fit into 216 bits
- */
- function toInt216(int256 value) internal pure returns (int216 downcasted) {
- downcasted = int216(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(216, value);
- }
- }
- /**
- * @dev Returns the downcasted int208 from int256, reverting on
- * overflow (when the input is less than smallest int208 or
- * greater than largest int208).
- *
- * Counterpart to Solidity's `int208` operator.
- *
- * Requirements:
- *
- * - input must fit into 208 bits
- */
- function toInt208(int256 value) internal pure returns (int208 downcasted) {
- downcasted = int208(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(208, value);
- }
- }
- /**
- * @dev Returns the downcasted int200 from int256, reverting on
- * overflow (when the input is less than smallest int200 or
- * greater than largest int200).
- *
- * Counterpart to Solidity's `int200` operator.
- *
- * Requirements:
- *
- * - input must fit into 200 bits
- */
- function toInt200(int256 value) internal pure returns (int200 downcasted) {
- downcasted = int200(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(200, value);
- }
- }
- /**
- * @dev Returns the downcasted int192 from int256, reverting on
- * overflow (when the input is less than smallest int192 or
- * greater than largest int192).
- *
- * Counterpart to Solidity's `int192` operator.
- *
- * Requirements:
- *
- * - input must fit into 192 bits
- */
- function toInt192(int256 value) internal pure returns (int192 downcasted) {
- downcasted = int192(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(192, value);
- }
- }
- /**
- * @dev Returns the downcasted int184 from int256, reverting on
- * overflow (when the input is less than smallest int184 or
- * greater than largest int184).
- *
- * Counterpart to Solidity's `int184` operator.
- *
- * Requirements:
- *
- * - input must fit into 184 bits
- */
- function toInt184(int256 value) internal pure returns (int184 downcasted) {
- downcasted = int184(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(184, value);
- }
- }
- /**
- * @dev Returns the downcasted int176 from int256, reverting on
- * overflow (when the input is less than smallest int176 or
- * greater than largest int176).
- *
- * Counterpart to Solidity's `int176` operator.
- *
- * Requirements:
- *
- * - input must fit into 176 bits
- */
- function toInt176(int256 value) internal pure returns (int176 downcasted) {
- downcasted = int176(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(176, value);
- }
- }
- /**
- * @dev Returns the downcasted int168 from int256, reverting on
- * overflow (when the input is less than smallest int168 or
- * greater than largest int168).
- *
- * Counterpart to Solidity's `int168` operator.
- *
- * Requirements:
- *
- * - input must fit into 168 bits
- */
- function toInt168(int256 value) internal pure returns (int168 downcasted) {
- downcasted = int168(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(168, value);
- }
- }
- /**
- * @dev Returns the downcasted int160 from int256, reverting on
- * overflow (when the input is less than smallest int160 or
- * greater than largest int160).
- *
- * Counterpart to Solidity's `int160` operator.
- *
- * Requirements:
- *
- * - input must fit into 160 bits
- */
- function toInt160(int256 value) internal pure returns (int160 downcasted) {
- downcasted = int160(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(160, value);
- }
- }
- /**
- * @dev Returns the downcasted int152 from int256, reverting on
- * overflow (when the input is less than smallest int152 or
- * greater than largest int152).
- *
- * Counterpart to Solidity's `int152` operator.
- *
- * Requirements:
- *
- * - input must fit into 152 bits
- */
- function toInt152(int256 value) internal pure returns (int152 downcasted) {
- downcasted = int152(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(152, value);
- }
- }
- /**
- * @dev Returns the downcasted int144 from int256, reverting on
- * overflow (when the input is less than smallest int144 or
- * greater than largest int144).
- *
- * Counterpart to Solidity's `int144` operator.
- *
- * Requirements:
- *
- * - input must fit into 144 bits
- */
- function toInt144(int256 value) internal pure returns (int144 downcasted) {
- downcasted = int144(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(144, value);
- }
- }
- /**
- * @dev Returns the downcasted int136 from int256, reverting on
- * overflow (when the input is less than smallest int136 or
- * greater than largest int136).
- *
- * Counterpart to Solidity's `int136` operator.
- *
- * Requirements:
- *
- * - input must fit into 136 bits
- */
- function toInt136(int256 value) internal pure returns (int136 downcasted) {
- downcasted = int136(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(136, 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
- */
- function toInt128(int256 value) internal pure returns (int128 downcasted) {
- downcasted = int128(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(128, value);
- }
- }
- /**
- * @dev Returns the downcasted int120 from int256, reverting on
- * overflow (when the input is less than smallest int120 or
- * greater than largest int120).
- *
- * Counterpart to Solidity's `int120` operator.
- *
- * Requirements:
- *
- * - input must fit into 120 bits
- */
- function toInt120(int256 value) internal pure returns (int120 downcasted) {
- downcasted = int120(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(120, value);
- }
- }
- /**
- * @dev Returns the downcasted int112 from int256, reverting on
- * overflow (when the input is less than smallest int112 or
- * greater than largest int112).
- *
- * Counterpart to Solidity's `int112` operator.
- *
- * Requirements:
- *
- * - input must fit into 112 bits
- */
- function toInt112(int256 value) internal pure returns (int112 downcasted) {
- downcasted = int112(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(112, value);
- }
- }
- /**
- * @dev Returns the downcasted int104 from int256, reverting on
- * overflow (when the input is less than smallest int104 or
- * greater than largest int104).
- *
- * Counterpart to Solidity's `int104` operator.
- *
- * Requirements:
- *
- * - input must fit into 104 bits
- */
- function toInt104(int256 value) internal pure returns (int104 downcasted) {
- downcasted = int104(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(104, value);
- }
- }
- /**
- * @dev Returns the downcasted int96 from int256, reverting on
- * overflow (when the input is less than smallest int96 or
- * greater than largest int96).
- *
- * Counterpart to Solidity's `int96` operator.
- *
- * Requirements:
- *
- * - input must fit into 96 bits
- */
- function toInt96(int256 value) internal pure returns (int96 downcasted) {
- downcasted = int96(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(96, value);
- }
- }
- /**
- * @dev Returns the downcasted int88 from int256, reverting on
- * overflow (when the input is less than smallest int88 or
- * greater than largest int88).
- *
- * Counterpart to Solidity's `int88` operator.
- *
- * Requirements:
- *
- * - input must fit into 88 bits
- */
- function toInt88(int256 value) internal pure returns (int88 downcasted) {
- downcasted = int88(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(88, value);
- }
- }
- /**
- * @dev Returns the downcasted int80 from int256, reverting on
- * overflow (when the input is less than smallest int80 or
- * greater than largest int80).
- *
- * Counterpart to Solidity's `int80` operator.
- *
- * Requirements:
- *
- * - input must fit into 80 bits
- */
- function toInt80(int256 value) internal pure returns (int80 downcasted) {
- downcasted = int80(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(80, value);
- }
- }
- /**
- * @dev Returns the downcasted int72 from int256, reverting on
- * overflow (when the input is less than smallest int72 or
- * greater than largest int72).
- *
- * Counterpart to Solidity's `int72` operator.
- *
- * Requirements:
- *
- * - input must fit into 72 bits
- */
- function toInt72(int256 value) internal pure returns (int72 downcasted) {
- downcasted = int72(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(72, value);
- }
- }
- /**
- * @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
- */
- function toInt64(int256 value) internal pure returns (int64 downcasted) {
- downcasted = int64(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(64, value);
- }
- }
- /**
- * @dev Returns the downcasted int56 from int256, reverting on
- * overflow (when the input is less than smallest int56 or
- * greater than largest int56).
- *
- * Counterpart to Solidity's `int56` operator.
- *
- * Requirements:
- *
- * - input must fit into 56 bits
- */
- function toInt56(int256 value) internal pure returns (int56 downcasted) {
- downcasted = int56(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(56, value);
- }
- }
- /**
- * @dev Returns the downcasted int48 from int256, reverting on
- * overflow (when the input is less than smallest int48 or
- * greater than largest int48).
- *
- * Counterpart to Solidity's `int48` operator.
- *
- * Requirements:
- *
- * - input must fit into 48 bits
- */
- function toInt48(int256 value) internal pure returns (int48 downcasted) {
- downcasted = int48(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(48, value);
- }
- }
- /**
- * @dev Returns the downcasted int40 from int256, reverting on
- * overflow (when the input is less than smallest int40 or
- * greater than largest int40).
- *
- * Counterpart to Solidity's `int40` operator.
- *
- * Requirements:
- *
- * - input must fit into 40 bits
- */
- function toInt40(int256 value) internal pure returns (int40 downcasted) {
- downcasted = int40(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(40, value);
- }
- }
- /**
- * @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
- */
- function toInt32(int256 value) internal pure returns (int32 downcasted) {
- downcasted = int32(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(32, value);
- }
- }
- /**
- * @dev Returns the downcasted int24 from int256, reverting on
- * overflow (when the input is less than smallest int24 or
- * 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 downcasted) {
- downcasted = int24(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(24, value);
- }
- }
- /**
- * @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
- */
- function toInt16(int256 value) internal pure returns (int16 downcasted) {
- downcasted = int16(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(16, value);
- }
- }
- /**
- * @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
- */
- function toInt8(int256 value) internal pure returns (int8 downcasted) {
- downcasted = int8(value);
- if (downcasted != value) {
- revert SafeCastOverflowedIntDowncast(8, value);
- }
- }
- /**
- * @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) {
- // Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
- if (value > uint256(type(int256).max)) {
- revert SafeCastOverflowedUintToInt(value);
- }
- return int256(value);
- }
- /**
- * @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.
- */
- function toUint(bool b) internal pure returns (uint256 u) {
- assembly ("memory-safe") {
- u := iszero(iszero(b))
- }
- }
- }
|