Skip to content

Commit dc24fe9

Browse files
committed
Cleanup
1 parent 07dec3c commit dc24fe9

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/utils/512Math.sol

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,10 +1477,10 @@ library Lib512MathArithmetic {
14771477
// Extract the upper 6 bits of `M` to be used as a table index. `M >> 250 < 16` is
14781478
// invalid (that would imply M<½), so our lookup table only needs to handle only 16
14791479
// through 63.
1480-
let i := shr(0xfa, M)
1480+
let Mbucket := shr(0xfa, M)
14811481
// We can't fit 48 seeds into a single word, so we split the table in 2 and use `c`
14821482
// to select which table we index.
1483-
let c := lt(0x27, i)
1483+
let c := lt(0x27, Mbucket)
14841484

14851485
// Each entry is 10 bits and the entries are ordered from lowest `i` to
14861486
// highest. The seed is the value for `Y` for the midpoint of the bucket, rounded
@@ -1490,13 +1490,13 @@ library Lib512MathArithmetic {
14901490
let table := xor(table_hi, mul(xor(table_lo, table_hi), c))
14911491

14921492
// Index the table to obtain the initial seed of `Y`.
1493-
let shift := add(0x186, mul(0x0a, sub(mul(0x18, c), i)))
1494-
// We begin the Newton-Raphson iteraitons with `Y` in Q247.9 format.
1493+
let shift := add(0x186, mul(0x0a, sub(mul(0x18, c), Mbucket)))
1494+
// We begin the Newton-Raphson iterations with `Y` in Q247.9 format.
14951495
Y := and(0x3ff, shr(shift, table))
14961496

1497-
// The worst-case seed for `Y` occurs when `i = 16`. For monotone quadratic
1497+
// The worst-case seed for `Y` occurs when `Mbucket = 16`. For monotone quadratic
14981498
// convergence, we desire that 1/√3 < Y·√M < √(5/3). At the boundaries (worst case)
1499-
// of the `i = 16` bucket, we are 0.407351 (41.3680%) from the lower bound and
1499+
// of the `Mbucket = 16` range, we are 0.407351 (41.3680%) from the lower bound and
15001500
// 0.275987 (27.1906%) from the higher bound.
15011501
}
15021502

@@ -1539,18 +1539,18 @@ library Lib512MathArithmetic {
15391539
// For small `e` (lower values of `x`), we can skip the 5th N-R iteration. The
15401540
// correct bits that this iteration would obtain are shifted away during the
15411541
// denormalization step. This branch is net gas-optimizing.
1542-
uint256 Y2 = Y * Y; // scale: 2²⁵⁴
1543-
uint256 MY2 = _inaccurateMulHi(M, Y2); // scale: 2²⁵⁴
1544-
uint256 T = 1.5 * 2 ** 254 - MY2; // scale: 2²⁵⁴
1545-
Y = _inaccurateMulHi(Y << 2, T); // scale: 2¹²⁷
1542+
uint256 Y2 = Y * Y; // scale: 2²⁵⁴
1543+
uint256 MY2 = _inaccurateMulHi(M, Y2); // scale: 2²⁵⁴
1544+
uint256 T = 1.5 * 2 ** 254 - MY2; // scale: 2²⁵⁴
1545+
Y = _inaccurateMulHi(Y << 2, T); // scale: 2¹²⁷
15461546
}
15471547
// `Y` is Q129.127
15481548
{
1549-
uint256 Y2 = Y * Y; // scale: 2²⁵⁴
1550-
uint256 MY2 = _inaccurateMulHi(M, Y2); // scale: 2²⁵⁴
1551-
uint256 T = 1.5 * 2 ** 254 - MY2; // scale: 2²⁵⁴
1552-
Y = _inaccurateMulHi(Y << 128, T); // scale: 2²⁵³
1553-
Y <<= 2; // scale: 2²⁵⁵ (Q1.255 format; effectively Q1.253)
1549+
uint256 Y2 = Y * Y; // scale: 2²⁵⁴
1550+
uint256 MY2 = _inaccurateMulHi(M, Y2); // scale: 2²⁵⁴
1551+
uint256 T = 1.5 * 2 ** 254 - MY2; // scale: 2²⁵⁴
1552+
Y = _inaccurateMulHi(Y << 128, T); // scale: 2²⁵³
1553+
Y <<= 2; // scale: 2²⁵⁵ (Q1.255 format; effectively Q1.253)
15541554
}
15551555
// `Y` is Q1.255
15561556

test/0.8.25/512Math.t.sol

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ contract Lib512MathTest is Test {
219219
assertTrue(r == e);
220220
}
221221

222-
function test512Math_sqrt(uint256 x_hi, uint256 x_lo) public pure {
222+
function test512Math_sqrt(uint256 x_hi, uint256 x_lo) external pure {
223223
uint512 x = alloc().from(x_hi, x_lo);
224224
uint256 r = x.sqrt();
225225

@@ -238,8 +238,4 @@ contract Lib512MathTest is Test {
238238
assertTrue((r2_hi > x_hi) || (r2_hi == x_hi && r2_lo > x_lo), "sqrt too low");
239239
}
240240
}
241-
242-
function test512Math_sqrt_table() external pure {
243-
test512Math_sqrt(0x000000000000000000000000000000000000000580398dae536e7fe242efe66a, 0x0000000000000000001d9ad7c2a7ff6112e8bfd6cb5a1057f01519d7623fbd4a);
244-
}
245241
}

0 commit comments

Comments
 (0)