fix(libc): compute jnf and ynf with FreeBSD msun's algorithm #18792

Merged
AlexWhitehouse merged 1 commit from upstream-pr/18792 into main 2026-11-17 22:27:13 +00:00
AlexWhitehouse commented 2026-11-12 09:47:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexWhitehouse on 2026-11-12T09:47:54Z.


What

libc jnf (NID M5KJmq-gKM8) and ynf (NID wdPaII721tY) returned the host's double jn/yn rounded to float. The console's libc uses FreeBSD msun's float e_jnf.c, which runs its own float recurrences on top of j0f, j1f, y0f and y1f, so results differ from the host's in the last bits. For example, on glibc 2.43, ynf(0, 0x3f000000) (0.5) is 0xbee397f6 on main and 0xbee397f5 in msun.

This PR ports e_jnf.c at FreeBSD release/10.0.0 (__ieee754_jnf and __ieee754_ynf) into a new core/libs/prx/libc/src/Jnf.cpp. It keeps the upstream notice byte for byte, along with the thresholds, the forward and backward recurrences, the continued-fraction start and the logf rescaling branch.

  • Revision: release/10.0.0, the revision of the repo's double Bessel family (Bessel.cpp, #7998) and of my j0f, j1f/y1f and y0f ports (#16747, #16847, #16866). #21013 has since moved j0f, j1f, y0f and y1f to a737ef56ab; e_jnf.c did not change between release/10.0.0 and that commit.
  • Building blocks: the recurrences call this libc's msun j0f, j1f, y0f, y1f and logf, not the host's.
  • Order arithmetic: upstream's -n, n+n and i+i overflow at INT_MIN. Here they wrap explicitly at 32 bits, which gives the same result as upstream's two's-complement build: for INT_MIN, the recurrence is skipped, so the result is j1f(|x|) or y1f(x) for finite nonzero x.
  • Special values:
    • NaNs propagate first, as upstream does.
    • ynf(±0) keeps the measured −∞ (#7906, #8864), with errno and exception flags unchanged, as the existing zero checks in GuestBessel require.
    • Finite negative arguments to ynf return a positive quiet NaN without setting errno or raising invalid, as y0f and y1f do here; ynf(-inf) gives an invalid NaN.
  • Debt: jnf and ynf move out of the shared host-results entry into their own entry, which records that console provenance, FMA contraction and nonzero exception flags are unverified, and that huge orders can take billions of steps, as upstream.

Tested

  • guest_bessel (core/libs/tests/GuestBessel.cpp) gains exact-bit jnf and ynf cases for 55 order/argument pairs, and every existing assertion is kept. They cover:

    • orders 0–4, 6–8, 33, 34, 40, 100, 300, negative orders, INT_MIN and INT_MAX;
    • both sides of the x > n recurrence switch and the 2^-30 tiny-argument (0x30800000) boundary, subnormals, FLT_MAX, and arguments near 3 and 100;
    • ±0, ±∞ and NaN payloads, with errno unchanged on every case;
    • finite negative ynf for every order class, with errno and existing exception flags unchanged, and invalid for ynf(-inf).

    Expected values come from upstream e_jnf.c compiled locally as a C reference, over upstream e_j0f.c and e_j1f.c at a737ef56ab. After #21013 merged, three jnf(3, x) cases near x = 3 changed (3e9e3d74, 3e9e3d78, 3e9e3d7a) and every other case matched the reference again, except the documented negative-argument NaN sign and INT_MIN wrapping.

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

    • on main, guest_bessel fails with Guest ynf(0, 3f000000): result bee397f6, expected bee397f5;
    • with this PR, guest_bessel passes.
  • Windows 11 (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release): guest_bessel passes at 3291444059, with no compiler warnings in the changed files. (Since then this branch was rebased onto current main and one debt word was fixed; the code is unchanged.)

  • Mutation check: changing a recurrence constant from 2 to 3 fails the new cases: jnf(3, 3e000000) returned 38aad5a9, expected 382a8005.

  • python3 tools/check_conventions.py --base origin/main: no errors. The one warning is about the shared host-results entry, which was already over 1,000 characters; this PR only removes jnf and ynf from it.

  • python3 tools/pr_overlap.py: no open PR changes jnf or ynf. Lifted-build PRs #15802–#15807 rewrite neighbouring Math.cpp lines, and #11634 (hypotl) edits the shared host-results debt line; both conflict textually only.

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:5275148913 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18792). Originally posted by [AlexWhitehouse](https://github.com/AlexWhitehouse) on 2026-11-12T09:47:54Z. --- ### What libc `jnf` (NID `M5KJmq-gKM8`) and `ynf` (NID `wdPaII721tY`) returned the host's double `jn`/`yn` rounded to float. The console's libc uses FreeBSD msun's float `e_jnf.c`, which runs its own float recurrences on top of `j0f`, `j1f`, `y0f` and `y1f`, so results differ from the host's in the last bits. For example, on glibc 2.43, `ynf(0, 0x3f000000)` (0.5) is `0xbee397f6` on `main` and `0xbee397f5` in msun. This PR ports [`e_jnf.c` at FreeBSD release/10.0.0](https://github.com/freebsd/freebsd-src/blob/release/10.0.0/lib/msun/src/e_jnf.c) (`__ieee754_jnf` and `__ieee754_ynf`) into a new `core/libs/prx/libc/src/Jnf.cpp`. It keeps the upstream notice byte for byte, along with the thresholds, the forward and backward recurrences, the continued-fraction start and the `logf` rescaling branch. - **Revision:** release/10.0.0, the revision of the repo's double Bessel family (`Bessel.cpp`, #7998) and of my `j0f`, `j1f`/`y1f` and `y0f` ports (#16747, #16847, #16866). #21013 has since moved `j0f`, `j1f`, `y0f` and `y1f` to `a737ef56ab`; `e_jnf.c` did not change between release/10.0.0 and that commit. - **Building blocks:** the recurrences call this libc's msun `j0f`, `j1f`, `y0f`, `y1f` and `logf`, not the host's. - **Order arithmetic:** upstream's `-n`, `n+n` and `i+i` overflow at `INT_MIN`. Here they wrap explicitly at 32 bits, which gives the same result as upstream's two's-complement build: for `INT_MIN`, the recurrence is skipped, so the result is `j1f(|x|)` or `y1f(x)` for finite nonzero `x`. - **Special values:** - NaNs propagate first, as upstream does. - `ynf(±0)` keeps the measured −∞ (#7906, #8864), with `errno` and exception flags unchanged, as the existing zero checks in `GuestBessel` require. - Finite negative arguments to `ynf` return a positive quiet NaN without setting `errno` or raising invalid, as `y0f` and `y1f` do here; `ynf(-inf)` gives an invalid NaN. - **Debt:** `jnf` and `ynf` move out of the shared host-results entry into their own entry, which records that console provenance, FMA contraction and nonzero exception flags are unverified, and that huge orders can take billions of steps, as upstream. ### Tested - **`guest_bessel`** (`core/libs/tests/GuestBessel.cpp`) gains exact-bit `jnf` and `ynf` cases for 55 order/argument pairs, and every existing assertion is kept. They cover: - orders 0–4, 6–8, 33, 34, 40, 100, 300, negative orders, `INT_MIN` and `INT_MAX`; - both sides of the `x > n` recurrence switch and the 2^-30 tiny-argument (`0x30800000`) boundary, subnormals, `FLT_MAX`, and arguments near 3 and 100; - ±0, ±∞ and NaN payloads, with `errno` unchanged on every case; - finite negative `ynf` for every order class, with `errno` and existing exception flags unchanged, and invalid for `ynf(-inf)`. Expected values come from upstream `e_jnf.c` compiled locally as a C reference, over upstream `e_j0f.c` and `e_j1f.c` at `a737ef56ab`. After #21013 merged, three `jnf(3, x)` cases near x = 3 changed (`3e9e3d74`, `3e9e3d78`, `3e9e3d7a`) and every other case matched the reference again, except the documented negative-argument NaN sign and `INT_MIN` wrapping. - **Linux x86-64** (Ubuntu 26.04, GCC 15, glibc 2.43, Release): - on `main`, `guest_bessel` fails with `Guest ynf(0, 3f000000): result bee397f6, expected bee397f5`; - with this PR, `guest_bessel` passes. - **Windows 11** (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release): `guest_bessel` passes at 3291444059, with no compiler warnings in the changed files. (Since then this branch was rebased onto current `main` and one debt word was fixed; the code is unchanged.) - **Mutation check:** changing a recurrence constant from 2 to 3 fails the new cases: `jnf(3, 3e000000)` returned `38aad5a9`, expected `382a8005`. - `python3 tools/check_conventions.py --base origin/main`: no errors. The one warning is about the shared host-results entry, which was already over 1,000 characters; this PR only removes `jnf` and `ynf` from it. - **`python3 tools/pr_overlap.py`:** no open PR changes `jnf` or `ynf`. Lifted-build PRs #15802–#15807 rewrite neighbouring `Math.cpp` lines, and #11634 (`hypotl`) edits the shared host-results debt line; both conflict textually only. ### 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)
PrivacyOnion commented 2026-11-12 11:11:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T11:11:49Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against e5774dc877:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_bessel_tests built cleanly.
  • CTest (guest_bessel):
    1/1 Test #1109: guest_bessel .....................   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/Math.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • core/libs/prx/libc/src/Jnf.cpp: Verified jnf_nid_postfix and ynf_nid_postfix for IEEE 754 special values (NaN payloads via ix > 0x7f800000, x = +-0.0f returning +-0.0f for jnf and -infinity for ynf, and x = +-infinity), confirming correct sign preservation and exception behavior without unintended errno modification.
    • core/libs/prx/libc/src/Jnf.cpp: Verified integer order handling for negative orders, INT_MIN, and INT_MAX, ensuring 32-bit two's-complement wrapping via WrappedAdd and unsigned bit-casts eliminates signed integer overflow undefined behavior while maintaining upstream recurrence termination.
    • core/libs/prx/libc/src/Jnf.cpp: Verified recurrence stability transitions in jnf_nid_postfix across the forward recurrence boundary ((float)n <= x), tiny arguments (ix < 0x30800000), and Miller backward recurrence continued-fraction convergence, correctly invoking libc's internal j0f_nid_postfix, j1f_nid_postfix, and logf_nid_postfix.
    • core/libs/prx/libc/src/Jnf.cpp: Verified ynf_nid_postfix domain handling for negative arguments (hx < 0), confirming finite negative values return quiet NaN without altering errno or raising invalid, while -infinity correctly triggers FE_INVALID via zero / zero.
    • core/libs/tests/GuestBessel.cpp: Verified CheckFloatIntegerOrders comprehensive test coverage across 55 order/input pairs, validating bit-exact output matching upstream FreeBSD msun reference values and ensuring errno and floating-point exception masks remain intact across multiple iterations.
    • docs/dev/debt/libc.md & core/libs/prx/libc/CMakeLists.txt: Verified technical debt documentation correctly reflects the port to FreeBSD msun 10.0.0 and unverified console provenance/FMA contraction, and verified CMakeLists.txt properly registers src/Jnf.cpp under target libc.
  • Minor: please drop the Generated with footer from the description; the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5873369365 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18792#pullrequestreview-5873369365). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T11:11:49Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `e5774dc877`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_bessel_tests` built cleanly. - **CTest** (`guest_bessel`): ``` 1/1 Test #1109: guest_bessel ..................... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libc/src/Math.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - core/libs/prx/libc/src/Jnf.cpp: Verified jnf_nid_postfix and ynf_nid_postfix for IEEE 754 special values (NaN payloads via ix > 0x7f800000, x = +-0.0f returning +-0.0f for jnf and -infinity for ynf, and x = +-infinity), confirming correct sign preservation and exception behavior without unintended errno modification. - core/libs/prx/libc/src/Jnf.cpp: Verified integer order handling for negative orders, INT_MIN, and INT_MAX, ensuring 32-bit two's-complement wrapping via WrappedAdd and unsigned bit-casts eliminates signed integer overflow undefined behavior while maintaining upstream recurrence termination. - core/libs/prx/libc/src/Jnf.cpp: Verified recurrence stability transitions in jnf_nid_postfix across the forward recurrence boundary ((float)n <= x), tiny arguments (ix < 0x30800000), and Miller backward recurrence continued-fraction convergence, correctly invoking libc's internal j0f_nid_postfix, j1f_nid_postfix, and logf_nid_postfix. - core/libs/prx/libc/src/Jnf.cpp: Verified ynf_nid_postfix domain handling for negative arguments (hx < 0), confirming finite negative values return quiet NaN without altering errno or raising invalid, while -infinity correctly triggers FE_INVALID via zero / zero. - core/libs/tests/GuestBessel.cpp: Verified CheckFloatIntegerOrders comprehensive test coverage across 55 order/input pairs, validating bit-exact output matching upstream FreeBSD msun reference values and ensuring errno and floating-point exception masks remain intact across multiple iterations. - docs/dev/debt/libc.md & core/libs/prx/libc/CMakeLists.txt: Verified technical debt documentation correctly reflects the port to FreeBSD msun 10.0.0 and unverified console provenance/FMA contraction, and verified CMakeLists.txt properly registers src/Jnf.cpp under target libc. - Minor: please drop the `Generated with` footer from the description; the AI-assisted checkbox can stay. Looks Good To Me
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!18792
No description provided.