cpu: lower FCOMI, FCOMIP, FUCOMI and FUCOMIP in the x87 profile #450
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/456"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T04:46:07Z.
Change
Refs #406. This follows #452 (integer conversions). It adds the comparisons into EFLAGS to the opt-in x87 profile in
lower(..., x87=True):fcomi,fcomip,fucomiandfucomipwithst, st(i). Thepforms pop after comparing.Flags. A comparison sets the two flags the integer lowering models:
st(0)<st(i)st(0)>st(i)With invalid operations masked,
fcomiandfucomigive the same flags. All 64 significand bits count, +0 equals -0, and a pseudo-denormal equals the normal value with the same significand at biased exponent 1.PF is not modelled. The integer lowering already rejects every instruction that reads PF, SF, OF or the whole flags register, so nothing in the profile can observe the flags it does not model. As a consequence, the usual compiler test for floating-point equality (
fucomip; jp; jne) stays outside the profile. Supporting it needs a parity flag in the integer lowering, which is a separate change.Out of scope: the comparisons into the status word (
fcom,fucom,fucompp,ftst,fxam,ficom) andfnstsw, which nothing in the profile could read. They remain rejected.Lowering (
tools/x87_lower.py).{ const int r = x87_compare(x87_slot[top], x87_slot[k]); zero = r == 1 || r == 3; carry = r == 0 || r == 3; }. It writes the samezeroandcarryvariables as the integer compares.st(i)operand at or beyond the current depth is an underflow.fcomipandfucomiplower the depth.Helper (
tools/x87_soft.h).x87_compare(a, b)returns 0 for less, 1 for equal, 2 for greater and 3 for unordered. It handles unordered operands first, then zeros of either sign, then opposite signs, and then compares the unpacked exponent and significand. Infinities unpack above every finite value.Fixture
tests/cpu/x87_compare.asmand.defare generated bytests/cpu/x87_compare.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_compare). The 11 exports cover:st(1);fcomi st, st(2)andfucomip st, st(2);fucomi st, st(0)andfcomip st, st(0);Every export stores the values left on the stack.
Flag record. After each comparison, a 20-byte record reads the flags through the integer lowering's own flag readers, with only flag-neutral
movs in between:je,jne,ja,jae,jbandjbe, each as a taken byte;sete;cmove,cmovneandcmovbe, each as a dword;rclof a zeroed register.rclrotates CF in and changes it, so it comes last and CF is also observed on its own.loopeandloopne, which also read ZF, are not used.Model. The model compares with
Fractionarithmetic and shares no code withtools/x87_soft.h. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs.Cases. 11913 cases. The first comparison of each case gives 3489 less, 2008 equal, 3520 greater and 2896 unordered. They cover:
Negative controls. Six mutated translations must each mismatch:
unordeqswapzeroorderlow11rawexpunnormalRejections. Translation rejects 19 sequences with the expected diagnostic:
jp,jnp,setp,cmovp,js,jl,lahf,sahfandpushfqafterfucomi, with the integer lowering's "unsupported query operation";fcomi st, st(1)with one value,fucomip st, st(2)with two, andfcomipon an empty stack;fcom,fucom,fucompp,ftst,fxam,ficomandfnstsw.Changes to existing tests: none. The
fcomandfcomprejection cases of the earlier x87 tests stay valid.Verification
Tested at
6804d0don7cca3d1.mainhas since moved to3a806d3(#455), which does not touch the x87 files ortools/ffx_version_query.py. This branch merges cleanly into3a806d3. 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).ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xmlgives 182 passed, 1 failed and 2 capability skips out of 185. The run includes the GPU tests. The failure isgpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in #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 at6804d0din the fork'sWindows checksrun 44986105043. Both Windows jobs ran and passed all eight x87 tests (157 of 157 tests in each).Coverage.
python tools/form_coverage.py out/build/dev/test-results.xmlgives x64 157 of 948 forms, and 153 withoutcpu_aot_x87_compare. The added forms arefcomi,fcomip,fucomiandfucomip.Not measured:
fcomiclears, because the integer lowering does not model them.fcomiraises for a QNaN andfucomidoes not.Overlap
I trial-merged this branch with all 89 open PRs. Each one conflicts in the same files against
mainand against this branch: the 17 that merge cleanly intomainalso merge cleanly here, and this branch adds no conflict to the others.Two open PRs change the flag model that this PR writes into:
overflowflag to the integer lowering, withsetoas a reader.fcomiclears OF, so if #335 lands first, the comparison statement here must also setoverflow = false. Otherwisesetoafterfcomiwould read a stale value. Whichever PR lands second should add that assignment.zeroandcarry. They read the flags this PR writes in the same way as the readers that already exist, so the two PRs compose.Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the comparisons, the flag mapping, the PF limitation, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).