fix(libc): compute atan2f with FreeBSD msun's algorithm, as titles' libc.prx does #9784

Merged
JJenkx merged 1 commit from upstream-pr/9784 into main 2026-10-27 08:06:06 +00:00
JJenkx commented 2026-10-22 14:46:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T14:46:50Z.


What

Based directly on main 3a930342d5a2189b78643818f04b8948fc4c3dc9 as e54e3569f38e815640fe0861734a1a1fd399448b, one topic commit. #9777 is already merged: its atanf implementation is inherited unchanged from main, and no local export declaration is added. This PR adds only atan2f’s implementation, bit regressions and debt entry.

atan2f returned the host's atan2, so its last bit depended on the host. On Windows (UCRT) it differs from the libc.prx that titles ship on 7,126,466 of the 200,001,024 argument pairs I compared (3.6%). Examples, console then main:

  • atan2f(0x3d6f53e1, 0x3fb6a73e) → 0x3d279f68;
  • atan2f(0xbe51a574, 0xbdf4773f) → 0xc006505a.

The titles' atan2f (NID EH-x713A99c) is FreeBSD msun's e_atan2f.c, which calls msun's atanf. atan2f now uses that code with the atanf port already merged in main (#9777):

  • the zero, infinity and x == 1 cases;
  • the |y/x| > 2^26 and < 2^-26 shortcuts;
  • the quadrant fix-up with pi_lo;
  • tiny and pi_lo read at run time, as msun's volatile reads do.

msun's NaN case is return x + y. The titles' binaries return y's NaN when both arguments are NaN (atan2f(0x7fc00000, 0xffc00001) is 0x7fc00000). So this returns y + y when y is NaN and x + x otherwise, which does not depend on how the compiler orders the commutative add. None of the copies checked sets errno, and neither does this.

atan2f is imported by 21 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA09614.

Tested

Current Linux refresh (2026-10-25): exact head e54e3569f38e815640fe0861734a1a1fd399448b directly on main 3a930342d5a2189b78643818f04b8948fc4c3dc9, without merge commits. The complete production file equals that main plus the original atan2f port in its original position after atanf; the original regression body is unchanged and called once. The documentation conflict preserves every current-main row and inserts only the atan2f debt entry, retaining newer rename/nan descriptions.

Fresh Linux x86-64 Release libc and all three math targets built. guest_math, guest_math_exact, guest_dinkumware_internals and public_headers each pass three runs (12 passes). Keeping candidate tests and substituting only exact main production Math.cpp makes guest_math abort as expected; the other three groups pass 9/9. Restored candidate gives 12 passes and a byte-identical raw libc.prx. Conventions, whitespace and merge-tree checks pass.

Exact-head logs, main negative control and full-file preservation proof (SHA-256 9561cce663daccfb7abdd7e2c185f5ac2f079c7d0418d3053262d4cfb6f9f590). The earlier module/Windows figures below stay pinned to their original revisions. No new large reference matrix, Windows, macOS, physical-console, exception-matrix, game/GPU or performance result is claimed. Depends on remains none.

Earlier Linux qualification (historical source pins)

Fresh Linux x86-64 Release qualification on e51eb4eeb9482d94a9851983a31d3194fa284345 against main 8b426927d7d91c137e6f4704acc860f060a2798a: rebuilt libc and all three focused math targets. guest_math, guest_math_exact, guest_dinkumware_internals and public_headers each pass three repeats. Retaining final tests and substituting only main’s production Math.cpp makes guest math abort while exact-math and Dinkumware pass. Restored candidate passes all four groups three times and produces byte-identical libc. Conventions and whitespace checks pass.

Source proof confirms the complete production file equals current main plus the original atan2f implementation at its original topic placement; atanf remains main’s already-merged body. The original atan2f bit regression function is unchanged, public libc header includes remain intact, and no local atanf declaration is added. No large reference matrix, Windows, title or performance test was repeated in this refresh.

