fix(libSceNgs2): compute the PitchShift ratio with libc's msun exp2f #12864
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12864"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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'sexp2fis one ulp off at +700 and -700 cents, andngs2_pitchshiftfails there.The ratio now comes from libc's
exp2f_nid_postfix. That is AnyPS5's port of FreeBSD 11.4 msuns_exp2f.c, the same function guest code already gets fromlibc:doublearithmetic over a 16-entry table, with no host libm.libSceNgs2.nativeandlibcboth build with-ffp-contract=off, so every host computes the same bits.floatquotientstatic_cast<float>(cents) / 1200.0f, as before this change. For all 2401 integer cents values in [-1200, 1200], the msun result equals the correctly roundedfloatof 2 raised to thatfloatquotient. 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 roundedfloatof the exact 2^(cents/1200): thefloatdivision already rounds the exponent, and the results differ at 227 of the 2401 values (for example -1185 cents:0x3f011d25, where the exact value rounds to0x3f011d26). 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.60libSceNgs2reference library run on a host "with host synchronization, TLS, math and SDK adapters", and that "these are not console runs".The earlier version of this PR used a
long doubleexp2. As the review points out, that still varies by host, and this change replaces it.Tested
Windows 11 Enterprise 10.0.22621 (
ucrtbase.dll10.0.22621.3593), Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, full build withBUILD_TESTING=ON, onmaine0caf13600.ctest -R "^ngs2" -j8 --repeat until-fail:3: 46/46 pass. This includesngs2_pitchshift, which fails onmainon this host. It failed in every full run ofmainhere.exp2fresult equals the correctly roundedfloatof 2 raised to thefloatquotientcents / 1200.0f. At ±700 that is0x1.7f910ep+0and0x1.55b810p-1.e2103c7f5conmain0580e5c951): 46/46 NGS2 tests pass three times with this PR, and also onmain, which does not reproduce the Windows failure there.ngs2_pitchshiftthere.Reverb.cppstill uses hostpow/log10; it is outside this PR.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:51:03Z.
Review: COMMENTED
exp2fis 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 roundedexp2ofcents/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.long doubleis the 80-bitexp2l; on macOS arm64 the body says it falls back todouble; on MSVC-ABIlong doubleisdouble. The double-rounding behaviour differs between those, and glibc'sexp2lis not specified as correctly rounded either. A title's audio then still depends on the host.float/doublethat is the same everywhere. The body notesReverb.cpphas the samepow/log10exposure and leaves it, so this is a one-site patch to one failing test.