fix(libc): round fmal correctly with FreeBSD msun s_fmal.c #18524

Merged
AlfaRojo merged 5 commits from upstream-pr/18524 into main 2026-11-16 06:07:46 +00:00
AlfaRojo commented 2026-11-11 20:06:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlfaRojo on 2026-11-11T20:06:26Z.


What

fmal called the host std::fma(long double) after its special cases. On Windows (WinLibs) that is not correctly rounded: against exact rational arithmetic it was wrong for about 29% of random operands and 11% of operands that cancel, and it returned x * y whenever z was far below the product, which loses the sticky bit (a product that is an exact tie and a tiny negative z must round down).

It is now FreeBSD msun s_fmal.c from release/11.4.0 (Dekker splitting, scaling, sticky-bit adjustment, directed rounding modes), in core/libs/prx/libc/src/MathExact.cpp. The special cases and their order are unchanged. The 11.4 source, still unfixed in FreeBSD main, calls the double ldexp on its extended-subnormal path (line 119), which loses that result; the long double overload of ldexp is used there, the only divergence from the source. It is recorded in docs/dev/debt/libc.md with the 53-bit x87 precision caveat. The tie check in GuestMathExact.cpp (a tie product with a tiny negative z) previously expected x * y, from 4f608a8c49, whose message claims a bit-for-bit match with PPSA08427's libc.prx. Unmodified msun built with host gcc returns the lower neighbour for these operands, as this port does, so the expected bits now follow msun; the claimed libc.prx match is unverified here.

