SafeCast.sol 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SafeCast.sol)
  3. // This file was procedurally generated from scripts/generate/templates/SafeCast.js.
  4. pragma solidity ^0.8.19;
  5. /**
  6. * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
  7. * checks.
  8. *
  9. * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
  10. * easily result in undesired exploitation or bugs, since developers usually
  11. * assume that overflows raise errors. `SafeCast` restores this intuition by
  12. * reverting the transaction when such an operation overflows.
  13. *
  14. * Using this library instead of the unchecked operations eliminates an entire
  15. * class of bugs, so it's recommended to use it always.
  16. */
  17. library SafeCast {
  18. /**
  19. * @dev Value doesn't fit in an uint of `bits` size.
  20. */
  21. error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);
  22. /**
  23. * @dev An int value doesn't fit in an uint of `bits` size.
  24. *
  25. * NOTE: The `bits` argument is `min(bits, 255)`.
  26. */
  27. error SafeCastOverflowedIntToUint(uint8 bits, int256 value);
  28. /**
  29. * @dev Value doesn't fit in an int of `bits` size.
  30. */
  31. error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);
  32. /**
  33. * @dev An uint value doesn't fit in an int of `bits` size.
  34. *
  35. * NOTE: The `bits` argument is `min(bits, 255)`.
  36. */
  37. error SafeCastOverflowedUintToInt(uint8 bits, uint256 value);
  38. /**
  39. * @dev Returns the downcasted uint248 from uint256, reverting on
  40. * overflow (when the input is greater than largest uint248).
  41. *
  42. * Counterpart to Solidity's `uint248` operator.
  43. *
  44. * Requirements:
  45. *
  46. * - input must fit into 248 bits
  47. *
  48. * _Available since v4.7._
  49. */
  50. function toUint248(uint256 value) internal pure returns (uint248) {
  51. if (value > type(uint248).max) {
  52. revert SafeCastOverflowedUintDowncast(248, value);
  53. }
  54. return uint248(value);
  55. }
  56. /**
  57. * @dev Returns the downcasted uint240 from uint256, reverting on
  58. * overflow (when the input is greater than largest uint240).
  59. *
  60. * Counterpart to Solidity's `uint240` operator.
  61. *
  62. * Requirements:
  63. *
  64. * - input must fit into 240 bits
  65. *
  66. * _Available since v4.7._
  67. */
  68. function toUint240(uint256 value) internal pure returns (uint240) {
  69. if (value > type(uint240).max) {
  70. revert SafeCastOverflowedUintDowncast(240, value);
  71. }
  72. return uint240(value);
  73. }
  74. /**
  75. * @dev Returns the downcasted uint232 from uint256, reverting on
  76. * overflow (when the input is greater than largest uint232).
  77. *
  78. * Counterpart to Solidity's `uint232` operator.
  79. *
  80. * Requirements:
  81. *
  82. * - input must fit into 232 bits
  83. *
  84. * _Available since v4.7._
  85. */
  86. function toUint232(uint256 value) internal pure returns (uint232) {
  87. if (value > type(uint232).max) {
  88. revert SafeCastOverflowedUintDowncast(232, value);
  89. }
  90. return uint232(value);
  91. }
  92. /**
  93. * @dev Returns the downcasted uint224 from uint256, reverting on
  94. * overflow (when the input is greater than largest uint224).
  95. *
  96. * Counterpart to Solidity's `uint224` operator.
  97. *
  98. * Requirements:
  99. *
  100. * - input must fit into 224 bits
  101. *
  102. * _Available since v4.2._
  103. */
  104. function toUint224(uint256 value) internal pure returns (uint224) {
  105. if (value > type(uint224).max) {
  106. revert SafeCastOverflowedUintDowncast(224, value);
  107. }
  108. return uint224(value);
  109. }
  110. /**
  111. * @dev Returns the downcasted uint216 from uint256, reverting on
  112. * overflow (when the input is greater than largest uint216).
  113. *
  114. * Counterpart to Solidity's `uint216` operator.
  115. *
  116. * Requirements:
  117. *
  118. * - input must fit into 216 bits
  119. *
  120. * _Available since v4.7._
  121. */
  122. function toUint216(uint256 value) internal pure returns (uint216) {
  123. if (value > type(uint216).max) {
  124. revert SafeCastOverflowedUintDowncast(216, value);
  125. }
  126. return uint216(value);
  127. }
  128. /**
  129. * @dev Returns the downcasted uint208 from uint256, reverting on
  130. * overflow (when the input is greater than largest uint208).
  131. *
  132. * Counterpart to Solidity's `uint208` operator.
  133. *
  134. * Requirements:
  135. *
  136. * - input must fit into 208 bits
  137. *
  138. * _Available since v4.7._
  139. */
  140. function toUint208(uint256 value) internal pure returns (uint208) {
  141. if (value > type(uint208).max) {
  142. revert SafeCastOverflowedUintDowncast(208, value);
  143. }
  144. return uint208(value);
  145. }
  146. /**
  147. * @dev Returns the downcasted uint200 from uint256, reverting on
  148. * overflow (when the input is greater than largest uint200).
  149. *
  150. * Counterpart to Solidity's `uint200` operator.
  151. *
  152. * Requirements:
  153. *
  154. * - input must fit into 200 bits
  155. *
  156. * _Available since v4.7._
  157. */
  158. function toUint200(uint256 value) internal pure returns (uint200) {
  159. if (value > type(uint200).max) {
  160. revert SafeCastOverflowedUintDowncast(200, value);
  161. }
  162. return uint200(value);
  163. }
  164. /**
  165. * @dev Returns the downcasted uint192 from uint256, reverting on
  166. * overflow (when the input is greater than largest uint192).
  167. *
  168. * Counterpart to Solidity's `uint192` operator.
  169. *
  170. * Requirements:
  171. *
  172. * - input must fit into 192 bits
  173. *
  174. * _Available since v4.7._
  175. */
  176. function toUint192(uint256 value) internal pure returns (uint192) {
  177. if (value > type(uint192).max) {
  178. revert SafeCastOverflowedUintDowncast(192, value);
  179. }
  180. return uint192(value);
  181. }
  182. /**
  183. * @dev Returns the downcasted uint184 from uint256, reverting on
  184. * overflow (when the input is greater than largest uint184).
  185. *
  186. * Counterpart to Solidity's `uint184` operator.
  187. *
  188. * Requirements:
  189. *
  190. * - input must fit into 184 bits
  191. *
  192. * _Available since v4.7._
  193. */
  194. function toUint184(uint256 value) internal pure returns (uint184) {
  195. if (value > type(uint184).max) {
  196. revert SafeCastOverflowedUintDowncast(184, value);
  197. }
  198. return uint184(value);
  199. }
  200. /**
  201. * @dev Returns the downcasted uint176 from uint256, reverting on
  202. * overflow (when the input is greater than largest uint176).
  203. *
  204. * Counterpart to Solidity's `uint176` operator.
  205. *
  206. * Requirements:
  207. *
  208. * - input must fit into 176 bits
  209. *
  210. * _Available since v4.7._
  211. */
  212. function toUint176(uint256 value) internal pure returns (uint176) {
  213. if (value > type(uint176).max) {
  214. revert SafeCastOverflowedUintDowncast(176, value);
  215. }
  216. return uint176(value);
  217. }
  218. /**
  219. * @dev Returns the downcasted uint168 from uint256, reverting on
  220. * overflow (when the input is greater than largest uint168).
  221. *
  222. * Counterpart to Solidity's `uint168` operator.
  223. *
  224. * Requirements:
  225. *
  226. * - input must fit into 168 bits
  227. *
  228. * _Available since v4.7._
  229. */
  230. function toUint168(uint256 value) internal pure returns (uint168) {
  231. if (value > type(uint168).max) {
  232. revert SafeCastOverflowedUintDowncast(168, value);
  233. }
  234. return uint168(value);
  235. }
  236. /**
  237. * @dev Returns the downcasted uint160 from uint256, reverting on
  238. * overflow (when the input is greater than largest uint160).
  239. *
  240. * Counterpart to Solidity's `uint160` operator.
  241. *
  242. * Requirements:
  243. *
  244. * - input must fit into 160 bits
  245. *
  246. * _Available since v4.7._
  247. */
  248. function toUint160(uint256 value) internal pure returns (uint160) {
  249. if (value > type(uint160).max) {
  250. revert SafeCastOverflowedUintDowncast(160, value);
  251. }
  252. return uint160(value);
  253. }
  254. /**
  255. * @dev Returns the downcasted uint152 from uint256, reverting on
  256. * overflow (when the input is greater than largest uint152).
  257. *
  258. * Counterpart to Solidity's `uint152` operator.
  259. *
  260. * Requirements:
  261. *
  262. * - input must fit into 152 bits
  263. *
  264. * _Available since v4.7._
  265. */
  266. function toUint152(uint256 value) internal pure returns (uint152) {
  267. if (value > type(uint152).max) {
  268. revert SafeCastOverflowedUintDowncast(152, value);
  269. }
  270. return uint152(value);
  271. }
  272. /**
  273. * @dev Returns the downcasted uint144 from uint256, reverting on
  274. * overflow (when the input is greater than largest uint144).
  275. *
  276. * Counterpart to Solidity's `uint144` operator.
  277. *
  278. * Requirements:
  279. *
  280. * - input must fit into 144 bits
  281. *
  282. * _Available since v4.7._
  283. */
  284. function toUint144(uint256 value) internal pure returns (uint144) {
  285. if (value > type(uint144).max) {
  286. revert SafeCastOverflowedUintDowncast(144, value);
  287. }
  288. return uint144(value);
  289. }
  290. /**
  291. * @dev Returns the downcasted uint136 from uint256, reverting on
  292. * overflow (when the input is greater than largest uint136).
  293. *
  294. * Counterpart to Solidity's `uint136` operator.
  295. *
  296. * Requirements:
  297. *
  298. * - input must fit into 136 bits
  299. *
  300. * _Available since v4.7._
  301. */
  302. function toUint136(uint256 value) internal pure returns (uint136) {
  303. if (value > type(uint136).max) {
  304. revert SafeCastOverflowedUintDowncast(136, value);
  305. }
  306. return uint136(value);
  307. }
  308. /**
  309. * @dev Returns the downcasted uint128 from uint256, reverting on
  310. * overflow (when the input is greater than largest uint128).
  311. *
  312. * Counterpart to Solidity's `uint128` operator.
  313. *
  314. * Requirements:
  315. *
  316. * - input must fit into 128 bits
  317. *
  318. * _Available since v2.5._
  319. */
  320. function toUint128(uint256 value) internal pure returns (uint128) {
  321. if (value > type(uint128).max) {
  322. revert SafeCastOverflowedUintDowncast(128, value);
  323. }
  324. return uint128(value);
  325. }
  326. /**
  327. * @dev Returns the downcasted uint120 from uint256, reverting on
  328. * overflow (when the input is greater than largest uint120).
  329. *
  330. * Counterpart to Solidity's `uint120` operator.
  331. *
  332. * Requirements:
  333. *
  334. * - input must fit into 120 bits
  335. *
  336. * _Available since v4.7._
  337. */
  338. function toUint120(uint256 value) internal pure returns (uint120) {
  339. if (value > type(uint120).max) {
  340. revert SafeCastOverflowedUintDowncast(120, value);
  341. }
  342. return uint120(value);
  343. }
  344. /**
  345. * @dev Returns the downcasted uint112 from uint256, reverting on
  346. * overflow (when the input is greater than largest uint112).
  347. *
  348. * Counterpart to Solidity's `uint112` operator.
  349. *
  350. * Requirements:
  351. *
  352. * - input must fit into 112 bits
  353. *
  354. * _Available since v4.7._
  355. */
  356. function toUint112(uint256 value) internal pure returns (uint112) {
  357. if (value > type(uint112).max) {
  358. revert SafeCastOverflowedUintDowncast(112, value);
  359. }
  360. return uint112(value);
  361. }
  362. /**
  363. * @dev Returns the downcasted uint104 from uint256, reverting on
  364. * overflow (when the input is greater than largest uint104).
  365. *
  366. * Counterpart to Solidity's `uint104` operator.
  367. *
  368. * Requirements:
  369. *
  370. * - input must fit into 104 bits
  371. *
  372. * _Available since v4.7._
  373. */
  374. function toUint104(uint256 value) internal pure returns (uint104) {
  375. if (value > type(uint104).max) {
  376. revert SafeCastOverflowedUintDowncast(104, value);
  377. }
  378. return uint104(value);
  379. }
  380. /**
  381. * @dev Returns the downcasted uint96 from uint256, reverting on
  382. * overflow (when the input is greater than largest uint96).
  383. *
  384. * Counterpart to Solidity's `uint96` operator.
  385. *
  386. * Requirements:
  387. *
  388. * - input must fit into 96 bits
  389. *
  390. * _Available since v4.2._
  391. */
  392. function toUint96(uint256 value) internal pure returns (uint96) {
  393. if (value > type(uint96).max) {
  394. revert SafeCastOverflowedUintDowncast(96, value);
  395. }
  396. return uint96(value);
  397. }
  398. /**
  399. * @dev Returns the downcasted uint88 from uint256, reverting on
  400. * overflow (when the input is greater than largest uint88).
  401. *
  402. * Counterpart to Solidity's `uint88` operator.
  403. *
  404. * Requirements:
  405. *
  406. * - input must fit into 88 bits
  407. *
  408. * _Available since v4.7._
  409. */
  410. function toUint88(uint256 value) internal pure returns (uint88) {
  411. if (value > type(uint88).max) {
  412. revert SafeCastOverflowedUintDowncast(88, value);
  413. }
  414. return uint88(value);
  415. }
  416. /**
  417. * @dev Returns the downcasted uint80 from uint256, reverting on
  418. * overflow (when the input is greater than largest uint80).
  419. *
  420. * Counterpart to Solidity's `uint80` operator.
  421. *
  422. * Requirements:
  423. *
  424. * - input must fit into 80 bits
  425. *
  426. * _Available since v4.7._
  427. */
  428. function toUint80(uint256 value) internal pure returns (uint80) {
  429. if (value > type(uint80).max) {
  430. revert SafeCastOverflowedUintDowncast(80, value);
  431. }
  432. return uint80(value);
  433. }
  434. /**
  435. * @dev Returns the downcasted uint72 from uint256, reverting on
  436. * overflow (when the input is greater than largest uint72).
  437. *
  438. * Counterpart to Solidity's `uint72` operator.
  439. *
  440. * Requirements:
  441. *
  442. * - input must fit into 72 bits
  443. *
  444. * _Available since v4.7._
  445. */
  446. function toUint72(uint256 value) internal pure returns (uint72) {
  447. if (value > type(uint72).max) {
  448. revert SafeCastOverflowedUintDowncast(72, value);
  449. }
  450. return uint72(value);
  451. }
  452. /**
  453. * @dev Returns the downcasted uint64 from uint256, reverting on
  454. * overflow (when the input is greater than largest uint64).
  455. *
  456. * Counterpart to Solidity's `uint64` operator.
  457. *
  458. * Requirements:
  459. *
  460. * - input must fit into 64 bits
  461. *
  462. * _Available since v2.5._
  463. */
  464. function toUint64(uint256 value) internal pure returns (uint64) {
  465. if (value > type(uint64).max) {
  466. revert SafeCastOverflowedUintDowncast(64, value);
  467. }
  468. return uint64(value);
  469. }
  470. /**
  471. * @dev Returns the downcasted uint56 from uint256, reverting on
  472. * overflow (when the input is greater than largest uint56).
  473. *
  474. * Counterpart to Solidity's `uint56` operator.
  475. *
  476. * Requirements:
  477. *
  478. * - input must fit into 56 bits
  479. *
  480. * _Available since v4.7._
  481. */
  482. function toUint56(uint256 value) internal pure returns (uint56) {
  483. if (value > type(uint56).max) {
  484. revert SafeCastOverflowedUintDowncast(56, value);
  485. }
  486. return uint56(value);
  487. }
  488. /**
  489. * @dev Returns the downcasted uint48 from uint256, reverting on
  490. * overflow (when the input is greater than largest uint48).
  491. *
  492. * Counterpart to Solidity's `uint48` operator.
  493. *
  494. * Requirements:
  495. *
  496. * - input must fit into 48 bits
  497. *
  498. * _Available since v4.7._
  499. */
  500. function toUint48(uint256 value) internal pure returns (uint48) {
  501. if (value > type(uint48).max) {
  502. revert SafeCastOverflowedUintDowncast(48, value);
  503. }
  504. return uint48(value);
  505. }
  506. /**
  507. * @dev Returns the downcasted uint40 from uint256, reverting on
  508. * overflow (when the input is greater than largest uint40).
  509. *
  510. * Counterpart to Solidity's `uint40` operator.
  511. *
  512. * Requirements:
  513. *
  514. * - input must fit into 40 bits
  515. *
  516. * _Available since v4.7._
  517. */
  518. function toUint40(uint256 value) internal pure returns (uint40) {
  519. if (value > type(uint40).max) {
  520. revert SafeCastOverflowedUintDowncast(40, value);
  521. }
  522. return uint40(value);
  523. }
  524. /**
  525. * @dev Returns the downcasted uint32 from uint256, reverting on
  526. * overflow (when the input is greater than largest uint32).
  527. *
  528. * Counterpart to Solidity's `uint32` operator.
  529. *
  530. * Requirements:
  531. *
  532. * - input must fit into 32 bits
  533. *
  534. * _Available since v2.5._
  535. */
  536. function toUint32(uint256 value) internal pure returns (uint32) {
  537. if (value > type(uint32).max) {
  538. revert SafeCastOverflowedUintDowncast(32, value);
  539. }
  540. return uint32(value);
  541. }
  542. /**
  543. * @dev Returns the downcasted uint24 from uint256, reverting on
  544. * overflow (when the input is greater than largest uint24).
  545. *
  546. * Counterpart to Solidity's `uint24` operator.
  547. *
  548. * Requirements:
  549. *
  550. * - input must fit into 24 bits
  551. *
  552. * _Available since v4.7._
  553. */
  554. function toUint24(uint256 value) internal pure returns (uint24) {
  555. if (value > type(uint24).max) {
  556. revert SafeCastOverflowedUintDowncast(24, value);
  557. }
  558. return uint24(value);
  559. }
  560. /**
  561. * @dev Returns the downcasted uint16 from uint256, reverting on
  562. * overflow (when the input is greater than largest uint16).
  563. *
  564. * Counterpart to Solidity's `uint16` operator.
  565. *
  566. * Requirements:
  567. *
  568. * - input must fit into 16 bits
  569. *
  570. * _Available since v2.5._
  571. */
  572. function toUint16(uint256 value) internal pure returns (uint16) {
  573. if (value > type(uint16).max) {
  574. revert SafeCastOverflowedUintDowncast(16, value);
  575. }
  576. return uint16(value);
  577. }
  578. /**
  579. * @dev Returns the downcasted uint8 from uint256, reverting on
  580. * overflow (when the input is greater than largest uint8).
  581. *
  582. * Counterpart to Solidity's `uint8` operator.
  583. *
  584. * Requirements:
  585. *
  586. * - input must fit into 8 bits
  587. *
  588. * _Available since v2.5._
  589. */
  590. function toUint8(uint256 value) internal pure returns (uint8) {
  591. if (value > type(uint8).max) {
  592. revert SafeCastOverflowedUintDowncast(8, value);
  593. }
  594. return uint8(value);
  595. }
  596. /**
  597. * @dev Converts a signed int256 into an unsigned uint256.
  598. *
  599. * Requirements:
  600. *
  601. * - input must be greater than or equal to 0.
  602. *
  603. * _Available since v3.0._
  604. */
  605. function toUint256(int256 value) internal pure returns (uint256) {
  606. if (value < 0) {
  607. revert SafeCastOverflowedIntToUint(255, value);
  608. }
  609. return uint256(value);
  610. }
  611. /**
  612. * @dev Returns the downcasted int248 from int256, reverting on
  613. * overflow (when the input is less than smallest int248 or
  614. * greater than largest int248).
  615. *
  616. * Counterpart to Solidity's `int248` operator.
  617. *
  618. * Requirements:
  619. *
  620. * - input must fit into 248 bits
  621. *
  622. * _Available since v4.7._
  623. */
  624. function toInt248(int256 value) internal pure returns (int248 downcasted) {
  625. downcasted = int248(value);
  626. if (downcasted != value) {
  627. revert SafeCastOverflowedIntDowncast(248, value);
  628. }
  629. }
  630. /**
  631. * @dev Returns the downcasted int240 from int256, reverting on
  632. * overflow (when the input is less than smallest int240 or
  633. * greater than largest int240).
  634. *
  635. * Counterpart to Solidity's `int240` operator.
  636. *
  637. * Requirements:
  638. *
  639. * - input must fit into 240 bits
  640. *
  641. * _Available since v4.7._
  642. */
  643. function toInt240(int256 value) internal pure returns (int240 downcasted) {
  644. downcasted = int240(value);
  645. if (downcasted != value) {
  646. revert SafeCastOverflowedIntDowncast(240, value);
  647. }
  648. }
  649. /**
  650. * @dev Returns the downcasted int232 from int256, reverting on
  651. * overflow (when the input is less than smallest int232 or
  652. * greater than largest int232).
  653. *
  654. * Counterpart to Solidity's `int232` operator.
  655. *
  656. * Requirements:
  657. *
  658. * - input must fit into 232 bits
  659. *
  660. * _Available since v4.7._
  661. */
  662. function toInt232(int256 value) internal pure returns (int232 downcasted) {
  663. downcasted = int232(value);
  664. if (downcasted != value) {
  665. revert SafeCastOverflowedIntDowncast(232, value);
  666. }
  667. }
  668. /**
  669. * @dev Returns the downcasted int224 from int256, reverting on
  670. * overflow (when the input is less than smallest int224 or
  671. * greater than largest int224).
  672. *
  673. * Counterpart to Solidity's `int224` operator.
  674. *
  675. * Requirements:
  676. *
  677. * - input must fit into 224 bits
  678. *
  679. * _Available since v4.7._
  680. */
  681. function toInt224(int256 value) internal pure returns (int224 downcasted) {
  682. downcasted = int224(value);
  683. if (downcasted != value) {
  684. revert SafeCastOverflowedIntDowncast(224, value);
  685. }
  686. }
  687. /**
  688. * @dev Returns the downcasted int216 from int256, reverting on
  689. * overflow (when the input is less than smallest int216 or
  690. * greater than largest int216).
  691. *
  692. * Counterpart to Solidity's `int216` operator.
  693. *
  694. * Requirements:
  695. *
  696. * - input must fit into 216 bits
  697. *
  698. * _Available since v4.7._
  699. */
  700. function toInt216(int256 value) internal pure returns (int216 downcasted) {
  701. downcasted = int216(value);
  702. if (downcasted != value) {
  703. revert SafeCastOverflowedIntDowncast(216, value);
  704. }
  705. }
  706. /**
  707. * @dev Returns the downcasted int208 from int256, reverting on
  708. * overflow (when the input is less than smallest int208 or
  709. * greater than largest int208).
  710. *
  711. * Counterpart to Solidity's `int208` operator.
  712. *
  713. * Requirements:
  714. *
  715. * - input must fit into 208 bits
  716. *
  717. * _Available since v4.7._
  718. */
  719. function toInt208(int256 value) internal pure returns (int208 downcasted) {
  720. downcasted = int208(value);
  721. if (downcasted != value) {
  722. revert SafeCastOverflowedIntDowncast(208, value);
  723. }
  724. }
  725. /**
  726. * @dev Returns the downcasted int200 from int256, reverting on
  727. * overflow (when the input is less than smallest int200 or
  728. * greater than largest int200).
  729. *
  730. * Counterpart to Solidity's `int200` operator.
  731. *
  732. * Requirements:
  733. *
  734. * - input must fit into 200 bits
  735. *
  736. * _Available since v4.7._
  737. */
  738. function toInt200(int256 value) internal pure returns (int200 downcasted) {
  739. downcasted = int200(value);
  740. if (downcasted != value) {
  741. revert SafeCastOverflowedIntDowncast(200, value);
  742. }
  743. }
  744. /**
  745. * @dev Returns the downcasted int192 from int256, reverting on
  746. * overflow (when the input is less than smallest int192 or
  747. * greater than largest int192).
  748. *
  749. * Counterpart to Solidity's `int192` operator.
  750. *
  751. * Requirements:
  752. *
  753. * - input must fit into 192 bits
  754. *
  755. * _Available since v4.7._
  756. */
  757. function toInt192(int256 value) internal pure returns (int192 downcasted) {
  758. downcasted = int192(value);
  759. if (downcasted != value) {
  760. revert SafeCastOverflowedIntDowncast(192, value);
  761. }
  762. }
  763. /**
  764. * @dev Returns the downcasted int184 from int256, reverting on
  765. * overflow (when the input is less than smallest int184 or
  766. * greater than largest int184).
  767. *
  768. * Counterpart to Solidity's `int184` operator.
  769. *
  770. * Requirements:
  771. *
  772. * - input must fit into 184 bits
  773. *
  774. * _Available since v4.7._
  775. */
  776. function toInt184(int256 value) internal pure returns (int184 downcasted) {
  777. downcasted = int184(value);
  778. if (downcasted != value) {
  779. revert SafeCastOverflowedIntDowncast(184, value);
  780. }
  781. }
  782. /**
  783. * @dev Returns the downcasted int176 from int256, reverting on
  784. * overflow (when the input is less than smallest int176 or
  785. * greater than largest int176).
  786. *
  787. * Counterpart to Solidity's `int176` operator.
  788. *
  789. * Requirements:
  790. *
  791. * - input must fit into 176 bits
  792. *
  793. * _Available since v4.7._
  794. */
  795. function toInt176(int256 value) internal pure returns (int176 downcasted) {
  796. downcasted = int176(value);
  797. if (downcasted != value) {
  798. revert SafeCastOverflowedIntDowncast(176, value);
  799. }
  800. }
  801. /**
  802. * @dev Returns the downcasted int168 from int256, reverting on
  803. * overflow (when the input is less than smallest int168 or
  804. * greater than largest int168).
  805. *
  806. * Counterpart to Solidity's `int168` operator.
  807. *
  808. * Requirements:
  809. *
  810. * - input must fit into 168 bits
  811. *
  812. * _Available since v4.7._
  813. */
  814. function toInt168(int256 value) internal pure returns (int168 downcasted) {
  815. downcasted = int168(value);
  816. if (downcasted != value) {
  817. revert SafeCastOverflowedIntDowncast(168, value);
  818. }
  819. }
  820. /**
  821. * @dev Returns the downcasted int160 from int256, reverting on
  822. * overflow (when the input is less than smallest int160 or
  823. * greater than largest int160).
  824. *
  825. * Counterpart to Solidity's `int160` operator.
  826. *
  827. * Requirements:
  828. *
  829. * - input must fit into 160 bits
  830. *
  831. * _Available since v4.7._
  832. */
  833. function toInt160(int256 value) internal pure returns (int160 downcasted) {
  834. downcasted = int160(value);
  835. if (downcasted != value) {
  836. revert SafeCastOverflowedIntDowncast(160, value);
  837. }
  838. }
  839. /**
  840. * @dev Returns the downcasted int152 from int256, reverting on
  841. * overflow (when the input is less than smallest int152 or
  842. * greater than largest int152).
  843. *
  844. * Counterpart to Solidity's `int152` operator.
  845. *
  846. * Requirements:
  847. *
  848. * - input must fit into 152 bits
  849. *
  850. * _Available since v4.7._
  851. */
  852. function toInt152(int256 value) internal pure returns (int152 downcasted) {
  853. downcasted = int152(value);
  854. if (downcasted != value) {
  855. revert SafeCastOverflowedIntDowncast(152, value);
  856. }
  857. }
  858. /**
  859. * @dev Returns the downcasted int144 from int256, reverting on
  860. * overflow (when the input is less than smallest int144 or
  861. * greater than largest int144).
  862. *
  863. * Counterpart to Solidity's `int144` operator.
  864. *
  865. * Requirements:
  866. *
  867. * - input must fit into 144 bits
  868. *
  869. * _Available since v4.7._
  870. */
  871. function toInt144(int256 value) internal pure returns (int144 downcasted) {
  872. downcasted = int144(value);
  873. if (downcasted != value) {
  874. revert SafeCastOverflowedIntDowncast(144, value);
  875. }
  876. }
  877. /**
  878. * @dev Returns the downcasted int136 from int256, reverting on
  879. * overflow (when the input is less than smallest int136 or
  880. * greater than largest int136).
  881. *
  882. * Counterpart to Solidity's `int136` operator.
  883. *
  884. * Requirements:
  885. *
  886. * - input must fit into 136 bits
  887. *
  888. * _Available since v4.7._
  889. */
  890. function toInt136(int256 value) internal pure returns (int136 downcasted) {
  891. downcasted = int136(value);
  892. if (downcasted != value) {
  893. revert SafeCastOverflowedIntDowncast(136, value);
  894. }
  895. }
  896. /**
  897. * @dev Returns the downcasted int128 from int256, reverting on
  898. * overflow (when the input is less than smallest int128 or
  899. * greater than largest int128).
  900. *
  901. * Counterpart to Solidity's `int128` operator.
  902. *
  903. * Requirements:
  904. *
  905. * - input must fit into 128 bits
  906. *
  907. * _Available since v3.1._
  908. */
  909. function toInt128(int256 value) internal pure returns (int128 downcasted) {
  910. downcasted = int128(value);
  911. if (downcasted != value) {
  912. revert SafeCastOverflowedIntDowncast(128, value);
  913. }
  914. }
  915. /**
  916. * @dev Returns the downcasted int120 from int256, reverting on
  917. * overflow (when the input is less than smallest int120 or
  918. * greater than largest int120).
  919. *
  920. * Counterpart to Solidity's `int120` operator.
  921. *
  922. * Requirements:
  923. *
  924. * - input must fit into 120 bits
  925. *
  926. * _Available since v4.7._
  927. */
  928. function toInt120(int256 value) internal pure returns (int120 downcasted) {
  929. downcasted = int120(value);
  930. if (downcasted != value) {
  931. revert SafeCastOverflowedIntDowncast(120, value);
  932. }
  933. }
  934. /**
  935. * @dev Returns the downcasted int112 from int256, reverting on
  936. * overflow (when the input is less than smallest int112 or
  937. * greater than largest int112).
  938. *
  939. * Counterpart to Solidity's `int112` operator.
  940. *
  941. * Requirements:
  942. *
  943. * - input must fit into 112 bits
  944. *
  945. * _Available since v4.7._
  946. */
  947. function toInt112(int256 value) internal pure returns (int112 downcasted) {
  948. downcasted = int112(value);
  949. if (downcasted != value) {
  950. revert SafeCastOverflowedIntDowncast(112, value);
  951. }
  952. }
  953. /**
  954. * @dev Returns the downcasted int104 from int256, reverting on
  955. * overflow (when the input is less than smallest int104 or
  956. * greater than largest int104).
  957. *
  958. * Counterpart to Solidity's `int104` operator.
  959. *
  960. * Requirements:
  961. *
  962. * - input must fit into 104 bits
  963. *
  964. * _Available since v4.7._
  965. */
  966. function toInt104(int256 value) internal pure returns (int104 downcasted) {
  967. downcasted = int104(value);
  968. if (downcasted != value) {
  969. revert SafeCastOverflowedIntDowncast(104, value);
  970. }
  971. }
  972. /**
  973. * @dev Returns the downcasted int96 from int256, reverting on
  974. * overflow (when the input is less than smallest int96 or
  975. * greater than largest int96).
  976. *
  977. * Counterpart to Solidity's `int96` operator.
  978. *
  979. * Requirements:
  980. *
  981. * - input must fit into 96 bits
  982. *
  983. * _Available since v4.7._
  984. */
  985. function toInt96(int256 value) internal pure returns (int96 downcasted) {
  986. downcasted = int96(value);
  987. if (downcasted != value) {
  988. revert SafeCastOverflowedIntDowncast(96, value);
  989. }
  990. }
  991. /**
  992. * @dev Returns the downcasted int88 from int256, reverting on
  993. * overflow (when the input is less than smallest int88 or
  994. * greater than largest int88).
  995. *
  996. * Counterpart to Solidity's `int88` operator.
  997. *
  998. * Requirements:
  999. *
  1000. * - input must fit into 88 bits
  1001. *
  1002. * _Available since v4.7._
  1003. */
  1004. function toInt88(int256 value) internal pure returns (int88 downcasted) {
  1005. downcasted = int88(value);
  1006. if (downcasted != value) {
  1007. revert SafeCastOverflowedIntDowncast(88, value);
  1008. }
  1009. }
  1010. /**
  1011. * @dev Returns the downcasted int80 from int256, reverting on
  1012. * overflow (when the input is less than smallest int80 or
  1013. * greater than largest int80).
  1014. *
  1015. * Counterpart to Solidity's `int80` operator.
  1016. *
  1017. * Requirements:
  1018. *
  1019. * - input must fit into 80 bits
  1020. *
  1021. * _Available since v4.7._
  1022. */
  1023. function toInt80(int256 value) internal pure returns (int80 downcasted) {
  1024. downcasted = int80(value);
  1025. if (downcasted != value) {
  1026. revert SafeCastOverflowedIntDowncast(80, value);
  1027. }
  1028. }
  1029. /**
  1030. * @dev Returns the downcasted int72 from int256, reverting on
  1031. * overflow (when the input is less than smallest int72 or
  1032. * greater than largest int72).
  1033. *
  1034. * Counterpart to Solidity's `int72` operator.
  1035. *
  1036. * Requirements:
  1037. *
  1038. * - input must fit into 72 bits
  1039. *
  1040. * _Available since v4.7._
  1041. */
  1042. function toInt72(int256 value) internal pure returns (int72 downcasted) {
  1043. downcasted = int72(value);
  1044. if (downcasted != value) {
  1045. revert SafeCastOverflowedIntDowncast(72, value);
  1046. }
  1047. }
  1048. /**
  1049. * @dev Returns the downcasted int64 from int256, reverting on
  1050. * overflow (when the input is less than smallest int64 or
  1051. * greater than largest int64).
  1052. *
  1053. * Counterpart to Solidity's `int64` operator.
  1054. *
  1055. * Requirements:
  1056. *
  1057. * - input must fit into 64 bits
  1058. *
  1059. * _Available since v3.1._
  1060. */
  1061. function toInt64(int256 value) internal pure returns (int64 downcasted) {
  1062. downcasted = int64(value);
  1063. if (downcasted != value) {
  1064. revert SafeCastOverflowedIntDowncast(64, value);
  1065. }
  1066. }
  1067. /**
  1068. * @dev Returns the downcasted int56 from int256, reverting on
  1069. * overflow (when the input is less than smallest int56 or
  1070. * greater than largest int56).
  1071. *
  1072. * Counterpart to Solidity's `int56` operator.
  1073. *
  1074. * Requirements:
  1075. *
  1076. * - input must fit into 56 bits
  1077. *
  1078. * _Available since v4.7._
  1079. */
  1080. function toInt56(int256 value) internal pure returns (int56 downcasted) {
  1081. downcasted = int56(value);
  1082. if (downcasted != value) {
  1083. revert SafeCastOverflowedIntDowncast(56, value);
  1084. }
  1085. }
  1086. /**
  1087. * @dev Returns the downcasted int48 from int256, reverting on
  1088. * overflow (when the input is less than smallest int48 or
  1089. * greater than largest int48).
  1090. *
  1091. * Counterpart to Solidity's `int48` operator.
  1092. *
  1093. * Requirements:
  1094. *
  1095. * - input must fit into 48 bits
  1096. *
  1097. * _Available since v4.7._
  1098. */
  1099. function toInt48(int256 value) internal pure returns (int48 downcasted) {
  1100. downcasted = int48(value);
  1101. if (downcasted != value) {
  1102. revert SafeCastOverflowedIntDowncast(48, value);
  1103. }
  1104. }
  1105. /**
  1106. * @dev Returns the downcasted int40 from int256, reverting on
  1107. * overflow (when the input is less than smallest int40 or
  1108. * greater than largest int40).
  1109. *
  1110. * Counterpart to Solidity's `int40` operator.
  1111. *
  1112. * Requirements:
  1113. *
  1114. * - input must fit into 40 bits
  1115. *
  1116. * _Available since v4.7._
  1117. */
  1118. function toInt40(int256 value) internal pure returns (int40 downcasted) {
  1119. downcasted = int40(value);
  1120. if (downcasted != value) {
  1121. revert SafeCastOverflowedIntDowncast(40, value);
  1122. }
  1123. }
  1124. /**
  1125. * @dev Returns the downcasted int32 from int256, reverting on
  1126. * overflow (when the input is less than smallest int32 or
  1127. * greater than largest int32).
  1128. *
  1129. * Counterpart to Solidity's `int32` operator.
  1130. *
  1131. * Requirements:
  1132. *
  1133. * - input must fit into 32 bits
  1134. *
  1135. * _Available since v3.1._
  1136. */
  1137. function toInt32(int256 value) internal pure returns (int32 downcasted) {
  1138. downcasted = int32(value);
  1139. if (downcasted != value) {
  1140. revert SafeCastOverflowedIntDowncast(32, value);
  1141. }
  1142. }
  1143. /**
  1144. * @dev Returns the downcasted int24 from int256, reverting on
  1145. * overflow (when the input is less than smallest int24 or
  1146. * greater than largest int24).
  1147. *
  1148. * Counterpart to Solidity's `int24` operator.
  1149. *
  1150. * Requirements:
  1151. *
  1152. * - input must fit into 24 bits
  1153. *
  1154. * _Available since v4.7._
  1155. */
  1156. function toInt24(int256 value) internal pure returns (int24 downcasted) {
  1157. downcasted = int24(value);
  1158. if (downcasted != value) {
  1159. revert SafeCastOverflowedIntDowncast(24, value);
  1160. }
  1161. }
  1162. /**
  1163. * @dev Returns the downcasted int16 from int256, reverting on
  1164. * overflow (when the input is less than smallest int16 or
  1165. * greater than largest int16).
  1166. *
  1167. * Counterpart to Solidity's `int16` operator.
  1168. *
  1169. * Requirements:
  1170. *
  1171. * - input must fit into 16 bits
  1172. *
  1173. * _Available since v3.1._
  1174. */
  1175. function toInt16(int256 value) internal pure returns (int16 downcasted) {
  1176. downcasted = int16(value);
  1177. if (downcasted != value) {
  1178. revert SafeCastOverflowedIntDowncast(16, value);
  1179. }
  1180. }
  1181. /**
  1182. * @dev Returns the downcasted int8 from int256, reverting on
  1183. * overflow (when the input is less than smallest int8 or
  1184. * greater than largest int8).
  1185. *
  1186. * Counterpart to Solidity's `int8` operator.
  1187. *
  1188. * Requirements:
  1189. *
  1190. * - input must fit into 8 bits
  1191. *
  1192. * _Available since v3.1._
  1193. */
  1194. function toInt8(int256 value) internal pure returns (int8 downcasted) {
  1195. downcasted = int8(value);
  1196. if (downcasted != value) {
  1197. revert SafeCastOverflowedIntDowncast(8, value);
  1198. }
  1199. }
  1200. /**
  1201. * @dev Converts an unsigned uint256 into a signed int256.
  1202. *
  1203. * Requirements:
  1204. *
  1205. * - input must be less than or equal to maxInt256.
  1206. *
  1207. * _Available since v3.0._
  1208. */
  1209. function toInt256(uint256 value) internal pure returns (int256) {
  1210. // Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
  1211. if (value > uint256(type(int256).max)) {
  1212. revert SafeCastOverflowedUintToInt(255, value);
  1213. }
  1214. return int256(value);
  1215. }
  1216. }