diff options
Diffstat (limited to 'src/m_fixed.h')
-rw-r--r-- | src/m_fixed.h | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/m_fixed.h b/src/m_fixed.h new file mode 100644 index 0000000..2eb36db --- /dev/null +++ b/src/m_fixed.h | |||
@@ -0,0 +1,101 @@ | |||
1 | /* Emacs style mode select -*- C++ -*- | ||
2 | *----------------------------------------------------------------------------- | ||
3 | * | ||
4 | * | ||
5 | * PrBoom: a Doom port merged with LxDoom and LSDLDoom | ||
6 | * based on BOOM, a modified and improved DOOM engine | ||
7 | * Copyright (C) 1999 by | ||
8 | * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman | ||
9 | * Copyright (C) 1999-2000 by | ||
10 | * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze | ||
11 | * Copyright 2005, 2006 by | ||
12 | * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko | ||
13 | * | ||
14 | * This program is free software; you can redistribute it and/or | ||
15 | * modify it under the terms of the GNU General Public License | ||
16 | * as published by the Free Software Foundation; either version 2 | ||
17 | * of the License, or (at your option) any later version. | ||
18 | * | ||
19 | * This program is distributed in the hope that it will be useful, | ||
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
22 | * GNU General Public License for more details. | ||
23 | * | ||
24 | * You should have received a copy of the GNU General Public License | ||
25 | * along with this program; if not, write to the Free Software | ||
26 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
27 | * 02111-1307, USA. | ||
28 | * | ||
29 | * DESCRIPTION: | ||
30 | * Fixed point arithemtics, implementation. | ||
31 | * | ||
32 | *-----------------------------------------------------------------------------*/ | ||
33 | |||
34 | #ifndef __M_FIXED__ | ||
35 | #define __M_FIXED__ | ||
36 | |||
37 | #include "config.h" | ||
38 | #include "doomtype.h" | ||
39 | |||
40 | /* | ||
41 | * Fixed point, 32bit as 16.16. | ||
42 | */ | ||
43 | |||
44 | #define FRACBITS 16 | ||
45 | #define FRACUNIT (1<<FRACBITS) | ||
46 | |||
47 | typedef int fixed_t; | ||
48 | |||
49 | /* | ||
50 | * Absolute Value | ||
51 | * | ||
52 | * killough 5/10/98: In djgpp, use inlined assembly for performance | ||
53 | * killough 9/05/98: better code seems to be gotten from using inlined C | ||
54 | */ | ||
55 | |||
56 | inline static const int D_abs(int x) | ||
57 | { | ||
58 | fixed_t _t = (x),_s; | ||
59 | _s = _t >> (8*sizeof _t-1); | ||
60 | return (_t^_s)-_s; | ||
61 | } | ||
62 | /* | ||
63 | * Fixed Point Multiplication | ||
64 | */ | ||
65 | |||
66 | /* CPhipps - made __inline__ to inline, as specified in the gcc docs | ||
67 | * Also made const */ | ||
68 | |||
69 | inline static CONSTFUNC fixed_t FixedMul(fixed_t a, fixed_t b) | ||
70 | { | ||
71 | return (fixed_t)((int_64_t) a*b >> FRACBITS); | ||
72 | } | ||
73 | /* | ||
74 | * Fixed Point Division | ||
75 | */ | ||
76 | |||
77 | /* CPhipps - made __inline__ to inline, as specified in the gcc docs | ||
78 | * Also made const */ | ||
79 | |||
80 | inline static CONSTFUNC fixed_t FixedDiv(fixed_t a, fixed_t b) | ||
81 | { | ||
82 | return ((unsigned)D_abs(a)>>14) >= (unsigned)D_abs(b) ? ((a^b)>>31) ^ INT_MAX : | ||
83 | (fixed_t)(((int_64_t) a << FRACBITS) / b); | ||
84 | } | ||
85 | |||
86 | |||
87 | /* CPhipps - | ||
88 | * FixedMod - returns a % b, guaranteeing 0<=a<b | ||
89 | * (notice that the C standard for % does not guarantee this) | ||
90 | */ | ||
91 | |||
92 | inline static CONSTFUNC fixed_t FixedMod(fixed_t a, fixed_t b) | ||
93 | { | ||
94 | if (b & (b-1)) { | ||
95 | fixed_t r = a % b; | ||
96 | return ((r<0) ? r+b : r); | ||
97 | } else | ||
98 | return (a & (b-1)); | ||
99 | } | ||
100 | |||
101 | #endif | ||