SDL: Removed SDL_stdinc.h dependency on HAVE_M_PI, added SDL_M_PIl (double) and SDL_M_PIf (float) instead

From 670f1df46924423a86eb49ea5487b3051d050bc9 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 25 Nov 2022 10:33:37 -0800
Subject: [PATCH] Removed SDL_stdinc.h dependency on HAVE_M_PI, added SDL_M_PIl
 (double) and SDL_M_PIf (float) instead

---
 WhatsNew.txt                             |  1 +
 docs/README-migration.md                 |  4 ++++
 include/SDL_stdinc.h                     | 14 ++++----------
 src/events/SDL_gesture.c                 |  6 +++---
 src/haptic/linux/SDL_syshaptic.c         |  7 +------
 src/joystick/hidapi/SDL_hidapi_ps4.c     |  2 +-
 src/joystick/hidapi/SDL_hidapi_ps5.c     |  2 +-
 src/joystick/hidapi/SDL_hidapi_steam.c   |  6 +++---
 src/joystick/hidapi/SDL_hidapi_switch.c  |  8 ++++----
 src/joystick/hidapi/SDL_hidapi_wii.c     |  6 +++---
 src/render/SDL_render.c                  |  2 +-
 src/render/direct3d11/SDL_render_d3d11.c |  6 +++---
 src/render/direct3d12/SDL_render_d3d12.c |  6 +++---
 src/render/software/SDL_rotate.c         |  2 +-
 src/sensor/windows/SDL_windowssensor.c   |  2 +-
 src/stdlib/SDL_stdlib.c                  |  8 ++++----
 16 files changed, 38 insertions(+), 44 deletions(-)

diff --git a/WhatsNew.txt b/WhatsNew.txt
index 4231b8834182..e196247f11c4 100644
--- a/WhatsNew.txt
+++ b/WhatsNew.txt
@@ -6,4 +6,5 @@ This is a list of major changes in SDL's version history.
 ---------------------------------------------------------------------------
 
 General:
+* M_PI is no longer defined in SDL_stdinc.h, now the symbols SDL_M_PIl (double) and SDL_M_PIf (float) are available
 * SDL_GetWindowWMInfo() returns a standard int result code instead of SDL_bool, and takes SDL_SYSWM_CURRENT_VERSION as a new third parameter
diff --git a/docs/README-migration.md b/docs/README-migration.md
index 80fa0f605550..6e83c8a88f24 100644
--- a/docs/README-migration.md
+++ b/docs/README-migration.md
@@ -4,6 +4,10 @@ This guide provides useful information for migrating applications from SDL 2.0 t
 
 We have provided a handy Python script to automate some of this work for you [link to script], and details on the changes are organized by SDL 2.0 header below.
 
+## SDL_stdinc.h
+
+M_PI is no longer defined in SDL_stdinc.h, you can use the new symbols SDL_M_PIl (double) and SDL_M_PIf (float) instead.
+
 ## SDL_syswm.h
 
 This header no longer includes platform specific headers and type definitions, instead allowing you to include the ones appropriate for your use case. You should define one or more of the following to enable the relevant platform-specific support:
diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h
index da643b35c7d7..5f29bb1e8544 100644
--- a/include/SDL_stdinc.h
+++ b/include/SDL_stdinc.h
@@ -80,13 +80,6 @@
 # include <ctype.h>
 #endif
 #ifdef HAVE_MATH_H
-# if defined(_MSC_VER) && !defined(_USE_MATH_DEFINES)
-/* Defining _USE_MATH_DEFINES is required to get M_PI to be defined on
-   Visual Studio.  See http://msdn.microsoft.com/en-us/library/4hwaceh6.aspx
-   for more information.
-*/
-#  define _USE_MATH_DEFINES
-# endif
 # include <math.h>
 #endif
 #ifdef HAVE_FLOAT_H
