Skip to content

Commit

Permalink
⚡️ Optimize cmp (#1257)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized authored Dec 29, 2024
1 parent b9c8546 commit cfd30a1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/utils/LibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -624,19 +624,19 @@ library LibBytes {
for { let i := 0x20 } 1 {} {
let x := mload(add(a, i))
let y := mload(add(b, i))
if or(xor(x, y), eq(i, n)) {
result := sub(gt(x, y), lt(x, y))
break
if iszero(or(xor(x, y), eq(i, n))) {
i := add(i, 0x20)
continue
}
i := add(i, 0x20)
result := sub(gt(x, y), lt(x, y))
break
}
}
// forgefmt: disable-next-item
if iszero(result) {
let x := and(mload(add(add(a, 0x20), n)),
not(shr(mul(7, sub(aLen, n)), shr(sub(aLen, n), not(result)))))
let y := and(mload(add(add(b, 0x20), n)),
not(shr(mul(7, sub(bLen, n)), shr(sub(bLen, n), not(result)))))
let l := 0x201f1e1d1c1b1a191817161514131211100f0e0d0c0b0a090807060504030201
let x := and(mload(add(add(a, 0x20), n)), shl(shl(3, byte(sub(aLen, n), l)), not(0)))
let y := and(mload(add(add(b, 0x20), n)), shl(shl(3, byte(sub(bLen, n), l)), not(0)))
result := sub(gt(x, y), lt(x, y))
if iszero(result) { result := sub(gt(aLen, bLen), lt(aLen, bLen)) }
}
Expand Down
16 changes: 8 additions & 8 deletions src/utils/g/LibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -628,19 +628,19 @@ library LibBytes {
for { let i := 0x20 } 1 {} {
let x := mload(add(a, i))
let y := mload(add(b, i))
if or(xor(x, y), eq(i, n)) {
result := sub(gt(x, y), lt(x, y))
break
if iszero(or(xor(x, y), eq(i, n))) {
i := add(i, 0x20)
continue
}
i := add(i, 0x20)
result := sub(gt(x, y), lt(x, y))
break
}
}
// forgefmt: disable-next-item
if iszero(result) {
let x := and(mload(add(add(a, 0x20), n)),
not(shr(mul(7, sub(aLen, n)), shr(sub(aLen, n), not(result)))))
let y := and(mload(add(add(b, 0x20), n)),
not(shr(mul(7, sub(bLen, n)), shr(sub(bLen, n), not(result)))))
let l := 0x201f1e1d1c1b1a191817161514131211100f0e0d0c0b0a090807060504030201
let x := and(mload(add(add(a, 0x20), n)), shl(shl(3, byte(sub(aLen, n), l)), not(0)))
let y := and(mload(add(add(b, 0x20), n)), shl(shl(3, byte(sub(bLen, n), l)), not(0)))
result := sub(gt(x, y), lt(x, y))
if iszero(result) { result := sub(gt(aLen, bLen), lt(aLen, bLen)) }
}
Expand Down

0 comments on commit cfd30a1

Please sign in to comment.