Skip to content

Commit a13a6b4

Browse files
committed
Golf
1 parent fec48d1 commit a13a6b4

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/utils/512Math.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ library Lib512MathArithmetic {
14551455
}
14561456
}
14571457

1458-
// gas benchmark 2025/09/18: ~1865 gas
1458+
// gas benchmark 2025/09/18: ~1730 gas
14591459
function sqrt(uint512 x) internal pure returns (uint256 r) {
14601460
(uint256 x_hi, uint256 x_lo) = x.into();
14611461

@@ -1533,9 +1533,9 @@ library Lib512MathArithmetic {
15331533
/// M·Y ≈ 2⁽⁵¹⁰⁻ᵉ⁾ · √x
15341534
/// r0 = ⌊M·Y / 2⁽⁵¹⁰⁻ᵉ⁾⌋ ≈ ⌊√x⌋
15351535
// We shift right by `510 - e` to account for both the Q1.255 scaling and
1536-
// denormalization
1537-
(uint256 r0_hi, uint256 r0_lo) = _mul(M, Y);
1538-
(, uint256 r0) = _shr(r0_hi, r0_lo, 254 + invE);
1536+
// denormalization. We don't care about accuracy in the low bits of `r0`, so we can cut
1537+
// some corners.
1538+
(, uint256 r0) = _shr(_inaccurateMulHi(M, Y), 0, 254 + invE);
15391539

15401540
/// `r0` is only an approximation of √x, so we perform a single Babylonian step to fully
15411541
/// converge on ⌊√x⌋ or ⌈√x⌉. The Babylonian step is:

0 commit comments

Comments
 (0)