4141#include < float.h>
4242#include < assert.h>
4343
44+ // Include GameMath headers when deterministic math is enabled
45+ // Use __has_include because wwmath.h is transitively included by targets that may not link gamemath.
46+ // Must be outside class WWMath to prevent injecting global symbols into the class.
47+ #if defined(__has_include) && __has_include("gmath.h")
48+ #include " gmath.h"
49+ #define USE_DETERMINISTIC_MATH (1 )
50+ #endif
51+
4452/*
4553** Some global constants.
4654*/
@@ -139,19 +147,10 @@ static WWINLINE float Asin(float val);
139147// Upstream reference: Okladnoj, PR #2670
140148// https://github.com/TheSuperHackers/GeneralsGameCode/pull/2670
141149
142- // Include GameMath headers when deterministic math is enabled
143- // Note: GameMath integration is pending. This header will be populated when
144- // GameMath submodule or library is available. For now, wrappers fallback to CRT.
145- #ifdef USE_DETERMINISTIC_MATH
146- // TODO: Uncomment when GameMath library is integrated as submodule
147- // #include "GameMath/deterministic_math.h"
148- #endif
149-
150150static WWINLINE float Atan (float x)
151151{
152152#ifdef USE_DETERMINISTIC_MATH
153- // TODO: return GameMath::Atan(x);
154- return static_cast <float >(atan (x));
153+ return gm_atanf (x);
155154#else
156155 return static_cast <float >(atan (x));
157156#endif
@@ -160,8 +159,7 @@ static WWINLINE float Atan(float x)
160159static WWINLINE float Atan2 (float y, float x)
161160{
162161#ifdef USE_DETERMINISTIC_MATH
163- // TODO: return GameMath::Atan2(y, x);
164- return static_cast <float >(atan2 (y, x));
162+ return gm_atan2f (y, x);
165163#else
166164 return static_cast <float >(atan2 (y, x));
167165#endif
@@ -172,28 +170,25 @@ static WWINLINE float Atan2(float y, float x)
172170static WWINLINE float SinTrig (float x)
173171{
174172#ifdef USE_DETERMINISTIC_MATH
175- // TODO: return GameMath::Sin(x);
176- return Sin (x);
173+ return gm_sinf (x);
177174#else
178- return Sin (x);
175+ return sinf (x);
179176#endif
180177}
181178
182179static WWINLINE float CosTrig (float x)
183180{
184181#ifdef USE_DETERMINISTIC_MATH
185- // TODO: return GameMath::Cos(x);
186- return Cos (x);
182+ return gm_cosf (x);
187183#else
188- return Cos (x);
184+ return cosf (x);
189185#endif
190186}
191187
192188static WWINLINE float TanTrig (float x)
193189{
194190#ifdef USE_DETERMINISTIC_MATH
195- // TODO: return GameMath::Tan(x);
196- return tanf (x);
191+ return gm_tanf (x);
197192#else
198193 return tanf (x);
199194#endif
@@ -202,34 +197,37 @@ static WWINLINE float TanTrig(float x)
202197static WWINLINE float ACosTrig (float x)
203198{
204199#ifdef USE_DETERMINISTIC_MATH
205- // TODO: return GameMath::Acos(x);
206- return Acos (x);
200+ return gm_acosf (x);
207201#else
208- return Acos (x);
202+ return acosf (x);
209203#endif
210204}
211205
212206static WWINLINE float ASinTrig (float x)
213207{
214208#ifdef USE_DETERMINISTIC_MATH
215- // TODO: return GameMath::Asin(x);
216- return Asin (x);
209+ return gm_asinf (x);
217210#else
218- return Asin (x);
211+ return asinf (x);
219212#endif
220213}
221214
222215// Phase 4: Origin sqrt gateway for geometry (Coord2D/Coord3D length calculations)
223216// Must dispatch to deterministic Sqrt when enabled
224- static WWINLINE double SqrtOrigin (double x)
225- {
226- #ifdef USE_DETERMINISTIC_MATH
227- // TODO: return GameMath::Sqrt(x);
228- return sqrt (x);
217+ // Origin wrappers: replace bare CRT math calls in GameLogic.
218+ // Each wrapper preserves the exact type (float vs double) of the vanilla CRT call.
219+ // Note: double overloads narrow to float before calling GameMath (gm_*f).
220+ // GameMath only provides float-precision functions. All call sites pass float-width
221+ // values, so the narrowing is lossless in practice.
222+ #if USE_DETERMINISTIC_MATH
223+ static WWINLINE double SqrtOrigin (double x) { return (double )gm_sqrtf ((float )x); }
224+ static WWINLINE float SqrtfOrigin (float x) { return gm_sqrtf (x); }
225+ static WWINLINE double Atan2Origin (double y, double x) { return (double )gm_atan2f ((float )y, (float )x); }
229226#else
230- return sqrt (x);
227+ static WWINLINE double SqrtOrigin (double x) { return sqrt (x); }
228+ static WWINLINE float SqrtfOrigin (float x) { return sqrtf (x); }
229+ static WWINLINE double Atan2Origin (double y, double x) { return atan2 (y, x); }
231230#endif
232- }
233231static WWINLINE float Sign (float val);
234232static WWINLINE float Ceil (float val) { return ceilf (val); }
235233static WWINLINE float Floor (float val) { return floorf (val); }
0 commit comments