fix(libSceNgs2): compute the PitchShift ratio with libc's msun exp2f #12864

Merged
JJenkx merged 1 commit from upstream-pr/12864 into main 2026-11-02 16:14:43 +00:00
JJenkx commented 2026-10-29 13:08:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-29T13:08:15Z.


What

The custom PitchShift ratio was std::exp2(cents / 1200) evaluated by the host libm, so its last bit depended on the host. On Windows 11 build 22621, UCRT's exp2f is one ulp off at +700 and -700 cents, and ngs2_pitchshift fails there.

The ratio now comes from libc's exp2f_nid_postfix. That is AnyPS5's port of FreeBSD 11.4 msun s_exp2f.c, the same function guest code already gets from libc:

  • It is fixed double arithmetic over a 16-entry table, with no host libm.
  • libSceNgs2.native and libc both build with -ffp-contract=off, so every host computes the same bits.
  • The exponent is the float quotient static_cast<float>(cents) / 1200.0f, as before this change. For all 2401 integer cents values in [-1200, 1200], the msun result equals the correctly rounded float of 2 raised to that float quotient. I checked this offline with a 60-digit decimal evaluation and a float32 emulation of the msun algorithm, and the Linux run below confirms it with 100 digits. It is not the correctly rounded float of the exact 2^(cents/1200): the float division already rounds the exponent, and the results differ at 227 of the 2401 values (for example -1185 cents: 0x3f011d25, where the exact value rounds to 0x3f011d26). What this change guarantees is determinism across hosts.

Provenance of the expected values, stated plainly.

  • ngs2_pitchshift's goldens come from #11315. Its description says they come from comparisons against the 13.60 libSceNgs2 reference library run on a host "with host synchronization, TLS, math and SDK adapters", and that "these are not console runs".
  • So the goldens reflect the reference module's code with a host math library underneath, not a PS5 measurement.
  • That #11315 needed math adapters suggests the module takes its math from imported library functions. I have not verified which function it uses for this ratio, and I have no console measurement of the ratio.
  • What this change guarantees is a host-independent, correctly rounded ratio computed with the console libc's msun algorithm. It does not guarantee the PS5's bits.

The earlier version of this PR used a long double exp2. As the review points out, that still varies by host, and this change replaces it.

Tested

Windows 11 Enterprise 10.0.22621 (ucrtbase.dll 10.0.22621.3593), Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, full build with BUILD_TESTING=ON, on main e0caf13600.

  • ctest -R "^ngs2" -j8 --repeat until-fail:3: 46/46 pass. This includes ngs2_pitchshift, which fails on main on this host. It failed in every full run of main here.
  • Offline check: for every integer cents value in [-1200, 1200], the msun exp2f result equals the correctly rounded float of 2 raised to the float quotient cents / 1200.0f. At ±700 that is 0x1.7f910ep+0 and 0x1.55b810p-1.
  • Linux (posted below, e2103c7f5c on main 0580e5c951): 46/46 NGS2 tests pass three times with this PR, and also on main, which does not reproduce the Windows failure there.
  • Not run:
    • Linux and macOS. The change removes the host dependence, but CI will confirm ngs2_pitchshift there.
    • No title was tested.
  • Reverb.cpp still uses host pow/log10; it is outside this PR.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5068579913 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12864). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-29T13:08:15Z. --- ### What The custom PitchShift ratio was `std::exp2(cents / 1200)` evaluated by the host libm, so its last bit depended on the host. On Windows 11 build 22621, UCRT's `exp2f` is one ulp off at +700 and -700 cents, and `ngs2_pitchshift` fails there. The ratio now comes from libc's `exp2f_nid_postfix`. That is AnyPS5's port of FreeBSD 11.4 msun `s_exp2f.c`, the same function guest code already gets from `libc`: - It is fixed `double` arithmetic over a 16-entry table, with no host libm. - `libSceNgs2.native` and `libc` both build with `-ffp-contract=off`, so every host computes the same bits. - The exponent is the `float` quotient `static_cast<float>(cents) / 1200.0f`, as before this change. For all 2401 integer cents values in [-1200, 1200], the msun result equals the correctly rounded `float` of 2 raised to that `float` quotient. I checked this offline with a 60-digit decimal evaluation and a float32 emulation of the msun algorithm, and the Linux run below confirms it with 100 digits. It is not the correctly rounded `float` of the exact 2^(cents/1200): the `float` division already rounds the exponent, and the results differ at 227 of the 2401 values (for example -1185 cents: `0x3f011d25`, where the exact value rounds to `0x3f011d26`). What this change guarantees is determinism across hosts. **Provenance of the expected values, stated plainly.** - `ngs2_pitchshift`'s goldens come from #11315. Its description says they come from comparisons against the 13.60 `libSceNgs2` reference library run on a host "with host synchronization, TLS, math and SDK adapters", and that "these are not console runs". - So the goldens reflect the reference module's code with a host math library underneath, not a PS5 measurement. - That #11315 needed math adapters suggests the module takes its math from imported library functions. I have not verified which function it uses for this ratio, and I have no console measurement of the ratio. - What this change guarantees is a host-independent, correctly rounded ratio computed with the console libc's msun algorithm. It does not guarantee the PS5's bits. The earlier version of this PR used a `long double` `exp2`. As the review points out, that still varies by host, and this change replaces it. ### Tested Windows 11 Enterprise 10.0.22621 (`ucrtbase.dll` 10.0.22621.3593), Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, full build with `BUILD_TESTING=ON`, on `main` `e0caf13600`. - `ctest -R "^ngs2" -j8 --repeat until-fail:3`: 46/46 pass. This includes `ngs2_pitchshift`, which fails on `main` on this host. It failed in every full run of `main` here. - Offline check: for every integer cents value in [-1200, 1200], the msun `exp2f` result equals the correctly rounded `float` of 2 raised to the `float` quotient `cents / 1200.0f`. At ±700 that is `0x1.7f910ep+0` and `0x1.55b810p-1`. - Linux (posted below, `e2103c7f5c` on `main` `0580e5c951`): 46/46 NGS2 tests pass three times with this PR, and also on `main`, which does not reproduce the Windows failure there. - **Not run:** - Linux and macOS. The change removes the host dependence, but CI will confirm `ngs2_pitchshift` there. - No title was tested. - `Reverb.cpp` still uses host `pow`/`log10`; it is outside this PR. ### 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
