1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
6 // Copyright (C) 1993-1996 by id Software, Inc.
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
19 // Fixed point arithemtics, implementation.
21 //-----------------------------------------------------------------------------
33 #include "rockmacros.h"
36 // Fixed point, 32bit as 16.16.
39 #define FRACUNIT (1<<FRACBITS)
41 #define D_abs(x) ({fixed_t _t = (x), _s = _t >> (8*sizeof _t-1); (_t^_s)-_s;})
45 inline static int FixedMul( int a
, int b
)
47 #if defined(CPU_COLDFIRE)
48 // Code contributed by Thom Johansen
51 "mac.l %[x],%[y],%%acc0 \n" /* multiply */
53 "mulu.l %[x],%%d2 \n" /* get lower half, avoid emac stall */
54 "movclr.l %%acc0,%[result] \n" /* get higher half */
55 "asl.l #8,%[result] \n" /* hi <<= 15, plus one free */
56 "asl.l #7,%[result] \n" /* hi <<= 15, plus one free */
57 "lsr.l #8,%%d2 \n" /* (unsigned)lo >>= 16 */
58 "lsr.l #8,%%d2 \n" /* (unsigned)lo >>= 16 */
59 "or.l %%d2 ,%[result] \n" /* combine result */
70 return (fixed_t
)((long long) a
*b
>> FRACBITS
);
74 inline static fixed_t
FixedDiv( fixed_t a
, fixed_t b
)
76 return (D_abs(a
)>>14) >= D_abs(b
) ? ((a
^b
)>>31) ^ MAXINT
:
77 (fixed_t
)(((long long) a
<< FRACBITS
) / b
);
81 * FixedMod - returns a % b, guaranteeing 0<=a<b
82 * (notice that the C standard for % does not guarantee this)
85 inline static fixed_t
FixedMod(fixed_t a
, fixed_t b
)
89 return ((r
<0) ? r
+b
: r
);