assemblyx86instruction-setx87

Does FADDP ST(0), ST(1) make sense?


I have a question: does the FADDP ST(0), ST(1) instruction make sense in the assembly language?
As far as I know, in this instruction, we add ST(0) and ST(1) and write the result to ST(0), after which ST(0) gets deleted.


Solution

  • faddp st(0), st(1) is not even encodeable into machine code.

    You're right, it wouldn't be useful (except to raise FP exceptions), that's why the architects of 8087 didn't include an faddp st(0), st(i) opcode. There are opcodes for 3 forms of fadd with a register source:

    So faddp st(0), st(0) is encodeable but not useful, using the faddp st(i), st(0) opcode, where the destination can be any register including the useless case of st(0), but the source is fixed as st(0).


    The cheapest way to pop the stack by 1 without doing anything else is fstp st(0). http://www.ray.masmcode.com/tutorial/fpuchap4.htm#fstp

    (And for instruction costs, see https://agner.org/optimize/. https://uops.info/ didn't test legacy x87 instructions, although it does have some IACA data for some of them.)

    If you really wanted to add a number and discard the result, e.g. to raise FP exceptions before returning from a math library function that used integer bit-manipulation, you can emulate that faddp with

    ; Emulate faddp st(0), st(1), e.g. for raising FP exceptions
       fadd st(0), st(1)
       fstp st(0)