CnRJay commented 2026-11-01 19:51:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:51:03Z.

Review: COMMENTED


  1. Correct rounding is not evidence of what the console produces. The case made is that UCRT's exp2f is one ulp off on build 22621 for +700/-700 cents, so the test fails there. That shows the host libm varies. It does not show the PS5's PitchShift ratio equals the correctly rounded exp2 of cents/1200. If #11315's golden values come from hardware, say so; if they came from another host's libm, the test is pinned to an accident and this makes it pass on a different one.
  2. The fix does not make the result platform-independent. On MinGW and x86 Linux long double is the 80-bit exp2l; on macOS arm64 the body says it falls back to double; on MSVC-ABI long double is double. The double-rounding behaviour differs between those, and glibc's exp2l is not specified as correctly rounded either. A title's audio then still depends on the host.
  3. Determinism is the actual requirement. If the ratio must be bit-exact across hosts, compute it without the host libm: a fixed table for the 2401 integer cents values, or an explicit polynomial in float/double that is the same everywhere. The body notes Reverb.cpp has the same pow/log10 exposure and leaves it, so this is a one-site patch to one failing test.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729927589 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12864#pullrequestreview-5729927589). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:51:03Z. **Review:** COMMENTED --- 1. **Correct rounding is not evidence of what the console produces.** The case made is that UCRT's `exp2f` is one ulp off on build 22621 for +700/-700 cents, so the test fails there. That shows the host libm varies. It does not show the PS5's PitchShift ratio equals the correctly rounded `exp2` of `cents/1200`. If #11315's golden values come from hardware, say so; if they came from another host's libm, the test is pinned to an accident and this makes it pass on a different one. 2. **The fix does not make the result platform-independent.** On MinGW and x86 Linux `long double` is the 80-bit `exp2l`; on macOS arm64 the body says it falls back to `double`; on MSVC-ABI `long double` is `double`. The double-rounding behaviour differs between those, and glibc's `exp2l` is not specified as correctly rounded either. A title's audio then still depends on the host. 3. **Determinism is the actual requirement.** If the ratio must be bit-exact across hosts, compute it without the host libm: a fixed table for the 2401 integer cents values, or an explicit polynomial in `float`/`double` that is the same everywhere. The body notes `Reverb.cpp` has the same `pow`/`log10` exposure and leaves it, so this is a one-site patch to one failing test.
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!12864
No description provided.