FloatMath.sol 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. // SPDX-License-Identifier: BSD-4-Clause
  2. /*
  3. * ABDK Math 64.64 Smart Contract Library. Copyright © 2019 by ABDK Consulting.
  4. * Author: Mikhail Vladimirov <mikhail.vladimirov@gmail.com>
  5. */
  6. pragma solidity ^0.6.0;
  7. /**
  8. * Smart contract library of mathematical functions operating with signed
  9. * 64.64-bit fixed point numbers. Signed 64.64-bit fixed point number is
  10. * basically a simple fraction whose numerator is signed 128-bit integer and
  11. * denominator is 2^64. As long as denominator is always the same, there is no
  12. * need to store it, thus in Solidity signed 64.64-bit fixed point numbers are
  13. * represented by int128 type holding only the numerator.
  14. */
  15. library FloatMath {
  16. /*
  17. * Minimum value signed 64.64-bit fixed point number may have.
  18. */
  19. int128 private constant MIN_64x64 = -0x80000000000000000000000000000000;
  20. /*
  21. * Maximum value signed 64.64-bit fixed point number may have.
  22. */
  23. int128 private constant MAX_64x64 = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
  24. /**
  25. * Convert signed 256-bit integer number into signed 64.64-bit fixed point
  26. * number. Revert on overflow.
  27. *
  28. * @param x signed 256-bit integer number
  29. * @return signed 64.64-bit fixed point number
  30. */
  31. function fromInt (int256 x) internal pure returns (int128) {
  32. require (x >= -0x8000000000000000 && x <= 0x7FFFFFFFFFFFFFFF);
  33. return int128 (x << 64);
  34. }
  35. /**
  36. * Convert signed 64.64 fixed point number into signed 64-bit integer number
  37. * rounding down.
  38. *
  39. * @param x signed 64.64-bit fixed point number
  40. * @return signed 64-bit integer number
  41. */
  42. function toInt (int128 x) internal pure returns (int64) {
  43. return int64 (x >> 64);
  44. }
  45. /**
  46. * Convert unsigned 256-bit integer number into signed 64.64-bit fixed point
  47. * number. Revert on overflow.
  48. *
  49. * @param x unsigned 256-bit integer number
  50. * @return signed 64.64-bit fixed point number
  51. */
  52. function fromUInt (uint256 x) internal pure returns (int128) {
  53. require (x <= 0x7FFFFFFFFFFFFFFF);
  54. return int128 (x << 64);
  55. }
  56. /**
  57. * Convert signed 64.64 fixed point number into unsigned 64-bit integer
  58. * number rounding down. Revert on underflow.
  59. *
  60. * @param x signed 64.64-bit fixed point number
  61. * @return unsigned 64-bit integer number
  62. */
  63. function toUInt (int128 x) internal pure returns (uint64) {
  64. require (x >= 0);
  65. return uint64 (x >> 64);
  66. }
  67. /**
  68. * Convert signed 128.128 fixed point number into signed 64.64-bit fixed point
  69. * number rounding down. Revert on overflow.
  70. *
  71. * @param x signed 128.128-bin fixed point number
  72. * @return signed 64.64-bit fixed point number
  73. */
  74. function from128x128 (int256 x) internal pure returns (int128) {
  75. int256 result = x >> 64;
  76. require (result >= MIN_64x64 && result <= MAX_64x64);
  77. return int128 (result);
  78. }
  79. /**
  80. * Convert signed 64.64 fixed point number into signed 128.128 fixed point
  81. * number.
  82. *
  83. * @param x signed 64.64-bit fixed point number
  84. * @return signed 128.128 fixed point number
  85. */
  86. function to128x128 (int128 x) internal pure returns (int256) {
  87. return int256 (x) << 64;
  88. }
  89. /**
  90. * Calculate x + y. Revert on overflow.
  91. *
  92. * @param x signed 64.64-bit fixed point number
  93. * @param y signed 64.64-bit fixed point number
  94. * @return signed 64.64-bit fixed point number
  95. */
  96. function add (int128 x, int128 y) internal pure returns (int128) {
  97. int256 result = int256(x) + y;
  98. require (result >= MIN_64x64 && result <= MAX_64x64);
  99. return int128 (result);
  100. }
  101. /**
  102. * Calculate x - y. Revert on overflow.
  103. *
  104. * @param x signed 64.64-bit fixed point number
  105. * @param y signed 64.64-bit fixed point number
  106. * @return signed 64.64-bit fixed point number
  107. */
  108. function sub (int128 x, int128 y) internal pure returns (int128) {
  109. int256 result = int256(x) - y;
  110. require (result >= MIN_64x64 && result <= MAX_64x64);
  111. return int128 (result);
  112. }
  113. /**
  114. * Calculate x * y rounding down. Revert on overflow.
  115. *
  116. * @param x signed 64.64-bit fixed point number
  117. * @param y signed 64.64-bit fixed point number
  118. * @return signed 64.64-bit fixed point number
  119. */
  120. function mul (int128 x, int128 y) internal pure returns (int128) {
  121. int256 result = int256(x) * y >> 64;
  122. require (result >= MIN_64x64 && result <= MAX_64x64);
  123. return int128 (result);
  124. }
  125. /**
  126. * Calculate x * y rounding towards zero, where x is signed 64.64 fixed point
  127. * number and y is signed 256-bit integer number. Revert on overflow.
  128. *
  129. * @param x signed 64.64 fixed point number
  130. * @param y signed 256-bit integer number
  131. * @return signed 256-bit integer number
  132. */
  133. function muli (int128 x, int256 y) internal pure returns (int256) {
  134. if (x == MIN_64x64) {
  135. require (y >= -0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF &&
  136. y <= 0x1000000000000000000000000000000000000000000000000);
  137. return -y << 63;
  138. } else {
  139. bool negativeResult = false;
  140. if (x < 0) {
  141. x = -x;
  142. negativeResult = true;
  143. }
  144. if (y < 0) {
  145. y = -y; // We rely on overflow behavior here
  146. negativeResult = !negativeResult;
  147. }
  148. uint256 absoluteResult = mulu (x, uint256 (y));
  149. if (negativeResult) {
  150. require (absoluteResult <=
  151. 0x8000000000000000000000000000000000000000000000000000000000000000);
  152. return -int256 (absoluteResult); // We rely on overflow behavior here
  153. } else {
  154. require (absoluteResult <=
  155. 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
  156. return int256 (absoluteResult);
  157. }
  158. }
  159. }
  160. /**
  161. * Calculate x * y rounding down, where x is signed 64.64 fixed point number
  162. * and y is unsigned 256-bit integer number. Revert on overflow.
  163. *
  164. * @param x signed 64.64 fixed point number
  165. * @param y unsigned 256-bit integer number
  166. * @return unsigned 256-bit integer number
  167. */
  168. function mulu (int128 x, uint256 y) internal pure returns (uint256) {
  169. if (y == 0) return 0;
  170. require (x >= 0);
  171. uint256 lo = (uint256 (x) * (y & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)) >> 64;
  172. uint256 hi = uint256 (x) * (y >> 128);
  173. require (hi <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
  174. hi <<= 64;
  175. require (hi <=
  176. 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF - lo);
  177. return hi + lo;
  178. }
  179. /**
  180. * Calculate x / y rounding towards zero. Revert on overflow or when y is
  181. * zero.
  182. *
  183. * @param x signed 64.64-bit fixed point number
  184. * @param y signed 64.64-bit fixed point number
  185. * @return signed 64.64-bit fixed point number
  186. */
  187. function div (int128 x, int128 y) internal pure returns (int128) {
  188. require (y != 0);
  189. int256 result = (int256 (x) << 64) / y;
  190. require (result >= MIN_64x64 && result <= MAX_64x64);
  191. return int128 (result);
  192. }
  193. /**
  194. * Calculate x / y rounding towards zero, where x and y are signed 256-bit
  195. * integer numbers. Revert on overflow or when y is zero.
  196. *
  197. * @param x signed 256-bit integer number
  198. * @param y signed 256-bit integer number
  199. * @return signed 64.64-bit fixed point number
  200. */
  201. function divi (int256 x, int256 y) internal pure returns (int128) {
  202. require (y != 0);
  203. bool negativeResult = false;
  204. if (x < 0) {
  205. x = -x; // We rely on overflow behavior here
  206. negativeResult = true;
  207. }
  208. if (y < 0) {
  209. y = -y; // We rely on overflow behavior here
  210. negativeResult = !negativeResult;
  211. }
  212. uint128 absoluteResult = divuu (uint256 (x), uint256 (y));
  213. if (negativeResult) {
  214. require (absoluteResult <= 0x80000000000000000000000000000000);
  215. return -int128 (absoluteResult); // We rely on overflow behavior here
  216. } else {
  217. require (absoluteResult <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
  218. return int128 (absoluteResult); // We rely on overflow behavior here
  219. }
  220. }
  221. /**
  222. * Calculate x / y rounding towards zero, where x and y are unsigned 256-bit
  223. * integer numbers. Revert on overflow or when y is zero.
  224. *
  225. * @param x unsigned 256-bit integer number
  226. * @param y unsigned 256-bit integer number
  227. * @return signed 64.64-bit fixed point number
  228. */
  229. function divu (uint256 x, uint256 y) internal pure returns (int128) {
  230. require (y != 0);
  231. uint128 result = divuu (x, y);
  232. require (result <= uint128 (MAX_64x64));
  233. return int128 (result);
  234. }
  235. /**
  236. * Calculate -x. Revert on overflow.
  237. *
  238. * @param x signed 64.64-bit fixed point number
  239. * @return signed 64.64-bit fixed point number
  240. */
  241. function neg (int128 x) internal pure returns (int128) {
  242. require (x != MIN_64x64);
  243. return -x;
  244. }
  245. /**
  246. * Calculate |x|. Revert on overflow.
  247. *
  248. * @param x signed 64.64-bit fixed point number
  249. * @return signed 64.64-bit fixed point number
  250. */
  251. function abs (int128 x) internal pure returns (int128) {
  252. require (x != MIN_64x64);
  253. return x < 0 ? -x : x;
  254. }
  255. /**
  256. * Calculate 1 / x rounding towards zero. Revert on overflow or when x is
  257. * zero.
  258. *
  259. * @param x signed 64.64-bit fixed point number
  260. * @return signed 64.64-bit fixed point number
  261. */
  262. function inv (int128 x) internal pure returns (int128) {
  263. require (x != 0);
  264. int256 result = int256 (0x100000000000000000000000000000000) / x;
  265. require (result >= MIN_64x64 && result <= MAX_64x64);
  266. return int128 (result);
  267. }
  268. /**
  269. * Calculate arithmetics average of x and y, i.e. (x + y) / 2 rounding down.
  270. *
  271. * @param x signed 64.64-bit fixed point number
  272. * @param y signed 64.64-bit fixed point number
  273. * @return signed 64.64-bit fixed point number
  274. */
  275. function avg (int128 x, int128 y) internal pure returns (int128) {
  276. return int128 ((int256 (x) + int256 (y)) >> 1);
  277. }
  278. /**
  279. * Calculate geometric average of x and y, i.e. sqrt (x * y) rounding down.
  280. * Revert on overflow or in case x * y is negative.
  281. *
  282. * @param x signed 64.64-bit fixed point number
  283. * @param y signed 64.64-bit fixed point number
  284. * @return signed 64.64-bit fixed point number
  285. */
  286. function gavg (int128 x, int128 y) internal pure returns (int128) {
  287. int256 m = int256 (x) * int256 (y);
  288. require (m >= 0);
  289. require (m <
  290. 0x4000000000000000000000000000000000000000000000000000000000000000);
  291. return int128 (sqrtu (uint256 (m)));
  292. }
  293. /**
  294. * Calculate x^y assuming 0^0 is 1, where x is signed 64.64 fixed point number
  295. * and y is unsigned 256-bit integer number. Revert on overflow.
  296. *
  297. * @param x signed 64.64-bit fixed point number
  298. * @param y uint256 value
  299. * @return signed 64.64-bit fixed point number
  300. */
  301. function pow (int128 x, uint256 y) internal pure returns (int128) {
  302. uint256 absoluteResult;
  303. bool negativeResult = false;
  304. if (x >= 0) {
  305. absoluteResult = powu (uint256 (x) << 63, y);
  306. } else {
  307. // We rely on overflow behavior here
  308. absoluteResult = powu (uint256 (uint128 (-x)) << 63, y);
  309. negativeResult = y & 1 > 0;
  310. }
  311. absoluteResult >>= 63;
  312. if (negativeResult) {
  313. require (absoluteResult <= 0x80000000000000000000000000000000);
  314. return -int128 (absoluteResult); // We rely on overflow behavior here
  315. } else {
  316. require (absoluteResult <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
  317. return int128 (absoluteResult); // We rely on overflow behavior here
  318. }
  319. }
  320. /**
  321. * Calculate sqrt (x) rounding down. Revert if x < 0.
  322. *
  323. * @param x signed 64.64-bit fixed point number
  324. * @return signed 64.64-bit fixed point number
  325. */
  326. function sqrt (int128 x) internal pure returns (int128) {
  327. require (x >= 0);
  328. return int128 (sqrtu (uint256 (x) << 64));
  329. }
  330. /**
  331. * Calculate binary logarithm of x. Revert if x <= 0.
  332. *
  333. * @param x signed 64.64-bit fixed point number
  334. * @return signed 64.64-bit fixed point number
  335. */
  336. function log_2 (int128 x) internal pure returns (int128) {
  337. require (x > 0);
  338. int256 msb = 0;
  339. int256 xc = x;
  340. if (xc >= 0x10000000000000000) { xc >>= 64; msb += 64; }
  341. if (xc >= 0x100000000) { xc >>= 32; msb += 32; }
  342. if (xc >= 0x10000) { xc >>= 16; msb += 16; }
  343. if (xc >= 0x100) { xc >>= 8; msb += 8; }
  344. if (xc >= 0x10) { xc >>= 4; msb += 4; }
  345. if (xc >= 0x4) { xc >>= 2; msb += 2; }
  346. if (xc >= 0x2) msb += 1; // No need to shift xc anymore
  347. int256 result = msb - 64 << 64;
  348. uint256 ux = uint256 (x) << uint256 (127 - msb);
  349. for (int256 bit = 0x8000000000000000; bit > 0; bit >>= 1) {
  350. ux *= ux;
  351. uint256 b = ux >> 255;
  352. ux >>= 127 + b;
  353. result += bit * int256 (b);
  354. }
  355. return int128 (result);
  356. }
  357. /**
  358. * Calculate natural logarithm of x. Revert if x <= 0.
  359. *
  360. * @param x signed 64.64-bit fixed point number
  361. * @return signed 64.64-bit fixed point number
  362. */
  363. function ln (int128 x) internal pure returns (int128) {
  364. require (x > 0);
  365. return int128 (
  366. uint256 (log_2 (x)) * 0xB17217F7D1CF79ABC9E3B39803F2F6AF >> 128);
  367. }
  368. /**
  369. * Calculate binary exponent of x. Revert on overflow.
  370. *
  371. * @param x signed 64.64-bit fixed point number
  372. * @return signed 64.64-bit fixed point number
  373. */
  374. function exp_2 (int128 x) internal pure returns (int128) {
  375. require (x < 0x400000000000000000); // Overflow
  376. if (x < -0x400000000000000000) return 0; // Underflow
  377. uint256 result = 0x80000000000000000000000000000000;
  378. if (x & 0x8000000000000000 > 0)
  379. result = result * 0x16A09E667F3BCC908B2FB1366EA957D3E >> 128;
  380. if (x & 0x4000000000000000 > 0)
  381. result = result * 0x1306FE0A31B7152DE8D5A46305C85EDEC >> 128;
  382. if (x & 0x2000000000000000 > 0)
  383. result = result * 0x1172B83C7D517ADCDF7C8C50EB14A791F >> 128;
  384. if (x & 0x1000000000000000 > 0)
  385. result = result * 0x10B5586CF9890F6298B92B71842A98363 >> 128;
  386. if (x & 0x800000000000000 > 0)
  387. result = result * 0x1059B0D31585743AE7C548EB68CA417FD >> 128;
  388. if (x & 0x400000000000000 > 0)
  389. result = result * 0x102C9A3E778060EE6F7CACA4F7A29BDE8 >> 128;
  390. if (x & 0x200000000000000 > 0)
  391. result = result * 0x10163DA9FB33356D84A66AE336DCDFA3F >> 128;
  392. if (x & 0x100000000000000 > 0)
  393. result = result * 0x100B1AFA5ABCBED6129AB13EC11DC9543 >> 128;
  394. if (x & 0x80000000000000 > 0)
  395. result = result * 0x10058C86DA1C09EA1FF19D294CF2F679B >> 128;
  396. if (x & 0x40000000000000 > 0)
  397. result = result * 0x1002C605E2E8CEC506D21BFC89A23A00F >> 128;
  398. if (x & 0x20000000000000 > 0)
  399. result = result * 0x100162F3904051FA128BCA9C55C31E5DF >> 128;
  400. if (x & 0x10000000000000 > 0)
  401. result = result * 0x1000B175EFFDC76BA38E31671CA939725 >> 128;
  402. if (x & 0x8000000000000 > 0)
  403. result = result * 0x100058BA01FB9F96D6CACD4B180917C3D >> 128;
  404. if (x & 0x4000000000000 > 0)
  405. result = result * 0x10002C5CC37DA9491D0985C348C68E7B3 >> 128;
  406. if (x & 0x2000000000000 > 0)
  407. result = result * 0x1000162E525EE054754457D5995292026 >> 128;
  408. if (x & 0x1000000000000 > 0)
  409. result = result * 0x10000B17255775C040618BF4A4ADE83FC >> 128;
  410. if (x & 0x800000000000 > 0)
  411. result = result * 0x1000058B91B5BC9AE2EED81E9B7D4CFAB >> 128;
  412. if (x & 0x400000000000 > 0)
  413. result = result * 0x100002C5C89D5EC6CA4D7C8ACC017B7C9 >> 128;
  414. if (x & 0x200000000000 > 0)
  415. result = result * 0x10000162E43F4F831060E02D839A9D16D >> 128;
  416. if (x & 0x100000000000 > 0)
  417. result = result * 0x100000B1721BCFC99D9F890EA06911763 >> 128;
  418. if (x & 0x80000000000 > 0)
  419. result = result * 0x10000058B90CF1E6D97F9CA14DBCC1628 >> 128;
  420. if (x & 0x40000000000 > 0)
  421. result = result * 0x1000002C5C863B73F016468F6BAC5CA2B >> 128;
  422. if (x & 0x20000000000 > 0)
  423. result = result * 0x100000162E430E5A18F6119E3C02282A5 >> 128;
  424. if (x & 0x10000000000 > 0)
  425. result = result * 0x1000000B1721835514B86E6D96EFD1BFE >> 128;
  426. if (x & 0x8000000000 > 0)
  427. result = result * 0x100000058B90C0B48C6BE5DF846C5B2EF >> 128;
  428. if (x & 0x4000000000 > 0)
  429. result = result * 0x10000002C5C8601CC6B9E94213C72737A >> 128;
  430. if (x & 0x2000000000 > 0)
  431. result = result * 0x1000000162E42FFF037DF38AA2B219F06 >> 128;
  432. if (x & 0x1000000000 > 0)
  433. result = result * 0x10000000B17217FBA9C739AA5819F44F9 >> 128;
  434. if (x & 0x800000000 > 0)
  435. result = result * 0x1000000058B90BFCDEE5ACD3C1CEDC823 >> 128;
  436. if (x & 0x400000000 > 0)
  437. result = result * 0x100000002C5C85FE31F35A6A30DA1BE50 >> 128;
  438. if (x & 0x200000000 > 0)
  439. result = result * 0x10000000162E42FF0999CE3541B9FFFCF >> 128;
  440. if (x & 0x100000000 > 0)
  441. result = result * 0x100000000B17217F80F4EF5AADDA45554 >> 128;
  442. if (x & 0x80000000 > 0)
  443. result = result * 0x10000000058B90BFBF8479BD5A81B51AD >> 128;
  444. if (x & 0x40000000 > 0)
  445. result = result * 0x1000000002C5C85FDF84BD62AE30A74CC >> 128;
  446. if (x & 0x20000000 > 0)
  447. result = result * 0x100000000162E42FEFB2FED257559BDAA >> 128;
  448. if (x & 0x10000000 > 0)
  449. result = result * 0x1000000000B17217F7D5A7716BBA4A9AE >> 128;
  450. if (x & 0x8000000 > 0)
  451. result = result * 0x100000000058B90BFBE9DDBAC5E109CCE >> 128;
  452. if (x & 0x4000000 > 0)
  453. result = result * 0x10000000002C5C85FDF4B15DE6F17EB0D >> 128;
  454. if (x & 0x2000000 > 0)
  455. result = result * 0x1000000000162E42FEFA494F1478FDE05 >> 128;
  456. if (x & 0x1000000 > 0)
  457. result = result * 0x10000000000B17217F7D20CF927C8E94C >> 128;
  458. if (x & 0x800000 > 0)
  459. result = result * 0x1000000000058B90BFBE8F71CB4E4B33D >> 128;
  460. if (x & 0x400000 > 0)
  461. result = result * 0x100000000002C5C85FDF477B662B26945 >> 128;
  462. if (x & 0x200000 > 0)
  463. result = result * 0x10000000000162E42FEFA3AE53369388C >> 128;
  464. if (x & 0x100000 > 0)
  465. result = result * 0x100000000000B17217F7D1D351A389D40 >> 128;
  466. if (x & 0x80000 > 0)
  467. result = result * 0x10000000000058B90BFBE8E8B2D3D4EDE >> 128;
  468. if (x & 0x40000 > 0)
  469. result = result * 0x1000000000002C5C85FDF4741BEA6E77E >> 128;
  470. if (x & 0x20000 > 0)
  471. result = result * 0x100000000000162E42FEFA39FE95583C2 >> 128;
  472. if (x & 0x10000 > 0)
  473. result = result * 0x1000000000000B17217F7D1CFB72B45E1 >> 128;
  474. if (x & 0x8000 > 0)
  475. result = result * 0x100000000000058B90BFBE8E7CC35C3F0 >> 128;
  476. if (x & 0x4000 > 0)
  477. result = result * 0x10000000000002C5C85FDF473E242EA38 >> 128;
  478. if (x & 0x2000 > 0)
  479. result = result * 0x1000000000000162E42FEFA39F02B772C >> 128;
  480. if (x & 0x1000 > 0)
  481. result = result * 0x10000000000000B17217F7D1CF7D83C1A >> 128;
  482. if (x & 0x800 > 0)
  483. result = result * 0x1000000000000058B90BFBE8E7BDCBE2E >> 128;
  484. if (x & 0x400 > 0)
  485. result = result * 0x100000000000002C5C85FDF473DEA871F >> 128;
  486. if (x & 0x200 > 0)
  487. result = result * 0x10000000000000162E42FEFA39EF44D91 >> 128;
  488. if (x & 0x100 > 0)
  489. result = result * 0x100000000000000B17217F7D1CF79E949 >> 128;
  490. if (x & 0x80 > 0)
  491. result = result * 0x10000000000000058B90BFBE8E7BCE544 >> 128;
  492. if (x & 0x40 > 0)
  493. result = result * 0x1000000000000002C5C85FDF473DE6ECA >> 128;
  494. if (x & 0x20 > 0)
  495. result = result * 0x100000000000000162E42FEFA39EF366F >> 128;
  496. if (x & 0x10 > 0)
  497. result = result * 0x1000000000000000B17217F7D1CF79AFA >> 128;
  498. if (x & 0x8 > 0)
  499. result = result * 0x100000000000000058B90BFBE8E7BCD6D >> 128;
  500. if (x & 0x4 > 0)
  501. result = result * 0x10000000000000002C5C85FDF473DE6B2 >> 128;
  502. if (x & 0x2 > 0)
  503. result = result * 0x1000000000000000162E42FEFA39EF358 >> 128;
  504. if (x & 0x1 > 0)
  505. result = result * 0x10000000000000000B17217F7D1CF79AB >> 128;
  506. result >>= uint256 (63 - (x >> 64));
  507. require (result <= uint256 (MAX_64x64));
  508. return int128 (result);
  509. }
  510. /**
  511. * Calculate natural exponent of x. Revert on overflow.
  512. *
  513. * @param x signed 64.64-bit fixed point number
  514. * @return signed 64.64-bit fixed point number
  515. */
  516. function exp (int128 x) internal pure returns (int128) {
  517. require (x < 0x400000000000000000); // Overflow
  518. if (x < -0x400000000000000000) return 0; // Underflow
  519. return exp_2 (
  520. int128 (int256 (x) * 0x171547652B82FE1777D0FFDA0D23A7D12 >> 128));
  521. }
  522. /**
  523. * Calculate x / y rounding towards zero, where x and y are unsigned 256-bit
  524. * integer numbers. Revert on overflow or when y is zero.
  525. *
  526. * @param x unsigned 256-bit integer number
  527. * @param y unsigned 256-bit integer number
  528. * @return unsigned 64.64-bit fixed point number
  529. */
  530. function divuu (uint256 x, uint256 y) private pure returns (uint128) {
  531. require (y != 0);
  532. uint256 result;
  533. if (x <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
  534. result = (x << 64) / y;
  535. else {
  536. uint256 msb = 192;
  537. uint256 xc = x >> 192;
  538. if (xc >= 0x100000000) { xc >>= 32; msb += 32; }
  539. if (xc >= 0x10000) { xc >>= 16; msb += 16; }
  540. if (xc >= 0x100) { xc >>= 8; msb += 8; }
  541. if (xc >= 0x10) { xc >>= 4; msb += 4; }
  542. if (xc >= 0x4) { xc >>= 2; msb += 2; }
  543. if (xc >= 0x2) msb += 1; // No need to shift xc anymore
  544. result = (x << 255 - msb) / ((y - 1 >> msb - 191) + 1);
  545. require (result <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
  546. uint256 hi = result * (y >> 128);
  547. uint256 lo = result * (y & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
  548. uint256 xh = x >> 192;
  549. uint256 xl = x << 64;
  550. if (xl < lo) xh -= 1;
  551. xl -= lo; // We rely on overflow behavior here
  552. lo = hi << 128;
  553. if (xl < lo) xh -= 1;
  554. xl -= lo; // We rely on overflow behavior here
  555. assert (xh == hi >> 128);
  556. result += xl / y;
  557. }
  558. require (result <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
  559. return uint128 (result);
  560. }
  561. /**
  562. * Calculate x^y assuming 0^0 is 1, where x is unsigned 129.127 fixed point
  563. * number and y is unsigned 256-bit integer number. Revert on overflow.
  564. *
  565. * @param x unsigned 129.127-bit fixed point number
  566. * @param y uint256 value
  567. * @return unsigned 129.127-bit fixed point number
  568. */
  569. function powu (uint256 x, uint256 y) private pure returns (uint256) {
  570. if (y == 0) return 0x80000000000000000000000000000000;
  571. else if (x == 0) return 0;
  572. else {
  573. int256 msb = 0;
  574. uint256 xc = x;
  575. if (xc >= 0x100000000000000000000000000000000) { xc >>= 128; msb += 128; }
  576. if (xc >= 0x10000000000000000) { xc >>= 64; msb += 64; }
  577. if (xc >= 0x100000000) { xc >>= 32; msb += 32; }
  578. if (xc >= 0x10000) { xc >>= 16; msb += 16; }
  579. if (xc >= 0x100) { xc >>= 8; msb += 8; }
  580. if (xc >= 0x10) { xc >>= 4; msb += 4; }
  581. if (xc >= 0x4) { xc >>= 2; msb += 2; }
  582. if (xc >= 0x2) msb += 1; // No need to shift xc anymore
  583. int256 xe = msb - 127;
  584. if (xe > 0) x >>= uint256 (xe);
  585. else x <<= uint256 (-xe);
  586. uint256 result = 0x80000000000000000000000000000000;
  587. int256 re = 0;
  588. while (y > 0) {
  589. if (y & 1 > 0) {
  590. result = result * x;
  591. y -= 1;
  592. re += xe;
  593. if (result >=
  594. 0x8000000000000000000000000000000000000000000000000000000000000000) {
  595. result >>= 128;
  596. re += 1;
  597. } else result >>= 127;
  598. if (re < -127) return 0; // Underflow
  599. require (re < 128); // Overflow
  600. } else {
  601. x = x * x;
  602. y >>= 1;
  603. xe <<= 1;
  604. if (x >=
  605. 0x8000000000000000000000000000000000000000000000000000000000000000) {
  606. x >>= 128;
  607. xe += 1;
  608. } else x >>= 127;
  609. if (xe < -127) return 0; // Underflow
  610. require (xe < 128); // Overflow
  611. }
  612. }
  613. if (re > 0) result <<= uint256 (re);
  614. else if (re < 0) result >>= uint256 (-re);
  615. return result;
  616. }
  617. }
  618. /**
  619. * Calculate sqrt (x) rounding down, where x is unsigned 256-bit integer
  620. * number.
  621. *
  622. * @param x unsigned 256-bit integer number
  623. * @return unsigned 128-bit integer number
  624. */
  625. function sqrtu (uint256 x) private pure returns (uint128) {
  626. if (x == 0) return 0;
  627. else {
  628. uint256 xx = x;
  629. uint256 r = 1;
  630. if (xx >= 0x100000000000000000000000000000000) { xx >>= 128; r <<= 64; }
  631. if (xx >= 0x10000000000000000) { xx >>= 64; r <<= 32; }
  632. if (xx >= 0x100000000) { xx >>= 32; r <<= 16; }
  633. if (xx >= 0x10000) { xx >>= 16; r <<= 8; }
  634. if (xx >= 0x100) { xx >>= 8; r <<= 4; }
  635. if (xx >= 0x10) { xx >>= 4; r <<= 2; }
  636. if (xx >= 0x8) { r <<= 1; }
  637. r = (r + x / r) >> 1;
  638. r = (r + x / r) >> 1;
  639. r = (r + x / r) >> 1;
  640. r = (r + x / r) >> 1;
  641. r = (r + x / r) >> 1;
  642. r = (r + x / r) >> 1;
  643. r = (r + x / r) >> 1; // Seven iterations should be enough
  644. uint256 r1 = x / r;
  645. return uint128 (r < r1 ? r : r1);
  646. }
  647. }
  648. }