Skip to content

Commit

Permalink
Add CLEAR_BYTE() macro
Browse files Browse the repository at this point in the history
  • Loading branch information
nfurfaro committed Oct 15, 2023
1 parent a14349d commit 8be928b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/Huffbits.huff
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define function writeNibble(uint256, uint256, uint256) pure returns(uint256)
#define function clearNibble(uint256, uint256) pure returns(uint256)
#define function queryNibble(uint256, uint256) pure returns(uint256)
#define function clearByte(uint256, uint256) pure returns(uint256)

#define constant MAX = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Expand Down Expand Up @@ -172,4 +173,11 @@
0x04 // [4, index, and]
mul // [prod, and]
shr // [shifted]
}

/// Given a bitmap, clear a nibble if set, otherwise return the original bitmap.
#define macro CLEAR_BYTE() = takes(2) returns(1) {
// input stack // [index, bitmap]
BYTEMASK() // [mask, bitmap]
and
}
6 changes: 5 additions & 1 deletion test/Huffbits.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface Huffbits {
function writeNibble(uint256, uint256, uint256) external pure returns(uint256);
function clearNibble(uint256, uint256) external pure returns(uint256);
function queryNibble(uint256, uint256) external pure returns(uint256);
function clearByte(uint256, uint256) external pure returns(uint256);
}


Expand Down Expand Up @@ -151,7 +152,10 @@ contract HuffbitsTest is Test {
assertEq(huffbits.byteMask(31), 0x00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
}

// byteMask
function testClearByte() public {
assertEq(huffbits.clearByte(1, 0), 0);
}

// clearByte
// WriteByte
// queryByte
Expand Down
23 changes: 17 additions & 6 deletions test/mocks/HuffbitsWrappers.huff
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,25 @@
}

#define macro CLEAR_NIBBLE_WRAPPER() = {
0x04 calldataload // [arg1]
0x24 calldataload // [arg2, arg1]
CLEAR_NIBBLE() // [cleared]
0x04 calldataload // [arg1]
0x24 calldataload // arg2, arg1]
CLEAR_NIBBLE() // [cleared]
0x00 mstore
0x20 0x00 return
}

#define macro QUERY_NIBBLE_WRAPPER() = {
0x04 calldataload // [arg1]
0x24 calldataload // [arg2, arg1]
QUERY_NIBBLE() // [result]
0x04 calldataload // [arg1]
0x24 calldataload // [arg2, arg1]
QUERY_NIBBLE() // [result]
0x00 mstore
0x20 0x00 return
}

#define macro CLEAR_BYTE_WRAPPER() = {
0x04 calldataload // [arg1]
0x24 calldataload //[arg2, arg1]
CLEAR_BYTE() // [cleared]
0x00 mstore
0x20 0x00 return
}
Expand All @@ -97,6 +105,7 @@
dup1 __FUNC_SIG(writeNibble) eq writeNibble jumpi
dup1 __FUNC_SIG(clearNibble) eq clearNibble jumpi
dup1 __FUNC_SIG(queryNibble) eq queryNibble jumpi
dup1 __FUNC_SIG(clearByte) eq clearByte jumpi


0x00 0x00 revert
Expand All @@ -123,4 +132,6 @@
CLEAR_NIBBLE_WRAPPER()
queryNibble:
QUERY_NIBBLE_WRAPPER()
clearByte:
CLEAR_BYTE_WRAPPER()
}

0 comments on commit 8be928b

Please sign in to comment.