Skip to content

⚡️ Cheaper expWad and lnWad via symmetric rationals - #1546

Open
ddallaire wants to merge 1 commit into
Vectorized:mainfrom
ddallaire:perf/expwad-lnwad-symmetry
Open

⚡️ Cheaper expWad and lnWad via symmetric rationals#1546
ddallaire wants to merge 1 commit into
Vectorized:mainfrom
ddallaire:perf/expwad-lnwad-symmetry

Conversation

@ddallaire

Copy link
Copy Markdown

Description

Cheaper expWad and lnWad using symmetric rational approximations, and a round-to-nearest 2⁹⁶-precision final step for lambertW0Wad.

expWad: a diagonal rational approximant of exp satisfies q(v) = p(-v), so splitting the numerator into even and odd parts p = E(v²) + v·O(v²) makes the denominator E − v·O, i.e. free. The core drops from 8 muls / 11 constants to 5 / 5 (−16% gas).

lnWad: ln(x) = ln2/2 + 2·atanh(s) with s = (x − √2)/(x + √2). atanh is odd, so the rational only has odd terms, and the √2 centering costs nothing (the tail's k·ln2 just becomes (2k+1)·ln2/2) while shrinking the fit domain enough for a (3,3) rational in . 12 muls / 14 constants become 7 / 7 (−13% gas). Floor rounding convention unchanged, none of the existing ln test vectors move, and lnWad(1e18) is now exactly 0.

lambertW0Wad: the final Iacono–Boyd step inherited lnWad's last-wei rounding noise, which is what the hand-tuned nudges were calibrated against. It now inlines the ln core at 2⁹⁶ precision and rounds once, putting W within one wei of correctly rounded in that branch (98% exactly rounded, from 46%), and slightly cheaper overall since the cheaper exp/ln inside the iterations pay for it.

Measured accuracy is unchanged against a 140-digit reference: exp at the same 9.68e-18 pipeline floor with the same worst cases, ln within 1 wei. Every constant is reproducible from a script, unlike the previous ones (see the note in the test file). Details, derivations, benchmarks, and proofs here.

Also updated the exact test vectors that moved; several known values are now the correctly rounded ones (exp(-3), the omega constant, W(2**71 - 1), W(2**80 - 1)), and the rest reshuffle by a wei in both directions within the unchanged error envelope, net toward truth (details in the repo above).

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Ran forge fmt?
  • Ran forge test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant