Repository navigation
cpu: lower FTST, FXAM, FICOM, FICOMP, FNOP and FNCLEX in the x87 profile - #627
Conversation
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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Verification of 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). |
Change
The x87 leaf-function profile in
tools/x87_lower.pynow accepts:ftst, which compares ST(0) with +0.0, andficomandficompwith an m16 or m32 integer, converted exactly as forfild. All three set C0, C2 and C3 asfcomdoes, andficomppops.fxam, which sets C3, C2 and C0 from the class of the 80-bit encoding through the newx87_examineintools/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.fnopandfnclex, which change nothing the profile models.fclexdecodes aswaitplusfnclex. 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,fnclexandfclex.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 everyfxamclass 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 rejectftstandficompon an empty stack, and a reader of C0 afterfnoporfnclex.Windows 11 build 26300.9974, Ryzen 5 7600, MSVC 19.51.36256, Python 3.14.4, on
mainat 9b0df70:devandrelease(/O2)normal(a denormal reported as normal)unsigned(the integer converted as unsigned)positive(ftstcompared with 1.0)ftstandficompon an empty stack; C0 read afterfnopand afterfnclexcpu_aot_x87_tests, this one includedcpu_tests,devcpu_native_archive_readerandcpu_native_archive_blast, which need the Zig-builtarchive_reader.exe(ziglangis 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
waitforffree st(1)incpu_aot_x87_add. Each test keeps the same number of rejection cases, and no key is duplicated:x87_addftstfsinx87_subfxamfcosx87_integersficom m16fyl2xp1x87_compareficomp m32,ftst,fxam,ficom m16fprem1,f2xm1,fptan,fbstp m80x87_iarithficom m16,ficomp m32fbld m80,fbstp m80x87_fnstcwfnclexfdecstpx87_statusFTST,FXAM,FICOM m16FSINCOS,FPATAN,FBSTP m80The docs sentences that listed the old cases are updated to match.
Evidence and follow-up
fstsw axstays uncovered. It decodes aswaitandfnstsw ax, and the profile follows the SDM, under whichwaitleaves C0, C2 and C3 undefined, so a reader afterfstswwould be rejected.fninit/finitchange 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.