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
|
#include <math.h>
#include "3dc.h"
void ADD_LL(LONGLONGCH *a, LONGLONGCH *b, LONGLONGCH *c);
void ADD_LL_PP(LONGLONGCH *c, LONGLONGCH *a);
void SUB_LL(LONGLONGCH *a, LONGLONGCH *b, LONGLONGCH *c);
void SUB_LL_MM(LONGLONGCH *c, LONGLONGCH *a);
void MUL_I_WIDE(int a, int b, LONGLONGCH *c);
int CMP_LL(LONGLONGCH *a, LONGLONGCH *b);
void EQUALS_LL(LONGLONGCH *a, LONGLONGCH *b);
void NEG_LL(LONGLONGCH *a);
void ASR_LL(LONGLONGCH *a, int shift);
void IntToLL(LONGLONGCH *a, int *b);
int MUL_FIXED(int a, int b);
int DIV_FIXED(int a, int b);
#define DIV_INT(a, b) ((a) / (b))
int NarrowDivide(LONGLONGCH *a, int b);
int WideMulNarrowDiv(int a, int b, int c);
void RotateVector_ASM(VECTORCH *v, MATRIXCH *m);
void RotateAndCopyVector_ASM(VECTORCH *v1, VECTORCH *v2, MATRIXCH *m);
#if 0
int FloatToInt(float);
#define f2i(a, b) { a = FloatToInt(b); }
#endif
#undef ASM386
#if !defined(ASM386)
static __int64 ConvertToLongLong(const LONGLONGCH* llch)
{
__int64 ll;
ll = ((__int64)llch->hi32 << 32) | ((__int64)llch->lo32 << 0);
return ll;
}
static void ConvertFromLongLong(LONGLONGCH* llch, const __int64* ll)
{
llch->lo32 = (unsigned int)((*ll>> 0) & 0xffffffff);
llch->hi32 = ( signed int)((*ll>>32) & 0xffffffff);
}
#endif
void ADD_LL(LONGLONGCH *a, LONGLONGCH *b, LONGLONGCH *c)
{
/*
_asm
{
mov esi,a
mov edi,b
mov ebx,c
mov eax,[esi]
mov edx,[esi+4]
add eax,[edi]
adc edx,[edi+4]
mov [ebx],eax
mov [ebx+4],edx
}
*/
#if defined(ASM386)
int dummy1, dummy2;
__asm__("movl 0(%%esi), %0 \n\t"
"movl 4(%%esi), %1 \n\t"
"addl 0(%%edi), %0 \n\t"
"adcl 4(%%edi), %1 \n\t"
"movl %0, 0(%%ebx) \n\t"
"movl %1, 4(%%ebx) \n\t"
: "=&r" (dummy1), "=&r" (dummy2)
: "S" (a), "D" (b), "b" (c)
: "memory", "cc"
);
/*
__asm__("movl 0(%%esi), %%eax \n\t"
"movl 4(%%esi), %%edx \n\t"
"addl 0(%%edi), %%eax \n\t"
"adcl 4(%%edi), %%edx \n\t"
: "=a" (c->lo32), "=d" (c->hi32)
: "S" (a), "D" (b)
);
*/
#else
__int64 aa = ConvertToLongLong(a);
__int64 bb = ConvertToLongLong(b);
__int64 cc = aa + bb;
ConvertFromLongLong(c, &cc);
#endif
}
/* ADD ++ */
void ADD_LL_PP(LONGLONGCH *c, LONGLONGCH *a)
{
/*
_asm
{
mov edi,c
mov esi,a
mov eax,[esi]
mov edx,[esi+4]
add [edi],eax
adc [edi+4],edx
}
*/
#if defined(ASM386)
int dummy1, dummy2;
__asm__("movl 0(%%esi), %0 \n\t"
"movl 4(%%esi), %1 \n\t"
"addl %0, 0(%%edi) \n\t"
"adcl %1, 4(%%edi) \n\t"
: "=&r" (dummy1), "=&r" (dummy2)
: "D" (c), "S" (a)
: "memory", "cc"
);
#else
__int64 cc = ConvertToLongLong(c);
__int64 aa = ConvertToLongLong(a);
cc += aa;
ConvertFromLongLong(c, &cc);
#endif
}
/* SUB */
void SUB_LL(LONGLONGCH *a, LONGLONGCH *b, LONGLONGCH *c)
{
/*
_asm
{
mov esi,a
mov edi,b
mov ebx,c
mov eax,[esi]
mov edx,[esi+4]
sub eax,[edi]
sbb edx,[edi+4]
mov [ebx],eax
mov [ebx+4],edx
}
*/
#if defined(ASM386)
int dummy1, dummy2;
__asm__("movl 0(%%esi), %0 \n\t"
"movl 4(%%esi), %1 \n\t"
"subl 0(%%edi), %0 \n\t"
"sbbl 4(%%edi), %1 \n\t"
"movl %0, 0(%%ebx) \n\t"
"movl %1, 4(%%ebx) \n\t"
: "=&r" (dummy1), "=&r" (dummy2)
: "S" (a), "D" (b), "b" (c)
: "memory", "cc"
);
#else
__int64 aa = ConvertToLongLong(a);
__int64 bb = ConvertToLongLong(b);
__int64 cc = aa - bb;
ConvertFromLongLong(c, &cc);
#endif
}
/* SUB -- */
void SUB_LL_MM(LONGLONGCH *c, LONGLONGCH *a)
{
/*
_asm
{
mov edi,c
mov esi,a
mov eax,[esi]
mov edx,[esi+4]
sub [edi],eax
sbb [edi+4],edx
}
*/
#if defined(ASM386)
int dummy1, dummy2;
__asm__("movl 0(%%esi), %0 \n\t"
"movl 4(%%esi), %1 \n\t"
"subl %0, 0(%%edi) \n\t"
"sbbl %1, 4(%%edi) \n\t"
: "=&r" (dummy1), "=&r" (dummy2)
: "D" (c), "S" (a)
: "memory", "cc"
);
#else
__int64 cc = ConvertToLongLong(c);
__int64 aa = ConvertToLongLong(a);
cc -= aa;
ConvertFromLongLong(c, &cc);
#endif
}
/*
MUL
This is the multiply we use, the 32 x 32 = 64 widening version
*/
void MUL_I_WIDE(int a, int b, LONGLONGCH *c)
{
/*
_asm
{
mov eax,a
mov ebx,c
imul b
mov [ebx],eax
mov [ebx+4],edx
}
*/
#if defined(ASM386)
unsigned int d1;
__asm__("imull %3 \n\t"
"movl %%eax, 0(%%ebx) \n\t"
"movl %%edx, 4(%%ebx) \n\t"
: "=a" (d1)
: "0" (a), "b" (c), "m" (b)
: "%edx", "memory", "cc"
);
#else
__int64 aa = (__int64) a;
__int64 bb = (__int64) b;
__int64 cc = aa * bb;
ConvertFromLongLong(c, &cc);
#endif
}
/*
CMP
This substitutes for ==, >, <, >=, <=
*/
int CMP_LL(LONGLONGCH *a, LONGLONGCH *b)
{
/*
int retval;
_asm
{
mov ebx,a
mov ecx,b
mov eax,[ebx]
mov edx,[ebx+4]
sub eax,[ecx]
sbb edx,[ecx+4]
and edx,edx
jne llnz
and eax,eax
je llgs
llnz:
mov retval,1
and edx,edx
jge llgs
neg retval
llgs:
}
*/
#if defined(ASM386)
int retval;
__asm__("movl 0(%%ebx), %%eax \n\t"
"movl 4(%%ebx), %%edx \n\t"
"subl 0(%%ecx), %%eax \n\t"
"sbbl 4(%%ecx), %%edx \n\t"
"xorl %%ebx, %%ebx \n\t"
"andl %%edx, %%edx \n\t"
"jne 0f \n\t" /* llnz */
"andl %%eax, %%eax \n\t"
"je 1f \n" /* llgs */
"0: \n\t" /* llnz */
"movl $1, %%ebx \n\t"
"andl %%edx, %%edx \n\t"
"jge 1f \n\t" /* llgs */
"negl %%ebx \n"
"1: \n\t" /* llgs */
: "=b" (retval)
: "b" (a), "c" (b)
: "%eax", "%edx", "memory", "cc"
);
return retval;
#else
if (a->hi32 > b->hi32)
return 1;
else if (a->hi32 < b->hi32)
return -1;
else if (a->lo32 > b->lo32)
return 1;
else if (a->lo32 < b->lo32)
return -1;
else
return 0;
#endif
}
/* EQUALS */
void EQUALS_LL(LONGLONGCH *a, LONGLONGCH *b)
{
/*
_asm
{
mov edi,a
mov esi,b
mov eax,[esi]
mov edx,[esi+4]
mov [edi],eax
mov [edi+4],edx
}
*/
#if defined(ASM386)
__asm__("movl 0(%%esi), %%eax \n\t"
"movl 4(%%esi), %%edx \n\t"
"movl %%eax, 0(%%edi) \n\t"
"movl %%edx, 4(%%edi) \n\t"
:
: "D" (a), "S" (b)
: "%eax", "%edx", "memory"
);
#else
*a = *b;
#endif
}
/* NEGATE */
void NEG_LL(LONGLONGCH *a)
{
/*
_asm
{
mov esi,a
not dword ptr[esi]
not dword ptr[esi+4]
add dword ptr[esi],1
adc dword ptr[esi+4],0
}
*/
#if defined(ASM386)
__asm__("notl 0(%%esi) \n\t"
"notl 4(%%esi) \n\t"
"addl $1, 0(%%esi) \n\t"
"adcl $0, 4(%%esi) \n\t"
:
: "S" (a)
: "memory", "cc"
);
#else
__int64 aa = ConvertToLongLong(a);
aa = -aa;
ConvertFromLongLong(a, &aa);
#endif
}
/* ASR */
void ASR_LL(LONGLONGCH *a, int shift)
{
/*
_asm
{
mov esi,a
mov eax,shift
and eax,eax
jle asrdn
asrlp:
sar dword ptr[esi+4],1
rcr dword ptr[esi],1
dec eax
jne asrlp
asrdn:
}
*/
#if defined(ASM386)
unsigned int d1;
__asm__ volatile
("andl %0, %0 \n\t"
"jle 0 \n" /* asrdn */
"1: \n\t" /* asrlp */
"sarl $1, 4(%%esi) \n\t"
"rcrl $1, 0(%%esi) \n\t"
"decl %0 \n\t"
"jne 1 \n"
"0: \n\t"
: "=&r" (d1)
: "S" (a), "a" (shift)
: "memory", "cc"
);
#else
__int64 aa = ConvertToLongLong(a);
aa >>= shift;
ConvertFromLongLong(a, &aa);
#endif
}
/* Convert int to LONGLONGCH */
void IntToLL(LONGLONGCH *a, int *b)
{
/*
_asm
{
mov esi,b
mov edi,a
mov eax,[esi]
cdq
mov [edi],eax
mov [edi+4],edx
}
*/
#if defined(ASM386)
__asm__("movl 0(%%esi), %%eax \n\t"
"cdq \n\t"
"movl %%eax, 0(%%edi) \n\t"
"movl %%edx, 4(%%edi) \n\t"
:
: "S" (b), "D" (a)
: "%eax", "%edx", "memory", "cc"
);
#else
__int64 aa = (__int64) *b;
ConvertFromLongLong(a, &aa);
#endif
}
/*
Fixed Point Multiply.
16.16 * 16.16 -> 16.16
or
16.16 * 0.32 -> 0.32
A proper version of this function ought to read
16.16 * 16.16 -> 32.16
but this would require a __int64 result
Algorithm:
Take the mid 32 bits of the 64 bit result
*/
/*
These functions have been checked for suitability for
a Pentium and look as if they would work adequately.
Might be worth a more detailed look at optimising
them though.
*/
int MUL_FIXED(int a, int b)
{
/*
int retval;
_asm
{
mov eax,a
imul b
shrd eax,edx,16
mov retval,eax
}
*/
#if defined(ASM386)
int retval;
__asm__("imull %2 \n\t"
"shrdl $16, %%edx, %%eax \n\t"
: "=a" (retval)
: "0" (a), "m" (b)
: "%edx", "cc"
);
return retval;
#else
__int64 aa = (__int64) a;
__int64 bb = (__int64) b;
__int64 cc = aa * bb;
return (int) ((cc >> 16) & 0xffffffff);
#endif
}
/*
Fixed Point Divide - returns a / b
*/
int DIV_FIXED(int a, int b)
{
if (b == 0) printf("DEBUG THIS: a = %d, b = %d\n", a, b);
if (b == 0) return 0; /* TODO: debug this! (start with alien on ferarco) */
/*
int retval;
_asm
{
mov eax,a
cdq
rol eax,16
mov dx,ax
xor ax,ax
idiv b
mov retval,eax
}
*/
#if defined(ASM386)
int retval;
__asm__("cdq \n\t"
"roll $16, %%eax \n\t"
"mov %%ax, %%dx \n\t"
"xor %%ax, %%ax \n\t"
"idivl %2 \n\t"
: "=a" (retval)
: "0" (a), "m" (b)
: "%edx", "cc"
);
return retval;
#else
{
__int64 aa = (__int64) a;
__int64 bb = (__int64) b;
__int64 cc = (aa << 16) / bb;
return (int) (cc & 0xffffffff);
}
#endif
}
/*
Multiply and Divide Functions.
*/
/*
32/32 division
This macro is a function on some other platforms
*/
#define DIV_INT(a, b) ((a) / (b))
/*
A Narrowing 64/32 Division
*/
int NarrowDivide(LONGLONGCH *a, int b)
{
/*
int retval;
_asm
{
mov esi,a
mov eax,[esi]
mov edx,[esi+4]
idiv b
mov retval,eax
}
*/
#if defined(ASM386)
int retval;
__asm__("movl 0(%%esi), %%eax \n\t"
"movl 4(%%esi), %%edx \n\t"
"idivl %2 \n\t"
: "=a" (retval)
: "S" (a), "m" (b)
: "%edx", "cc"
);
return retval;
#else
__int64 aa = ConvertToLongLong(a);
__int64 bb = (__int64) b;
__int64 cc = aa / bb;
return (int) (cc & 0xffffffff);
#endif
}
/*
This function performs a Widening Multiply followed by a Narrowing Divide.
a = (a * b) / c
*/
int WideMulNarrowDiv(int a, int b, int c)
{
/*
int retval;
_asm
{
mov eax,a
imul b
idiv c
mov retval,eax
}
*/
#if defined(ASM386)
int retval;
__asm__("imull %2 \n\t"
"idivl %3 \n\t"
: "=a" (retval)
: "0" (a), "m" (b), "m" (c)
: "%edx", "cc"
);
return retval;
#else
__int64 aa = (__int64) a;
__int64 bb = (__int64) b;
__int64 cc = (__int64) c;
__int64 dd = (aa * bb) / cc;
return (int) (dd & 0xffffffff);
#endif
}
/*
Square Root
Returns the Square Root of a 32-bit number
*/
int SqRoot32(int A)
{
/*
_asm
{
finit
fild A
fsqrt
fistp temp2
fwait
}
*/
#if defined(ASM386)
static volatile int sqrt_temp;
__asm__ volatile
("finit \n\t"
"fildl %0 \n\t"
"fsqrt \n\t"
"fistpl sqrt_temp \n\t"
"fwait \n\t"
:
: "m" (A)
: "memory", "cc"
);
return sqrt_temp;
#else
return (int) sqrt( (float)A );
#endif
}
/*
This may look ugly (it is) but it is a MUCH faster way to convert "float" into "int" than
the function call "CHP" used by the WATCOM compiler.
*/
volatile float fti_fptmp;
volatile int fti_itmp;
void FloatToInt()
{
#if defined(ASM386)
__asm__ volatile
("flds fti_fptmp \n\t"
"fistpl fti_itmp \n\t"
:
:
: "memory", "cc"
);
#else
fti_itmp = (int)fti_fptmp;
#endif
}
|