-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix.c
776 lines (603 loc) · 14.1 KB
/
fix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/*
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
*/
/*
* $Source: Smoke:miner:source:fix::RCS:fix.c $
* $Revision: 1.7 $
* $Author: allender $
* $Date: 1995/09/22 14:08:16 $
*
* C version of fixed point library
*
* $Log: fix.c $
* Revision 1.7 1995/09/22 14:08:16 allender
* fixed fix_atan2 to work correctly with doubles
*
* Revision 1.6 1995/08/31 15:43:49 allender
* *** empty log message ***
*
* Revision 1.5 1995/07/05 16:15:15 allender
* make fixmuldiv use doubles for PPC implementation
*
* Revision 1.4 1995/05/15 13:57:36 allender
* make fixmuldiv compile when compiling under 68k
*
* Revision 1.3 1995/05/11 13:02:59 allender
* some routines are now in assembly
*
* Revision 1.2 1995/05/04 20:04:45 allender
* use MPW fixdiv if compiling with MPW (why did I do this?)
*
* Revision 1.1 1995/04/17 11:37:54 allender
* Initial revision
*
*
* --- PC RCS Info ---
* Revision 1.1 1995/03/08 18:55:09 matt
* Initial revision
*
*
*/
#pragma off (unreferenced)
static char rcsid[] = "$Id: fix.c 1.7 1995/09/22 14:08:16 allender Exp $";
#pragma on (unreferenced)
#include <stdlib.h>
#include <ToolUtils.h>
#include <math.h>
#include "error.h"
#include "fix.h"
extern ubyte guess_table[];
extern short sincos_table[];
extern ushort asin_table[];
extern ushort acos_table[];
extern fix isqrt_guess_table[];
#if !(defined(__WATCOMC__) && defined(USE_INLINE))
//negate a quad
void fixquadnegate(quad *q)
{
q->low = 0 - q->low;
q->high = 0 - q->high - (q->low != 0);
}
//multiply two ints & add 64-bit result to 64-bit sum
void fixmulaccum(quad *q,fix a,fix b)
{
ulong aa,bb;
ulong ah,al,bh,bl;
ulong t,c=0,old;
int neg;
neg = ((a^b) < 0);
aa = labs(a); bb = labs(b);
ah = aa>>16; al = aa&0xffff;
bh = bb>>16; bl = bb&0xffff;
t = ah*bl + bh*al;
if (neg)
fixquadnegate(q);
old = q->low;
q->low += al*bl;
if (q->low < old) q->high++;
old = q->low;
q->low += (t<<16);
if (q->low < old) q->high++;
q->high += ah*bh + (t>>16) + c;
if (neg)
fixquadnegate(q);
}
//extract a fix from a quad product
fix fixquadadjust(quad *q)
{
return (q->high<<16) + (q->low>>16);
}
#ifndef __powerc
fix fixmul(fix a, fix b)
{
return (fix)FixMul((Fixed)a, (Fixed)b);
}
//divide a quad by a fix, returning a fix
long fixdivquadlong(ulong nl,ulong nh,ulong d)
{
int i;
ulong tmp0;
ubyte tmp1;
ulong r;
ubyte T,Q,M;
r = 0;
Q = ((nh&0x80000000)!=0);
M = ((d&0x80000000)!=0);
T = (M!=Q);
if (M == 0)
{
for (i=0; i<32; i++ ) {
r <<= 1;
r |= T;
T = ((nl&0x80000000L)!=0);
nl <<= 1;
switch( Q ) {
case 0:
Q = (unsigned char)((0x80000000L & nh) != 0 );
nh = (nh << 1) | (unsigned long)T;
tmp0 = nh;
nh -= d;
tmp1 = (nh>tmp0);
if (Q == 0)
Q = tmp1;
else
Q = (unsigned char)(tmp1 == 0);
break;
case 1:
Q = (unsigned char)((0x80000000L & nh) != 0 );
nh = (nh << 1) | (unsigned long)T;
tmp0 = nh;
nh += d;
tmp1 = (nh<tmp0);
if (Q == 0)
Q = tmp1;
else
Q = (unsigned char)(tmp1 == 0);
break;
}
T = (Q==M);
}
}
else
{
for (i=0; i<32; i++ ) {
r <<= 1;
r |= T;
T = ((nl&0x80000000L)!=0);
nl <<= 1;
switch( Q ) {
case 0:
Q = (unsigned char)((0x80000000L & nh) != 0 );
nh = (nh << 1) | (unsigned long)T;
tmp0 = nh;
nh += d;
tmp1 = (nh<tmp0);
if (Q == 1)
Q = tmp1;
else
Q = (unsigned char)(tmp1 == 0);
break;
case 1:
Q = (unsigned char)((0x80000000L & nh) != 0 );
nh = (nh << 1) | (unsigned long)T;
tmp0 = nh;
nh = nh - d;
tmp1 = (nh>tmp0);
if (Q == 1)
Q = tmp1;
else
Q = (unsigned char)(tmp1 == 0);
break;
}
T = (Q==M);
}
}
r = (r << 1) | T;
return r;
}
fix fixdiv(fix a, fix b)
{
return fixdivquadlong(a<<16,a>>16,b);
// return (fix)FixDiv((Fixed)a,(Fixed)b);
}
//multiply two fixes, then divide by a third, return a fix
fix fixmuldiv(fix a,fix b,fix c)
{
quad q;
ulong t,old;
int neg;
ulong aa,bb;
ulong ah,al,bh,bl;
neg = ((a^b) < 0);
aa = labs(a); bb = labs(b);
ah = aa>>16; al = aa&0xffff;
bh = bb>>16; bl = bb&0xffff;
t = ah*bl + bh*al;
q.high = 0;
old = q.low = al*bl;
q.low += (t<<16);
if (q.low < old) q.high++;
q.high += ah*bh + (t>>16);
if (neg)
fixquadnegate(&q);
return fixdivquadlong(q.low,q.high,c);
}
fixang fix_atan2(fix cos,fix sin)
{
quad q;
fix m,t;
//Assert(!(cos==0 && sin==0));
//find smaller of two
q.low = q.high = 0;
fixmulaccum(&q,sin,sin);
fixmulaccum(&q,cos,cos);
m = quad_sqrt(q.low,q.high);
if (m==0)
return 0;
if (labs(sin) < labs(cos)) { //sin is smaller, use arcsin
t = fix_asin(fixdiv(sin,m));
if (cos<0)
t = 0x8000 - t;
return t;
}
else {
t = fix_acos(fixdiv(cos,m));
if (sin<0)
t = -t;
return t;
}
}
//computes the square root of a quad, returning a long
ulong quad_sqrt(long low,long high)
{
long cnt,r,old_r,t;
quad tq;
if (high<0)
return 0;
if (high==0 && low>=0)
return long_sqrt(low);
if (high & 0xff000000)
cnt=12+16;
else if (high & 0xff0000)
cnt=8+16;
else if (high & 0xff00)
cnt=4+16;
else
cnt=0+16;
r = guess_table[(high>>cnt)&0xff]<<cnt;
//quad loop usually executed 4 times
r = (fixdivquadlong(low,high,r)+r)/2;
r = (fixdivquadlong(low,high,r)+r)/2;
r = (fixdivquadlong(low,high,r)+r)/2;
do {
old_r = r;
t = fixdivquadlong(low,high,r);
if (t==r) //got it!
return r;
r = (t+r)/2;
} while (!(r==t || r==old_r));
t = fixdivquadlong(low,high,r);
tq.low=tq.high;
fixmulaccum(&tq,r,t);
if (tq.low!=low || tq.high!=high)
r++;
return r;
}
#else
#define EPSILON (F1_0/100)
fix fixdiv(fix a, fix b)
{
#if 0
double d;
if ((double)b == 0)
Int3();
d = (((double)a * 65536.0) / (double)b);
if (abs((d * (double)b) - ((double)a * 65536.0)) > EPSILON)
Int3();
return (fix)(d);
#endif
return (fix)(((double)a * 65536.0) / (double)b);
}
fix fixmuldiv(fix a, fix b, fix c)
{
double d;
#if 0
if ((double)c == 0)
Int3();
d = (double)a * (double)b;
d /= (double)c;
if (abs((d * (double)c) - ((double)b * (double)a)) > EPSILON)
Int3();
return (fix)(d);
#endif
d = (double)a * (double) b;
return (fix)(d / (double) c);
}
//given cos & sin of an angle, return that angle.
//parms need not be normalized, that is, the ratio of the parms cos/sin must
//equal the ratio of the actual cos & sin for the result angle, but the parms
//need not be the actual cos & sin.
//NOTE: this is different from the standard C atan2, since it is left-handed.
fixang fix_atan2(fix cos,fix sin)
{
double d, dsin, dcos;
fix m,t;
//Assert(!(cos==0 && sin==0));
//find smaller of two
dsin = (double)sin;
dcos = (double)cos;
d = sqrt((dsin * dsin) + (dcos * dcos));
if (d==0.0)
return 0;
if (labs(sin) < labs(cos)) { //sin is smaller, use arcsin
t = fix_asin((fix)((dsin / d) * 65536.0));
if (cos<0)
t = 0x8000 - t;
return t;
}
else {
t = fix_acos((fix)((dcos / d) * 65536.0));
if (sin<0)
t = -t;
return t;
}
}
//divide a quad by a fix, returning a fix
long fixdivquadlong(ulong nl,ulong nh,ulong d)
{
int i;
ulong tmp0;
ubyte tmp1;
ulong r;
ubyte T,Q,M;
r = 0;
Q = ((nh&0x80000000)!=0);
M = ((d&0x80000000)!=0);
T = (M!=Q);
if (M == 0)
{
for (i=0; i<32; i++ ) {
r <<= 1;
r |= T;
T = ((nl&0x80000000L)!=0);
nl <<= 1;
switch( Q ) {
case 0:
Q = (unsigned char)((0x80000000L & nh) != 0 );
nh = (nh << 1) | (unsigned long)T;
tmp0 = nh;
nh -= d;
tmp1 = (nh>tmp0);
if (Q == 0)
Q = tmp1;
else
Q = (unsigned char)(tmp1 == 0);
break;
case 1:
Q = (unsigned char)((0x80000000L & nh) != 0 );
nh = (nh << 1) | (unsigned long)T;
tmp0 = nh;
nh += d;
tmp1 = (nh<tmp0);
if (Q == 0)
Q = tmp1;
else
Q = (unsigned char)(tmp1 == 0);
break;
}
T = (Q==M);
}
}
else
{
for (i=0; i<32; i++ ) {
r <<= 1;
r |= T;
T = ((nl&0x80000000L)!=0);
nl <<= 1;
switch( Q ) {
case 0:
Q = (unsigned char)((0x80000000L & nh) != 0 );
nh = (nh << 1) | (unsigned long)T;
tmp0 = nh;
nh += d;
tmp1 = (nh<tmp0);
if (Q == 1)
Q = tmp1;
else
Q = (unsigned char)(tmp1 == 0);
break;
case 1:
Q = (unsigned char)((0x80000000L & nh) != 0 );
nh = (nh << 1) | (unsigned long)T;
tmp0 = nh;
nh = nh - d;
tmp1 = (nh>tmp0);
if (Q == 1)
Q = tmp1;
else
Q = (unsigned char)(tmp1 == 0);
break;
}
T = (Q==M);
}
}
r = (r << 1) | T;
return r;
}
ulong quad_sqrt(long low,long high)
{
long cnt,r,old_r,t;
quad tq;
if (high<0)
return 0;
if (high==0 && low>=0)
return long_sqrt(low);
if (high & 0xff000000)
cnt=12+16;
else if (high & 0xff0000)
cnt=8+16;
else if (high & 0xff00)
cnt=4+16;
else
cnt=0+16;
r = guess_table[(high>>cnt)&0xff]<<cnt;
//quad loop usually executed 4 times
r = (fixdivquadlong(low,high,r)+r)/2;
r = (fixdivquadlong(low,high,r)+r)/2;
r = (fixdivquadlong(low,high,r)+r)/2;
do {
old_r = r;
t = fixdivquadlong(low,high,r);
if (t==r) //got it!
return r;
r = (t+r)/2;
} while (!(r==t || r==old_r));
t = fixdivquadlong(low,high,r);
tq.low=tq.high;
fixmulaccum(&tq,r,t);
if (tq.low!=low || tq.high!=high)
r++;
return r;
}
#if 0
fix fixdiv(fix a, fix b)
{
return fixdivquadlong(a<<16,a>>16,b);
// return (fix)FixDiv((Fixed)a,(Fixed)b);
}
//multiply two fixes, then divide by a third, return a fix
fix fixmuldiv(fix a,fix b,fix c)
{
quad q;
ulong t,old;
int neg;
ulong aa,bb;
ulong ah,al,bh,bl;
neg = ((a^b) < 0);
aa = labs(a); bb = labs(b);
ah = aa>>16; al = aa&0xffff;
bh = bb>>16; bl = bb&0xffff;
t = ah*bl + bh*al;
q.high = 0;
old = q.low = al*bl;
q.low += (t<<16);
if (q.low < old) q.high++;
q.high += ah*bh + (t>>16);
if (neg)
fixquadnegate(&q);
return fixdivquadlong(q.low,q.high,c);
}
#endif
#endif __powerc
#endif
//computes the square root of a long, returning a short
ushort long_sqrt(long a)
{
int cnt,r,old_r,t;
if (a<=0)
return 0;
if (a & 0xff000000)
cnt=12;
else if (a & 0xff0000)
cnt=8;
else if (a & 0xff00)
cnt=4;
else
cnt=0;
r = guess_table[(a>>cnt)&0xff]<<cnt;
//the loop nearly always executes 3 times, so we'll unroll it 2 times and
//not do any checking until after the third time. By my calcutations, the
//loop is executed 2 times in 99.97% of cases, 3 times in 93.65% of cases,
//four times in 16.18% of cases, and five times in 0.44% of cases. It never
//executes more than five times. By timing, I determined that is is faster
//to always execute three times and not check for termination the first two
//times through. This means that in 93.65% of cases, we save 6 cmp/jcc pairs,
//and in 6.35% of cases we do an extra divide. In real life, these numbers
//might not be the same.
r = ((a/r)+r)/2;
r = ((a/r)+r)/2;
do {
old_r = r;
t = a/r;
if (t==r) //got it!
return r;
r = (t+r)/2;
} while (!(r==t || r==old_r));
if (a % r)
r++;
return r;
}
//computes the square root of a fix, returning a fix
fix fix_sqrt(fix a)
{
return ((fix) long_sqrt(a)) << 8;
}
//compute sine and cosine of an angle, filling in the variables
//either of the pointers can be NULL
//with interpolation
void fix_sincos(fix a,fix *s,fix *c)
{
int i,f;
fix ss,cc;
i = (a>>8)&0xff;
f = a&0xff;
ss = sincos_table[i];
*s = (ss + (((sincos_table[i+1] - ss) * f)>>8))<<2;
cc = sincos_table[i+64];
*c = (cc + (((sincos_table[i+64+1] - cc) * f)>>8))<<2;
}
//compute sine and cosine of an angle, filling in the variables
//either of the pointers can be NULL
//no interpolation
void fix_fastsincos(fix a,fix *s,fix *c)
{
int i;
i = (a>>8)&0xff;
*s = sincos_table[i] << 2;
*c = sincos_table[i+64] << 2;
}
//compute inverse sine
fixang fix_asin(fix v)
{
fix vv;
int i,f,aa;
vv = labs(v);
if (vv >= f1_0) //check for out of range
return 0x4000;
i = (vv>>8)&0xff;
f = vv&0xff;
aa = asin_table[i];
aa = aa + (((asin_table[i+1] - aa) * f)>>8);
if (v < 0)
aa = -aa;
return aa;
}
//compute inverse cosine
fixang fix_acos(fix v)
{
fix vv;
int i,f,aa;
vv = labs(v);
if (vv >= f1_0) //check for out of range
return 0;
i = (vv>>8)&0xff;
f = vv&0xff;
aa = acos_table[i];
aa = aa + (((acos_table[i+1] - aa) * f)>>8);
if (v < 0)
aa = 0x8000 - aa;
return aa;
}
#define TABLE_SIZE 1024
//for passed value a, returns 1/sqrt(a)
fix fix_isqrt( fix a )
{
int i, b = a;
int cnt = 0;
int r;
if ( a == 0 ) return 0;
while( b >= TABLE_SIZE ) {
b >>= 1;
cnt++;
}
//printf( "Count = %d (%d>>%d)\n", cnt, b, (cnt+1)/2 );
r = isqrt_guess_table[b] >> ((cnt+1)/2);
//printf( "Initial r = %d\n", r );
for (i=0; i<3; i++ ) {
int old_r = r;
r = fixmul( ( (3*65536) - fixmul(fixmul(r,r),a) ), r) / 2;
//printf( "r %d = %d\n", i, r );
if ( old_r >= r ) return (r+old_r)/2;
}
return r;
}