SafeCast.sol 34 KB

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