Skip to content

Compute double TensorPrimitives.Exp10 and Exp2 without rounding x * ln(b) - #136059

Draft
pcshrosbree wants to merge 1 commit into
dotnet:mainfrom
pcshrosbree:fix/tensors-exp10-exp2-double-accuracy
Draft

pcshrosbree wants to merge 1 commit into
dotnet:mainfrom
pcshrosbree:fix/tensors-exp10-exp2-double-accuracy

Conversation

@pcshrosbree

@pcshrosbree pcshrosbree commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: For double, the vectorized TensorPrimitives.Exp10 and Exp2 compute Exp(x * ln(b)), which loses accuracy as |x| grows (up to 1115 and 726 ulps), and Exp2(1024) returns a finite value instead of +∞. This PR adds double kernels that reduce the argument in the base's own terms; the largest measured error drops to 2 ulps, and the timings below are faster than the shipped code. I opened it as a draft for feedback on the two questions in #136058: whether to take this approach, and where the kernels should live.

Fixes #136058.

I opened this as a draft for feedback on the two questions in #136058: whether to take this approach, and where the kernels should live.

The bug

For double, the vectorized TensorPrimitives.Exp10 and Exp2 compute Exp(x * ln(b)). Rounding ln(b) to a double, and then rounding the product, leaves an absolute error in the argument that grows with |x|. Exp turns that argument error into the same relative error in the result; the measured errors are under Accuracy below. Exp2(1024) also returns 1.7976931348622732E+308 instead of +∞, which #136011 lists under "Not changed here". Exp10M1 and Exp2M1 use the same operators.

The fix

The new ExpDoubleCore kernels reduce the argument in the base's own terms instead of forming x * ln(b):

  • Exp2: n = round(x), so x − n is exact, and r = (x − n) · ln(2).
  • Exp10: n = round(x · log2(10)), and r = (x − n · log10(2)) · ln(10). log10(2) is split into a high part with 40 significant bits and a low part. n · high is exact for every n the kernel produces, so the reduction is accurate whether or not MultiplyAddEstimate fuses.

Both evaluate e^r with the polynomial of VectorMath.ExpDouble, copied with its AMD attribution. The fast path uses conservative bounds: |x| ≤ 1020 for Exp2 and |x| ≤ 307 for Exp10 in every lane. Within these bounds, every result is a normal double, and 2^n is applied by adding n to the exponent field. Otherwise a separate, non-inlined path clamps the input and scales by 2^n1 · 2^n2 with n1 + n2 = n, so a subnormal result is rounded once and an overflowing one becomes +∞. Some vectors whose results are all normal also take this path; for example, an input lane of 1023 exceeds the fast-path bound for Exp2. NaN inputs are returned unchanged. The shipped code falls back to scalar code for every vector with a NaN lane or a lane where |x · ln(b)| > 708, which is where ExpDouble stops using its vector path.

Exp10Operator and Exp2Operator call these kernels for double only; the float paths are unchanged. #136011 changes the same operators for float, so whichever of the two PRs merges second will need a small rebase.

Accuracy

I compared TensorPrimitives.Exp10 and Exp2 from the built library with a reference computed in 40-digit decimal arithmetic and rounded to double. The inputs were 2^20 random values from [-323, 308.25) for Exp10 and [-1074, 1024) for Exp2, 2^18 from [-1, 1), the 2^15 doubles around the overflow threshold, and 28 edge cases including NaN, ±∞ and -0.

Largest error before Largest error after
Exp10 1115 ulps 2 ulps
Exp2 726 ulps 2 ulps

In every configuration I tested, the largest error after the change is 2 ulps, and every tested result agrees with the reference on whether it is NaN, infinite or finite. I tested Vector512, Vector256 and Vector128 on x64; Vector128 on x64 with AVX2 (and so FMA) disabled; and Vector128 on arm64. Before the change, Exp2(1024) was finite instead of +∞.

Cost

The table reports timings of TensorPrimitives.Exp10 and Exp2 over 4,096 doubles, in ns per element. I used a simple timing loop, alternating the shipped package and this PR's library built for net10.0, and report the fastest of repeated runs. Both ran on .NET 10.0.12. The x64 runs were pinned to two cores on a shared Ryzen 9 9950X machine; the arm64 runs used an Apple M5 Max. The inputs are in [-300, 300] for Exp10 and [-1000, 1000] for Exp2, where every result is a normal double.

Exp10 shipped Exp10 this PR Exp2 shipped Exp2 this PR
x64 Vector512 0.46 0.36 0.44 0.31
x64 Vector256 0.70 0.57 0.69 0.46
x64 Vector128 1.37 1.11 1.36 0.90
arm64 Vector128 0.93 0.80 0.92 0.74

With inputs spread over [-323, 308) for Exp10 and [-1074, 1024) for Exp2, where some vectors have lanes outside the fast path's bounds, this PR is 1.4 to 4.2 times as fast as the shipped code.

Tests

The new DoubleGenericTensorPrimitives.Exp10AndExp2_WholeRange_WithinUlpsOfScalar test compares each result with double.Exp10 or double.Exp2, allowing 4 ulps. It compares in ulps rather than with a relative tolerance so that subnormal results are judged correctly. If either the result or the scalar method's value is NaN or infinite, the two must agree: both NaN, or the same infinity. The test does not check NaN payloads. The inputs are 2^16 random values across the whole range, the 64 doubles around each threshold where the result overflows, becomes subnormal or rounds to zero, every integer from -1080 to 1030, and special values. Without the fix, it fails for both functions; with the fix, it passes.

The System.Numerics.Tensors tests pass on linux-x64 Release, with 6268 tests and 0 failures. I ran them five times: by default (Vector512), with DOTNET_PreferredVectorBitWidth=256, with DOTNET_PreferredVectorBitWidth=128, with DOTNET_EnableHWIntrinsic=0, and with DOTNET_EnableAVX2=0.

For double, the vectorized Exp10 and Exp2 computed Exp(x * ln(b)).
Rounding ln(b) and the product leaves an absolute error in the argument
that grows with |x|, and Exp turns it into the same relative error in
the result: in my measurements, up to 1115 ulps for Exp10 and 726 for
Exp2. Exp2(1024) also returned a finite value instead of infinity.

Add dedicated kernels for double that reduce the argument in the
base's own terms. For 2^x, n = round(x) and x - n is exact. For 10^x,
n = round(x * log2(10)), and x - n * log10(2) is formed with log10(2)
split into a 40-bit high part and a low part, so the reduction is
accurate whether or not MultiplyAddEstimate fuses. Both then evaluate
the polynomial of VectorMath.ExpDouble. When every lane is within
conservative bounds (|x| <= 1020 for 2^x, |x| <= 307 for 10^x), every
result is a normal double and 2^n is applied by adding n to the
exponent field. Otherwise a separate path scales by two powers of two,
so subnormal results round once and large ones become infinity.

For 2^20 random inputs across the whole range, plus inputs next to
the overflow threshold and special values, both functions were within
2 ulps of a 40-digit decimal reference rounded to double. That held
with Vector512, Vector256 and Vector128 on x64, with Vector128 on x64
without FMA, and with Vector128 on arm64.

Exp10 was measured through TensorPrimitives on 4,096 doubles. On x64
with Vector512, the new code takes 0.36 ns per element against 0.46
for the shipped code. On arm64 with Vector128, it takes 0.80 ns per
element against 0.93.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 19, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

This branch has not been deployed

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

Labels

area-System.Numerics community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TensorPrimitives.Exp10 and Exp2 for double are off by hundreds of ulps on the vectorized path

1 participant