Skip to main content

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)
Dev note

Has to use 192.64-bit fixed-point numbers. See https://ethereum.stackexchange.com/a/96594/24693.

Parameters

NameTypeDescription
xuint256The exponent as an unsigned 192.64-bit fixed-point number.

Fields

NameTypeDescription

Return Values

NameTypeDescription
resultuint256The 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)
Dev note

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

NameTypeDescription
xuint256The uint256 number for which to find the index of the most significant bit.

Fields

NameTypeDescription

Return Values

NameTypeDescription
resultuint256The 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)
Dev note

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:
\begin\{cases\} x * y = MAX\_UINT256 * UNIT \\ (x * y) \% UNIT \geq \frac\{UNIT\}\{2\} \end\{cases\}

Requirements:

  • Refer to the requirements in {mulDiv}.
  • The result must fit in uint256.

Parameters

NameTypeDescription
xuint256The multiplicand as an unsigned 60.18-decimal fixed-point number.
yuint256The multiplier as an unsigned 60.18-decimal fixed-point number.

Fields

NameTypeDescription

Return Values

NameTypeDescription
resultuint256The 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)
Dev note

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

NameTypeDescription
xuint256The multiplicand as a uint256.
yuint256The multiplier as a uint256.
denominatoruint256The divisor as a uint256.

Fields

NameTypeDescription

Return Values

NameTypeDescription
resultuint256The result as a uint256.