Repository navigation
Conversation
…ions
Replace the Horner-form rational evaluations in FixedPointMathLib.expWadFast
and FixedPointMathLib.lnWadFast (both src/utils/FixedPointMathLib.sol and
src/utils/clz/FixedPointMathLib.sol) with lattice-refined Remez continued
fractions, tightening expWadFast error bounds by 16+ orders of magnitude while
reducing gas on all four compiler/EVM profiles.
- expWadFast:
- Replace the two-step WAD-to-Q96 division and k*ln(2) subtraction with a
single Q202 base-2 multiply and shift extraction to a Q129 fractional
octave f.
- Evaluate 2**f = 1 + 2*f / (C(f**2) - f) via a 10-term Q129 continued
fraction fitted to 2**t * exp(-a*t) (odd tilt balancing the -3 * 2**-130
one-sided margin) with LLL/Babai lattice refinement on the low bits.
- Tighten the certified one-sided relative shortfall from 2.58e-22 to
< 1.7237 * 2**-128 (< 5.07e-39), expanding the exact floor(E) or
floor(E) - 1 domain from x <= 8265113944572514620 (~3885 * 1e18) to
x <= 46731834294601968657 (~1.97e38, covering all int128 values), while
keeping r <= E, expWadFast(0) == 1e18, monotonicity across all 2**k seams,
and identical domain/revert boundaries.
- Single-call gas (solc 0.8.35, 1000 runs): 387 -> 358 (Paris legacy),
353 -> 300 (Paris via-ir), 383 -> 354 (Osaka legacy), 350 -> 297
(Osaka via-ir).
- lnWadFast:
- Normalize to Q71 and center on round(sqrt(2) * 2**71) using sub(c, x) to
avoid pushing a second negated literal.
- Evaluate 2 * 1e18 * atanh(s) via a 7-term Q70 continued fraction with
alternating gauge scaling alpha = 2**61 / 1e18 (all 7 constants fit 9
bytes and sdiv(s << 81, d) lands directly in Q20 WAD without a WAD
multiply) and LLL/Babai lattice refinement on the low bits.
- Preserve r in {floor(L), floor(L) - 1}, lnWadFast(1e18) == 0, global
monotonicity, and LnWadUndefined() reverts.
- Single-call gas and runtime size (solc 0.8.35, 1000 runs):
536g/535B -> 456g/405B (Paris legacy), 446g/507B -> 378g/384B (Paris
via-ir), 397g/400B -> 311g/265B (Osaka legacy), 309g/372B -> 241g/248B
(Osaka via-ir).
- Credit David Dallaire (ddallaire) in NatSpec for both functions, update
generated docs and golden/seam tests (including worst-case vectors and
fixing the double-wrapping bug in testExpWadFastMonotonicallyIncreasingAround).
Co-Authored-By: Gemini 4 Neon <noreply@google.com>
Co-Authored-By: Cursor <cursoragent@cursor.com>
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Apply optimizations and polish from GPT-6.2 Astra adversarial review:
- expWadFast:
- Retain all 202 fractional bits in the linear numerator `n = shl(54, p)`
instead of shifting `f = sar(127, n)` back left by 127, decrement `D0` by
1 to center the `f`-floor bias in `den`, pin `x == 0` via `iszero(x)`,
and place the WAD multiplier literal first in `mul(...)`.
- Tightens the pre-floor relative shortfall bound from `1.7237 * 2**-128`
to `1.4307 * 2**-128` (`< 4.21e-39`), expanding the `floor(E)` or
`floor(E) - 1` domain to `x <= 46918143602790025991` (`~2.38e38`, 1.40x
`type(int128).max`), while shrinking runtime bytecode across all 8
compiler/EVM/runs configurations and cutting single-call gas by 1 gas in
legacy and 6-9 gas in `--via-ir` (`294` Paris / `291` Osaka at 1000 runs).
- lnWadFast:
- Compute the centered Mobius argument via unsigned division as
`s = shl(70, 1) - div(shl(71, c), add(x, c))` and add the final signed
quotient to `BIAS - r * LN2`.
- Saves 6-12 gas across all 8 compiler/EVM/runs configurations (`444` Paris
legacy, `372` Paris via-ir, `305` Osaka legacy, `232` Osaka via-ir) and
tightens pre-floor shortfall to `< 0.0860` wei while preserving
`r in {floor(L), floor(L) - 1}` and `lnWadFast(1e18) == 0`.
- Docs, comments, credit, and tests:
- Credit both David Dallaire (ddallaire) and Duncan Townsend (duncancmt).
- Remove unqualified relative-error phrasing from `expWadFast` NatSpec and
differential test comments, state the overflow cutoff explicitly, use
"nondecreasing", and align all inline rounding/bias figures with the
pure exact-rational Bernstein proofs.
- Update `testExpWadFast` golden vectors (including the current octave 0->1
boundary `346573590279972654/346573590279972655` and new threshold
`46918143602790025991`) and replace binary search in `_expWadFastSeamOf`
with direct ceiling division (10x faster seam test).
Co-Authored-By: Gemini 4 Neon <noreply@google.com>
Co-Authored-By: Cursor <cursoragent@cursor.com>
Co-Authored-By: GPT-6.2 Astra <noreply@openai.com>
Gas Snapshot Comparison Report
|
2 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WIP: stacked on top of #1546
Description
Describe the changes made in your pull request here.
Checklist
Ensure you completed all of the steps below before submitting your pull request:
forge fmt?forge test?Pull requests with an incomplete checklist will be thrown out.