feat(libSceLibcInternal): implement __truncdfhf2 #15822

Merged
user236404 merged 2 commits from upstream-pr/15822 into main 2026-11-16 08:08:05 +00:00
user236404 commented 2026-11-04 18:02:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by user236404 on 2026-11-04T18:02:31Z.


What

Implements __truncdfhf2 (libSceLibcInternal), compiler-rt's double-to-half conversion, which threw as a parameterless placeholder in RecoveredInventory.cpp. It converts as LLVM 19.1.7's compiler-rt truncdfhf2.c and fp_trunc_impl.inc do:

  • rounds to nearest, ties to even; values from 65520 up become infinity; small values give denormal halves or a signed zero;
  • a NaN keeps its sign and its top nine payload bits and gets the quiet bit;
  • only integer operations are used, so no floating-point exception is raised, as in compiler-rt.

compiler-rt returns the result as _Float16 in XMM0 when built with COMPILER_RT_HAS_FLOAT16 and as uint16_t in AX otherwise (fp_trunc.h), and the console's module was not read. The export is therefore a short assembly entry, built like sigsetjmp in SignalSets.cpp, that calls the C++ conversion and returns the result in both AX and XMM0, so a caller built with either convention gets it.

  • The declaration moves from RecoveredInventory.hpp to include/Api.hpp as std::uint16_t __truncdfhf2(double).
  • docs/dev/debt/libSceLibcInternal.md: new entry (return register unverified, both are set), recovered-inventory placeholder count cut from 21 to 20 (recounted from RecoveredInventory.cpp).

Tested

Rebased onto main 32b292ba38. Linux x86_64 (Fedora 45, i7-9700K), Release build with tests on. Titles, console and macOS: not run. Windows: see the two Windows runs in the comments (on the earlier heads).

  • All 57 tests that libSceLibcInternal/CMakeLists.txt registers pass, including the new guest_libc_internal_half_precision (0.1 s); all 65 public_headers* tests pass.
  • The test's reference is independent of the implementation: all 63,488 finite halves as exact doubles, the nearest found by binary search, ties decided against exact midpoints. It checks that all 65,536 half encodings round-trip (NaNs come back quiet), every midpoint and both its neighbours with both signs, overflow at 65520, denormals and the 2^-25 tie, NaN payloads, 2,000,000 random doubles, and that no floating-point exception is raised. Every check reads AX and, through an assembly helper in the test, XMM0.
  • Removing the copy into XMM0, or rounding ties away from zero, makes the test fail.
  • The entry and the test helper assemble with clang for COFF, Mach-O and ELF with the expected symbol names.
  • tools/check_conventions.py --base upstream/main, tools/check_test_registration.py and tools/check_main_sync.py --strict pass.
  • tools/pr_overlap.py: no other open PR changes __truncdfhf2. #15577 (debt list line) and #15931 (CMakeLists.txt line) conflict only on neighbouring lines. #15581 shows the __truncdfhf2 placeholder only as unchanged context.

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:5159115495 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15822). Originally posted by [user236404](https://github.com/user236404) on 2026-11-04T18:02:31Z. --- ### What Implements `__truncdfhf2` (libSceLibcInternal), compiler-rt's double-to-half conversion, which threw as a parameterless placeholder in `RecoveredInventory.cpp`. It converts as LLVM 19.1.7's compiler-rt [`truncdfhf2.c`](https://github.com/llvm/llvm-project/blob/cd708029e0b2869e80abe31ddb175f7c35361f90/compiler-rt/lib/builtins/truncdfhf2.c#L13) and [`fp_trunc_impl.inc`](https://github.com/llvm/llvm-project/blob/cd708029e0b2869e80abe31ddb175f7c35361f90/compiler-rt/lib/builtins/fp_trunc_impl.inc#L45-L155) do: - rounds to nearest, ties to even; values from 65520 up become infinity; small values give denormal halves or a signed zero; - a NaN keeps its sign and its top nine payload bits and gets the quiet bit; - only integer operations are used, so no floating-point exception is raised, as in compiler-rt. compiler-rt returns the result as `_Float16` in XMM0 when built with `COMPILER_RT_HAS_FLOAT16` and as `uint16_t` in AX otherwise ([`fp_trunc.h`](https://github.com/llvm/llvm-project/blob/cd708029e0b2869e80abe31ddb175f7c35361f90/compiler-rt/lib/builtins/fp_trunc.h#L83-L88)), and the console's module was not read. The export is therefore a short assembly entry, built like `sigsetjmp` in `SignalSets.cpp`, that calls the C++ conversion and returns the result in both AX and XMM0, so a caller built with either convention gets it. - The declaration moves from `RecoveredInventory.hpp` to `include/Api.hpp` as `std::uint16_t __truncdfhf2(double)`. - `docs/dev/debt/libSceLibcInternal.md`: new entry (return register unverified, both are set), recovered-inventory placeholder count cut from 21 to 20 (recounted from `RecoveredInventory.cpp`). ### 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. --> Rebased onto `main` `32b292ba38`. Linux x86_64 (Fedora 45, i7-9700K), Release build with tests on. Titles, console and macOS: not run. Windows: see the two Windows runs in the comments (on the earlier heads). - All 57 tests that `libSceLibcInternal/CMakeLists.txt` registers pass, including the new `guest_libc_internal_half_precision` (0.1 s); all 65 `public_headers*` tests pass. - The test's reference is independent of the implementation: all 63,488 finite halves as exact doubles, the nearest found by binary search, ties decided against exact midpoints. It checks that all 65,536 half encodings round-trip (NaNs come back quiet), every midpoint and both its neighbours with both signs, overflow at 65520, denormals and the 2^-25 tie, NaN payloads, 2,000,000 random doubles, and that no floating-point exception is raised. Every check reads AX and, through an assembly helper in the test, XMM0. - Removing the copy into XMM0, or rounding ties away from zero, makes the test fail. - The entry and the test helper assemble with clang for COFF, Mach-O and ELF with the expected symbol names. - `tools/check_conventions.py --base upstream/main`, `tools/check_test_registration.py` and `tools/check_main_sync.py --strict` pass. - `tools/pr_overlap.py`: no other open PR changes `__truncdfhf2`. #15577 (debt list line) and #15931 (`CMakeLists.txt` line) conflict only on neighbouring lines. #15581 shows the `__truncdfhf2` placeholder only as unchanged context. ### 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)
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!15822
No description provided.