Fresh regression/negative/restoration logs, source proof, exact pins and patch, SHA-256 d6266456e70e7ace08d07d7ecbe989834e954e70bcbe0b7f1bcc16fd6dc2ca48.

Earlier qualification (historical source pins)

Rebased-head check (2026-10-22): on Windows, the retained build log for bc78074cc859cd4e9a1e0d9d259de4d34e7f044e completes successfully. The retained controller summary reports eight focused tests passing in each of three runs and conventions passing on that head; raw per-run CTest output was not retained in that summary. This is focused regression evidence on base 3bbd2321de, not a new 200,001,024-pair comparison or a claim that the base is the latest upstream revision. See the rebase report.

Original implementation validation: the sampled comparison and negative control below were performed on the original base, not rerun exhaustively on the rebased head.

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, main 8b3e39832a + #9777.

  • Sampled comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. It compared their atan2f with this build's atan2f_nid_postfix on 200,001,024 pairs:

    • every pair of 32 special values (signed zeros, subnormals, ±1 and neighbours, the atanf range edges, 2^26-scale values, infinities, quiet and signalling NaNs);
    • 50M uniformly random bit pairs;
    • 50M pairs with exponents within 30 of each other;
    • 50M with x near ±1;
    • 50M with a zero y a third of the time.

    All give the same bits, and no copy sets errno. Pairs outside the sample are unverified, because 2^64 pairs can't be run exhaustively.

  • guest_math: the new CheckAtan2fConsoleBits has 23 cases with the console's outputs: random pairs in all four quadrants where UCRT differs, the special cases, and both NaN orders. guest_math and the libc tests (8) pass in three runs out of three. With #9777's Math.cpp (ported atanf, host atan2f) and this test, guest_math aborts in a Require.

  • python tools/check_conventions.py and git diff --check pass.

  • Independent Linux review: Dom3005's report tested head bc78074cc merged cleanly onto main fcd455870: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, BUILD_TESTING=ON. The reviewer reports five passing guest_math runs (--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.

  • Not tested: macOS, a console.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source: none; the msun function is rewritten, as #9668, #5036 and the sinhf entry did
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4969299707 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9784). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T14:46:50Z. --- ### What Based directly on main `3a930342d5a2189b78643818f04b8948fc4c3dc9` as `e54e3569f38e815640fe0861734a1a1fd399448b`, one topic commit. #9777 is already merged: its atanf implementation is inherited unchanged from main, and no local export declaration is added. This PR adds only atan2f’s implementation, bit regressions and debt entry. `atan2f` returned the host's `atan2`, so its last bit depended on the host. On Windows (UCRT) it differs from the `libc.prx` that titles ship on 7,126,466 of the 200,001,024 argument pairs I compared (3.6%). Examples, console then `main`: - `atan2f(0x3d6f53e1, 0x3fb6a73e)` → `0x3d279f68`; - `atan2f(0xbe51a574, 0xbdf4773f)` → `0xc006505a`. The titles' `atan2f` (NID `EH-x713A99c`) is FreeBSD msun's [`e_atan2f.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_atan2f.c), which calls msun's `atanf`. `atan2f` now uses that code with the `atanf` port already merged in main (#9777): - the zero, infinity and `x == 1` cases; - the `|y/x| > 2^26` and `< 2^-26` shortcuts; - the quadrant fix-up with `pi_lo`; - `tiny` and `pi_lo` read at run time, as msun's `volatile` reads do. msun's NaN case is `return x + y`. The titles' binaries return `y`'s NaN when both arguments are NaN (`atan2f(0x7fc00000, 0xffc00001)` is `0x7fc00000`). So this returns `y + y` when `y` is NaN and `x + x` otherwise, which does not depend on how the compiler orders the commutative add. None of the copies checked sets `errno`, and neither does this. `atan2f` is imported by 21 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA09614. ### Tested **Current Linux refresh (2026-10-25):** exact head `e54e3569f38e815640fe0861734a1a1fd399448b` directly on main `3a930342d5a2189b78643818f04b8948fc4c3dc9`, without merge commits. The complete production file equals that main plus the original atan2f port in its original position after atanf; the original regression body is unchanged and called once. The documentation conflict preserves every current-main row and inserts only the atan2f debt entry, retaining newer rename/nan descriptions. Fresh Linux x86-64 Release libc and all three math targets built. `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` and `public_headers` each pass three runs (12 passes). Keeping candidate tests and substituting only exact main production `Math.cpp` makes `guest_math` abort as expected; the other three groups pass 9/9. Restored candidate gives 12 passes and a byte-identical raw `libc.prx`. Conventions, whitespace and merge-tree checks pass. [Exact-head logs, main negative control and full-file preservation proof](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr9784-heade54e3569-main3a93-linux-refresh.tar.gz) (SHA-256 `9561cce663daccfb7abdd7e2c185f5ac2f079c7d0418d3053262d4cfb6f9f590`). The earlier module/Windows figures below stay pinned to their original revisions. No new large reference matrix, Windows, macOS, physical-console, exception-matrix, game/GPU or performance result is claimed. Depends on remains none. #### Earlier Linux qualification (historical source pins) Fresh Linux x86-64 Release qualification on `e51eb4eeb9482d94a9851983a31d3194fa284345` against main `8b426927d7d91c137e6f4704acc860f060a2798a`: rebuilt libc and all three focused math targets. `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` and `public_headers` each pass three repeats. Retaining final tests and substituting only main’s production Math.cpp makes guest math abort while exact-math and Dinkumware pass. Restored candidate passes all four groups three times and produces byte-identical libc. Conventions and whitespace checks pass. Source proof confirms the complete production file equals current main plus the original atan2f implementation at its original topic placement; atanf remains main’s already-merged body. The original atan2f bit regression function is unchanged, public libc header includes remain intact, and no local atanf declaration is added. No large reference matrix, Windows, title or performance test was repeated in this refresh. [Fresh regression/negative/restoration logs, source proof, exact pins and patch](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr9784-heade51eb4ee-main8b42-linux-validation.tar.gz), SHA-256 `d6266456e70e7ace08d07d7ecbe989834e954e70bcbe0b7f1bcc16fd6dc2ca48`. #### Earlier qualification (historical source pins) **Rebased-head check (2026-10-22):** on Windows, the retained build log for `bc78074cc859cd4e9a1e0d9d259de4d34e7f044e` completes successfully. The retained controller summary reports eight focused tests passing in each of three runs and conventions passing on that head; raw per-run CTest output was not retained in that summary. This is focused regression evidence on base `3bbd2321de`, not a new 200,001,024-pair comparison or a claim that the base is the latest upstream revision. See the [rebase report](https://github.com/boykopovar/AnyPS5/pull/9784#issuecomment-6350460480). **Original implementation validation:** the sampled comparison and negative control below were performed on the original base, not rerun exhaustively on the rebased head. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 8b3e39832a + #9777. - **Sampled comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. It compared their `atan2f` with this build's `atan2f_nid_postfix` on 200,001,024 pairs: - every pair of 32 special values (signed zeros, subnormals, ±1 and neighbours, the `atanf` range edges, `2^26`-scale values, infinities, quiet and signalling NaNs); - 50M uniformly random bit pairs; - 50M pairs with exponents within 30 of each other; - 50M with `x` near ±1; - 50M with a zero `y` a third of the time. All give the same bits, and no copy sets `errno`. Pairs outside the sample are unverified, because 2^64 pairs can't be run exhaustively. - **`guest_math`:** the new `CheckAtan2fConsoleBits` has 23 cases with the console's outputs: random pairs in all four quadrants where UCRT differs, the special cases, and both NaN orders. `guest_math` and the libc tests (8) pass in three runs out of three. With #9777's `Math.cpp` (ported `atanf`, host `atan2f`) and this test, `guest_math` aborts in a `Require`. - `python tools/check_conventions.py` and `git diff --check` pass. - **Independent Linux review:** [Dom3005's report](https://github.com/boykopovar/AnyPS5/pull/9784#issuecomment-6353486246) tested head `bc78074cc` merged cleanly onto `main` `fcd455870`: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, `BUILD_TESTING=ON`. The reviewer reports five passing `guest_math` runs (`--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. - Not tested: macOS, a console. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source: none; the msun function is rewritten, as #9668, #5036 and the `sinhf` entry did - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-22 16:31:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T16:31:59Z.

