fix(libc): build libc without floating-point contraction #11059

Merged
MarcZX10 merged 4 commits from upstream-pr/11059 into main 2026-10-27 05:30:45 +00:00
MarcZX10 commented 2026-10-25 16:13:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T16:13:46Z.


What

guest_math fails on ubuntu-latest runners, which now ship Ubuntu GCC 15.2.0 (15.2.0-16ubuntu1). That compiler targets x86-64-v3 by default (g++ -Q --help=target reports -march=x86-64-v3 and -mfma [enabled]; __FP_FAST_FMA is defined). In the default gnu++20 mode GCC also defaults to -ffp-contract=fast, so it fuses the a * b + c steps of libc's msun-style float code into FMA instructions. That rounds once instead of twice and changes the last bit of results that are meant to match the console. With atanf on that runner:

Input Console Built by Ubuntu GCC 15
0x3e0007c9 0x3dfebd2a 0x3dfebd29
0xbdccce4c 0xbdcc2090 0xbdcc208f

The same atanf source built there with -ffp-contract=off or -march=x86-64 has no FMA instructions (12 with the default flags) and returns the console bits. Debian's GCC 15 and GCC 12, which target baseline x86-64, pass. Anyone who builds with a toolchain that defaults to x86-64-v3, or who adds -march=native on a CPU with FMA, gets the same drift in every libc math function written this way.

This compiles libc with -ffp-contract=off on GCC and Clang, which is what ISO -std=c++20 already does. MSVC does not contract unless /fp:contract is given, so it is left alone. The flag only stops fusion that the source does not ask for; explicit std::fma calls are unaffected.

libc_floating_point_contraction is a new CMake script test for x86-64 GCC and Clang builds. It compiles float MultiplyAdd(float a, float b, float c) { return a * b + c; } with -O2 -mfma plus libc's own -f compile options and fails if the assembly contains a vfmadd/vfmsub-style instruction. Only compilation is involved, so it guards the fix on hosts without FMA too, where guest_math cannot see the problem. BUILD.md notes why libc is built this way.

