The FNSTSW instruction is very slow on all processors. The PPro, PII and PIII processors have FCOMI instructions to avoid the slow FNSTSW. Using FCOMI instead of the common sequence FCOM / FNSTSW AX / SAHF will save you 8 clock cycles. You should therefore use FCOMI to avoid FNSTSW wherever possible, even in cases where it costs some extra code.
On processors without FCOMI instructions, the usual way of doing floating point comparisons is:
FLD [a] FCOMP [b] FSTSW AX SAHF JB ASmallerThanBYou may improve this code by using FNSTSW AX rather than FSTSW AX and test AH directly rather than using the non-pairable SAHF (TASM version 3.0 has a bug with the FNSTSW AX instruction):
FLD [a] FCOMP [b] FNSTSW AX SHR AH,1 JC ASmallerThanB
Testing for zero or equality:
FTST FNSTSW AX AND AH,40H JNZ IsZero ; (the zero flag is inverted!)
Test if greater:
FLD [a] FCOMP [b] FNSTSW AX AND AH,41H JZ AGreaterThanB
Do not use TEST AH,41H as it is not pairable on PPlain and PMMX.
On the PPlain and PMMX, the FNSTSW instruction takes 2 clocks, but it is delayed for an additional 4 clocks after any floating point instruction because it is waiting for the status word to retire from the pipeline. This delay comes even after FNOP which cannot change the status word, but not after integer instructions. You can fill the latency between FCOM and FNSTSW with integer instructions taking up to four clock cycles. A paired FXCH immediately after FCOM doesn't delay the FNSTSW, not even if the pairing is imperfect:
FCOM ; clock 1 FXCH ; clock 1-2 (imperfect pairing) INC DWORD PTR [EBX] ; clock 3-5 FNSTSW AX ; clock 6-7
You may want to use FCOM rather than FTST here because FTST is not pairable. Remember to include the N in FNSTSW. FSTSW (without N) has a WAIT prefix which delays it further.
It is sometimes faster to use integer instructions for comparing floating point values, as described in chapter 27.6.