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

Merged
JJenkx merged 1 commit from upstream-pr/9777 into main 2026-10-23 18:27:14 +00:00
JJenkx commented 2026-10-22 14:35:24 +00:00 (Migrated from github.com)

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


What

atanf returned the host's atan, so its last bit depended on the host. On Windows (UCRT) it differs from the libc.prx that titles ship on 6,481,820 of the 2^32 floats. Examples, console then main:

  • 0x3f40077e → 0x3f24c148, main gives 0x3f24c149;
  • 0x4080055c → 0x3fa9b5a8, main gives 0x3fa9b5a7.

The titles' atanf (NID weDug8QD-lE) is FreeBSD msun's s_atanf.c. atanf now computes it the same way:

  • the four argument reductions;
  • the atanhi/atanlo tables and the odd/even split of the aT polynomial;
  • atanlo[3] read at run time, as msun's volatile read does.

The constants are written as msun writes them, decimal values converted to float. The build targets baseline x86-64, so nothing is contracted into FMA. Tiny arguments return x without raising FE_INEXACT. None of the copies checked sets errno, and neither does this.

atanf is imported by 21 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA09614. Unlike asinf (#9771) and acosf (#9668), the FMA-contracted copy bundled with PPSA13934 gives the same bits here, so one port matches every copy checked. This is a separate topic from #9771 and does not depend on it; the two branches merge without conflicts.

Tested

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

  • Exhaustive comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. Their atanf and this build's atanf_nid_postfix give the same bits for all 2^32 arguments, NaN payloads included, and neither sets errno.
  • guest_math: the new CheckAtanfConsoleBits has 17 cases with the console's outputs: each of the five argument ranges, both signs, 1, 2^26 and the infinities, a NaN payload, -0, the smallest subnormal and tiny. guest_math and the libc tests (8) pass in three runs out of three. With main's Math.cpp and this test, guest_math aborts in a Require.
  • python tools/check_conventions.py --base origin/main and git diff --check pass. tools/pr_overlap.py --branch finds no conflicting pull request among those it could compare.
  • Independent Linux review: Dom3005's report tested head 5e03230c2 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:4969144688 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9777). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T14:35:24Z. --- ### What `atanf` returned the host's `atan`, so its last bit depended on the host. On Windows (UCRT) it differs from the `libc.prx` that titles ship on 6,481,820 of the 2^32 floats. Examples, console then `main`: - `0x3f40077e` → `0x3f24c148`, `main` gives `0x3f24c149`; - `0x4080055c` → `0x3fa9b5a8`, `main` gives `0x3fa9b5a7`. The titles' `atanf` (NID `weDug8QD-lE`) is FreeBSD msun's [`s_atanf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_atanf.c). `atanf` now computes it the same way: - the four argument reductions; - the `atanhi`/`atanlo` tables and the odd/even split of the `aT` polynomial; - `atanlo[3]` read at run time, as msun's `volatile` read does. The constants are written as msun writes them, decimal values converted to float. The build targets baseline x86-64, so nothing is contracted into FMA. Tiny arguments return `x` without raising `FE_INEXACT`. None of the copies checked sets `errno`, and neither does this. `atanf` is imported by 21 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA09614. Unlike `asinf` (#9771) and `acosf` (#9668), the FMA-contracted copy bundled with PPSA13934 gives the same bits here, so one port matches every copy checked. This is a separate topic from #9771 and does not depend on it; the two branches merge without conflicts. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 8b3e39832a. - **Exhaustive comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. Their `atanf` and this build's `atanf_nid_postfix` give the same bits for all 2^32 arguments, NaN payloads included, and neither sets `errno`. - **`guest_math`:** the new `CheckAtanfConsoleBits` has 17 cases with the console's outputs: each of the five argument ranges, both signs, 1, `2^26` and the infinities, a NaN payload, -0, the smallest subnormal and tiny. `guest_math` and the libc tests (8) pass in three runs out of three. With `main`'s `Math.cpp` and this test, `guest_math` aborts in a `Require`. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. `tools/pr_overlap.py --branch` finds no conflicting pull request among those it could compare. - **Independent Linux review:** [Dom3005's report](https://github.com/boykopovar/AnyPS5/pull/9777#issuecomment-6353486535) tested head `5e03230c2` 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).
harisibrar1 commented 2026-10-22 18:17:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T18:17:13Z.

Review: APPROVED


Checked against main a6646f8464 and FreeBSD 11.4 lib/msun/src/s_atanf.c:

  • This merges cleanly with main a6646f8464, and main still returns the host std::atan.
  • The constants, the seven thresholds, the four argument reductions, NaN x+x, the odd/even aT split and the sign handling match s_atanf.c line by line. The float-rounded tables give exactly the hex values in msun's comments (0x3eed6338 … 0x33a22168).
  • The volatile local for atanlo[3] has the same effect as msun's volatile-pointer read.
  • The only difference I found is the tiny-argument path: msun's huge+x>one raises FE_INEXACT, which this skips, and that is already in the debt entry.
  • There are no FMA or -march flags in main's CMake, so baseline x86-64 holds.
  • I couldn't run a compiled exhaustive comparison (Windows Application Control blocked my test binary). Instead I reproduced the 16 non-NaN CheckAtanfConsoleBits expectations with a float32-rounded emulation of msun's algorithm, and all 16 match.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5618843236 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9777#pullrequestreview-5618843236). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T18:17:13Z. **Review:** APPROVED --- Checked against main a6646f8464 and FreeBSD 11.4 `lib/msun/src/s_atanf.c`: - This merges cleanly with main a6646f8464, and main still returns the host `std::atan`. - The constants, the seven thresholds, the four argument reductions, NaN `x+x`, the odd/even `aT` split and the sign handling match `s_atanf.c` line by line. The float-rounded tables give exactly the hex values in msun's comments (0x3eed6338 … 0x33a22168). - The `volatile` local for `atanlo[3]` has the same effect as msun's volatile-pointer read. - The only difference I found is the tiny-argument path: msun's `huge+x>one` raises FE_INEXACT, which this skips, and that is already in the debt entry. - There are no FMA or `-march` flags in main's CMake, so baseline x86-64 holds. - I couldn't run a compiled exhaustive comparison (Windows Application Control blocked my test binary). Instead I reproduced the 16 non-NaN `CheckAtanfConsoleBits` expectations with a float32-rounded emulation of msun's algorithm, and all 16 match. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9777
No description provided.