Skip to content

perf: Exp is a port of Arm's optimized-routines exp, 2-3.5x math.Exp - #15

Merged
tannevaled merged 2 commits into
mainfrom
perf/exp
Oct 4, 2026
Merged

tannevaled merged 2 commits into
mainfrom
perf/exp

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

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 = 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.

🤖 Generated with Claude Code

tannevaled and others added 2 commits October 4, 2026 16:14
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>
@tannevaled
tannevaled merged commit 3f7046f into main Oct 4, 2026
12 checks passed
@tannevaled
tannevaled deleted the perf/exp branch October 4, 2026 14:19
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