Repository navigation
perf: Exp is a port of Arm's optimized-routines exp, 2-3.5x math.Exp - #15
Merged
Merged
Conversation
Exp went through Map(math.Exp), and math.Exp itself was the cost: 13.2 ns per element on a Zen 3 core, 3.9 ns on M4. Exp now runs a port of the double-precision exp of Arm's optimized-routines (also glibc's since 2.28; MIT licence, attributed in the file): a 128-entry 2^(k/N) table and a degree-5 polynomial, with exp.c's special-case handling for overflow and the subnormal range. The common case is written out in the loop: the function does not inline, and a call per element cost a third of the time. Accuracy, against a 300-bit math/big reference (ln 2 from its atanh series): worst 0.504 ULP over 80 000 inputs covering the whole finite range, near 0, the overflow edge and the subnormal range (Arm documents 0.509; math.Exp measured 0.879). The table is not trusted as copied: TestExpTable re-derives all 256 entries from 2^(k/128) at 300 bits. It also removes a bug go-ndarray inherited: Go 1.26's amd64 math.Exp returns +Inf for x >= 1023.5*ln2 ~ 709.436, though exp is finite up to 709.7827 (numpy: exp(709.5) = 1.3549863193146328e+308). Its assembly rounds k = x*log2(e) to 1024 there and the biased-exponent check calls it overflow. Confirmed on a real Zen 3, not only Rosetta; TestExpTopOfRange fails on it with the old Map(math.Exp). Speed, kernel alone at 16 Ki: M4 30 us (math.Exp 65), one Zen 3 core 62 us (math.Exp 215, NumPy 78). Whole Exp on Zen 3, 16 cores: x2.7 at 1 Ki, x3 at 16 Ki, x1.6 at 4 Mi; 2-5x NumPy from 256 Ki on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed and not shipped Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Exp went through Map(math.Exp), and math.Exp itself was the cost: 13.2 ns
per element on a Zen 3 core, 3.9 ns on M4. Exp now runs a port of the
double-precision exp of Arm's optimized-routines (also glibc's since 2.28;
MIT licence, attributed in the file): a 128-entry 2^(k/N) table and a
degree-5 polynomial, with exp.c's special-case handling for overflow and
the subnormal range. The common case is written out in the loop: the
function does not inline, and a call per element cost a third of the time.
Accuracy, against a 300-bit math/big reference (ln 2 from its atanh
series): worst 0.504 ULP over 80 000 inputs covering the whole finite
range, near 0, the overflow edge and the subnormal range (Arm documents
0.509; math.Exp measured 0.879). The table is not trusted as copied:
TestExpTable re-derives all 256 entries from 2^(k/128) at 300 bits.
It also removes a bug go-ndarray inherited: Go 1.26's amd64 math.Exp
returns +Inf for x >= 1023.5ln2 ~ 709.436, though exp is finite up to
709.7827 (numpy: exp(709.5) = 1.3549863193146328e+308). Its assembly
rounds k = xlog2(e) to 1024 there and the biased-exponent check calls it
overflow. Confirmed on a real Zen 3, not only Rosetta;
TestExpTopOfRange fails on it with the old Map(math.Exp).
Speed, kernel alone at 16 Ki: M4 30 us (math.Exp 65), one Zen 3 core
62 us (math.Exp 215, NumPy 78). Whole Exp on Zen 3, 16 cores: x2.7 at
1 Ki, x3 at 16 Ki, x1.6 at 4 Mi; 2-5x NumPy from 256 Ki on.
🤖 Generated with Claude Code