fix(libc): compute erfl and erfcl with FreeBSD msun's ld80 s_erfl.c #19342

Merged
AlfaRojo merged 6 commits from upstream-pr/19342 into main 2026-11-15 22:38:55 +00:00
AlfaRojo commented 2026-11-13 04:42:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlfaRojo on 2026-11-13T04:42:31Z.


What

erfl and erfcl returned the host's results. They now follow FreeBSD msun's ld80 s_erfl.c (release/11.4.0) over the msun expl, including NaN, infinities, the tiny-argument branch that avoids spurious underflow, and the overflow-to-1, underflow-to-0 and 2 - tiny cut-offs. The unverified console behaviour is recorded in docs/dev/debt/libc.md.

This branch is stacked on #18525 (msun expl), so until that merges the diff also shows its commits. This PR is the two commits after 765ca80990: 53b4f14658 (the port) and d3e19f99fc (one extra test).

Tested

Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7, Release.

  • Red on the base Math.cpp with the new tests: Guest erfl failed for 0xc.ccccccccccccccdp-7, then abort.
  • Green: ctest -R '^guest_math' passes 3 of 3 (guest_math, guest_math_elementary, guest_math_exact), three runs.
  • The expected bits come from the real FreeBSD s_erfl.c, s_expl.c and k_expl.h compiled standalone on the host, not from the host libm. The tests cover 49 erfl and 57 erfcl inputs on every branch, the exception flags, 53-bit precision and erfl leaving FE_UNDERFLOW clear for a tiny argument.
  • Run locally with a harness that is not in the repository: 100 million random inputs at 64-bit and 5 million at 53-bit precision give 0 bitwise differences from that oracle for both functions.
  • Not compared against a console libc.prx; the 11.4.0 source is the only evidence.
  • tools/check_conventions.py against main and against the stacked base: 0 errors (one length warning on an existing debt entry, not mine).
  • No open PR defines erfl or erfcl (checked #10226, #18694, #18787, #18792, #19328 and every open libc/math PR). tools/pr_overlap.py lists open PRs that touch docs/dev/debt/libc.md, and #11266 (exp2l) for Math.cpp and GuestMath.cpp.
  • Head ac6b3c4b4b (merged with upstream/main, which now has #18525 and #19328): 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: #18525
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5290159981 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19342). Originally posted by [AlfaRojo](https://github.com/AlfaRojo) on 2026-11-13T04:42:31Z. --- ### What `erfl` and `erfcl` returned the host's results. They now follow FreeBSD msun's ld80 [`s_erfl.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/ld80/s_erfl.c) (release/11.4.0) over the msun `expl`, including NaN, infinities, the tiny-argument branch that avoids spurious underflow, and the overflow-to-1, underflow-to-0 and `2 - tiny` cut-offs. The unverified console behaviour is recorded in `docs/dev/debt/libc.md`. This branch is stacked on #18525 (msun `expl`), so until that merges the diff also shows its commits. This PR is the two commits after 765ca80990: 53b4f14658 (the port) and d3e19f99fc (one extra test). ### 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 Pro 10.0.26200, WinLibs GCC 15.2.0 r7, Release. - Red on the base `Math.cpp` with the new tests: `Guest erfl failed for 0xc.ccccccccccccccdp-7`, then abort. - Green: `ctest -R '^guest_math'` passes 3 of 3 (`guest_math`, `guest_math_elementary`, `guest_math_exact`), three runs. - The expected bits come from the real FreeBSD `s_erfl.c`, `s_expl.c` and `k_expl.h` compiled standalone on the host, not from the host libm. The tests cover 49 `erfl` and 57 `erfcl` inputs on every branch, the exception flags, 53-bit precision and `erfl` leaving `FE_UNDERFLOW` clear for a tiny argument. - Run locally with a harness that is not in the repository: 100 million random inputs at 64-bit and 5 million at 53-bit precision give 0 bitwise differences from that oracle for both functions. - Not compared against a console `libc.prx`; the 11.4.0 source is the only evidence. - `tools/check_conventions.py` against `main` and against the stacked base: 0 errors (one length warning on an existing debt entry, not mine). - No open PR defines `erfl` or `erfcl` (checked #10226, #18694, #18787, #18792, #19328 and every open libc/math PR). `tools/pr_overlap.py` lists open PRs that touch `docs/dev/debt/libc.md`, and #11266 (`exp2l`) for `Math.cpp` and `GuestMath.cpp`. - Head `ac6b3c4b4b` (merged with upstream/main, which now has #18525 and #19328): `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 <!-- 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: #18525 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-13 04:44:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-13T04:44:25Z.

Review: CHANGES_REQUESTED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_math_tests built cleanly.
  • CTest (guest_math, guest_math_elementary, guest_math_exact):
    1/3 Test #1046: guest_math .......................   Passed    0.01 sec
    2/3 Test #1047: guest_math_elementary ............   Passed    0.01 sec
    3/3 Test #1120: guest_math_exact .................Subprocess aborted***Exception:   0.12 sec
    67% tests passed, 1 tests failed out of 3
    
    The following tests FAILED:
    
  • Note: the AI-assisted checkbox can stay.

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5886245453 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19342#pullrequestreview-5886245453). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-13T04:44:25Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `d3e19f99fc`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_math_tests` built cleanly. - **CTest** (`guest_math`, `guest_math_elementary`, `guest_math_exact`): ``` 1/3 Test #1046: guest_math ....................... Passed 0.01 sec 2/3 Test #1047: guest_math_elementary ............ Passed 0.01 sec 3/3 Test #1120: guest_math_exact .................Subprocess aborted***Exception: 0.12 sec 67% tests passed, 1 tests failed out of 3 ``` ``` The following tests FAILED: ``` - Note: the AI-assisted checkbox can stay. Requesting changes for the points above.
PrivacyOnion commented 2026-11-15 20:23:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T20:23:54Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_math_tests built cleanly.
  • CTest (guest_math, guest_math_elementary, guest_math_exact):
    1/3 Test #1120: guest_math_exact .................   Passed    0.03 sec
    2/3 Test #1047: guest_math_elementary ............   Passed    0.04 sec
    3/3 Test #1046: guest_math .......................   Passed    0.03 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libc/src/Math.cpp reverted to main and the new tests kept, the suite fails:
    1/3 Test #1047: guest_math_elementary ............   Passed    0.03 sec
    2/3 Test #1120: guest_math_exact .................   Passed    0.06 sec
    3/3 Test #1046: guest_math .......................Subprocess aborted***Exception:   0.48 sec
    67% tests passed, 1 tests failed out of 3
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libc/src/Math.cpp: Verified ErflMsun and ErfclMsun handle all IEEE 754 special values including signed zeros (+-0.0), subnormals avoiding spurious FE_UNDERFLOW via (8 * x + ErflEfx8 * x) / 8 for |x| < 0x1p-16373L, infinities (+-inf returning +-1 for erfl, 0/2 for erfcl), and NaNs via 1 / x.
    • core/libs/prx/libc/src/Math.cpp: Verified polynomial evaluations in ErflPolynomial across all branch thresholds (|x| < 0.84375, |x| < 1.25, ax < 2.85715, ax < 7, ax < 108, and ax >= 108) use correct Horner form and safe index bounds over ErflPp, ErflQq, ErflPa, ErflQa, ErflRa, ErflSa, ErflRb, ErflSb, ErflRc, and ErflSc.
    • core/libs/prx/libc/src/Math.cpp: Verified ErflTail exact float splitting z = static_cast(ax) ensures z * z has at most 48 mantissa bits, preventing catastrophic cancellation in ExplMsun(-z * z - 0.5625) * ExplMsun((z - ax) * (z + ax) + ratio).
    • core/libs/prx/libc/src/Math.cpp: Verified erfl_nid_postfix and erfcl_nid_postfix maintain FreeBSD msun errno behavior (leaving errno unchanged), execute at the caller's x87 precision, and adhere to APS5_VABI calling conventions.
    • core/libs/tests/GuestMath.cpp: Verified CheckLongDoubleErf test coverage over 49 erfl and 57 erfcl bitwise cases, exception status flags (FE_INEXACT, FE_UNDERFLOW), 53-bit precision via ExtendedBitsAt53Bits, and FE_UNDERFLOW preservation on subnormals.
    • docs/dev/debt/libc.md: Verified technical debt documentation correctly updates erfl and erfcl status from host libm delegation to FreeBSD release/11.4.0 msun s_erfl.c, documenting unverified console libc.prx behavior and caller x87 precision.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5912736416 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19342#pullrequestreview-5912736416). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-15T20:23:54Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `ac6b3c4b4b`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_math_tests` built cleanly. - **CTest** (`guest_math`, `guest_math_elementary`, `guest_math_exact`): ``` 1/3 Test #1120: guest_math_exact ................. Passed 0.03 sec 2/3 Test #1047: guest_math_elementary ............ Passed 0.04 sec 3/3 Test #1046: guest_math ....................... Passed 0.03 sec 100% tests passed, 0 tests failed out of 3 ``` - **Negative control**: with `core/libs/prx/libc/src/Math.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/3 Test #1047: guest_math_elementary ............ Passed 0.03 sec 2/3 Test #1120: guest_math_exact ................. Passed 0.06 sec 3/3 Test #1046: guest_math .......................Subprocess aborted***Exception: 0.48 sec 67% tests passed, 1 tests failed out of 3 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libc/src/Math.cpp: Verified ErflMsun and ErfclMsun handle all IEEE 754 special values including signed zeros (+-0.0), subnormals avoiding spurious FE_UNDERFLOW via (8 * x + ErflEfx8 * x) / 8 for |x| < 0x1p-16373L, infinities (+-inf returning +-1 for erfl, 0/2 for erfcl), and NaNs via 1 / x. - core/libs/prx/libc/src/Math.cpp: Verified polynomial evaluations in ErflPolynomial across all branch thresholds (|x| < 0.84375, |x| < 1.25, ax < 2.85715, ax < 7, ax < 108, and ax >= 108) use correct Horner form and safe index bounds over ErflPp, ErflQq, ErflPa, ErflQa, ErflRa, ErflSa, ErflRb, ErflSb, ErflRc, and ErflSc. - core/libs/prx/libc/src/Math.cpp: Verified ErflTail exact float splitting z = static_cast<float>(ax) ensures z * z has at most 48 mantissa bits, preventing catastrophic cancellation in ExplMsun(-z * z - 0.5625) * ExplMsun((z - ax) * (z + ax) + ratio). - core/libs/prx/libc/src/Math.cpp: Verified erfl_nid_postfix and erfcl_nid_postfix maintain FreeBSD msun errno behavior (leaving errno unchanged), execute at the caller's x87 precision, and adhere to APS5_VABI calling conventions. - core/libs/tests/GuestMath.cpp: Verified CheckLongDoubleErf test coverage over 49 erfl and 57 erfcl bitwise cases, exception status flags (FE_INEXACT, FE_UNDERFLOW), 53-bit precision via ExtendedBitsAt53Bits, and FE_UNDERFLOW preservation on subnormals. - docs/dev/debt/libc.md: Verified technical debt documentation correctly updates erfl and erfcl status from host libm delegation to FreeBSD release/11.4.0 msun s_erfl.c, documenting unverified console libc.prx behavior and caller x87 precision. - Note: the AI-assisted checkbox can stay. Looks Good To Me
PrivacyOnion commented 2026-11-15 20:23:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T20:23:55Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_math_tests built cleanly.
  • CTest (guest_math, guest_math_elementary, guest_math_exact):
    1/3 Test #1120: guest_math_exact .................   Passed    0.03 sec
    2/3 Test #1047: guest_math_elementary ............   Passed    0.04 sec
    3/3 Test #1046: guest_math .......................   Passed    0.03 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libc/src/Math.cpp reverted to main and the new tests kept, the suite fails:
    1/3 Test #1047: guest_math_elementary ............   Passed    0.03 sec
    2/3 Test #1120: guest_math_exact .................   Passed    0.06 sec
    3/3 Test #1046: guest_math .......................Subprocess aborted***Exception:   0.48 sec
    67% tests passed, 1 tests failed out of 3
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libc/src/Math.cpp: Verified ErflMsun and ErfclMsun handle all IEEE 754 special values including signed zeros (+-0.0), subnormals avoiding spurious FE_UNDERFLOW via (8 * x + ErflEfx8 * x) / 8 for |x| < 0x1p-16373L, infinities (+-inf returning +-1 for erfl, 0/2 for erfcl), and NaNs via 1 / x.
    • core/libs/prx/libc/src/Math.cpp: Verified polynomial evaluations in ErflPolynomial across all branch thresholds (|x| < 0.84375, |x| < 1.25, ax < 2.85715, ax < 7, ax < 108, and ax >= 108) use correct Horner form and safe index bounds over ErflPp, ErflQq, ErflPa, ErflQa, ErflRa, ErflSa, ErflRb, ErflSb, ErflRc, and ErflSc.
    • core/libs/prx/libc/src/Math.cpp: Verified ErflTail exact float splitting z = static_cast(ax) ensures z * z has at most 48 mantissa bits, preventing catastrophic cancellation in ExplMsun(-z * z - 0.5625) * ExplMsun((z - ax) * (z + ax) + ratio).
    • core/libs/prx/libc/src/Math.cpp: Verified erfl_nid_postfix and erfcl_nid_postfix maintain FreeBSD msun errno behavior (leaving errno unchanged), execute at the caller's x87 precision, and adhere to APS5_VABI calling conventions.
    • core/libs/tests/GuestMath.cpp: Verified CheckLongDoubleErf test coverage over 49 erfl and 57 erfcl bitwise cases, exception status flags (FE_INEXACT, FE_UNDERFLOW), 53-bit precision via ExtendedBitsAt53Bits, and FE_UNDERFLOW preservation on subnormals.
    • docs/dev/debt/libc.md: Verified technical debt documentation correctly updates erfl and erfcl status from host libm delegation to FreeBSD release/11.4.0 msun s_erfl.c, documenting unverified console libc.prx behavior and caller x87 precision.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5912736472 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19342#pullrequestreview-5912736472). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-15T20:23:55Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `ac6b3c4b4b`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_math_tests` built cleanly. - **CTest** (`guest_math`, `guest_math_elementary`, `guest_math_exact`): ``` 1/3 Test #1120: guest_math_exact ................. Passed 0.03 sec 2/3 Test #1047: guest_math_elementary ............ Passed 0.04 sec 3/3 Test #1046: guest_math ....................... Passed 0.03 sec 100% tests passed, 0 tests failed out of 3 ``` - **Negative control**: with `core/libs/prx/libc/src/Math.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/3 Test #1047: guest_math_elementary ............ Passed 0.03 sec 2/3 Test #1120: guest_math_exact ................. Passed 0.06 sec 3/3 Test #1046: guest_math .......................Subprocess aborted***Exception: 0.48 sec 67% tests passed, 1 tests failed out of 3 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libc/src/Math.cpp: Verified ErflMsun and ErfclMsun handle all IEEE 754 special values including signed zeros (+-0.0), subnormals avoiding spurious FE_UNDERFLOW via (8 * x + ErflEfx8 * x) / 8 for |x| < 0x1p-16373L, infinities (+-inf returning +-1 for erfl, 0/2 for erfcl), and NaNs via 1 / x. - core/libs/prx/libc/src/Math.cpp: Verified polynomial evaluations in ErflPolynomial across all branch thresholds (|x| < 0.84375, |x| < 1.25, ax < 2.85715, ax < 7, ax < 108, and ax >= 108) use correct Horner form and safe index bounds over ErflPp, ErflQq, ErflPa, ErflQa, ErflRa, ErflSa, ErflRb, ErflSb, ErflRc, and ErflSc. - core/libs/prx/libc/src/Math.cpp: Verified ErflTail exact float splitting z = static_cast<float>(ax) ensures z * z has at most 48 mantissa bits, preventing catastrophic cancellation in ExplMsun(-z * z - 0.5625) * ExplMsun((z - ax) * (z + ax) + ratio). - core/libs/prx/libc/src/Math.cpp: Verified erfl_nid_postfix and erfcl_nid_postfix maintain FreeBSD msun errno behavior (leaving errno unchanged), execute at the caller's x87 precision, and adhere to APS5_VABI calling conventions. - core/libs/tests/GuestMath.cpp: Verified CheckLongDoubleErf test coverage over 49 erfl and 57 erfcl bitwise cases, exception status flags (FE_INEXACT, FE_UNDERFLOW), 53-bit precision via ExtendedBitsAt53Bits, and FE_UNDERFLOW preservation on subnormals. - docs/dev/debt/libc.md: Verified technical debt documentation correctly updates erfl and erfcl status from host libm delegation to FreeBSD release/11.4.0 msun s_erfl.c, documenting unverified console libc.prx behavior and caller x87 precision. - 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!19342
No description provided.