test(libc): cover log and logf special values and domain errors #15142

Merged
Oscar45397 merged 2 commits from upstream-pr/15142 into main 2026-11-13 05:39:33 +00:00
Oscar45397 commented 2026-11-02 18:23:09 +00:00 (Migrated from github.com)

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


What

log and logf have no test. CheckLogSpecialValues is added to guest_math_exact:

  • log(1) == +0; log(+-0) == -inf with errno == ERANGE; log(-1) and log(-inf) are NaN with errno == EDOM; log(+inf) == +inf; log(NaN) is NaN;
  • log(2) and log(0.5) within a relative 1e-15 of +-ln 2 (0.6931471805599453), log(2.718281828459045) within 1e-15 of 1;
  • the same for logf, within a relative 1e-6, plus logf(1.4012985e-45f) (the smallest subnormal, 2^-149) within 1e-6 of -103.27893, which is -149 ln 2.

Sources: C11 F.10.3.7 (log(1) returns +0; log(+-0) returns -inf and raises divide-by-zero; log(x) for x < 0 returns a NaN and raises invalid; log(+inf) returns +inf); POSIX log: a pole error sets errno to ERANGE and a domain error sets it to EDOM.

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 log returning std::log2(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 errno checks rely on the host log 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:5125301826 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15142). Originally posted by [Oscar45397](https://github.com/Oscar45397) on 2026-11-02T18:23:09Z. --- ### What `log` and `logf` have no test. `CheckLogSpecialValues` is added to `guest_math_exact`: - `log(1) == +0`; `log(+-0) == -inf` with `errno == ERANGE`; `log(-1)` and `log(-inf)` are NaN with `errno == EDOM`; `log(+inf) == +inf`; `log(NaN)` is NaN; - `log(2)` and `log(0.5)` within a relative 1e-15 of +-ln 2 (`0.6931471805599453`), `log(2.718281828459045)` within 1e-15 of 1; - the same for `logf`, within a relative 1e-6, plus `logf(1.4012985e-45f)` (the smallest subnormal, 2^-149) within 1e-6 of -103.27893, which is -149 ln 2. Sources: C11 F.10.3.7 (`log(1)` returns +0; `log(+-0)` returns -inf and raises divide-by-zero; `log(x)` for x < 0 returns a NaN and raises invalid; `log(+inf)` returns +inf); POSIX `log`: a pole error sets `errno` to `ERANGE` and a domain error sets it to `EDOM`. 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 `log` returning `std::log2(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 `errno` checks rely on the host `log` 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:30 +00:00 (Migrated from github.com)

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

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:5742374682 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15142#pullrequestreview-5742374682). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T19:45:30Z. **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!15142
No description provided.