SDL_mixer: do not use volatiles in (possible) long loops of _Eff_position_*

From 66c166b427b0b2daf0fdfcc823525a2a6face271 Mon Sep 17 00:00:00 2001
From: pionere <[EMAIL REDACTED]>
Date: Thu, 16 Dec 2021 15:03:53 +0100
Subject: [PATCH] do not use volatiles in (possible) long loops of
 _Eff_position_*

---
 src/effect_position.c | 108 +++++++++++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 43 deletions(-)

diff --git a/src/effect_position.c b/src/effect_position.c
index e28a7aeb..0819ded6 100644
--- a/src/effect_position.c
+++ b/src/effect_position.c
@@ -111,8 +111,10 @@ static void SDLCALL _Eff_PositionDone(int channel, void *udata)
 
 static void SDLCALL _Eff_position_u8(int chan, void *stream, int len, void *udata)
 {
-    volatile position_args *args = (volatile position_args *) udata;
     Uint8 *ptr = (Uint8 *) stream;
+    const float dist_f = ((position_args *)udata)->distance_f;
+    const float left_f = ((position_args *)udata)->left_f;
+    const float right_f = ((position_args *)udata)->right_f;
     int i;
 
     (void)chan;
@@ -123,28 +125,28 @@ static void SDLCALL _Eff_position_u8(int chan, void *stream, int len, void *udat
      *  1.0, and are therefore throwaways.
      */
     if (len % (int)sizeof(Uint16) != 0) {
-        *ptr = (Uint8) (((float) *ptr) * args->distance_f);
+        *ptr = (Uint8) (((float) *ptr) * dist_f);
         ptr++;
         len--;
     }
 
-    if (args->room_angle == 180)
+    if (((position_args *)udata)->room_angle == 180)
     for (i = 0; i < len; i += sizeof (Uint8) * 2) {
         /* must adjust the sample so that 0 is the center */
         *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))
-            * args->right_f) * args->distance_f) + 128);
+            * right_f) * dist_f) + 128);
         ptr++;
         *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))
-            * args->left_f) * args->distance_f) + 128);
+            * left_f) * dist_f) + 128);
         ptr++;
     }
     else for (i = 0; i < len; i += sizeof (Uint8) * 2) {
         /* must adjust the sample so that 0 is the center */
         *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))
-            * args->left_f) * args->distance_f) + 128);
+            * left_f) * dist_f) + 128);
         ptr++;
         *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))
-            * args->right_f) * args->distance_f) + 128);
+            * right_f) * dist_f) + 128);
         ptr++;
     }
 }
@@ -412,8 +414,10 @@ static void SDLCALL _Eff_position_table_u8(int chan, void *stream, int len, void
 
 static void SDLCALL _Eff_position_s8(int chan, void *stream, int len, void *udata)
 {
-    volatile position_args *args = (volatile position_args *) udata;
     Sint8 *ptr = (Sint8 *) stream;
+    const float dist_f = ((position_args *)udata)->distance_f;
+    const float left_f = ((position_args *)udata)->left_f;
+    const float right_f = ((position_args *)udata)->right_f;
     int i;
 
     (void)chan;
@@ -424,23 +428,23 @@ static void SDLCALL _Eff_position_s8(int chan, void *stream, int len, void *udat
      *  1.0, and are therefore throwaways.
      */
     if (len % (int)sizeof(Sint16) != 0) {
-        *ptr = (Sint8) (((float) *ptr) * args->distance_f);
+        *ptr = (Sint8) (((float) *ptr) * dist_f);
         ptr++;
         len--;
     }
 
-    if (args->room_angle == 180)
+    if (((position_args *)udata)->room_angle == 180)
     for (i = 0; i < len; i += sizeof (Sint8) * 2) {
-        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f);
+        *ptr = (Sint8)((((float) *ptr) * right_f) * dist_f);
         ptr++;
-        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f);
+        *ptr = (Sint8)((((float) *ptr) * left_f) * dist_f);
         ptr++;
     }
     else
     for (i = 0; i < len; i += sizeof (Sint8) * 2) {
-        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f);
+        *ptr = (Sint8)((((float) *ptr) * left_f) * dist_f);
         ptr++;
-        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f);
+        *ptr = (Sint8)((((float) *ptr) * right_f) * dist_f);
         ptr++;
     }
 }
