Skip to content

[General]: FCOMI/FUCOMI updates comparison flags on unmasked invalid operation #72

Open

Description

@OsamaAlkhodairy

Problem

The shared executable operation writes ZF/PF/CF before testing whether invalid operation is unmasked. The PDF says those three flags are updated only when the invalid-operation exception is masked. The source and PDF therefore specify different comparison flags for the same inputs.

Concrete case

With x87 enabled and no pending exception, compare signaling NaN 0x7fff8000000000000001 in ST(0) with 1 in ST(1), using FCOMI, control word 0x037e and incoming ZF=PF=CF=0. The source writes all three flags to 1, then suppresses result/pop effects. The PDF implies that the incoming comparison flags remain zero. The shared empty-stack branch also writes the flags before considering the mask.

Expected behavior and references

References use Intel SDM 325462-093, checked on 2026-10-01. PDF page numbers below refer to the combined manual.

Source

Source checked at Intel SDM repository commit b5218b0f1cf512304d4b3cb364433a08fcee47cb.

Suggested correction

Follow the SDM's comparison-flag rule by preserving ZF/PF/CF when invalid operation is unmasked. In the empty-stack branch, write these flags only when IM=1; after Compare, write them only when IE=0 or IM=1. Retain the clearing of OF/SF/AF and the existing pop suppression.

Evidence

On AMD EPYC-Milan under KVM, the unmasked-invalid comparison sets ZF/PF/CF to 1, matching the executable operation.

AI disclosure

Assisted by Codex

Activity

  1. tremalrik commented on Oct 8, 2026

    @tremalrik

    For the FCOMI instruction, citing the behavior of an AMD EPYC as evidence is a bit iffy, especially given that AMD and Intel document different flags behavior for this instruction (AMD documents that OF/SF/AF are left unmodified, while Intel documents them as set 0).

    However, in any case, I checked the behavior on both an Intel Core I7-13800H and an AMD Zen3, and: in the case of an unordered compare that causes IE=1, both of them set ZF=PF=CF=1 even when IM=0. The executable specification is correct, and the SDM needs to be fixed.

    (Also, the AMD processor sets OF=SF=AF=0, which is the same as Intel's documented behavior but runs contrary to AMD's documentation)

  2. OsamaAlkhodairy commented on Oct 8, 2026

    @OsamaAlkhodairy
    Author

    Thanks for checking. I have run more tests and agree with your assessment.

  3. added
    SDM PDF issueThis problem is present in the SDM PDF and is/may be present in the website as a consequence
    and removed on Oct 13, 2026
  4. brandonhalesintel commented on Oct 13, 2026

    @brandonhalesintel

    Thanks for the detailed report. We investigated this and also agree with this assessment.

    The executable operation is validated against recorded silicon traces. Those traces contradict the suggested correction. On an FUCOMI ST(0), ST(i) that raises an unmasked invalid-operation exception, the hardware-recorded EFLAGS after the instruction is 0x00000047 - ZF = PF = CF = 1. With the suggested change applied (withholding ZF/PF/CF when IM = 0) the spec produces 0x00000002, and it diverges from hardware on every trace in which an FCOMI-family instruction takes that path.

    This is consistent with the deferred nature of x87 numeric exceptions. An unmasked x87 exception does not abort the instruction that caused it: the instruction completes, records the exception in the x87 status word, sets FPUStatusWord.ES, and the #MF fault is reported later, on the next waiting x87 or WAIT/FWAIT instruction. What is suppressed is the x87-side effect - the register writeback, and the stack pop for the popping forms - which the operation already models via write_result. EFLAGS is not x87 state, so it is written unconditionally.

    There is a genuine discrepancy here. The Table 1-18 note stating that ZF, PF and CF are not updated when the invalid-operation exception is unmasked does not describe the hardware, and is not consistent with the deferred-exception model described in Vol. 1 section 8.7. The correction belongs in that note rather than in the executable operation, so we are re-scoping this as a documentation fix.

    The executable operation is unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    SDM PDF issueThis problem is present in the SDM PDF and is/may be present in the website as a consequence

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions