another speed question (sin/cos)

Started by
6 comments, last by apocalexiz 21 years ago
hi and here is my question: is it really faster if i make a sin/cos table instead of calculatig the values? if yes why? bye apo
Advertisement
It may or may not be. Caching can have a large and effect on lookup table performance, and caching isn''t always predictable.

Premature optimization is the root of all evil. Make your program with plain sin/cos first, find out if it''s too slow, find out if it''s sin/cos that''s causing the bottleneck, and if so fix it.
Depending on how accurate you need your values you can gain a hugh speed increase. Just -calling- sin can be slower than getting the result from a table. I personally use wrads (65536 degrees per circle) and create a float look-up array for each value. For games like I write this is fine.

Mark
Cornutopia Games
http://www.cornutopia.net
Bytten Independent Games Magazine
http://www.bytten.com
A 256 KB (if I understand you correctly) lookup table is exactly the kind of thing to avoid - you''re trashing your cache.

1) sin and cos can be had for 100-200 clocks (for both), which is less expensive than constant page misses
2) if your app can tolerate quantizing down to (say) 1024 units per circle, I''d use a LUT
3) try approximating the function yourself.
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
BTW: if you need sin and cos of the same angle use something like:
[source}
static inline void SinCos(float in_angle, float &out_sin, float &out_cos)
{
__asm {
fld in_angle
fsincos
mov edi, out_cos
fstp dword ptr [edi]
mov edi, out_sin
fstp dword ptr [edi]
}
}

static inline void SinCos(double in_angle, double &out_sin, double &out_cos)
{
__asm {
fld in_angle
fsincos
mov edi, out_cos
fstp qword ptr [edi]
mov edi, out_sin
fstp qword ptr [edi]
}
}
[/source]

This is faster than calling sin and cos.

- Andre
Andre Loker | Personal blog on .NET
hmm...working with the fpu right? the angle argument should be RAD? or?

bye

apo
Calculating several terms of the taylor series can give a fast approximation, but you''d be better off looking for something else to optimise.
quote:Original post by apocalexiz
hmm...working with the fpu right? the angle argument should be RAD? or?


yes, radians

- Andre
Andre Loker | Personal blog on .NET

This topic is closed to new replies.

Advertisement