SafeCast.sol 34 KB

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