Review: DISMISSED


I reviewed the incremental second commit (cc3e30485f) separately from its prerequisite #9777. The implementation follows the cited FreeBSD msun quadrant/special-case algorithm, and the added guest_math vectors cover the reported UCRT mismatches, signed zeros, infinities and NaN ordering. Current-main check_conventions.py and git diff --check pass. I did not run guest_math locally. The PR reports three Windows runs and a 200,001,024-pair comparison against three title libc.prx copies; the upstream PR’s automated relinker/register-context checks pass, while full Linux and Windows product jobs are skipped.

This stack is 19 commits behind live main (8499ea52e571b0edfc1feecfd3f6b9944012b359). CONTRIBUTING.md requires branches to be based on and kept up to date with main. Please refresh the #9777/#9784 stack against current main and rerun guest_math on the final head before merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5617581937 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9784#pullrequestreview-5617581937). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T16:31:59Z. **Review:** DISMISSED --- I reviewed the incremental second commit (`cc3e30485f`) separately from its prerequisite #9777. The implementation follows the cited FreeBSD msun quadrant/special-case algorithm, and the added `guest_math` vectors cover the reported UCRT mismatches, signed zeros, infinities and NaN ordering. Current-main `check_conventions.py` and `git diff --check` pass. I did not run `guest_math` locally. The PR reports three Windows runs and a 200,001,024-pair comparison against three title libc.prx copies; the upstream PR’s automated relinker/register-context checks pass, while full Linux and Windows product jobs are skipped. This stack is 19 commits behind live `main` (`8499ea52e571b0edfc1feecfd3f6b9944012b359`). CONTRIBUTING.md requires branches to be based on and kept up to date with `main`. Please refresh the #9777/#9784 stack against current main and rerun `guest_math` on the final head before merge.
MarcZX10 commented 2026-10-22 16:56:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T16:56:31Z.

