fix(libc): raise FE_INVALID from fdim and fdimf for equal infinities #12366

Merged
JJenkx merged 5 commits from upstream-pr/12366 into main 2026-11-05 00:18:45 +00:00
JJenkx commented 2026-10-28 11:20:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-28T11:20:23Z.


What

Titles' libc.prx computes x - y before comparing it with zero in fdim and fdimf. So fdim(inf, inf) and fdim(-inf, -inf) return +0 and raise FE_INVALID, from the invalid inf - inf. The exports here forwarded to the host fdim, which compares first and raised nothing.

Both now return the host result for a NaN argument, which already matches the titles and raises nothing for a quiet NaN. Otherwise they compute the difference and return it when it is positive, else +0. A new CheckFdimFlags covers equal infinities (+0 with FE_INVALID), ordinary and infinite differences, and NaN arguments (NaN, no flag).

Tested

Current-main integration — 2026-11-04. Head c5d31ec4d7835ddc1fcf938afa87b51447015bd8 includes main b11c7bf84f5d2f636ff60166cc3371bd071e24d5 and preserves previous public commits and attribution/licence notices. Retains the fdim/fdimf result, errno and exception regressions alongside current main.

Linux x86-64, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON: rebuilt actual libc/Ampr and selected executables. guest_math, guest_math_exact, guest_dinkumware_internals each pass 3/3 (9 runtime passes, zero skips); public_headers, conventions and whitespace checks pass. Pinned main passes all 17 selected baseline groups 3/3 (51 passes) plus headers. Before/after dependency checks agree: 34 recursive trees and 31,551 tracked blobs, zero errors; the update=none QuickJS test262 corpus is explicitly excluded from these library inputs.

Source/parent/gitlink proofs, library/test hashes, raw logs/JUnit and head-leased push receipts: qualification archive, SHA-256 7c347f620951ad6f39068740750949212c6412815da094a46a96eca1d15c8bbf (download verified). Older platform, stress, negative-control and reference reports retain their original pins; no new Windows/macOS, full-suite, SDK-module, console, title/GPU or performance claim. AI-assisted: yes (Codex).

Earlier dated qualifications below retain their original source/platform pins.

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

  • guest_math passes in each of 3 runs.
  • Negative control: with main's Math.cpp and this test, guest_math fails.
  • Special-value pairs against titles' own libc.prx: I loaded PPSA07923's and PPSA13934's bundled libc.prx natively and called fdim and fdimf by NID, side by side with this build. That covered 441 pairs from 21 values (±0, ±1, ±2, ±0.5, ±3, ±1.5, ±inf, ±NaN, ±1e300, a subnormal, 1024, -1075), comparing result bits, errno and raised FE_INVALID/FE_DIVBYZERO/FE_OVERFLOW/FE_UNDERFLOW:
    • fdim: main 2 differences → 0 (the two equal-infinity pairs);
    • fdimf: 8 → 0.
  • Random inputs (PPSA07923, PPSA05916; 300,000 pairs each): 0 value and 0 errno differences for both.

Not tested on Linux or macOS.

Overlap: tools/pr_overlap.py reports AlexFigas's #11260, #11262 and #11266 editing Math.cpp lines 550–552. Their acosl line is next to the two fdim lines this replaces, so it's a textual adjacency with no shared behaviour. AlexFigas agreed on #11260 that this lands first and rebased that stack to depend on it.

