问题
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?
回答1:
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 gave 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
(an probably slower, I believe it has to do a range reduction, use a truncated Taylor expansion or a Padé approximation and than 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 (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. A 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 the x
's digits off to the right).FYL2XP1
won't do x - 1
and won't lose precision.
来源:https://stackoverflow.com/questions/52736011/instruction-fyl2xp1