SafeCast.sol 35 KB

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