fix(libc): compute logb and logbf with FreeBSD msun's algorithm, as titles' libc.prx does #10346

Merged
JJenkx merged 1 commit from upstream-pr/10346 into main 2026-10-27 08:09:13 +00:00
JJenkx commented 2026-10-23 18:31:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T18:31:54Z.


What

logb and logbf returned the host's logb, which differs from the libc.prx that titles ship in two ways on Windows (UCRT):

  • for ±0 it sets errno to ERANGE, while the titles' copies return -inf and leave errno alone;
  • it returns a signalling NaN unchanged, while the titles' copies quiet it. For example, logb(0x7ff0000000000001) is 0x7ff8000000000001 there.

The titles' copies (NIDs owKuegZU4ew and RWqyr1OKuw4) are FreeBSD msun's s_logb.c and s_logbf.c. Both functions now compute them:

  • -1 / |x| for ±0;
  • x * x for infinities and NaNs;
  • the exponent of a subnormal after scaling by 2^54 (2^25 in float);
  • the biased exponent otherwise.

Tested

Rebased as 0f7056d67bde93eec09453764c5b5ead5c5a98b1, one topic commit directly on main 5b85225ef39b9321d8f5c251ecb391b8331e8aea, without merge commits. Only the additive debt append conflicted; all main entries and this PR's logb/logbf entry are retained. The added and removed line multisets in implementation, tests and debt are unchanged from previous public head a9a153ac2b2493a53929855d86b9f401c19f70cd.

Fresh Linux x86-64 Release builds of libc and the three math executables pass. guest_math, guest_math_exact, guest_dinkumware_internals and public_headers each pass three consecutive runs on main and on the change (12 passes per arm, no skips). Conventions and whitespace checks pass. This establishes Linux compatibility; it does not demonstrate a Linux defect fixed. Windows, title-library comparisons and floating-point exception flags were not rerun for this rebase.

Exact-head refresh patch, preservation proof and build/test logs — SHA256 d080877ea94697087a0faab38d7de212d72bf059be3d2f1ded1a7b1c2d2b0c8a; uploaded archive downloaded and hash-verified.