Tested

Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Ninja, Release, based on main 65351c2931.

  • guest_math_exact_tests passes 3/3 runs. New checks: Annex F specials (infinity times zero, infinity minus infinity, NaN propagation, signed zeros, overflow, LDBL_MAX * 1 + LDBL_MAX), 100+ cases whose expected bits were computed with Python fractions exact arithmetic and agree with unmodified msun built with host gcc (nearest, down, up, toward zero; cancellation, near-tie, z far above and far below, subnormal results with ties, x * y overflowing while the result is finite) and the sign of an exact zero in each rounding mode. The host-differing example -0xd.80abeb4fa460f27p-6 * -0xe.21454d37039f87cp-14 + -0xb.ecaa4c8a13997a9p-16 gives 0x9.3815b7c884c1ee4p-80 (host 0x9.3815b7e884c1ee4p-80).
  • Negative control: with MathExact.cpp from main the test aborts at the corrected tie check (GuestMathExact.cpp:139).
  • Port vs unmodified msun s_fmal.c (host gcc), 16,000,000 calls over all four rounding modes (random, cancellation, z far above and far below the product, subnormal results, exact ties): the port equals msun with the long double ldexp in every call; against the unmodified source it differs in 116,891 calls, all extended-subnormal results through the double ldexp. The comparison found a far-apart toward-zero branch with an inverted sign test, fixed with a test for all four modes.
  • Not compared with a libc.prx or a console.
  • tools/check_test_registration.py and tools/check_conventions.py --base upstream/main pass.
  • tools/pr_overlap.py found 116 open PRs sharing files, all through docs/dev/debt/libc.md or GuestMathExact.cpp. It could not compare 44 of them because they conflict with the base. I grepped the diffs of the PRs it could not compare that touch MathExact.cpp or the test (#10780 _LExp, #15141, #15142, #15143) and the others: none mentions fmal except #14212 (unrelated debt wording) and #18122, which only adds a libSceLibcInternal forward that calls fmal_nid_postfix and does not change it. gh pr list --search fmal shows no open PR.
  • Head 9b00be5c6e (merged with upstream/main): guest_math, guest_math_elementary and guest_math_exact, all three test executables rebuilt, pass in 3/3 runs; check_conventions.py --base upstream/main reports no errors.

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:5266067382 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18524). Originally posted by [AlfaRojo](https://github.com/AlfaRojo) on 2026-11-11T20:06:26Z. --- ### What `fmal` called the host `std::fma(long double)` after its special cases. On Windows (WinLibs) that is not correctly rounded: against exact rational arithmetic it was wrong for about 29% of random operands and 11% of operands that cancel, and it returned `x * y` whenever `z` was far below the product, which loses the sticky bit (a product that is an exact tie and a tiny negative `z` must round down). It is now FreeBSD msun [`s_fmal.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_fmal.c) from release/11.4.0 (Dekker splitting, scaling, sticky-bit adjustment, directed rounding modes), in `core/libs/prx/libc/src/MathExact.cpp`. The special cases and their order are unchanged. The 11.4 source, still unfixed in FreeBSD main, calls the double `ldexp` on its extended-subnormal path ([line 119](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_fmal.c#L119)), which loses that result; the long double overload of `ldexp` is used there, the only divergence from the source. It is recorded in `docs/dev/debt/libc.md` with the 53-bit x87 precision caveat. The tie check in `GuestMathExact.cpp` (a tie product with a tiny negative `z`) previously expected `x * y`, from 4f608a8c49, whose message claims a bit-for-bit match with PPSA08427's `libc.prx`. Unmodified msun built with host gcc returns the lower neighbour for these operands, as this port does, so the expected bits now follow msun; the claimed `libc.prx` match is unverified here. ### Tested Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Ninja, Release, based on `main` 65351c2931. - `guest_math_exact_tests` passes 3/3 runs. New checks: Annex F specials (infinity times zero, infinity minus infinity, NaN propagation, signed zeros, overflow, `LDBL_MAX * 1 + LDBL_MAX`), 100+ cases whose expected bits were computed with Python `fractions` exact arithmetic and agree with unmodified msun built with host gcc (nearest, down, up, toward zero; cancellation, near-tie, `z` far above and far below, subnormal results with ties, `x * y` overflowing while the result is finite) and the sign of an exact zero in each rounding mode. The host-differing example `-0xd.80abeb4fa460f27p-6 * -0xe.21454d37039f87cp-14 + -0xb.ecaa4c8a13997a9p-16` gives `0x9.3815b7c884c1ee4p-80` (host `0x9.3815b7e884c1ee4p-80`). - Negative control: with `MathExact.cpp` from `main` the test aborts at the corrected tie check (`GuestMathExact.cpp:139`). - Port vs unmodified msun `s_fmal.c` (host gcc), 16,000,000 calls over all four rounding modes (random, cancellation, z far above and far below the product, subnormal results, exact ties): the port equals msun with the long double `ldexp` in every call; against the unmodified source it differs in 116,891 calls, all extended-subnormal results through the double `ldexp`. The comparison found a far-apart toward-zero branch with an inverted sign test, fixed with a test for all four modes. - Not compared with a `libc.prx` or a console. - `tools/check_test_registration.py` and `tools/check_conventions.py --base upstream/main` pass. - `tools/pr_overlap.py` found 116 open PRs sharing files, all through `docs/dev/debt/libc.md` or `GuestMathExact.cpp`. It could not compare 44 of them because they conflict with the base. I grepped the diffs of the PRs it could not compare that touch `MathExact.cpp` or the test (#10780 `_LExp`, #15141, #15142, #15143) and the others: none mentions `fmal` except #14212 (unrelated debt wording) and #18122, which only adds a `libSceLibcInternal` forward that calls `fmal_nid_postfix` and does not change it. `gh pr list --search fmal` shows no open PR. - Head `9b00be5c6e` (merged with upstream/main): `guest_math`, `guest_math_elementary` and `guest_math_exact`, all three test executables rebuilt, pass in 3/3 runs; `check_conventions.py --base upstream/main` reports no errors. ### Checklist - [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
PrivacyOnion commented 2026-11-12 06:08:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T06:08:39Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_math_exact_tests built cleanly.
  • CTest (guest_math_exact):
    1/1 Test #1067: guest_math_exact .................   Passed    0.05 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/MathExact.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1067: guest_math_exact .................Subprocess aborted***Exception:   0.37 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified Dekker splitting and two-sum arithmetic in MultiplyExact and AddExact (core/libs/prx/libc/src/MathExact.cpp:424-464), ensuring exact decomposition of 64-bit significands using Split = 0x1p32L + 1.0L without intermediate overflow for normalized inputs in [0.5, 1.0).
    • Verified directed rounding mode behavior in FmaFarApart (core/libs/prx/libc/src/MathExact.cpp:466-480) for spread < -64, confirming that FE_TONEAREST, FE_TOWARDZERO, FE_DOWNWARD, and FE_UPWARD correctly perturb z towards or away from zero according to product and operand signs.
    • Verified subnormal handling in AddAndDenormalize (core/libs/prx/libc/src/MathExact.cpp:442-451), confirming the sign bit is masked out when calculating bitsLost, round-to-odd/sticky bit adjustment functions correctly, and long double std::ldexp avoids truncation.
    • Verified special case handling in fmal_nid_postfix (core/libs/prx/libc/src/MathExact.cpp:596-612), including infinity times zero, infinity subtraction NaNs, signaling NaN passthrough, and sign preservation of exact zeros under FE_DOWNWARD vs other modes.
    • Verified floating-point environment and errno isolation across FmaScaled and fmal_nid_postfix (core/libs/prx/libc/src/MathExact.cpp:481-499, 603-611), ensuring ErrnoGuard preserves errno and std::fesetround restores the caller's rounding mode on all return paths.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5869884850 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18524#pullrequestreview-5869884850). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T06:08:39Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `6f22dea341`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_math_exact_tests` built cleanly. - **CTest** (`guest_math_exact`): ``` 1/1 Test #1067: guest_math_exact ................. Passed 0.05 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libc/src/MathExact.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #1067: guest_math_exact .................Subprocess aborted***Exception: 0.37 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified Dekker splitting and two-sum arithmetic in MultiplyExact and AddExact (core/libs/prx/libc/src/MathExact.cpp:424-464), ensuring exact decomposition of 64-bit significands using Split = 0x1p32L + 1.0L without intermediate overflow for normalized inputs in [0.5, 1.0). - Verified directed rounding mode behavior in FmaFarApart (core/libs/prx/libc/src/MathExact.cpp:466-480) for spread < -64, confirming that FE_TONEAREST, FE_TOWARDZERO, FE_DOWNWARD, and FE_UPWARD correctly perturb z towards or away from zero according to product and operand signs. - Verified subnormal handling in AddAndDenormalize (core/libs/prx/libc/src/MathExact.cpp:442-451), confirming the sign bit is masked out when calculating bitsLost, round-to-odd/sticky bit adjustment functions correctly, and long double std::ldexp avoids truncation. - Verified special case handling in fmal_nid_postfix (core/libs/prx/libc/src/MathExact.cpp:596-612), including infinity times zero, infinity subtraction NaNs, signaling NaN passthrough, and sign preservation of exact zeros under FE_DOWNWARD vs other modes. - Verified floating-point environment and errno isolation across FmaScaled and fmal_nid_postfix (core/libs/prx/libc/src/MathExact.cpp:481-499, 603-611), ensuring ErrnoGuard preserves errno and std::fesetround restores the caller's rounding mode on all return paths. - Note: 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!18524
No description provided.