SafeCast.sol 34 KB

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