2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
14 * $Source: /cvs/cvsroot/d2x/maths/fixc.c,v $
15 * $Revision: 1.1.1.1 $
17 * $Date: 2001-01-19 03:29:58 $
19 * C version of fixed point library
21 * $Log: not supported by cvs2svn $
22 * Revision 1.3 1999/10/18 00:31:01 donut
23 * more alpha fixes from Falk Hueffner
25 * Revision 1.2 1999/08/05 22:53:41 sekmu
27 * D3D patch(es) from ADB
29 * Revision 1.1.1.1 1999/06/14 22:13:35 donut
30 * Import of d1x 1.37 source.
32 * Revision 1.7 1995/09/22 14:08:16 allender
33 * fixed fix_atan2 to work correctly with doubles
35 * Revision 1.6 1995/08/31 15:43:49 allender
36 * *** empty log message ***
38 * Revision 1.5 1995/07/05 16:15:15 allender
39 * make fixmuldiv use doubles for PPC implementation
41 * Revision 1.4 1995/05/15 13:57:36 allender
42 * make fixmuldiv compile when compiling under 68k
44 * Revision 1.3 1995/05/11 13:02:59 allender
45 * some routines are now in assembly
47 * Revision 1.2 1995/05/04 20:04:45 allender
48 * use MPW fixdiv if compiling with MPW (why did I do this?)
50 * Revision 1.1 1995/04/17 11:37:54 allender
55 * Revision 1.1 1995/03/08 18:55:09 matt
63 static char rcsid[] = "$Id: fixc.c,v 1.1.1.1 2001-01-19 03:29:58 bradleyb Exp $";
78 #pragma message ("warning: FIX NOT INLINED")
80 #warning "FIX NOT INLINED"
84 extern ubyte guess_table[];
85 extern short sincos_table[];
86 extern ushort asin_table[];
87 extern ushort acos_table[];
88 extern fix isqrt_guess_table[];
91 void fixquadnegate(quad *q)
94 q->high = 0 - q->high - (q->low != 0);
97 //multiply two ints & add 64-bit result to 64-bit sum
98 void fixmulaccum(quad *q,fix a,fix b)
101 u_int32_t ah,al,bh,bl;
107 aa = labs(a); bb = labs(b);
109 ah = aa>>16; al = aa&0xffff;
110 bh = bb>>16; bl = bb&0xffff;
119 if (q->low < old) q->high++;
123 if (q->low < old) q->high++;
125 q->high += ah*bh + (t>>16) + c;
132 //extract a fix from a quad product
133 fix fixquadadjust(quad *q)
135 return (q->high<<16) + (q->low>>16);
139 #define EPSILON (F1_0/100)
142 #define QLONG __int64
144 #define QLONG long long
148 fix fixmul(fix a, fix b) {
149 /* return (fix)(((double)a*(double)b)/65536.0);*/
151 asm("imul %%edx; shrd $16,%%edx,%%eax" : "=a" (ret) : "a" (a), "d" (b) : "%edx");
153 return (fix)((((QLONG)a)*b) >> 16);
156 fix fixdiv(fix a, fix b)
158 /* return (fix)(((double)a * 65536.0) / (double)b);*/
159 return (fix)((((QLONG)a) << 16)/b);
161 asm("mov %%eax,%%edx; sar $16,%%edx; shl $16,%%eax; idiv %%ebx" : "=a" (ret) : "a" (a), "b" (b) : "%edx");
165 fix fixmuldiv(fix a, fix b, fix c)
168 asm("imul %%edx; idiv %%ebx" : "=a" (ret) : "a" (a), "d" (b), "b" (c) : "%edx");
173 d = (double)a * (double) b;
174 return (fix)(d / (double) c);
176 return (fix)((((QLONG)a)*b)/c);
180 //given cos & sin of an angle, return that angle.
181 //parms need not be normalized, that is, the ratio of the parms cos/sin must
182 //equal the ratio of the actual cos & sin for the result angle, but the parms
183 //need not be the actual cos & sin.
184 //NOTE: this is different from the standard C atan2, since it is left-handed.
186 fixang fix_atan2(fix cos,fix sin)
188 double d, dsin, dcos;
191 //Assert(!(cos==0 && sin==0));
193 //find smaller of two
197 d = sqrt((dsin * dsin) + (dcos * dcos));
202 if (labs(sin) < labs(cos)) { //sin is smaller, use arcsin
203 t = fix_asin((fix)((dsin / d) * 65536.0));
209 t = fix_acos((fix)((dcos / d) * 65536.0));
217 //divide a quad by a fix, returning a fix
218 int32_t fixdivquadlong(u_int32_t nl,u_int32_t nh,u_int32_t d)
228 Q = ((nh&0x80000000)!=0);
229 M = ((d&0x80000000)!=0);
234 for (i=0; i<32; i++ ) {
238 T = ((nl&0x80000000L)!=0);
244 Q = (unsigned char)((0x80000000L & nh) != 0 );
245 nh = (nh << 1) | (u_int32_t)T;
253 Q = (unsigned char)(tmp1 == 0);
256 Q = (unsigned char)((0x80000000L & nh) != 0 );
257 nh = (nh << 1) | (u_int32_t)T;
265 Q = (unsigned char)(tmp1 == 0);
273 for (i=0; i<32; i++ ) {
277 T = ((nl&0x80000000L)!=0);
283 Q = (unsigned char)((0x80000000L & nh) != 0 );
284 nh = (nh << 1) | (u_int32_t)T;
292 Q = (unsigned char)(tmp1 == 0);
295 Q = (unsigned char)((0x80000000L & nh) != 0 );
296 nh = (nh << 1) | (u_int32_t)T;
304 Q = (unsigned char)(tmp1 == 0);
316 unsigned int fixdivquadlongu(uint nl, uint nh, uint d)
318 return fixdivquadlong((u_int32_t) nl,(u_int32_t) nh,(u_int32_t) d);
321 int32_t fixdivquadlong(u_int32_t nl,u_int32_t nh,u_int32_t d) {
325 :"a" (nl), "d" (nh), "r" (d)
330 static inline u_int32_t fixdivquadlongu(u_int32_t nl,u_int32_t nh,u_int32_t d) {
334 :"a" (nl), "d" (nh), "r" (d)
341 u_int32_t quad_sqrt(u_int32_t low,int32_t high)
350 if (high==0 && (int32_t)low>=0)
351 return long_sqrt((int32_t)low);
353 if (high & 0xff000000) {
354 cnt=12+16; i = high >> 24;
355 } else if (high & 0xff0000) {
356 cnt=8+16; i = high >> 16;
357 } else if (high & 0xff00) {
358 cnt=4+16; i = high >> 8;
363 r = guess_table[i]<<cnt;
365 //quad loop usually executed 4 times
367 r = (fixdivquadlongu(low,high,r)+r)/2;
368 r = (fixdivquadlongu(low,high,r)+r)/2;
369 r = (fixdivquadlongu(low,high,r)+r)/2;
374 t = fixdivquadlongu(low,high,r);
381 } while (!(r==t || r==old_r));
383 t = fixdivquadlongu(low,high,r);
384 //edited 05/17/99 Matt Mueller - tq.high is undefined here.. so set them to = 0
387 fixmulaccum(&tq,r,t);
388 if (tq.low!=low || tq.high!=high)
394 //computes the square root of a long, returning a short
395 ushort long_sqrt(int32_t a)
404 else if (a & 0xff0000)
411 r = guess_table[(a>>cnt)&0xff]<<cnt;
413 //the loop nearly always executes 3 times, so we'll unroll it 2 times and
414 //not do any checking until after the third time. By my calcutations, the
415 //loop is executed 2 times in 99.97% of cases, 3 times in 93.65% of cases,
416 //four times in 16.18% of cases, and five times in 0.44% of cases. It never
417 //executes more than five times. By timing, I determined that is is faster
418 //to always execute three times and not check for termination the first two
419 //times through. This means that in 93.65% of cases, we save 6 cmp/jcc pairs,
420 //and in 6.35% of cases we do an extra divide. In real life, these numbers
421 //might not be the same.
436 } while (!(r==t || r==old_r));
444 //computes the square root of a fix, returning a fix
447 return ((fix) long_sqrt(a)) << 8;
451 //compute sine and cosine of an angle, filling in the variables
452 //either of the pointers can be NULL
454 void fix_sincos(fix a,fix *s,fix *c)
462 ss = sincos_table[i];
463 if (s) *s = (ss + (((sincos_table[i+1] - ss) * f)>>8))<<2;
465 cc = sincos_table[i+64];
466 if (c) *c = (cc + (((sincos_table[i+64+1] - cc) * f)>>8))<<2;
469 //compute sine and cosine of an angle, filling in the variables
470 //either of the pointers can be NULL
472 void fix_fastsincos(fix a,fix *s,fix *c)
478 if (s) *s = sincos_table[i] << 2;
479 if (c) *c = sincos_table[i+64] << 2;
482 //compute inverse sine
483 fixang fix_asin(fix v)
490 if (vv >= f1_0) //check for out of range
497 aa = aa + (((asin_table[i+1] - aa) * f)>>8);
505 //compute inverse cosine
506 fixang fix_acos(fix v)
513 if (vv >= f1_0) //check for out of range
520 aa = aa + (((acos_table[i+1] - aa) * f)>>8);
528 #define TABLE_SIZE 1024
530 //for passed value a, returns 1/sqrt(a)
531 fix fix_isqrt( fix a )
537 if ( a == 0 ) return 0;
539 while( b >= TABLE_SIZE ) {
544 //printf( "Count = %d (%d>>%d)\n", cnt, b, (cnt+1)/2 );
545 r = isqrt_guess_table[b] >> ((cnt+1)/2);
547 //printf( "Initial r = %d\n", r );
549 for (i=0; i<3; i++ ) {
551 r = fixmul( ( (3*65536) - fixmul(fixmul(r,r),a) ), r) / 2;
552 //printf( "r %d = %d\n", i, r );
553 if ( old_r >= r ) return (r+old_r)/2;