1 /* Maths.h library header file */
6 #define NO_FIX_INLINE 1
13 #define D_RAND_MAX 32767
15 void d_srand (unsigned int seed);
16 int d_rand (); // Random number function which returns in the range 0-0x7FFF
19 //=============================== FIXED POINT ===============================
21 typedef int32_t fix; //16 bits int, 16 bits frac
22 typedef int16_t fixang; //angles
24 typedef struct quadint // integer 64 bit, previously called "quad"
32 //Convert an int to a fix
33 #define i2f(i) ((i)<<16)
35 //Get the int part of a fix
36 #define f2i(f) ((f)>>16)
38 //Get the int part of a fix, with rounding
39 #define f2ir(f) (((f)+f0_5)>>16)
41 //Convert fix to float and float to fix
42 #define f2fl(f) (((float) (f)) / 65536.0)
43 #define f2db(f) (((double) (f)) / 65536.0)
44 #define fl2f(f) ((fix) ((f) * 65536))
46 //Some handy constants
65 //#if defined(NO_FIX_INLINE) || (!defined(__GNUC__) && !defined(__WATCOMC__))
66 //multiply two fixes, return a fix
67 fix fixmul (fix a, fix b);
69 //divide two fixes, return a fix
70 fix fixdiv (fix a, fix b);
72 //multiply two fixes, then divide by a third, return a fix
73 fix fixmuldiv (fix a, fix b, fix c);
79 fix fixmul (fix a, fix b);
81 #pragma aux fixmul parm [eax] [edx] = \
87 fix fixdiv (fix a, fix b);
89 #pragma aux fixdiv parm [eax] [ebx] modify exact [eax edx] = \
96 fix fixmuldiv (fix a, fix b, fix c);
98 #pragma aux fixmuldiv parm [eax] [edx] [ebx] modify exact [eax edx] = \
102 //#elif defined (__GNUC__)
105 fixmul (fix a, fix b)
111 " shrd $16,%%edx,%%eax"
112 : "=a" (__retval): "a" (a), "d" (b):"%edx");
118 //multiply two fixes, return a fix
119 fix fixmul (fix a, fix b);
122 //divide two fixes, return a fix
123 fix fixdiv (fix a, fix b);
125 //multiply two fixes, then divide by a third, return a fix
126 fix fixmuldiv (fix a, fix b, fix c);
130 fixdiv (fix a, fix b)
133 register fix __retval;
135 asm (" mov %%eax,%%edx;"
138 " idiv %%ecx" /* adb: how to make this %0 w/o chance of edx? */
139 : "=a" (__retval): "a" (a), "c" (b):"%edx");
146 fixmuldiv (fix a, fix b, fix c)
149 register fix __retval;
152 " idiv %%ecx" /* adb: how to make this %0 w/o chance of edx? */
153 : "=a" (__retval): "a" (a), "r" (b), "c" (c):"%edx");
160 //#endif // __GNUC__ / __WATCOMC__
161 //#endif /* defined NO_FIX_INLINE || (!defined __GNUC__ && !defined __WATCOMC__) */
165 //multiply two fixes, and add 64-bit product to a quadint
166 void fixmulaccum (quadint * q, fix a, fix b);
168 //extract a fix from a quadint product
169 fix fixquadadjust (quadint * q);
171 //divide a quadint by a long
172 int32_t fixdivquadlong (u_int32_t qlow, u_int32_t qhigh, u_int32_t d);
175 void fixquadnegate (quadint * q);
177 //computes the square root of a long, returning a short
178 ushort long_sqrt (int32_t a);
180 //computes the square root of a quadint, returning a long
181 u_int32_t quad_sqrt (u_int32_t low, int32_t high);
182 //ulong quad_sqrt (long low, long high);
184 //computes the square root of a fix, returning a fix
185 fix fix_sqrt (fix a);
187 //compute sine and cosine of an angle, filling in the variables
188 //either of the pointers can be NULL
189 void fix_sincos (fix a, fix * s, fix * c); //with interpolation
191 void fix_fastsincos (fix a, fix * s, fix * c); //no interpolation
193 //compute inverse sine & cosine
194 fixang fix_asin (fix v);
196 fixang fix_acos (fix v);
198 //given cos & sin of an angle, return that angle.
199 //parms need not be normalized, that is, the ratio of the parms cos/sin must
200 //equal the ratio of the actual cos & sin for the result angle, but the parms
201 //need not be the actual cos & sin.
202 //NOTE: this is different from the standard C atan2, since it is left-handed.
203 fixang fix_atan2 (fix cos, fix sin);
205 //for passed value a, returns 1/sqrt(a)
206 fix fix_isqrt (fix a);