fix(libc): compute j0f with FreeBSD msun's algorithm #16747

Merged
AlexWhitehouse merged 2 commits from upstream-pr/16747 into main 2026-11-11 21:30:21 +00:00
AlexWhitehouse commented 2026-11-07 15:10:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexWhitehouse on 2026-11-07T15:10:18Z.


What

libc j0f (NID RmE3aE8WHuY) returned the host's double j0 rounded to float: static_cast<float>(::j0(x)). The console's libc uses FreeBSD msun's float e_j0f.c, which evaluates its own float rational approximations, so results differ in the last bit even where no trigonometry is involved. For example, on glibc 2.43:

input main msun
0x3f800001 0x3f43e3fe 0x3f43e3fd
0x3f800005 0x3f43e3fa 0x3f43e3f9
0x3f800009 0x3f43e3f7 0x3f43e3f6

This PR ports e_j0f.c at FreeBSD release/10.0.0, meaning __ieee754_j0f with its pzerof and qzerof tables, into a new core/libs/prx/libc/src/J0f.cpp. It keeps the upstream notice byte for byte, the coefficients, thresholds, evaluation order and the volatile huge read.

  • Revision: release/10.0.0 is the revision the repo's double j0, j1, jn, y0, y1 and yn follow (Bessel.cpp, #7998).
  • Why not 11.4: FreeBSD 11.4's e_j0f.c changes the tiny-input cutoffs (2^-13 and 2^-27 become 2^-9 and 2^-12), fixes the unreachable large-input shortcut, moves both table-selection thresholds (0x40f71c58 → 0x409173eb, 0x4036db68 → 0x4036d917) and uses sincosf. Without float measurements from the console, this PR stays with the family's revision, and the debt entry records that choice.
  • Large arguments: for |x| >= 2 it calls this libc's msun sinf, cosf and sqrtf, not the host's.
  • Special values: ±0 → 1, ±∞ → +0, NaN propagates. errno is untouched, as on main.
  • Other functions: j1f, jnf, y0f, y1f and ynf are unchanged; the only line removed from Math.cpp is the old j0f wrapper.

