feat(libc): keep the guest's x87 long double exactly in lifted builds #15802

Closed
oberonlift wants to merge 22 commits from upstream-pr/15802 into main
oberonlift commented 2026-11-04 16:24:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oberonlift on 2026-11-04T16:24:01Z.


Merge after #15801. Until the PRs before it merge, this PR also shows their changes; oberonlift/AnyPS5#12 shows only its own.

ps5lift is a static recompiler for PS5 executables: it lifts a game's x86-64 code to LLVM IR and compiles it ahead of time to aarch64, which runs on Linux against AnyPS5's own libraries with no interpreter or JIT. It runs AnyPS5's PS5-style test programs and a homebrew media player to its home screen; commercial games are not playable yet. Branch ps5lift-preview merges all of its PRs (how to try it); native Apple Silicon support is coming along fast. It is a separate approach from the ARM64 compiler in #5021; #1835 has an introduction.

This is 6 of 8 PRs that let AnyPS5's libraries run a lifted program; the lifter itself follows them.

x86-64: behaviour unchanged. There GuestLongDouble is long double and its helpers are the standard functions, so strtold is still std::strtold; ldexpl and scalbnl now call std::scalbln and drem calls std::remquo, which return the same values.

What

A lifted libc held the guest's x87 long double in the host's: IEEE binary128 on aarch64 Linux, which holds every 80-bit value but rounds each step differently, and a plain double on macOS, which holds neither.

  • GuestLongDouble loads and stores the guest's 80-bit values where libc takes them in and hands them out (formatting, scanning, strtold, modfl, _LDtest), and long doubles that host callers pass through a va_list are rounded to the guest's precision. On an x87 host it only copies.
  • Lifted builds keep the value in GuestLongDouble, laid out as the guest stores it. Arithmetic, square root, integer rounding and conversions to double, float and integers run through the ps5lift runtime's x87, so they round, honour precision control and raise exceptions as the guest's control and status words say. Comparisons, conversions from integers and doubles, frexp, scalbn, fmod, remainder, remquo, modf, logb and the fixed-direction roundings work on the bits; a NaN operand or a domain error in fmod, remainder and remquo goes through the x87, so the NaN propagates or invalid is raised as on the guest.
  • The _LDint, _LDscale, _LDunscale, _LFpcomp and _LPoly helpers take GuestLongDouble too; in lifted builds _LDscale and _LDunscale scale and split the value on its bits.
  • strtold and scanf("%Lf") parse with exact big-integer arithmetic, correctly rounded in the guest's rounding mode.
  • Builds that are not lifted keep the host's long double wherever it is wider than 80 bits too, such as Emscripten's wasm64 target: there strtold is still the host's.
  • cbrtl runs FreeBSD's x87 algorithm, as on main, on GuestLongDouble, and in lifted builds remainder, remainderf, remquo and remquof take their result and quotient bits from its exact remainder instead of fprem1.
  • The other transcendental l functions and fmal still compute in the host's widest type and round the result. The libc debt notes say so, and record that lifted builds don't build __mulxc3 and __divxc3 and that long double scans store the guest's format for host callers too.

#5021 also changes some of the same files: Strings.cpp, WindowsFormatting.hpp.

Tested

Compiled for x86-64 Linux before and after this PR (GCC 13, same flags): 137 of the 146 objects it reaches are byte-identical. libSceNet's Export.cpp differs only in the generated name of a switch table and Strings.cpp in an out-of-line copy of the inline strtold wrapper; the other seven are libc's long double, formatting and scanning sources.

On a DGX Spark (aarch64 Linux, NVIDIA GB10, LLVM 18), with all eight PRs and ps5lift on top: every library builds for a lifted program, 2,285 of AnyPS5's own tests pass in that build on lavapipe, and ps5lift's 69 tests pass, among them its libc, exceptions, kernel, fiber and module-loading fixtures and upstream's libc test programs.

