fix(libc): preserve errno across fmodf domain errors #9643

Merged
Noxy-HNR merged 1 commit from upstream-pr/9643 into main 2026-10-23 13:03:10 +00:00
Noxy-HNR commented 2026-10-22 05:55:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Noxy-HNR on 2026-10-22T05:55:06Z.


What

Preserve errno around fmodf, as the existing double-precision fmod wrapper does. The original Windows library changes sentinel errno 73 to EDOM 33 for fmodf(1, 0), leaking the host CRT's policy into this model.

FreeBSD 11.4's C implementation returns the invalid-operation NaN without an errno store. Its selected amd64 assembly implementation likewise has no errno store. The wrapper continues to use the host remainder result and flags; console errno, flags and NaN payloads remain unmeasured and are recorded in debt.

Tested

Windows 11 26H2 (build 10.0.26300), WinLibs GCC 15.2.0 r7, full Release build, head ee057ea on main d6e7ab4.

  • guest_math and guest_floating_point pass, 2/2.
  • The regression covers both zero divisors, infinite operands, quiet and signaling NaNs in either operand, ordinary signed remainders, signed zero, subnormal operands and results, errno preservation, FE_INVALID and preservation of an existing exception, and restores the prior floating-point environment and errno. Against main's fmodf it fails with errno 73 changed to EDOM 33.
  • #9771 also edits core/libs/prx/libc/src/Math.cpp next to fmodf. Either landing order gives one conflict hunk there, resolved by keeping both functions; the tests and docs/dev/debt/libc.md merge cleanly. That combination built and passed guest_math and guest_floating_point, 2/2, and the #9771 author confirmed either order is fine.
  • tools/check_conventions.py --base origin/main and tools/check_test_registration.py pass. tools/pr_overlap.py and merge tests against the 103 open pull requests that touch these files find no conflict other than #9771.
  • Linux, PS5 hardware and title behavior of this revision are untested here.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4962810523 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9643). Originally posted by [Noxy-HNR](https://github.com/Noxy-HNR) on 2026-10-22T05:55:06Z. --- ### What Preserve errno around `fmodf`, as the existing double-precision `fmod` wrapper does. The original Windows library changes sentinel errno 73 to EDOM 33 for `fmodf(1, 0)`, leaking the host CRT's policy into this model. FreeBSD 11.4's [C implementation](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_fmodf.c) returns the invalid-operation NaN without an errno store. Its selected [amd64 assembly implementation](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/amd64/e_fmodf.S) likewise has no errno store. The wrapper continues to use the host remainder result and flags; console errno, flags and NaN payloads remain unmeasured and are recorded in debt. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 26H2 (build 10.0.26300), WinLibs GCC 15.2.0 r7, full Release build, head ee057ea on main d6e7ab4. - `guest_math` and `guest_floating_point` pass, 2/2. - The regression covers both zero divisors, infinite operands, quiet and signaling NaNs in either operand, ordinary signed remainders, signed zero, subnormal operands and results, errno preservation, `FE_INVALID` and preservation of an existing exception, and restores the prior floating-point environment and errno. Against main's `fmodf` it fails with errno 73 changed to EDOM 33. - #9771 also edits `core/libs/prx/libc/src/Math.cpp` next to `fmodf`. Either landing order gives one conflict hunk there, resolved by keeping both functions; the tests and `docs/dev/debt/libc.md` merge cleanly. That combination built and passed `guest_math` and `guest_floating_point`, 2/2, and [the #9771 author confirmed](https://github.com/boykopovar/AnyPS5/pull/9771#issuecomment-6347773089) either order is fine. - `tools/check_conventions.py --base origin/main` and `tools/check_test_registration.py` pass. `tools/pr_overlap.py` and merge tests against the 103 open pull requests that touch these files find no conflict other than #9771. - Linux, PS5 hardware and title behavior of this revision are untested here. ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
rooklz commented 2026-10-22 07:27:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T07:27:03Z.


Data on the console behaviour this models, from the acosf of the libc.prx bundled with PPSA28319 (catalog zip sha256 6426468091b4…, the same binary the sinhf, asinl and atanhf debt entries cite), export QI-x0SL8jhw at 0x40980 of the unwrapped module:

  • For |x| <= 1 it is FreeBSD msun's e_acosf.c instruction for instruction: the same branch points (0x3f800000, 0x3effffff, 0x32800000), pS0..qS1 (0x3e2aaa75, 0xbd2f13ba, 0xbc0dd36b, 0xbf34e5ae), pio2_hi 0x3fc90fda, pio2_lo 0x33a22168 read from .data (msun's volatile), and the 0xfffff000 mask on sqrtf(z).
  • When hx & 0x7fffffff is above 0x3f800000, which covers |x| > 1, ±inf and every NaN, it calls a helper at 0x11740 with edi = 1 (FE_INVALID) before returning (x - x) / (x - x). The helper stores 0x21 (EDOM) through libkernel's __error (the import slot at 0x10f8b8 is 9BcDykPmo1I), calls the module's feraiseexcept (0x11a70: fnstenv/fldenv and stmxcsr/ldmxcsr with the flags or-ed in, then fwait), and stores EDOM again because bit 0 is set (ERANGE, 0x22, for the overflow and underflow bits).