Tested

  • On an ubuntu-latest runner (AMD EPYC 7763, Ubuntu GCC 15.2.0), before this change: guest_math aborts in CheckAtanfConsoleBits (GuestMath.cpp:373 with the Require line printed). With this branch: libc_floating_point_contraction and guest_math pass.
  • Linux x86-64, GCC 12.2 (no FMA on this host): both tests pass. The script test fails when libc's options lack -ffp-contract=off (it reports vfmadd132ss) and passes with it, with GCC 12, Debian GCC 15 and Clang 19.
  • On fork runners, -ffp-contract=off compiles with MinGW-Builds GCC 15.2.0 on windows-latest, Apple clang 17.0.0 on macos-15-intel and Apple clang 21.0.0 on macos-latest, and the contraction script test passes with the first two (x86-64). The full project was not built there, since this pull request's linux and windows jobs are skipped by path.
  • python3 tools/check_conventions.py --base upstream/main and git diff --check upstream/main pass. tools/pr_overlap.py --branch lists only pull requests that also edit core/libs/prx/libc/CMakeLists.txt or docs/dev/BUILD.md, with no conflicting lines.

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 (not applicable; no third-party code added)
  • 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:5006416003 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11059). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T16:13:46Z. --- ### What `guest_math` fails on `ubuntu-latest` runners, which now ship Ubuntu GCC 15.2.0 (`15.2.0-16ubuntu1`). That compiler targets x86-64-v3 by default (`g++ -Q --help=target` reports `-march=x86-64-v3` and `-mfma [enabled]`; `__FP_FAST_FMA` is defined). In the default `gnu++20` mode GCC also defaults to `-ffp-contract=fast`, so it fuses the `a * b + c` steps of libc's msun-style float code into FMA instructions. That rounds once instead of twice and changes the last bit of results that are meant to match the console. With `atanf` on that runner: | Input | Console | Built by Ubuntu GCC 15 | |---|---|---| | `0x3e0007c9` | `0x3dfebd2a` | `0x3dfebd29` | | `0xbdccce4c` | `0xbdcc2090` | `0xbdcc208f` | The same `atanf` source built there with `-ffp-contract=off` or `-march=x86-64` has no FMA instructions (12 with the default flags) and returns the console bits. Debian's GCC 15 and GCC 12, which target baseline x86-64, pass. Anyone who builds with a toolchain that defaults to x86-64-v3, or who adds `-march=native` on a CPU with FMA, gets the same drift in every libc math function written this way. This compiles libc with `-ffp-contract=off` on GCC and Clang, which is what ISO `-std=c++20` already does. MSVC does not contract unless `/fp:contract` is given, so it is left alone. The flag only stops fusion that the source does not ask for; explicit `std::fma` calls are unaffected. `libc_floating_point_contraction` is a new CMake script test for x86-64 GCC and Clang builds. It compiles `float MultiplyAdd(float a, float b, float c) { return a * b + c; }` with `-O2 -mfma` plus libc's own `-f` compile options and fails if the assembly contains a `vfmadd`/`vfmsub`-style instruction. Only compilation is involved, so it guards the fix on hosts without FMA too, where `guest_math` cannot see the problem. BUILD.md notes why libc is built this way. ### Tested - On an `ubuntu-latest` runner (AMD EPYC 7763, Ubuntu GCC 15.2.0), before this change: `guest_math` aborts in `CheckAtanfConsoleBits` (`GuestMath.cpp:373` with the `Require` line printed). With this branch: `libc_floating_point_contraction` and `guest_math` pass. - Linux x86-64, GCC 12.2 (no FMA on this host): both tests pass. The script test fails when libc's options lack `-ffp-contract=off` (it reports `vfmadd132ss`) and passes with it, with GCC 12, Debian GCC 15 and Clang 19. - On fork runners, `-ffp-contract=off` compiles with MinGW-Builds GCC 15.2.0 on `windows-latest`, Apple clang 17.0.0 on `macos-15-intel` and Apple clang 21.0.0 on `macos-latest`, and the contraction script test passes with the first two (x86-64). The full project was not built there, since this pull request's `linux` and `windows` jobs are skipped by path. - `python3 tools/check_conventions.py --base upstream/main` and `git diff --check upstream/main` pass. `tools/pr_overlap.py --branch` lists only pull requests that also edit `core/libs/prx/libc/CMakeLists.txt` or `docs/dev/BUILD.md`, with no conflicting lines. ### 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 (not applicable; no third-party code added) - [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
THEMANnt commented 2026-10-26 19:16:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-10-26T19:16:46Z.

Review: APPROVED


Reviewed 6c1ac043c2888afb838449ee5ef477762d33f7af. Approve; no actionable finding in this focused review.

The libc compile option disables contraction for GNU/Clang regardless of target CPU, and the focused test receives the actual target compile options. The x86-only assembly check is kept off unsupported toolchains.

Validation: Ran the submitted LibcFloatingPointContraction.cmake with WinLibs GCC 15.2: -ffp-contract=off passes. The controlled fast-contraction arm fails after detecting vfmadd132ss, showing that the check is sensitive.

Scope / next step: No full libc math, Clang or console bit-accuracy run is claimed.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5657682183 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11059#pullrequestreview-5657682183). Originally posted by [THEMANnt](https://github.com/THEMANnt) on 2026-10-26T19:16:46Z. **Review:** APPROVED --- Reviewed `6c1ac043c2888afb838449ee5ef477762d33f7af`. Approve; no actionable finding in this focused review. The libc compile option disables contraction for GNU/Clang regardless of target CPU, and the focused test receives the actual target compile options. The x86-only assembly check is kept off unsupported toolchains. **Validation:** Ran the submitted LibcFloatingPointContraction.cmake with WinLibs GCC 15.2: -ffp-contract=off passes. The controlled fast-contraction arm fails after detecting vfmadd132ss, showing that the check is sensitive. **Scope / next step:** No full libc math, Clang or console bit-accuracy run is claimed.
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!11059
No description provided.