@@ -612,8 +616,11 @@ static void SDLCALL _Eff_position_table_s8(int chan, void *stream, int len, void
 
 static void SDLCALL _Eff_position_u16lsb(int chan, void *stream, int len, void *udata)
 {
-    volatile position_args *args = (volatile position_args *) udata;
     Uint16 *ptr = (Uint16 *) stream;
+    const SDL_bool opp = ((position_args *)udata)->room_angle == 180 ? SDL_TRUE : SDL_FALSE;
+    const float dist_f = ((position_args *)udata)->distance_f;
+    const float left_f = ((position_args *)udata)->left_f;
+    const float right_f = ((position_args *)udata)->right_f;
     int i;
 
     (void)chan;
@@ -622,12 +629,12 @@ static void SDLCALL _Eff_position_u16lsb(int chan, void *stream, int len, void *
         Sint16 sampl = (Sint16) (SDL_SwapLE16(*(ptr+0)) - 32768);
         Sint16 sampr = (Sint16) (SDL_SwapLE16(*(ptr+1)) - 32768);
 
-        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
-                                    * args->distance_f) + 32768);
-        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
-                                    * args->distance_f) + 32768);
+        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * left_f)
+                                    * dist_f) + 32768);
+        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * right_f)
+                                    * dist_f) + 32768);
 
-        if (args->room_angle == 180) {
+        if (opp) {
             *(ptr++) = (Uint16) SDL_SwapLE16(swapr);
             *(ptr++) = (Uint16) SDL_SwapLE16(swapl);
         }
@@ -759,8 +766,11 @@ static void SDLCALL _Eff_position_u16lsb_c6(int chan, void *stream, int len, voi
 static void SDLCALL _Eff_position_s16lsb(int chan, void *stream, int len, void *udata)
 {
     /* 16 signed bits (lsb) * 2 channels. */
-    volatile position_args *args = (volatile position_args *) udata;
     Sint16 *ptr = (Sint16 *) stream;
+    const SDL_bool opp = ((position_args *)udata)->room_angle == 180 ? SDL_TRUE : SDL_FALSE;
+    const float dist_f = ((position_args *)udata)->distance_f;
+    const float left_f = ((position_args *)udata)->left_f;
+    const float right_f = ((position_args *)udata)->right_f;
     int i;
 
     (void)chan;
@@ -774,10 +784,10 @@ static void SDLCALL _Eff_position_s16lsb(int chan, void *stream, int len, void *
 
     for (i = 0; i < len; i += sizeof (Sint16) * 2) {
         Sint16 swapl = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+0))) *
-                                    args->left_f) * args->distance_f);
+                                    left_f) * dist_f);
         Sint16 swapr = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+1))) *
-                                    args->right_f) * args->distance_f);
-        if (args->room_angle == 180) {
+                                    right_f) * dist_f);
+        if (opp) {
             *(ptr++) = (Sint16) SDL_SwapLE16(swapr);
             *(ptr++) = (Sint16) SDL_SwapLE16(swapl);
         }
@@ -896,8 +906,11 @@ static void SDLCALL _Eff_position_s16lsb_c6(int chan, void *stream, int len, voi
 static void SDLCALL _Eff_position_u16msb(int chan, void *stream, int len, void *udata)
 {
     /* 16 signed bits (lsb) * 2 channels. */
-    volatile position_args *args = (volatile position_args *) udata;
     Uint16 *ptr = (Uint16 *) stream;
+    const SDL_bool opp = ((position_args *)udata)->room_angle == 180 ? SDL_TRUE : SDL_FALSE;
+    const float dist_f = ((position_args *)udata)->distance_f;
+    const float left_f = ((position_args *)udata)->left_f;
+    const float right_f = ((position_args *)udata)->right_f;
     int i;
 
     (void)chan;
@@ -906,12 +919,12 @@ static void SDLCALL _Eff_position_u16msb(int chan, void *stream, int len, void *
         Sint16 sampl = (Sint16) (SDL_SwapBE16(*(ptr+0)) - 32768);
         Sint16 sampr = (Sint16) (SDL_SwapBE16(*(ptr+1)) - 32768);
 
-        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
-                                    * args->distance_f) + 32768);
-        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
-                                    * args->distance_f) + 32768);
+        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * left_f)
+                                    * dist_f) + 32768);
+        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * right_f)
+                                    * dist_f) + 32768);
 
-        if (args->room_angle == 180) {
+        if (opp) {
             *(ptr++) = (Uint16) SDL_SwapBE16(swapr);
             *(ptr++) = (Uint16) SDL_SwapBE16(swapl);
         }
@@ -1045,17 +1058,19 @@ static void SDLCALL _Eff_position_u16msb_c6(int chan, void *stream, int len, voi
 static void SDLCALL _Eff_position_s16msb(int chan, void *stream, int len, void *udata)
 {
     /* 16 signed bits (lsb) * 2 channels. */
-    volatile position_args *args = (volatile position_args *) udata;
     Sint16 *ptr = (Sint16 *) stream;
+    const float dist_f = ((position_args *)udata)->distance_f;
+    const float left_f = ((position_args *)udata)->left_f;
+    const float right_f = ((position_args *)udata)->right_f;
     int i;
 
     (void)chan;
 
     for (i = 0; i < len; i += sizeof (Sint16) * 2) {
         Sint16 swapl = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+0))) *
-                                    args->left_f) * args->distance_f);
+                                    left_f) * dist_f);
         Sint16 swapr = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+1))) *
