How are sin and cos implemented hardware wise?

五迷三道 提交于 2019-12-10 21:02:42

问题


I have been doing some research as to how sine and cosine can be calculated. I found a couple of "standard" methods, including a lookup table, a CORDIC algorithm, and Taylor series. I also found that most modern processors have an assembler instruction calculating trigonometric functions. What I want to know is how those commands work.

So, my question is: What specific algorithm do current gen processors use for calculating sine and cosine?


回答1:


The answer to a related, but different question here talks of how FPUs perform such instructions:

Once you've reduced your argument, most chips use a CORDIC algorithm to compute the sines and cosines. You may hear people say that computers use Taylor series. That sounds reasonable, but it's not true. The CORDIC algorithms are much better suited to efficient hardware implementation. (Software libraries may use Taylor series, say on hardware that doesn't support trig functions.) There may be some additional processing, using the CORDIC algorithm to get fairly good answers but then doing something else to improve accuracy.

Note though that it says "most chips", as attempts to improve performance, accuracy or (ideally) both would obviously be something that chip manufacturers strive for, and so, there will be differences between them.

Those differences my well lead to greater performance at the cost of less accuracy, or vice-versa (and of course, they can just be plain bad at both, since we live in an imperfect world) so there would be times when one might favour performing the algorithm in the CPU (as would happen if you coded the algorithm yourself) rather than in the FPU like fsin passes to.

This archived blog post talks of how Sun's implementation of the JVM on Intel only uses a plain call to fsin with inputs of a certain range, because of flaws in that implementation. The paper linked to from that article presumably discusses that implementation of fsin, and it's issues, in more detail, but you'll need to be a subscriber or pay to read that article (which I have hence not done).



来源:https://stackoverflow.com/questions/21217418/how-are-sin-and-cos-implemented-hardware-wise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!