Skip to content

fix(tests): keep guest_math_exact's pseudo-denormal out of constant folding - #10888

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
krishalivaibhav:fix/macos-math-exact-test
Oct 26, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
krishalivaibhav:fix/macos-math-exact-test

Conversation

@krishalivaibhav

Copy link
Copy Markdown
Contributor

What

guest_math_exact aborted on macOS. The Extended() helper builds an 80-bit pseudo-denormal with memcpy. AppleClang inlined it and folded the result into a normal long double, so _LDtest and __fpclassifyl never saw a pseudo-denormal. The library (MathExact.cpp) is correct.

This PR marks the helper [[gnu::noinline]], so the value is built at run time and the test checks what it was written to check (_LDtest == -1, __fpclassifyl == -2). This is a test-only change.

Tested

macOS: Apple Silicon M2, macOS 27.2, AppleClang, x86_64 Release, on a local integration of main + #10419 + #10366 + #10369 + #7631 + #7804 + #4630 + #10552 + #10853 + #10876 + #10877 + this commit:

  • guest_math_exact passes (it aborted before);
  • ctest -R '^(guest_|libc_|libkernel_)': no new failures.

Linux/Windows: not built locally. The attribute is valid in GCC and Clang and has no effect on the check's meaning.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none to review; building on macOS needs fix(libkernel): build the file, module and rtld calls on macOS hosts #10552 and the PRs above
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

Clang folds the constant built by Extended into a normal long double, so
guest_math_exact never fed a pseudo-denormal to __fpclassifyl on macOS.
Keeping the helper out of line makes the value cross the call at run time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@MarcZX10 MarcZX10 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.

Reviewed the sole test change: preventing the helper from inlining preserves the runtime representation under test, and the same GNU noinline attribute is already used in this codebase. The reported macOS x86_64 run exercises the affected compiler path; the required repository checks pass. No blocking issue found.

@thepoppfish thepoppfish 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.

Reviewed exact head 38a570af49123ae68a79759bc781af3accecbf09. No Windows regression found. This fills the author's untested Windows side; the AppleClang failure remains author-reported rather than independently reproduced here.

Windows 11 build 26200, MSYS2 UCRT64 GCC 16.2.0-6, CMake 4.3.1, Ninja 1.13.2, Release (-O3, no LTO):

  • The complete unmodified GuestMathExact.cpp passes against the actual MathExact.cpp, preserving its repository -frounding-math setting. Initial pass, three repeats (0.02/0.01/0.02 s), and final replay pass.
  • An additional probe directly includes the submitted test source, calls its actual Extended helper and the two actual classification exports, and reads the 10-byte representation. It reports mantissa=8000000000000000 signExponent=0000 LDtest=-1 fpclassifyl=-2. This confirms the pseudo-denormal reaches the consumer with the intended bits on this compiler. The probe also passes three repeats.
  • A scratch counterpart removing only [[gnu::noinline]] also passes the complete test and the representation probe on GCC 16. This is an explicit passing baseline comparison, not a reproduced failing control for the AppleClang issue.
  • WinLibs GCC 15.2.0 accepts the actual test with -std=c++20 -fsyntax-only. No GCC 15 runtime result is claimed: a default-SEH standalone setup hits the existing System V assembly limitation with both test variants; the product's separate DWARF unwind route was not built here.
  • Conventions, whitespace and merge-tree pass against local main b2c7a388406614fb3bc6723a6717a37196d617b8.

The sole attribute prevents this helper from being inlined without changing its byte construction or weakening a classification assertion. GNU's namespaced noinline attribute is consistent with the project's supported GCC/Clang toolchains. The observed Windows behavior retains the expected raw representation and the distinction between _LDtest and __fpclassifyl.

Scope: target-level native Windows validation. The harness uses the actual production MathExact.cpp and full actual test, with no math/classification/floating-point replacements. Its only support is __error returning the host errno pointer, as Process.cpp does, and disabling Windows fault-report UI for aborts. No full shared-libc/product build, independent AppleClang/macOS/Rosetta run, Linux run, sanitizer, title/GPU run or composition of the author's macOS integration stack is claimed.

AI-assisted: yes, OpenAI Codex.

Standalone replay

Place these files outside the checkout and use GCC 16.2.0-6, CMake and Ninja on PATH. REPO points to the exact PR checkout. The passing baseline comparison removes only the attribute into a scratch file; it never modifies the checkout.

