feat(libc): implement _LExp #10780

Merged
00xDisaster merged 2 commits from upstream-pr/10780 into main 2026-11-13 23:54:04 +00:00
00xDisaster commented 2026-10-25 00:06:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 00xDisaster on 2026-10-25T00:06:02Z.


What

Add _LExp, the three-argument Dinkumware helper that computes y * exp(x) * 2^scale. The finite path uses the Dinkumware rational sequence, multiplies by y before the final scaling, and raises the libc error and floating-point exception state. The implementation follows the _LExp of firmware 13.42's system libSceLibcInternal.sprx as measured natively off-console by @BunnyFl:

  • When both operands are finite and nonzero, the range cutoffs are applied to x alone, before the offset is used. x > 29490 stores y * inf, returns Infinite, and raises overflow with ERANGE and FE_INEXACT. x < -29490 stores 0 * y, returns Zero, and raises no errno or flags. A 30000 operand with offset -32768 therefore still overflows, and a -60000 operand with offset 0 still returns zero.
  • A NaN multiplier is stored as NaN (kind 2, no errno, no flags). y == 0 stores y itself, preserving -0. x = +inf, y = 0 and x = -inf, y = +inf store NaN, set errno to EDOM, and raise FE_INVALID; the remaining infinity combinations follow the module's table.
  • A subnormal finite result returns Finite and preserves the incoming errno/flags except for raising FE_INEXACT. An underflow to zero stores +0 (even with a negative multiplier), returns Zero, and raises ERANGE with FE_UNDERFLOW|FE_INEXACT. Overflow always stores signed infinity, including in directed rounding modes.
  • The module reads the offset as a sign-extended 64-bit value, while the Dinkumware declaration uses short. The debt entry records that the published declaration is narrower than the module's read; signaling NaNs and a title's bundled libc.prx remain unverified.

The tests assert the measured operands from @BunnyFl's run, including the exact bits of _LExp(-0.5, 1, -16400) (0x0.0001368b2fc6f96p-16385, kind -1, errno == 0, FE_INEXACT only), the four rounding modes for the ±29490 cutoffs, the two -inf/+inf invalid cases, the +0 underflow sign with y = -1, and the x = -30000, x = -60000 and x = 30000 cases. The 401-point exp(i/16) sweep remains within the 1e-17 relative tolerance.

Tested

