Skip to content

Commit

Permalink
toUint256
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowBirdy committed Feb 6, 2024
1 parent cbe02ed commit ee40d63
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/HuffCastLib.huff
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,13 @@
REVERT_OVERFLOW()
success:
}


#define macro TO_UINT256() = takes (1) returns (1) {
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
dup2
sgt
success jumpi
REVERT_OVERFLOW()
success:
}
6 changes: 6 additions & 0 deletions src/HuffCastLibImportoor.huff
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
#define function toInt240(uint256) view returns (int240)
#define function toInt248(uint256) view returns (int248)
#define function toInt256(uint256) view returns (int256)
#define function toUint256(int256) view returns (uint256)




Expand Down Expand Up @@ -135,6 +137,7 @@
dup2 __FUNC_SIG(toInt240) eq i240 jumpi
dup2 __FUNC_SIG(toInt248) eq i248 jumpi
dup2 __FUNC_SIG(toInt256) eq i256 jumpi
dup2 __FUNC_SIG(toUint256) eq u256 jumpi


0x00 0x00 revert
Expand Down Expand Up @@ -329,6 +332,9 @@
i256:
TO_INT256()
ret jump
u256:
TO_UINT256()
ret jump


ret:
Expand Down
1 change: 1 addition & 0 deletions src/IHuffCastLibImportoor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ interface IHuffCastLibImportoor {
function toUint24(uint256) external view returns (uint24);
function toUint240(uint256) external view returns (uint240);
function toUint248(uint256) external view returns (uint248);
function toUint256(int256) external view returns (uint256);
function toUint32(uint256) external view returns (uint32);
function toUint40(uint256) external view returns (uint40);
function toUint48(uint256) external view returns (uint48);
Expand Down
14 changes: 14 additions & 0 deletions test/HuffCastLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -934,5 +934,19 @@ contract HuffCastTest is Test {
vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector
huffUser.toInt256(value);
}

// int256 to uint256
function testHappyToUint256(int256 value) public {
vm.assume(value >= 0);

uint256 castingResult = huffUser.toUint256(value);
assertEq(castingResult, uint256(value));
}
function testUnappyToUint256(int256 value) public {
vm.assume(value < 0);

vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector
huffUser.toUint256(value);
}
}

0 comments on commit ee40d63

Please sign in to comment.