So on the console acosf(2.0f) sets errno to EDOM and raises FE_INVALID, and a quiet NaN argument takes the same branch, so it does too. That differs from "keeps errno" and "leaves a quiet NaN alone" in this PR's debt line, and from the errno == 0 check #3688 added from FreeBSD's source, where e_acosf.c itself doesn't touch errno but the console's build of it does. This is a reading of the binary, not a measurement on a console.

I'm not touching those paths. Separately I'm porting the |x| <= 1 part, since the host libraries differ there: over the 2,130,706,434 floats in [-1, 1], msun's acosf differs from glibc 2.39's on 1,046,180 and from Apple's x86-64 libm on 17,127,560 (acosf(0.5f) is 0x3f860a91 on macOS, 0x3f860a92 in msun). That changes the lines below the ones you edit, so the two should merge cleanly.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6335238135 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9643#issuecomment-6335238135). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T07:27:03Z. --- Data on the console behaviour this models, from the `acosf` of the `libc.prx` bundled with PPSA28319 (catalog zip sha256 `6426468091b4…`, the same binary the `sinhf`, `asinl` and `atanhf` debt entries cite), export `QI-x0SL8jhw` at `0x40980` of the unwrapped module: - For `|x| <= 1` it is FreeBSD msun's `e_acosf.c` instruction for instruction: the same branch points (`0x3f800000`, `0x3effffff`, `0x32800000`), `pS0`..`qS1` (`0x3e2aaa75`, `0xbd2f13ba`, `0xbc0dd36b`, `0xbf34e5ae`), `pio2_hi` `0x3fc90fda`, `pio2_lo` `0x33a22168` read from `.data` (msun's `volatile`), and the `0xfffff000` mask on `sqrtf(z)`. - When `hx & 0x7fffffff` is above `0x3f800000`, which covers `|x| > 1`, `±inf` and every NaN, it calls a helper at `0x11740` with `edi = 1` (`FE_INVALID`) before returning `(x - x) / (x - x)`. The helper stores `0x21` (`EDOM`) through libkernel's `__error` (the import slot at `0x10f8b8` is `9BcDykPmo1I`), calls the module's `feraiseexcept` (`0x11a70`: `fnstenv`/`fldenv` and `stmxcsr`/`ldmxcsr` with the flags or-ed in, then `fwait`), and stores `EDOM` again because bit 0 is set (`ERANGE`, `0x22`, for the overflow and underflow bits). So on the console `acosf(2.0f)` sets `errno` to `EDOM` and raises `FE_INVALID`, and a quiet NaN argument takes the same branch, so it does too. That differs from "keeps `errno`" and "leaves a quiet NaN alone" in this PR's debt line, and from the `errno == 0` check #3688 added from FreeBSD's source, where `e_acosf.c` itself doesn't touch `errno` but the console's build of it does. This is a reading of the binary, not a measurement on a console. I'm not touching those paths. Separately I'm porting the `|x| <= 1` part, since the host libraries differ there: over the 2,130,706,434 floats in `[-1, 1]`, msun's `acosf` differs from glibc 2.39's on 1,046,180 and from Apple's x86-64 libm on 17,127,560 (`acosf(0.5f)` is `0x3f860a91` on macOS, `0x3f860a92` in msun). That changes the lines below the ones you edit, so the two should merge cleanly.
jotitadev commented 2026-10-22 07:37:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T07:37:13Z.

Review: COMMENTED


Linux run, since the description lists Linux behavior as untested: the updated guest_math is red on main and green with the PR on glibc.

Machine: Linux, Ubuntu 26.04, GCC 15.2.0 (glibc host libm), x86-64, i7-8700. Head bd769c3c3 merged with git merge --no-commit onto current main d6eafa54d. Full build, BUILD_TESTING=ON, ANYPS5_RELINKER_ONLY=OFF, Ninja Release; the whole build passes.

  • PR test with main's Math.cpp: guest_math aborts on the first new case: Guest acosf failed for 3f800001: got ffc00000, expected ffc00000, invalid 0, errno 73, the same missing invalid flag and untouched sentinel errno described for Windows.
  • PR: guest_math and guest_floating_point pass (2/2).

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5610725048 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9643#pullrequestreview-5610725048). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-22T07:37:13Z. **Review:** COMMENTED --- Linux run, since the description lists Linux behavior as untested: the updated `guest_math` is red on `main` and green with the PR on glibc. **Machine:** Linux, Ubuntu 26.04, GCC 15.2.0 (glibc host libm), x86-64, i7-8700. Head `bd769c3c3` merged with `git merge --no-commit` onto current `main` `d6eafa54d`. Full build, `BUILD_TESTING=ON`, `ANYPS5_RELINKER_ONLY=OFF`, Ninja Release; the whole build passes. - **PR test with `main`'s `Math.cpp`:** `guest_math` aborts on the first new case: `Guest acosf failed for 3f800001: got ffc00000, expected ffc00000, invalid 0, errno 73`, the same missing invalid flag and untouched sentinel errno described for Windows. - **PR:** `guest_math` and `guest_floating_point` pass (2/2). Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
rooklz commented 2026-10-22 11:31:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T11:31:02Z.


A data point from @JJenkx on #9668 that narrows what I wrote above: with __error supplied to a harness, PPSA13934's libc.prx did not set errno for acosf(2.0f). That copy is a different build (FMA-contracted in the |x| <= 1 part), so the EDOM store I described is confirmed for PPSA28319's build only, and the error path can differ between the builds titles ship.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6339676109 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9643#issuecomment-6339676109). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T11:31:02Z. --- A data point from @JJenkx on #9668 that narrows what I wrote above: with `__error` supplied to a harness, PPSA13934's `libc.prx` did not set `errno` for `acosf(2.0f)`. That copy is a different build (FMA-contracted in the `|x| <= 1` part), so the `EDOM` store I described is confirmed for PPSA28319's build only, and the error path can differ between the builds titles ship.
MarcZX10 commented 2026-10-22 14:41:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T14:41:21Z.


While reviewing #9771, I compared the current heads. Its asinf insertion and this PR's fmodf change produce an adjacent-hunk conflict in Math.cpp, but the two edits compose by retaining both functions. CONTRIBUTING.md asks authors to agree merge order for overlapping PRs and record a dependency when one must land first. Could you coordinate with the #9771 author whether either order is acceptable, and note that agreement on the PR checklist? No functional conflict is apparent from the pairwise merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6343510564 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9643#issuecomment-6343510564). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T14:41:21Z. --- While reviewing #9771, I compared the current heads. Its `asinf` insertion and this PR's `fmodf` change produce an adjacent-hunk conflict in `Math.cpp`, but the two edits compose by retaining both functions. `CONTRIBUTING.md` asks authors to agree merge order for overlapping PRs and record a dependency when one must land first. Could you coordinate with the #9771 author whether either order is acceptable, and note that agreement on the PR checklist? No functional conflict is apparent from the pairwise merge.
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!9643
No description provided.