PackedUint128Math
Last updated
Last updated
This library contains functions to encode and decode two uint128 into a single bytes32 and interact with the encoded bytes32.
Encodes two uint128 into a single bytes32
Parameters
x1
uint128
The first uint128
x2
uint128
The second uint128
Returns
z
bytes32
The encoded bytes32 as follows: [0 - 128[: x1 [128 - 256[: x2
Encodes a uint128 into a single bytes32 as the first uint128
Parameters
x1
uint128
The uint128
Returns
z
bytes32
The encoded bytes32 as follows: [0 - 128[: x1 [128 - 256[: empty
Encodes a uint128 into a single bytes32 as the second uint128
Parameters
x2
uint128
The uint128
Returns
z
bytes32
The encoded bytes32 as follows: [0 - 128[: empty [128 - 256[: x2
Encodes a uint128 into a single bytes32 as the first or second uint128
Parameters
x
uint128
The uint128
first
bool
Whether to encode as the first or second uint128
Returns
z
bytes32
The encoded bytes32 as follows: if first: [0 - 128[: x [128 - 256[: empty else: [0 - 128[: empty [128 - 256[: x
Decodes a bytes32 into two uint128
Parameters
z
bytes32
The encoded bytes32 as follows: [0 - 128[: x1 [128 - 256[: x2
Returns
x1
uint128
The first uint128
x2
uint128
The second uint128
Decodes a bytes32 into a uint128 as the first uint128
Parameters
z
bytes32
The encoded bytes32 as follows: [0 - 128[: x [128 - 256[: any
Returns
x
uint128
The first uint128
Decodes a bytes32 into a uint128 as the second uint128
Parameters
z
bytes32
The encoded bytes32 as follows: [0 - 128[: any [128 - 256[: y
Returns
y
uint128
The second uint128
Decodes a bytes32 into a uint128 as the first or second uint128
Parameters
z
bytes32
The encoded bytes32 as follows: if first: [0 - 128[: x1 [128 - 256[: empty else: [0 - 128[: empty [128 - 256[: x2
first
bool
Whether to decode as the first or second uint128
Returns
x
uint128
The decoded uint128
Adds two encoded bytes32, reverting on overflow on any of the uint128
Parameters
x
bytes32
The first bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2
y
bytes32
The second bytes32 encoded as follows: [0 - 128[: y1 [128 - 256[: y2
Returns
z
bytes32
The sum of x and y encoded as follows: [0 - 128[: x1 + y1 [128 - 256[: x2 + y2
Adds an encoded bytes32 and two uint128, reverting on overflow on any of the uint128
Parameters
x
bytes32
The bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2
y1
uint128
The first uint128
y2
uint128
The second uint128
Returns
<none>
bytes32
z The sum of x and y encoded as follows: [0 - 128[: x1 + y1 [128 - 256[: x2 + y2
Subtracts two encoded bytes32, reverting on underflow on any of the uint128
Parameters
x
bytes32
The first bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2
y
bytes32
The second bytes32 encoded as follows: [0 - 128[: y1 [128 - 256[: y2
Returns
z
bytes32
The difference of x and y encoded as follows: [0 - 128[: x1 - y1 [128 - 256[: x2 - y2
Subtracts an encoded bytes32 and two uint128, reverting on underflow on any of the uint128
Parameters
x
bytes32
The bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2
y1
uint128
The first uint128
y2
uint128
The second uint128
Returns
<none>
bytes32
z The difference of x and y encoded as follows: [0 - 128[: x1 - y1 [128 - 256[: x2 - y2
Returns whether any of the uint128 of x is strictly greater than the corresponding uint128 of y
Parameters
x
bytes32
The first bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2
y
bytes32
The second bytes32 encoded as follows: [0 - 128[: y1 [128 - 256[: y2
Returns
<none>
bool
x1 < y1
Returns whether any of the uint128 of x is strictly greater than the corresponding uint128 of y
Parameters
x
bytes32
The first bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2
y
bytes32
The second bytes32 encoded as follows: [0 - 128[: y1 [128 - 256[: y2
Returns
<none>
bool
x1 < y1
Multiplies an encoded bytes32 by a uint128 then divides the result by 10_000, rounding down The result can't overflow as the multiplier needs to be smaller or equal to 10_000
Parameters
x
bytes32
The bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2
multiplier
uint128
The uint128 to multiply by (must be smaller or equal to 10_000)
Returns
z
bytes32
The product of x and multiplier encoded as follows: [0 - 128[: floor((x1 * multiplier) / 10_000) [128 - 256[: floor((x2 * multiplier) / 10_000)