cmake -S . -B build16 -G Ninja -DCMAKE_BUILD_TYPE=Release '-DCMAKE_CXX_COMPILER=g++.exe' '-DREPO=C:/path/to/exact-pr-checkout'
cmake --build build16 -j 3
ctest --test-dir build16 --output-on-failure --repeat until-fail:3
./build16/representation_probe.exe

For the passing no-attribute counterpart, adjust make_control.py's checkout path if needed, run it, then configure a separate build with -DTEST_SOURCE=GuestMathExact-inline.cpp and the same REPO/toolchain.

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(AnyPS5MathExactReview LANGUAGES CXX)
enable_testing()
set(REPO "${CMAKE_CURRENT_LIST_DIR}/../review-10888" CACHE PATH "Exact PR checkout")
set(TEST_SOURCE "${REPO}/core/libs/tests/GuestMathExact.cpp" CACHE FILEPATH "Test source under review")
add_executable(guest_math_exact
    "${REPO}/core/libs/prx/libc/src/MathExact.cpp"
    "${TEST_SOURCE}"
    Support.cpp)
target_compile_features(guest_math_exact PRIVATE cxx_std_20)
target_include_directories(guest_math_exact PRIVATE "${REPO}/core/libs")
set_source_files_properties("${REPO}/core/libs/prx/libc/src/MathExact.cpp" PROPERTIES COMPILE_OPTIONS "-frounding-math")
target_compile_options(guest_math_exact PRIVATE -g -Wall -Wextra)
add_custom_command(TARGET guest_math_exact POST_BUILD
    COMMAND "${CMAKE_OBJCOPY}" --rename-section .eh_frame=.ehfram --rename-section .eh_fram=.ehfram "$<TARGET_FILE:guest_math_exact>" VERBATIM)
add_test(NAME guest_math_exact COMMAND guest_math_exact)
set_tests_properties(guest_math_exact PROPERTIES TIMEOUT 30)
add_executable(representation_probe
    "${REPO}/core/libs/prx/libc/src/MathExact.cpp"
    RepresentationProbe.cpp Support.cpp)
target_compile_features(representation_probe PRIVATE cxx_std_20)
target_include_directories(representation_probe PRIVATE "${REPO}/core/libs")
target_compile_definitions(representation_probe PRIVATE GUEST_MATH_EXACT_SOURCE="${TEST_SOURCE}")
target_compile_options(representation_probe PRIVATE -g -Wall -Wextra)
add_custom_command(TARGET representation_probe POST_BUILD
    COMMAND "${CMAKE_OBJCOPY}" --rename-section .eh_frame=.ehfram --rename-section .eh_fram=.ehfram "$<TARGET_FILE:representation_probe>" VERBATIM)
add_test(NAME representation_probe COMMAND representation_probe)
set_tests_properties(representation_probe PROPERTIES TIMEOUT 30)

Support.cpp

#include "prx/libc/include/Api.hpp"
#include <cerrno>
#include <cstdlib>

[[maybe_unused]] const unsigned int abortFlags = _set_abort_behavior(0, _CALL_REPORTFAULT);

extern "C" int* APS5_VABI __error_nid_postfix() {
    return &errno;
}

RepresentationProbe.cpp

#define main RepositoryGuestMathExactMain
#include GUEST_MATH_EXACT_SOURCE
#undef main
#include <cstdio>

int main() {
    const long double value = Extended(0x0000, 0x8000000000000000ull);
    std::uint64_t mantissa = 0;
    std::uint16_t exponent = 0;
    std::memcpy(&mantissa, &value, sizeof(mantissa));
    std::memcpy(&exponent, reinterpret_cast<const unsigned char*>(&value) + 8, sizeof(exponent));
    const auto test = _LDtest_nid_postfix(&value);
    const auto classification = __fpclassifyl_nid_postfix(value);
    std::printf("mantissa=%016llx signExponent=%04x LDtest=%d fpclassifyl=%d\n",
        static_cast<unsigned long long>(mantissa), static_cast<unsigned>(exponent), test, classification);
    return mantissa != 0x8000000000000000ull || exponent != 0 || test != -1 || classification != -2;
}

make_control.py

from pathlib import Path

root = Path(__file__).resolve().parent
source = (root.parent / 'review-10888/core/libs/tests/GuestMathExact.cpp').read_text()
attribute = '[[gnu::noinline]] '
assert source.count(attribute) == 1
(root / 'GuestMathExact-inline.cpp').write_text(source.replace(attribute, ''))

@oneandonlydean oneandonlydean self-assigned this Oct 26, 2026
@oneandonlydean
oneandonlydean merged commit bb61ff9 into boykopovar:main Oct 26, 2026
10 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.

4 participants