assemblyx86floating-pointinstructionsx87

Instruction FYL2XP1


I'm wondering why the instruction FYL2XP1 on x86-architecture computes exactly the mathematical formula y · log2(x + 1).

What's special with this formula?


Solution

  • The y operand is usually a compile time constant, for the moment forget about the x + 1.

    Since log_b(x) = log_b(2) * log_2(x) the instruction allows to compute the logarithm in any base of x + 1.
    Note that log_b(2) is a constant since it is seldom necessary to compute the logarithm with a degree of freedom in the base.

    FYL2XP1 and FYL2X are the only two x87 instructions that compute the logarithm.
    If the logarithm was an algebraic function a single instruction would suffice, but since it is transcendental, Intel provided two versions.

    FYL2X works on the full domain of the logarithm but it is not perfectly accurate over this full range, particularly for very small values of x (and probably slower, I believe it has to do a range reduction, use a truncated Taylor expansion or a Padé approximation and then improve the accuracy with a table lookup).

    FYL2XP1 instead works only for input in the small range ±( 1 – sqrt(2) ⁄ 2 ).
    This should be faster (because of no range reduction) and more importantly, for the given input range, the approximation method used should have a precision equal or greater than the x87 80-bit floating point precision.

    This instruction provides optimal accuracy for values of epsilon [the value in register ST(0)] that are close to 0. For small epsilon (ε) values, more significant digits can be retained by using the FYL2XP1 instruction than by using (ε+1) as an argument to the FYL2X instruction.

    @Mysticial's comment is a spot on:
    The algorithm used by FYL2X is probably using, after all the other necessary steps, an approximation formula for log(x + 1).
    To transform this into a formula for log(x) the input must be subtracted by one. An x - 1 operation will lose precision if x is very small (because the big difference in the exponents of the two numbers will shift most of x's digits off to the right).
    FYL2XP1 doesn't do x - 1 and hence won't lose precision.