feat(libc): implement _Exp and _FExp #10786

Merged
00xDisaster merged 3 commits from upstream-pr/10786 into main 2026-10-26 07:02:47 +00:00
00xDisaster commented 2026-10-25 00:36:18 +00:00 (Migrated from github.com)

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


What

Add _Exp and _FExp, the double and float Dinkumware helpers that compute y * exp(x) * 2^offset and return the result classification. The special-value precedence and range cutoffs follow PRAGMATA PPSA02530's bundled libc.prx (static disassembly).

Both operands are classified before shortcuts, and only finite/denormal pairs take the finite path. The strict cutoffs use x alone: below -229/-1842 the helpers store 0.0 * y and return Zero without errno or floating-point exceptions; above 229/1842 they store y * inf and raise overflow. An offset does not move the cutoff. A zero multiplier keeps its sign, a NaN multiplier leaves *px unchanged, +inf with zero or -inf with infinity is invalid, and finite multipliers with +inf/-inf return signed infinity/zero without flags. Inside the cutoffs, subnormal results return Finite without flags; a zero result from scaling raises underflow. The x == 1, y == 1, offset == 0 fast path stores the exact console e bits.

_FExp uses the measured reduction constants c1 = 0.69287109375, c2 = 0.00027608682, invln2 = 1.4426950216, with the rational constants folded as Q = 0.09997485578 and P = 0.5 + 0.0083208261 z. The _Exp finite coefficients, exact bit-for-bit _Exp results, signaling NaNs, directed-rounding behavior and non-default flush-to-zero modes remain unverified and are recorded in the libc debt entry.

This PR intentionally leaves _Dsign/_FDsign to open PR #6085 and _Fpcomp/_FFpcomp to open PR #8397; those exports are not duplicated here.

Tested

