Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast implementation of trigonometric functions for c++

Short version: I'd like to know whether there are implementations of the standard trigonometric functions that are faster than the ones included in math.h.

Long version: I got a program that's quite heavy on numerics (it's a physics simulation) and that needs to call trigonometric functions, mostly sin and cos, a lot. Currently I'm simply using the implementations included in math.h. Profiling shows that the calls to these functions cost more than I was expecting (hoping).

While there is most certainly plenty of room for optimization in other parts of the code, having faster sin and cos might give me some additional percent.. So, do you guys have any suggestions?
In another post the usage of self-made lookup tables is suggested. But maybe there are alternatives? Or ready-made and well tested lookup solutions in some libraries?

like image 683
janitor048 Avatar asked Apr 25 '11 09:04

janitor048


1 Answers

Here are some good slides on how to do power series approximations (NOT Taylor series though) of trig functions: Faster Math Functions.

It's geared towards game programmers, which means accuracy gets sacrificed for performance, but you should be able to add another term or two to the approximations to get some of the accuracy back.

The nice thing about this is that you should also be able to extend it to SIMD easily, so that you could compute the sin or cos of 4 values at one (2 if you're using double precision).

Hope that helps...

like image 68
celion Avatar answered Sep 21 '22 14:09

celion