Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / plugins / doom / m_fixed.h
blob15fd9867a3630813414b4f87392adf3e0348405e
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // $Id$
5 //
6 // Copyright (C) 1993-1996 by id Software, Inc.
7 //
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.
18 // DESCRIPTION:
19 // Fixed point arithemtics, implementation.
21 //-----------------------------------------------------------------------------
24 #ifndef __M_FIXED__
25 #define __M_FIXED__
28 #ifdef __GNUG__
29 #pragma interface
30 #endif
32 #include "doomtype.h"
33 #include "rockmacros.h"
36 // Fixed point, 32bit as 16.16.
38 #define FRACBITS 16
39 #define FRACUNIT (1<<FRACBITS)
41 #define D_abs(x) ({fixed_t _t = (x), _s = _t >> (8*sizeof _t-1); (_t^_s)-_s;})
43 typedef int fixed_t;
45 inline static int FixedMul( int a, int b )
47 #if defined(CPU_COLDFIRE)
48 // Code contributed by Thom Johansen
49 register int result;
50 asm (
51 "mac.l %[x],%[y],%%acc0 \n" /* multiply */
52 "move.l %[y],%%d2 \n"
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 */
60 : /* outputs */
61 [result]"=&d"(result)
62 : /* inputs */
63 [x] "d" (a),
64 [y] "d" (b)
65 : /* clobbers */
66 "d2"
68 return result;
69 #else
70 return (fixed_t)((long long) a*b >> FRACBITS);
71 #endif
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);
80 /* CPhipps -
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)
87 if (b & (b-1)) {
88 fixed_t r = a % b;
89 return ((r<0) ? r+b : r);
90 } else
91 return (a & (b-1));
94 #endif