在继续逆向工程英特尔 8087 浮点单元(FPU)的过程中,[Ken Shirriff] 及其团队研究了其中一个三角函数,具体是 FPTAN。这类逆向工程中最激动人心的部分,或许在于弄清楚实现中采用了何种算法,并尝试推导出最终硬件设计背后的逻辑。
如果你运行的是 6502 或 Z80 这类没有硬件加速功能的简单 MCU 或 MPU,你可能会使用 CORDIC 或类似的算法,因为这类算法仅需要加法、减法、位移和查找表等基本硬件特性。如果有硬件支持以进一步提升速度,也可以使用多项式近似;或者像 8087 那样,采用一种旨在平衡速度与精度的混合方法。
文章详细描述了实现 64 位精度的确切过程:首先使用 CORDIC 计算前 16 位,然后切换到涉及两个多项式之比的 Padé 近似技术。由于在使用 CORDIC 计算出最终数值的大部分之后,剩余部分是一个相对较小的值,因此这种多项式近似不仅非常准确,而且速度很快。
这种方法使得该 FPU 中的 FPTAN 和类似的三角函数能够达到极高的精度,同时无需承担使用 CORDIC 处理剩余位时所要求的庞大查找表大小和额外的时间开销。对于希望查看英特尔工程师使用的完整算法的读者,[Ken] 在文章中也提供了带有注释的完整微代码列表。
至于这种方法带来的确切加速效果,[Ken] 计算出,对于某个数值,FPTAN 将 33% 的时间花在 CORDIC 伪除法上,47% 的时间花在 CORDIC 伪乘法上,仅 15% 的时间用于多项式近似,再加上约 5% 的开销。
在 Pentium 系列 CPU 中,英特尔完全抛弃了 CORDIC,因为很明显,尽管它可能非常精确,但很难在不产生显著时间惩罚的情况下扩展到相当多的位。随着 SIMD 指令集的引入,x87 指令集架构的功能进一步减少,但这一分析再次展示了 8087 在发布时为何会产生如此大的影响。


Continuing their reverse-engineering of Intel’s 8087 FPU, [Ken Shirriff] and friends took a look at one of the trigonometric functions, specifically FPTAN. The most exciting part with such reverse-engineering is probably figuring out which algorithm was used in the implementation, while trying to determine the reasoning behind the final hardware design.
If you’re running a simple MCU or MPU like the 6502 or Z80 without hardware functions you’d likely use an algorithm such as CORDIC or similar, as this requires only basic hardware features like addition, subtraction, bitshift, and look-up tables. One can also use polynomial approximation if there’s hardware support for a potential speed-up, or as is the case in the 8087, create a hybrid approach that targets speed and accuracy.
In the article the exact implementation to get to 64 bits of accuracy is detailed, starting with the 16 bits calculated using CORDIC before switching to the Padé approximant technique involving the ratio of two polynomials. Since after calculating the brunt of the final value with CORDIC the remainder is a fairly small value, this polynomial approximation is not just very accurate but also fast.
This approach allows the FPTAN and similar trigonometric functions in this FPU to hit a very high level of accuracy and not require the look-up table sizes and additional time required to work through the remaining bits with CORDIC. For those who want to see the full algorithm Intel’s engineers used, [Ken] has the full microcode listing with comments in the article as well.
As for the exact speed-up from this approach, [Ken] calculates for one value that FPTAN would spend 33% on CORDIC pseudo-division, 47% on CORDIC pseudo-multiplication and a mere 15% on the polynomial approximation along with about 5% overhead.
With the Pentium series of CPUs Intel moved completely away from CORDIC, as it’s clear that as accurate as it may be, it’s hard to scale to a significant number of bits without incurring significant time penalties. With the introduction of SIMD instructions the x87 ISA has further seen its functionality reduced, but this analysis shows once again why the 8087 made such an impact when it was released.
本文来源:https://hackaday.com/2026-09-27/going-on-a-tangent-with-the-intel-8087s-hybrid-cordic-algorithm/
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。