changeset 2014:7abe37467fa5

Replaced unions with calls to SDL_SwapFloat...
author Ryan C. Gordon <icculus@icculus.org>
date Fri, 01 Sep 2006 19:29:49 +0000
parents 0615fa58c0be
children 152dcc2f089f
files src/audio/SDL_audiocvt.c src/audio/SDL_mixer.c
diffstat 2 files changed, 32 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
--- a/src/audio/SDL_audiocvt.c	Fri Sep 01 19:17:05 2006 +0000
+++ b/src/audio/SDL_audiocvt.c	Fri Sep 01 19:29:49 2006 +0000
@@ -187,35 +187,25 @@
 
     case AUDIO_F32:
         {
-            /* !!! FIXME: this convert union is nasty. */
-            union
-            {
-                float f;
-                Uint32 ui32;
-            } f2i;
-            const Uint32 *src = (const Uint32 *) cvt->buf;
-            Uint32 *dst = (Uint32 *) cvt->buf;
+            const float *src = (const float *) cvt->buf;
+            float *dst = (float *) cvt->buf;
             if (SDL_AUDIO_ISBIGENDIAN(format)) {
                 for (i = cvt->len_cvt / 8; i; --i, src += 2) {
                     float src1, src2;
-                    f2i.ui32 = SDL_SwapBE32(src[0]);
-                    src1 = f2i.f;
-                    f2i.ui32 = SDL_SwapBE32(src[1]);
-                    src2 = f2i.f;
+                    src1 = SDL_SwapFloatBE(src[0]);
+                    src2 = SDL_SwapFloatBE(src[1]);
                     const double added = ((double) src1) + ((double) src2);
-                    f2i.f = (float) (added * 0.5);
-                    *(dst++) = SDL_SwapBE32(f2i.ui32);
+                    src1 = (float) (added * 0.5);
+                    *(dst++) = SDL_SwapFloatBE(src1);
                 }
             } else {
                 for (i = cvt->len_cvt / 8; i; --i, src += 2) {
                     float src1, src2;
-                    f2i.ui32 = SDL_SwapLE32(src[0]);
-                    src1 = f2i.f;
-                    f2i.ui32 = SDL_SwapLE32(src[1]);
-                    src2 = f2i.f;
+                    src1 = SDL_SwapFloatLE(src[0]);
+                    src2 = SDL_SwapFloatLE(src[1]);
                     const double added = ((double) src1) + ((double) src2);
-                    f2i.f = (float) (added * 0.5);
-                    *(dst++) = SDL_SwapLE32(f2i.ui32);
+                    src1 = (float) (added * 0.5);
+                    *(dst++) = SDL_SwapFloatLE(src1);
                 }
             }
         }
@@ -577,54 +567,35 @@
 
     case AUDIO_F32:
         {
-            union
-            {
-                float f;
-                Uint32 ui32;
-            } f2i;              /* !!! FIXME: lame. */
             float lf, rf, ce;
-            const Uint32 *src = (const Uint32 *) cvt->buf + cvt->len_cvt;
-            Uint32 *dst = (Uint32 *) cvt->buf + cvt->len_cvt * 3;
+            const float *src = (const float *) cvt->buf + cvt->len_cvt;
+            float *dst = (float *) cvt->buf + cvt->len_cvt * 3;
 
             if (SDL_AUDIO_ISBIGENDIAN(format)) {
                 for (i = cvt->len_cvt / 8; i; --i) {
                     dst -= 6;
                     src -= 2;
-                    f2i.ui32 = SDL_SwapBE32(src[0]);
-                    lf = f2i.f;
-                    f2i.ui32 = SDL_SwapBE32(src[1]);
-                    rf = f2i.f;
+                    lf = SDL_SwapFloatBE(src[0]);
+                    rf = SDL_SwapFloatBE(src[1]);
                     ce = (lf * 0.5f) + (rf * 0.5f);
                     dst[0] = src[0];
                     dst[1] = src[1];
-                    f2i.f = (lf - ce);
-                    dst[2] = SDL_SwapBE32(f2i.ui32);
-                    f2i.f = (rf - ce);
-                    dst[3] = SDL_SwapBE32(f2i.ui32);
-                    f2i.f = ce;
-                    f2i.ui32 = SDL_SwapBE32(f2i.ui32);
-                    dst[4] = f2i.ui32;
-                    dst[5] = f2i.ui32;
+                    dst[2] = SDL_SwapFloatBE(lf - ce);
+                    dst[3] = SDL_SwapFloatBE(rf - ce);
+                    dst[4] = dst[5] = SDL_SwapFloatBE(ce);
                 }
             } else {
                 for (i = cvt->len_cvt / 8; i; --i) {
                     dst -= 6;
                     src -= 2;
-                    f2i.ui32 = SDL_SwapLE32(src[0]);
-                    lf = f2i.f;
-                    f2i.ui32 = SDL_SwapLE32(src[1]);
-                    rf = f2i.f;
+                    lf = SDL_SwapFloatLE(src[0]);
+                    rf = SDL_SwapFloatLE(src[1]);
                     ce = (lf * 0.5f) + (rf * 0.5f);
                     dst[0] = src[0];
                     dst[1] = src[1];
-                    f2i.f = (lf - ce);
-                    dst[2] = SDL_SwapLE32(f2i.ui32);
-                    f2i.f = (rf - ce);
-                    dst[3] = SDL_SwapLE32(f2i.ui32);
-                    f2i.f = ce;
-                    f2i.ui32 = SDL_SwapLE32(f2i.ui32);
-                    dst[4] = f2i.ui32;
-                    dst[5] = f2i.ui32;
+                    dst[2] = SDL_SwapFloatLE(lf - ce);
+                    dst[3] = SDL_SwapFloatLE(rf - ce);
+                    dst[4] = dst[5] = SDL_SwapFloatLE(ce);
                 }
             }
         }