Tested

  • guest_math (core/libs/tests/GuestMath.cpp) gains 116 exact-bit j0f cases, and every existing assertion is kept. They cover:

    • both signs of every branch boundary: the tiny cutoffs, |x| < 2, the four pzerof/qzerof table ranges and the large-argument path;
    • ±0, ±∞ and NaNs;
    • twelve inputs where the host's rounded double differs from msun.

    Expected values come from upstream e_j0f.c compiled locally with -O2 -ffp-contract=off -fno-builtin. 995,999 sampled finite inputs also matched that reference exactly.

  • Linux x86-64 (Ubuntu 26.04, GCC 15, glibc 2.43, Release):

    • on main, guest_math fails with Guest j0f failed for 3f800001: 3f43e3fe expected 3f43e3fd;
    • with this PR (53acf73d72), guest_math, guest_math_elementary, guest_math_exact and guest_bessel pass 4/4.
  • Windows 11 (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release):

    • the same four tests pass at 53acf73d72, with no compiler warnings;
    • with main's Math.cpp, guest_math fails with the same Guest j0f failed for 3f800001: 3f43e3fe expected 3f43e3fd (70 mismatches).
  • Mutation check: changing the R02 coefficient fails the new cases.

  • python3 tools/check_conventions.py --base origin/main: no errors. The one warning is about the double Bessel debt entry, which was already over 1000 characters (1,439 on main); this PR only removes j0f from it (1,434).

  • python3 tools/pr_overlap.py --branch fix/libc-j0f-msun: no open PR changes j0f. 62 PRs were trial-merged, including my #16740 (sincos) and the open msun PRs, and #16740 merges cleanly. The PRs that gain a conflict:

    • #15802, #15803, #15804 and #15807 (lifted-build support) rewrite the hypot3l line directly above the removed wrapper, so they conflict on that adjacent line. The export can be defined in only one place, so removing the wrapper is unavoidable.
    • #11634, #15095 and #14212 edit neighbouring libc.md entries.

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
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5202935427 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16747). Originally posted by [AlexWhitehouse](https://github.com/AlexWhitehouse) on 2026-11-07T15:10:18Z. --- ### What libc `j0f` (NID `RmE3aE8WHuY`) returned the host's double `j0` rounded to float: `static_cast<float>(::j0(x))`. The console's libc uses FreeBSD msun's float `e_j0f.c`, which evaluates its own float rational approximations, so results differ in the last bit even where no trigonometry is involved. For example, on glibc 2.43: | input | `main` | msun | |---|---|---| | `0x3f800001` | `0x3f43e3fe` | `0x3f43e3fd` | | `0x3f800005` | `0x3f43e3fa` | `0x3f43e3f9` | | `0x3f800009` | `0x3f43e3f7` | `0x3f43e3f6` | This PR ports [`e_j0f.c` at FreeBSD release/10.0.0](https://github.com/freebsd/freebsd-src/blob/release/10.0.0/lib/msun/src/e_j0f.c), meaning `__ieee754_j0f` with its `pzerof` and `qzerof` tables, into a new `core/libs/prx/libc/src/J0f.cpp`. It keeps the upstream notice byte for byte, the coefficients, thresholds, evaluation order and the `volatile huge` read. - **Revision:** release/10.0.0 is the revision the repo's double `j0`, `j1`, `jn`, `y0`, `y1` and `yn` follow (`Bessel.cpp`, #7998). - **Why not 11.4:** FreeBSD 11.4's `e_j0f.c` changes the tiny-input cutoffs (2^-13 and 2^-27 become 2^-9 and 2^-12), fixes the unreachable large-input shortcut, moves both table-selection thresholds (`0x40f71c58` → `0x409173eb`, `0x4036db68` → `0x4036d917`) and uses `sincosf`. Without float measurements from the console, this PR stays with the family's revision, and the debt entry records that choice. - **Large arguments:** for `|x| >= 2` it calls this libc's msun `sinf`, `cosf` and `sqrtf`, not the host's. - **Special values:** ±0 → 1, ±∞ → +0, NaN propagates. `errno` is untouched, as on `main`. - **Other functions:** `j1f`, `jnf`, `y0f`, `y1f` and `ynf` are unchanged; the only line removed from `Math.cpp` is the old `j0f` wrapper. ### Tested - **`guest_math`** (`core/libs/tests/GuestMath.cpp`) gains 116 exact-bit `j0f` cases, and every existing assertion is kept. They cover: - both signs of every branch boundary: the tiny cutoffs, `|x| < 2`, the four `pzerof`/`qzerof` table ranges and the large-argument path; - ±0, ±∞ and NaNs; - twelve inputs where the host's rounded double differs from msun. Expected values come from upstream `e_j0f.c` compiled locally with `-O2 -ffp-contract=off -fno-builtin`. 995,999 sampled finite inputs also matched that reference exactly. - **Linux x86-64** (Ubuntu 26.04, GCC 15, glibc 2.43, Release): - on `main`, `guest_math` fails with `Guest j0f failed for 3f800001: 3f43e3fe expected 3f43e3fd`; - with this PR (53acf73d72), `guest_math`, `guest_math_elementary`, `guest_math_exact` and `guest_bessel` pass 4/4. - **Windows 11** (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release): - the same four tests pass at 53acf73d72, with no compiler warnings; - with `main`'s `Math.cpp`, `guest_math` fails with the same `Guest j0f failed for 3f800001: 3f43e3fe expected 3f43e3fd` (70 mismatches). - **Mutation check:** changing the `R02` coefficient fails the new cases. - `python3 tools/check_conventions.py --base origin/main`: no errors. The one warning is about the double Bessel debt entry, which was already over 1000 characters (1,439 on `main`); this PR only removes `j0f` from it (1,434). - **`python3 tools/pr_overlap.py --branch fix/libc-j0f-msun`:** no open PR changes `j0f`. 62 PRs were trial-merged, including my #16740 (`sincos`) and the open msun PRs, and #16740 merges cleanly. The PRs that gain a conflict: - **#15802, #15803, #15804 and #15807** (lifted-build support) rewrite the `hypot3l` line directly above the removed wrapper, so they conflict on that adjacent line. The export can be defined in only one place, so removing the wrapper is unavoidable. - **#11634, #15095 and #14212** edit neighbouring `libc.md` entries. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 - [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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-07 15:46:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T15:46:26Z.

Review: COMMENTED


A question on the source revision. I compared J0f.cpp with FreeBSD's e_j0f.c at release/10.0.0: the 92 table constants, R02–S04, the thresholds and the evaluation order match. That part looks right. My question is whether 10.0.0 is the right revision for the console.

The new debt entry says 10.0.0 matches "the established double Bessel family's source revision". The double entry places the console before 186f620727 (2015-03-10). But FreeBSD's 2656e946c8 (2014-12-05), which is before that commit, changed both files together:

  • in e_j0.c, the tiny-input branch went from one - 0.25*x*x to one - x*x/4;
  • in e_j0f.c, the constants and the tiny-input cutoffs changed. 10.2.0 has ix<0x3b000000 (2^-9) and ix<0x39800000 (2^-12), where 10.0.0 has 2^-13 and 2^-27.

main's Bessel.cpp, which follows the J03093718 disassembly with the console's fused multiply-adds, computes that double branch as Fused(x * x, -0.25, One) (line 188). That is the shape one - x*x/4 compiles to: x*x first, then fused with −0.25. one - 0.25*x*x would give fma(0.25*x, -x, 1) instead. If line 188 mirrors the disassembly, and isn't just written that way, the console's e_j0.c already has 2656e946c8. Its e_j0f.c would then have the newer cutoffs and constants too, which this PR leaves out on purpose.

Disassembling j0f in J03093718, or the |x| < 2^-13 branch of j0, would settle it. Until then, the entry might say the revision is unknown rather than matched.

Read only; not built or run here.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5813361955 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16747#pullrequestreview-5813361955). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T15:46:26Z. **Review:** COMMENTED --- A question on the source revision. I compared `J0f.cpp` with FreeBSD's `e_j0f.c` at release/10.0.0: the 92 table constants, `R02`–`S04`, the thresholds and the evaluation order match. That part looks right. My question is whether 10.0.0 is the right revision for the console. The new debt entry says 10.0.0 matches "the established double Bessel family's source revision". The double entry places the console before `186f620727` (2015-03-10). But FreeBSD's `2656e946c8` (2014-12-05), which is before that commit, changed both files together: - in `e_j0.c`, the tiny-input branch went from `one - 0.25*x*x` to `one - x*x/4`; - in `e_j0f.c`, the constants and the tiny-input cutoffs changed. 10.2.0 has `ix<0x3b000000` (2^-9) and `ix<0x39800000` (2^-12), where 10.0.0 has 2^-13 and 2^-27. `main`'s `Bessel.cpp`, which follows the `J03093718` disassembly with the console's fused multiply-adds, computes that double branch as `Fused(x * x, -0.25, One)` (line 188). That is the shape `one - x*x/4` compiles to: `x*x` first, then fused with −0.25. `one - 0.25*x*x` would give `fma(0.25*x, -x, 1)` instead. If line 188 mirrors the disassembly, and isn't just written that way, the console's `e_j0.c` already has `2656e946c8`. Its `e_j0f.c` would then have the newer cutoffs and constants too, which this PR leaves out on purpose. Disassembling `j0f` in `J03093718`, or the |x| < 2^-13 branch of `j0`, would settle it. Until then, the entry might say the revision is unknown rather than matched. Read only; not built or run here. AI-assisted: yes.
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!16747
No description provided.