test(libc): cover exp and expf special values and range errors #15141

Merged
Oscar45397 merged 2 commits from upstream-pr/15141 into main 2026-11-11 23:33:32 +00:00
Oscar45397 commented 2026-11-02 18:23:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Oscar45397 on 2026-11-02T18:23:07Z.


What

exp and expf have no test. CheckExpSpecialValues is added to guest_math_exact:

  • exp(+-0) == 1, exp(+inf) == +inf, exp(-inf) == +0 (positive zero), exp(NaN) is NaN;
  • exp(1) and exp(-1) within a relative 1e-15 of e and 1/e (2.718281828459045, 0.36787944117144233);
  • exp(710) overflows to +inf with errno == ERANGE (ln of DBL_MAX is about 709.78); exp(-746) is 0 (below half the smallest subnormal, whose natural logarithm is about -744.44);
  • the same for expf, with expf(1) within a relative 1e-6, expf(89) overflowing to +inf with ERANGE (ln of FLT_MAX is about 88.72) and expf(-110) equal to 0.

Sources: C11 F.10.3.1 (exp(+-0) returns 1, exp(-inf) returns +0, exp(+inf) returns +inf); POSIX exp: a range error occurs on overflow and the function returns HUGE_VAL, with errno set to ERANGE. The overflow and underflow thresholds follow from the logarithms of the largest finite and smallest subnormal values of each format.

Changed: core/libs/tests/GuestMathExact.cpp. Test only, no change to the library.

Tested

Windows 11 Home 26200, Ryzen 9 5900X, WinLibs GCC 15.2.0 r7, Release, on main b28bb77217 plus this test.

  • ctest --test-dir build -R '^guest_math_exact$' --repeat until-fail:3: passes 3 of 3 runs.
  • Red control: with expf returning std::exp2(x), guest_math_exact fails. Restored, it passes again.
  • python tools/check_conventions.py --base upstream/main: passed.

Not run on Linux or macOS: the ERANGE checks rely on the host exp setting errno, which POSIX requires.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5125301286 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15141). Originally posted by [Oscar45397](https://github.com/Oscar45397) on 2026-11-02T18:23:07Z. --- ### What `exp` and `expf` have no test. `CheckExpSpecialValues` is added to `guest_math_exact`: - `exp(+-0) == 1`, `exp(+inf) == +inf`, `exp(-inf) == +0` (positive zero), `exp(NaN)` is NaN; - `exp(1)` and `exp(-1)` within a relative 1e-15 of e and 1/e (`2.718281828459045`, `0.36787944117144233`); - `exp(710)` overflows to `+inf` with `errno == ERANGE` (ln of DBL_MAX is about 709.78); `exp(-746)` is 0 (below half the smallest subnormal, whose natural logarithm is about -744.44); - the same for `expf`, with `expf(1)` within a relative 1e-6, `expf(89)` overflowing to `+inf` with `ERANGE` (ln of FLT_MAX is about 88.72) and `expf(-110)` equal to 0. Sources: C11 F.10.3.1 (`exp(+-0)` returns 1, `exp(-inf)` returns +0, `exp(+inf)` returns +inf); POSIX `exp`: a range error occurs on overflow and the function returns HUGE_VAL, with `errno` set to `ERANGE`. The overflow and underflow thresholds follow from the logarithms of the largest finite and smallest subnormal values of each format. Changed: `core/libs/tests/GuestMathExact.cpp`. Test only, no change to the library. ### 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 Home 26200, Ryzen 9 5900X, WinLibs GCC 15.2.0 r7, Release, on `main` b28bb77217 plus this test. - `ctest --test-dir build -R '^guest_math_exact$' --repeat until-fail:3`: passes 3 of 3 runs. - Red control: with `expf` returning `std::exp2(x)`, `guest_math_exact` fails. Restored, it passes again. - `python tools/check_conventions.py --base upstream/main`: passed. Not run on Linux or macOS: the `ERANGE` checks rely on the host `exp` setting `errno`, which POSIX requires. ### 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: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
BunnyFl commented 2026-11-02 19:45:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T19:45:27Z.

Review: APPROVED


The errno rows hold for the console's system libc too, not only for the host math libraries. Firmware 13.42's system/common/lib/libSceLibcInternal.sprx (sha256 a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285), disassembly only: exp, expf, log, logf and pow all report errors through one helper at 0x16450, which stores into the location returned by its call at 0x16463 and picks 0x21 (EDOM) when bit 0 of its flag argument is set, otherwise 0x22 (ERANGE) when any of bits 0x1c is set:

1647b: test $0x1,%bl ; jne 1648b      invalid -> 0x21
16480: mov $0x22,%r14d
16486: test $0x1c,%bl ; je 16493      overflow / underflow / divide-by-zero -> 0x22
16490: mov %r14d,(%rax)

For example exp calls it with 8 on overflow (4c323). I haven't read every call site's flag, and I haven't checked the libc.prx that titles ship, which the debt file says uses FreeBSD msun. Tested on Windows 11, WinLibs GCC 15.2.0, with this head merged onto main: guest_math_exact passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5742374102 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15141#pullrequestreview-5742374102). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T19:45:27Z. **Review:** APPROVED --- The `errno` rows hold for the console's system libc too, not only for the host math libraries. Firmware 13.42's `system/common/lib/libSceLibcInternal.sprx` (sha256 `a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285`), disassembly only: `exp`, `expf`, `log`, `logf` and `pow` all report errors through one helper at `0x16450`, which stores into the location returned by its call at `0x16463` and picks `0x21` (`EDOM`) when bit 0 of its flag argument is set, otherwise `0x22` (`ERANGE`) when any of bits `0x1c` is set: ``` 1647b: test $0x1,%bl ; jne 1648b invalid -> 0x21 16480: mov $0x22,%r14d 16486: test $0x1c,%bl ; je 16493 overflow / underflow / divide-by-zero -> 0x22 16490: mov %r14d,(%rax) ``` For example `exp` calls it with 8 on overflow (`4c323`). I haven't read every call site's flag, and I haven't checked the `libc.prx` that titles ship, which the debt file says uses FreeBSD msun. Tested on Windows 11, WinLibs GCC 15.2.0, with this head merged onto `main`: `guest_math_exact` passes.
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!15141
No description provided.