1 #ifndef MATHLINE_H
2 #define MATHLINE_H
3 
4 #include <math.h>
5 
6 #define f2i(a, b) a = lrintf(b)
7 
8 /*
9 
10  Fixed Point Multiply.
11 
12 
13  16.16 * 16.16 -> 16.16
14  or
15  16.16 * 0.32 -> 0.32
16 
17  A proper version of this function ought to read
18  16.16 * 16.16 -> 32.16
19  but this would require a __int64 result
20 
21  Algorithm:
22 
23  Take the mid 32 bits of the 64 bit result
24 
25 */
26 
27 /*
28 	These functions have been checked for suitability for
29 	a Pentium and look as if they would work adequately.
30 	Might be worth a more detailed look at optimising
31 	them though.
32 */
33 
MUL_FIXED(int a,int b)34 static __inline int MUL_FIXED(int a, int b)
35 {
36 /*
37 	int retval;
38 	_asm
39 	{
40 		mov eax,a
41 		imul b
42 		shrd eax,edx,16
43 		mov retval,eax
44 	}
45 */
46 
47 #if defined(ASM386)
48 	int retval;
49 __asm__("imull	%2			\n\t"
50 	"shrdl	$16, %%edx, %%eax	\n\t"
51 	: "=a" (retval)
52 	: "0" (a), "m" (b)
53 	: "%edx", "cc"
54 	);
55 	return retval;
56 #else
57 	__int64 aa = (__int64) a;
58 	__int64 bb = (__int64) b;
59 
60 	__int64 cc = aa * bb;
61 
62 	return (int) ((cc >> 16) & 0xffffffff);
63 #endif
64 }
65 
66 #endif
67