Linux CachyOS 7.3.0-4-cachyos, GCC 16.2.1. Base upstream/main 0836dfdf4fecb1e0326100c490476e15d3a74da5; head d0a02abde419c60a39b94c5134e29d309b9d5530.

  • cmake --build build --target guest_math_exact_tests guest_math_tests -j$(nproc): passed.
  • ctest --test-dir build -R 'guest_math(_exact)?$' --output-on-failure: 2/2 passed in 0.05 s.
  • The measured _LExp(-0.5, 1, -16400) subnormal matches @BunnyFl's module bits exactly. The measured rows for +inf, 0, 0; 1, -0, 0; 1, NaN, 0; +inf, 1, 0; -inf, 1, 0; -inf, +inf, 0; 30000, 1, -32768; -60000, 1, 0; -60000, +inf, 0; and -5, +inf, 0 in toward-zero mode all assert the stored value, kind, errno and flags from that run.
  • Cross-check: @BunnyFl ran the same 43 fixed operands plus a 401-point sweep through the module and reported 447/476 matches at the previous head; the 29 differences were the old ±50000 cutoffs, the LDBL_MAX-on-directed-overflow path, the restored flags on subnormal results, the NaN multiplier, and the -inf toward-zero case. Those paths are replaced by the module behavior above and now have direct assertions.
  • tools/check_conventions.py --base upstream/main --head HEAD and git diff --check upstream/main..HEAD: clean.
  • Windows 11 26300, WinLibs GCC 15.2.0, Release (maskym, independent run at an earlier head): build clean; guest_math_exact passed in a joint 160/160 run of the libc, CesCs, Font, FreeType, LibreSsl3 and NpUniversalDataSystem suites. Not run on a PS5.
  • GPUs tested: n/a.

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: #10569
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: no
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5000324290 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10780). Originally posted by [00xDisaster](https://github.com/00xDisaster) on 2026-10-25T00:06:02Z. --- ### What Add `_LExp`, the three-argument Dinkumware helper that computes `y * exp(x) * 2^scale`. The finite path uses the Dinkumware rational sequence, multiplies by `y` before the final scaling, and raises the libc error and floating-point exception state. The implementation follows the `_LExp` of firmware 13.42's system `libSceLibcInternal.sprx` as measured natively off-console by @BunnyFl: - When both operands are finite and nonzero, the range cutoffs are applied to `x` alone, before the offset is used. `x > 29490` stores `y * inf`, returns `Infinite`, and raises overflow with `ERANGE` and `FE_INEXACT`. `x < -29490` stores `0 * y`, returns `Zero`, and raises no `errno` or flags. A `30000` operand with offset `-32768` therefore still overflows, and a `-60000` operand with offset `0` still returns zero. - A NaN multiplier is stored as NaN (kind 2, no `errno`, no flags). `y == 0` stores `y` itself, preserving `-0`. `x = +inf, y = 0` and `x = -inf, y = +inf` store NaN, set `errno` to `EDOM`, and raise `FE_INVALID`; the remaining infinity combinations follow the module's table. - A subnormal finite result returns `Finite` and preserves the incoming `errno`/flags except for raising `FE_INEXACT`. An underflow to zero stores `+0` (even with a negative multiplier), returns `Zero`, and raises `ERANGE` with `FE_UNDERFLOW|FE_INEXACT`. Overflow always stores signed infinity, including in directed rounding modes. - The module reads the offset as a sign-extended 64-bit value, while the Dinkumware declaration uses `short`. The debt entry records that the published declaration is narrower than the module's read; signaling NaNs and a title's bundled `libc.prx` remain unverified. The tests assert the measured operands from @BunnyFl's run, including the exact bits of `_LExp(-0.5, 1, -16400)` (`0x0.0001368b2fc6f96p-16385`, kind -1, `errno == 0`, `FE_INEXACT` only), the four rounding modes for the `±29490` cutoffs, the two `-inf`/`+inf` invalid cases, the `+0` underflow sign with `y = -1`, and the `x = -30000`, `x = -60000` and `x = 30000` cases. The 401-point `exp(i/16)` sweep remains within the `1e-17` relative tolerance. ### Tested Linux CachyOS 7.3.0-4-cachyos, GCC 16.2.1. Base `upstream/main` `0836dfdf4fecb1e0326100c490476e15d3a74da5`; head `d0a02abde419c60a39b94c5134e29d309b9d5530`. - `cmake --build build --target guest_math_exact_tests guest_math_tests -j$(nproc)`: passed. - `ctest --test-dir build -R 'guest_math(_exact)?$' --output-on-failure`: 2/2 passed in 0.05 s. - The measured `_LExp(-0.5, 1, -16400)` subnormal matches @BunnyFl's module bits exactly. The measured rows for `+inf, 0, 0`; `1, -0, 0`; `1, NaN, 0`; `+inf, 1, 0`; `-inf, 1, 0`; `-inf, +inf, 0`; `30000, 1, -32768`; `-60000, 1, 0`; `-60000, +inf, 0`; and `-5, +inf, 0` in toward-zero mode all assert the stored value, kind, `errno` and flags from that run. - Cross-check: @BunnyFl ran the same 43 fixed operands plus a 401-point sweep through the module and reported 447/476 matches at the previous head; the 29 differences were the old ±50000 cutoffs, the `LDBL_MAX`-on-directed-overflow path, the restored flags on subnormal results, the NaN multiplier, and the `-inf` toward-zero case. Those paths are replaced by the module behavior above and now have direct assertions. - `tools/check_conventions.py --base upstream/main --head HEAD` and `git diff --check upstream/main..HEAD`: clean. - Windows 11 26300, WinLibs GCC 15.2.0, Release (maskym, independent run at an earlier head): build clean; `guest_math_exact` passed in a joint 160/160 run of the libc, CesCs, Font, FreeType, LibreSsl3 and NpUniversalDataSystem suites. Not run on a PS5. - GPUs tested: n/a. ### 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: #10569 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: no
MarcZX10 commented 2026-10-25 00:08:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:08:06Z.

Review: COMMENTED


The positive path has focused coverage and the patch is carefully documented. I found an ordering edge case in the new front-end special cases: y == 0 is handled before classifying NaNs. For x = NaN, y = 0, the current code returns zero, while the subsequent NaN branch suggests NaNs should propagate; please add an explicit test and establish the intended console behavior for this combination. Likewise, the x > 29490 fast path runs before checking y: with x = +infinity, y = 0, it raises overflow and returns infinity even though the zero multiplier branch otherwise returns zero. These special-value combinations need an explicit ordering backed by the target helper behavior, especially since the PR states exception behavior is unverified. I have not approved pending that clarification.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640708444 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10780#pullrequestreview-5640708444). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:08:06Z. **Review:** COMMENTED --- The positive path has focused coverage and the patch is carefully documented. I found an ordering edge case in the new front-end special cases: `y == 0` is handled before classifying NaNs. For `x = NaN, y = 0`, the current code returns zero, while the subsequent NaN branch suggests NaNs should propagate; please add an explicit test and establish the intended console behavior for this combination. Likewise, the `x > 29490` fast path runs before checking `y`: with `x = +infinity, y = 0`, it raises overflow and returns infinity even though the zero multiplier branch otherwise returns zero. These special-value combinations need an explicit ordering backed by the target helper behavior, especially since the PR states exception behavior is unverified. I have not approved pending that clarification.
MarcZX10 commented 2026-10-25 00:08:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:08:09Z.

Review: CHANGES_REQUESTED


The finite path has focused tests, and the PR documents what remains unverified. I found a special-value ordering issue that changes return values and exception state. The y == 0 shortcut precedes NaN classification, so x = NaN, y = 0 returns zero. The x > 29490 shortcut also precedes multiplier classification, so x = +infinity, y = 0 returns infinity and raises overflow even though the zero-multiplier shortcut otherwise returns zero. Please add tests for both combinations, plus x = NaN, y = 0 with alternate scale if relevant, and establish the intended Dinkumware/console semantics before choosing precedence. Because this helper explicitly updates errno and floating-point exceptions, those cases should assert both outputs and state. I did not run the full build locally; the PR reports its focused Linux suites and hosted checks are underway.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640708647 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10780#pullrequestreview-5640708647). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:08:09Z. **Review:** CHANGES_REQUESTED --- The finite path has focused tests, and the PR documents what remains unverified. I found a special-value ordering issue that changes return values and exception state. The `y == 0` shortcut precedes NaN classification, so `x = NaN, y = 0` returns zero. The `x > 29490` shortcut also precedes multiplier classification, so `x = +infinity, y = 0` returns infinity and raises overflow even though the zero-multiplier shortcut otherwise returns zero. Please add tests for both combinations, plus `x = NaN, y = 0` with alternate scale if relevant, and establish the intended Dinkumware/console semantics before choosing precedence. Because this helper explicitly updates errno and floating-point exceptions, those cases should assert both outputs and state. I did not run the full build locally; the PR reports its focused Linux suites and hosted checks are underway.
MarcZX10 commented 2026-10-25 00:27:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:27:17Z.

Review: CHANGES_REQUESTED


I compiled the exact MathExact.cpp from head 30ec7bea29e1349d9efa3f6297435f02436c86c9 with a small harness and confirmed an overflow-rounding mismatch: with FE_TOWARDZERO, _LExp(&value, 1.0L, 16384) stores +infinity and returns Infinite, while host scalbnl(1.0L, 16384) stores LDBL_MAX under the same mode. Both set FE_OVERFLOW|FE_INEXACT; the _LExp result is observably different. The implementation handles the subnormal rounding direction but the exponent-overflow branch always constructs infinity. Please make overflow honor the active rounding direction (including sign) and add tests for positive and negative overflow under FE_TOWARDZERO/directed rounding, asserting both stored value and return classification. I did not run the full project test suite; this was an isolated compile/run of the PR translation unit and harness.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640759209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10780#pullrequestreview-5640759209). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:27:17Z. **Review:** CHANGES_REQUESTED --- I compiled the exact `MathExact.cpp` from head `30ec7bea29e1349d9efa3f6297435f02436c86c9` with a small harness and confirmed an overflow-rounding mismatch: with `FE_TOWARDZERO`, `_LExp(&value, 1.0L, 16384)` stores +infinity and returns `Infinite`, while host `scalbnl(1.0L, 16384)` stores `LDBL_MAX` under the same mode. Both set `FE_OVERFLOW|FE_INEXACT`; the `_LExp` result is observably different. The implementation handles the subnormal rounding direction but the exponent-overflow branch always constructs infinity. Please make overflow honor the active rounding direction (including sign) and add tests for positive and negative overflow under `FE_TOWARDZERO`/directed rounding, asserting both stored value and return classification. I did not run the full project test suite; this was an isolated compile/run of the PR translation unit and harness.
MarcZX10 commented 2026-10-25 01:12:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T01:12:24Z.

Review: CHANGES_REQUESTED


Thanks for addressing the two special-value cases and overflow rounding. A further directed-rounding case remains in ScaleExtended: when rightShift >= 65, it unconditionally stores zero. For a positive nonzero exact value below half the least subnormal, FE_UPWARD must round to denorm_min(); for a negative value, FE_DOWNWARD must round to -denorm_min(). I confirmed the host scalbnl(1.0L, -20000) behavior with -frounding-math: nearest/toward-zero/downward produce zero, while upward produces 0x0.000000000000001p-16385 (the 80-bit extended minimum subnormal). The current early return skips the mode/sign check, so _LExp loses that directed rounding result even though the PR otherwise handles rounding modes explicitly. Please preserve the sign and return the least subnormal in the directed-away-from-zero cases, then add positive and negative tests for both sufficiently tiny and just-below-half-minsub inputs, including return classification, errno, and exception flags. I only ran this focused host scalbnl control; I have not run the updated project target locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640893968 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10780#pullrequestreview-5640893968). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T01:12:24Z. **Review:** CHANGES_REQUESTED --- Thanks for addressing the two special-value cases and overflow rounding. A further directed-rounding case remains in `ScaleExtended`: when `rightShift >= 65`, it unconditionally stores zero. For a positive nonzero exact value below half the least subnormal, `FE_UPWARD` must round to `denorm_min()`; for a negative value, `FE_DOWNWARD` must round to `-denorm_min()`. I confirmed the host `scalbnl(1.0L, -20000)` behavior with `-frounding-math`: nearest/toward-zero/downward produce zero, while upward produces `0x0.000000000000001p-16385` (the 80-bit extended minimum subnormal). The current early return skips the mode/sign check, so `_LExp` loses that directed rounding result even though the PR otherwise handles rounding modes explicitly. Please preserve the sign and return the least subnormal in the directed-away-from-zero cases, then add positive and negative tests for both sufficiently tiny and just-below-half-minsub inputs, including return classification, `errno`, and exception flags. I only ran this focused host `scalbnl` control; I have not run the updated project target locally.
MarcZX10 commented 2026-10-25 02:45:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T02:45:43Z.

Review: COMMENTED


The current head addresses the earlier special-value ordering, directed overflow, and tiny directed-rounding findings. Focused host tests reported by the author pass, and my isolated exact-head compile/run confirmed the large-magnitude branches behave as described; I left one non-blocking follow-up question about signed zero in the negative-exponent early-underflow path. I am leaving a comment rather than approving while console-specific edge semantics remain unverified.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641221877 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10780#pullrequestreview-5641221877). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:45:43Z. **Review:** COMMENTED --- The current head addresses the earlier special-value ordering, directed overflow, and tiny directed-rounding findings. Focused host tests reported by the author pass, and my isolated exact-head compile/run confirmed the large-magnitude branches behave as described; I left one non-blocking follow-up question about signed zero in the negative-exponent early-underflow path. I am leaving a comment rather than approving while console-specific edge semantics remain unverified.
MarcZX10 commented 2026-10-25 03:05:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T03:05:28Z.

Review: CHANGES_REQUESTED


The current head addresses the special-value ordering, directed overflow and directed tiny-result issues from my earlier reviews. My exact-head host harness confirmed the large-exponent cases; the author reports guest_math_exact and guest_math pass, and hosted conventions, eligibility, labels, relinker, sanitizer, register-context and write-watch checks pass (product Linux/Windows jobs are skipped). A remaining repository-rule blocker is the overlap report: open #8397 changes the same core/libs/prx/libc/include/Api.hpp lines, and open #10569 changes the same docs/dev/debt/libc.md lines. All three current checklists say Depends on: none. CONTRIBUTING.md requires the authors to agree on merge order and record it with Depends on. Please coordinate with #8397 and #10569 and update the affected checklists. I will update #10569 after we agree; I have not modified your branch.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641277245 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10780#pullrequestreview-5641277245). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T03:05:28Z. **Review:** CHANGES_REQUESTED --- The current head addresses the special-value ordering, directed overflow and directed tiny-result issues from my earlier reviews. My exact-head host harness confirmed the large-exponent cases; the author reports `guest_math_exact` and `guest_math` pass, and hosted conventions, eligibility, labels, relinker, sanitizer, register-context and write-watch checks pass (product Linux/Windows jobs are skipped). A remaining repository-rule blocker is the overlap report: open #8397 changes the same `core/libs/prx/libc/include/Api.hpp` lines, and open #10569 changes the same `docs/dev/debt/libc.md` lines. All three current checklists say `Depends on: none`. CONTRIBUTING.md requires the authors to agree on merge order and record it with `Depends on`. Please coordinate with #8397 and #10569 and update the affected checklists. I will update #10569 after we agree; I have not modified your branch.
MarcZX10 commented 2026-10-28 22:09:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T22:09:54Z.

Review: APPROVED


Reviewing exact head 70a0b39dcef4a4c3786982841b965ccbbe50a46c.

What I ran

  • Inspected all nine commits, the final implementation and tests, the previous correctness findings, and hosted checks.
  • Current main has not changed MathExact.cpp, GuestMathExact.cpp, or the libc debt file since the PR's tested main snapshot ca0e9557aa.
  • Hosted Windows, macOS/Linux register-context, relinker, ASan, write-watch, conventions, eligibility, labels, and report checks pass. The product Linux build is skipped.
  • I did not run a local build or tests.

Results

Check Result Evidence
Finite, special-value, overflow, underflow and rounding paths Reviewed Exact nine-commit diff and regression assertions
Linux guest_math_exact and guest_math Pass, author-reported 2/2 on the tested head
Windows math/libc suite Pass, independent reviewer-reported guest_math_exact included in 160/160
Hosted Windows and supporting platform/policy checks Pass GitHub Actions
Hosted Linux product build Skipped GitHub Actions
Local build and tests Not run This review

Findings

Blocking

None.

Non-blocking

  • The earlier findings have concrete fixes and regression tests: NaN precedence before the zero shortcut; directed overflow; preserved finite results when exponent and multiplier scales compensate; directed subnormal/underflow behavior; and overflow independent of a tiny multiplier. The last Linux build portability issue was fixed by using the long double overload of std::frexp.
  • The branch records Depends on: #10569, #11203; both remain open. Preserve the adjacent debt entry and shared math-test helper when resolving those textual overlaps. This approval is for the reviewed implementation, not a request to merge ahead of those dependencies.
  • Console-specific result bits, thresholds and exception behavior remain explicitly unverified in the debt note. The PR tests the documented Dinkumware contract and host behavior, not console equivalence.

Sources

Not verified

  • I did not run a local build, CTest, or a PS5 console comparison.
  • Linux and Windows focused test results are reported by the author and an independent reviewer; the hosted product Linux job is skipped.

To get this to a good state

No blocking issue remains on this head. Land after #10569 and #11203, preserving their shared debt and test changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5690357679 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10780#pullrequestreview-5690357679). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T22:09:54Z. **Review:** APPROVED --- Reviewing exact head 70a0b39dcef4a4c3786982841b965ccbbe50a46c. ## What I ran - Inspected all nine commits, the final implementation and tests, the previous correctness findings, and hosted checks. - Current main has not changed MathExact.cpp, GuestMathExact.cpp, or the libc debt file since the PR's tested main snapshot ca0e9557aa. - Hosted Windows, macOS/Linux register-context, relinker, ASan, write-watch, conventions, eligibility, labels, and report checks pass. The product Linux build is skipped. - I did not run a local build or tests. ## Results | Check | Result | Evidence | |---|---|---| | Finite, special-value, overflow, underflow and rounding paths | Reviewed | Exact nine-commit diff and regression assertions | | Linux guest_math_exact and guest_math | Pass, author-reported | 2/2 on the tested head | | Windows math/libc suite | Pass, independent reviewer-reported | guest_math_exact included in 160/160 | | Hosted Windows and supporting platform/policy checks | Pass | GitHub Actions | | Hosted Linux product build | Skipped | GitHub Actions | | Local build and tests | Not run | This review | ## Findings ### Blocking None. ### Non-blocking - The earlier findings have concrete fixes and regression tests: NaN precedence before the zero shortcut; directed overflow; preserved finite results when exponent and multiplier scales compensate; directed subnormal/underflow behavior; and overflow independent of a tiny multiplier. The last Linux build portability issue was fixed by using the long double overload of std::frexp. - The branch records Depends on: #10569, #11203; both remain open. Preserve the adjacent debt entry and shared math-test helper when resolving those textual overlaps. This approval is for the reviewed implementation, not a request to merge ahead of those dependencies. - Console-specific result bits, thresholds and exception behavior remain explicitly unverified in the debt note. The PR tests the documented Dinkumware contract and host behavior, not console equivalence. ## Sources - [Repository contribution and test rules](https://github.com/boykopovar/AnyPS5/blob/main/AGENTS.md) - [Current pull request and follow-up test evidence](https://github.com/boykopovar/AnyPS5/pull/10780) ## Not verified - I did not run a local build, CTest, or a PS5 console comparison. - Linux and Windows focused test results are reported by the author and an independent reviewer; the hosted product Linux job is skipped. ## To get this to a good state No blocking issue remains on this head. Land after #10569 and #11203, preserving their shared debt and test changes.
XairRrR commented 2026-10-29 11:38:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T11:38:23Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

The finite path builds and the focused tests pass, but _LExp's special-value and range handling contradicts the console-measured behaviour of its double/float siblings _Exp and _FExp already on main, and the new tests pin those differences.

What I ran

  • PR head: 1147f76d012a7aa0133c49a88fc5440a5e77f92c
  • main: ba5b5330b37724bca4b99f30faab504653635a04
  • Host: Arch Linux 7.2.11-zen, GCC 16.2.1, Intel Core i9-14900KF, Release build with Ninja and BUILD_TESTING=ON, in a clean worktree. Build and test commands ran in a sandbox with no network access.
git fetch -q origin main pull/10780/head:refs/review/10780
git checkout -q --detach origin/main
git merge -q --no-ff --no-edit refs/review/10780        # clean
git diff --check origin/main...refs/review/10780
python3 tools/check_conventions.py --base origin/main
cmake --build build -j10 --target guest_math_exact_tests guest_math_tests
cmake --build build -j10 --target libs
ctest --test-dir build -R '^guest_(math_exact|math)$' --output-on-failure
# my own probes, linked against the built libc.prx:
#  - _Exp (main) vs _LExp (this PR) on the same special operands
#  - _LExp(x, 1, 0) vs libquadmath expq(x) in ulps, 2,000,000 random x per range
# negative controls: see below

Results

Check Result
Merge into main Clean
git diff --check Clean
tools/check_conventions.py All conventions checks passed.
Build guest_math_exact_tests, guest_math_tests, libs OK
guest_math, guest_math_exact 2/2 passed
_LExp vs _Exp on special operands Differ in all 9 cases below
_LExp(x, 1, 0) accuracy vs expq Up to 2.1 ulp; about 7 % of results more than 1 ulp off
Same operands through _Exp (main) and _LExp (this PR), round to nearest
x=+inf y=0
  _Exp   -> nan                        kind= 2 errno=33 ovf=0 unf=0 inv=1
  _LExp  -> 0x0p+0                     kind= 0 errno=0  ovf=0 unf=0 inv=0
x=1 y=-0
  _Exp   -> -0x0p+0                    kind= 0 errno=0  ovf=0 unf=0 inv=0
  _LExp  -> 0x0p+0                     kind= 0 errno=0  ovf=0 unf=0 inv=0
x=1 y=NaN
  _Exp   -> 0x8p-3                     kind= 2 errno=0  ovf=0 unf=0 inv=0
  _LExp  -> nan                        kind= 2 errno=0  ovf=0 unf=0 inv=0
x=+inf y=1
  _Exp   -> inf                        kind= 1 errno=0  ovf=0 unf=0 inv=0
  _LExp  -> inf                        kind= 1 errno=34 ovf=1 unf=0 inv=0
x=-inf y=1
  _Exp   -> 0x0p+0                     kind= 0 errno=0  ovf=0 unf=0 inv=0
  _LExp  -> 0x0p+0                     kind= 0 errno=34 ovf=0 unf=1 inv=0
x=-inf y=inf
  _Exp   -> nan                        kind= 2 errno=33 ovf=0 unf=0 inv=1
  _LExp  -> 0x0p+0                     kind= 0 errno=34 ovf=0 unf=1 inv=0
x=-0.5 y=1, subnormal result (offset -1060 for _Exp, -16400 for _LExp)
  _Exp   -> 0x9.b44p-1064              kind=-1 errno=0  ovf=0 unf=0 inv=0
  _LExp  -> 0x0.0001368b2fc6f96p-16385 kind=-2 errno=34 ovf=0 unf=1 inv=0
x past the cutoff, offset compensates
  _Exp(1843, 1, -2000)       -> inf                          kind=1  errno=34 ovf=1
  _LExp(30000, 1, -32768)    -> 0xe.6ea97839ad67302p+10509   kind=-1
x below the cutoff, offset 0
  _Exp(-1900, 1, 0)          -> 0x0p+0  kind=0 errno=0  unf=0
  _LExp(-60000, 1, 0)        -> 0x0p+0  kind=0 errno=34 unf=1

Infinite multiplier beyond |x| > 50000 (_LExp only):

x=-60000 y=inf s=0                 -> 0x0p+0                    kind=0  errno=34 unf=1
x=-100   y=inf s=0                 -> inf                       kind=1  errno=0
x=60000  y=inf s=0 towardzero      -> 0xf.fffffffffffffffp+16380 kind=-1 errno=34 ovf=1
x=-5     y=inf s=0 towardzero      -> inf                       kind=1  errno=0
Accuracy of _LExp(x, 1, 0) against libquadmath expq
ulp err at x=1: 0.313, nextup(1): 0.046, nextdown(1): 0.007
|x|<=1: max 2.113 ulp at x=-0xf.a85cdd426591c2ap-4; >0.5ulp 764962/2000000; >1ulp 142649/2000000
|x|<=10: max 2.075 ulp at x=0xf.a28ee4857d6688cp-2; >0.5ulp 763896/2000000; >1ulp 142824/2000000
|x|<=11000: max 2.104 ulp at x=-0xc.ea1929226bec10cp+9; >0.5ulp 764953/2000000; >1ulp 142671/2000000

Negative control

  1. I restored MathExact.cpp and Api.hpp from main. guest_math_exact_tests no longer compiles because _LExp_nid_postfix is not declared. This is expected for a new export, and matches the Windows control already posted in this thread.
  2. I removed only the x == 1.0L branch (MathExact.cpp:175-180). guest_math_exact then aborts at GuestMathExact.cpp:216, the exact-bits check in CheckLongDoubleExpCompensatingRange. So the _LExp(1, LDBL_MAX, -2) vector passes only because of that branch (see the second non-blocking finding).

After both controls I restored the merged files, rebuilt, and both tests passed again.

Findings

Blocking

  1. Special operands and range cutoffs contradict the measured _Exp/_FExp on main.

    _Exp and _FExp are the double and float forms of the same Dinkumware helper. Commits 35c38ce8d5 and 79d65956a1 changed them on main while this PR was in review. Their debt entry (docs/dev/debt/libc.md:215) records what PRAGMATA PPSA02530's bundled libc.prx does:

    • the x-only cutoffs ignore the offset: below the cutoff the result is 0.0*y, above it y*inf with overflow;
    • a zero multiplier keeps its sign;
    • a NaN multiplier leaves *px unchanged;
    • +inf with zero, and -inf with infinity, are invalid.

    The code is at DinkumwareInternals.cpp:219-249 and :265-273. Subnormal results there return Finite (-1) and restore the entry flags and errno.

    _LExp does something different in each of these cases (probe output above):

    • Zero multiplier: y == -0 stores +0 (MathExact.cpp:412-415). This is also wrong for the stated contract y * exp(x) * 2^scale.
    • NaN multiplier: it overwrites *value with the NaN (:408-411).
    • x = +inf, y = 0: returns 0 with no exception. x = -inf, y = inf: returns 0 with underflow. _Exp raises FE_INVALID/EDOM and returns NaN for both.
    • x = ±inf with a finite multiplier: errno = ERANGE plus FE_OVERFLOW or FE_UNDERFLOW. _Exp returns the exact infinity or zero without either (:160-167).
    • Subnormal results: return Denormal (-2) with ERANGE and FE_UNDERFLOW (:138-143). This includes exact ones such as _LExp(0, denorm_min, 0).
    • Cutoffs: there is no x-only cutoff, so the offset compensates. _LExp(30000, 1, -32768) is finite, while _Exp(1843, 1, -2000) overflows. Below the cutoff the result underflows with ERANGE instead of returning 0.0*y silently.
    • Infinite multiplier: it is checked after the ±50000 cutoffs (:168-174). So _LExp(-60000, inf, 0) returns 0 while _LExp(-100, inf, 0) returns inf. Under FE_TOWARDZERO, _LExp(60000, inf, 0) returns LDBL_MAX while _LExp(-5, inf, 0) returns inf. _Exp returns y for any infinite multiplier with finite x.

    Several of these are asserted as intended behaviour, for example GuestMathExact.cpp:349 (+inf, 0 returns zero), :355 (+inf, 1 sets ERANGE/FE_OVERFLOW) and :363 (a NaN multiplier is stored).

    I have not measured the console's _LExp, so I can't say which _LExp result is right in every case. The libc.prx that served as the oracle for _Exp/_FExp should also export _LExp, though, and the measured evidence for the family points away from several of the policies chosen here. Many of those policies were added in answer to earlier review requests (directed overflow, compensating offsets). Those requests were about host scalbnl/mathematical semantics, not the console's helper.

    Suggested fix: measure _LExp from the same libc.prx the way the _Exp entry was measured, and follow it. Where that is not possible yet, follow the measured _Exp structure: special operands first, *value = y for zero and infinite multipliers, x-only cutoffs, no range state for subnormal or infinite-operand results. Any long-double-specific choice that stays unknown, such as the cutoff value, should throw or be recorded in the debt entry rather than pinned by tests. At minimum, fix the signed zero and the infinite-multiplier ordering, which are wrong under any reading of y * exp(x) * 2^scale.

Non-blocking

  1. Accuracy and test tolerance. Against a binary128 reference, _LExp(x, 1, 0) is off by up to 2.1 ulp, and about 7 % of results are off by more than 1 ulp (details above). That may well be the console's own error if these are the titles' coefficients, but the PR doesn't say where p0..q1, ln2High/ln2Low and the 0x1p-66 shortcut come from, and the debt entry says nothing about accuracy. Meanwhile, the sweep in GuestMathExact.cpp:331-336 allows a relative error of 1e-17, about 90 ulp for an 80-bit long double, so it would not catch a coefficient regression. Suggestion: name the source of the constants in the debt entry, as the _Exp entry does, and tighten the tolerance to a few ulp or assert exact bits for a handful of points.
  2. The x == 1.0L shortcut is broader than its precedent. MathExact.cpp:175-180 replaces the general path for every multiplier and scale whenever x == 1. _Exp takes its E shortcut only for offset == 0 && x == 1 && y == 1. As negative control 2 shows, the branch exists to make the _LExp(1, LDBL_MAX, -2) vector bit-exact, so it is fitted to a review test case rather than measured behaviour. Either match the measured condition, with evidence, or drop it and assert that vector to a stated ulp bound.
  3. Parameter name. Api.hpp declares the last parameter as offset, like _Exp/_FExp, but the definition names it scale. Using offset in both would keep the family consistent.

Not verified

  • No console or libc.prx measurement of _LExp. The comparison above is against _Exp/_FExp as implemented on main and their measured debt entry, not against _LExp on hardware.
  • Windows was not run here; other reviewers in this thread posted Windows results for earlier heads.
  • No AGC, shader or VideoOut code is touched, so the Vulkan validation steps do not apply.

To get this to a good state

  1. Bring _LExp's special operands, cutoffs, return codes and errno/fenv behaviour in line with the measured _Exp/_FExp, or measure _LExp itself from the same libc.prx. Update the tests and the debt entry to match.
  2. In any case, keep the sign of a zero multiplier and handle an infinite multiplier before the cutoffs, and add tests for both.
  3. Optionally: name the source of the coefficients, tighten the sweep tolerance, and narrow or justify the x == 1 shortcut.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5697052407 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10780#pullrequestreview-5697052407). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-29T11:38:23Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested The finite path builds and the focused tests pass, but `_LExp`'s special-value and range handling contradicts the console-measured behaviour of its `double`/`float` siblings `_Exp` and `_FExp` already on `main`, and the new tests pin those differences. ## What I ran - PR head: `1147f76d012a7aa0133c49a88fc5440a5e77f92c` - `main`: `ba5b5330b37724bca4b99f30faab504653635a04` - Host: Arch Linux 7.2.11-zen, GCC 16.2.1, Intel Core i9-14900KF, Release build with Ninja and `BUILD_TESTING=ON`, in a clean worktree. Build and test commands ran in a sandbox with no network access. ```sh git fetch -q origin main pull/10780/head:refs/review/10780 git checkout -q --detach origin/main git merge -q --no-ff --no-edit refs/review/10780 # clean git diff --check origin/main...refs/review/10780 python3 tools/check_conventions.py --base origin/main cmake --build build -j10 --target guest_math_exact_tests guest_math_tests cmake --build build -j10 --target libs ctest --test-dir build -R '^guest_(math_exact|math)$' --output-on-failure # my own probes, linked against the built libc.prx: # - _Exp (main) vs _LExp (this PR) on the same special operands # - _LExp(x, 1, 0) vs libquadmath expq(x) in ulps, 2,000,000 random x per range # negative controls: see below ``` ## Results | Check | Result | |---|---| | Merge into `main` | Clean | | `git diff --check` | Clean | | `tools/check_conventions.py` | `All conventions checks passed.` | | Build `guest_math_exact_tests`, `guest_math_tests`, `libs` | OK | | `guest_math`, `guest_math_exact` | 2/2 passed | | `_LExp` vs `_Exp` on special operands | Differ in all 9 cases below | | `_LExp(x, 1, 0)` accuracy vs `expq` | Up to 2.1 ulp; about 7 % of results more than 1 ulp off | <details><summary>Same operands through <code>_Exp</code> (main) and <code>_LExp</code> (this PR), round to nearest</summary> ```text x=+inf y=0 _Exp -> nan kind= 2 errno=33 ovf=0 unf=0 inv=1 _LExp -> 0x0p+0 kind= 0 errno=0 ovf=0 unf=0 inv=0 x=1 y=-0 _Exp -> -0x0p+0 kind= 0 errno=0 ovf=0 unf=0 inv=0 _LExp -> 0x0p+0 kind= 0 errno=0 ovf=0 unf=0 inv=0 x=1 y=NaN _Exp -> 0x8p-3 kind= 2 errno=0 ovf=0 unf=0 inv=0 _LExp -> nan kind= 2 errno=0 ovf=0 unf=0 inv=0 x=+inf y=1 _Exp -> inf kind= 1 errno=0 ovf=0 unf=0 inv=0 _LExp -> inf kind= 1 errno=34 ovf=1 unf=0 inv=0 x=-inf y=1 _Exp -> 0x0p+0 kind= 0 errno=0 ovf=0 unf=0 inv=0 _LExp -> 0x0p+0 kind= 0 errno=34 ovf=0 unf=1 inv=0 x=-inf y=inf _Exp -> nan kind= 2 errno=33 ovf=0 unf=0 inv=1 _LExp -> 0x0p+0 kind= 0 errno=34 ovf=0 unf=1 inv=0 x=-0.5 y=1, subnormal result (offset -1060 for _Exp, -16400 for _LExp) _Exp -> 0x9.b44p-1064 kind=-1 errno=0 ovf=0 unf=0 inv=0 _LExp -> 0x0.0001368b2fc6f96p-16385 kind=-2 errno=34 ovf=0 unf=1 inv=0 x past the cutoff, offset compensates _Exp(1843, 1, -2000) -> inf kind=1 errno=34 ovf=1 _LExp(30000, 1, -32768) -> 0xe.6ea97839ad67302p+10509 kind=-1 x below the cutoff, offset 0 _Exp(-1900, 1, 0) -> 0x0p+0 kind=0 errno=0 unf=0 _LExp(-60000, 1, 0) -> 0x0p+0 kind=0 errno=34 unf=1 ``` Infinite multiplier beyond `|x| > 50000` (`_LExp` only): ```text x=-60000 y=inf s=0 -> 0x0p+0 kind=0 errno=34 unf=1 x=-100 y=inf s=0 -> inf kind=1 errno=0 x=60000 y=inf s=0 towardzero -> 0xf.fffffffffffffffp+16380 kind=-1 errno=34 ovf=1 x=-5 y=inf s=0 towardzero -> inf kind=1 errno=0 ``` </details> <details><summary>Accuracy of <code>_LExp(x, 1, 0)</code> against libquadmath <code>expq</code></summary> ```text ulp err at x=1: 0.313, nextup(1): 0.046, nextdown(1): 0.007 |x|<=1: max 2.113 ulp at x=-0xf.a85cdd426591c2ap-4; >0.5ulp 764962/2000000; >1ulp 142649/2000000 |x|<=10: max 2.075 ulp at x=0xf.a28ee4857d6688cp-2; >0.5ulp 763896/2000000; >1ulp 142824/2000000 |x|<=11000: max 2.104 ulp at x=-0xc.ea1929226bec10cp+9; >0.5ulp 764953/2000000; >1ulp 142671/2000000 ``` </details> ## Negative control 1. I restored `MathExact.cpp` and `Api.hpp` from `main`. `guest_math_exact_tests` no longer compiles because `_LExp_nid_postfix` is not declared. This is expected for a new export, and matches the Windows control already posted in this thread. 2. I removed only the `x == 1.0L` branch ([MathExact.cpp:175-180](https://github.com/boykopovar/AnyPS5/blob/1147f76d012a7aa0133c49a88fc5440a5e77f92c/core/libs/prx/libc/src/MathExact.cpp#L175-L180)). `guest_math_exact` then aborts at `GuestMathExact.cpp:216`, the exact-bits check in `CheckLongDoubleExpCompensatingRange`. So the `_LExp(1, LDBL_MAX, -2)` vector passes only because of that branch (see the second non-blocking finding). After both controls I restored the merged files, rebuilt, and both tests passed again. ## Findings ### Blocking 1. **Special operands and range cutoffs contradict the measured `_Exp`/`_FExp` on `main`.** `_Exp` and `_FExp` are the `double` and `float` forms of the same Dinkumware helper. Commits 35c38ce8d5 and 79d65956a1 changed them on `main` while this PR was in review. Their debt entry ([docs/dev/debt/libc.md:215](https://github.com/boykopovar/AnyPS5/blob/ba5b5330b37724bca4b99f30faab504653635a04/docs/dev/debt/libc.md#L215)) records what PRAGMATA PPSA02530's bundled `libc.prx` does: - the x-only cutoffs ignore the offset: below the cutoff the result is `0.0*y`, above it `y*inf` with overflow; - a zero multiplier keeps its sign; - a NaN multiplier leaves `*px` unchanged; - `+inf` with zero, and `-inf` with infinity, are invalid. The code is at [DinkumwareInternals.cpp:219-249](https://github.com/boykopovar/AnyPS5/blob/ba5b5330b37724bca4b99f30faab504653635a04/core/libs/prx/libc/src/DinkumwareInternals.cpp#L219-L249) and [:265-273](https://github.com/boykopovar/AnyPS5/blob/ba5b5330b37724bca4b99f30faab504653635a04/core/libs/prx/libc/src/DinkumwareInternals.cpp#L265-L273). Subnormal results there return `Finite` (-1) and restore the entry flags and `errno`. `_LExp` does something different in each of these cases (probe output above): - **Zero multiplier:** `y == -0` stores `+0` ([MathExact.cpp:412-415](https://github.com/boykopovar/AnyPS5/blob/1147f76d012a7aa0133c49a88fc5440a5e77f92c/core/libs/prx/libc/src/MathExact.cpp#L412-L415)). This is also wrong for the stated contract `y * exp(x) * 2^scale`. - **NaN multiplier:** it overwrites `*value` with the NaN ([:408-411](https://github.com/boykopovar/AnyPS5/blob/1147f76d012a7aa0133c49a88fc5440a5e77f92c/core/libs/prx/libc/src/MathExact.cpp#L408-L411)). - **`x = +inf, y = 0`:** returns 0 with no exception. **`x = -inf, y = inf`:** returns 0 with underflow. `_Exp` raises `FE_INVALID`/`EDOM` and returns NaN for both. - **`x = ±inf` with a finite multiplier:** `errno = ERANGE` plus `FE_OVERFLOW` or `FE_UNDERFLOW`. `_Exp` returns the exact infinity or zero without either ([:160-167](https://github.com/boykopovar/AnyPS5/blob/1147f76d012a7aa0133c49a88fc5440a5e77f92c/core/libs/prx/libc/src/MathExact.cpp#L160-L167)). - **Subnormal results:** return `Denormal` (-2) with `ERANGE` and `FE_UNDERFLOW` ([:138-143](https://github.com/boykopovar/AnyPS5/blob/1147f76d012a7aa0133c49a88fc5440a5e77f92c/core/libs/prx/libc/src/MathExact.cpp#L138-L143)). This includes exact ones such as `_LExp(0, denorm_min, 0)`. - **Cutoffs:** there is no x-only cutoff, so the offset compensates. `_LExp(30000, 1, -32768)` is finite, while `_Exp(1843, 1, -2000)` overflows. Below the cutoff the result underflows with `ERANGE` instead of returning `0.0*y` silently. - **Infinite multiplier:** it is checked after the ±50000 cutoffs ([:168-174](https://github.com/boykopovar/AnyPS5/blob/1147f76d012a7aa0133c49a88fc5440a5e77f92c/core/libs/prx/libc/src/MathExact.cpp#L168-L174)). So `_LExp(-60000, inf, 0)` returns 0 while `_LExp(-100, inf, 0)` returns inf. Under `FE_TOWARDZERO`, `_LExp(60000, inf, 0)` returns `LDBL_MAX` while `_LExp(-5, inf, 0)` returns inf. `_Exp` returns `y` for any infinite multiplier with finite `x`. Several of these are asserted as intended behaviour, for example [GuestMathExact.cpp:349](https://github.com/boykopovar/AnyPS5/blob/1147f76d012a7aa0133c49a88fc5440a5e77f92c/core/libs/tests/GuestMathExact.cpp#L349) (`+inf`, `0` returns zero), [:355](https://github.com/boykopovar/AnyPS5/blob/1147f76d012a7aa0133c49a88fc5440a5e77f92c/core/libs/tests/GuestMathExact.cpp#L355) (`+inf`, `1` sets `ERANGE`/`FE_OVERFLOW`) and [:363](https://github.com/boykopovar/AnyPS5/blob/1147f76d012a7aa0133c49a88fc5440a5e77f92c/core/libs/tests/GuestMathExact.cpp#L363) (a NaN multiplier is stored). I have not measured the console's `_LExp`, so I can't say which `_LExp` result is right in every case. The `libc.prx` that served as the oracle for `_Exp`/`_FExp` should also export `_LExp`, though, and the measured evidence for the family points away from several of the policies chosen here. Many of those policies were added in answer to earlier review requests (directed overflow, compensating offsets). Those requests were about host `scalbnl`/mathematical semantics, not the console's helper. Suggested fix: measure `_LExp` from the same `libc.prx` the way the `_Exp` entry was measured, and follow it. Where that is not possible yet, follow the measured `_Exp` structure: special operands first, `*value = y` for zero and infinite multipliers, x-only cutoffs, no range state for subnormal or infinite-operand results. Any long-double-specific choice that stays unknown, such as the cutoff value, should throw or be recorded in the debt entry rather than pinned by tests. At minimum, fix the signed zero and the infinite-multiplier ordering, which are wrong under any reading of `y * exp(x) * 2^scale`. ### Non-blocking 1. **Accuracy and test tolerance.** Against a binary128 reference, `_LExp(x, 1, 0)` is off by up to 2.1 ulp, and about 7 % of results are off by more than 1 ulp (details above). That may well be the console's own error if these are the titles' coefficients, but the PR doesn't say where `p0..q1`, `ln2High`/`ln2Low` and the `0x1p-66` shortcut come from, and the debt entry says nothing about accuracy. Meanwhile, the sweep in [GuestMathExact.cpp:331-336](https://github.com/boykopovar/AnyPS5/blob/1147f76d012a7aa0133c49a88fc5440a5e77f92c/core/libs/tests/GuestMathExact.cpp#L331-L336) allows a relative error of `1e-17`, about 90 ulp for an 80-bit long double, so it would not catch a coefficient regression. Suggestion: name the source of the constants in the debt entry, as the `_Exp` entry does, and tighten the tolerance to a few ulp or assert exact bits for a handful of points. 2. **The `x == 1.0L` shortcut is broader than its precedent.** [MathExact.cpp:175-180](https://github.com/boykopovar/AnyPS5/blob/1147f76d012a7aa0133c49a88fc5440a5e77f92c/core/libs/prx/libc/src/MathExact.cpp#L175-L180) replaces the general path for every multiplier and scale whenever `x == 1`. `_Exp` takes its `E` shortcut only for `offset == 0 && x == 1 && y == 1`. As negative control 2 shows, the branch exists to make the `_LExp(1, LDBL_MAX, -2)` vector bit-exact, so it is fitted to a review test case rather than measured behaviour. Either match the measured condition, with evidence, or drop it and assert that vector to a stated ulp bound. 3. **Parameter name.** `Api.hpp` declares the last parameter as `offset`, like `_Exp`/`_FExp`, but the definition names it `scale`. Using `offset` in both would keep the family consistent. ## Not verified - No console or `libc.prx` measurement of `_LExp`. The comparison above is against `_Exp`/`_FExp` as implemented on `main` and their measured debt entry, not against `_LExp` on hardware. - Windows was not run here; other reviewers in this thread posted Windows results for earlier heads. - No AGC, shader or VideoOut code is touched, so the Vulkan validation steps do not apply. ## To get this to a good state 1. Bring `_LExp`'s special operands, cutoffs, return codes and `errno`/fenv behaviour in line with the measured `_Exp`/`_FExp`, or measure `_LExp` itself from the same `libc.prx`. Update the tests and the debt entry to match. 2. In any case, keep the sign of a zero multiplier and handle an infinite multiplier before the cutoffs, and add tests for both. 3. Optionally: name the source of the coefficients, tighten the sweep tolerance, and narrow or justify the `x == 1` shortcut.
XairRrR commented 2026-11-02 20:32:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-02T20:32:48Z.

Review: APPROVED


Re-checked — approved

The nine earlier commits are replaced by one commit, d0a02abde4. It rebuilds _LExp around BunnyFl's native run of firmware 13.42's system libSceLibcInternal.sprx, which resolves my blocking finding. It builds, guest_math and guest_math_exact pass, and a mutation of the new cutoff makes guest_math_exact fail.

What changed since my last review

  • The PR is rebased onto a newer main, and its nine commits are replaced by one: d0a02abde4 fix(libc): match measured _LExp overflow and cutoff behavior.
  • Special operands are now handled before any cutoff:
    • A NaN multiplier is stored.
    • +inf with a zero multiplier, and -inf with an infinite one, give NaN with EDOM and FE_INVALID.
    • A zero or infinite multiplier is stored as it is, so -0 keeps its sign.
  • The cutoffs apply to x alone, at ±29490, before the offset is used:
    • above +29490: y*inf, Infinite, ERANGE, overflow;
    • below -29490: 0*y, Zero, no errno and no flags.
  • Overflow is always a signed infinity. Underflow to zero stores +0 with ERANGE. A subnormal result returns Finite, restores the entry state and raises only FE_INEXACT.
  • The x == 1 shortcut now applies only to y == 1, offset == 0. The parameter is named offset in both the declaration and the definition.
  • The debt entry now records the measurement and the 64-bit offset read.
  • In the discussion, BunnyFl posted the module measurement (2026-10-31), the author rebuilt the branch around it, and maskym reported a Windows pass at this head (author- and reviewer-reported, not re-run here).

What I ran

  • Head d0a02abde419c60a39b94c5134e29d309b9d5530, main 096c45570f516de76a43bbe1b97781381c84f55f, merged tree 97cef9af053238e3f053784369bc26fe6d1ab0f5.
  • Machine: Debian 13 VM, 32 vCPU Xeon Gold 6136, GCC 14.2.0, Release, Ninja.
git diff --check 096c45570f516de76a43bbe1b97781381c84f55f...d0a02abde419c60a39b94c5134e29d309b9d5530
python3 tools/check_conventions.py --base 096c45570f516de76a43bbe1b97781381c84f55f
ctest --test-dir build -R '^guest_math(_exact)?$' --output-on-failure
# mutation control: inputLimit 29490.0L -> 50000.0L in MathExact.cpp
ninja -C build -j32 -l32 guest_math_exact_tests libs
ctest --test-dir build -R '^guest_math_exact$' --output-on-failure
# restore MathExact.cpp from the merged tree, rebuild, rerun both tests
git status --porcelain
Check Result
git diff --check Clean
check_conventions.py All conventions checks passed.
Build (script) and libs OK
guest_math, guest_math_exact 2/2 passed
Selected tests (script) No new failures and no timeouts. The 9 known failures also fail on main.
Mutation: cutoff back to ±50000 guest_math_exact aborts
After restoring 2/2 passed, git status --porcelain empty

On the negative control: if the production files are restored from main, the test does not compile, because the export is new (the same result as my earlier control). The cutoff mutation shows that the new range assertions actually check the 29490 cutoff.

Previous findings

  • Blocking 1, special operands and cutoffs contradict the measured _Exp/_FExp: Resolved. The PR now follows a measurement of _LExp itself, which is what I asked for.
    • The code at MathExact.cpp:560-633 and the cutoffs at :208-213 match each row of BunnyFl's table.
    • The tests assert those rows, including the exact subnormal bits 0x0.0001368b2fc6f96p-16385 (GuestMathExact.cpp:204-208), the cutoff cases (:217-243) and the NaN, zero and infinity rows (:376-438). All of them pass here.
    • Signed zero and the order of infinite multipliers are fixed.
    • The module's results are as BunnyFl reported them; I did not re-run the module.
  • Non-blocking 1, accuracy and test tolerance: Still open, non-blocking.
    • The debt entry still does not name the source of p0..q1, ln2High/ln2Low and tiny.
    • The sweep at GuestMathExact.cpp:361-366 still allows 1e-17 relative error, about 90 ulp. BunnyFl reports the 401 points as within that tolerance, not as identical bits, so the sweep does not show bit equality with the module.
  • Non-blocking 2, x == 1 shortcut: Resolved. It is now limited to offset == 0 && x == 1 && y == 1 (MathExact.cpp:625-628), as in _Exp. The constant 0xa.df85458a2bb4a9bp-2L is e correctly rounded: the 64-bit mantissa 0xadf85458a2bb4a9a has a remainder of 0.687, so it rounds up.
  • Non-blocking 3, parameter name: Resolved. The parameter is offset in Api.hpp and in the definition.

New findings

Blocking

None.

Non-blocking

  1. Dead code left over from earlier versions.
    • OverflowToInfinity, OverflowScaled, FinishExpScale and UnderflowScaled (core/libs/prx/libc/src/MathExact.cpp:70-140) are never called.
    • The special-operand branches at the top of ExpScaled (:160-207: infinite x, infinite multiplier, x == 0, x == 1) cannot be reached, because _LExp_nid_postfix handles those cases before it calls ExpScaled.
    • The build here is quiet only because -Wunused-function is off.
    • Removing them leaves a single copy of the special-case logic.
  2. Underflow sign and rounding in ScaleExtended depend on the shift.
    • When rightShift >= 65, it stores +0 in every rounding mode (:105-108), as measured.
    • When rightShift is 64 or less, it keeps the sign (a negative product that rounds to zero stores -0) and rounds up to ±denorm_min in the directed modes (:110-127).
    • Neither the tests nor BunnyFl's run cover the second branch, so the two paths may disagree with the module near denorm_min.
    • The same applies to the x == 0, nonzero-offset path. ScaleExtended raises no flag, and :611-616 restore the entry flags, so a subnormal result that lost bits there raises no FE_INEXACT, unlike the ExpScaled path.
    • Suggested fix: either measure these cases, or record them in the debt entry.
  3. Duplicate include. core/libs/tests/GuestMathExact.cpp:11 adds #include <limits>, which line 13 already has.
  4. Debt entry order. docs/dev/debt/libc.md:162 opens with what matches. CONTRIBUTING asks entries to state the gap first: the unverified title libc.prx, console and signaling NaNs, the short/64-bit offset difference, and the unnamed coefficient source.

Not verified

  • I did not re-run the firmware module or a title's libc.prx, and there was no console run. The agreement with the module comes from BunnyFl's report, which the tests encode.
  • Windows was not run here. maskym reports a pass at this head.
  • Vulkan validation does not apply: no AGC, shader or VideoOut file is changed.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5742927916 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10780#pullrequestreview-5742927916). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-11-02T20:32:48Z. **Review:** APPROVED --- # Re-checked — approved The nine earlier commits are replaced by one commit, `d0a02abde4`. It rebuilds `_LExp` around BunnyFl's native run of firmware 13.42's system `libSceLibcInternal.sprx`, which resolves my blocking finding. It builds, `guest_math` and `guest_math_exact` pass, and a mutation of the new cutoff makes `guest_math_exact` fail. ## What changed since my last review - The PR is rebased onto a newer `main`, and its nine commits are replaced by one: `d0a02abde4 fix(libc): match measured _LExp overflow and cutoff behavior`. - Special operands are now handled before any cutoff: - A NaN multiplier is stored. - `+inf` with a zero multiplier, and `-inf` with an infinite one, give NaN with `EDOM` and `FE_INVALID`. - A zero or infinite multiplier is stored as it is, so `-0` keeps its sign. - The cutoffs apply to `x` alone, at ±29490, before the offset is used: - above +29490: `y*inf`, `Infinite`, `ERANGE`, overflow; - below -29490: `0*y`, `Zero`, no `errno` and no flags. - Overflow is always a signed infinity. Underflow to zero stores `+0` with `ERANGE`. A subnormal result returns `Finite`, restores the entry state and raises only `FE_INEXACT`. - The `x == 1` shortcut now applies only to `y == 1, offset == 0`. The parameter is named `offset` in both the declaration and the definition. - The debt entry now records the measurement and the 64-bit offset read. - In the discussion, BunnyFl posted the module measurement (2026-10-31), the author rebuilt the branch around it, and maskym reported a Windows pass at this head (author- and reviewer-reported, not re-run here). ## What I ran - Head `d0a02abde419c60a39b94c5134e29d309b9d5530`, `main` `096c45570f516de76a43bbe1b97781381c84f55f`, merged tree `97cef9af053238e3f053784369bc26fe6d1ab0f5`. - Machine: Debian 13 VM, 32 vCPU Xeon Gold 6136, GCC 14.2.0, Release, Ninja. ```sh git diff --check 096c45570f516de76a43bbe1b97781381c84f55f...d0a02abde419c60a39b94c5134e29d309b9d5530 python3 tools/check_conventions.py --base 096c45570f516de76a43bbe1b97781381c84f55f ctest --test-dir build -R '^guest_math(_exact)?$' --output-on-failure # mutation control: inputLimit 29490.0L -> 50000.0L in MathExact.cpp ninja -C build -j32 -l32 guest_math_exact_tests libs ctest --test-dir build -R '^guest_math_exact$' --output-on-failure # restore MathExact.cpp from the merged tree, rebuild, rerun both tests git status --porcelain ``` | Check | Result | |---|---| | `git diff --check` | Clean | | `check_conventions.py` | `All conventions checks passed.` | | Build (script) and `libs` | OK | | `guest_math`, `guest_math_exact` | 2/2 passed | | Selected tests (script) | No new failures and no timeouts. The 9 known failures also fail on `main`. | | Mutation: cutoff back to ±50000 | `guest_math_exact` aborts | | After restoring | 2/2 passed, `git status --porcelain` empty | On the negative control: if the production files are restored from `main`, the test does not compile, because the export is new (the same result as my earlier control). The cutoff mutation shows that the new range assertions actually check the 29490 cutoff. ## Previous findings - **Blocking 1, special operands and cutoffs contradict the measured `_Exp`/`_FExp`: Resolved.** The PR now follows a measurement of `_LExp` itself, which is what I asked for. - The code at `MathExact.cpp:560-633` and the cutoffs at `:208-213` match each row of BunnyFl's table. - The tests assert those rows, including the exact subnormal bits `0x0.0001368b2fc6f96p-16385` (`GuestMathExact.cpp:204-208`), the cutoff cases (`:217-243`) and the NaN, zero and infinity rows (`:376-438`). All of them pass here. - Signed zero and the order of infinite multipliers are fixed. - The module's results are as BunnyFl reported them; I did not re-run the module. - **Non-blocking 1, accuracy and test tolerance: Still open, non-blocking.** - The debt entry still does not name the source of `p0..q1`, `ln2High`/`ln2Low` and `tiny`. - The sweep at `GuestMathExact.cpp:361-366` still allows `1e-17` relative error, about 90 ulp. BunnyFl reports the 401 points as within that tolerance, not as identical bits, so the sweep does not show bit equality with the module. - **Non-blocking 2, `x == 1` shortcut: Resolved.** It is now limited to `offset == 0 && x == 1 && y == 1` (`MathExact.cpp:625-628`), as in `_Exp`. The constant `0xa.df85458a2bb4a9bp-2L` is `e` correctly rounded: the 64-bit mantissa `0xadf85458a2bb4a9a` has a remainder of 0.687, so it rounds up. - **Non-blocking 3, parameter name: Resolved.** The parameter is `offset` in `Api.hpp` and in the definition. ## New findings ### Blocking None. ### Non-blocking 1. **Dead code left over from earlier versions.** - `OverflowToInfinity`, `OverflowScaled`, `FinishExpScale` and `UnderflowScaled` (`core/libs/prx/libc/src/MathExact.cpp:70-140`) are never called. - The special-operand branches at the top of `ExpScaled` (`:160-207`: infinite `x`, infinite multiplier, `x == 0`, `x == 1`) cannot be reached, because `_LExp_nid_postfix` handles those cases before it calls `ExpScaled`. - The build here is quiet only because `-Wunused-function` is off. - Removing them leaves a single copy of the special-case logic. 2. **Underflow sign and rounding in `ScaleExtended` depend on the shift.** - When `rightShift >= 65`, it stores `+0` in every rounding mode (`:105-108`), as measured. - When `rightShift` is 64 or less, it keeps the sign (a negative product that rounds to zero stores `-0`) and rounds up to `±denorm_min` in the directed modes (`:110-127`). - Neither the tests nor BunnyFl's run cover the second branch, so the two paths may disagree with the module near `denorm_min`. - The same applies to the `x == 0`, nonzero-offset path. `ScaleExtended` raises no flag, and `:611-616` restore the entry flags, so a subnormal result that lost bits there raises no `FE_INEXACT`, unlike the `ExpScaled` path. - Suggested fix: either measure these cases, or record them in the debt entry. 3. **Duplicate include.** `core/libs/tests/GuestMathExact.cpp:11` adds `#include <limits>`, which line 13 already has. 4. **Debt entry order.** `docs/dev/debt/libc.md:162` opens with what matches. CONTRIBUTING asks entries to state the gap first: the unverified title `libc.prx`, console and signaling NaNs, the `short`/64-bit offset difference, and the unnamed coefficient source. ## Not verified - I did not re-run the firmware module or a title's `libc.prx`, and there was no console run. The agreement with the module comes from BunnyFl's report, which the tests encode. - Windows was not run here. maskym reports a pass at this head. - Vulkan validation does not apply: no AGC, shader or VideoOut file is changed. <!-- anyps5-bots job=1912 head=d0a02abde419c60a39b94c5134e29d309b9d5530 -->
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!10780
No description provided.