Skip to content

Fix incorrect result for pow(x, 1) on ARM/ARM64 - #131286

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-arm-pow-x-1-fix
Jul 24, 2026
Merged

tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-arm-pow-x-1-fix

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Fixes #12139.

ARM/ARM64 return 0 for Math.Pow(double.Epsilon, 1) (and MathF.Pow(float.Epsilon, 1)) when they should return the input. The ARM/ARM64 CRT computes pow via exp(y * log(x)), which underflows a subnormal x to 0 when y is exactly 1. pow(x, 1) is x for all x (including NaN, +/-inf, +/-0), so an exact y == 1 early return is correct.

  • Add an ARM-guarded y == 1 fast path returning x to the Math.Pow/MathF.Pow runtime helpers in floatdouble.cpp/floatsingle.cpp.

  • Mirror the fixup in the JIT value-number constant folding (EvalMathFuncBinary). Folding Math.Pow(const, 1) uses the same buggy host pow when the JIT itself runs natively on ARM, which would otherwise defeat the helper fix and diverge from runtime behavior.

  • Add subnormal-x, y == 1 regression cases to the existing Math.Pow/MathF.Pow theory data.

The ARM #if branches can't be compiled or exercised on the x64 dev host; the expected test values were verified on x64 (Math.Pow(double.Epsilon, 1) == double.Epsilon and the float equivalent both hold), so the added cases pass on x64 and would fail on unfixed ARM. CI ARM legs will provide the real validation.

Note

This PR description and the code changes were drafted with GitHub Copilot.

The ARM/ARM64 CRT computes pow via exp(y * log(x)), which underflows a subnormal x to 0 for y == 1 rather than returning x. Add an ARM-guarded exact y == 1 fast path to the Math.Pow/MathF.Pow runtime helpers, and mirror it in the JIT value-number constant folding so ARM-targeted codegen stays consistent.

Co-authored-by: Copilot App <[email protected]>
@azure-pipelines

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses an ARM/ARM64 correctness issue where Math.Pow(x, 1) / MathF.Pow(x, 1) can incorrectly return 0 for subnormal x (e.g., double.Epsilon, float.Epsilon). The fix adds an ARM/ARM64-only y == 1 fast path in the CoreCLR math helpers, and mirrors the same behavior in the JIT’s value-number-based constant folding so JIT-time folding doesn’t reintroduce the wrong result when the JIT runs on ARM.

Changes:

  • Add ARM/ARM64-only y == 1 early-return for Pow in CoreCLR runtime helpers (floatsingle.cpp, floatdouble.cpp).
  • Align JIT constant folding (ValueNumStore::EvalMathFuncBinary) for Math.Pow/powf with the runtime helper behavior on ARM/ARM64.
  • Add regression test cases for x subnormal + y == 1 to existing Math.Pow / MathF.Pow test data.
Show a summary per file
File Description
src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/MathF.cs Adds float.Epsilon + y==1 regression case to MathF.Pow theory data.
src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs Adds double.Epsilon + y==1 regression case to Math.Pow member data.
src/coreclr/vm/floatsingle.cpp Adds ARM/ARM64-guarded y == 1.0f fast path returning x before calling powf.
src/coreclr/vm/floatdouble.cpp Adds ARM/ARM64-guarded y == 1.0 fast path returning x before calling pow.
src/coreclr/jit/valuenum.cpp Ensures ARM/ARM64 JIT constant folding of Pow(const, 1) returns the base constant directly instead of calling CRT pow/powf.

Copilot's findings

  • Files reviewed: 5/5 changed files
  • Comments generated: 0

@EgorBo

EgorBo commented Jul 23, 2026

Copy link
Copy Markdown
Member

I think this is yet another reason to make pow fully managed

@tannergooding
tannergooding enabled auto-merge (squash) July 23, 2026 22:46
@tannergooding
tannergooding merged commit 3a76b78 into dotnet:main Jul 24, 2026
159 of 163 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Jul 24, 2026
@tannergooding
tannergooding deleted the tannergooding-arm-pow-x-1-fix branch July 24, 2026 14:04
hez2010 pushed a commit to hez2010/runtime that referenced this pull request Jul 26, 2026
Fixes dotnet#12139.

ARM/ARM64 return `0` for `Math.Pow(double.Epsilon, 1)` (and
`MathF.Pow(float.Epsilon, 1)`) when they should return the input. The
ARM/ARM64 CRT computes `pow` via `exp(y * log(x))`, which underflows a
subnormal `x` to `0` when `y` is exactly `1`. `pow(x, 1)` is `x` for all
`x` (including NaN, +/-inf, +/-0), so an exact `y == 1` early return is
correct.

- Add an ARM-guarded `y == 1` fast path returning `x` to the
`Math.Pow`/`MathF.Pow` runtime helpers in
`floatdouble.cpp`/`floatsingle.cpp`.

----------

- Mirror the fixup in the JIT value-number constant folding
(`EvalMathFuncBinary`). Folding `Math.Pow(const, 1)` uses the same buggy
host `pow` when the JIT itself runs natively on ARM, which would
otherwise defeat the helper fix and diverge from runtime behavior.

----------

- Add subnormal-`x`, `y == 1` regression cases to the existing
`Math.Pow`/`MathF.Pow` theory data.

The ARM `#if` branches can't be compiled or exercised on the x64 dev
host; the expected test values were verified on x64
(`Math.Pow(double.Epsilon, 1) == double.Epsilon` and the `float`
equivalent both hold), so the added cases pass on x64 and would fail on
unfixed ARM. CI ARM legs will provide the real validation.

> [!NOTE]
> This PR description and the code changes were drafted with GitHub
Copilot.

Co-authored-by: Copilot App <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ARM/ARM64 returns an incorrect result for pow(x, 1)

3 participants