Skip to content

Compute Math functions as V8 does and link libm only when a program needs it - #2248

Closed
ctate wants to merge 1 commit into
mainfrom
ctate/compute-math-functions-a6c9a0
Closed

ctate wants to merge 1 commit into
mainfrom
ctate/compute-math-functions-a6c9a0

Conversation

@ctate

@ctate ctate commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator
  • Math.sin, exp, cbrt, atan2 and the other transcendentals, and **/Math.pow, now print exactly Node's values (a port of V8's fdlibm, and V8's pow rules).
  • Executables that don't call pow no longer load libm.so: hello world starts 12% faster.
  • Every Math function stays faster than Node; against glibc, exp, log and cos are slower and cbrt is faster. bench:runtime and the Benchmarks Game kit are neutral (typed-array-scan +1.4% from its Math.sin setup).

…eeds it

Math.sin, Math.exp, Math.cbrt, Math.atan2 and the other transcendental
functions called the platform libm, whose results differ from Node's in
the last bit for many inputs (Math.cbrt(0.3), Math.atan2(2.5, 3)). Node
computes them with V8's fdlibm port on every platform; scr_ieee754.c is
a C port of that file, and the compiled Math members call it.

Math.pow and ** follow V8's math::pow: a literal exponent 2 is x * x, a
literal 0.5 is sqrt(x + 0), and every other exponent calls scr_math_pow,
which answers V8's special cases (2, 0.5, a NaN base, +-1 ** +-Infinity)
before the platform pow. ** called libm's pow, which differs from x * x
and sqrt in the last bit for some inputs, and which LLVM may rewrite.

On glibc, the runtime defines floor, ceil, trunc, round, fmod, sqrt,
ldexp, scalbn and frexp (scr_glibc_math.c), each exact; floor, ceil and
trunc select SSE4.1's rounding instruction when the CPU has it, as
glibc's do. libm links as needed, so a program that does not call pow
starts without loading libm.so.
@vercel

vercel Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
scriptc Ready Ready Preview, v0 Oct 25, 2026 7:38pm UTC

@ctate

ctate commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator Author

Superseded by #2287.

@ctate ctate closed this Oct 26, 2026

This branch was successfully deployed

1 active deployment
Preview — 828f4196 Deployed Oct 25, 2026 by vercel[bot]
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