@@ -615,10 +608,11 @@ extern DECLSPEC int SDLCALL SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size
 extern DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
 extern DECLSPEC int SDLCALL SDL_vasprintf(char **strp, const char *fmt, va_list ap);
 
-#ifndef HAVE_M_PI
-#ifndef M_PI
-#define M_PI    3.14159265358979323846264338327950288   /**< pi */
+#ifndef SDL_M_PIl
+#define SDL_M_PIl   3.141592653589793238462643383279502884L         /**< pi (double) */
 #endif
+#ifndef SDL_M_PIf
+#define SDL_M_PIf   ((float)3.141592653589793238462643383279502884) /**< pi (float) */
 #endif
 
 /**
diff --git a/src/events/SDL_gesture.c b/src/events/SDL_gesture.c
index 2cdb0d594b1f..c35dd9a04c83 100644
--- a/src/events/SDL_gesture.c
+++ b/src/events/SDL_gesture.c
@@ -304,9 +304,9 @@ static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
       -TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-
       -"http://depts.washington.edu/aimgroup/proj/dollar/"
     */
-    double ta = -M_PI/4;
-    double tb = M_PI/4;
-    double dt = M_PI/90;
+    double ta = -SDL_M_PIl/4;
+    double tb = SDL_M_PIl/4;
+    double dt = SDL_M_PIl/90;
     float x1 = (float)(PHI*ta + (1-PHI)*tb);
     float f1 = dollarDifference(points,templ,x1);
     float x2 = (float)((1-PHI)*ta + PHI*tb);
diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c
index a9be7e0d39a9..de8cb27fba6e 100644
--- a/src/haptic/linux/SDL_syshaptic.c
+++ b/src/haptic/linux/SDL_syshaptic.c
@@ -37,11 +37,6 @@
 #include <errno.h>              /* errno, strerror */
 #include <sys/stat.h>           /* stat */
 
-/* Just in case. */
-#ifndef M_PI
-#  define M_PI     3.14159265358979323846
-#endif
-
 
 #define MAX_HAPTICS  32         /* It's doubtful someone has more then 32 evdev */
 
@@ -721,7 +716,7 @@ SDL_SYS_ToDirection(Uint16 *dest, SDL_HapticDirection * src)
                       --> add 45000 in total
                       --> finally convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR.
                     */
-                tmp = (((Sint32) (f * 18000. / M_PI)) + 45000) % 36000;
+                tmp = (((Sint32) (f * 18000.0 / SDL_M_PIl)) + 45000) % 36000;
             tmp = (tmp * 0x8000) / 18000; /* convert to range [0,0xFFFF] */
             *dest = (Uint16) tmp;
         }
