←
atarpara375 posts
atarpara avatar

atarpara

@atarpara

Open to work | ✯ solady.org | prev. @onchainheroes

◷ Joined January 2015▧ 34 Photos and videos
atarpara avatar
atarpara@atarpara·May 16

New article: deriving the math behind Solady’s `cbrt(x)` implementation from first principles. What is the 0x90b5e5 constant actually doing? original implementation by @duncancmt * Why cube root needs magic multipliers * Why sqrt does not * Optimal Initial guess * Exactly 5 Newton Rashson iterations Full blog post: atarpara.github.io/solady-cb…

Post attachment
atarpara avatar
atarpara@atarpara·May 30

4/4 DUP3 + SWAP2 = 6 gas. Two opcodes. That's the entire delta. Solady already uses this in FixedPointMathLib.sol (fullMulDiv): r := mulmod(x, y, d) ← before check t := and(d, sub(0, d)) ← before check if iszero(...) { revert } Rule: batch intermediates before the revert check. One unified block → compiler plans the full layout. JUMPI in the middle → fall-through starts blind.

atarpara avatar
atarpara@atarpara·May 30

3/4 JUMPI pops condition + destination — whatever's left is exactly what the fall-through block opens with. Compare both snapshots right after JUMPI: D1 → [lo, rem, d, extra] all 4 live — SUB/DIV/ADD fire directly D2 → [b, a, d, lo] rem and extra don't exist yet In D2, MULMOD needs [a, b, d] on top. Block B opens with [b, a, d, lo]. Two ops fix it: DUP3 [d, b, a, d, lo] <--- copy d SWAP2 [a, b, d, d, lo] <--- reorder args for MULMOD

atarpara avatar
atarpara@atarpara·May 30

2/4 The EVM has a strict LIFO stack. Accessing variables buried deep requires expensive DUP and SWAP opcodes (3 gas each). In D1, declaring rem and extra BEFORE the check stages all four locals on top of the stack by the time iszero runs: D1 — stack at if iszero(d) [d, lo, rem, extra] <- extra at top All 4 values within DUP1–DUP4. Perfectly aligned for add(div(sub(lo, rem), d), extra). Zero extra ops after the check.

atarpara avatar
atarpara@atarpara·May 30

1/4 Answer: Version D1 is cheaper by 6 gas! 🎉 This behavior is caused by the Solidity compiler's internal "stack scheduling" heuristic during code generation. It seems counterintuitive to delay a revert check, but on the happy path, it makes a massive difference to stack layout. Here is exactly why:

Solidity Compiler Quiz Challenge Both functions calculate the exact same mathematical expression. Which version is cheaper on the happy path, and why? 👇 (Spoiler: This is used in `solady.fullMulDiv()`)

Post attachment
atarpara avatar
atarpara@atarpara·May 30

Solady update: Added a CLZ variant to LibRLP. LibRLP.encodeUint() is now 118 gas cheaper. PR: github.com/Vectorized/solady… More optimizations coming soon.

atarpara avatar
atarpara@atarpara·May 28

Solidity Compiler Quiz Challenge Both functions calculate the exact same mathematical expression. Which version is cheaper on the happy path, and why? 👇 (Spoiler: This is used in `solady.fullMulDiv()`)

Post attachment
atarpara avatar
atarpara@atarpara·May 14

10 mathematical facts packed into 8 lines of Yul. Deep math beats brute force in EVM optimization. Thanks to @duncancmt for formally verifying this in 0x-settler. Full blog post link : atarpara.github.io/solady-sq…

atarpara avatar
atarpara@atarpara·May 14

The quirk: Integer Babylonian iterations get stuck bouncing between floor and ceil forever if x+1 is a perfect square (like x=15). `sub(z, lt(div(x, z), z))` is the floor correction. It catches the cycle and subtracts 1.

atarpara avatar
atarpara@atarpara·May 14

Why 6 iterations and not 5 or 7? Because 5 leaves us at 80 bits (under target). 7 is wasted gas. 6 is the mathematically optimal unroll to clear 128 bits with a safety margin for integer rounding.

atarpara avatar
atarpara@atarpara·May 14

With CLZ: the initial guess starts at ~1.27 bits of accuracy. Thanks to quadratic convergence, the error squares every step. 6 iterations gets us to 161 bits of accuracy, safely clearing the 128 bits needed for a uint256 max result.

atarpara avatar
atarpara@atarpara·May 14

The classic integer sqrt optimization: hunt for magic constants to tighten the initial guess (à la Quake's Fast InvSqrt). But: the Newton-Raphson error map for sqrt is perfectly symmetric. Any multiplier makes the worst-case error strictly worse. 1 is optimal.

atarpara avatar
atarpara@atarpara·May 14

New article: deriving the math behind Solady's 67-byte, 114-gas sqrt(x) from first principles. The Solady CLZ PR just merged! 🚀 github.com/Vectorized/solady… No magic multipliers Exactly 6 Babylonian iterations 1 floor correction Full derivation 👇

Post attachment
Jarrod Watts avatar
⟳ atarpara retweeted
Jarrod Watts@jarrodwatts·1 Dec 2025

Ethereum’s next major upgrade, “Fusaka”, goes live in less than 48 hours. I read all 13 EIPs being included so you don’t have to. So, here are 13 tweets (with diagrams) to explain the 13 upgrades in simple terms: 🧵

Post attachment