--- a/src/audio/SDL_mixer.c	Fri Sep 01 19:17:05 2006 +0000
+++ b/src/audio/SDL_mixer.c	Fri Sep 01 19:29:49 2006 +0000
@@ -321,32 +321,19 @@
             const double max_audioval = 3.40282347e+38F;
             const double min_audioval = -3.40282347e+38F;
 
-            /* !!! FIXME: this is a little nasty. */
-            union
-            {
-                float f;
-                Uint32 ui32;
-            } cvt;
-
             len /= 4;
             while (len--) {
-                cvt.f = *(src32++);
-                cvt.ui32 = SDL_SwapLE32(cvt.ui32);
-                src1 = ((cvt.f * fvolume) * fmaxvolume);
+                src1 = ((SDL_SwapFloatLE(*src32) * fvolume) * fmaxvolume);
+                src2 = SDL_SwapFloatLE(*dst32);
+                src32++;
 
-                cvt.f = *dst32;
-                cvt.ui32 = SDL_SwapLE32(cvt.ui32);
-                src2 = cvt.f;
-
-                dst_sample = src1 + src2;
+                dst_sample = ((double) src1) + ((double) src2);
                 if (dst_sample > max_audioval) {
                     dst_sample = max_audioval;
                 } else if (dst_sample < min_audioval) {
                     dst_sample = min_audioval;
                 }
-                cvt.f = ((float) dst_sample);
-                cvt.ui32 = SDL_SwapLE32(cvt.ui32);
-                *(dst32++) = cvt.f;
+                *(dst32++) = SDL_SwapFloatLE((float) dst_sample);
             }
         }
         break;
@@ -363,32 +350,19 @@
             const double max_audioval = 3.40282347e+38F;
             const double min_audioval = -3.40282347e+38F;
 
-            /* !!! FIXME: this is a little nasty. */
-            union
-            {
-                float f;
-                Uint32 ui32;
-            } cvt;
-
             len /= 4;
             while (len--) {
-                cvt.f = *(src32++);
-                cvt.ui32 = SDL_SwapBE32(cvt.ui32);
-                src1 = ((cvt.f * fvolume) * fmaxvolume);
+                src1 = ((SDL_SwapFloatBE(*src32) * fvolume) * fmaxvolume);
+                src2 = SDL_SwapFloatBE(*dst32);
+                src32++;
 
-                cvt.f = *dst32;
-                cvt.ui32 = SDL_SwapBE32(cvt.ui32);
-                src2 = cvt.f;
-
-                dst_sample = src1 + src2;
+                dst_sample = ((double) src1) + ((double) src2);
                 if (dst_sample > max_audioval) {
                     dst_sample = max_audioval;
                 } else if (dst_sample < min_audioval) {
                     dst_sample = min_audioval;
                 }
-                cvt.f = ((float) dst_sample);
-                cvt.ui32 = SDL_SwapBE32(cvt.ui32);
-                *(dst32++) = cvt.f;
+                *(dst32++) = SDL_SwapFloatBE((float) dst_sample);
             }
         }
         break;