diff --git a/src/joystick/hidapi/SDL_hidapi_ps4.c b/src/joystick/hidapi/SDL_hidapi_ps4.c
index 63d720df50ce..e04bf6de2bfd 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps4.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps4.c
@@ -567,7 +567,7 @@ HIDAPI_DriverPS4_ApplyCalibrationData(SDL_DriverPS4_Context *ctx, int index, Sin
 
     /* Convert the raw data to the units expected by SDL */
     if (index < 3) {
-        result = (result / GYRO_RES_PER_DEGREE) * (float)M_PI / 180.0f;
+        result = (result / GYRO_RES_PER_DEGREE) * SDL_M_PIf / 180.0f;
     } else {
         result = (result / ACCEL_RES_PER_G) * SDL_STANDARD_GRAVITY;
     }
diff --git a/src/joystick/hidapi/SDL_hidapi_ps5.c b/src/joystick/hidapi/SDL_hidapi_ps5.c
index 7458adaf4522..cfd88010c56b 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps5.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps5.c
@@ -612,7 +612,7 @@ HIDAPI_DriverPS5_ApplyCalibrationData(SDL_DriverPS5_Context *ctx, int index, Sin
 
     /* Convert the raw data to the units expected by SDL */
     if (index < 3) {
-        result = (result / GYRO_RES_PER_DEGREE) * (float)M_PI / 180.0f;
+        result = (result / GYRO_RES_PER_DEGREE) * SDL_M_PIf / 180.0f;
     } else {
         result = (result / ACCEL_RES_PER_G) * SDL_STANDARD_GRAVITY;
     }
diff --git a/src/joystick/hidapi/SDL_hidapi_steam.c b/src/joystick/hidapi/SDL_hidapi_steam.c
index 9221d12e887a..f2a761e6b556 100644
--- a/src/joystick/hidapi/SDL_hidapi_steam.c
+++ b/src/joystick/hidapi/SDL_hidapi_steam.c
@@ -1260,9 +1260,9 @@ HIDAPI_DriverSteam_UpdateDevice(SDL_HIDAPI_Device *device)
 
                 ctx->timestamp_us += ctx->update_rate_in_us;
 
-                values[0] = (ctx->m_state.sGyroX / 32768.0f) * (2000.0f * ((float)M_PI / 180.0f));
-                values[1] = (ctx->m_state.sGyroZ / 32768.0f) * (2000.0f * ((float)M_PI / 180.0f));
-                values[2] = (ctx->m_state.sGyroY / 32768.0f) * (2000.0f * ((float)M_PI / 180.0f));
+                values[0] = (ctx->m_state.sGyroX / 32768.0f) * (2000.0f * (SDL_M_PIf / 180.0f));
+                values[1] = (ctx->m_state.sGyroZ / 32768.0f) * (2000.0f * (SDL_M_PIf / 180.0f));
+                values[2] = (ctx->m_state.sGyroY / 32768.0f) * (2000.0f * (SDL_M_PIf / 180.0f));
                 SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_GYRO, ctx->timestamp_us, values, 3);
 
                 values[0] = (ctx->m_state.sAccelX / 32768.0f) * 2.0f * SDL_STANDARD_GRAVITY;
diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index 901761fac679..514c954a616a 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -805,7 +805,7 @@ static SDL_bool LoadIMUCalibration(SDL_DriverSwitch_Context* ctx)
 
     if (!WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SPIFlashRead, (uint8_t*)&readParams, sizeof(readParams), &reply)) {
         const float accelScale = SDL_STANDARD_GRAVITY / SWITCH_ACCEL_SCALE;
-        const float gyroScale = (float)M_PI / 180.0f / SWITCH_GYRO_SCALE;
+        const float gyroScale = SDL_M_PIf / 180.0f / SWITCH_GYRO_SCALE;
 
         ctx->m_IMUScaleData.fAccelScaleX = accelScale;
         ctx->m_IMUScaleData.fAccelScaleY = accelScale;
@@ -850,9 +850,9 @@ static SDL_bool LoadIMUCalibration(SDL_DriverSwitch_Context* ctx)
     ctx->m_IMUScaleData.fAccelScaleZ = SWITCH_ACCEL_SCALE_MULT / (float)(SWITCH_ACCEL_SCALE_OFFSET - (float)sAccelRawZ) * SDL_STANDARD_GRAVITY;
 
     /* Gyro scale */
-    ctx->m_IMUScaleData.fGyroScaleX = SWITCH_GYRO_SCALE_MULT / (float)(SWITCH_GYRO_SCALE_OFFSET - (float)sGyroRawX) * (float)M_PI / 180.0f;
-    ctx->m_IMUScaleData.fGyroScaleY = SWITCH_GYRO_SCALE_MULT / (float)(SWITCH_GYRO_SCALE_OFFSET - (float)sGyroRawY) * (float)M_PI / 180.0f;
-    ctx->m_IMUScaleData.fGyroScaleZ = SWITCH_GYRO_SCALE_MULT / (float)(SWITCH_GYRO_SCALE_OFFSET - (float)sGyroRawZ) * (float)M_PI / 180.0f;
+    ctx->m_IMUScaleData.fGyroScaleX = SWITCH_GYRO_SCALE_MULT / (float)(SWITCH_GYRO_SCALE_OFFSET - (float)sGyroRawX) * SDL_M_PIf / 180.0f;
+    ctx->m_IMUScaleData.fGyroScaleY = SWITCH_GYRO_SCALE_MULT / (float)(SWITCH_GYRO_SCALE_OFFSET - (float)sGyroRawY) * SDL_M_PIf / 180.0f;
+    ctx->m_IMUScaleData.fGyroScaleZ = SWITCH_GYRO_SCALE_MULT / (float)(SWITCH_GYRO_SCALE_OFFSET - (float)sGyroRawZ) * SDL_M_PIf / 180.0f;
 
     return SDL_TRUE;
 }