The conventions check passes on this branch.

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: #15801
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5157735450 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15802). Originally posted by [oberonlift](https://github.com/oberonlift) on 2026-11-04T16:24:01Z. --- **Merge after #15801.** Until the PRs before it merge, this PR also shows their changes; oberonlift/AnyPS5#12 shows only its own. **ps5lift** is a static recompiler for PS5 executables: it lifts a game's x86-64 code to LLVM IR and compiles it ahead of time to aarch64, which runs on Linux against AnyPS5's own libraries with no interpreter or JIT. It runs AnyPS5's PS5-style test programs and a homebrew media player to its home screen; commercial games are not playable yet. Branch [`ps5lift-preview`](https://github.com/oberonlift/AnyPS5/tree/ps5lift-preview) merges all of its PRs ([how to try it](https://github.com/oberonlift/AnyPS5/blob/ps5lift-preview/PS5LIFT_PREVIEW.md)); native Apple Silicon support is coming along fast. It is a separate approach from the ARM64 compiler in #5021; #1835 has an introduction. This is 6 of 8 PRs that let AnyPS5's libraries run a lifted program; the lifter itself follows them. **x86-64:** behaviour unchanged. There `GuestLongDouble` is `long double` and its helpers are the standard functions, so `strtold` is still `std::strtold`; `ldexpl` and `scalbnl` now call `std::scalbln` and `drem` calls `std::remquo`, which return the same values. ### What A lifted libc held the guest's x87 `long double` in the host's: IEEE binary128 on aarch64 Linux, which holds every 80-bit value but rounds each step differently, and a plain double on macOS, which holds neither. - `GuestLongDouble` loads and stores the guest's 80-bit values where libc takes them in and hands them out (formatting, scanning, `strtold`, `modfl`, `_LDtest`), and long doubles that host callers pass through a `va_list` are rounded to the guest's precision. On an x87 host it only copies. - Lifted builds keep the value in `GuestLongDouble`, laid out as the guest stores it. Arithmetic, square root, integer rounding and conversions to double, float and integers run through the ps5lift runtime's x87, so they round, honour precision control and raise exceptions as the guest's control and status words say. Comparisons, conversions from integers and doubles, `frexp`, `scalbn`, `fmod`, `remainder`, `remquo`, `modf`, `logb` and the fixed-direction roundings work on the bits; a NaN operand or a domain error in `fmod`, `remainder` and `remquo` goes through the x87, so the NaN propagates or invalid is raised as on the guest. - The `_LDint`, `_LDscale`, `_LDunscale`, `_LFpcomp` and `_LPoly` helpers take `GuestLongDouble` too; in lifted builds `_LDscale` and `_LDunscale` scale and split the value on its bits. - `strtold` and `scanf("%Lf")` parse with exact big-integer arithmetic, correctly rounded in the guest's rounding mode. - Builds that are not lifted keep the host's `long double` wherever it is wider than 80 bits too, such as Emscripten's wasm64 target: there `strtold` is still the host's. - `cbrtl` runs FreeBSD's x87 algorithm, as on `main`, on `GuestLongDouble`, and in lifted builds `remainder`, `remainderf`, `remquo` and `remquof` take their result and quotient bits from its exact remainder instead of `fprem1`. - The other transcendental `l` functions and `fmal` still compute in the host's widest type and round the result. The libc debt notes say so, and record that lifted builds don't build `__mulxc3` and `__divxc3` and that long double scans store the guest's format for host callers too. #5021 also changes some of the same files: `Strings.cpp`, `WindowsFormatting.hpp`. ### Tested Compiled for x86-64 Linux before and after this PR (GCC 13, same flags): 137 of the 146 objects it reaches are byte-identical. libSceNet's `Export.cpp` differs only in the generated name of a switch table and `Strings.cpp` in an out-of-line copy of the inline `strtold` wrapper; the other seven are libc's long double, formatting and scanning sources. On a DGX Spark (aarch64 Linux, NVIDIA GB10, LLVM 18), with all eight PRs and ps5lift on top: every library builds for a lifted program, 2,285 of AnyPS5's own tests pass in that build on lavapipe, and ps5lift's 69 tests pass, among them its libc, exceptions, kernel, fiber and module-loading fixtures and upstream's libc test programs. The conventions check passes on this branch. ### 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: #15801 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)

Pull request closed

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!15802
No description provided.