UD60x18Common
UNIT
UNIT_INVERSE
UNIT_LPOTD
PRBMath_MulDiv18_Overflow
Thrown when the resultant value in {mulDiv18} overflows uint256.
error PRBMath_MulDiv18_Overflow(uint256 x, uint256 y)
PRBMath_MulDiv_Overflow
Thrown when the resultant value in {mulDiv} overflows uint256.
error PRBMath_MulDiv_Overflow(uint256 x, uint256 y, uint256 denominator)
exp2
Calculates the binary exponent of x using the binary fraction method.
function exp2(uint256 x) internal pure returns (uint256 result)
Has to use 192.64-bit fixed-point numbers. See https://ethereum.stackexchange.com/a/96594/24693.
Parameters
Name | Type | Description |
---|---|---|
x | uint256 | The exponent as an unsigned 192.64-bit fixed-point number. |
Fields
Name | Type | Description |
---|
Return Values
Name | Type | Description |
---|---|---|
result | uint256 | The result as an unsigned 60.18-decimal fixed-point number. |
msb
Finds the zero-based index of the first 1 in the binary representation of x.
function msb(uint256 x) internal pure returns (uint256 result)
See the note on "msb" in this Wikipedia article: https://en.wikipedia.org/wiki/Find_first_set
Each step in this implementation is equivalent to this high-level code:
if (x >= 2 ** 128) \{
x >>= 128;
result += 128;
\}
Where 128 is replaced with each respective power of two factor. See the full high-level implementation here: https://gist.github.com/PaulRBerg/f932f8693f2733e30c4d479e8e980948
The Yul instructions used below are:
- "gt" is "greater than"
- "or" is the OR bitwise operator
- "shl" is "shift left"
- "shr" is "shift right"
Parameters
Name | Type | Description |
---|---|---|
x | uint256 | The uint256 number for which to find the index of the most significant bit. |
Fields
Name | Type | Description |
---|
Return Values
Name | Type | Description |
---|---|---|
result | uint256 | The index of the most significant bit as a uint256. |
mulDiv18
Calculates x*y÷1e18 with 512-bit precision.
function mulDiv18(uint256 x, uint256 y) internal pure returns (uint256 result)
A variant of {mulDiv} with constant folding, i.e. in which the denominator is hard coded to 1e18.
Notes:
- The body is purposely left uncommented; to understand how this works, see the documentation in {mulDiv}.
- The result is rounded toward zero.
- We take as an axiom that the result cannot be
MAX_UINT256
when x and y solve the following system of equations:
Requirements:
- Refer to the requirements in {mulDiv}.
- The result must fit in uint256.
Parameters
Name | Type | Description |
---|---|---|
x | uint256 | The multiplicand as an unsigned 60.18-decimal fixed-point number. |
y | uint256 | The multiplier as an unsigned 60.18-decimal fixed-point number. |
Fields
Name | Type | Description |
---|
Return Values
Name | Type | Description |
---|---|---|
result | uint256 | The result as an unsigned 60.18-decimal fixed-point number. |
mulDiv
Calculates x*y÷denominator with 512-bit precision.
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result)
Credits to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv.
Notes:
- The result is rounded toward zero.
Requirements:
- The denominator must not be zero.
- The result must fit in uint256.
Parameters
Name | Type | Description |
---|---|---|
x | uint256 | The multiplicand as a uint256. |
y | uint256 | The multiplier as a uint256. |
denominator | uint256 | The divisor as a uint256. |
Fields
Name | Type | Description |
---|
Return Values
Name | Type | Description |
---|---|---|
result | uint256 | The result as a uint256. |