Skip to content

cpu: lower FTST, FXAM, FICOM, FICOMP, FNOP and FNCLEX in the x87 profile - #627

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
danielradosa:cpu/x87-examine-compare
Nov 23, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
danielradosa:cpu/x87-examine-compare

Conversation

@danielradosa

@danielradosa danielradosa commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Change

The x87 leaf-function profile in tools/x87_lower.py now accepts:

  • ftst, which compares ST(0) with +0.0, and ficom and ficomp with an m16 or m32 integer, converted exactly as for fild. All three set C0, C2 and C3 as fcom does, and ficomp pops.
  • fxam, which sets C3, C2 and C0 from the class of the 80-bit encoding through the new x87_examine in tools/x87_soft.h. The classes are unsupported, NaN, normal, infinity, zero and denormal; a pseudo-denormal counts as a denormal. An empty ST(0) is known at translation time and gives C3 and C0. C1, which holds the sign, stays unknown, as it does for every instruction in the profile.
  • fnop and fnclex, which change nothing the profile models. fclex decodes as wait plus fnclex. Like every other x87 instruction, they leave C0, C2 and C3 undefined.

They add 7 x64 forms to the coverage chart: ftst, fxam, ficom, ficomp, fnop, fnclex and fclex.

Verification

cpu_aot_x87_examine (tests/cpu/x87_examine.asm, 4 exports) runs through the native harness, like the other x87 tests. It writes C0, C2 and C3 after each status-word writer and checks them against an independent model, which uses exact rationals for the comparisons and classifies the 80-bit encoding. The inputs cover every fxam class with both signs, including unnormals, pseudo-infinities, pseudo-NaNs and pseudo-denormals. They also cover the m16 and m32 integer bounds and values on either side of an integer. Three mutated controls must mismatch. Translation must reject ftst and ficomp on an empty stack, and a reader of C0 after fnop or fnclex.

Windows 11 build 26300.9974, Ryzen 5 7600, MSVC 19.51.36256, Python 3.14.4, on main at 9b0df70:

ctest --preset dev --no-tests=error -R '^cpu_aot_x87_examine$' -V
Check Result
native and translated DLLs, dev and release (/O2) 782 cases matched the model; every export returned with control 0x027f and an empty stack
control normal (a denormal reported as normal) 5/782 mismatches
control unsigned (the integer converted as unsigned) 136/782 mismatches
control positive (ftst compared with 1.0) 7/782 mismatches
rejections ftst and ficomp on an empty stack; C0 read after fnop and after fnclex
all 19 cpu_aot_x87_ tests, this one included passed
all cpu_ tests, dev 113 of 115 passed; the 2 failures are cpu_native_archive_reader and cpu_native_archive_blast, which need the Zig-built archive_reader.exe (ziglang is not installed on this machine)

The native run confirms the model on the edge cases: Zen 4 reports a pseudo-denormal as denormal and an unnormal as unsupported. Another CPU vendor may differ, which a reviewer's run would show.

Rejection cases in earlier tests

Seven earlier x87 tests used these instructions as examples of forms outside the profile. Their cases now use instructions that stay outside it, the way #513 swapped wait for ffree st(1) in cpu_aot_x87_add. Each test keeps the same number of rejection cases, and no key is duplicated:

Test Was Now
x87_add ftst fsin
x87_sub fxam fcos
x87_integers ficom m16 fyl2xp1
x87_compare ficomp m32, ftst, fxam, ficom m16 fprem1, f2xm1, fptan, fbstp m80
x87_iarith ficom m16, ficomp m32 fbld m80, fbstp m80
x87_fnstcw fnclex fdecstp
x87_status FTST, FXAM, FICOM m16 FSINCOS, FPATAN, FBSTP m80

The docs sentences that listed the old cases are updated to match.

Evidence and follow-up

fstsw ax stays uncovered. It decodes as wait and fnstsw ax, and the profile follows the SDM, under which wait leaves C0, C2 and C3 undefined, so a reader after fstsw would be rejected. fninit/finit change the control word to 0x037F and remain outside the fixed profile.

Companion PRs, independent of this one: fences and cache hints (#626), and cmpxchg8b/cmpxchg16b (#628). x64 coverage goes from 551 (the published chart for 9b0df70) to 573 of 948 with all three.

AI-assisted.

FTST compares ST(0) with +0.0, and FICOM and FICOMP with an m16 or m32
integer converted exactly as for FILD; they set C0, C2 and C3 as FCOM
does, and FICOMP pops. FXAM sets them from the class of the 80-bit
encoding through x87_examine: unsupported, NaN, normal, infinity, zero
and denormal, a pseudo-denormal included; an empty ST(0) is known at
translation time. C1 stays unknown. FNOP and FNCLEX change nothing the
profile models, and FCLEX decodes as WAIT and FNCLEX.

cpu_aot_x87_examine runs 4 exports over 782 cases through the native
harness and matches an exact-rational model in the native and translated
DLLs. Controls that report a denormal as normal, convert the integer as
unsigned and compare FTST with 1.0 gave 5, 136 and 7 mismatches.
Translation rejects FTST and FICOMP on an empty stack and a reader of C0
after FNOP or FNCLEX. Rejection cases in seven earlier x87 tests that
used these instructions now use FSIN, FCOS, FYL2XP1, FPREM1, F2XM1,
FPTAN, FBLD, FBSTP, FDECSTP, FSINCOS and FPATAN, which stay outside the
profile.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T16:24:39.994787Z c91340b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@legacy-code-archaeologist

legacy-code-archaeologist commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Verification of c91340b on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029, dev preset. It merges cleanly with main, and all 115 cpu_ tests pass, including every existing x87 test this PR touches:

ctest --preset dev --no-tests=error -R "x87_(examine|compare|status)" -V
cpu_aot_x87_examine: 782 cases over 4 exports matched the model in native and translated DLLs; FXAM classes ['denormal', 'infinity', 'nan', 'normal', 'unsupported', 'zero'] and empty; each export returned with control 0x027f and an empty stack

This answers the open point from the description, that another vendor may differ from Zen 4: on Intel Skylake the native DLL matches the same model, so it also classifies a pseudo-denormal as denormal and an unnormal as unsupported. All six FXAM classes plus the empty case match.

AI-assisted (Claude Code).

@R0Y00
R0Y00 merged commit cfb2600 into R0Y00:main Nov 23, 2026
5 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