Skip to content

Commit

Permalink
toInt16
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowBirdy committed Dec 29, 2023
1 parent 9e0f2ff commit 0723553
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
43 changes: 42 additions & 1 deletion src/HuffCastLib.huff
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,45 @@
success jumpi
REVERT_OVERFLOW()
success:
}
}
#define macro TO_INT16() = takes (1) returns (1) {
0x8000
dup2
lt
success jumpi
REVERT_OVERFLOW()
success:
}
//define macros to_int24, to_int32, to_int40, to_int48, to_int56, to_int64, to_int72, to_int80, to_int88, to_int96, to_int104, to_int112, to_int120, to_int128, to_int136, to_int144, to_int152, to_int160, to_int168, to_int176, to_int184, to_int192, to_int200, to_int208, to_int216, to_int224, to_int232, to_int240, to_int248
#define macro TO_INT24() = takes (1) returns (1) {
0x800000
dup2
lt
success jumpi
REVERT_OVERFLOW()
success:
}
#define macro TO_INT32() = takes (1) returns (1) {
0x80000000
dup2
lt
success jumpi
REVERT_OVERFLOW()
success:
}
#define macro TO_INT40() = takes (1) returns (1) {
0x8000000000
dup2
lt
success jumpi
REVERT_OVERFLOW()
success:
}
#define macro TO_INT48() = takes (1) returns (1) {
0x800000000000
dup2
lt
success jumpi
REVERT_OVERFLOW()
success:
}
8 changes: 7 additions & 1 deletion src/HuffCastLibImportoor.huff
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#define function toUint240(uint256) view returns (uint240)
#define function toUint248(uint256) view returns (uint248)
#define function toInt8(uint256) view returns (int8)
#define function toInt16(uint256) view returns (int16)



Expand Down Expand Up @@ -72,7 +73,8 @@
dup2 __FUNC_SIG(toUint232) eq u232 jumpi
dup2 __FUNC_SIG(toUint240) eq u240 jumpi
dup2 __FUNC_SIG(toUint248) eq u248 jumpi
dup2 __FUNC_SIG(toInt8) eq i8 jumpi
dup2 __FUNC_SIG(toInt8) eq i8 jumpi
dup2 __FUNC_SIG(toInt16) eq i16 jumpi



Expand Down Expand Up @@ -175,6 +177,10 @@
i8:
TO_INT8()
ret jump
i16:
TO_INT16()
ret jump

ret:
0x00 mstore
0x20 0x00 return
Expand Down
16 changes: 16 additions & 0 deletions test/HuffCastLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ contract HuffCastTest is Test {
assertEq(abi.decode(abi.encode(casted8),(uint256)), value);
assertLe(casted8, type(int8).max);
}
function testHappyToInt16(uint256 value) public {
vm.assume(value <= type(uint16).max/2);

int16 casted16 = huffUser.toInt16(value);
assertEq(abi.decode(abi.encode(casted16),(uint256)), value);
assertLe(casted16, type(int16).max);

}
// unhappy paths
function testUnhappyToInt8(uint256 value) public {
vm.assume(value > type(uint8).max/2);
Expand All @@ -277,6 +285,12 @@ contract HuffCastTest is Test {
huffUser.toInt8(value);
}

function testUnhappyToInt16(uint256 value) public {
vm.assume(value > type(uint16).max/2);

vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector
huffUser.toInt16(value);
}
function testUnhappyToUint8(uint256 value) public {
vm.assume(value > type(uint8).max);

Expand Down Expand Up @@ -533,6 +547,8 @@ interface SafeCastLibImplementoor {
function toUint248(uint256 value) external returns (uint248);

function toInt8(uint256) external view returns (int8);
function toInt16(uint256) external view returns (int16);


error Overflow();
}

0 comments on commit 0723553

Please sign in to comment.