8

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?

Borgcube
  • 83
  • 1
  • 3
  • Isn't sine basically just sine a = opposite/hypotenuse? Should be a simple geometric function, to me. But I do know that the math functions are table-generated a lot of the times. – Nathan M Jan 19 '14 at 13:35
  • 2
    @NathanM that definition is correct but, in order to construct the triangle, you'd need the `sin` function; an obvious circularity. Luckily the trigonometric functions can be evaluated by polynomial expansion which, I believe, is still the way processors do it. – Bathsheba Jan 19 '14 at 13:37
  • Unit circle calculations, maybe? – Nathan M Jan 19 '14 at 13:43
  • @NathanM There are a couple of ways to calculate sine, Taylor series being probably the most common way, and CORDIC algorithm likewise often used, so you should look those up. What I want to know is what specific algorithm are our processors using. I have a hunch it's one of the aforementioned two, but I'd like to know which one. – Borgcube Jan 19 '14 at 13:49
  • Yes, I'm familiar with the Taylor series. It seems like the simplest way to do it. Check the manufacturer's website? If it's an intel processor, a well-constructed google search will probably yield the answer. – Nathan M Jan 19 '14 at 13:52
  • Ahh, sorry then, I thought, based on your first comment, that you aren't. Yes, one would think that such information would be readily available! Alas, I have been having problems with that, which is why I asked this question here. – Borgcube Jan 20 '14 at 00:04
  • Possible duplicate of [How does C compute sin() and other math functions?](https://stackoverflow.com/questions/2284860/how-does-c-compute-sin-and-other-math-functions) – phuclv Aug 14 '18 at 10:22
  • duplicate of [What algorithms do FPUs use to compute transcendental functions?](https://stackoverflow.com/q/13877303/995714) – phuclv Aug 14 '18 at 10:28

1 Answers1

4

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).

Community
  • 1
  • 1
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251