Independent reports on this head (their runs, summarised here):

  • @bunbeans2-arch (report): Windows 11 Pro, MSYS2 UCRT64 GCC 16.1.0, head d19ce8c4c1 applied to main 673fa865f4: guest_math passes 5/5; with Math.cpp from main it fails 5/5 at the first new fdim check.

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 (AlexFigas agreed on #11260 that this goes first; #11260 now depends on it)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5049608324 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12366). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-28T11:20:23Z. --- ### What Titles' `libc.prx` computes `x - y` before comparing it with zero in `fdim` and `fdimf`. So `fdim(inf, inf)` and `fdim(-inf, -inf)` return `+0` and raise `FE_INVALID`, from the invalid `inf - inf`. The exports here forwarded to the host `fdim`, which compares first and raised nothing. Both now return the host result for a NaN argument, which already matches the titles and raises nothing for a quiet NaN. Otherwise they compute the difference and return it when it is positive, else `+0`. A new `CheckFdimFlags` covers equal infinities (`+0` with `FE_INVALID`), ordinary and infinite differences, and NaN arguments (NaN, no flag). ### Tested **Current-main integration — 2026-11-04.** Head `c5d31ec4d7835ddc1fcf938afa87b51447015bd8` includes main `b11c7bf84f5d2f636ff60166cc3371bd071e24d5` and preserves previous public commits and attribution/licence notices. Retains the fdim/fdimf result, errno and exception regressions alongside current main. Linux x86-64, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON: rebuilt actual libc/Ampr and selected executables. `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` each pass 3/3 (**9 runtime passes, zero skips**); `public_headers`, conventions and whitespace checks pass. Pinned main passes all 17 selected baseline groups 3/3 (51 passes) plus headers. Before/after dependency checks agree: 34 recursive trees and 31,551 tracked blobs, zero errors; the update=none QuickJS test262 corpus is explicitly excluded from these library inputs. Source/parent/gitlink proofs, library/test hashes, raw logs/JUnit and head-leased push receipts: [qualification archive](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-libc10-mainb11c-linux-20261104.tar.gz), SHA-256 `7c347f620951ad6f39068740750949212c6412815da094a46a96eca1d15c8bbf` (download verified). Older platform, stress, negative-control and reference reports retain their original pins; no new Windows/macOS, full-suite, SDK-module, console, title/GPU or performance claim. AI-assisted: yes (Codex). Earlier dated qualifications below retain their original source/platform pins. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, on `main` `3a40945e62`. - `guest_math` passes in each of 3 runs. - **Negative control:** with `main`'s `Math.cpp` and this test, `guest_math` fails. - **Special-value pairs against titles' own `libc.prx`:** I loaded PPSA07923's and PPSA13934's bundled `libc.prx` natively and called `fdim` and `fdimf` by NID, side by side with this build. That covered 441 pairs from 21 values (±0, ±1, ±2, ±0.5, ±3, ±1.5, ±inf, ±NaN, ±1e300, a subnormal, 1024, -1075), comparing result bits, `errno` and raised `FE_INVALID`/`FE_DIVBYZERO`/`FE_OVERFLOW`/`FE_UNDERFLOW`: - `fdim`: `main` 2 differences → **0** (the two equal-infinity pairs); - `fdimf`: 8 → **0**. - **Random inputs** (PPSA07923, PPSA05916; 300,000 pairs each): 0 value and 0 `errno` differences for both. Not tested on Linux or macOS. **Overlap:** `tools/pr_overlap.py` reports AlexFigas's #11260, #11262 and #11266 editing `Math.cpp` lines 550–552. Their `acosl` line is next to the two `fdim` lines this replaces, so it's a textual adjacency with no shared behaviour. AlexFigas agreed on #11260 that this lands first and rebased that stack to depend on it. **Independent reports** on this head (their runs, summarised here): - @bunbeans2-arch ([report](https://github.com/boykopovar/AnyPS5/pull/12366#issuecomment-6512191009)): Windows 11 Pro, MSYS2 UCRT64 GCC 16.1.0, head `d19ce8c4c1` applied to `main` `673fa865f4`: `guest_math` passes 5/5; with `Math.cpp` from `main` it fails 5/5 at the first new `fdim` check. ### 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 (AlexFigas agreed on #11260 that this goes first; #11260 now depends on it) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-10-28 11:21:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T11:21:10Z.

Review: COMMENTED


I reviewed the change and the special-value tests. Computing the subtraction before the comparison reproduces the equal-infinity invalid exception; the NaN path preserves the tested title behavior, and returning literal 0.0 keeps the nonpositive result positive zero. The paired title-libc comparisons described here are useful evidence. I have not approved yet: the current head 53ac55da has only started its CI, and the first conventions run was cancelled. I will recheck after the refreshed checks complete.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5681799940 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12366#pullrequestreview-5681799940). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T11:21:10Z. **Review:** COMMENTED --- I reviewed the change and the special-value tests. Computing the subtraction before the comparison reproduces the equal-infinity invalid exception; the NaN path preserves the tested title behavior, and returning literal `0.0` keeps the nonpositive result positive zero. The paired title-libc comparisons described here are useful evidence. I have not approved yet: the current head `53ac55da` has only started its CI, and the first conventions run was cancelled. I will recheck after the refreshed checks complete.
rooklz commented 2026-10-28 13:14:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T13:14:16Z.

Review: COMMENTED


macOS / clang + GCC check at head 53ac55da58 (base main 3a40945e62; merges cleanly onto 750cf6cf48)

The body says Linux and macOS were not tested. To check it here, I built the two fdim/fdimf bodies from Math.cpp (head and main) as a separate translation unit, which is how libc is built, together with this PR's CheckFdimFlags unchanged. I didn't build the full libc target (its submodules aren't available here). Apple clang 21.0.0 and Homebrew GCC 16.2.0, macOS.

impl clang arm64 -O0/-O2/-O3 clang x86_64 (Rosetta) -O0/-O2/-O3 GCC 16 -O2/-O3
head OK OK OK
main FAIL FAIL FAIL

So the fix and the new test both hold on macOS and with libc++. check_conventions --base upstream/main: passed.

Mutants (separate TU, the PR test as-is):

  • NaN arm dropped → killed. NaN arm returns x → killed. : 0.0 → : -0.0 → killed.
  • difference > 0.0 → difference >= 0.0 SURVIVES. With fdim(-0.0, +0.0) the difference is -0.0, so >= returns -0.0 where head returns +0. Your 441-pair comparison includes the ±0 pairs and reports 0 differences, so the titles' libc returns +0 there. This one line kills the mutant (I checked it against head and >=):
    Require(std::bit_cast<std::uint64_t>(fdim_nid_postfix(-0.0, 0.0)) == 0 && std::bit_cast<std::uint32_t>(fdimf_nid_postfix(-0.0f, 0.0f)) == 0);
    
  • Compare-first (return x > y ? x - y : 0.0;, which is main's semantics) is killed at -O0 and under GCC -O2 (it branches with comisd/jbe before subsd). It is not killed under clang -O2 on either arch. Clang turns the select into an unconditional fsub + fcsel/maxsd, so inf - inf raises anyway. This doesn't block anything: the GCC lanes do discriminate. It does mean that on clang builds the test only proves the flag, not the evaluation order.

Non-blocking robustness note. If the body gets inlined into a constant-argument caller, clang folds inf - inf at compile time and the flag is lost. That happens with a single TU, and with -flto across TUs: head FAILS the test that way on both arches. -frounding-math (or -ffp-exception-behavior=strict) on the defining TU brings it back (head OK under LTO). GCC 16 keeps the flag even with -flto. Nothing in the tree enables IPO/LTO today, so this is latent. MathExact.cpp already gets -frounding-math in core/libs/prx/libc/CMakeLists.txt. If more flag-sensitive exports land in Math.cpp, giving it the same treatment would make them independent of inlining.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5683136294 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12366#pullrequestreview-5683136294). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-28T13:14:16Z. **Review:** COMMENTED --- ### macOS / clang + GCC check at head `53ac55da58` (base `main` `3a40945e62`; merges cleanly onto `750cf6cf48`) The body says Linux and macOS were not tested. To check it here, I built the two `fdim`/`fdimf` bodies from `Math.cpp` (head and `main`) as a **separate translation unit**, which is how `libc` is built, together with this PR's `CheckFdimFlags` unchanged. I didn't build the full libc target (its submodules aren't available here). Apple clang 21.0.0 and Homebrew GCC 16.2.0, macOS. | impl | clang arm64 -O0/-O2/-O3 | clang x86_64 (Rosetta) -O0/-O2/-O3 | GCC 16 -O2/-O3 | |---|---|---|---| | head | OK | OK | OK | | `main` | FAIL | FAIL | FAIL | So the fix and the new test both hold on macOS and with libc++. `check_conventions --base upstream/main`: passed. Mutants (separate TU, the PR test as-is): - NaN arm dropped → killed. NaN arm returns `x` → killed. `: 0.0` → `: -0.0` → killed. - **`difference > 0.0` → `difference >= 0.0` SURVIVES.** With `fdim(-0.0, +0.0)` the difference is `-0.0`, so `>=` returns `-0.0` where head returns `+0`. Your 441-pair comparison includes the ±0 pairs and reports 0 differences, so the titles' libc returns `+0` there. This one line kills the mutant (I checked it against head and `>=`): ```cpp Require(std::bit_cast<std::uint64_t>(fdim_nid_postfix(-0.0, 0.0)) == 0 && std::bit_cast<std::uint32_t>(fdimf_nid_postfix(-0.0f, 0.0f)) == 0); ``` - Compare-first (`return x > y ? x - y : 0.0;`, which is `main`'s semantics) is killed at -O0 and under GCC -O2 (it branches with `comisd`/`jbe` before `subsd`). It is **not** killed under clang -O2 on either arch. Clang turns the select into an unconditional `fsub` + `fcsel`/`maxsd`, so `inf - inf` raises anyway. This doesn't block anything: the GCC lanes do discriminate. It does mean that on clang builds the test only proves the flag, not the evaluation order. Non-blocking robustness note. If the body gets inlined into a constant-argument caller, clang folds `inf - inf` at compile time and the flag is lost. That happens with a single TU, and with `-flto` across TUs: head FAILS the test that way on both arches. `-frounding-math` (or `-ffp-exception-behavior=strict`) on the defining TU brings it back (head OK under LTO). GCC 16 keeps the flag even with `-flto`. Nothing in the tree enables IPO/LTO today, so this is latent. `MathExact.cpp` already gets `-frounding-math` in `core/libs/prx/libc/CMakeLists.txt`. If more flag-sensitive exports land in `Math.cpp`, giving it the same treatment would make them independent of inlining. AI-assisted: yes (Claude Code)
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!12366
No description provided.