-                                    args->right_f) * args->distance_f);
+                                    right_f) * dist_f);
         *(ptr++) = (Sint16) SDL_SwapBE16(swapl);
         *(ptr++) = (Sint16) SDL_SwapBE16(swapr);
     }
@@ -1171,8 +1186,11 @@ static void SDLCALL _Eff_position_s16msb_c6(int chan, void *stream, int len, voi
 static void SDLCALL _Eff_position_s32lsb(int chan, void *stream, int len, void *udata)
 {
     /* 32 signed bits (lsb) * 2 channels. */
-    volatile position_args *args = (volatile position_args *) udata;
     Sint32 *ptr = (Sint32 *) stream;
+    const SDL_bool opp = ((position_args *)udata)->room_angle == 180 ? SDL_TRUE : SDL_FALSE;
+    const float dist_f = ((position_args *)udata)->distance_f;
+    const float left_f = ((position_args *)udata)->left_f;
+    const float right_f = ((position_args *)udata)->right_f;
     int i;
 
     (void)chan;
@@ -1186,10 +1204,10 @@ static void SDLCALL _Eff_position_s32lsb(int chan, void *stream, int len, void *
 
     for (i = 0; i < len; i += sizeof (Sint32) * 2) {
         Sint32 swapl = (Sint32) ((((float) (Sint32) SDL_SwapLE32(*(ptr+0))) *
-                                    args->left_f) * args->distance_f);
+                                    left_f) * dist_f);
         Sint32 swapr = (Sint32) ((((float) (Sint32) SDL_SwapLE32(*(ptr+1))) *
-                                    args->right_f) * args->distance_f);
-        if (args->room_angle == 180) {
+                                    right_f) * dist_f);
+        if (opp) {
             *(ptr++) = (Sint32) SDL_SwapLE32(swapr);
             *(ptr++) = (Sint32) SDL_SwapLE32(swapl);
         }
@@ -1309,17 +1327,19 @@ static void SDLCALL _Eff_position_s32lsb_c6(int chan, void *stream, int len, voi
 static void SDLCALL _Eff_position_s32msb(int chan, void *stream, int len, void *udata)
 {
     /* 32 signed bits (lsb) * 2 channels. */
-    volatile position_args *args = (volatile position_args *) udata;
     Sint32 *ptr = (Sint32 *) stream;
+    const float dist_f = ((position_args *)udata)->distance_f;
+    const float left_f = ((position_args *)udata)->left_f;
+    const float right_f = ((position_args *)udata)->right_f;
     int i;
 
     (void)chan;
 
     for (i = 0; i < len; i += sizeof (Sint32) * 2) {
         Sint32 swapl = (Sint32) ((((float) (Sint32) SDL_SwapBE32(*(ptr+0))) *
-                                    args->left_f) * args->distance_f);
+                                    left_f) * dist_f);
         Sint32 swapr = (Sint32) ((((float) (Sint32) SDL_SwapBE32(*(ptr+1))) *
-                                    args->right_f) * args->distance_f);
+                                    right_f) * dist_f);
         *(ptr++) = (Sint32) SDL_SwapBE32(swapl);
         *(ptr++) = (Sint32) SDL_SwapBE32(swapr);
     }
@@ -1435,15 +1455,17 @@ static void SDLCALL _Eff_position_s32msb_c6(int chan, void *stream, int len, voi
 static void SDLCALL _Eff_position_f32sys(int chan, void *stream, int len, void *udata)
 {
     /* float * 2 channels. */
-    volatile position_args *args = (volatile position_args *) udata;
     float *ptr = (float *) stream;
+    const float dist_f = ((position_args *)udata)->distance_f;
+    const float left_f = ((position_args *)udata)->left_f;
+    const float right_f = ((position_args *)udata)->right_f;
     int i;
 
     (void)chan;
 
     for (i = 0; i < len; i += sizeof (float) * 2) {
-        float swapl = ((*(ptr+0) * args->left_f) * args->distance_f);
-        float swapr = ((*(ptr+1) * args->right_f) * args->distance_f);
+        float swapl = ((*(ptr+0) * left_f) * dist_f);
+        float swapr = ((*(ptr+1) * right_f) * dist_f);
         *(ptr++) = swapl;
         *(ptr++) = swapr;
     }