Earlier Windows report, retained from public head d7bf9b3772; Windows was not rerun for this Linux refresh:

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, main 65ac1d3b50.

  • Comparison: a scratch loader runs the decrypted libc.prx of eleven titles natively and supplies __error: PPSA04158, PPSA05916, PPSA07923, PPSA09614, PPSA13934, PPSA20423, PPSA25636, PPSA26528, PPSA33655, PPSA34008 and PPSA35169. It compared their logb and logbf with this PR's code, checking result bits (NaN payloads included) and errno, on 4,032,768 inputs per title:

    • every exponent of both signs with five significands;
    • the 4,096 smallest subnormals at three scales;
    • 4 million random bit patterns;
    • the float check uses the high 32 bits of each input.

    All eleven give identical bits on all of them, and none sets errno. Against the host's logb with errno ignored, 997 double and 7,957 float inputs differ, all signalling NaNs.

  • guest_math: the new CheckLogbConsoleBits has 15 double and 15 float cases with the console's results:

    • ±0 and ± the smallest subnormal;
    • the largest subnormal and the smallest normal;
    • 0.75, ±8 and the largest finite value;
    • ±inf, a signalling NaN of each sign and a quiet NaN.

    Each case also checks that errno stays unchanged. main's existing logb checks still pass.

    guest_math and guest_dinkumware_internals pass in three runs out of three on the exact head.

  • Negative control: with main's Math.cpp and these tests, guest_math aborts in a Require, while guest_dinkumware_internals still passes.

  • A standalone -O0 and -O3 build of the new code with the new vectors passes.

  • python tools/check_conventions.py --base origin/main and git diff --check pass.

  • Order: this PR adds a test and a debt entry at the same anchors as my other libc PRs, so it conflicts with them textually only and lands in any order.

  • Not tested: macOS, a physical console, the floating-point exception flags, and logbl.

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: none; the msun functions are rewritten, as #9668, #5036 and the sinhf entry did
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4987521431 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10346). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T18:31:54Z. --- ### What `logb` and `logbf` returned the host's `logb`, which differs from the `libc.prx` that titles ship in two ways on Windows (UCRT): - for ±0 it sets `errno` to `ERANGE`, while the titles' copies return `-inf` and leave `errno` alone; - it returns a signalling NaN unchanged, while the titles' copies quiet it. For example, `logb(0x7ff0000000000001)` is `0x7ff8000000000001` there. The titles' copies (NIDs `owKuegZU4ew` and `RWqyr1OKuw4`) are FreeBSD msun's [`s_logb.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_logb.c) and [`s_logbf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_logbf.c). Both functions now compute them: - `-1 / |x|` for ±0; - `x * x` for infinities and NaNs; - the exponent of a subnormal after scaling by 2^54 (2^25 in float); - the biased exponent otherwise. ### Tested Rebased as `0f7056d67bde93eec09453764c5b5ead5c5a98b1`, one topic commit directly on main `5b85225ef39b9321d8f5c251ecb391b8331e8aea`, without merge commits. Only the additive debt append conflicted; all main entries and this PR's logb/logbf entry are retained. The added and removed line multisets in implementation, tests and debt are unchanged from previous public head `a9a153ac2b2493a53929855d86b9f401c19f70cd`. Fresh Linux x86-64 Release builds of libc and the three math executables pass. `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` and `public_headers` each pass three consecutive runs on main and on the change (12 passes per arm, no skips). Conventions and whitespace checks pass. This establishes Linux compatibility; it does not demonstrate a Linux defect fixed. Windows, title-library comparisons and floating-point exception flags were not rerun for this rebase. [Exact-head refresh patch, preservation proof and build/test logs](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10346-head0f7056d6-main5b85-linux-refresh.tar.gz) — SHA256 `d080877ea94697087a0faab38d7de212d72bf059be3d2f1ded1a7b1c2d2b0c8a`; uploaded archive downloaded and hash-verified. Earlier Windows report, retained from public head `d7bf9b3772`; Windows was not rerun for this Linux refresh: Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 65ac1d3b50. - **Comparison:** a scratch loader runs the decrypted `libc.prx` of eleven titles natively and supplies `__error`: PPSA04158, PPSA05916, PPSA07923, PPSA09614, PPSA13934, PPSA20423, PPSA25636, PPSA26528, PPSA33655, PPSA34008 and PPSA35169. It compared their `logb` and `logbf` with this PR's code, checking result bits (NaN payloads included) and `errno`, on 4,032,768 inputs per title: - every exponent of both signs with five significands; - the 4,096 smallest subnormals at three scales; - 4 million random bit patterns; - the float check uses the high 32 bits of each input. All eleven give identical bits on all of them, and none sets `errno`. Against the host's `logb` with `errno` ignored, 997 double and 7,957 float inputs differ, all signalling NaNs. - **`guest_math`:** the new `CheckLogbConsoleBits` has 15 double and 15 float cases with the console's results: - ±0 and ± the smallest subnormal; - the largest subnormal and the smallest normal; - 0.75, ±8 and the largest finite value; - ±`inf`, a signalling NaN of each sign and a quiet NaN. Each case also checks that `errno` stays unchanged. `main`'s existing `logb` checks still pass. `guest_math` and `guest_dinkumware_internals` pass in three runs out of three on the exact head. - **Negative control:** with `main`'s `Math.cpp` and these tests, `guest_math` aborts in a `Require`, while `guest_dinkumware_internals` still passes. - A standalone `-O0` and `-O3` build of the new code with the new vectors passes. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - **Order:** this PR adds a test and a debt entry at the same anchors as my other libc PRs, so it conflicts with them textually only and lands in any order. - **Not tested:** macOS, a physical console, the floating-point exception flags, and `logbl`. ### Checklist - [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: none; the msun functions are rewritten, as #9668, #5036 and the `sinhf` entry did - [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 AI-assisted (Claude Code).
MarcZX10 commented 2026-10-23 18:59:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T18:59:18Z.

Review: APPROVED


Reviewed the bit-level implementation and the subnormal scaling paths for both precisions. The zero case produces negative infinity without setting errno, and the x * x path quiets signaling NaNs while retaining the payload as the reported title copies do. The added vectors cover signed zero, subnormal boundaries, finite extremes, infinities, signaling NaNs and quiet NaNs.

The PR reports matching output bits and errno against eleven bundled title libc.prx copies over 4,032,768 inputs per function, plus three passing guest_math runs and a negative control. Hosted conventions, eligibility, intake, labels, report and relinker checks pass; full Linux/Windows product jobs are skipped by repository policy. I did not run a local product test because this host fails the project's CPU feature guard. I found no blocking issue in the inspected changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633148042 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10346#pullrequestreview-5633148042). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T18:59:18Z. **Review:** APPROVED --- Reviewed the bit-level implementation and the subnormal scaling paths for both precisions. The zero case produces negative infinity without setting `errno`, and the `x * x` path quiets signaling NaNs while retaining the payload as the reported title copies do. The added vectors cover signed zero, subnormal boundaries, finite extremes, infinities, signaling NaNs and quiet NaNs. The PR reports matching output bits and `errno` against eleven bundled title `libc.prx` copies over 4,032,768 inputs per function, plus three passing `guest_math` runs and a negative control. Hosted conventions, eligibility, intake, labels, report and relinker checks pass; full Linux/Windows product jobs are skipped by repository policy. I did not run a local product test because this host fails the project's CPU feature guard. I found no blocking issue in the inspected changes.
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!10346
No description provided.