Debian Codegraph @

prboom2/src/m_fixed.h

raw
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 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <stdlib.h>
42 #include "doomtype.h"
43
44 /*
45 * Fixed point, 32bit as 16.16.
46 */
47
48 #define FRACBITS 16
49 #define FRACUNIT (1<<FRACBITS)
50
51 typedef int fixed_t;
52 typedef unsigned int ufixed_t;
53
54 /*
55 * Absolute Value
56 *
57 * killough 5/10/98: In djgpp, use inlined assembly for performance
58 * killough 9/05/98: better code seems to be gotten from using inlined C
59 */
60
61 // e6y
62 // Microsoft and Intel compilers produce stupid and slow code for asm version of D_abs:
63 //
64 // mov DWORD PTR $T49478[esp+12], eax
65 // mov eax, DWORD PTR $T49478[esp+12]
66 //
67 // Plane abs() generates absolutely the same code as in asm version of D_abs(),
68 // so we do not need additional implementations for abs() at all.
69 //
70 // btw, GCC generates code without nonsenses
71
72 #define D_abs abs
73
74 /*
75 * Fixed Point Multiplication
76 */
77
78
79 /* CPhipps - made __inline__ to inline, as specified in the gcc docs
80 * Also made const */
81
82 inline static CONSTFUNC fixed_t FixedMul(fixed_t a, fixed_t b)
83 {
84 return (fixed_t)((int64_t) a*b >> FRACBITS);
85 }
86
87 inline static CONSTFUNC int64_t FixedMul64(int64_t a, int64_t b)
88 {
89 return a * b >> FRACBITS;
90 }
91
92 /*
93 * Fixed Point Division
94 */
95
96 static CONSTFUNC fixed_t FixedDiv(fixed_t a, fixed_t b)
97 {
98 return (D_abs(a)>>14) >= D_abs(b) ? ((a^b)>>31) ^ INT_MAX :
99 (fixed_t)(((int64_t) a << FRACBITS) / b);
100 }
101
102 /* CPhipps -
103 * FixedMod - returns a % b, guaranteeing 0<=a<b
104 * (notice that the C standard for % does not guarantee this)
105 */
106
107 inline static CONSTFUNC fixed_t FixedMod(fixed_t a, fixed_t b)
108 {
109 if (b & (b-1)) {
110 fixed_t r = a % b;
111 return ((r<0) ? r+b : r);
112 } else
113 return (a & (b-1));
114 }
115
116 static CONSTFUNC fixed_t Scale(fixed_t a, fixed_t b, fixed_t c)
117 {
118 return (fixed_t)(((int64_t)a*b)/c);
119 }
120
121 #endif