1 /* $Id: fixc.c,v 1.7 2004-08-28 23:17:45 schaffner 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
26 static char rcsid[] = "$Id: fixc.c,v 1.7 2004-08-28 23:17:45 schaffner Exp $";
37 #pragma message ("warning: FIX NOT INLINED")
39 // #warning "FIX NOT INLINED" fixc is now stable
43 extern ubyte guess_table[];
44 extern short sincos_table[];
45 extern ushort asin_table[];
46 extern ushort acos_table[];
47 extern fix isqrt_guess_table[];
50 void fixquadnegate(quadint *q)
53 q->high = 0 - q->high - (q->low != 0);
56 //multiply two ints & add 64-bit result to 64-bit sum
57 void fixmulaccum(quadint *q,fix a,fix b)
60 u_int32_t ah,al,bh,bl;
66 aa = labs(a); bb = labs(b);
68 ah = aa>>16; al = aa&0xffff;
69 bh = bb>>16; bl = bb&0xffff;
78 if (q->low < old) q->high++;
82 if (q->low < old) q->high++;
84 q->high += ah*bh + (t>>16) + c;
91 //extract a fix from a quad product
92 fix fixquadadjust(quadint *q)
94 return (q->high<<16) + (q->low>>16);
98 #define EPSILON (F1_0/100)
101 #define QLONG __int64
103 #define QLONG long long
107 fix fixmul(fix a, fix b) {
108 /* return (fix)(((double)a*(double)b)/65536.0);*/
110 asm("imul %%edx; shrd $16,%%edx,%%eax" : "=a" (ret) : "a" (a), "d" (b) : "%edx");
112 return (fix)((((QLONG)a)*b) >> 16);
115 fix fixdiv(fix a, fix b)
117 /* return (fix)(((double)a * 65536.0) / (double)b);*/
118 return (fix)((((QLONG)a) << 16)/b);
120 asm("mov %%eax,%%edx; sar $16,%%edx; shl $16,%%eax; idiv %%ebx" : "=a" (ret) : "a" (a), "b" (b) : "%edx");
124 fix fixmuldiv(fix a, fix b, fix c)
127 asm("imul %%edx; idiv %%ebx" : "=a" (ret) : "a" (a), "d" (b), "b" (c) : "%edx");
132 d = (double)a * (double) b;
133 return (fix)(d / (double) c);
135 return (fix)((((QLONG)a)*b)/c);
139 //given cos & sin of an angle, return that angle.
140 //parms need not be normalized, that is, the ratio of the parms cos/sin must
141 //equal the ratio of the actual cos & sin for the result angle, but the parms
142 //need not be the actual cos & sin.
143 //NOTE: this is different from the standard C atan2, since it is left-handed.
145 fixang fix_atan2(fix cos,fix sin)
147 double d, dsin, dcos;
150 //Assert(!(cos==0 && sin==0));
152 //find smaller of two
156 d = sqrt((dsin * dsin) + (dcos * dcos));
161 if (labs(sin) < labs(cos)) { //sin is smaller, use arcsin
162 t = fix_asin((fix)((dsin / d) * 65536.0));
168 t = fix_acos((fix)((dcos / d) * 65536.0));
176 //divide a quadint by a fix, returning a fix
177 int32_t fixdivquadlong(u_int32_t nl,u_int32_t nh,u_int32_t d)
187 Q = ((nh&0x80000000)!=0);
188 M = ((d&0x80000000)!=0);
193 for (i=0; i<32; i++ ) {
197 T = ((nl&0x80000000L)!=0);
203 Q = (unsigned char)((0x80000000L & nh) != 0 );
204 nh = (nh << 1) | (u_int32_t)T;
212 Q = (unsigned char)(tmp1 == 0);
215 Q = (unsigned char)((0x80000000L & nh) != 0 );
216 nh = (nh << 1) | (u_int32_t)T;
224 Q = (unsigned char)(tmp1 == 0);
232 for (i=0; i<32; i++ ) {
236 T = ((nl&0x80000000L)!=0);
242 Q = (unsigned char)((0x80000000L & nh) != 0 );
243 nh = (nh << 1) | (u_int32_t)T;
251 Q = (unsigned char)(tmp1 == 0);
254 Q = (unsigned char)((0x80000000L & nh) != 0 );
255 nh = (nh << 1) | (u_int32_t)T;
263 Q = (unsigned char)(tmp1 == 0);
276 // this version caused inf loop with:
277 // quad_sqrt(0x27eb7121/*low=669741345*/,
278 // 0x4cd40ad8/*high=1288964824*/);
279 unsigned int fixdivquadlongu(uint nl, uint nh, uint d)
281 return fixdivquadlong((u_int32_t) nl,(u_int32_t) nh,(u_int32_t) d);
285 unsigned int fixdivquadlongu(uint nl, uint nh, uint d)
287 u_int64_t n = (u_int64_t)nl | (((u_int64_t)nh) << 32 );
288 return n / ((u_int64_t)d);
291 #else //of ifdef NO_FIX_INLINE
292 int32_t fixdivquadlong(u_int32_t nl,u_int32_t nh,u_int32_t d) {
296 :"a" (nl), "d" (nh), "r" (d)
301 static inline u_int32_t fixdivquadlongu(u_int32_t nl,u_int32_t nh,u_int32_t d) {
305 :"a" (nl), "d" (nh), "r" (d)
310 #endif //def NO_FIX_INLINE
312 u_int32_t quad_sqrt(u_int32_t low,int32_t high)
321 if (high==0 && (int32_t)low>=0)
322 return long_sqrt((int32_t)low);
324 if (high & 0xff000000) {
325 cnt=12+16; i = high >> 24;
326 } else if (high & 0xff0000) {
327 cnt=8+16; i = high >> 16;
328 } else if (high & 0xff00) {
329 cnt=4+16; i = high >> 8;
334 r = guess_table[i]<<cnt;
336 //quad loop usually executed 4 times
338 r = fixdivquadlongu(low,high,r)/2 + r/2;
339 r = fixdivquadlongu(low,high,r)/2 + r/2;
340 r = fixdivquadlongu(low,high,r)/2 + r/2;
345 t = fixdivquadlongu(low,high,r);
352 } while (!(r==t || r==old_r));
354 t = fixdivquadlongu(low,high,r);
355 //edited 05/17/99 Matt Mueller - tq.high is undefined here.. so set them to = 0
358 fixmulaccum(&tq,r,t);
359 if (tq.low!=low || tq.high!=high)
365 //computes the square root of a long, returning a short
366 ushort long_sqrt(int32_t a)
375 else if (a & 0xff0000)
382 r = guess_table[(a>>cnt)&0xff]<<cnt;
384 //the loop nearly always executes 3 times, so we'll unroll it 2 times and
385 //not do any checking until after the third time. By my calcutations, the
386 //loop is executed 2 times in 99.97% of cases, 3 times in 93.65% of cases,
387 //four times in 16.18% of cases, and five times in 0.44% of cases. It never
388 //executes more than five times. By timing, I determined that is is faster
389 //to always execute three times and not check for termination the first two
390 //times through. This means that in 93.65% of cases, we save 6 cmp/jcc pairs,
391 //and in 6.35% of cases we do an extra divide. In real life, these numbers
392 //might not be the same.
407 } while (!(r==t || r==old_r));
415 //computes the square root of a fix, returning a fix
418 return ((fix) long_sqrt(a)) << 8;
422 //compute sine and cosine of an angle, filling in the variables
423 //either of the pointers can be NULL
425 void fix_sincos(fix a,fix *s,fix *c)
433 ss = sincos_table[i];
434 if (s) *s = (ss + (((sincos_table[i+1] - ss) * f)>>8))<<2;
436 cc = sincos_table[i+64];
437 if (c) *c = (cc + (((sincos_table[i+64+1] - cc) * f)>>8))<<2;
440 //compute sine and cosine of an angle, filling in the variables
441 //either of the pointers can be NULL
443 void fix_fastsincos(fix a,fix *s,fix *c)
449 if (s) *s = sincos_table[i] << 2;
450 if (c) *c = sincos_table[i+64] << 2;
453 //compute inverse sine
454 fixang fix_asin(fix v)
461 if (vv >= f1_0) //check for out of range
468 aa = aa + (((asin_table[i+1] - aa) * f)>>8);
476 //compute inverse cosine
477 fixang fix_acos(fix v)
484 if (vv >= f1_0) //check for out of range
491 aa = aa + (((acos_table[i+1] - aa) * f)>>8);
499 #define TABLE_SIZE 1024
501 //for passed value a, returns 1/sqrt(a)
502 fix fix_isqrt( fix a )
508 if ( a == 0 ) return 0;
510 while( b >= TABLE_SIZE ) {
515 //printf( "Count = %d (%d>>%d)\n", cnt, b, (cnt+1)/2 );
516 r = isqrt_guess_table[b] >> ((cnt+1)/2);
518 //printf( "Initial r = %d\n", r );
520 for (i=0; i<3; i++ ) {
522 r = fixmul( ( (3*65536) - fixmul(fixmul(r,r),a) ), r) / 2;
523 //printf( "r %d = %d\n", i, r );
524 if ( old_r >= r ) return (r+old_r)/2;