SafeCast.sol 36 KB

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