Skip to content

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

Description

@pcshrosbree

TL;DR: For double, the vectorized TensorPrimitives.Exp10 and Exp2 compute Exp(x * ln(b)), which loses accuracy as |x| grows: up to 1092 and 721 ulps in my measurements, and Exp2(1024) returns a finite value instead of +infinity. Dedicated double kernels in a draft PR measured at most 2 ulps and are also faster. I am asking whether you would take these kernels, and whether they should live in System.Numerics.Tensors or in VectorMath.

Description

For double, the vectorized path of TensorPrimitives.Exp10 and TensorPrimitives.Exp2 computes Exp(x * ln(b)). #97623 introduced this as a stopgap ("for now"). Rounding ln(b) to a double, and then rounding the product, leaves an absolute error in the argument that grows with |x|. Exp turns an absolute error in its argument into the same relative error in its result. Near the ends of the input ranges below, the relative error is about 1e-13, or several hundred ulps. In these measurements, the scalar double.Exp10 and double.Exp2 stayed within 1 ulp.

I compared against a reference computed in 40-digit decimal arithmetic and rounded to double, for 2^20 random inputs drawn from [-323, 308.25) for Exp10 and [-1074, 1024) for Exp2. These ranges cover nearly every input with a finite, nonzero result; "whole range" below means these ranges.

Largest error, whole range Largest error, x in [-1, 1)
TensorPrimitives.Exp10<double> 1092 ulps 4 ulps
TensorPrimitives.Exp2<double> 721 ulps 2 ulps

Some exact cases are visibly wrong:

double[] x = [1023, 1024];
double[] d = new double[2];
TensorPrimitives.Exp2<double>(x, d);
// d[0] == 8.98846567431186E+307, but 2^1023 is 8.98846567431158E+307
// d[1] == 1.7976931348622732E+308, but 2^1024 overflows to +infinity

The existing random-input tests use doubles from [-1, 1), with tolerances of 1e-13 (Exp10) and 1e-14 (Exp2) set in #99708, so they do not catch this.

A fix that is also faster

I am opening a draft PR with a fix; it will link to this issue. It adds dedicated kernels for double that reduce the argument in the base's own terms instead of forming x * ln(b) first:

  • Exp2: n = round(x), so x − n is exact.
  • Exp10: n = round(x · log2(10)), and x − n · log10(2) is formed with log10(2) split into two doubles, so the reduction stays accurate whether or not multiply-add is fused.

Both then reuse the polynomial of VectorMath.ExpDouble. A separate vector path handles vectors with NaN or ±infinity inputs or with lanes outside conservative bounds. Every input whose result is subnormal, zero or infinite lies outside those bounds. ExpDouble instead falls back to scalar code for vectors with NaN or very large lanes.

I tested the PR's library on the same inputs, plus the 2^15 inputs around the overflow threshold and edge cases such as NaN, ±infinity and -0. The largest error was 2 ulps for both functions. The tested configurations include Vector512, Vector256 and Vector128 on x64, runs without FMA, and runs on arm64. Every tested result agrees with the reference on whether it is NaN, infinite or finite. Exp2(1023) returns 2^1023, and Exp2(1024) returns +infinity.

The PR is also faster. I timed TensorPrimitives.Exp10 and Exp2 over 4,096 doubles on .NET 10.0.12, using inputs in [-300, 300] for Exp10 and [-1000, 1000] for Exp2, where every result is a normal double. Timings are in ns per element:

Exp10 shipped Exp10 PR Exp2 shipped Exp2 PR
Ryzen 9 9950X, Vector512 0.46 0.36 0.44 0.31
Ryzen 9 9950X, Vector256 0.70 0.57 0.69 0.46
Ryzen 9 9950X, Vector128 1.37 1.11 1.36 0.90
Apple M5 Max, 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, the PR is 1.4 to 4.2 times as fast as the shipped code.

Questions

  1. Would you take these kernels for the double paths of Exp10Operator and Exp2Operator, along with tests that cover the whole range?
  2. Should the kernels live in System.Numerics.Tensors, as in the draft, or in VectorMath next to ExpDouble, for reuse by Vector128.Exp10, Vector128.Exp2 and their wider forms if those APIs are added?

The float Exp10 overflow bug (#135718) is separate; my PR #136011 fixes it.

Activity

  1. added
    untriaged
    on Oct 19, 2026
  2. dotnet-policy-service commented on Oct 19, 2026

    @dotnet-policy-service
    Contributor

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Numericsuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions