fix(libc): wrap div, ldiv, lldiv and imaxdiv on a -1 divisor #16210

Merged
MarcZX10 merged 6 commits from upstream-pr/16210 into main 2026-11-09 03:53:00 +00:00
MarcZX10 commented 2026-11-05 20:50:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-05T20:50:44Z.


What

  • div, ldiv, lldiv and imaxdiv with a divisor of -1 and the most negative numerator (INT_MIN / INT64_MIN) divide with a quotient that does not fit the result type. C leaves that undefined, and the x86-64 host idiv faults on it. Fix: a divisor of -1 returns the two's-complement negation of the numerator (computed unsigned, then cast) with a remainder of 0. This is what wrapping arithmetic gives and avoids the host trap.
  • A zero divisor is not changed; it still goes to the standard division, as before.
  • Commits: fix for div (Strings.cpp), fix for ldiv/lldiv/imaxdiv (Math.cpp), test rows for each (GuestDivisionPrxAbi.cpp), and a docs(debt) entry in docs/dev/debt/libc.md recording that the console result for INT_MIN / -1 and INT64_MIN / -1 has not been measured.

Tested

  • Not yet built or run: the focused test guest_division_prx_abi_tests is queued, not yet run.
  • Run directly: python3 tools/check_conventions.py --base upstream/main (passed), git diff --check upstream/main (clean).
  • The public header audit (test_public_headers.py --audit-only) was run on this branch; no export declarations change.

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:5178519088 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16210). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-05T20:50:44Z. --- ### What - `div`, `ldiv`, `lldiv` and `imaxdiv` with a divisor of -1 and the most negative numerator (`INT_MIN` / `INT64_MIN`) divide with a quotient that does not fit the result type. C leaves that undefined, and the x86-64 host `idiv` faults on it. Fix: a divisor of -1 returns the two's-complement negation of the numerator (computed unsigned, then cast) with a remainder of 0. This is what wrapping arithmetic gives and avoids the host trap. - A zero divisor is not changed; it still goes to the standard division, as before. - Commits: `fix` for `div` (Strings.cpp), `fix` for `ldiv`/`lldiv`/`imaxdiv` (Math.cpp), `test` rows for each (GuestDivisionPrxAbi.cpp), and a `docs(debt)` entry in `docs/dev/debt/libc.md` recording that the console result for `INT_MIN / -1` and `INT64_MIN / -1` has not been measured. ### Tested - Not yet built or run: the focused test `guest_division_prx_abi_tests` is queued, not yet run. - Run directly: `python3 tools/check_conventions.py --base upstream/main` (passed), `git diff --check upstream/main` (clean). - The public header audit (`test_public_headers.py --audit-only`) was run on this branch; no export declarations change. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!16210
No description provided.