   My lookup table isn't faster than sine/cosine
  I was bored, and I coded this for amusement not out of necessity.  It returns a value with an approximation always good to 4 decimal places (I used it for some animation stuff and it actually seems to work perfectly with no noticable glitches).  However, I did a simple timing test using gettickcount(), and this is at *least* 10 times slower than using regular sine/cosine from the c runtime library...wow.  Does regular cosf and sinf use lookup tables?trigtable::trigtable(){for(int i = 0; i < 360; i++){cosinetable = 0.0f;sinetable = 0.0f;}for(i = 0; i < 360; i++){cosinetable=cosf(DEG2RAD((float)i));sinetable=sinf(DEG2RAD((float)i));}}//Need to interpolate angles all of the time//also handle when the angle is greater than 360//I don''t know if this produces more assembly than regular sine/cosine, hmm//only works for angles less than 360 and greater than zero floattrigtable::GetCosine(floatdegrees){if(degrees < 0)degrees = -degrees;intceil = ceilf(degrees);intfloor = floorf(degrees);floatden = ceil-floor;if(den) //The degree marker lies between boundaries{floatfrac = (degrees-floor) * (1/(den));returncosinetable[floor]  +((cosinetable[ceil]-cosinetable[floor]) * frac);}else //Degree marker does not lay between boundaries{returncosinetable[(int)degrees];}returncosinetable[0];}floattrigtable::GetSine(floatdegrees){if(degrees < 0){degrees = 360 + degrees;}intceil=ceilf(degrees);intfloor=floorf(degrees);floatden = ceil-floor;if(den){floatfrac=(degrees-floor) * (1/(den));returnsinetable[floor] +((sinetable[ceil]-sinetable[floor])) * frac;}else{returnsinetable[(int)degrees];}return sinetable[0];} </pre>   </i>  
quote:Does regular cosf and sinf use lookup tables?There are fsin, fcos assembly instructions on modern processors. I assume the C runtime functions use them.Muhammad Haggag[edit]Clarification[/edit]   [edited by - Coder on February 15, 2004 12:31:04 AM];  You''ve got a lot of code for a simple sine or cosine lookup.  Of course it''s slower than fsin or fcos.And while it''s probably irrelevant, if you use this:while(degrees < 0)  degrees += 360;while(degrees > 360)  degrees -= 360;  Instead of what you have, the lookups can handle any angle. ;  Using 360 degrees is not a good idea. Rather use 256 or 512 entries for your table and angle &= 255 to wrap the value. ;  I really honestly just did it because I was bored, I never intended it to be functional, but I like the feedback anyway.  ;quote:There are fsin, fcos assembly instructions on modern processors. I assume the C runtime functions use them.Muhammad Haggag[edit]Clarification[/edit] [edited by - Coder on February 15, 2004 12:31:04 AM]fsin and fcos are slow on AMD Athlon XP processors - 97+ clocks.. and I don''t believe SSE/3DNOW has similar functions.I doubt the C libraries are using these instructions- Rockoon;  What?! I was told that pentium1 could do sin/cos ops in around 1cycle! ;  In around 1/2 a cycle with pipelining and a lookup table ;  A lookup table''s going to be a cache miss unless you are doing a lot of lookups together though, especially if you are pushing a lot of other information through the cache at the same time.Writing a good approximation (Taylor / Lagrange) would probably be faster ;  I think it''s those ceils and floors that are killing performance. Doing a fast conversion from a float to int is an absolute necessity in this kind of codes. - Mikko Kauppila 