fix(libc): compute powf like the libc.prx that titles ship #9850
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9850"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T18:08:47Z.
What
powfreturned the host'spow, apart from returning 1 for base 1 (#3602), so its last bit and itserrnodepended on the host. On Windows (UCRT) the bits differ from thelibc.prxthat titles ship on 3,171,678 of the 100,001,089 argument pairs I compared. Examples, console thenmain:powf(0x3f9e2cf9, 0xbf5a98dc)→0x3f55ab48;powf(0x3fad4946, 0x40c1ad7a)→0x40c82840.The titles'
powf(NID1D0H2KNjshE) is FreeBSD msun'se_powf.cas of release/9.0.0 (identified by @harisibrar1). Its arithmetic is the same as 11.4's, but it differs in two special cases:1**yis 1 even for NaN" shortcut. Sopowf(1, NaN)is that NaN (powf(1, 0x7f800001)=0x7fc00001), not 1.(±1)**±infreturnsy - y, the default NaN0xffc00000, not 1.powf(0x7fc00001, 0xffc00002)=0x7fc00001). 9.0.0 writes this as(x+0.0F)+(y+0.0F). The implementation explicitly selects the first NaN and sets its quiet bit: C++x + ydoes not guarantee which payload survives, and GCC 12 selected the second payload in review. A signaling operand raisesFE_INVALID, preserving the prior arithmetic behavior on the tested Linux hosts; zero exponents still return 1 before this path. Console exception flags remain unverified.With msun 11.4's two special cases, the port differs from the titles on exactly those 8 cases of the special grid, plus 284 NaN-pair payloads. With the older rules it matches every pair. So this reverses #3602's
powf(1, y) == 1for NaNy, which followed the current msun source rather than a title's copy, and updatesCheckPowfSpecialValueto the titles' results.errno: msun sets none, but the titles' copy sets:EDOMin the two(x - x) / (x - x)branches: a negative finite base with a non-integer exponent,-1included;ERANGEonly in the final overflow and underflow returns.The
|y| > 2^27shortcut returns infinity or zero withouterrno, for examplepowf(2, 0x4e000000). So do the zero and infinity special cases. The port setserrnoin exactly those branches.powfis imported by all 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA09614.Tested
Historical Windows validation before the NaN portability correction below (not a Windows rerun of the new head):
Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release,
BUILD_TESTING=ON,maina6646f8464.Sampled comparison: a scratch loader runs the decrypted
libc.prxof PPSA07923, PPSA05916 and PPSA13934 natively and supplies__error. It compared theirpowfwith this build'spowf_nid_postfixon 100,001,089 pairs:2^24/2^27integer limits, the overflow/underflow edges, infinities, quiet and signalling NaNs);[0.5, 2)with larger exponents;PPSA07923 and PPSA05916 give the same bits and
errnofor every pair. PPSA13934 has the sameerrnoeverywhere, but its FMA-contracted build differs by 1 ulp on 26,405 pairs. Pairs outside the sample are unverified.guest_math: The originalCheckPowfSpecialValuehad 33 cases with the console's results anderrno: random pairs where UCRT differs, the±1/NaN/infinity cases above, square roots, a negative base with odd and non-integer exponents, zero bases with negative exponents, overflow and underflow on both sides of the edges, the2^27shortcut, infinite bases and three NaN/NaN pairs.guest_mathand the libc tests (8) pass in three runs out of three. Withmain'sMath.cppand this test,guest_mathaborts in aRequire.python tools/check_conventions.py --base origin/mainandgit diff --checkpass.Overlap: the branch merges without conflicts with my other math PRs (#9771, #9777, #9784, #9839, #9841, #9844).
tools/pr_overlap.pyfinds one adjacency indocs/dev/debt/libc.mdwith #8863 (the neighbouringfegetenventry); keeping both entries resolves it.Independent Linux review: Dom3005's report tested head
65e1d32b7merged cleanly ontomainfcd455870: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release,BUILD_TESTING=ON. The reviewer reports five passingguest_mathruns (--repeat until-fail:5). Keeping this PR's test while restoring main's changed production sources makes the test fail on Linux as well. This supplies a Linux regression comparison in addition to the Windows evidence above; it is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim.NaN portability correction
2ab8da4a4d106aa1610ce67ea8b731dd368e60a0: Linux reproduced MarcZX10's exact reported two-NaN failure with Ubuntu GCC 12.4.0 at-O3:x=7fc00001 y=ffc00002 actual=ffc00002 expected=7fc00001 errno=1234. The corrected implementation and 43 special-value cases pass with GCC 12.4.0, host GCC 16.2.1 and Clang at-O0,-O2, and-O3(nine focused builds/runs). These focused checks compile the exact productionMath.cppand the special-value function extracted fromGuestMath.cpp; they are not full GCC 12 CMake builds. The isolated Release CMake build of this complete PR revision passes the fullguest_mathCTest three times with GCC 16.2.1, without skips. A separate 176-case NaN matrix passes under GCC 16 and Clang, checking result bits, unchangederrno, signaling invalid exceptions, and preservation of an existing divide-by-zero flag. No fresh Windows result is claimed for this correction; rerun requested.Not tested: macOS, a console, console floating-point exception flags, or Windows on the NaN portability correction.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)sinhfentry didAI-assisted (Claude Code).
Current-main integration, 2026-10-23
Refreshed head
a3f26973058083f257d86731d0b01a692e7dd6f4is the same three commits rebased onto main16319642a4c84e861188b75583ce853dfbe0a101; range-diff marks all three unchanged.check_main_sync.pyreports 3 ahead / 0 behind and merge clean. The full Releaseguest_math_testsbuild and three consecutiveguest_mathCTest runs pass with GCC 16.2.1 on Linux, with no skips. Conventions and whitespace checks pass. Earlier compiler-specific special-value results are preserved above; the fresh full integration run is GCC 16. Windows rerun on this refreshed head is pending.Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T18:19:35Z.
Review: COMMENTED
Checked against main
a6646f8464and FreeBSD msun e_powf.c at release/11.4.0, 10.0.0, 9.0.0, 8.0.0 and 7.0.0:|y| > 2^27shortcut, the log2/exp2 cores and the overflow/underflow thresholds match msun line by line, and folding the else-if chain into two ifs is equivalent.cp_h/cp_larithmetic, but no1**yshortcut andy - yfor (±1)**±inf. That matches this body, except that it returns(x+0.0F)+(y+0.0F)for NaN where this PR hasx + y. 10.0.0 already has 11.4's special cases. Could the debt entry (and the body) link 9.0.0 instead of 11.4 "in its older form", as thesinhfandj0entries pin their matching revision?Math.fround) transcription of msun 9.0powfreproduces all 23 non-NaN vectors inCheckPowfSpecialValue, and differs from 11.4 only on (±1, ±inf) and (1, NaN), both on a grid and on 3M random pairs. I couldn't run a native build here (Smart App Control blocked it), and NaN payloads weren't checked.sinhf,atanhf,asinl) and yourexp2fentry in #9844 are under "Functional".x + y(which payload wins) isn't covered. One NaN/NaN case would pin it.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T03:49:21Z.
Review: CHANGES_REQUESTED
I locally built the exact PR
Math.cppandGuestMath.cppand ran the isolatedCheckPowfSpecialValuecases on Linux with GCC 12.2.0, Release (-O3). The check fails for the included two-NaN case:The early NaN path returns
x + y, whose NaN payload selection differs on this host/compiler from the first-NaN payload recorded for the console and asserted by the test. Since the implementation is shared across Linux and Windows, please make the payload behavior deterministic (including signaling-NaN cases as applicable), then rerun the focusedguest_mathtest on Linux and Windows. The local build reused an older configured source snapshot, so this is a focused source/test result rather than a fresh full-tree build; the PR's math and test files are unchanged since its tested base.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T05:24:43Z.
Review: COMMENTED
The new NaN handling resolves the reproducible payload mismatch: the exact head’s 43-case
CheckPowfSpecialValuetable passes in an isolated GCC 12.2-O3 -fno-fast-mathharness, and current-main conventions plus diff whitespace pass. I did not run the full guest_math executable. The remaining review item is current-main integration and a focused guest_math rerun on the resulting head; this is the documented branch-freshness requirement, not a queue-volume concern.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T06:21:03Z.
Review: APPROVED
Reviewed exact current-main head
f20fb10933aa421fcc3c163306c4ff8083eab886. The earlier GCC 12 two-NaN payload mismatch is fixed by explicit deterministic first-NaN selection/quieting; the current source and special-value table are the same three commits as the previously reviewed revision. The author rebased ontomain0db47b263, reports 3 ahead/0 behind with no conflicts, and reports fresh Windows 11 / WinLibs GCC 15.2 UCRTguest_mathRelease build plus three passing runs on this exact head. The identical patch passed Linuxguest_math3/3 on the immediately preceding main revision, with focused Linux GCC/Clang special-value coverage across optimization levels; this Linux result is not a rerun on the exact current-main head. Hosted eligibility, conventions, labels, report, relinker matrix, ASAN, register-context and write-watch checks pass; product Linux/Windows jobs are skipped by policy. I previously reran the 43-case special-value table in an isolated Linux GCC 12.2-O3harness; I did not run a full local build here. Approval is for this exact head and retains the documented limitation on console FP exception flags.