Review: COMMENTED


I reviewed the incremental #9784 change separately from prerequisite #9777. The updated stack #9777 → #9784 has reported three repeated Windows guest_math/libc suite runs and the 200,001,024-pair comparison against title libc.prx implementations. Upstream relinker/register-context checks pass; full product jobs are skipped. The current #9784 head merges cleanly and git diff --check passes, but the stack base is still 499b1cf9fc, eight commits behind current main (11f976b27c16ed91633f00039dba5dfd97dd7cc1). Please refresh both commits and rerun guest_math on the final stack head before merge. I did not run the math tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5617855675 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9784#pullrequestreview-5617855675). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T16:56:31Z. **Review:** COMMENTED --- I reviewed the incremental #9784 change separately from prerequisite #9777. The updated stack `#9777` → `#9784` has reported three repeated Windows `guest_math`/libc suite runs and the 200,001,024-pair comparison against title libc.prx implementations. Upstream relinker/register-context checks pass; full product jobs are skipped. The current #9784 head merges cleanly and `git diff --check` passes, but the stack base is still `499b1cf9fc`, eight commits behind current `main` (`11f976b27c16ed91633f00039dba5dfd97dd7cc1`). Please refresh both commits and rerun `guest_math` on the final stack head before merge. I did not run the math tests locally.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!9784
No description provided.