Repository navigation
cpu: lower FSQRT in the x87 profile - #460
Merged
Merged
Conversation
The opt-in x87 leaf profile now accepts FSQRT. The helper takes the 64-bit integer root of the scaled significand, folds the remainder into the sticky bit and rounds to 53 bits in the extended exponent range. cpu_aot_x87_sqrt compares 9 native and translated exports against an integer-root model, including inputs next to rounding ties, with six negative controls. AI-assisted (Claude Code).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change
Refs #406. This follows #456 (comparisons). It adds
fsqrtto the opt-in x87 profile inlower(..., x87=True).Semantics.
st(0)is rounded under the profile's control word, to 53 significand bits and to nearest-even, within the extended exponent range. The result never leaves the extended normal range, so no overflow or underflow handling is needed.Out of scope: the transcendental instructions (
fsin,fcos,fsincos,fptan,fpatan,f2xm1,fyl2x,fyl2xp1),fprem,fprem1,fscaleandfxtract. They remain rejected.Lowering (
tools/x87_lower.py).x87_slot[top] = x87_sqrt(x87_slot[top]);, with an empty stack rejected as an underflow.Helper (
tools/x87_soft.h).x87_sqrtcomputes the root bitwise in integers:_umul128.x87_roundwith the arithmetic format.Fixture
tests/cpu/x87_sqrt.asmand.defare generated bytests/cpu/x87_sqrt.py asm, andemitchecks that every decoded x87 instruction is the form its sequence names. The.asmregisters its test on its first line (; coverage: source cpu_aot_x87_sqrt). The 9 exports cover:sqrt(b) + a, andsqrt(a * a + b * b)(built fromfld st(0),fmulpandfaddp);Model. The model shares no code with
tools/x87_soft.h. It takes an integer square root with about 160 bits, scaled to the operand's magnitude. An inexact root is replaced by the midpoint of its unit interval, which rounds the same way, and the result is then rounded to 53 bits. The arithmetic exports reuse theFractionmodels of the add and multiply tests. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs.Cases. 13083 cases:
Negative controls. Six mutated translations must each mismatch:
pc64doublenostickynegzeronegabssnannostickyis the control that the near-tie cases exist for. An ignored remainder changes a rounded root only when the 11 bits below the 53rd are exactly a half, which random inputs rarely hit.Rejections. Translation rejects 6 sequences with the expected diagnostic:
fsqrton an empty stack,fsqrtwhose result is left on the stack at the return,fsin,fsincos,fnsaveandfrstor.Changes to existing tests.
cpu_aot_x87_divusedfsqrtas a rejection case. It becomesfnsave, an environment save that the profile will not lower, so the test keeps its number of rejections. Its case and control counts are unchanged.Verification
Tested at
44b3707on6804d0d(the head of #456).mainhas since moved to2bdb504(the merge of #456, #327 and #459), and none of those touch the x87 files ortools/ffx_version_query.py. This branch merges cleanly into2bdb504. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD).x87_add,x87_mul,x87_div,x87_sub,x87_moves,x87_constants,x87_integers,x87_compare,x87_sqrt).ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xmlgives 183 passed, 1 failed and 2 capability skips out of 186. The run includes the GPU tests. The failure isgpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in gpu_alias_lookup_writes_hardware: array atomics probe reads 256 per buffer on RX 9070 XT #369, which fails the same way onmain. This change touches no GPU code.tests/form_coverage_check.py,tests/docs_links_check.pyandtests/bidi_controls_check.pypass./O2build-and-test, Linux Python and gfx1013 LLVM jobs passed at44b3707in the fork'sWindows checksrun 44987342880. Both Windows jobs ran and passed all nine x87 tests (158 of 158 tests in each).Coverage.
python tools/form_coverage.py out/build/dev/test-results.xmlgives x64 158 of 948 forms, and 157 withoutcpu_aot_x87_sqrt. The added form isfsqrt.Not measured:
fldcw.Overlap
I trial-merged this branch with all 88 open PRs. Each one conflicts in the same files against
mainand against this branch: the 16 that merge cleanly intomainalso merge cleanly here, and this branch adds no conflict to the others. The changes stay inside the x87 files, the new fixture, one rejection case oftests/cpu/x87_div.py, a CMake block after thecpu_aot_x87_comparetest, and the x87 section ofdocs/experiments.md.Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the square root, the near-tie cases, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).