Replaced unions with calls to SDL_SwapFloat...
authorRyan C. Gordon <icculus@icculus.org>
Fri, 01 Sep 2006 19:29:49 +0000
changeset 20147abe37467fa5
parent 2013 0615fa58c0be
child 2015 152dcc2f089f
Replaced unions with calls to SDL_SwapFloat...
src/audio/SDL_audiocvt.c
src/audio/SDL_mixer.c
     1.1 --- a/src/audio/SDL_audiocvt.c	Fri Sep 01 19:17:05 2006 +0000
     1.2 +++ b/src/audio/SDL_audiocvt.c	Fri Sep 01 19:29:49 2006 +0000
     1.3 @@ -187,35 +187,25 @@
     1.4  
     1.5      case AUDIO_F32:
     1.6          {
     1.7 -            /* !!! FIXME: this convert union is nasty. */
     1.8 -            union
     1.9 -            {
    1.10 -                float f;
    1.11 -                Uint32 ui32;
    1.12 -            } f2i;
    1.13 -            const Uint32 *src = (const Uint32 *) cvt->buf;
    1.14 -            Uint32 *dst = (Uint32 *) cvt->buf;
    1.15 +            const float *src = (const float *) cvt->buf;
    1.16 +            float *dst = (float *) cvt->buf;
    1.17              if (SDL_AUDIO_ISBIGENDIAN(format)) {
    1.18                  for (i = cvt->len_cvt / 8; i; --i, src += 2) {
    1.19                      float src1, src2;
    1.20 -                    f2i.ui32 = SDL_SwapBE32(src[0]);
    1.21 -                    src1 = f2i.f;
    1.22 -                    f2i.ui32 = SDL_SwapBE32(src[1]);
    1.23 -                    src2 = f2i.f;
    1.24 +                    src1 = SDL_SwapFloatBE(src[0]);
    1.25 +                    src2 = SDL_SwapFloatBE(src[1]);
    1.26                      const double added = ((double) src1) + ((double) src2);
    1.27 -                    f2i.f = (float) (added * 0.5);
    1.28 -                    *(dst++) = SDL_SwapBE32(f2i.ui32);
    1.29 +                    src1 = (float) (added * 0.5);
    1.30 +                    *(dst++) = SDL_SwapFloatBE(src1);
    1.31                  }
    1.32              } else {
    1.33                  for (i = cvt->len_cvt / 8; i; --i, src += 2) {
    1.34                      float src1, src2;
    1.35 -                    f2i.ui32 = SDL_SwapLE32(src[0]);
    1.36 -                    src1 = f2i.f;
    1.37 -                    f2i.ui32 = SDL_SwapLE32(src[1]);
    1.38 -                    src2 = f2i.f;
    1.39 +                    src1 = SDL_SwapFloatLE(src[0]);
    1.40 +                    src2 = SDL_SwapFloatLE(src[1]);
    1.41                      const double added = ((double) src1) + ((double) src2);
    1.42 -                    f2i.f = (float) (added * 0.5);
    1.43 -                    *(dst++) = SDL_SwapLE32(f2i.ui32);
    1.44 +                    src1 = (float) (added * 0.5);
    1.45 +                    *(dst++) = SDL_SwapFloatLE(src1);
    1.46                  }
    1.47              }
    1.48          }
    1.49 @@ -577,54 +567,35 @@
    1.50  
    1.51      case AUDIO_F32:
    1.52          {
    1.53 -            union
    1.54 -            {
    1.55 -                float f;
    1.56 -                Uint32 ui32;
    1.57 -            } f2i;              /* !!! FIXME: lame. */
    1.58              float lf, rf, ce;
    1.59 -            const Uint32 *src = (const Uint32 *) cvt->buf + cvt->len_cvt;
    1.60 -            Uint32 *dst = (Uint32 *) cvt->buf + cvt->len_cvt * 3;
    1.61 +            const float *src = (const float *) cvt->buf + cvt->len_cvt;
    1.62 +            float *dst = (float *) cvt->buf + cvt->len_cvt * 3;
    1.63  
    1.64              if (SDL_AUDIO_ISBIGENDIAN(format)) {
    1.65                  for (i = cvt->len_cvt / 8; i; --i) {
    1.66                      dst -= 6;
    1.67                      src -= 2;
    1.68 -                    f2i.ui32 = SDL_SwapBE32(src[0]);
    1.69 -                    lf = f2i.f;
    1.70 -                    f2i.ui32 = SDL_SwapBE32(src[1]);
    1.71 -                    rf = f2i.f;
    1.72 +                    lf = SDL_SwapFloatBE(src[0]);
    1.73 +                    rf = SDL_SwapFloatBE(src[1]);
    1.74                      ce = (lf * 0.5f) + (rf * 0.5f);
    1.75                      dst[0] = src[0];
    1.76                      dst[1] = src[1];
    1.77 -                    f2i.f = (lf - ce);
    1.78 -                    dst[2] = SDL_SwapBE32(f2i.ui32);
    1.79 -                    f2i.f = (rf - ce);
    1.80 -                    dst[3] = SDL_SwapBE32(f2i.ui32);
    1.81 -                    f2i.f = ce;
    1.82 -                    f2i.ui32 = SDL_SwapBE32(f2i.ui32);
    1.83 -                    dst[4] = f2i.ui32;
    1.84 -                    dst[5] = f2i.ui32;
    1.85 +                    dst[2] = SDL_SwapFloatBE(lf - ce);
    1.86 +                    dst[3] = SDL_SwapFloatBE(rf - ce);
    1.87 +                    dst[4] = dst[5] = SDL_SwapFloatBE(ce);
    1.88                  }
    1.89              } else {
    1.90                  for (i = cvt->len_cvt / 8; i; --i) {
    1.91                      dst -= 6;
    1.92                      src -= 2;
    1.93 -                    f2i.ui32 = SDL_SwapLE32(src[0]);
    1.94 -                    lf = f2i.f;
    1.95 -                    f2i.ui32 = SDL_SwapLE32(src[1]);
    1.96 -                    rf = f2i.f;
    1.97 +                    lf = SDL_SwapFloatLE(src[0]);
    1.98 +                    rf = SDL_SwapFloatLE(src[1]);
    1.99                      ce = (lf * 0.5f) + (rf * 0.5f);
   1.100                      dst[0] = src[0];
   1.101                      dst[1] = src[1];
   1.102 -                    f2i.f = (lf - ce);
   1.103 -                    dst[2] = SDL_SwapLE32(f2i.ui32);
   1.104 -                    f2i.f = (rf - ce);
   1.105 -                    dst[3] = SDL_SwapLE32(f2i.ui32);
   1.106 -                    f2i.f = ce;
   1.107 -                    f2i.ui32 = SDL_SwapLE32(f2i.ui32);
   1.108 -                    dst[4] = f2i.ui32;
   1.109 -                    dst[5] = f2i.ui32;
   1.110 +                    dst[2] = SDL_SwapFloatLE(lf - ce);
   1.111 +                    dst[3] = SDL_SwapFloatLE(rf - ce);
   1.112 +                    dst[4] = dst[5] = SDL_SwapFloatLE(ce);
   1.113                  }
   1.114              }
   1.115          }
     2.1 --- a/src/audio/SDL_mixer.c	Fri Sep 01 19:17:05 2006 +0000
     2.2 +++ b/src/audio/SDL_mixer.c	Fri Sep 01 19:29:49 2006 +0000
     2.3 @@ -321,32 +321,19 @@
     2.4              const double max_audioval = 3.40282347e+38F;
     2.5              const double min_audioval = -3.40282347e+38F;
     2.6  
     2.7 -            /* !!! FIXME: this is a little nasty. */
     2.8 -            union
     2.9 -            {
    2.10 -                float f;
    2.11 -                Uint32 ui32;
    2.12 -            } cvt;
    2.13 -
    2.14              len /= 4;
    2.15              while (len--) {
    2.16 -                cvt.f = *(src32++);
    2.17 -                cvt.ui32 = SDL_SwapLE32(cvt.ui32);
    2.18 -                src1 = ((cvt.f * fvolume) * fmaxvolume);
    2.19 +                src1 = ((SDL_SwapFloatLE(*src32) * fvolume) * fmaxvolume);
    2.20 +                src2 = SDL_SwapFloatLE(*dst32);
    2.21 +                src32++;
    2.22  
    2.23 -                cvt.f = *dst32;
    2.24 -                cvt.ui32 = SDL_SwapLE32(cvt.ui32);
    2.25 -                src2 = cvt.f;
    2.26 -
    2.27 -                dst_sample = src1 + src2;
    2.28 +                dst_sample = ((double) src1) + ((double) src2);
    2.29                  if (dst_sample > max_audioval) {
    2.30                      dst_sample = max_audioval;
    2.31                  } else if (dst_sample < min_audioval) {
    2.32                      dst_sample = min_audioval;
    2.33                  }
    2.34 -                cvt.f = ((float) dst_sample);
    2.35 -                cvt.ui32 = SDL_SwapLE32(cvt.ui32);
    2.36 -                *(dst32++) = cvt.f;
    2.37 +                *(dst32++) = SDL_SwapFloatLE((float) dst_sample);
    2.38              }
    2.39          }
    2.40          break;
    2.41 @@ -363,32 +350,19 @@
    2.42              const double max_audioval = 3.40282347e+38F;
    2.43              const double min_audioval = -3.40282347e+38F;
    2.44  
    2.45 -            /* !!! FIXME: this is a little nasty. */
    2.46 -            union
    2.47 -            {
    2.48 -                float f;
    2.49 -                Uint32 ui32;
    2.50 -            } cvt;
    2.51 -
    2.52              len /= 4;
    2.53              while (len--) {
    2.54 -                cvt.f = *(src32++);
    2.55 -                cvt.ui32 = SDL_SwapBE32(cvt.ui32);
    2.56 -                src1 = ((cvt.f * fvolume) * fmaxvolume);
    2.57 +                src1 = ((SDL_SwapFloatBE(*src32) * fvolume) * fmaxvolume);
    2.58 +                src2 = SDL_SwapFloatBE(*dst32);
    2.59 +                src32++;
    2.60  
    2.61 -                cvt.f = *dst32;
    2.62 -                cvt.ui32 = SDL_SwapBE32(cvt.ui32);
    2.63 -                src2 = cvt.f;
    2.64 -
    2.65 -                dst_sample = src1 + src2;
    2.66 +                dst_sample = ((double) src1) + ((double) src2);
    2.67                  if (dst_sample > max_audioval) {
    2.68                      dst_sample = max_audioval;
    2.69                  } else if (dst_sample < min_audioval) {
    2.70                      dst_sample = min_audioval;
    2.71                  }
    2.72 -                cvt.f = ((float) dst_sample);
    2.73 -                cvt.ui32 = SDL_SwapBE32(cvt.ui32);
    2.74 -                *(dst32++) = cvt.f;
    2.75 +                *(dst32++) = SDL_SwapFloatBE((float) dst_sample);
    2.76              }
    2.77          }
    2.78          break;