faster sin() really faster?

Started by
1 comment, last by StormArmageddon 20 years, 9 months ago
I made a little math function that calculates sin of an angle (in radians). I tested it against the math.h sin function and when you run it once, its twice as fast, but the more times you run them, the math.h sin starts to take less time than mine after about 5 calls. Why is this? Maybe its my code, does anyone see the problem or how I could speed this up? I was think about a table for fa. Is mod (%) slow? __inline double dsin(double x) { double xsq = x*x; double xp = x*xsq; double fa = 1; for(int i=3; i<24; i+=2) { fa = fa*i*(i-1); if((i+1)%4 == 0) x -= xp/fa; else x += xp/fa; xp = xp*xsq; } return x; } I just used a infinite power series to a limited degree, not perfectly accurate, but all I need.
Advertisement
Yes, mod is slow. You can unroll the loop yourself, although an aggressive optimizer may be able to do it.

The other thing is that you are just adding too many terms. If you need that much precission, just use sin(), which is implemented in hardware and much faster than any software implementation you can write. You can only win in speed by sacrificing precission.
you can replace "(i+1) % 4" by "(i+1) & 3"


Everything is better with Metal.

This topic is closed to new replies.

Advertisement