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.
16 * C version of fixed point library
32 #pragma message ("warning: FIX NOT INLINED")
34 // #warning "FIX NOT INLINED" fixc is now stable
38 extern ubyte guess_table[];
39 extern short sincos_table[];
40 extern ushort asin_table[];
41 extern ushort acos_table[];
42 extern fix isqrt_guess_table[];
45 void fixquadnegate(quadint *q)
48 q->high = 0 - q->high - (q->low != 0);
51 //multiply two ints & add 64-bit result to 64-bit sum
52 void fixmulaccum(quadint *q,fix a,fix b)
55 uint32_t ah, al, bh, bl;
56 uint32_t t, c = 0, old;
61 aa = (uint32_t)labs(a); bb = (uint32_t)labs(b);
63 ah = aa>>16; al = aa&0xffff;
64 bh = bb>>16; bl = bb&0xffff;
73 if (q->low < old) q->high++;
77 if (q->low < old) q->high++;
79 q->high += ah*bh + (t>>16) + c;
86 //extract a fix from a quad product
87 fix fixquadadjust(quadint *q)
89 return (q->high<<16) + (q->low>>16);
93 #define EPSILON (F1_0/100)
98 #define QLONG long long
102 fix fixmul(fix a, fix b) {
103 /* return (fix)(((double)a*(double)b)/65536.0);*/
105 asm("imul %%edx; shrd $16,%%edx,%%eax" : "=a" (ret) : "a" (a), "d" (b) : "%edx");
107 return (fix)((((QLONG)a)*b) >> 16);
110 fix fixdiv(fix a, fix b)
112 /* return (fix)(((double)a * 65536.0) / (double)b);*/
113 return (fix)((((QLONG)a) << 16)/b);
115 asm("mov %%eax,%%edx; sar $16,%%edx; shl $16,%%eax; idiv %%ebx" : "=a" (ret) : "a" (a), "b" (b) : "%edx");
119 fix fixmuldiv(fix a, fix b, fix c)
122 asm("imul %%edx; idiv %%ebx" : "=a" (ret) : "a" (a), "d" (b), "b" (c) : "%edx");
127 d = (double)a * (double) b;
128 return (fix)(d / (double) c);
130 return (fix)((((QLONG)a)*b)/c);
134 //given cos & sin of an angle, return that angle.
135 //parms need not be normalized, that is, the ratio of the parms cos/sin must
136 //equal the ratio of the actual cos & sin for the result angle, but the parms
137 //need not be the actual cos & sin.
138 //NOTE: this is different from the standard C atan2, since it is left-handed.
140 fixang fix_atan2(fix cos,fix sin)
142 double d, dsin, dcos;
145 //Assert(!(cos==0 && sin==0));
147 //find smaller of two
151 d = sqrt((dsin * dsin) + (dcos * dcos));
156 if (labs(sin) < labs(cos)) { //sin is smaller, use arcsin
157 t = fix_asin((fix)((dsin / d) * 65536.0));
163 t = fix_acos((fix)((dcos / d) * 65536.0));
171 //divide a quadint by a fix, returning a fix
172 int32_t fixdivquadlong(uint32_t nl, uint32_t nh, uint32_t d)
182 Q = ((nh&0x80000000)!=0);
183 M = ((d&0x80000000)!=0);
188 for (i=0; i<32; i++ ) {
192 T = ((nl&0x80000000L)!=0);
198 Q = (unsigned char)((0x80000000L & nh) != 0 );
199 nh = (nh << 1) | (uint32_t)T;
207 Q = (unsigned char)(tmp1 == 0);
210 Q = (unsigned char)((0x80000000L & nh) != 0 );
211 nh = (nh << 1) | (uint32_t)T;
219 Q = (unsigned char)(tmp1 == 0);
227 for (i=0; i<32; i++ ) {
231 T = ((nl&0x80000000L)!=0);
237 Q = (unsigned char)((0x80000000L & nh) != 0 );
238 nh = (nh << 1) | (uint32_t)T;
246 Q = (unsigned char)(tmp1 == 0);
249 Q = (unsigned char)((0x80000000L & nh) != 0 );
250 nh = (nh << 1) | (uint32_t)T;
258 Q = (unsigned char)(tmp1 == 0);
271 // this version caused inf loop with:
272 // quad_sqrt(0x27eb7121/*low=669741345*/,
273 // 0x4cd40ad8/*high=1288964824*/);
274 unsigned int fixdivquadlongu(uint nl, uint nh, uint d)
276 return fixdivquadlong((uint32_t) nl,(uint32_t) nh,(uint32_t) d);
280 unsigned int fixdivquadlongu(uint nl, uint nh, uint d)
282 uint64_t n = (uint64_t)nl | (((uint64_t)nh) << 32 );
283 return (unsigned int)(n / ((uint64_t)d));
286 #else //of ifdef NO_FIX_INLINE
287 int32_t fixdivquadlong(uint32_t nl, uint32_t nh, uint32_t d)
292 :"a" (nl), "d" (nh), "r" (d)
297 static inline uint32_t fixdivquadlongu(uint32_t nl, uint32_t nh, uint32_t d)
302 :"a" (nl), "d" (nh), "r" (d)
307 #endif //def NO_FIX_INLINE
309 uint32_t quad_sqrt(uint32_t low, int32_t high)
312 uint32_t r, old_r, t;
318 if (high==0 && (int32_t)low>=0)
319 return long_sqrt((int32_t)low);
321 if (high & 0xff000000) {
322 cnt=12+16; i = high >> 24;
323 } else if (high & 0xff0000) {
324 cnt=8+16; i = high >> 16;
325 } else if (high & 0xff00) {
326 cnt=4+16; i = high >> 8;
331 r = guess_table[i]<<cnt;
333 //quad loop usually executed 4 times
335 r = fixdivquadlongu(low,high,r)/2 + r/2;
336 r = fixdivquadlongu(low,high,r)/2 + r/2;
337 r = fixdivquadlongu(low,high,r)/2 + r/2;
342 t = fixdivquadlongu(low,high,r);
349 } while (!(r==t || r==old_r));
351 t = fixdivquadlongu(low,high,r);
352 //edited 05/17/99 Matt Mueller - tq.high is undefined here.. so set them to = 0
355 fixmulaccum(&tq,r,t);
356 if (tq.low!=low || tq.high!=high)
362 //computes the square root of a long, returning a short
363 ushort long_sqrt(int32_t a)
372 else if (a & 0xff0000)
379 r = guess_table[(a>>cnt)&0xff]<<cnt;
381 //the loop nearly always executes 3 times, so we'll unroll it 2 times and
382 //not do any checking until after the third time. By my calcutations, the
383 //loop is executed 2 times in 99.97% of cases, 3 times in 93.65% of cases,
384 //four times in 16.18% of cases, and five times in 0.44% of cases. It never
385 //executes more than five times. By timing, I determined that is is faster
386 //to always execute three times and not check for termination the first two
387 //times through. This means that in 93.65% of cases, we save 6 cmp/jcc pairs,
388 //and in 6.35% of cases we do an extra divide. In real life, these numbers
389 //might not be the same.
404 } while (!(r==t || r==old_r));
412 //computes the square root of a fix, returning a fix
415 return ((fix) long_sqrt(a)) << 8;
419 //compute sine and cosine of an angle, filling in the variables
420 //either of the pointers can be NULL
422 void fix_sincos(fix a,fix *s,fix *c)
430 ss = sincos_table[i];
431 if (s) *s = (ss + (((sincos_table[i+1] - ss) * f)>>8))<<2;
433 cc = sincos_table[i+64];
434 if (c) *c = (cc + (((sincos_table[i+64+1] - cc) * f)>>8))<<2;
437 //compute sine and cosine of an angle, filling in the variables
438 //either of the pointers can be NULL
440 void fix_fastsincos(fix a,fix *s,fix *c)
446 if (s) *s = sincos_table[i] << 2;
447 if (c) *c = sincos_table[i+64] << 2;
450 //compute inverse sine
451 fixang fix_asin(fix v)
458 if (vv >= f1_0) //check for out of range
465 aa = aa + (((asin_table[i+1] - aa) * f)>>8);
473 //compute inverse cosine
474 fixang fix_acos(fix v)
481 if (vv >= f1_0) //check for out of range
488 aa = aa + (((acos_table[i+1] - aa) * f)>>8);
496 #define TABLE_SIZE 1024
498 //for passed value a, returns 1/sqrt(a)
499 fix fix_isqrt( fix a )
505 if ( a == 0 ) return 0;
507 while( b >= TABLE_SIZE ) {
512 //printf( "Count = %d (%d>>%d)\n", cnt, b, (cnt+1)/2 );
513 r = isqrt_guess_table[b] >> ((cnt+1)/2);
515 //printf( "Initial r = %d\n", r );
517 for (i=0; i<3; i++ ) {
519 r = fixmul( ( (3*65536) - fixmul(fixmul(r,r),a) ), r) / 2;
520 //printf( "r %d = %d\n", i, r );
521 if ( old_r >= r ) return (r+old_r)/2;