fix(libc): compute hypotl with FreeBSD msun's ld80 code as the bundled libc.prx does #11634
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11634"
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 AlexFigas on 2026-10-27T00:21:32Z.
What
hypotlreturned the host'shypotlafter an infinity check. On 20,000,000 random pairs with nearby exponents, glibc's result differs from the console's on 539,841 of them, and its NaN bits on 729,412. For example,hypotl(0x8e0412b10b328599:4021, 0xa5e56711d52bca5e:c00f)is0x8e0412b11140db2f:4021on the console and…2eon the host.The evidence is the
libc.prxbundled with PPSA21564, buildJ03093718, the library's own code. Itshypotl(0x48b90, no calls) is FreeBSD msun'se_hypotl.cfor ld80. This PR ports it operation for operation:ahas the larger exponent field, then take|a|and|b|.a + bwhen the exponent fields differ by more than 71.w = |x + 0| - |y + 0|; this raisesFE_INVALIDfor ∞ - ∞, as the binary does. Returnaorbif either is an infinity, otherwisew.0x5ff3, rebias both by −0x2058.0x200bwith a normal exponent, rebias both by +0x2058.breturnsa.bwith a zero exponent field gives the default NaN withFE_INVALID, as below.a(or2a) andbby clearing the low 32 bits of the mantissa (ClearLowMantissaWord), then takefsqrtof the same exact-product expression.2^k, built from its bits.The subnormal case follows the binary. When the argument with the smaller exponent field (
yon a tie) is nonzero with a zero exponent field and the other is below 2^-16311, the binary returns the default NaN0xc000000000000000:ffffand raisesFE_INVALID, and so does this port.0x7fff:4000000000000000(at 0x159dc0), a pseudo-NaN.a,bandw: after checkingb's mantissa it never reads the arguments again and has now > btest. It runs thew <= barithmetic on the pseudo-NaN, and each operation is invalid or propagates the default NaN.t1 + t1, the first of those operations (fadd st(0), st(0)at 0x48d99). Multiplying the arguments by it instead would leavew * (-w)on two default NaNs of opposite sign, for which the x87 returns the positive one whatever the operand order, so the result would be0xc000000000000000:7fff.hypotl(3 * 2^-16445, 4 * 2^-16445)is that NaN here too, at every precision, andhypotl(3 * 2^-16445, -0)is still3 * 2^-16445.The binary has no control-word instruction (msun's
ENTERIchanges the precision only on i386), so it runs at the caller's x87 precision, and so does this port, ascbrtldoes in #11632. The precision a guest thread starts with is #11849.The debt fragment
docs/dev/debt/changes/fix-libc-hypotl-msun-ld80.mdadds an entry forhypotlthat records that other SDK builds and the systemlibSceLibcInternalare not compared, and describes the subnormal case. The host-computed list dropshypotl. Its C11 F.10.4.3 note (∞ wins over NaN) moves to that entry; the port keeps that behaviour.Licence.
e_hypotl.c's notice is copied at the top ofMath.cpp, after #11632's, behind aDerived from FreeBSD lib/msun/src/e_hypotl.c (release/11.4.0)line, the form #12541 uses. The source has a trailing space after "this notice"; it is dropped here, as asked on #12541, sogit diff --checkpasses.Tested
Head
6e37e8fcd4is two commits ona4630de6b0(oneandonlydean's merge ofmain023c6eaf23).a4630de6b0had left two copies of the host-computed list line indocs/dev/debt/libc.md, and the debt files are no longer edited directly (Fragments).0fc7b591c1mergesmaindcf3d15978without a conflict and setslibc.mdtomain's;6e37e8fcd4addsdocs/dev/debt/changes/fix-libc-hypotl-msun-ld80.md, which changes that line and adds thehypotlentry. With the fragment applied,tools/debt_fold.py --show libcgivesmain's line withouthypotland its C11 F.10.4.3 note, and thehypotlentry under Functional. (debt_fold.py --checkstops ondocs-nkwebkit-drop-readded-count-lines.md, which fails onmainitself, so I ran it on a copy without that file.) The PR's diff ofMath.cppandGuestMath.cppagainstmainis line for line the same as at360e13ebfa.BUILD_TESTING=ON, Ninja:guest_math,guest_math_elementaryandguest_math_exactpass 3 runs each.Math.cppreverted tomain's and the tests kept,guest_mathaborts in 3 of 3 runs atGuest hypotl failed for 8000000000000000:0001, 8000000000000000:0001 with control word 037f; restored, it passes 3 of 3 again.tools/select_tests.pyselects 5828 of 5967 tests for the three changed files, on this head and onmaindcf3d15978. The 129 tests that fail, are skipped or do not run on this head do the same onmain;mainalso failsagc_driver_cached_image_multisample_guard. Not covered: Windows Defender quarantined 34 freshly linked AGC executables on both sides (33 tests andagc_shader_replay.exe, flagged asTrojan:Win64/ShellCode.MKA!MTB), so their tests did not run or failed, andagc_driver_upload_cache_read_only_flush_testsdoes not build on Windows (setenv, #19899).The runs below are on Ubuntu 24.04 under WSL2 (GCC 13.3, Release). Head
f51e83dd53mergesmain813f46cd68(which has #11632) into the previous headc9e16f16bc, without a conflict.c9e16f16bcmerged #11632's headb2a99b137f, which brings inmaina4eca2a056, into307aaca60f, with one conflict indocs/dev/debt/libc.md:mainhas #11849's shorterasinlentry directly below thehypotlentry, so the resolution keptmain'sasinland this PR'shypotl.307aaca60fmerged #11632's head5ab9b2926a, which brings inmaine0caf13600, intocfee0b7fe5(71ce66e0b1and68b7307f0abefore it). Every run below is onf51e83dd53except the negative control, made on the earlier head f6e7494db8, and the hosted run.Native comparison. I used the harness from #11260, with that
libc.prx's segments mapped into a Linux process, and ran the binary'shypotlnext to this head's at 64, 53 and 24 bits under each rounding mode: 10,000,000 random pairs over every exponent (subnormals, ±∞, NaNs and non-canonical encodings included) and 10,000,000 pairs with exponent fields at most 80 apart, 240,000,000 calls in all. The results, NaN bits included,errnoand the exception flags match on every pair, and the control word is unchanged after every call.Negative control. With msun's 2^16382 factor put back,
guest_mathfails onhypotl(3 * 2^-16445, 4 * 2^-16445)and the same sweep reports result and flag differences on the subnormal path, for exampleFE_INVALIDon the console andFE_INEXACT | FE_UNDERFLOWthere.guest_mathandguest_math_exactpass 3 runs each.guest_mathgainsCheckHypotlConsoleBits, 24 known-answer pairs from that binary, each run at control word0x037fand at0x027f(53 bits) with the binary's result for that precision:hypotl(max, max)overflowing to ∞;0x8e0412b11140e000:4021at 53 bits;FE_INVALIDfor two subnormals, two larger subnormals of opposite sign, an unnormal with a subnormal, +0 with a subnormal, and a gap of 71 in the exponent field; the subnormal itself for a subnormal with −0.Each check requires the binary's result and x87 exception flags, no SSE flag, the same control word afterwards, and
errnounchanged. The existinghypotl(3, -4)andhypotl(NaN, -∞)checks still pass.Hosted run. On GitHub's
ubuntu-latestrunner (x86_64, GCC 13.3.0, Release), the earlier head7e9165cb8bplus only a fork-only workflow commit (04a0367917) passesguest_math3 runs (run).Whole suite. Not rerun on this head, which changes only the subnormal branch and its checks; all 2536 tests passed on the earlier head 2850689af7.
Conventions.
python3 tools/check_conventions.py --base upstream/mainpasses with 0 errors on6e37e8fcd4, with this title and description. Its one warning is the length of the host-computed list entry the fragment changes, 1419 characters.Overlap. On
6e37e8fcd4,python3 tools/pr_overlap.py --branch fix/libc-hypotl-msun-ld80reports no pull request that shares lines or exports. It could not compare #6295, #7544 and #17026, which conflict withmainand shareMath.cppandGuestMath.cpp; none of their diffs mentionshypotl.cbrtlentry,JoinExtendedand theasinlentry aremain's.log1pl,acoshl,asinhl,atanhlandjnf,ynf.debt_fold.pyrefuses two fragments that change one entry, so whichever lands second has to rework its fragment against the line the first one leaves. This PR was opened and approved first, so I have not added a dependency.Stacking. #11632 has merged, so this PR is now only the
hypotlcommit; it addedJoinExtendedand the test helperCallAtControlWord, which are onmain.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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T00:22:47Z.
Review: CHANGES_REQUESTED
This PR depends on #11632, but the new
hypotlimplementation has the same unresolved x87 precision-control requirement ascbrtl: the debt entry says it needs 64-bit precision, while Windows guest threads started through_beginthreadexuse 53-bit precision. The implementation neither sets nor restores the x87 control word, and the reported 100-million-pair comparison was run under WSL2, so it does not establish correctness in the guest thread's Windows floating-point environment. A known-answer test under the host's default environment also does not cover this.Please coordinate this with #11632 and make the result match the target independently of the caller's precision mode (preserving the caller's floating-point environment), or provide evidence that guest calls execute with 64-bit precision. Add tests that run the relevant cases at both 53-bit and 64-bit precision, including the reported one-bit-difference input. I cannot approve this stacked change while its documented runtime precision requirement is unmet.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T11:01:23Z.
Review: COMMENTED
I reviewed the exact head 2850689af7c63b38bd18adb89153885a2eb4a5e8. The implementation handles the exponent-difference shortcut, infinities/NaNs, large values, subnormals, and compensated square accumulation in the same order described from the bundled module. Its tests cover the stacked cbrtl behavior as well as hypotl edge cases. I found no specific arithmetic defect in this pass.
Independent local check: I built and ran the exact
guest_math_teststarget natively on Linux; it exited 0. The hosted Windows check and Ubuntu/Windows relinker plus ASan checks pass. The two macOS relinker builds, macOS register-context jobs, and Intel write-watch job remain queued; the full Linux product job is skipped by path policy. Since this PR depends on #11632, I am leaving a comment review until the remaining platform checks finish and the dependency is integrated.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T15:13:52Z.
Review: COMMENTED
I reviewed the current head bf6406e74d0b1f868e9830a8ef50161a89b63fe5. The
hypotlpath covers the exponent-gap shortcut, special values, large and tiny scaling, and compensated square accumulation. I did not find a new arithmetic defect in this pass. The documented bundled-binary subnormal discrepancy is explicit and has a focused test for the chosen msun behavior.Local check: I applied the
Math.cpp,GuestMath.cpp, and libc debt changes from #11632 and #11634 to a clean #11213-based checkout, builtguest_math_testsin Release, and ranguest_maththree times; all three passed. The combined tree includes #11213's unrelated exception changes, so this verifies the focused math sources and tests, not a literal build of this exact commit.The hosted Windows, Ubuntu/macOS relinker, Ubuntu register-context, Windows, and ASan checks pass. The macOS x86_64 relinker, Intel Debug register-context, and Intel write-watch checks are still queued, and the Linux product job is skipped by path policy. This PR declares its dependency on #11632. I am leaving a comment review until that dependency is resolved and the queued platform checks finish.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T15:26:52Z.
Review: CHANGES_REQUESTED
I reviewed the current head bf6406e74d0b1f868e9830a8ef50161a89b63fe5. The ld80 hypotl path and the noncanonical/precision tests are otherwise well supported. The focused local guest_math run passed three times, and the hosted checks are now green.
I found one compatibility blocker in the explicitly documented subnormal case. For hypotl(3 * 2^-16445, 4 * 2^-16445), the bundled PPSA21564 libc.prx returns the default NaN and raises FE_INVALID, while this implementation returns msun's finite 5 * 2^-16445; the new test asserts the latter. The PR attributes the binary result to a probable compiler miscompile, but the other SDK builds and system libSceLibcInternal have not been compared. Since this change is presented as reproducing the console libc behavior, the single observed target binary is currently the only direct runtime evidence for this case.
Please either reproduce that measured behavior for the supported target, or provide evidence from the system library/another supported SDK showing that the msun result is the behavior AnyPS5 should emulate, and update the claim and regression coverage accordingly. I cannot approve a known behavior difference based only on an unverified miscompile explanation. The #11632 dependency and its separate debt-file ordering issue also remain unresolved.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T18:15:55Z.
Review: COMMENTED
I rechecked the updated head
f6e7494db8ebb9b7e1d71f8d03aedc8c578b1328and its delta from the previously reviewed head. The tiny-subnormal case now follows the observed PPSA21564libc.prxresult (0xc000000000000000:ffff,FE_INVALID), with regression vectors for the measured subnormal paths; the prior known-result discrepancy is addressed in code and debt text.git diff --checkpasses and the PR merges cleanly into the currentmainpreview. The PR reports extensive current-head binary comparison andguest_mathevidence, which I have not independently rerun on this updated head. Six macOS checks remain queued, and the declared dependency #11632 is still open, so I am leaving this as a comment review rather than approval.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T19:33:01Z.
Review: APPROVED
I re-reviewed exact head f6e7494db8ebb9b7e1d71f8d03aedc8c578b1328 after the remaining validation completed. The author's fork-only Ubuntu run of guest_math on this head passes three runs; the previously queued upstream macOS checks have now all passed, along with Ubuntu, Windows, ASan and register-context checks. The upstream Linux product job is skipped for this PR's path filters.
My earlier focused local guest_math build/test also passed, and the arithmetic review found no defect in the tiny-subnormal handling or control-word preservation. The landing order is explicit: #11632 precedes this PR, which remains a declared prerequisite of #11849. No PS5 title run was performed. I approve the reviewed change and its available test evidence.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T16:51:49Z.
Review: APPROVED
Re-reviewed exact current head
679a6c6eee95abae5e00037fd5cb8a3a366578c8. The post-approval changes add the requireds_cbrtl.cande_hypotl.ccopyright/license notices directly above the corresponding ports; the notices match FreeBSD release/11.4.0. The previously reviewed tiny-subnormal behavior, tests and dependency on #11632 remain in place. Hosted Ubuntu, Windows, macOS, ASan, register-context and write-watch checks pass; Linux product is skipped by workflow filters. The author reportsguest_mathand binary comparisons on the new heads. No PS5 title run is claimed.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T11:07:55Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
The notice is now positioned after #11632's as requested, and the update reports three
guest_mathruns on the PR and Ubuntu plus the bundled-libchypotlsweeps. The branch is based on1b580f13cf; currentmainisba5b5330b3and has changedMath.cpp,GuestMath.cppand the libc debt file touched by this stack. Please integrate current main and rerun the focused math tests andhypotlcomparison on the resulting exact head. I am not treating the previous-head approval as validation of the new integration.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T10:20:37Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
Re-reviewed exact head
72ec427b783bf7f1638bb9f0ee668b54e3aa1b65; the remaining blocker is the maintainer-directed dependency on #12541, which this PR's checklist does not declare.What I ran
The PR base is
98ddd9c1792bbab016ceed5db267a93d1d61f00a; currentmainis07253ec3794121657a8d9a2229a6224651b0c8f5. A merge preview onto currentmainis clean. Among the PR's changed paths, currentmainhas changed onlydocs/dev/debt/libc.md;Math.cppandGuestMath.cppare unchanged since the PR base. Local no-build checks ran on Linux (Debian, Python 3.11.2, GCC 12.2.0); I did not configure or build.Results
887ca6389307599c8acdc026098eb49c8904fd65.guest_mathand bundled-libc comparisonhypotlcomparisons across precisions/rounding modes; I did not independently run them.Selected unedited output
Findings
Blocking
The current checklist says
Depends on: #11632, but maintainer direction on #12541 is to land #12541 first and declareDepends on: #12541on both this PR and #11632. Both add license notices at the top ofMath.cpp, so that insertion conflict needs an agreed and recorded order before approval. Please update this checklist toDepends on: #11632, #12541. After #12541 merges, rebase #11632 and then this PR onto the updated main in dependency order, keep both notice blocks, and obtain fresh CI.Non-blocking
The conventions check warns that the modified libc debt entry is 1,324 characters, above its 1,000-character guidance. It is not a conventions error and does not block this review request.
Sources
Not verified
I did not build or run
guest_mathor the bundled-library comparison locally. The author-reported math results are on the exact PR head; the physical console was not tested. CI statuses above are from the exact-head snapshot and may change after this review.To get this to a good state
Update the checklist to
Depends on: #11632, #12541. After #12541 merges, rebase in dependency order and rerun fresh CI and focused math validation. I will rerun the same available no-build checks and inspect the resulting exact head before reviewing again.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T15:00:39Z.
Review: CHANGES_REQUESTED
Re-checked — new head, changes requested
The earlier #12541 dependency-order finding is resolved, and this head includes #11632’s current reviewed head; the remaining blocker is that neither branch includes current
main, as the repository rules require.What I ran
Reviewed head
68b7307f0ac73a3e80660eed416aee3174575972against PR baseda026c70655a132031d45e2008c24f993870d801on Debian 12 x86_64, Python 3.11.2, GCC 12.2.0. Currentmainised1e0ed08fc2452192df91793a33dd0cc7ad5212; the merge preview is clean.Results
main; preview tree7977071409095684a366f0485e288bcf3f78f0d5.9d91493c28768bfaa7e63d5eb6c92263835139f7; checklist records #11632 and #12541 as already merged/included in the required order.guest_mathruns and 24 ten-million-argumenthypotlsweeps on this head. The 2,536-test full suite was reported on an earlier head; I did not run tests locally.Selected unedited output
Negative control
Not independently exercised. The reported comparison and focused-test runs are author-provided; I did not build an exact-head binary.
Findings
Blocking
This head contains
mainonly throughda026c70655a132031d45e2008c24f993870d801, while currentmainised1e0ed08fc2452192df91793a33dd0cc7ad5212.CONTRIBUTING.mdrequires keeping branches up to date. The merge preview is clean and the latermaincommits do not change this PR's source/test/debt paths, but the branch remains behind. Please update #11632 with currentmainfirst, then refresh #11634 on that updated dependency head, preserving the #11632 → #11634 → #11849 order.Non-blocking
MacOS CI remains queued, the full suite was reported on an earlier head, and the precheck's stack-cycle result is inconsistent with its own clean dependency preview.
Sources
Not verified
No local build or CTest run; the reports of focused tests, comparison sweeps and the earlier full-suite run were not independently verified. No PS5-console run. The hosted macOS jobs are pending.
To get this to a good state
Update #11632 with current
main, then refresh #11634 from that head and rerun the focused math tests on the resulting exact head. I will repeat the no-build checks and inspect that refreshed stack before reviewing again.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T01:11:23Z.
Review: APPROVED
Re-checked — new head, looks good
I found no new blocking issue at head
cfee0b7fe5bc06e84423bfb0047a4a2fe5b82d28; it includes #11632's current head, merges cleanly onto currentmain, and Windows CI passed.What I ran
Reviewed head
cfee0b7fe5bc06e84423bfb0047a4a2fe5b82d28against base8a4fc62318a4b73b6895bf20a024c625efeb7c3bon Debian 12 x86_64, Python 3.11.2 and GCC 12.2.0. The preview onto currentmain(7c4dcf0ad84de7a473ce97bb21644c614f1c3bd3) merged cleanly, and the head contains #11632 at02c58684e5413be99fbbd9d635c86927db187b9c.Results
29249e9d9375ff97fac8956ac73e8fae8e0eccdb. No newermaincommits touch the changed math, test, or debt paths.write-watchjobs failed during CMake configuration because that test project registers duplicate names; the failure is outside this PR's changed paths.guest_mathandguest_math_exactpassing three runs each, plus 24 console-differentialhypotlsweeps of 10 million pairs per sweep across x87 precisions and rounding modes. I did not run these locally.Selected unedited output
Sources
s_cbrtl.ce_hypotl.cNot verified
No local C++ build or CTest run, no Linux product test, and no physical-console run. The extensive console-differential results above are author-reported, not independently reproduced.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T07:07:55Z.
Review: APPROVED
Reviewed — looks good
The
hypotlport, regression cases, and technical debt entry were reviewed at the pinned head; no blocking defect was identified in the inspected changes.What I ran
No commands or tests were run. The author reports Windows
guest_math,guest_math_elementary, andguest_math_exactruns on this head; these were not independently run here.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The full test suite and all author-reported runs were not independently verified. The changed files listed as reviewed in the manifest were all read:
core/libs/prx/libc/src/Math.cpp,core/libs/tests/GuestMath.cpp, anddocs/dev/debt/libc.md. No files were listed as excluded in the supplied manifest;files.jsonwas unavailable through the reader, so exclusions beyond that manifest are not verified.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T10:38:39Z.
Review: COMMENTED
Reviewed — no blocking findings
The inspected implementation follows the described
hypotlpaths, but I did not run checks on this exact head, so a formal approval is not justified.What I ran
Head:
6e37e8fcd42dd4ca5812439639da6ac117a84249. I usedrepo_read.pyto inspect the prepared description, commits, manifest, diff, and targeted source ranges. No build or tests were run by me.Results
Negative control
Not run by me. The description reports a negative control on an earlier head; I did not verify it.
Findings
Blocking
None identified in the inspected changes.
Non-blocking
No code change requested. The implementation and focused tests appear consistent with the stated goal in the ranges reviewed.
Sources
Math.cppat reviewed headGuestMath.cppat reviewed headfix-libc-hypotl-msun-ld80.mdat reviewed headNot verified
I did not run the build, test suite, or negative control. The reported test results are author-provided; most are for earlier heads, and the description does not establish that they ran on this exact head. I reviewed the changed implementation and helper ranges, the new test function and call site, and the debt entry; I did not read every line of the two large C++ files. No changed files were excluded from the prepared diff.
To get this to a good state
Nothing is required. Optional: run the focused tests on this exact head before merging.