Sitelet https://github.com/Vectorized/solady/pull/1420/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/utils/libbit.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ function and(bool x, bool y) internal pure returns (bool z)

Returns `x & y`.

### and(bool,bool,bool)

```solidity
function and(bool w, bool x, bool y) internal pure returns (bool z)
```

Returns `w & x & y`.

### and(bool,bool,bool,bool)

```solidity
function and(bool v, bool w, bool x, bool y)
internal
pure
returns (bool z)
```

Returns `v & w & x & y`.

### rawOr(bool,bool)

```solidity
Expand All @@ -183,6 +202,25 @@ function or(bool x, bool y) internal pure returns (bool z)

Returns `x | y`.

### or(bool,bool,bool)

```solidity
function or(bool w, bool x, bool y) internal pure returns (bool z)
```

Returns `w | x | y`.

### or(bool,bool,bool,bool)

```solidity
function or(bool v, bool w, bool x, bool y)
internal
pure
returns (bool z)
```

Returns `v | w | x | y`.

### rawToUint(bool)

```solidity
Expand Down
34 changes: 33 additions & 1 deletion src/utils/LibBit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,22 @@ library LibBit {
}
}

/// @dev Returns `w & x & y`.
function and(bool w, bool x, bool y) internal pure returns (bool z) {
/// @solidity memory-safe-assembly
assembly {
z := iszero(or(iszero(w), or(iszero(x), iszero(y))))
}
}

/// @dev Returns `v & w & x & y`.
function and(bool v, bool w, bool x, bool y) internal pure returns (bool z) {
/// @solidity memory-safe-assembly
assembly {
z := iszero(or(or(iszero(v), iszero(w)), or(iszero(x), iszero(y))))
}
}

/// @dev Returns `x | y`. Inputs must be clean.
function rawOr(bool x, bool y) internal pure returns (bool z) {
/// @solidity memory-safe-assembly
Expand All @@ -231,7 +247,23 @@ library LibBit {
function or(bool x, bool y) internal pure returns (bool z) {
/// @solidity memory-safe-assembly
assembly {
z := or(iszero(iszero(x)), iszero(iszero(y)))
z := iszero(iszero(or(x, y)))
}
}

/// @dev Returns `w | x | y`.
function or(bool w, bool x, bool y) internal pure returns (bool z) {
/// @solidity memory-safe-assembly
assembly {
z := iszero(iszero(or(w, or(x, y))))
}
}

/// @dev Returns `v | w | x | y`.
function or(bool v, bool w, bool x, bool y) internal pure returns (bool z) {
/// @solidity memory-safe-assembly
assembly {
z := iszero(iszero(or(v, or(w, or(x, y)))))
}
}

Expand Down
8 changes: 6 additions & 2 deletions test/LibBit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ contract LibBitTest is SoladyTest {
}
}

function testAnd(bool x, bool y) public {
function testAnd(bool v, bool w, bool x, bool y) public {
assertEq(LibBit.and(x, y), x && y);
assertEq(LibBit.and(w, x, y), w && x && y);
assertEq(LibBit.and(v, w, x, y), v && w && x && y);
assertEq(LibBit.rawAnd(x, y), LibBit.and(x, y));
}

Expand All @@ -104,8 +106,10 @@ contract LibBitTest is SoladyTest {
}
}

function testOr(bool x, bool y) public {
function testOr(bool v, bool w, bool x, bool y) public {
assertEq(LibBit.or(x, y), x || y);
assertEq(LibBit.or(w, x, y), w || x || y);
assertEq(LibBit.or(v, w, x, y), v || w || x || y);
assertEq(LibBit.rawOr(x, y), LibBit.or(x, y));
}

Expand Down