diff --git a/src/joystick/hidapi/SDL_hidapi_wii.c b/src/joystick/hidapi/SDL_hidapi_wii.c
index 3b4e7941141e..e768934338f8 100644
--- a/src/joystick/hidapi/SDL_hidapi_wii.c
+++ b/src/joystick/hidapi/SDL_hidapi_wii.c
@@ -1192,9 +1192,9 @@ static void HandleMotionPlusData(SDL_DriverWii_Context *ctx, SDL_Joystick *joyst
             z *= 2000;
         }
 
-        values[0] = -((float)z / GYRO_RES_PER_DEGREE) * (float)M_PI / 180.0f;
-        values[1] = ((float)x / GYRO_RES_PER_DEGREE) * (float)M_PI / 180.0f;
-        values[2] = ((float)y / GYRO_RES_PER_DEGREE) * (float)M_PI / 180.0f;
+        values[0] = -((float)z / GYRO_RES_PER_DEGREE) * SDL_M_PIf / 180.0f;
+        values[1] = ((float)x / GYRO_RES_PER_DEGREE) * SDL_M_PIf / 180.0f;
+        values[2] = ((float)y / GYRO_RES_PER_DEGREE) * SDL_M_PIf / 180.0f;
         SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_GYRO, 0, values, 3);
     }
 }
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index d103e958ed95..ea6517a6b6d7 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -3679,7 +3679,7 @@ SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture,
         float s_minx, s_miny, s_maxx, s_maxy;
         float c_minx, c_miny, c_maxx, c_maxy;
 
-        const float radian_angle = (float)((M_PI * angle) / 180.0);
+        const float radian_angle = (float)((SDL_M_PIl * angle) / 180.0);
         const float s = SDL_sinf(radian_angle);
         const float c = SDL_cosf(radian_angle);
 
diff --git a/src/render/direct3d11/SDL_render_d3d11.c b/src/render/direct3d11/SDL_render_d3d11.c
index ffba1f561984..371226f2c62a 100644
--- a/src/render/direct3d11/SDL_render_d3d11.c
+++ b/src/render/direct3d11/SDL_render_d3d11.c
@@ -1821,13 +1821,13 @@ D3D11_UpdateViewport(SDL_Renderer * renderer)
             projection = MatrixIdentity();
             break;
         case DXGI_MODE_ROTATION_ROTATE270:
-            projection = MatrixRotationZ(SDL_static_cast(float, M_PI * 0.5f));
+            projection = MatrixRotationZ(SDL_M_PIf * 0.5f);
             break;
         case DXGI_MODE_ROTATION_ROTATE180:
-            projection = MatrixRotationZ(SDL_static_cast(float, M_PI));
+            projection = MatrixRotationZ(SDL_M_PIf);
             break;
         case DXGI_MODE_ROTATION_ROTATE90:
-            projection = MatrixRotationZ(SDL_static_cast(float, -M_PI * 0.5f));
+            projection = MatrixRotationZ(-SDL_M_PIf * 0.5f);
             break;
         default:
             return SDL_SetError("An unknown DisplayOrientation is being used");
