Skip to content

Commit

Permalink
perf: shift left by 128 bits in "exp2" to speed it up
Browse files Browse the repository at this point in the history
refactor: make result type "int256" in "exp" and "exp2"
refactor: rename "x" to "xAux" and "result" to "resultAux" in "exp2"
  • Loading branch information
PaulRBerg committed Apr 7, 2021
1 parent 7c35e9b commit f49d787
Showing 1 changed file with 76 additions and 72 deletions.
148 changes: 76 additions & 72 deletions contracts/PRBMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ library PRBMath {
///
/// Requirements:
/// - x must be 128e18 or lower.
/// - x must be 0 or higher.
/// - x cannot be negative.
/// - result must fit within MAX_59x18.
///
/// @param x The exponent as a 59.18 decimal fixed-point number.
/// @return result The result as a 59.18 decimal fixed-point number.
Expand All @@ -147,84 +148,87 @@ library PRBMath {
// prettier-ignore
unchecked {
// Convert x to the 128.128-bit fixed-point format.
uint256 xb = uint256(x) * 2**128 / uint256(UNIT);
uint256 xAux = (uint256(x) << 128) / uint256(UNIT);

// Start from 0.5 in the 128.128-bit fixed-point format.
result = 0x80000000000000000000000000000000;
// Start from 0.5 in the 128.128-bit fixed-point format. We need to use uint256 because the intermediary
// may get very close to 2^256, which doesn't fit in int256.
uint256 resultAux = 0x80000000000000000000000000000000;

// Multiply the result by root(2, 2^-i) when the bit at position i is 1. None of the intermediary results overflows
// because the initial result is 2^127 and all magic factors are lower than 2^129.
if (xb & 0x80000000000000000000000000000000 > 0) result = (result * 0x16A09E667F3BCC908B2FB1366EA957D3E) >> 128;
if (xb & 0x40000000000000000000000000000000 > 0) result = (result * 0x1306FE0A31B7152DE8D5A46305C85EDED) >> 128;
if (xb & 0x20000000000000000000000000000000 > 0) result = (result * 0x1172B83C7D517ADCDF7C8C50EB14A7920 ) >> 128;
if (xb & 0x10000000000000000000000000000000 > 0) result = (result * 0x10B5586CF9890F6298B92B71842A98364) >> 128;
if (xb & 0x8000000000000000000000000000000 > 0) result = (result * 0x1059B0D31585743AE7C548EB68CA417FE) >> 128;
if (xb & 0x4000000000000000000000000000000 > 0) result = (result * 0x102C9A3E778060EE6F7CACA4F7A29BDE9) >> 128;
if (xb & 0x2000000000000000000000000000000 > 0) result = (result * 0x10163DA9FB33356D84A66AE336DCDFA40) >> 128;
if (xb & 0x1000000000000000000000000000000 > 0) result = (result * 0x100B1AFA5ABCBED6129AB13EC11DC9544) >> 128;
if (xb & 0x800000000000000000000000000000 > 0) result = (result * 0x10058C86DA1C09EA1FF19D294CF2F679C) >> 128;
if (xb & 0x400000000000000000000000000000 > 0) result = (result * 0x1002C605E2E8CEC506D21BFC89A23A011) >> 128;
if (xb & 0x200000000000000000000000000000 > 0) result = (result * 0x100162F3904051FA128BCA9C55C31E5E0) >> 128;
if (xb & 0x100000000000000000000000000000 > 0) result = (result * 0x1000B175EFFDC76BA38E31671CA939726) >> 128;
if (xb & 0x80000000000000000000000000000 > 0) result = (result * 0x100058BA01FB9F96D6CACD4B180917C3E) >> 128;
if (xb & 0x40000000000000000000000000000 > 0) result = (result * 0x10002C5CC37DA9491D0985C348C68E7B4) >> 128;
if (xb & 0x20000000000000000000000000000 > 0) result = (result * 0x1000162E525EE054754457D5995292027) >> 128;
if (xb & 0x10000000000000000000000000000 > 0) result = (result * 0x10000B17255775C040618BF4A4ADE83FD) >> 128;
if (xb & 0x8000000000000000000000000000 > 0) result = (result * 0x1000058B91B5BC9AE2EED81E9B7D4CFAC) >> 128;
if (xb & 0x4000000000000000000000000000 > 0) result = (result * 0x100002C5C89D5EC6CA4D7C8ACC017B7CA) >> 128;
if (xb & 0x2000000000000000000000000000 > 0) result = (result * 0x10000162E43F4F831060E02D839A9D16D) >> 128;
if (xb & 0x1000000000000000000000000000 > 0) result = (result * 0x100000B1721BCFC99D9F890EA06911763) >> 128;
if (xb & 0x800000000000000000000000000 > 0) result = (result * 0x10000058B90CF1E6D97F9CA14DBCC1629) >> 128;
if (xb & 0x400000000000000000000000000 > 0) result = (result * 0x1000002C5C863B73F016468F6BAC5CA2C) >> 128;
if (xb & 0x200000000000000000000000000 > 0) result = (result * 0x100000162E430E5A18F6119E3C02282A6) >> 128;
if (xb & 0x100000000000000000000000000 > 0) result = (result * 0x1000000B1721835514B86E6D96EFD1BFF) >> 128;
if (xb & 0x80000000000000000000000000 > 0) result = (result * 0x100000058B90C0B48C6BE5DF846C5B2F0) >> 128;
if (xb & 0x40000000000000000000000000 > 0) result = (result * 0x10000002C5C8601CC6B9E94213C72737B) >> 128;
if (xb & 0x20000000000000000000000000 > 0) result = (result * 0x1000000162E42FFF037DF38AA2B219F07) >> 128;
if (xb & 0x10000000000000000000000000 > 0) result = (result * 0x10000000B17217FBA9C739AA5819F44FA) >> 128;
if (xb & 0x8000000000000000000000000 > 0) result = (result * 0x1000000058B90BFCDEE5ACD3C1CEDC824) >> 128;
if (xb & 0x4000000000000000000000000 > 0) result = (result * 0x100000002C5C85FE31F35A6A30DA1BE51) >> 128;
if (xb & 0x2000000000000000000000000 > 0) result = (result * 0x10000000162E42FF0999CE3541B9FFFD0) >> 128;
if (xb & 0x1000000000000000000000000 > 0) result = (result * 0x100000000B17217F80F4EF5AADDA45554) >> 128;
if (xb & 0x800000000000000000000000 > 0) result = (result * 0x10000000058B90BFBF8479BD5A81B51AE) >> 128;
if (xb & 0x400000000000000000000000 > 0) result = (result * 0x1000000002C5C85FDF84BD62AE30A74CD) >> 128;
if (xb & 0x200000000000000000000000 > 0) result = (result * 0x100000000162E42FEFB2FED257559BDAA) >> 128;
if (xb & 0x100000000000000000000000 > 0) result = (result * 0x1000000000B17217F7D5A7716BBA4A9AF) >> 128;
if (xb & 0x80000000000000000000000 > 0) result = (result * 0x100000000058B90BFBE9DDBAC5E109CCF) >> 128;
if (xb & 0x40000000000000000000000 > 0) result = (result * 0x10000000002C5C85FDF4B15DE6F17EB0E) >> 128;
if (xb & 0x20000000000000000000000 > 0) result = (result * 0x1000000000162E42FEFA494F1478FDE05) >> 128;
if (xb & 0x10000000000000000000000 > 0) result = (result * 0x10000000000B17217F7D20CF927C8E94D) >> 128;
if (xb & 0x8000000000000000000000 > 0) result = (result * 0x1000000000058B90BFBE8F71CB4E4B33E) >> 128;
if (xb & 0x4000000000000000000000 > 0) result = (result * 0x100000000002C5C85FDF477B662B26946) >> 128;
if (xb & 0x2000000000000000000000 > 0) result = (result * 0x10000000000162E42FEFA3AE53369388D) >> 128;
if (xb & 0x1000000000000000000000 > 0) result = (result * 0x100000000000B17217F7D1D351A389D41) >> 128;
if (xb & 0x800000000000000000000 > 0) result = (result * 0x10000000000058B90BFBE8E8B2D3D4EDF) >> 128;
if (xb & 0x400000000000000000000 > 0) result = (result * 0x1000000000002C5C85FDF4741BEA6E77F) >> 128;
if (xb & 0x200000000000000000000 > 0) result = (result * 0x100000000000162E42FEFA39FE95583C3) >> 128;
if (xb & 0x100000000000000000000 > 0) result = (result * 0x1000000000000B17217F7D1CFB72B45E3) >> 128;
if (xb & 0x80000000000000000000 > 0) result = (result * 0x100000000000058B90BFBE8E7CC35C3F2) >> 128;
if (xb & 0x40000000000000000000 > 0) result = (result * 0x10000000000002C5C85FDF473E242EA39) >> 128;
if (xb & 0x20000000000000000000 > 0) result = (result * 0x1000000000000162E42FEFA39F02B772C) >> 128;
if (xb & 0x10000000000000000000 > 0) result = (result * 0x10000000000000B17217F7D1CF7D83C1A) >> 128;
if (xb & 0x8000000000000000000 > 0) result = (result * 0x1000000000000058B90BFBE8E7BDCBE2E) >> 128;
if (xb & 0x4000000000000000000 > 0) result = (result * 0x100000000000002C5C85FDF473DEA871F) >> 128;
if (xb & 0x2000000000000000000 > 0) result = (result * 0x10000000000000162E42FEFA39EF44D92) >> 128;
if (xb & 0x1000000000000000000 > 0) result = (result * 0x100000000000000B17217F7D1CF79E949) >> 128;
if (xb & 0x800000000000000000 > 0) result = (result * 0x10000000000000058B90BFBE8E7BCE545) >> 128;
if (xb & 0x400000000000000000 > 0) result = (result * 0x1000000000000002C5C85FDF473DE6ECA) >> 128;
if (xb & 0x200000000000000000 > 0) result = (result * 0x100000000000000162E42FEFA39EF366F) >> 128;
if (xb & 0x100000000000000000 > 0) result = (result * 0x1000000000000000B17217F7D1CF79AFA) >> 128;
if (xb & 0x80000000000000000 > 0) result = (result * 0x100000000000000058B90BFBE8E7BCD6E) >> 128;
if (xb & 0x40000000000000000 > 0) result = (result * 0x10000000000000002C5C85FDF473DE6B3) >> 128;
if (xb & 0x20000000000000000 > 0) result = (result * 0x1000000000000000162E42FEFA39EF359) >> 128;
if (xb & 0x10000000000000000 > 0) result = (result * 0x10000000000000000B17217F7D1CF79AC) >> 128;

// Multiply the result by the integer part 2^n + 1. We shift by one bit extra because we have already divided
if (xAux & 0x80000000000000000000000000000000 > 0) resultAux = (resultAux * 0x16A09E667F3BCC908B2FB1366EA957D3E) >> 128;
if (xAux & 0x40000000000000000000000000000000 > 0) resultAux = (resultAux * 0x1306FE0A31B7152DE8D5A46305C85EDED) >> 128;
if (xAux & 0x20000000000000000000000000000000 > 0) resultAux = (resultAux * 0x1172B83C7D517ADCDF7C8C50EB14A7920) >> 128;
if (xAux & 0x10000000000000000000000000000000 > 0) resultAux = (resultAux * 0x10B5586CF9890F6298B92B71842A98364) >> 128;
if (xAux & 0x8000000000000000000000000000000 > 0) resultAux = (resultAux * 0x1059B0D31585743AE7C548EB68CA417FE) >> 128;
if (xAux & 0x4000000000000000000000000000000 > 0) resultAux = (resultAux * 0x102C9A3E778060EE6F7CACA4F7A29BDE9) >> 128;
if (xAux & 0x2000000000000000000000000000000 > 0) resultAux = (resultAux * 0x10163DA9FB33356D84A66AE336DCDFA40) >> 128;
if (xAux & 0x1000000000000000000000000000000 > 0) resultAux = (resultAux * 0x100B1AFA5ABCBED6129AB13EC11DC9544) >> 128;
if (xAux & 0x800000000000000000000000000000 > 0) resultAux = (resultAux * 0x10058C86DA1C09EA1FF19D294CF2F679C) >> 128;
if (xAux & 0x400000000000000000000000000000 > 0) resultAux = (resultAux * 0x1002C605E2E8CEC506D21BFC89A23A011) >> 128;
if (xAux & 0x200000000000000000000000000000 > 0) resultAux = (resultAux * 0x100162F3904051FA128BCA9C55C31E5E0) >> 128;
if (xAux & 0x100000000000000000000000000000 > 0) resultAux = (resultAux * 0x1000B175EFFDC76BA38E31671CA939726) >> 128;
if (xAux & 0x80000000000000000000000000000 > 0) resultAux = (resultAux * 0x100058BA01FB9F96D6CACD4B180917C3E) >> 128;
if (xAux & 0x40000000000000000000000000000 > 0) resultAux = (resultAux * 0x10002C5CC37DA9491D0985C348C68E7B4) >> 128;
if (xAux & 0x20000000000000000000000000000 > 0) resultAux = (resultAux * 0x1000162E525EE054754457D5995292027) >> 128;
if (xAux & 0x10000000000000000000000000000 > 0) resultAux = (resultAux * 0x10000B17255775C040618BF4A4ADE83FD) >> 128;
if (xAux & 0x8000000000000000000000000000 > 0) resultAux = (resultAux * 0x1000058B91B5BC9AE2EED81E9B7D4CFAC) >> 128;
if (xAux & 0x4000000000000000000000000000 > 0) resultAux = (resultAux * 0x100002C5C89D5EC6CA4D7C8ACC017B7CA) >> 128;
if (xAux & 0x2000000000000000000000000000 > 0) resultAux = (resultAux * 0x10000162E43F4F831060E02D839A9D16D) >> 128;
if (xAux & 0x1000000000000000000000000000 > 0) resultAux = (resultAux * 0x100000B1721BCFC99D9F890EA06911763) >> 128;
if (xAux & 0x800000000000000000000000000 > 0) resultAux = (resultAux * 0x10000058B90CF1E6D97F9CA14DBCC1629) >> 128;
if (xAux & 0x400000000000000000000000000 > 0) resultAux = (resultAux * 0x1000002C5C863B73F016468F6BAC5CA2C) >> 128;
if (xAux & 0x200000000000000000000000000 > 0) resultAux = (resultAux * 0x100000162E430E5A18F6119E3C02282A6) >> 128;
if (xAux & 0x100000000000000000000000000 > 0) resultAux = (resultAux * 0x1000000B1721835514B86E6D96EFD1BFF) >> 128;
if (xAux & 0x80000000000000000000000000 > 0) resultAux = (resultAux * 0x100000058B90C0B48C6BE5DF846C5B2F0) >> 128;
if (xAux & 0x40000000000000000000000000 > 0) resultAux = (resultAux * 0x10000002C5C8601CC6B9E94213C72737B) >> 128;
if (xAux & 0x20000000000000000000000000 > 0) resultAux = (resultAux * 0x1000000162E42FFF037DF38AA2B219F07) >> 128;
if (xAux & 0x10000000000000000000000000 > 0) resultAux = (resultAux * 0x10000000B17217FBA9C739AA5819F44FA) >> 128;
if (xAux & 0x8000000000000000000000000 > 0) resultAux = (resultAux * 0x1000000058B90BFCDEE5ACD3C1CEDC824) >> 128;
if (xAux & 0x4000000000000000000000000 > 0) resultAux = (resultAux * 0x100000002C5C85FE31F35A6A30DA1BE51) >> 128;
if (xAux & 0x2000000000000000000000000 > 0) resultAux = (resultAux * 0x10000000162E42FF0999CE3541B9FFFD0) >> 128;
if (xAux & 0x1000000000000000000000000 > 0) resultAux = (resultAux * 0x100000000B17217F80F4EF5AADDA45554) >> 128;
if (xAux & 0x800000000000000000000000 > 0) resultAux = (resultAux * 0x10000000058B90BFBF8479BD5A81B51AE) >> 128;
if (xAux & 0x400000000000000000000000 > 0) resultAux = (resultAux * 0x1000000002C5C85FDF84BD62AE30A74CD) >> 128;
if (xAux & 0x200000000000000000000000 > 0) resultAux = (resultAux * 0x100000000162E42FEFB2FED257559BDAA) >> 128;
if (xAux & 0x100000000000000000000000 > 0) resultAux = (resultAux * 0x1000000000B17217F7D5A7716BBA4A9AF) >> 128;
if (xAux & 0x80000000000000000000000 > 0) resultAux = (resultAux * 0x100000000058B90BFBE9DDBAC5E109CCF) >> 128;
if (xAux & 0x40000000000000000000000 > 0) resultAux = (resultAux * 0x10000000002C5C85FDF4B15DE6F17EB0E) >> 128;
if (xAux & 0x20000000000000000000000 > 0) resultAux = (resultAux * 0x1000000000162E42FEFA494F1478FDE05) >> 128;
if (xAux & 0x10000000000000000000000 > 0) resultAux = (resultAux * 0x10000000000B17217F7D20CF927C8E94D) >> 128;
if (xAux & 0x8000000000000000000000 > 0) resultAux = (resultAux * 0x1000000000058B90BFBE8F71CB4E4B33E) >> 128;
if (xAux & 0x4000000000000000000000 > 0) resultAux = (resultAux * 0x100000000002C5C85FDF477B662B26946) >> 128;
if (xAux & 0x2000000000000000000000 > 0) resultAux = (resultAux * 0x10000000000162E42FEFA3AE53369388D) >> 128;
if (xAux & 0x1000000000000000000000 > 0) resultAux = (resultAux * 0x100000000000B17217F7D1D351A389D41) >> 128;
if (xAux & 0x800000000000000000000 > 0) resultAux = (resultAux * 0x10000000000058B90BFBE8E8B2D3D4EDF) >> 128;
if (xAux & 0x400000000000000000000 > 0) resultAux = (resultAux * 0x1000000000002C5C85FDF4741BEA6E77F) >> 128;
if (xAux & 0x200000000000000000000 > 0) resultAux = (resultAux * 0x100000000000162E42FEFA39FE95583C3) >> 128;
if (xAux & 0x100000000000000000000 > 0) resultAux = (resultAux * 0x1000000000000B17217F7D1CFB72B45E3) >> 128;
if (xAux & 0x80000000000000000000 > 0) resultAux = (resultAux * 0x100000000000058B90BFBE8E7CC35C3F2) >> 128;
if (xAux & 0x40000000000000000000 > 0) resultAux = (resultAux * 0x10000000000002C5C85FDF473E242EA39) >> 128;
if (xAux & 0x20000000000000000000 > 0) resultAux = (resultAux * 0x1000000000000162E42FEFA39F02B772C) >> 128;
if (xAux & 0x10000000000000000000 > 0) resultAux = (resultAux * 0x10000000000000B17217F7D1CF7D83C1A) >> 128;
if (xAux & 0x8000000000000000000 > 0) resultAux = (resultAux * 0x1000000000000058B90BFBE8E7BDCBE2E) >> 128;
if (xAux & 0x4000000000000000000 > 0) resultAux = (resultAux * 0x100000000000002C5C85FDF473DEA871F) >> 128;
if (xAux & 0x2000000000000000000 > 0) resultAux = (resultAux * 0x10000000000000162E42FEFA39EF44D92) >> 128;
if (xAux & 0x1000000000000000000 > 0) resultAux = (resultAux * 0x100000000000000B17217F7D1CF79E949) >> 128;
if (xAux & 0x800000000000000000 > 0) resultAux = (resultAux * 0x10000000000000058B90BFBE8E7BCE545) >> 128;
if (xAux & 0x400000000000000000 > 0) resultAux = (resultAux * 0x1000000000000002C5C85FDF473DE6ECA) >> 128;
if (xAux & 0x200000000000000000 > 0) resultAux = (resultAux * 0x100000000000000162E42FEFA39EF366F) >> 128;
if (xAux & 0x100000000000000000 > 0) resultAux = (resultAux * 0x1000000000000000B17217F7D1CF79AFA) >> 128;
if (xAux & 0x80000000000000000 > 0) resultAux = (resultAux * 0x100000000000000058B90BFBE8E7BCD6E) >> 128;
if (xAux & 0x40000000000000000 > 0) resultAux = (resultAux * 0x10000000000000002C5C85FDF473DE6B3) >> 128;
if (xAux & 0x20000000000000000 > 0) resultAux = (resultAux * 0x1000000000000000162E42FEFA39EF359) >> 128;
if (xAux & 0x10000000000000000 > 0) resultAux = (resultAux * 0x10000000000000000B17217F7D1CF79AC) >> 128;

// Multiply the result by the integer part 2^n + 1. We have to shift by one bit extra because we have already divided
// by two when we set the result equal to 0.5 above.
result = result << ((xb >> 128) + 1);
resultAux = resultAux << ((xAux >> 128) + 1);

// Convert the result to the 59.18 decimal fixed-point format.
result = int256(mulDiv(uint256(result), uint256(UNIT), 2**128));
resultAux = mulDiv(resultAux, uint256(UNIT), 2**128);
require(resultAux <= uint256(MAX_59x18));
result = int256(resultAux);
}
}

Expand Down

0 comments on commit f49d787

Please sign in to comment.