Linux CachyOS 7.3.0-2-cachyos, GCC 16.2.1. Base 941ace5f34; head 35c38ce8d5; mergeable with current main 6bc1139698.

  • cmake --build build --target guest_dinkumware_internals_tests guest_math_tests guest_math_exact_tests libs --parallel 8: passed.
  • ctest --test-dir build -R '^(guest_dinkumware_internals|guest_math|guest_math_exact)$' --output-on-failure: 3/3 passed.
  • guest_dinkumware_internals covers double and float known values, finite/scale sweeps, zero multipliers, overflow/underflow and non-finite inputs. The special-value cases assert output, errno and floating-point state, including signed zero, NaN write-back, both infinities with zero and finite multipliers, +inf/-inf operand combinations, x-only cutoffs with compensating offsets, subnormal results, scaling underflow, and the exact e fast path.
  • guest_math and guest_math_exact are the neighboring libm suites and pass unchanged.
  • tools/check_conventions.py --base upstream/main --head HEAD and git diff --check: clean.
  • 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: no
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5000495645 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10786). Originally posted by [00xDisaster](https://github.com/00xDisaster) on 2026-10-25T00:36:18Z. --- ### What Add `_Exp` and `_FExp`, the double and float Dinkumware helpers that compute `y * exp(x) * 2^offset` and return the result classification. The special-value precedence and range cutoffs follow PRAGMATA PPSA02530's bundled `libc.prx` (static disassembly). Both operands are classified before shortcuts, and only finite/denormal pairs take the finite path. The strict cutoffs use `x` alone: below -229/-1842 the helpers store `0.0 * y` and return `Zero` without `errno` or floating-point exceptions; above 229/1842 they store `y * inf` and raise overflow. An offset does not move the cutoff. A zero multiplier keeps its sign, a NaN multiplier leaves `*px` unchanged, `+inf` with zero or `-inf` with infinity is invalid, and finite multipliers with `+inf`/`-inf` return signed infinity/zero without flags. Inside the cutoffs, subnormal results return `Finite` without flags; a zero result from scaling raises underflow. The `x == 1, y == 1, offset == 0` fast path stores the exact console `e` bits. `_FExp` uses the measured reduction constants `c1 = 0.69287109375`, `c2 = 0.00027608682`, `invln2 = 1.4426950216`, with the rational constants folded as `Q = 0.09997485578` and `P = 0.5 + 0.0083208261 z`. The `_Exp` finite coefficients, exact bit-for-bit `_Exp` results, signaling NaNs, directed-rounding behavior and non-default flush-to-zero modes remain unverified and are recorded in the libc debt entry. This PR intentionally leaves `_Dsign`/`_FDsign` to open PR #6085 and `_Fpcomp`/`_FFpcomp` to open PR #8397; those exports are not duplicated here. ### Tested Linux CachyOS 7.3.0-2-cachyos, GCC 16.2.1. Base `941ace5f34`; head `35c38ce8d5`; mergeable with current `main` `6bc1139698`. - `cmake --build build --target guest_dinkumware_internals_tests guest_math_tests guest_math_exact_tests libs --parallel 8`: passed. - `ctest --test-dir build -R '^(guest_dinkumware_internals|guest_math|guest_math_exact)$' --output-on-failure`: 3/3 passed. - `guest_dinkumware_internals` covers double and float known values, finite/scale sweeps, zero multipliers, overflow/underflow and non-finite inputs. The special-value cases assert output, `errno` and floating-point state, including signed zero, NaN write-back, both infinities with zero and finite multipliers, `+inf`/`-inf` operand combinations, x-only cutoffs with compensating offsets, subnormal results, scaling underflow, and the exact `e` fast path. - `guest_math` and `guest_math_exact` are the neighboring libm suites and pass unchanged. - `tools/check_conventions.py --base upstream/main --head HEAD` and `git diff --check`: clean. - 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: no
MarcZX10 commented 2026-10-25 00:38:40 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


The focused finite-value tests and current automated checks I could inspect pass, but the range fast paths and scaling status handling have observable correctness gaps. I ran a focused GCC 12.2 harness against this exact head; details are attached inline. Please add regression coverage and address these before merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640791618 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10786#pullrequestreview-5640791618). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:38:40Z. **Review:** CHANGES_REQUESTED --- The focused finite-value tests and current automated checks I could inspect pass, but the range fast paths and scaling status handling have observable correctness gaps. I ran a focused GCC 12.2 harness against this exact head; details are attached inline. Please add regression coverage and address these before merge.
MarcZX10 commented 2026-10-25 00:58:56 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


The updated head still has a range-handling bug in both precision paths. I compiled DinkumwareInternals.cpp from this exact head with GCC 12.2 (-std=c++20 -Wall -Wextra -Werror) and linked a focused harness directly to these functions (function-section garbage collection used only to discard unrelated unresolved imports). For _Exp, x = 710, y = 1e-100, offset = 0 returns a finite value around 2.2339947661617108e208 and classification Finite, but sets errno to ERANGE and raises overflow/inexact flags. The x > HugeExp early return is based only on x, even though the actual operation is y * exp(x) * 2^offset; small nonzero y can bring that product back into range. Conversely, the x < -HugeExp shortcut returns zero for x = -710, y = 1e300, although the product is finite (~4.47628622567513e-9). _FExp has the same shape of defect around its HugeExp threshold. Please base range decisions and error signaling on the final scaled product (including y and offset), and add both-direction regressions for double and float. If these thresholds intentionally model measured console behavior, please attach evidence showing these products are classified/range-signaled this way on-console; the current debt text says the thresholds and exception behavior are unverified.

The implementation file compiles cleanly with -Wall -Wextra -Werror; git diff --check and tools/check_conventions.py also pass. I did not run the project's full CMake/CTest suite in this environment, and the hosted product Linux/Windows jobs are path-skipped; several hosted cross-platform checks are still pending. These checks do not resolve the numerical cases above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640847038 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10786#pullrequestreview-5640847038). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:58:56Z. **Review:** CHANGES_REQUESTED --- The updated head still has a range-handling bug in both precision paths. I compiled `DinkumwareInternals.cpp` from this exact head with GCC 12.2 (`-std=c++20 -Wall -Wextra -Werror`) and linked a focused harness directly to these functions (function-section garbage collection used only to discard unrelated unresolved imports). For `_Exp`, `x = 710`, `y = 1e-100`, `offset = 0` returns a finite value around `2.2339947661617108e208` and classification `Finite`, but sets `errno` to `ERANGE` and raises overflow/inexact flags. The `x > HugeExp` early return is based only on `x`, even though the actual operation is `y * exp(x) * 2^offset`; small nonzero `y` can bring that product back into range. Conversely, the `x < -HugeExp` shortcut returns zero for `x = -710`, `y = 1e300`, although the product is finite (~`4.47628622567513e-9`). `_FExp` has the same shape of defect around its `HugeExp` threshold. Please base range decisions and error signaling on the final scaled product (including `y` and `offset`), and add both-direction regressions for double and float. If these thresholds intentionally model measured console behavior, please attach evidence showing these products are classified/range-signaled this way on-console; the current debt text says the thresholds and exception behavior are unverified. The implementation file compiles cleanly with `-Wall -Wextra -Werror`; `git diff --check` and `tools/check_conventions.py` also pass. I did not run the project's full CMake/CTest suite in this environment, and the hosted product Linux/Windows jobs are path-skipped; several hosted cross-platform checks are still pending. These checks do not resolve the numerical cases above.
MarcZX10 commented 2026-10-25 01:59:07 +00:00 (Migrated from github.com)

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

Review: COMMENTED


I rechecked the exact current head 878124772c13c76de4e180bc7e801a7c4abb0525 after your update. The added subnormal/error tests address the cases listed in your reply, but the two threshold branches remain unchanged: for x > 1842, the function returns signed infinity based only on x; for x < -1842, it returns signed zero based only on x. They still do not account for y or offset, and the tests shown do not cover compensating multipliers near those cutoffs (my earlier x=710/-710 repros are actually below the shortcut threshold, so they expose a separate intermediate/range issue in the scaler path). Please rerun those exact cases against this head and add double/float regressions, or provide console measurements that establish the early-cutoff behavior. I am keeping the existing request for changes until the result range and errno/fenv state are validated for those cases.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641038993 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10786#pullrequestreview-5641038993). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T01:59:07Z. **Review:** COMMENTED --- I rechecked the exact current head `878124772c13c76de4e180bc7e801a7c4abb0525` after your update. The added subnormal/error tests address the cases listed in your reply, but the two threshold branches remain unchanged: for `x > 1842`, the function returns signed infinity based only on `x`; for `x < -1842`, it returns signed zero based only on `x`. They still do not account for `y` or `offset`, and the tests shown do not cover compensating multipliers near those cutoffs (my earlier `x=710`/`-710` repros are actually below the shortcut threshold, so they expose a separate intermediate/range issue in the scaler path). Please rerun those exact cases against this head and add double/float regressions, or provide console measurements that establish the early-cutoff behavior. I am keeping the existing request for changes until the result range and errno/fenv state are validated for those cases.
MarcZX10 commented 2026-10-25 03:06:42 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


The sign and status additions address the previous findings, but the x-only cutoffs still violate this helper’s documented y * exp(x) * 2^offset behavior when offset compensates the exponent. I checked finite reference values independently with the stated formula: _Exp(x=1843, y=1, offset=-2660) is about 0.4623 and _Exp(x=-1843, y=1, offset=2660) is about 2.1630, while the current x > 1842 / x < -1842 branches return infinity / zero. The analogous float cases _FExp(x=230, y=1, offset=-332) and _FExp(x=-230, y=1, offset=332) are about 0.8826 and 1.1330; the current x > 229 / x < -229 branches also return infinity / zero. Please add these compensating-scale regressions for both precision paths and base the range/error decisions on the final result. Assert the returned classification, stored value, errno and fenv flags. Hosted conventions, eligibility, labels and cross-platform policy jobs pass; product Linux/Windows jobs are skipped, so the reported focused tests do not currently cover this boundary.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641283377 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10786#pullrequestreview-5641283377). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T03:06:42Z. **Review:** CHANGES_REQUESTED --- The sign and status additions address the previous findings, but the x-only cutoffs still violate this helper’s documented `y * exp(x) * 2^offset` behavior when `offset` compensates the exponent. I checked finite reference values independently with the stated formula: `_Exp(x=1843, y=1, offset=-2660)` is about 0.4623 and `_Exp(x=-1843, y=1, offset=2660)` is about 2.1630, while the current `x > 1842` / `x < -1842` branches return infinity / zero. The analogous float cases `_FExp(x=230, y=1, offset=-332)` and `_FExp(x=-230, y=1, offset=332)` are about 0.8826 and 1.1330; the current `x > 229` / `x < -229` branches also return infinity / zero. Please add these compensating-scale regressions for both precision paths and base the range/error decisions on the final result. Assert the returned classification, stored value, errno and fenv flags. Hosted conventions, eligibility, labels and cross-platform policy jobs pass; product Linux/Windows jobs are skipped, so the reported focused tests do not currently cover this boundary.
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!10786
No description provided.