Debian Codegraph @

fix/fix.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/fix/fix.cpp $
21 * $Revision: 6 $
22 * $Date: 4/22/99 8:29p $
23 * $Author: Kevin $
24 *
25 * Fixed-point math functions, including trig & float conversions
26 *
27 * $Log: /DescentIII/Main/fix/fix.cpp $
28 *
29 * 6 4/22/99 8:29p Kevin
30 * made psrand.h come after stdlib.h
31 *
32 * 5 4/21/99 11:05a Kevin
33 * new ps_rand and ps_srand to replace rand & srand
34 *
35 * 4 8/26/97 10:15a Jason
36 * corrected the FixCeil function
37 *
38 * 3 8/22/97 12:34p Jason
39 * added FixCeil and FixFloor
40 *
41 * 2 8/21/97 5:57p Samir
42 * Added initialization of random number generator.
43 *
44 * 7 2/27/97 4:47 PM Jeremy
45 * moved inline assembly language functions processor for
46 * fixmul/fixdiv/fixmuldiv into fixmac.h and fixwin32.h
47 *
48 * 6 2/12/97 5:35p Jason
49 * fixed yet another dumb syntax error...will I ever learn?
50 *
51 * 5 2/12/97 5:34p Jason
52 * fixed dumb syntax error
53 *
54 * 4 2/12/97 5:27p Jason
55 * implemented asin,acos,atan2
56 *
57 * 3 2/10/97 1:57p Jason
58 * changed FloatToFix back to what it originally was
59 *
60 * 2 2/07/97 5:44p Matt
61 * Moved fixed-point math funcs from vecmat to fix
62 * Changed trig functions to use small table like D2
63 *
64 * $NoKeywords: $
65 */
66
67 #include <cmath>
68
69 #include "fix.h"cxx . . $ `<file>/fix/fix.h`/
70
71 // Tables for trig functions
72 float sincos_tablecxx . . $ sincos_table.[321]; // 256 entries + 64 sin-only + 1 for interpolation
73 anglecxx . . $ angle# asin_tablecxx . . $ asin_table.[257]; // 1 quadrants worth, +1 for interpolation
74 anglecxx . . $ angle# acos_tablecxx . . $ acos_table.[257];
75
76 // Generate the data for the trig tables
77 void InitMathTablescxx . . $ InitMathTables(49f6e7a06ebc5aa8).() {
78 int i;
79 float rad, s, c;
80
81 for (i = 0; i < 321; i++) {
82 rad = (float)((double)i / 256.0 * 2 * PIcxx . . $ `fix/fix.h:73:9`!);
83 sincos_tablecxx . . $ sincos_table.[i] = stdcxx . . $ std/::sin(rad);
84 }
85
86 for (i = 0; i < 256; i++) {
87
88 s = stdcxx . . $ std/::asin((float)i / 256.0f);
89 c = stdcxx . . $ std/::acos((float)i / 256.0f);
90
91 s = (s / (PIcxx . . $ `fix/fix.h:73:9`! * 2));
92 c = (c / (PIcxx . . $ `fix/fix.h:73:9`! * 2));
93
94 asin_tablecxx . . $ asin_table.[i] = FloatToFixcxx . . $ `fix/fix.h:104:9`!(s);
95 acos_tablecxx . . $ acos_table.[i] = FloatToFixcxx . . $ `fix/fix.h:104:9`!(c);
96 }
97
98 asin_tablecxx . . $ asin_table.[256] = asin_tablecxx . . $ asin_table.[255];
99 acos_tablecxx . . $ acos_table.[256] = acos_tablecxx . . $ acos_table.[255];
100 }
101
102 // Returns the sine of the given angle. Linearly interpolates between two entries in a 256-entry table
103 float FixSincxx . . $ FixSin(9a38df040a81ca35).(anglecxx . . $ angle# a) {
104 int i, f;
105 float s0, s1;
106
107 i = (a >> 8) & 0xff;
108 f = a & 0xff;
109
110 s0 = sincos_tablecxx . . $ sincos_table.[i];
111 s1 = sincos_tablecxx . . $ sincos_table.[i + 1];
112 return (float)(s0 + ((s1 - s0) * (double)f / 256.0));
113 }
114
115 // Returns the cosine of the given angle. Linearly interpolates between two entries in a 256-entry table
116 float FixCoscxx . . $ FixCos(9a38df040a81ca35).(anglecxx . . $ angle# a) {
117 int i, f;
118 float c0, c1;
119
120 i = (a >> 8) & 0xff;
121 f = a & 0xff;
122
123 c0 = sincos_tablecxx . . $ sincos_table.[i + 64];
124 c1 = sincos_tablecxx . . $ sincos_table.[i + 64 + 1];
125 return (float)(c0 + ((c1 - c0) * (double)f / 256.0));
126 }
127
128 // Returns the sine of the given angle, but does no interpolation
129 float FixSinFastcxx . . $ FixSinFast(9a38df040a81ca35).(anglecxx . . $ angle# a) {
130 int i;
131
132 i = ((a + 0x80) >> 8) & 0xff;
133
134 return sincos_tablecxx . . $ sincos_table.[i];
135 }
136
137 // Returns the cosine of the given angle, but does no interpolation
138 float FixCosFastcxx . . $ FixCosFast(9a38df040a81ca35).(anglecxx . . $ angle# a) {
139 int i;
140
141 i = ((a + 0x80) >> 8) & 0xff;
142
143 return sincos_tablecxx . . $ sincos_table.[i + 64];
144 }
145
146 // use this instead of:
147 // for: (int)floor(x+0.5f) use FloatRound(x)
148 // (int)ceil(x-0.5f) use FloatRound(x)
149 // (int)floor(x-0.5f) use FloatRound(x-1.0f)
150 // (int)floor(x) use FloatRound(x-0.5f)
151 // for values in the range -2048 to 2048
152
153 // Set a vector to {0,0,0}
154 int FloatRoundcxx . . $ FloatRound(1bb0578f3efdc255).(float x) {
155 float nf;
156 nf = x + 8390656.0f;
157 return ((*((int *)&nf)) & 0x7FFFFF) - 2048;
158 }
159
160 // A fast way to convert floats to fix
161 fixcxx . . $ fix# FloatToFixFastcxx . . $ FloatToFixFast(1bb0578f3efdc255).(float x) {
162
163 float nf;
164 nf = x * 65536.0f + 8390656.0f;
165 return ((*((int *)&nf)) & 0x7FFFFF) - 2048;
166 }
167
168 // Get rid of the "no return value" warnings in the next three functions
169 #pragma warning(disable : 4035)
170
171 // compute inverse sine
172 anglecxx . . $ angle# FixAsincxx . . $ FixAsin(a7602afe0c5a7a59).(float v) {
173 fixcxx . . $ fix# vv;
174 int i, f, aa;
175
176 vv = FloatToFixcxx . . $ `fix/fix.h:104:9`!(std::fabs(v));
177
178 if (vv >= F1_0cxx . . $ `fix/fix.h:81:9`!) // check for out of range
179 return 0x4000;
180
181 i = (vv >> 8) & 0xff;
182 f = vv & 0xff;
183
184 aa = asin_tablecxx . . $ asin_table.[i];
185 aa = aa + (((asin_tablecxx . . $ asin_table.[i + 1] - aa) * f) >> 8);
186
187 if (v < 0)
188 aa = F1_0cxx . . $ `fix/fix.h:81:9`! - aa;
189
190 return aa;
191 }
192
193 // compute inverse cosine
194 anglecxx . . $ angle# FixAcoscxx . . $ FixAcos(a7602afe0c5a7a59).(float v) {
195 fixcxx . . $ fix# vv;
196 int i, f, aa;
197
198 vv = FloatToFixcxx . . $ `fix/fix.h:104:9`!(std::fabs(v));
199
200 if (vv >= F1_0cxx . . $ `fix/fix.h:81:9`!) // check for out of range
201 return 0;
202
203 i = (vv >> 8) & 0xff;
204 f = vv & 0xff;
205
206 aa = acos_tablecxx . . $ acos_table.[i];
207 aa = aa + (((acos_tablecxx . . $ acos_table.[i + 1] - aa) * f) >> 8);
208
209 if (v < 0)
210 aa = 0x8000 - aa;
211
212 return aa;
213 }
214
215 // given cos & sin of an angle, return that angle.
216 // parms need not be normalized, that is, the ratio of the parms cos/sin must
217 // equal the ratio of the actual cos & sin for the result angle, but the parms
218 // need not be the actual cos & sin.
219 // NOTE: this is different from the standard C atan2, since it is left-handed.
220 anglecxx . . $ angle# FixAtan2cxx . . $ FixAtan2(e98d38e0c629e912).(float cos, float sin) {
221 float q, m;
222 anglecxx . . $ angle# t;
223
224 // find smaller of two
225
226 q = (sin * sin) + (cos * cos);
227
228 m = stdcxx . . $ std/::sqrt(q);
229
230 if (m == 0)
231 return 0;
232
233 if (stdcxx . . $ std/::fabs(sin) < stdcxx . . $ std/::fabs(cos)) {
234 // sin is smaller, use arcsin
235 t = FixAsincxx . . $ FixAsin(a7602afe0c5a7a59).(sin / m);
236 if (cos < 0)
237 t = 0x8000 - t;
238
239 return t;
240 } else {
241 t = FixAcoscxx . . $ FixAcos(a7602afe0c5a7a59).(cos / m);
242 if (sin < 0)
243 t = F1_0cxx . . $ `fix/fix.h:81:9`! - t;
244
245 return t;
246 }
247 }
248
249 // Does a ceiling operation on a fixed number
250 fixcxx . . $ fix# FixCeilcxx . . $ FixCeil(7864480464b09eea).(fixcxx . . $ fix# num) {
251 int int_num;
252 fixcxx . . $ fix# new_num;
253
254 int_num = FixToIntcxx . . $ `fix/fix.h:108:9`!(num);
255
256 if (num & 0xFFFF) {
257 new_num = IntToFixcxx . . $ `fix/fix.h:105:9`!(int_num + 1);
258 return new_num;
259 }
260
261 new_num = IntToFixcxx . . $ `fix/fix.h:105:9`!(int_num);
262 return (new_num);
263 }
264
265 // Floors a fixed number
266 fixcxx . . $ fix# FixFloorcxx . . $ FixFloor(7864480464b09eea).(fixcxx . . $ fix# num) {
267 int int_num = FixToIntcxx . . $ `fix/fix.h:108:9`!(num);
268
269 return (IntToFixcxx . . $ `fix/fix.h:105:9`!(int_num));
270 }