1 /* $Id: fixc.c,v 1.5 2002-10-28 20:12:48 btb Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * C version of fixed point library
20 * Revision 1.7 1995/09/22 14:08:16 allender
21 * fixed fix_atan2 to work correctly with doubles
23 * Revision 1.6 1995/08/31 15:43:49 allender
24 * *** empty log message ***
26 * Revision 1.5 1995/07/05 16:15:15 allender
27 * make fixmuldiv use doubles for PPC implementation
29 * Revision 1.4 1995/05/15 13:57:36 allender
30 * make fixmuldiv compile when compiling under 68k
32 * Revision 1.3 1995/05/11 13:02:59 allender
33 * some routines are now in assembly
35 * Revision 1.2 1995/05/04 20:04:45 allender
36 * use MPW fixdiv if compiling with MPW (why did I do this?)
38 * Revision 1.1 1995/04/17 11:37:54 allender
43 * Revision 1.1 1995/03/08 18:55:09 matt
54 static char rcsid[] = "$Id: fixc.c,v 1.5 2002-10-28 20:12:48 btb Exp $";
65 #pragma message ("warning: FIX NOT INLINED")
67 #warning "FIX NOT INLINED"
71 extern ubyte guess_table[];
72 extern short sincos_table[];
73 extern ushort asin_table[];
74 extern ushort acos_table[];
75 extern fix isqrt_guess_table[];
78 void fixquadnegate(quadint *q)
81 q->high = 0 - q->high - (q->low != 0);
84 //multiply two ints & add 64-bit result to 64-bit sum
85 void fixmulaccum(quadint *q,fix a,fix b)
88 u_int32_t ah,al,bh,bl;
94 aa = labs(a); bb = labs(b);
96 ah = aa>>16; al = aa&0xffff;
97 bh = bb>>16; bl = bb&0xffff;
106 if (q->low < old) q->high++;
110 if (q->low < old) q->high++;
112 q->high += ah*bh + (t>>16) + c;
119 //extract a fix from a quad product
120 fix fixquadadjust(quadint *q)
122 return (q->high<<16) + (q->low>>16);
126 #define EPSILON (F1_0/100)
129 #define QLONG __int64
131 #define QLONG long long
135 fix fixmul(fix a, fix b) {
136 /* return (fix)(((double)a*(double)b)/65536.0);*/
138 asm("imul %%edx; shrd $16,%%edx,%%eax" : "=a" (ret) : "a" (a), "d" (b) : "%edx");
140 return (fix)((((QLONG)a)*b) >> 16);
143 fix fixdiv(fix a, fix b)
145 /* return (fix)(((double)a * 65536.0) / (double)b);*/
146 return (fix)((((QLONG)a) << 16)/b);
148 asm("mov %%eax,%%edx; sar $16,%%edx; shl $16,%%eax; idiv %%ebx" : "=a" (ret) : "a" (a), "b" (b) : "%edx");
152 fix fixmuldiv(fix a, fix b, fix c)
155 asm("imul %%edx; idiv %%ebx" : "=a" (ret) : "a" (a), "d" (b), "b" (c) : "%edx");
160 d = (double)a * (double) b;
161 return (fix)(d / (double) c);
163 return (fix)((((QLONG)a)*b)/c);
167 //given cos & sin of an angle, return that angle.
168 //parms need not be normalized, that is, the ratio of the parms cos/sin must
169 //equal the ratio of the actual cos & sin for the result angle, but the parms
170 //need not be the actual cos & sin.
171 //NOTE: this is different from the standard C atan2, since it is left-handed.
173 fixang fix_atan2(fix cos,fix sin)
175 double d, dsin, dcos;
178 //Assert(!(cos==0 && sin==0));
180 //find smaller of two
184 d = sqrt((dsin * dsin) + (dcos * dcos));
189 if (labs(sin) < labs(cos)) { //sin is smaller, use arcsin
190 t = fix_asin((fix)((dsin / d) * 65536.0));
196 t = fix_acos((fix)((dcos / d) * 65536.0));
204 //divide a quadint by a fix, returning a fix
205 int32_t fixdivquadlong(u_int32_t nl,u_int32_t nh,u_int32_t d)
215 Q = ((nh&0x80000000)!=0);
216 M = ((d&0x80000000)!=0);
221 for (i=0; i<32; i++ ) {
225 T = ((nl&0x80000000L)!=0);
231 Q = (unsigned char)((0x80000000L & nh) != 0 );
232 nh = (nh << 1) | (u_int32_t)T;
240 Q = (unsigned char)(tmp1 == 0);
243 Q = (unsigned char)((0x80000000L & nh) != 0 );
244 nh = (nh << 1) | (u_int32_t)T;
252 Q = (unsigned char)(tmp1 == 0);
260 for (i=0; i<32; i++ ) {
264 T = ((nl&0x80000000L)!=0);
270 Q = (unsigned char)((0x80000000L & nh) != 0 );
271 nh = (nh << 1) | (u_int32_t)T;
279 Q = (unsigned char)(tmp1 == 0);
282 Q = (unsigned char)((0x80000000L & nh) != 0 );
283 nh = (nh << 1) | (u_int32_t)T;
291 Q = (unsigned char)(tmp1 == 0);
304 // this version caused inf loop with:
305 // quad_sqrt(0x27eb7121/*low=669741345*/,
306 // 0x4cd40ad8/*high=1288964824*/);
307 unsigned int fixdivquadlongu(uint nl, uint nh, uint d)
309 return fixdivquadlong((u_int32_t) nl,(u_int32_t) nh,(u_int32_t) d);
313 unsigned int fixdivquadlongu(uint nl, uint nh, uint d)
315 u_int64_t n = (u_int64_t)nl | (((u_int64_t)nh) << 32 );
316 return n / ((u_int64_t)d);
319 #else //of ifdef NO_FIX_INLINE
320 int32_t fixdivquadlong(u_int32_t nl,u_int32_t nh,u_int32_t d) {
324 :"a" (nl), "d" (nh), "r" (d)
329 static inline u_int32_t fixdivquadlongu(u_int32_t nl,u_int32_t nh,u_int32_t d) {
333 :"a" (nl), "d" (nh), "r" (d)
338 #endif //def NO_FIX_INLINE
340 u_int32_t quad_sqrt(u_int32_t low,int32_t high)
349 if (high==0 && (int32_t)low>=0)
350 return long_sqrt((int32_t)low);
352 if (high & 0xff000000) {
353 cnt=12+16; i = high >> 24;
354 } else if (high & 0xff0000) {
355 cnt=8+16; i = high >> 16;
356 } else if (high & 0xff00) {
357 cnt=4+16; i = high >> 8;
362 r = guess_table[i]<<cnt;
364 //quad loop usually executed 4 times
366 r = fixdivquadlongu(low,high,r)/2 + r/2;
367 r = fixdivquadlongu(low,high,r)/2 + r/2;
368 r = fixdivquadlongu(low,high,r)/2 + r/2;
373 t = fixdivquadlongu(low,high,r);
380 } while (!(r==t || r==old_r));
382 t = fixdivquadlongu(low,high,r);
383 //edited 05/17/99 Matt Mueller - tq.high is undefined here.. so set them to = 0
386 fixmulaccum(&tq,r,t);
387 if (tq.low!=low || tq.high!=high)
393 //computes the square root of a long, returning a short
394 ushort long_sqrt(int32_t a)
403 else if (a & 0xff0000)
410 r = guess_table[(a>>cnt)&0xff]<<cnt;
412 //the loop nearly always executes 3 times, so we'll unroll it 2 times and
413 //not do any checking until after the third time. By my calcutations, the
414 //loop is executed 2 times in 99.97% of cases, 3 times in 93.65% of cases,
415 //four times in 16.18% of cases, and five times in 0.44% of cases. It never
416 //executes more than five times. By timing, I determined that is is faster
417 //to always execute three times and not check for termination the first two
418 //times through. This means that in 93.65% of cases, we save 6 cmp/jcc pairs,
419 //and in 6.35% of cases we do an extra divide. In real life, these numbers
420 //might not be the same.
435 } while (!(r==t || r==old_r));
443 //computes the square root of a fix, returning a fix
446 return ((fix) long_sqrt(a)) << 8;
450 //compute sine and cosine of an angle, filling in the variables
451 //either of the pointers can be NULL
453 void fix_sincos(fix a,fix *s,fix *c)
461 ss = sincos_table[i];
462 if (s) *s = (ss + (((sincos_table[i+1] - ss) * f)>>8))<<2;
464 cc = sincos_table[i+64];
465 if (c) *c = (cc + (((sincos_table[i+64+1] - cc) * f)>>8))<<2;
468 //compute sine and cosine of an angle, filling in the variables
469 //either of the pointers can be NULL
471 void fix_fastsincos(fix a,fix *s,fix *c)
477 if (s) *s = sincos_table[i] << 2;
478 if (c) *c = sincos_table[i+64] << 2;
481 //compute inverse sine
482 fixang fix_asin(fix v)
489 if (vv >= f1_0) //check for out of range
496 aa = aa + (((asin_table[i+1] - aa) * f)>>8);
504 //compute inverse cosine
505 fixang fix_acos(fix v)
512 if (vv >= f1_0) //check for out of range
519 aa = aa + (((acos_table[i+1] - aa) * f)>>8);
527 #define TABLE_SIZE 1024
529 //for passed value a, returns 1/sqrt(a)
530 fix fix_isqrt( fix a )
536 if ( a == 0 ) return 0;
538 while( b >= TABLE_SIZE ) {
543 //printf( "Count = %d (%d>>%d)\n", cnt, b, (cnt+1)/2 );
544 r = isqrt_guess_table[b] >> ((cnt+1)/2);
546 //printf( "Initial r = %d\n", r );
548 for (i=0; i<3; i++ ) {
550 r = fixmul( ( (3*65536) - fixmul(fixmul(r,r),a) ), r) / 2;
551 //printf( "r %d = %d\n", i, r );
552 if ( old_r >= r ) return (r+old_r)/2;