diff --git a/src/render/direct3d12/SDL_render_d3d12.c b/src/render/direct3d12/SDL_render_d3d12.c
index ba8c3e9e5c81..032a4589402c 100644
--- a/src/render/direct3d12/SDL_render_d3d12.c
+++ b/src/render/direct3d12/SDL_render_d3d12.c
@@ -2366,13 +2366,13 @@ D3D12_UpdateViewport(SDL_Renderer * renderer)
             projection = MatrixIdentity();
             break;
         case DXGI_MODE_ROTATION_ROTATE270:
-            projection = MatrixRotationZ(SDL_static_cast(float, M_PI * 0.5f));
+            projection = MatrixRotationZ(SDL_M_PIf * 0.5f);
             break;
         case DXGI_MODE_ROTATION_ROTATE180:
-            projection = MatrixRotationZ(SDL_static_cast(float, M_PI));
+            projection = MatrixRotationZ(SDL_M_PIf);
             break;
         case DXGI_MODE_ROTATION_ROTATE90:
-            projection = MatrixRotationZ(SDL_static_cast(float, -M_PI * 0.5f));
+            projection = MatrixRotationZ(-SDL_M_PIf * 0.5f);
             break;
         default:
             return SDL_SetError("An unknown DisplayOrientation is being used");
diff --git a/src/render/software/SDL_rotate.c b/src/render/software/SDL_rotate.c
index ade0872656f8..2ea5c0138a6c 100644
--- a/src/render/software/SDL_rotate.c
+++ b/src/render/software/SDL_rotate.c
@@ -122,7 +122,7 @@ SDLgfx_rotozoomSurfaceSizeTrig(int width, int height, double angle, const SDL_FP
     double sinangle;
     double cosangle;
 
-    radangle = angle * (M_PI / 180.0);
+    radangle = angle * (SDL_M_PIl / 180.0);
     sinangle = SDL_sin(radangle);
     cosangle = SDL_cos(radangle);
 
diff --git a/src/sensor/windows/SDL_windowssensor.c b/src/sensor/windows/SDL_windowssensor.c
index b2edffce096f..e4b9f1f1190b 100644
--- a/src/sensor/windows/SDL_windowssensor.c
+++ b/src/sensor/windows/SDL_windowssensor.c
@@ -180,7 +180,7 @@ static HRESULT STDMETHODCALLTYPE ISensorEventsVtbl_OnDataUpdated(ISensorEvents *
                     hrZ = ISensorDataReport_GetSensorValue(pNewData, &SDL_SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, &valueZ);
                     if (SUCCEEDED(hrX) && SUCCEEDED(hrY) && SUCCEEDED(hrZ) &&
                         valueX.vt == VT_R8 && valueY.vt == VT_R8 && valueZ.vt == VT_R8) {
-                        const float DEGREES_TO_RADIANS = (float)(M_PI / 180.0f);
+                        const float DEGREES_TO_RADIANS = (SDL_M_PIf / 180.0f);
                         float values[3];
 
                         values[0] = (float)valueX.dblVal * DEGREES_TO_RADIANS;
diff --git a/src/stdlib/SDL_stdlib.c b/src/stdlib/SDL_stdlib.c
index 2af18b1f05e1..8b87bad7d0b1 100644
--- a/src/stdlib/SDL_stdlib.c
+++ b/src/stdlib/SDL_stdlib.c
@@ -79,12 +79,12 @@ SDL_acos(double val)
 #else
     double result;
     if (val == -1.0) {
-        result = M_PI;
+        result = SDL_M_PIl;
     } else {
         result = SDL_atan(SDL_sqrt(1.0 - val * val) / val);
         if (result < 0.0)
         {
-            result += M_PI;
+            result += SDL_M_PIl;
         }
     }
     return result;
@@ -109,9 +109,9 @@ SDL_asin(double val)
 #else
     double result;
     if (val == -1.0) {
-        result = -(M_PI / 2.0);
+        result = -(SDL_M_PIl / 2.0);
     } else {
-        result = (M_PI / 2.0) - SDL_acos(val);
+        result = (SDL_M_PIl / 2.0) - SDL_acos(val);
     }
     return result;
 #endif