CloberPackedUint256
This library treats uint256 as a group of uint8, uint16, uint32, or uint64 and adds,
subtracts, or updates the elements of the group by indexing them.
*Unsafe
optimizes gas costs by using assembly under the assumption
that the user will not provide an input that would cause an arithmetic underflow or overflow.
get8Unsafe
function get8Unsafe(uint256 packed, uint256 index) external pure returns (uint8)
Retrieves a uint8 from the packed uint256 at the specified index with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256 to retrieve the uint8 from. |
index | uint256 | The index of the uint8 to retrieve. |
Return Values
Name | Type | Description |
---|
[0] | uint8 | The uint8 at the specified index of the packed uint256. |
get8
function get8(uint256 packed, uint256 index) external pure returns (uint8)
Retrieves a uint8 from the packed uint256 at the specified index.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256 to retrieve the uint8 from. |
index | uint256 | The index of the uint8 to retrieve. |
Return Values
Name | Type | Description |
---|
[0] | uint8 | The uint8 at the specified index of the packed uint256. |
get16Unsafe
function get16Unsafe(uint256 packed, uint256 index) external pure returns (uint16)
Retrieves a uint16 from the packed uint256 at the specified index with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256 to retrieve the uint16 from. |
index | uint256 | The index of the uint16 to retrieve. |
Return Values
Name | Type | Description |
---|
[0] | uint16 | The uint16 at the specified index of the packed uint256. |
get16
function get16(uint256 packed, uint256 index) external pure returns (uint16)
Retrieves a uint16 from the packed uint256 at the specified index.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256 to retrieve the uint16 from. |
index | uint256 | The index of the uint16 to retrieve. |
Return Values
Name | Type | Description |
---|
[0] | uint16 | The uint16 at the specified index of the packed uint256. |
get32Unsafe
function get32Unsafe(uint256 packed, uint256 index) external pure returns (uint32)
Retrieves a uint32 from the packed uint256 at the specified index with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256 to retrieve the uint32 from. |
index | uint256 | The index of the uint32 to retrieve. |
Return Values
Name | Type | Description |
---|
[0] | uint32 | The uint32 at the specified index of the packed uint256. |
get32
function get32(uint256 packed, uint256 index) external pure returns (uint32)
Retrieves a uint32 from the packed uint256 at the specified index.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256 to retrieve the uint32 from. |
index | uint256 | The index of the uint32 to retrieve. |
Return Values
Name | Type | Description |
---|
[0] | uint32 | The uint32 at the specified index of the packed uint256. |
get64Unsafe
function get64Unsafe(uint256 packed, uint256 index) external pure returns (uint64)
Retrieves a uint64 from the packed uint256 at the specified index with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256 to retrieve the uint64 from. |
index | uint256 | The index of the uint64 to retrieve. |
Return Values
Name | Type | Description |
---|
[0] | uint64 | The uint64 at the specified index of the packed uint256. |
get64
function get64(uint256 packed, uint256 index) external pure returns (uint64)
Retrieves a uint64 from the packed uint256 at the specified index.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256 to retrieve the uint64 from. |
index | uint256 | The index of the uint64 to retrieve. |
Return Values
Name | Type | Description |
---|
[0] | uint64 | The uint64 at the specified index of the packed uint256. |
add8Unsafe
function add8Unsafe(uint256 packed, uint256 index, uint8 value) external pure returns (uint256)
Adds a uint8 value to a uint8 element in the packed uint256 with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to add the uint8. |
value | uint8 | The uint8 value to add. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint8 added at the specified index. |
add8
function add8(uint256 packed, uint256 index, uint8 value) external pure returns (uint256)
Adds a uint8 value to a uint8 element in the packed uint256.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to add the uint8. |
value | uint8 | The uint8 value to add. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint8 added at the specified index. |
add16Unsafe
function add16Unsafe(uint256 packed, uint256 index, uint16 value) external pure returns (uint256)
Adds a uint16 value to a uint16 element in the packed uint256 with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to add the uint16. |
value | uint16 | The uint16 value to add. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint16 added at the specified index. |
add16
function add16(uint256 packed, uint256 index, uint16 value) external pure returns (uint256)
Adds a uint16 value to a uint16 element in the packed uint256.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to add the uint16. |
value | uint16 | The uint16 value to add. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint16 added at the specified index. |
add32Unsafe
function add32Unsafe(uint256 packed, uint256 index, uint32 value) external pure returns (uint256)
Adds a uint32 value to a uint32 element in the packed uint256 with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to add the uint32. |
value | uint32 | The uint32 value to add. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint32 added at the specified index. |
add32
function add32(uint256 packed, uint256 index, uint32 value) external pure returns (uint256)
Adds a uint32 value to a uint32 element in the packed uint256.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to add the uint32. |
value | uint32 | The uint32 value to add. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint32 added at the specified index. |
add64Unsafe
function add64Unsafe(uint256 packed, uint256 index, uint64 value) external pure returns (uint256)
Adds a uint64 value to a uint64 element in the packed uint256 with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to add the uint64. |
value | uint64 | The uint64 value to add. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint64 added at the specified index. |
add64
function add64(uint256 packed, uint256 index, uint64 value) external pure returns (uint256)
Adds a uint64 value to a uint64 element in the packed uint256.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to add the uint64. |
value | uint64 | The uint64 value to add. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint64 added at the specified index. |
sub8Unsafe
function sub8Unsafe(uint256 packed, uint256 index, uint8 value) external pure returns (uint256)
Subtracts a uint8 value to a uint8 element in the packed uint256 with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to subtract the uint8. |
value | uint8 | The uint8 value to subtract. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint8 subtracted at the specified index. |
sub8
function sub8(uint256 packed, uint256 index, uint8 value) external pure returns (uint256)
Subtracts a uint8 value to a uint8 element in the packed uint256.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to subtract the uint8. |
value | uint8 | The uint8 value to subtract. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint8 subtracted at the specified index. |
sub16Unsafe
function sub16Unsafe(uint256 packed, uint256 index, uint16 value) external pure returns (uint256)
Subtracts a uint16 value from the packed uint256 at the specified index with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to subtract the uint16. |
value | uint16 | The uint16 value to subtract. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint16 subtracted at the specified index. |
sub16
function sub16(uint256 packed, uint256 index, uint16 value) external pure returns (uint256)
Subtracts a uint16 value to a uint16 element in the packed uint256.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to subtract the uint16. |
value | uint16 | The uint16 value to subtract. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint16 subtracted at the specified index. |
sub32Unsafe
function sub32Unsafe(uint256 packed, uint256 index, uint32 value) external pure returns (uint256)
Subtracts a uint32 value to a uint32 element in the packed uint256 with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to subtract the uint32. |
value | uint32 | The uint32 value to subtract. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint32 subtracted at the specified index. |
sub32
function sub32(uint256 packed, uint256 index, uint32 value) external pure returns (uint256)
Subtracts a uint32 value to a uint32 element in the packed uint256.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to subtract the uint32. |
value | uint32 | The uint32 value to subtract. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint32 subtracted at the specified index. |
sub64Unsafe
function sub64Unsafe(uint256 packed, uint256 index, uint64 value) external pure returns (uint256)
Subtracts a uint64 value to a uint64 element in the packed uint256 with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to subtract the uint64. |
value | uint64 | The uint64 value to subtract. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint64 subtracted at the specified index. |
sub64
function sub64(uint256 packed, uint256 index, uint64 value) external pure returns (uint256)
Subtracts a uint64 value to a uint64 element in the packed uint256.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index at which to subtract the uint64. |
value | uint64 | The uint64 value to subtract. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint64 subtracted at the specified index. |
update8Unsafe
function update8Unsafe(uint256 packed, uint256 index, uint8 value) external pure returns (uint256)
Updates the packed uint256 at the specified index with a uint8 value with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index of the packed uint256 to update. |
value | uint8 | The uint8 value to update the packed uint256 with. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint8 value at the specified index. |
update8
function update8(uint256 packed, uint256 index, uint8 value) external pure returns (uint256)
Updates the packed uint256 at the specified index with a uint8 value.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index of the packed uint256 to update. |
value | uint8 | The uint8 value to update the packed uint256 with. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint8 value at the specified index. |
update16Unsafe
function update16Unsafe(uint256 packed, uint256 index, uint16 value) external pure returns (uint256)
Updates the packed uint256 at the specified index with a uint16 value with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index of the packed uint256 to update. |
value | uint16 | The uint16 value to update the packed uint256 with. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint16 value at the specified index. |
update16
function update16(uint256 packed, uint256 index, uint16 value) external pure returns (uint256)
Updates the packed uint256 at the specified index with a uint16 value.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index of the packed uint256 to update. |
value | uint16 | The uint16 value to update the packed uint256 with. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint16 value at the specified index. |
update32Unsafe
function update32Unsafe(uint256 packed, uint256 index, uint32 value) external pure returns (uint256)
Updates the packed uint256 at the specified index with a uint32 value with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index of the packed uint256 to update. |
value | uint32 | The uint32 value to update the packed uint256 with. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint32 value at the specified index. |
update32
function update32(uint256 packed, uint256 index, uint32 value) external pure returns (uint256)
Updates the packed uint256 at the specified index with a uint32 value.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index of the packed uint256 to update. |
value | uint32 | The uint32 value to update the packed uint256 with. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint32 value at the specified index. |
update64Unsafe
function update64Unsafe(uint256 packed, uint256 index, uint64 value) external pure returns (uint256)
Updates the packed uint256 at the specified index with a uint64 value with unchecked.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index of the packed uint256 to update. |
value | uint64 | The uint64 value to update the packed uint256 with. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint64 value at the specified index. |
update64
function update64(uint256 packed, uint256 index, uint64 value) external pure returns (uint256)
Updates the packed uint256 at the specified index with a uint64 value.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
index | uint256 | The index of the packed uint256 to update. |
value | uint64 | The uint64 value to update the packed uint256 with. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The resulting packed uint256 with the uint64 value at the specified index. |
total32
function total32(uint256 packed) external pure returns (uint256)
Calculates the sum of all the uint32 values in the packed uint256.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The sum of all the uint32 values in the packed uint256. |
total64
function total64(uint256 packed) external pure returns (uint256)
Calculates the sum of all the uint64 values in the packed uint256.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The sum of all the uint64 values in the packed uint256. |
sum32
function sum32(uint256 packed, uint256 from, uint256 to) external pure returns (uint256)
Calculates the sum of the uint32 values in the packed uint256 within the specified range.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
from | uint256 | The starting index (inclusive) of the uint32 values to add up. |
to | uint256 | The ending index (exclusive) of the uint32 values to add up. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The sum of the uint32 values in the packed uint256 within the specified range. |
sum64
function sum64(uint256 packed, uint256 from, uint256 to) external pure returns (uint256)
Calculates the sum of the uint64 values in the packed uint256 within the specified range.
Parameters
Name | Type | Description |
---|
packed | uint256 | The packed uint256. |
from | uint256 | The starting index (inclusive) of the uint64 values to add up. |
to | uint256 | The ending index (exclusive) of the uint64 values to add up. |
Return Values
Name | Type | Description |
---|
[0] | uint256 | The sum of the uint64 values in the packed uint256 within the specified range. |