Skip to main content

LeftRightLibrary

Git Source

Author: Axicon Labs Limited

Simple data type that divides a 256-bit word into two 128-bit slots.

State Variables

LEFT_HALF_BIT_MASK

AND bitmask to isolate the left half of a uint256.

uint256 internal constant LEFT_HALF_BIT_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000;

LEFT_HALF_BIT_MASK_INT

AND bitmask to isolate the left half of an int256.

int256 internal constant LEFT_HALF_BIT_MASK_INT =
int256(uint256(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000));

RIGHT_HALF_BIT_MASK

AND bitmask to isolate the right half of an int256.

int256 internal constant RIGHT_HALF_BIT_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;

Functions

rightSlot

Get the "right" slot from a bit pattern.

function rightSlot(LeftRightUnsigned self) internal pure returns (uint128);

Parameters

NameTypeDescription
selfLeftRightUnsignedThe 256 bit value to extract the right half from

Returns

NameTypeDescription
<none>uint128The right half of self

rightSlot

Get the "right" slot from a bit pattern.

function rightSlot(LeftRightSigned self) internal pure returns (int128);

Parameters

NameTypeDescription
selfLeftRightSignedThe 256 bit value to extract the right half from

Returns

NameTypeDescription
<none>int128The right half of self

toRightSlot

Add to the "right" slot in a 256-bit pattern.

function toRightSlot(LeftRightUnsigned self, uint128 right) internal pure returns (LeftRightUnsigned);

Parameters

NameTypeDescription
selfLeftRightUnsignedThe 256-bit pattern to be written to
rightuint128The value to be added to the right slot

Returns

NameTypeDescription
<none>LeftRightUnsignedself with right added (not overwritten, but added) to the value in its right 128 bits

toRightSlot

Add to the "right" slot in a 256-bit pattern.

function toRightSlot(LeftRightSigned self, int128 right) internal pure returns (LeftRightSigned);

Parameters

NameTypeDescription
selfLeftRightSignedThe 256-bit pattern to be written to
rightint128The value to be added to the right slot

Returns

NameTypeDescription
<none>LeftRightSignedself with right added (not overwritten, but added) to the value in its right 128 bits

leftSlot

Get the "left" slot from a bit pattern.

function leftSlot(LeftRightUnsigned self) internal pure returns (uint128);

Parameters

NameTypeDescription
selfLeftRightUnsignedThe 256 bit value to extract the left half from

Returns

NameTypeDescription
<none>uint128The left half of self

leftSlot

Get the "left" slot from a bit pattern.

function leftSlot(LeftRightSigned self) internal pure returns (int128);

Parameters

NameTypeDescription
selfLeftRightSignedThe 256 bit value to extract the left half from

Returns

NameTypeDescription
<none>int128The left half of self

toLeftSlot

All toLeftSlot functions add bits to the left slot without clearing it first

Add to the "left" slot in a 256-bit pattern.

function toLeftSlot(LeftRightUnsigned self, uint128 left) internal pure returns (LeftRightUnsigned);

Parameters

NameTypeDescription
selfLeftRightUnsignedThe 256-bit pattern to be written to
leftuint128The value to be added to the left slot

Returns

NameTypeDescription
<none>LeftRightUnsignedself with left added (not overwritten, but added) to the value in its left 128 bits

toLeftSlot

Add to the "left" slot in a 256-bit pattern.

function toLeftSlot(LeftRightSigned self, int128 left) internal pure returns (LeftRightSigned);

Parameters

NameTypeDescription
selfLeftRightSignedThe 256-bit pattern to be written to
leftint128The value to be added to the left slot

Returns

NameTypeDescription
<none>LeftRightSignedself with left added (not overwritten, but added) to the value in its left 128 bits

add

Add two LeftRight-encoded words; revert on overflow or underflow.

function add(LeftRightUnsigned x, LeftRightUnsigned y) internal pure returns (LeftRightUnsigned z);

Parameters

NameTypeDescription
xLeftRightUnsignedThe augend
yLeftRightUnsignedThe addend

Returns

NameTypeDescription
zLeftRightUnsignedThe sum x + y

sub

Subtract two LeftRight-encoded words; revert on overflow or underflow.

function sub(LeftRightUnsigned x, LeftRightUnsigned y) internal pure returns (LeftRightUnsigned z);

Parameters

NameTypeDescription
xLeftRightUnsignedThe minuend
yLeftRightUnsignedThe subtrahend

Returns

NameTypeDescription
zLeftRightUnsignedThe difference x - y

add

Add two LeftRight-encoded words; revert on overflow or underflow.

function add(LeftRightUnsigned x, LeftRightSigned y) internal pure returns (LeftRightSigned z);

Parameters

NameTypeDescription
xLeftRightUnsignedThe augend
yLeftRightSignedThe addend

Returns

NameTypeDescription
zLeftRightSignedThe sum x + y

add

Add two LeftRight-encoded words; revert on overflow or underflow.

function add(LeftRightSigned x, LeftRightSigned y) internal pure returns (LeftRightSigned z);

Parameters

NameTypeDescription
xLeftRightSignedThe augend
yLeftRightSignedThe addend

Returns

NameTypeDescription
zLeftRightSignedThe sum x + y

sub

Subtract two LeftRight-encoded words; revert on overflow or underflow.

function sub(LeftRightSigned x, LeftRightSigned y) internal pure returns (LeftRightSigned z);

Parameters

NameTypeDescription
xLeftRightSignedThe minuend
yLeftRightSignedThe subtrahend

Returns

NameTypeDescription
zLeftRightSignedThe difference x - y

subRect

Subtract two LeftRight-encoded words; revert on overflow or underflow.

For each slot, rectify difference x - y to 0 if negative.

function subRect(LeftRightSigned x, LeftRightSigned y) internal pure returns (LeftRightUnsigned z);

Parameters

NameTypeDescription
xLeftRightSignedThe minuend
yLeftRightSignedThe subtrahend

Returns

NameTypeDescription
zLeftRightUnsignedThe difference x - y

addCapped

Adds two sets of LeftRight-encoded words, freezing both right slots if either overflows, and vice versa.

Used for linked accumulators, so if the accumulator for one side overflows for a token, both cease to accumulate.

function addCapped(LeftRightUnsigned x, LeftRightUnsigned dx, LeftRightUnsigned y, LeftRightUnsigned dy)
internal
pure
returns (LeftRightUnsigned, LeftRightUnsigned);

Parameters

NameTypeDescription
xLeftRightUnsignedThe first augend
dxLeftRightUnsignedThe addend for x
yLeftRightUnsignedThe second augend
dyLeftRightUnsignedThe addend for y

Returns

NameTypeDescription
<none>LeftRightUnsignedThe sum x + dx
<none>LeftRightUnsignedThe sum y + dy