PDA

View Full Version : Sin/Cos lookup tables. How?


Joseph Duchesne
2004.09.06, 07:06 PM
I'm wondering if anyone has any nice speedy sin and cos lookup table code in C lying around?
Thanks :D

Jake
2004.09.06, 07:08 PM
Google is your friend - http://www.google.com/search?hl=en&lr=&ie=UTF-8&q=sine+cosine+lookup+table+code&btnG=Search , 2nd one down

phydeaux
2004.09.06, 07:40 PM
I read a while back that on a lot of newer machines, using tables for sin and cos will actually give you worse performance because you have to hit your machine's memory. Not only that, but some processors now have sin and cos as assembly instructions (don't quote me on this- I have no idea if this is true on PPC machines either.)

Some of this is replicated on the flipcode site on that google search. In any case, I wouldn't waste my time trying to implement this.

Josh
2004.09.06, 08:59 PM
Lookup tables were nice back in the day, but as phydeaux said, they don't help much anymore. And while sin and cos are costly functions, rarely are they a true bottleneck.

Jake
2004.09.06, 09:06 PM
Just use that maclaurin function I gave to you on iChat a while ago, or was that a Tangent, either way you can get one for sin and cos with a google search.

skyhawk
2004.09.06, 09:20 PM
there are also other ways to reduce the number of sin and cos calls. I would suggest doing those before resorting to a lookup table (storing reused values... the mac has PLENTY of registers)

arekkusu
2004.09.06, 10:32 PM
PPC does not have intrinsic trig functions, although the G5 added a real sqrt.

Radeon 9600+ have sin/cos/log/pow/exp/rsq instructions though ;)

Diplomtennis
2004.09.07, 12:31 AM
If you use lookup tables in metal it WILL speed things up enormously.

p.s.: Yeah, I know METAL is not a good example ;)

mnajera
2004.09.14, 05:57 PM
This is something I always wondered about. When I began programming games, the big thing was to have every piece of your code optimized as much as possible. That meant lookup tables for sin/cos and other math functions, and other things, like making sure that your video blitting routines were all in asm. Shift left / right instead of division and multiplication by two.

From what I learned in school; modern compilers are smart enough to do all this for you, and in most cases they do better optimizations than you can do by hand. The compiler knows best how to vectorize loops, and optimize for cache hits, for example.

This is what I've heard, at least, not what I have seen with my own eyes. Has anybody actually tried measuring how well today's systems handle simple things like sin/cos? For example, has anybody measured how many ms it takes a gcc 3.4.1 program to calculate 1,000,000 sin/cos values, versus looking up 1,000,000 values in a table? Or, how long it takes to do 1,000,000 divide by twos versus 1,000,000 shift rights?

Josh
2004.09.15, 12:16 AM
My take on it is that if a few cos/sin calls or multiplications by 2 are your biggest bottleneck, you're in good shape. :)

arekkusu
2004.09.15, 12:59 AM
sin(), sqrt() and related functions have significant overhead on PPC, and Shark will point this out to you. In the general case table lookups are still a win, but you have to determine the minimum precision your application can get away with. If your entire table fits into L1 cache, you win.

The only case where lookups lose is when you're heavily memory bus bound, as in Altivec code. Then you may have the spare cycles to do a series approximation inbetween data fetches.