Repository navigation
Fix incorrect result for pow(x, 1) on ARM/ARM64 - #131286
Merged
tannergooding merged 1 commit intoJul 24, 2026
Merged
Conversation
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: 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. |
Contributor
There was a problem hiding this comment.
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 == 1early-return forPowin CoreCLR runtime helpers (floatsingle.cpp,floatdouble.cpp). - Align JIT constant folding (
ValueNumStore::EvalMathFuncBinary) forMath.Pow/powfwith the runtime helper behavior on ARM/ARM64. - Add regression test cases for
xsubnormal +y == 1to existingMath.Pow/MathF.Powtest 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
Member
|
I think this is yet another reason to make pow fully managed |
EgorBo
approved these changes
Jul 23, 2026
tannergooding
enabled auto-merge (squash)
July 23, 2026 22:46
This was referenced Jul 24, 2026
Open
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #12139.
ARM/ARM64 return
0forMath.Pow(double.Epsilon, 1)(andMathF.Pow(float.Epsilon, 1)) when they should return the input. The ARM/ARM64 CRT computespowviaexp(y * log(x)), which underflows a subnormalxto0whenyis exactly1.pow(x, 1)isxfor allx(including NaN, +/-inf, +/-0), so an exacty == 1early return is correct.y == 1fast path returningxto theMath.Pow/MathF.Powruntime helpers infloatdouble.cpp/floatsingle.cpp.EvalMathFuncBinary). FoldingMath.Pow(const, 1)uses the same buggy hostpowwhen the JIT itself runs natively on ARM, which would otherwise defeat the helper fix and diverge from runtime behavior.x,y == 1regression cases to the existingMath.Pow/MathF.Powtheory data.The ARM
#ifbranches 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.Epsilonand thefloatequivalent 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.