Skip to content

fix(libc): split modfl as the bundled libc.prx does - #23447

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
DerKarussellbremser:fix/modfl-ld
Nov 19, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
DerKarussellbremser:fix/modfl-ld

Conversation

@DerKarussellbremser

Copy link
Copy Markdown
Contributor

What

modfl handed the work to the host's modf. On Windows that differs from the libc.prx bundled with PPSA10122 01.101.000 (SDK 5.00, modfl at 0x4d200) in two ways:

  • the fraction of a negative integral value came back as +0: modfl(-3.0L, &i) returned +0 where the module, and C, give -0;
  • FE_INEXACT was raised for values that are not integral, where the module raises nothing.

On 200,000 random inputs 20,555 results differed in their bits and 158,891 more only in the flags.

It is now written out after FreeBSD msun's s_modfl.c (release/11.4.0, named above the function): below 1 the integral part is a zero with the sign of x and the fraction is x; from 2^63 up, and for a value without fraction bits, the integral part is x and the fraction a zero with its sign; otherwise the fraction bits are cleared for the integral part and the fraction is the difference, which is exact. A NaN is returned as it is. The integral part is still stored through the checked guest copy.

Tested

Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Release, on main e9ff851.

  • Against the module, with a harness that is not in the repository and calls the module's function natively: 300,000 random inputs at each of the x87 control words 0x37f, 0x27f, 0x7f, 0x77f, 0xb7f and 0xf7f, and 300,000 more at 0x37f, 0x27f and 0xf7f of which a sixth are zeros, infinities, quiet and signaling NaNs, the smallest and largest values, pseudo-denormals and unnormals: 0 differences in the returned bits, the stored integral part and the masked flags.
  • guest_math_exact gets CheckModflPartsAndFlags: eight values with both parts and no flag raised (-3, 3, 2.5, -0.75, -0, -(2^62 + 0.5), -2^63, negative infinity). Red on the base: with MathExact.cpp as on main, guest_math_exact aborts at the fraction of the first case (GuestMathExact.cpp:145, the -0 of modfl(-3)). Green: with the change guest_math_exact, guest_math and guest_math_elementary pass. Trial merges with my open libc pull requests (fix(libc): compute expm1l as the bundled libc.prx does #23265, fix(libc): compute powl as the bundled libc.prx does #23367, fix(libc): raise the flags of the unselected value in roundl and fdiml #23401, fix(libc): generate printf's decimal digits as the bundled libc.prx does #23444) are clean.
  • tools/check_conventions.py --base origin/main and tools/debt_fold.py --check pass. tools/pr_overlap.py --branch fix/modfl-ld: no overlap with open pull requests.
  • Not covered: a console, another title's module, Linux and macOS builds; what their hosts' modf returned was not measured, they now run the same code as Windows.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

https://claude.ai/code/session_011TXsSW8zeAgKgHLCFsYwM3

modfl went to the host's modf, which on Windows returns +0 as the
fraction of a negative integral value and raises FE_INEXACT. It now
follows FreeBSD msun's s_modfl.c, whose results and flags equal those
of the libc.prx bundled with PPSA10122 01.101.000.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011TXsSW8zeAgKgHLCFsYwM3

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against eba24c99dd:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_math_exact_tests built cleanly.
  • CTest (guest_math_exact):
    1/1 Test #1230: guest_math_exact .................   Passed    0.00 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/MathExact.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1230: guest_math_exact .................   Passed    0.00 sec
    100% tests passed, 0 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified modfl_nid_postfix correctly splits integer and fractional parts across exponent ranges (< 0, in [0, 62], >= 63, and special values like Inf/NaN) matching IEEE 754 / x87 extended precision.
    • Verified GuestCopyOut_nid_no_patch is used properly to store the integral part into guest memory.
    • Verified sign preservation for negative zeros and exact flag behavior (no spurious FE_INEXACT).
    • Checked AGENTS.md rules, technical debt fragment at docs/dev/debt/changes/fix-modfl-ld.md, and test coverage in GuestMathExact.cpp.

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit 07f4f8f into boykopovar:main Nov 19, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants