102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
fixed_point.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
6#include "pcdogs/math/types.h"
9#include <math.h>
10
11/* Native Q12 division faults if the signed quotient cannot fit in EAX. */
12static int32_t divide_q12(int32_t numerator, int32_t divisor)
13{
14 int64_t dividend = (int64_t)numerator * MATH_Q12_ONE;
15 uint32_t low = (uint32_t)dividend;
16 int32_t high = (int32_t)(dividend >> 32);
17
18 __asm__ volatile("idivl %[divisor]" : "+a"(low), "+d"(high) : [divisor] "rm"(divisor) : "cc");
19 return (int32_t)low;
20}
21
22// Original pcdogs.exe: 0x406380
23// Evaluates symmetric quadratic easing around the Q12 midpoint. Retains integer truncation and the native divide-overflow fault for out-of-range inputs.
24int32_t Math_EaseInOutQuadraticQ12(int32_t input_q12)
25{
26 int32_t x = input_q12 >= MATH_Q12_HALF ? MATH_Q12_ONE - input_q12 : input_q12;
27 int32_t square_q12 = (int32_t)(((int64_t)x * x) >> MATH_Q12_SHIFT);
28 int32_t twice_square = divide_q12(square_q12, MATH_Q12_HALF);
29
30 return input_q12 >= MATH_Q12_HALF ? MATH_Q12_ONE - twice_square : twice_square;
31}
32
33// Original pcdogs.exe: 0x415e20
34// Normalizes the cross product of two triangle edges with the recovered floating-point intermediate precision. Degenerate triangles are not guarded against division by zero.
35struct Math_Vec3F *Math_CalculateFaceNormal(struct Math_Vec3F *out_normal, struct Math_Vec3F const *point0, struct Math_Vec3F const *point1, struct Math_Vec3F const *point2)
36{
37 /* 415e20: x87 keeps the first edge and second Z component in extended
38 precision, but spills second X/Y and normal X/Y to single precision. */
39 long double ax = (long double)point1->x - point0->x;
40 long double ay = (long double)point1->y - point0->y;
41 long double az = (long double)point1->z - point0->z;
42 volatile float bx = (float)((long double)point2->x - point1->x);
43 volatile float by = (float)((long double)point2->y - point1->y);
44 long double bz = (long double)point2->z - point1->z;
45 volatile float nx = (float)(ay * bz - az * by);
46 volatile float ny = (float)(az * bx - ax * bz);
47 long double nz = ax * by - ay * bx;
48 long double scale = 1.0L / sqrtl(nz * nz + (long double)ny * ny + (long double)nx * nx);
49
50 out_normal->x = (float)(nx * scale);
51 out_normal->y = (float)(ny * scale);
52 out_normal->z = (float)(nz * scale);
53
54 return out_normal;
55}
56
57// Original pcdogs.exe: 0x424f90
58// Approximates distance by sorting absolute coordinate differences and applying distinct weighted sums for three, two, or one nonzero components.
59int32_t Math_ApproxDistance3DWeighted(int32_t dx, int32_t dy, int32_t dz)
60{
61 int32_t abs_dx = dx;
62
63 if (abs_dx < 0)
64 abs_dx = -(abs_dx);
65
66 int32_t abs_dy = dy;
67
68 if (abs_dy < 0)
69 abs_dy = -(abs_dy);
70
71 int32_t abs_dz = dz;
72
73 if (abs_dz < 0)
74 abs_dz = -(abs_dz);
75
76 int32_t min_component;
77
78 if (abs_dx <= abs_dy)
79 {
80 min_component = abs_dx;
81 abs_dx = abs_dy;
82 }
83 else
84 min_component = abs_dy;
85
86 int32_t mid_component;
87
88 if (abs_dz > abs_dx)
89 {
90 mid_component = abs_dx;
91 abs_dx = abs_dz;
92 }
93 else if (abs_dz >= min_component)
94 mid_component = abs_dz;
95 else
96 {
97 mid_component = min_component;
98 min_component = abs_dz;
99 }
100
101 if (min_component)
102 return ((mid_component * MATH_DISTANCE_3D_MIDDLE_WEIGHT) >> 3) + ((abs_dx * MATH_DISTANCE_3D_MAX_WEIGHT) >> 4) + ((min_component * MATH_DISTANCE_3D_MIN_WEIGHT) >> 5);
103
104 if (!mid_component)
105 return abs_dx;
106
107 return ((abs_dx * MATH_DISTANCE_2D_MAX_WEIGHT) >> 7) + ((mid_component * MATH_DISTANCE_2D_MIN_WEIGHT) >> 7);
108}
109
110// Original pcdogs.exe: 0x425020
111// Computes an integer square root scaled for Q12 using 22 unsigned bit-pair iterations.
112int32_t Math_SqrtFP12(uint32_t value)
113{
114 /* Native 0x425031 SHR and 0x425044 JB require unsigned arithmetic. */
115 uint32_t remainder = 0, root = 0;
116
117 for (unsigned pair = 0; pair < MATH_SQRT_Q12_PAIR_COUNT; ++pair)
118 {
119 remainder = (remainder << 2) | (value >> 30);
120 value <<= 2;
121 root <<= 1;
122
123 if (remainder >= root * 2 + 1)
124 {
125 remainder -= root * 2 + 1;
126 ++root;
127 }
128 }
129
130 return (int32_t)root;
131}
132
133// Original pcdogs.exe: 0x425050
134// Computes an integer square root scaled for Q14 using 23 unsigned bit-pair iterations.
135int32_t Math_SqrtFP14(uint32_t value)
136{
137 uint32_t remainder = 0;
138 uint32_t root = 0;
139
140 for (unsigned pair = 0; pair < MATH_SQRT_Q14_PAIR_COUNT; ++pair)
141 {
142 root <<= 1;
143 remainder = (remainder << 2) | (value >> 30);
144 value <<= 2;
145
146 if (remainder >= root * 2 + 1)
147 {
148 remainder -= root * 2 + 1;
149 ++root;
150 }
151 }
152
153 return root;
154}
155
156// Original pcdogs.exe: 0x425c40
157// Interpolates triangle height using the recovered floating-point precision and integer conversion. Degenerate projected triangles retain the original asymmetric vertex-height fallback.
158int32_t Math_CalculateTriangleHeight(int32_t local_xq6, int32_t local_zq6, struct Collision_Vertex const *tri_a, struct Collision_Vertex const *tri_b, struct Collision_Vertex const *tri_c)
159{
160 const long double ab_x = (tri_a->pos.x - tri_b->pos.x) / MATH_Q6_SCALE_LONG_DOUBLE;
161 const long double ab_y = (tri_a->pos.y - tri_b->pos.y) / MATH_Q6_SCALE_LONG_DOUBLE;
162 const long double ab_z = (tri_a->pos.z - tri_b->pos.z) / MATH_Q6_SCALE_LONG_DOUBLE;
163 const long double bc_x = (tri_b->pos.x - tri_c->pos.x) / MATH_Q6_SCALE_LONG_DOUBLE;
164 const long double bc_y = (tri_b->pos.y - tri_c->pos.y) / MATH_Q6_SCALE_LONG_DOUBLE;
165
166 /* These four single-precision stores precede the native zero-area test. */
167 volatile float bc_z = (float)((tri_b->pos.z - tri_c->pos.z) / MATH_Q6_SCALE_LONG_DOUBLE);
168 volatile float x = (float)(local_xq6 / MATH_Q12_SCALE_LONG_DOUBLE - tri_c->pos.x / MATH_Q6_SCALE_LONG_DOUBLE);
169 volatile float z = (float)(local_zq6 / MATH_Q12_SCALE_LONG_DOUBLE - tri_c->pos.z / MATH_Q6_SCALE_LONG_DOUBLE);
170 const long double determinant = bc_z * ab_x - bc_x * ab_z;
171 volatile float divisor = (float)determinant;
172
173 /* FCOMP at 425d3b compares the unspilled determinant and leaves the
174 comparison in the x87 status word, including on the fallback path. */
175 uint16_t status;
176 __asm__ volatile("ftst\n\tfnstsw %%ax" : "=a"(status) : "t"(determinant));
177 if (!(status & X87_STATUS_C3))
178 {
179 const long double ab_weight = (long double)x * bc_z - z * bc_x;
180 const long double bc_weight = z * ab_x - x * ab_z;
181 const long double height = (ab_weight * ab_y + bc_weight * bc_y) / divisor + tri_c->pos.y / MATH_Q6_SCALE_LONG_DOUBLE;
182 return (int32_t)__ftol_msvc(height * MATH_Q12_SCALE_LONG_DOUBLE);
183 }
184
185 /* Native branches 425d50..425d78 are asymmetric; preserve them exactly. */
186 int32_t y = tri_b->pos.y;
187
188 if (tri_a->pos.y > tri_b->pos.y)
189 {
190 if (tri_a->pos.y > tri_c->pos.y)
191 return tri_a->pos.y * 64;
192
193 y = tri_c->pos.y;
194 }
195 else if (tri_b->pos.y > tri_c->pos.y)
196 y = tri_c->pos.y;
197
198 return y * 64;
199}
200
201// Original pcdogs.exe: 0x43ae20
202// Moves a wrapped 12-bit angle toward its target by a signed 16-bit half-step, accepting the target directly for the sentinel current angle or sufficiently large step.
203uint16_t Math_ClampAngleDelta(int16_t target_angle, int16_t current_angle, int16_t max_delta)
204{
205 /* 0x43ae32: SAR DX,1, preserving a signed 16-bit half-step. */
206 int16_t half_step = max_delta >> 1;
207
208 if ((uint16_t)current_angle == MATH_ANGLE_FULL_TURN || half_step >= MATH_ANGLE_HALF_TURN)
209 return (uint16_t)target_angle;
210
211 int32_t delta = (half_step - current_angle + target_angle) & MATH_ANGLE_WRAP_MASK;
212
213 if ((int16_t)delta <= max_delta)
214 return (uint16_t)target_angle;
215
216 if (((delta - half_step) & MATH_ANGLE_WRAP_MASK) <= MATH_ANGLE_HALF_TURN)
217 return (current_angle + half_step) & MATH_ANGLE_WRAP_MASK;
218
219 return (current_angle - half_step) & MATH_ANGLE_WRAP_MASK;
220}
221
222// Original pcdogs.exe: 0x4403e0
223// Advances the global random seed with unsigned 32-bit arithmetic and scales its upper bits by the requested range.
224uint32_t Math_GenerateRandom(uint32_t range)
225{
226 /* Native DIV, JBE and SHR operate on unsigned 32-bit register values. */
227 uint32_t seed = (uint32_t)g_randomSeed;
228
230
231 if (seed > MATH_RANDOM_MODULUS)
232 seed -= MATH_RANDOM_MODULUS;
233 else if (seed == 0)
234 seed = 1;
235
236 g_randomSeed = seed;
237
238 return ((seed >> 15) * range) >> 16;
239}
240
241// Original pcdogs.exe: 0x440480
242// Normalizes and stores a new unsigned seed, mapping zero to one and subtracting the modulus once above its limit. Returns the previous seed bits.
243uint32_t Math_SetRandomSeed(uint32_t seed)
244{
245 uint32_t previous_seed = (uint32_t)g_randomSeed;
246
247 if (seed == 0)
248 seed = 1;
249 else if (seed > MATH_RANDOM_MODULUS)
250 seed -= MATH_RANDOM_MODULUS;
251
252 g_randomSeed = seed;
253 return previous_seed;
254}
255
256// Original pcdogs.exe: 0x440710
257// Looks up Q14 sine for a wrapped 12-bit angle, reading unsigned table magnitudes and negating values in the second half-cycle.
258int32_t Math_SinQ14(int32_t angle)
259{
260 int32_t angle_index = angle & MATH_ANGLE_WRAP_MASK;
261
262 if (angle_index >= MATH_ANGLE_HALF_TURN)
263 {
264 int32_t negative_half_magnitude = (uint16_t)g_sinTableQ14[angle_index & MATH_ANGLE_HALF_MASK];
265
266 return -(negative_half_magnitude);
267 }
268
269 return (uint16_t)g_sinTableQ14[angle_index];
270}
271
272// Original pcdogs.exe: 0x440740
273// Calculates a wrapped angle through reciprocal and ratio tables using unsigned product truncation. Returns 0x1000 when both shifted inputs vanish; unchecked reads retain original image pointer values.
274int32_t Math_CalculateAtan2Fast(int32_t y, int32_t x)
275{
276 int32_t abs_y_div64 = y >> 6;
277 int32_t abs_x_div64 = x >> 6;
278
279 if (!abs_y_div64 && !abs_x_div64)
281
282 if (abs_y_div64 < 0)
283 abs_y_div64 = -abs_y_div64;
284
285 if (abs_x_div64 < 0)
286 abs_x_div64 = -abs_x_div64;
287
288 int32_t angle;
289
290 if (abs_y_div64 <= abs_x_div64)
291 {
292 uint32_t x_reciprocal_q12 = image_read_table_word(g_atanReciprocalQ12, (uint32_t)abs_x_div64);
293 uint32_t ratio_q12 = ((uint32_t)abs_x_div64 / 2 + (uint32_t)abs_y_div64 * MATH_Q12_HALF) * x_reciprocal_q12;
294
296 }
297 else
298 {
299 uint32_t y_reciprocal_q12 = image_read_table_word(g_atanReciprocalQ12, (uint32_t)abs_y_div64);
300 uint32_t ratio_q12 = ((uint32_t)abs_y_div64 / 2 + (uint32_t)abs_x_div64 * MATH_Q12_HALF) * y_reciprocal_q12;
301
302 angle = (int16_t)image_read_table_word(g_atanRatioAngleTableQ12, ratio_q12 >> MATH_Q12_SHIFT);
303 }
304
305 if (y < 0)
306 angle = MATH_ANGLE_HALF_TURN - angle;
307
308 if (x < 0)
309 angle = MATH_ANGLE_FULL_TURN - angle;
310
311 return angle & MATH_ANGLE_WRAP_MASK;
312}
313
314/* Both table lookups round the ratio using a wrapping 32-bit numerator and DIV. */
315static uint32_t angle_ratio_index(int32_t minor, int32_t major)
316{
317 uint32_t rounded = (uint32_t)minor * MATH_Q12_HALF + (uint32_t)(major >> 1);
318 return rounded / (uint32_t)major;
319}
320
321// Original pcdogs.exe: 0x4407e0
322// Calculates a wrapped 12-bit angle from the ratio table with quadrant correction, returning 0x1000 for the zero vector.
323int16_t Math_CalculateAtan2FP12(int32_t y, int32_t x)
324{
325 int32_t abs_x = x;
326 int32_t abs_y = y;
327
328 if (!abs_y && !abs_x)
330
331 if (abs_y < 0)
332 abs_y = -abs_y;
333
334 if (abs_x < 0)
335 abs_x = -abs_x;
336
337 int32_t angle;
338
339 if (abs_y <= abs_x)
341 else
342 angle = (int32_t)g_atanRatioAngleTableQ12[angle_ratio_index(abs_x, abs_y)];
343
344 if (y < 0)
345 angle = MATH_ANGLE_HALF_TURN - angle;
346
347 if (x < 0)
348 angle = MATH_ANGLE_FULL_TURN - angle;
349
350 return (uint16_t)angle & MATH_ANGLE_WRAP_MASK;
351}
352
353// Original pcdogs.exe: 0x440870
354// Converts a signed ratio to a wrapped arcsine angle, saturating magnitudes at a quarter-turn and returning 0x1000 for a zero denominator.
355int32_t Math_AsinRatioFP12(int32_t numerator, int32_t denominator)
356{
357 int32_t cos_input = denominator;
358 int32_t sin_input = numerator;
359 int32_t abs_sin = sin_input;
360 int32_t abs_cos = cos_input;
361
362 if (!cos_input)
364
365 if (sin_input < 0)
366 abs_sin = -(sin_input);
367
368 if (cos_input < 0)
369 {
370 abs_cos = -(cos_input);
371 sin_input = -(sin_input);
372 }
373
374 int32_t angle;
375
376 if (abs_sin < abs_cos)
377 angle = g_asinRatioAngleTableQ12[angle_ratio_index(abs_sin, abs_cos)];
378 else
380
381 if (sin_input < 0 && angle > 0)
382 return MATH_ANGLE_FULL_TURN - angle;
383
384 return angle;
385}
386
387// Original pcdogs.exe: 0x4408d0
388// Converts the arcsine-ratio result to its complementary angle and adjusts it to the recovered turn range.
389int32_t Math_AcosRatioFP12(int32_t numerator, int32_t denominator)
390{
391 int32_t atan_angle = (int32_t)Math_AsinRatioFP12(numerator, denominator);
392 int32_t cos_phase_angle = MATH_ANGLE_QUARTER_TURN - atan_angle;
393
394 if (cos_phase_angle < 0)
395 return cos_phase_angle + MATH_ANGLE_FULL_TURN;
396
397 if (cos_phase_angle <= MATH_ANGLE_FULL_TURN)
398 return cos_phase_angle;
399
400 return cos_phase_angle - MATH_ANGLE_FULL_TURN;
401}
402
403// Original pcdogs.exe: 0x442fb0
404// Evaluates a Q12 ease-in, linear, and ease-out curve, normalizing durations whose sum exceeds one. Retains integer truncation and native signed divide faults.
405int32_t Math_EaseInOut(int32_t t_q12, int32_t ease_in_q12, int32_t ease_out_q12)
406{
407 int32_t ease_duration_sum = ease_in_q12 + ease_out_q12;
408
409 if (!ease_duration_sum)
410 return t_q12;
411
412 if (ease_duration_sum > MATH_Q12_ONE)
413 {
414 ease_in_q12 = divide_q12(ease_in_q12, ease_duration_sum);
415 ease_out_q12 = divide_q12(ease_out_q12, ease_duration_sum);
416 }
417
418 int32_t velocity_scale_q12 = MATH_Q12_SQUARED_ONE / ((2 * MATH_Q12_ONE) - ease_in_q12 - ease_out_q12);
419
420 if (t_q12 < ease_in_q12)
421 {
422 int32_t square_q12 = (int32_t)(((int64_t)t_q12 * t_q12) >> MATH_Q12_SHIFT);
423 int32_t product_q12 = (int32_t)(((int64_t)square_q12 * velocity_scale_q12) >> MATH_Q12_SHIFT);
424
425 return divide_q12(product_q12, ease_in_q12);
426 }
427
428 if (t_q12 < MATH_Q12_ONE - ease_out_q12)
429 {
430 int32_t linear_term = t_q12 * 2 - ease_in_q12;
431
432 return (int32_t)(((int64_t)linear_term * velocity_scale_q12) >> MATH_Q12_SHIFT);
433 }
434
435 int32_t remaining_q12 = MATH_Q12_ONE - t_q12;
436 int32_t square_q12 = (int32_t)(((int64_t)remaining_q12 * remaining_q12) >> MATH_Q12_SHIFT);
437 int32_t product_q12 = (int32_t)(((int64_t)square_q12 * velocity_scale_q12) >> MATH_Q12_SHIFT);
438
439 return MATH_Q12_ONE - divide_q12(product_q12, ease_out_q12);
440}
int32_t Math_SinQ14(int32_t angle)
int32_t Math_SqrtFP14(uint32_t value)
int32_t Math_CalculateTriangleHeight(int32_t local_xq6, int32_t local_zq6, struct Collision_Vertex const *tri_a, struct Collision_Vertex const *tri_b, struct Collision_Vertex const *tri_c)
int32_t Math_SqrtFP12(uint32_t value)
int32_t Math_EaseInOut(int32_t t_q12, int32_t ease_in_q12, int32_t ease_out_q12)
static int32_t divide_q12(int32_t numerator, int32_t divisor)
Definition fixed_point.c:12
uint16_t Math_ClampAngleDelta(int16_t target_angle, int16_t current_angle, int16_t max_delta)
static uint32_t angle_ratio_index(int32_t minor, int32_t major)
int32_t Math_AcosRatioFP12(int32_t numerator, int32_t denominator)
uint32_t Math_SetRandomSeed(uint32_t seed)
struct Math_Vec3F * Math_CalculateFaceNormal(struct Math_Vec3F *out_normal, struct Math_Vec3F const *point0, struct Math_Vec3F const *point1, struct Math_Vec3F const *point2)
Definition fixed_point.c:35
int32_t Math_ApproxDistance3DWeighted(int32_t dx, int32_t dy, int32_t dz)
Definition fixed_point.c:59
int32_t Math_EaseInOutQuadraticQ12(int32_t input_q12)
Definition fixed_point.c:24
int16_t Math_CalculateAtan2FP12(int32_t y, int32_t x)
uint32_t Math_GenerateRandom(uint32_t range)
int32_t Math_AsinRatioFP12(int32_t numerator, int32_t denominator)
int32_t Math_CalculateAtan2Fast(int32_t y, int32_t x)
#define MATH_Q12_SHIFT
Definition constants.h:8
#define MATH_ANGLE_QUARTER_TURN
Definition constants.h:15
#define MATH_RANDOM_MULTIPLIER
Definition constants.h:19
#define MATH_Q6_SCALE_LONG_DOUBLE
Definition constants.h:6
#define MATH_ANGLE_HALF_MASK
Definition constants.h:17
#define MATH_RANDOM_QUOTIENT
Definition constants.h:20
#define MATH_DISTANCE_3D_MIDDLE_WEIGHT
Definition constants.h:27
#define MATH_ANGLE_WRAP_MASK
Definition constants.h:16
#define MATH_SQRT_Q14_PAIR_COUNT
Definition constants.h:23
#define MATH_Q12_ONE
Definition constants.h:9
#define MATH_ANGLE_HALF_TURN
Definition constants.h:14
#define MATH_RANDOM_REMAINDER
Definition constants.h:21
#define MATH_Q12_SQUARED_ONE
Definition constants.h:12
#define MATH_DISTANCE_3D_MIN_WEIGHT
Definition constants.h:28
#define MATH_SQRT_Q12_PAIR_COUNT
Definition constants.h:22
#define MATH_Q12_SCALE_LONG_DOUBLE
Definition constants.h:7
#define MATH_DISTANCE_2D_MAX_WEIGHT
Definition constants.h:29
#define MATH_Q12_HALF
Definition constants.h:10
#define MATH_DISTANCE_3D_MAX_WEIGHT
Definition constants.h:26
#define MATH_ANGLE_FULL_TURN
Definition constants.h:13
#define MATH_DISTANCE_2D_MIN_WEIGHT
Definition constants.h:30
#define MATH_RANDOM_MODULUS
Definition constants.h:18
#define X87_STATUS_C3
Definition constants.h:78
int64_t __ftol_msvc(long double value)
uint16_t image_read_table_word(const void *table, uint32_t index)
Definition image_data.c:287
int16_t z
Definition types.h:111
struct Collision_Vertex::@111126275124057154070202104324102117233165176236::@061322026074334072070347216127267241215241121036 pos
int16_t x
Definition types.h:109
int16_t y
Definition types.h:110
float z
Definition types.h:448
float y
Definition types.h:447
float x
Definition types.h:446
#define g_atanRatioAngleTableQ12
Definition values.h:126
#define g_randomSeed
Definition values.h:120
#define g_asinRatioAngleTableQ12
Definition values.h:129
#define g_atanReciprocalQ12
Definition values.h:132
#define g_sinTableQ14
Definition values.h:123