src/audio/SDL_audiotypecvt.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 29 Aug 2017 00:02:04 -0400
changeset 11403 4cdc242e4102
parent 10838 57ac8b68d7c8
child 11406 f40c2dedaded
permissions -rw-r--r--
audio: Converting audio samples from int to float was using wrong equation.

Fixes Bugzilla #3775.
icculus@1982
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5535
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5535
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@1982
    20
*/
icculus@1982
    21
icculus@8093
    22
#include "../SDL_internal.h"
icculus@1982
    23
#include "SDL_audio.h"
icculus@1982
    24
#include "SDL_audio_c.h"
icculus@10815
    25
#include "SDL_cpuinfo.h"
icculus@10575
    26
#include "SDL_assert.h"
icculus@1982
    27
icculus@10815
    28
/* !!! FIXME: write NEON code. */
icculus@10815
    29
#define HAVE_NEON_INTRINSICS 0
icculus@10815
    30
icculus@10835
    31
#ifdef __SSE2__
icculus@10835
    32
#define HAVE_SSE2_INTRINSICS 1
icculus@10815
    33
#endif
icculus@10815
    34
icculus@10815
    35
#if defined(__x86_64__) && HAVE_SSE2_INTRINSICS
icculus@10815
    36
#define NEED_SCALAR_CONVERTER_FALLBACKS 0  /* x86_64 guarantees SSE2. */
icculus@10815
    37
#elif __MACOSX__ && HAVE_SSE2_INTRINSICS
icculus@10815
    38
#define NEED_SCALAR_CONVERTER_FALLBACKS 0  /* Mac OS X/Intel guarantees SSE2. */
icculus@10815
    39
#elif defined(__ARM_ARCH) && (__ARM_ARCH >= 8) && HAVE_NEON_INTRINSICS
icculus@10815
    40
#define NEED_SCALAR_CONVERTER_FALLBACKS 0  /* ARMv8+ promise NEON. */
icculus@10815
    41
#elif defined(__APPLE__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7) && HAVE_NEON_INTRINSICS
icculus@10815
    42
#define NEED_SCALAR_CONVERTER_FALLBACKS 0  /* All Apple ARMv7 chips promise NEON support. */
icculus@10815
    43
#endif
icculus@10815
    44
icculus@10815
    45
/* Set to zero if platform is guaranteed to use a SIMD codepath here. */
icculus@10815
    46
#ifndef NEED_SCALAR_CONVERTER_FALLBACKS
icculus@10815
    47
#define NEED_SCALAR_CONVERTER_FALLBACKS 1
icculus@10815
    48
#endif
icculus@10815
    49
icculus@10815
    50
/* Function pointers set to a CPU-specific implementation. */
icculus@10815
    51
SDL_AudioFilter SDL_Convert_S8_to_F32 = NULL;
icculus@10815
    52
SDL_AudioFilter SDL_Convert_U8_to_F32 = NULL;
icculus@10815
    53
SDL_AudioFilter SDL_Convert_S16_to_F32 = NULL;
icculus@10815
    54
SDL_AudioFilter SDL_Convert_U16_to_F32 = NULL;
icculus@10815
    55
SDL_AudioFilter SDL_Convert_S32_to_F32 = NULL;
icculus@10815
    56
SDL_AudioFilter SDL_Convert_F32_to_S8 = NULL;
icculus@10815
    57
SDL_AudioFilter SDL_Convert_F32_to_U8 = NULL;
icculus@10815
    58
SDL_AudioFilter SDL_Convert_F32_to_S16 = NULL;
icculus@10815
    59
SDL_AudioFilter SDL_Convert_F32_to_U16 = NULL;
icculus@10815
    60
SDL_AudioFilter SDL_Convert_F32_to_S32 = NULL;
icculus@10815
    61
icculus@10815
    62
icculus@11403
    63
#define DIVBY128 0.0078125f
icculus@11403
    64
#define DIVBY32768 0.000030517578125f
icculus@11403
    65
#define DIVBY2147483648 0.00000000046566128730773926
icculus@1982
    66
icculus@10815
    67
icculus@10815
    68
#if NEED_SCALAR_CONVERTER_FALLBACKS
icculus@10815
    69
static void SDLCALL
icculus@10815
    70
SDL_Convert_S8_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10575
    71
{
icculus@10814
    72
    const Sint8 *src = ((const Sint8 *) (cvt->buf + cvt->len_cvt)) - 1;
icculus@10575
    73
    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
icculus@10575
    74
    int i;
icculus@3021
    75
icculus@10575
    76
    LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32");
icculus@1982
    77
icculus@10814
    78
    for (i = cvt->len_cvt; i; --i, --src, --dst) {
icculus@11403
    79
        *dst = ((float) *src) * DIVBY128;
icculus@1982
    80
    }
icculus@1982
    81
icculus@10575
    82
    cvt->len_cvt *= 4;
icculus@1982
    83
    if (cvt->filters[++cvt->filter_index]) {
icculus@10575
    84
        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
icculus@1982
    85
    }
icculus@1982
    86
}
icculus@1982
    87
icculus@10815
    88
static void SDLCALL
icculus@10815
    89
SDL_Convert_U8_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@1982
    90
{
icculus@10575
    91
    const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
icculus@10575
    92
    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
icculus@1982
    93
    int i;
icculus@1982
    94
icculus@10575
    95
    LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32");
icculus@1982
    96
icculus@10814
    97
    for (i = cvt->len_cvt; i; --i, --src, --dst) {
icculus@11403
    98
        *dst = (((float) *src) * DIVBY128) - 1.0f;
icculus@10575
    99
    }
icculus@10575
   100
icculus@10575
   101
    cvt->len_cvt *= 4;
icculus@10575
   102
    if (cvt->filters[++cvt->filter_index]) {
icculus@10575
   103
        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
icculus@10575
   104
    }
icculus@10575
   105
}
icculus@10575
   106
icculus@10815
   107
static void SDLCALL
icculus@10815
   108
SDL_Convert_S16_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10575
   109
{
icculus@10575
   110
    const Sint16 *src = ((const Sint16 *) (cvt->buf + cvt->len_cvt)) - 1;
icculus@10575
   111
    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
icculus@10575
   112
    int i;
icculus@10575
   113
icculus@10575
   114
    LOG_DEBUG_CONVERT("AUDIO_S16", "AUDIO_F32");
icculus@10575
   115
icculus@10575
   116
    for (i = cvt->len_cvt / sizeof (Sint16); i; --i, --src, --dst) {
icculus@11403
   117
        *dst = ((float) *src) * DIVBY32768;
icculus@1982
   118
    }
icculus@1982
   119
icculus@1982
   120
    cvt->len_cvt *= 2;
icculus@1982
   121
    if (cvt->filters[++cvt->filter_index]) {
icculus@10575
   122
        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
icculus@1982
   123
    }
icculus@1982
   124
}
icculus@1982
   125
icculus@10815
   126
static void SDLCALL
icculus@10815
   127
SDL_Convert_U16_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@1982
   128
{
icculus@10575
   129
    const Uint16 *src = ((const Uint16 *) (cvt->buf + cvt->len_cvt)) - 1;
icculus@10575
   130
    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
icculus@1982
   131
    int i;
icculus@1982
   132
icculus@10575
   133
    LOG_DEBUG_CONVERT("AUDIO_U16", "AUDIO_F32");
icculus@1982
   134
icculus@10575
   135
    for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
icculus@11403
   136
        *dst = (((float) *src) * DIVBY32768) - 1.0f;
icculus@1982
   137
    }
icculus@1982
   138
icculus@1982
   139
    cvt->len_cvt *= 2;
icculus@1982
   140
    if (cvt->filters[++cvt->filter_index]) {
icculus@10575
   141
        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
icculus@1982
   142
    }
icculus@1982
   143
}
icculus@1982
   144
icculus@10815
   145
static void SDLCALL
icculus@10815
   146
SDL_Convert_S32_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@1982
   147
{
icculus@10814
   148
    const Sint32 *src = (const Sint32 *) cvt->buf;
icculus@10575
   149
    float *dst = (float *) cvt->buf;
icculus@1982
   150
    int i;
icculus@1982
   151
icculus@10575
   152
    LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32");
icculus@1982
   153
icculus@10575
   154
    for (i = cvt->len_cvt / sizeof (Sint32); i; --i, ++src, ++dst) {
icculus@11403
   155
        *dst = (float) (((double) *src) * DIVBY2147483648);
icculus@1982
   156
    }
icculus@1982
   157
icculus@1982
   158
    if (cvt->filters[++cvt->filter_index]) {
icculus@10575
   159
        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
icculus@1982
   160
    }
icculus@1982
   161
}
icculus@1982
   162
icculus@10815
   163
static void SDLCALL
icculus@10815
   164
SDL_Convert_F32_to_S8_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@1982
   165
{
icculus@10575
   166
    const float *src = (const float *) cvt->buf;
icculus@10575
   167
    Sint8 *dst = (Sint8 *) cvt->buf;
icculus@1982
   168
    int i;
icculus@1982
   169
icculus@10575
   170
    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S8");
icculus@1982
   171
icculus@10575
   172
    for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
icculus@10575
   173
        *dst = (Sint8) (*src * 127.0f);
icculus@1982
   174
    }
icculus@1982
   175
icculus@10575
   176
    cvt->len_cvt /= 4;
icculus@1982
   177
    if (cvt->filters[++cvt->filter_index]) {
icculus@10575
   178
        cvt->filters[cvt->filter_index](cvt, AUDIO_S8);
icculus@1982
   179
    }
icculus@1982
   180
}
icculus@1982
   181
icculus@10815
   182
static void SDLCALL
icculus@10815
   183
SDL_Convert_F32_to_U8_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@1982
   184
{
icculus@10575
   185
    const float *src = (const float *) cvt->buf;
icculus@10575
   186
    Uint8 *dst = (Uint8 *) cvt->buf;
icculus@1982
   187
    int i;
icculus@1982
   188
icculus@10575
   189
    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U8");
icculus@1982
   190
icculus@10575
   191
    for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
icculus@10575
   192
        *dst = (Uint8) ((*src + 1.0f) * 127.0f);
icculus@1982
   193
    }
icculus@1982
   194
icculus@10575
   195
    cvt->len_cvt /= 4;
icculus@1982
   196
    if (cvt->filters[++cvt->filter_index]) {
icculus@10575
   197
        cvt->filters[cvt->filter_index](cvt, AUDIO_U8);
icculus@1982
   198
    }
icculus@1982
   199
}
icculus@1982
   200
icculus@10815
   201
static void SDLCALL
icculus@10815
   202
SDL_Convert_F32_to_S16_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@1982
   203
{
icculus@10575
   204
    const float *src = (const float *) cvt->buf;
icculus@10575
   205
    Sint16 *dst = (Sint16 *) cvt->buf;
icculus@1982
   206
    int i;
icculus@1982
   207
icculus@10575
   208
    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S16");
icculus@1982
   209
icculus@10575
   210
    for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
icculus@10575
   211
        *dst = (Sint16) (*src * 32767.0f);
icculus@1982
   212
    }
icculus@1982
   213
icculus@1982
   214
    cvt->len_cvt /= 2;
icculus@1982
   215
    if (cvt->filters[++cvt->filter_index]) {
icculus@10575
   216
        cvt->filters[cvt->filter_index](cvt, AUDIO_S16SYS);
icculus@1982
   217
    }
icculus@1982
   218
}
icculus@1982
   219
icculus@10815
   220
static void SDLCALL
icculus@10815
   221
SDL_Convert_F32_to_U16_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@1982
   222
{
icculus@10575
   223
    const float *src = (const float *) cvt->buf;
icculus@10575
   224
    Uint16 *dst = (Uint16 *) cvt->buf;
icculus@1982
   225
    int i;
icculus@1982
   226
icculus@10575
   227
    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U16");
icculus@1982
   228
icculus@10575
   229
    for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
icculus@10575
   230
        *dst = (Uint16) ((*src + 1.0f) * 32767.0f);
icculus@1982
   231
    }
icculus@1982
   232
icculus@1982
   233
    cvt->len_cvt /= 2;
icculus@1982
   234
    if (cvt->filters[++cvt->filter_index]) {
icculus@10575
   235
        cvt->filters[cvt->filter_index](cvt, AUDIO_U16SYS);
icculus@1982
   236
    }
icculus@1982
   237
}
icculus@1982
   238
icculus@10815
   239
static void SDLCALL
icculus@10815
   240
SDL_Convert_F32_to_S32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@1982
   241
{
icculus@10575
   242
    const float *src = (const float *) cvt->buf;
icculus@10575
   243
    Sint32 *dst = (Sint32 *) cvt->buf;
icculus@1982
   244
    int i;
icculus@1982
   245
icculus@10575
   246
    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32");
icculus@1982
   247
icculus@10575
   248
    for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
icculus@10814
   249
        *dst = (Sint32) (((double) *src) * 2147483647.0);
icculus@1982
   250
    }
icculus@1982
   251
icculus@1982
   252
    if (cvt->filters[++cvt->filter_index]) {
icculus@10575
   253
        cvt->filters[cvt->filter_index](cvt, AUDIO_S32SYS);
icculus@1982
   254
    }
icculus@1982
   255
}
icculus@10815
   256
#endif
icculus@10815
   257
icculus@10815
   258
icculus@10815
   259
#if HAVE_SSE2_INTRINSICS
icculus@10815
   260
static void SDLCALL
icculus@10815
   261
SDL_Convert_S8_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10815
   262
{
icculus@10815
   263
    const Sint8 *src = ((const Sint8 *) (cvt->buf + cvt->len_cvt)) - 1;
icculus@10815
   264
    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
icculus@10815
   265
    int i;
icculus@10815
   266
icculus@10815
   267
    LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32 (using SSE2)");
icculus@10815
   268
icculus@10815
   269
    /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
icculus@10815
   270
    for (i = cvt->len_cvt; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) {
icculus@11403
   271
        *dst = ((float) *src) * DIVBY128;
icculus@10815
   272
    }
icculus@10815
   273
icculus@10815
   274
    src -= 15; dst -= 15;  /* adjust to read SSE blocks from the start. */
icculus@10815
   275
    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
icculus@10815
   276
icculus@10815
   277
    /* Make sure src is aligned too. */
icculus@10815
   278
    if ((((size_t) src) & 15) == 0) {
icculus@10815
   279
        /* Aligned! Do SSE blocks as long as we have 16 bytes available. */
icculus@10815
   280
        const __m128i *mmsrc = (const __m128i *) src;
icculus@10815
   281
        const __m128i zero = _mm_setzero_si128();
icculus@11403
   282
        const __m128 divby128 = _mm_set1_ps(DIVBY128);
icculus@10815
   283
        while (i >= 16) {   /* 16 * 8-bit */
icculus@10815
   284
            const __m128i bytes = _mm_load_si128(mmsrc);  /* get 16 sint8 into an XMM register. */
icculus@10815
   285
            /* treat as int16, shift left to clear every other sint16, then back right with sign-extend. Now sint16. */
icculus@10815
   286
            const __m128i shorts1 = _mm_srai_epi16(_mm_slli_epi16(bytes, 8), 8);
icculus@10815
   287
            /* right-shift-sign-extend gets us sint16 with the other set of values. */
icculus@10815
   288
            const __m128i shorts2 = _mm_srai_epi16(bytes, 8);
icculus@10815
   289
            /* unpack against zero to make these int32, shift to make them sign-extend, convert to float, multiply. Whew! */
icculus@11403
   290
            const __m128 floats1 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpacklo_epi16(shorts1, zero), 16), 16)), divby128);
icculus@11403
   291
            const __m128 floats2 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpacklo_epi16(shorts2, zero), 16), 16)), divby128);
icculus@11403
   292
            const __m128 floats3 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpackhi_epi16(shorts1, zero), 16), 16)), divby128);
icculus@11403
   293
            const __m128 floats4 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpackhi_epi16(shorts2, zero), 16), 16)), divby128);
icculus@10815
   294
            /* Interleave back into correct order, store. */
icculus@10815
   295
            _mm_store_ps(dst, _mm_unpacklo_ps(floats1, floats2));
icculus@10815
   296
            _mm_store_ps(dst+4, _mm_unpackhi_ps(floats1, floats2));
icculus@10815
   297
            _mm_store_ps(dst+8, _mm_unpacklo_ps(floats3, floats4));
icculus@10815
   298
            _mm_store_ps(dst+12, _mm_unpackhi_ps(floats3, floats4));
icculus@10815
   299
            i -= 16; mmsrc--; dst -= 16;
icculus@10815
   300
        }
icculus@10815
   301
icculus@10815
   302
        src = (const Sint8 *) mmsrc;
icculus@10815
   303
    }
icculus@10815
   304
icculus@10815
   305
    src += 15; dst += 15;  /* adjust for any scalar finishing. */
icculus@10815
   306
icculus@10815
   307
    /* Finish off any leftovers with scalar operations. */
icculus@10815
   308
    while (i) {
icculus@11403
   309
        *dst = ((float) *src) * DIVBY128;
icculus@10815
   310
        i--; src--; dst--;
icculus@10815
   311
    }
icculus@10815
   312
icculus@10815
   313
    cvt->len_cvt *= 4;
icculus@10815
   314
    if (cvt->filters[++cvt->filter_index]) {
icculus@10815
   315
        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
icculus@10815
   316
    }
icculus@10815
   317
}
icculus@10815
   318
icculus@10815
   319
static void SDLCALL
icculus@10815
   320
SDL_Convert_U8_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10815
   321
{
icculus@10815
   322
    const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
icculus@10815
   323
    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
icculus@10815
   324
    int i;
icculus@10815
   325
icculus@10815
   326
    LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32 (using SSE2)");
icculus@10815
   327
icculus@10815
   328
    /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
icculus@10815
   329
    for (i = cvt->len_cvt; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) {
icculus@11403
   330
        *dst = (((float) *src) * DIVBY128) - 1.0f;
icculus@10815
   331
    }
icculus@10815
   332
icculus@10815
   333
    src -= 15; dst -= 15;  /* adjust to read SSE blocks from the start. */
icculus@10815
   334
    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
icculus@10815
   335
icculus@10815
   336
    /* Make sure src is aligned too. */
icculus@10815
   337
    if ((((size_t) src) & 15) == 0) {
icculus@10815
   338
        /* Aligned! Do SSE blocks as long as we have 16 bytes available. */
icculus@10815
   339
        const __m128i *mmsrc = (const __m128i *) src;
icculus@10815
   340
        const __m128i zero = _mm_setzero_si128();
icculus@11403
   341
        const __m128 divby128 = _mm_set1_ps(DIVBY128);
icculus@10815
   342
        const __m128 minus1 = _mm_set1_ps(-1.0f);
icculus@10815
   343
        while (i >= 16) {   /* 16 * 8-bit */
icculus@10815
   344
            const __m128i bytes = _mm_load_si128(mmsrc);  /* get 16 uint8 into an XMM register. */
icculus@10815
   345
            /* treat as int16, shift left to clear every other sint16, then back right with zero-extend. Now uint16. */
icculus@10815
   346
            const __m128i shorts1 = _mm_srli_epi16(_mm_slli_epi16(bytes, 8), 8);
icculus@10815
   347
            /* right-shift-zero-extend gets us uint16 with the other set of values. */
icculus@10815
   348
            const __m128i shorts2 = _mm_srli_epi16(bytes, 8);
icculus@10815
   349
            /* unpack against zero to make these int32, convert to float, multiply, add. Whew! */
icculus@10815
   350
            /* Note that AVX2 can do floating point multiply+add in one instruction, fwiw. SSE2 cannot. */
icculus@11403
   351
            const __m128 floats1 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(shorts1, zero)), divby128), minus1);
icculus@11403
   352
            const __m128 floats2 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(shorts2, zero)), divby128), minus1);
icculus@11403
   353
            const __m128 floats3 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(shorts1, zero)), divby128), minus1);
icculus@11403
   354
            const __m128 floats4 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(shorts2, zero)), divby128), minus1);
icculus@10815
   355
            /* Interleave back into correct order, store. */
icculus@10815
   356
            _mm_store_ps(dst, _mm_unpacklo_ps(floats1, floats2));
icculus@10815
   357
            _mm_store_ps(dst+4, _mm_unpackhi_ps(floats1, floats2));
icculus@10815
   358
            _mm_store_ps(dst+8, _mm_unpacklo_ps(floats3, floats4));
icculus@10815
   359
            _mm_store_ps(dst+12, _mm_unpackhi_ps(floats3, floats4));
icculus@10815
   360
            i -= 16; mmsrc--; dst -= 16;
icculus@10815
   361
        }
icculus@10815
   362
icculus@10815
   363
        src = (const Uint8 *) mmsrc;
icculus@10815
   364
    }
icculus@10815
   365
icculus@10815
   366
    src += 15; dst += 15;  /* adjust for any scalar finishing. */
icculus@10815
   367
icculus@10815
   368
    /* Finish off any leftovers with scalar operations. */
icculus@10815
   369
    while (i) {
icculus@11403
   370
        *dst = (((float) *src) * DIVBY128) - 1.0f;
icculus@10815
   371
        i--; src--; dst--;
icculus@10815
   372
    }
icculus@10815
   373
icculus@10815
   374
    cvt->len_cvt *= 4;
icculus@10815
   375
    if (cvt->filters[++cvt->filter_index]) {
icculus@10815
   376
        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
icculus@10815
   377
    }
icculus@10815
   378
}
icculus@10815
   379
icculus@10815
   380
static void SDLCALL
icculus@10815
   381
SDL_Convert_S16_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10815
   382
{
icculus@10815
   383
    const Sint16 *src = ((const Sint16 *) (cvt->buf + cvt->len_cvt)) - 1;
icculus@10815
   384
    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
icculus@10815
   385
    int i;
icculus@10815
   386
icculus@10815
   387
    LOG_DEBUG_CONVERT("AUDIO_S16", "AUDIO_F32 (using SSE2)");
icculus@10815
   388
icculus@10815
   389
    /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
icculus@10815
   390
    for (i = cvt->len_cvt / sizeof (Sint16); i && (((size_t) (dst-7)) & 15); --i, --src, --dst) {
icculus@11403
   391
        *dst = ((float) *src) * DIVBY32768;
icculus@10815
   392
    }
icculus@10815
   393
icculus@10815
   394
    src -= 7; dst -= 7;  /* adjust to read SSE blocks from the start. */
icculus@10815
   395
    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
icculus@10815
   396
icculus@10815
   397
    /* Make sure src is aligned too. */
icculus@10815
   398
    if ((((size_t) src) & 15) == 0) {
icculus@10815
   399
        /* Aligned! Do SSE blocks as long as we have 16 bytes available. */
icculus@11403
   400
        const __m128 divby32768 = _mm_set1_ps(DIVBY32768);
icculus@10815
   401
        while (i >= 8) {   /* 8 * 16-bit */
icculus@10815
   402
            const __m128i ints = _mm_load_si128((__m128i const *) src);  /* get 8 sint16 into an XMM register. */
icculus@10815
   403
            /* treat as int32, shift left to clear every other sint16, then back right with sign-extend. Now sint32. */
icculus@10815
   404
            const __m128i a = _mm_srai_epi32(_mm_slli_epi32(ints, 16), 16);
icculus@10815
   405
            /* right-shift-sign-extend gets us sint32 with the other set of values. */
icculus@10815
   406
            const __m128i b = _mm_srai_epi32(ints, 16);
icculus@10815
   407
            /* Interleave these back into the right order, convert to float, multiply, store. */
icculus@11403
   408
            _mm_store_ps(dst, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi32(a, b)), divby32768));
icculus@11403
   409
            _mm_store_ps(dst+4, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi32(a, b)), divby32768));
icculus@10815
   410
            i -= 8; src -= 8; dst -= 8;
icculus@10815
   411
        }
icculus@10815
   412
    }
icculus@10815
   413
icculus@10815
   414
    src += 7; dst += 7;  /* adjust for any scalar finishing. */
icculus@10815
   415
icculus@10815
   416
    /* Finish off any leftovers with scalar operations. */
icculus@10815
   417
    while (i) {
icculus@11403
   418
        *dst = ((float) *src) * DIVBY32768;
icculus@10815
   419
        i--; src--; dst--;
icculus@10815
   420
    }
icculus@10815
   421
icculus@10815
   422
    cvt->len_cvt *= 2;
icculus@10815
   423
    if (cvt->filters[++cvt->filter_index]) {
icculus@10815
   424
        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
icculus@10815
   425
    }
icculus@10815
   426
}
icculus@10815
   427
icculus@10815
   428
static void SDLCALL
icculus@10815
   429
SDL_Convert_U16_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10815
   430
{
icculus@10815
   431
    const Uint16 *src = ((const Uint16 *) (cvt->buf + cvt->len_cvt)) - 1;
icculus@10815
   432
    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
icculus@10815
   433
    int i;
icculus@10815
   434
icculus@10815
   435
    LOG_DEBUG_CONVERT("AUDIO_U16", "AUDIO_F32 (using SSE2)");
icculus@10815
   436
icculus@10815
   437
    /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
icculus@10815
   438
    for (i = cvt->len_cvt / sizeof (Sint16); i && (((size_t) (dst-7)) & 15); --i, --src, --dst) {
icculus@11403
   439
        *dst = (((float) *src) * DIVBY32768) - 1.0f;
icculus@10815
   440
    }
icculus@10815
   441
icculus@10815
   442
    src -= 7; dst -= 7;  /* adjust to read SSE blocks from the start. */
icculus@10815
   443
    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
icculus@10815
   444
icculus@10815
   445
    /* Make sure src is aligned too. */
icculus@10815
   446
    if ((((size_t) src) & 15) == 0) {
icculus@10815
   447
        /* Aligned! Do SSE blocks as long as we have 16 bytes available. */
icculus@11403
   448
        const __m128 divby32768 = _mm_set1_ps(DIVBY32768);
icculus@10815
   449
        const __m128 minus1 = _mm_set1_ps(1.0f);
icculus@10815
   450
        while (i >= 8) {   /* 8 * 16-bit */
icculus@10815
   451
            const __m128i ints = _mm_load_si128((__m128i const *) src);  /* get 8 sint16 into an XMM register. */
icculus@10815
   452
            /* treat as int32, shift left to clear every other sint16, then back right with zero-extend. Now sint32. */
icculus@10815
   453
            const __m128i a = _mm_srli_epi32(_mm_slli_epi32(ints, 16), 16);
icculus@10815
   454
            /* right-shift-sign-extend gets us sint32 with the other set of values. */
icculus@10815
   455
            const __m128i b = _mm_srli_epi32(ints, 16);
icculus@10815
   456
            /* Interleave these back into the right order, convert to float, multiply, store. */
icculus@11403
   457
            _mm_store_ps(dst, _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi32(a, b)), divby32768), minus1));
icculus@11403
   458
            _mm_store_ps(dst+4, _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi32(a, b)), divby32768), minus1));
icculus@10815
   459
            i -= 8; src -= 8; dst -= 8;
icculus@10815
   460
        }
icculus@10815
   461
    }
icculus@10815
   462
icculus@10815
   463
    src += 7; dst += 7;  /* adjust for any scalar finishing. */
icculus@10815
   464
icculus@10815
   465
    /* Finish off any leftovers with scalar operations. */
icculus@10815
   466
    while (i) {
icculus@11403
   467
        *dst = (((float) *src) * DIVBY32768) - 1.0f;
icculus@10815
   468
        i--; src--; dst--;
icculus@10815
   469
    }
icculus@10815
   470
icculus@10815
   471
    cvt->len_cvt *= 2;
icculus@10815
   472
    if (cvt->filters[++cvt->filter_index]) {
icculus@10815
   473
        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
icculus@10815
   474
    }
icculus@10815
   475
}
icculus@10815
   476
icculus@10815
   477
static void SDLCALL
icculus@10815
   478
SDL_Convert_S32_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10815
   479
{
icculus@10815
   480
    const Sint32 *src = (const Sint32 *) cvt->buf;
icculus@10815
   481
    float *dst = (float *) cvt->buf;
icculus@10815
   482
    int i;
icculus@10815
   483
icculus@10815
   484
    LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32 (using SSE2)");
icculus@10815
   485
icculus@10815
   486
    /* Get dst aligned to 16 bytes */
icculus@10815
   487
    for (i = cvt->len_cvt / sizeof (Sint32); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
icculus@11403
   488
        *dst = (float) (((double) *src) * DIVBY2147483648);
icculus@10815
   489
    }
icculus@10815
   490
icculus@10815
   491
    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
icculus@10815
   492
    SDL_assert(!i || ((((size_t) src) & 15) == 0));
icculus@10815
   493
icculus@10815
   494
    {
icculus@10815
   495
        /* Aligned! Do SSE blocks as long as we have 16 bytes available. */
icculus@11403
   496
        const __m128d divby2147483648 = _mm_set1_pd(DIVBY2147483648);
icculus@10815
   497
        const __m128i *mmsrc = (const __m128i *) src;
icculus@10815
   498
        while (i >= 4) {   /* 4 * sint32 */
icculus@10815
   499
            const __m128i ints = _mm_load_si128(mmsrc);
icculus@10815
   500
            /* bitshift the whole register over, so _mm_cvtepi32_pd can read the top ints in the bottom of the vector. */
icculus@11403
   501
            const __m128d doubles1 = _mm_mul_pd(_mm_cvtepi32_pd(_mm_srli_si128(ints, 8)), divby2147483648);
icculus@11403
   502
            const __m128d doubles2 = _mm_mul_pd(_mm_cvtepi32_pd(ints), divby2147483648);
icculus@10815
   503
            /* convert to float32, bitshift/or to get these into a vector to store. */
icculus@10837
   504
            _mm_store_ps(dst, _mm_castsi128_ps(_mm_or_si128(_mm_slli_si128(_mm_castps_si128(_mm_cvtpd_ps(doubles1)), 8), _mm_castps_si128(_mm_cvtpd_ps(doubles2)))));
icculus@10815
   505
            i -= 4; mmsrc++; dst += 4;
icculus@10815
   506
        }
icculus@10815
   507
        src = (const Sint32 *) mmsrc;
icculus@10815
   508
    }
icculus@10815
   509
icculus@10815
   510
    /* Finish off any leftovers with scalar operations. */
icculus@10815
   511
    while (i) {
icculus@11403
   512
        *dst = (float) (((double) *src) * DIVBY2147483648);
icculus@10815
   513
        i--; src++; dst++;
icculus@10815
   514
    }
icculus@10815
   515
icculus@10815
   516
    if (cvt->filters[++cvt->filter_index]) {
icculus@10815
   517
        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
icculus@10815
   518
    }
icculus@10815
   519
}
icculus@10815
   520
icculus@10815
   521
static void SDLCALL
icculus@10815
   522
SDL_Convert_F32_to_S8_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10815
   523
{
icculus@10815
   524
    const float *src = (const float *) cvt->buf;
icculus@10815
   525
    Sint8 *dst = (Sint8 *) cvt->buf;
icculus@10815
   526
    int i;
icculus@10815
   527
icculus@10815
   528
    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S8 (using SSE2)");
icculus@10815
   529
icculus@10815
   530
    /* Get dst aligned to 16 bytes */
icculus@10815
   531
    for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
icculus@10815
   532
        *dst = (Sint8) (*src * 127.0f);
icculus@10815
   533
    }
icculus@10815
   534
icculus@10815
   535
    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
icculus@10815
   536
icculus@10815
   537
    /* Make sure src is aligned too. */
icculus@10815
   538
    if ((((size_t) src) & 15) == 0) {
icculus@10815
   539
        /* Aligned! Do SSE blocks as long as we have 16 bytes available. */
icculus@10815
   540
        const __m128 mulby127 = _mm_set1_ps(127.0f);
icculus@10815
   541
        __m128i *mmdst = (__m128i *) dst;
icculus@10815
   542
        while (i >= 16) {   /* 16 * float32 */
icculus@10815
   543
            const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src), mulby127));  /* load 4 floats, convert to sint32 */
icculus@10815
   544
            const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src+4), mulby127));  /* load 4 floats, convert to sint32 */
icculus@10815
   545
            const __m128i ints3 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src+8), mulby127));  /* load 4 floats, convert to sint32 */
icculus@10815
   546
            const __m128i ints4 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src+12), mulby127));  /* load 4 floats, convert to sint32 */
icculus@10815
   547
            _mm_store_si128(mmdst, _mm_packs_epi16(_mm_packs_epi32(ints1, ints2), _mm_packs_epi32(ints3, ints4)));  /* pack down, store out. */
icculus@10815
   548
            i -= 16; src += 16; mmdst++;
icculus@10815
   549
        }
icculus@10815
   550
        dst = (Sint8 *) mmdst;
icculus@10815
   551
    }
icculus@10815
   552
icculus@10815
   553
    /* Finish off any leftovers with scalar operations. */
icculus@10815
   554
    while (i) {
icculus@10815
   555
        *dst = (Sint8) (*src * 127.0f);
icculus@10815
   556
        i--; src++; dst++;
icculus@10815
   557
    }
icculus@10815
   558
icculus@10815
   559
    cvt->len_cvt /= 4;
icculus@10815
   560
    if (cvt->filters[++cvt->filter_index]) {
icculus@10815
   561
        cvt->filters[cvt->filter_index](cvt, AUDIO_S8);
icculus@10815
   562
    }
icculus@10815
   563
}
icculus@10815
   564
icculus@10815
   565
static void SDLCALL
icculus@10815
   566
SDL_Convert_F32_to_U8_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10815
   567
{
icculus@10815
   568
    const float *src = (const float *) cvt->buf;
icculus@10815
   569
    Uint8 *dst = (Uint8 *) cvt->buf;
icculus@10815
   570
    int i;
icculus@10815
   571
icculus@10815
   572
    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U8 (using SSE2)");
icculus@10815
   573
icculus@10815
   574
    /* Get dst aligned to 16 bytes */
icculus@10815
   575
    for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
icculus@10815
   576
        *dst = (Uint8) ((*src + 1.0f) * 127.0f);
icculus@10815
   577
    }
icculus@10815
   578
icculus@10815
   579
    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
icculus@10815
   580
icculus@10815
   581
    /* Make sure src is aligned too. */
icculus@10815
   582
    if ((((size_t) src) & 15) == 0) {
icculus@10815
   583
        /* Aligned! Do SSE blocks as long as we have 16 bytes available. */
icculus@10815
   584
        const __m128 add1 = _mm_set1_ps(1.0f);
icculus@10815
   585
        const __m128 mulby127 = _mm_set1_ps(127.0f);
icculus@10815
   586
        __m128i *mmdst = (__m128i *) dst;
icculus@10815
   587
        while (i >= 16) {   /* 16 * float32 */
icculus@10815
   588
            const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_load_ps(src), add1), mulby127));  /* load 4 floats, convert to sint32 */
icculus@10815
   589
            const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_load_ps(src+4), add1), mulby127));  /* load 4 floats, convert to sint32 */
icculus@10815
   590
            const __m128i ints3 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_load_ps(src+8), add1), mulby127));  /* load 4 floats, convert to sint32 */
icculus@10815
   591
            const __m128i ints4 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_load_ps(src+12), add1), mulby127));  /* load 4 floats, convert to sint32 */
icculus@10815
   592
            _mm_store_si128(mmdst, _mm_packus_epi16(_mm_packs_epi32(ints1, ints2), _mm_packs_epi32(ints3, ints4)));  /* pack down, store out. */
icculus@10815
   593
            i -= 16; src += 16; mmdst++;
icculus@10815
   594
        }
icculus@10815
   595
        dst = (Uint8 *) mmdst;
icculus@10815
   596
    }
icculus@10815
   597
icculus@10815
   598
    /* Finish off any leftovers with scalar operations. */
icculus@10815
   599
    while (i) {
icculus@10815
   600
        *dst = (Uint8) ((*src + 1.0f) * 127.0f);
icculus@10815
   601
        i--; src++; dst++;
icculus@10815
   602
    }
icculus@10815
   603
icculus@10815
   604
    cvt->len_cvt /= 4;
icculus@10815
   605
    if (cvt->filters[++cvt->filter_index]) {
icculus@10815
   606
        cvt->filters[cvt->filter_index](cvt, AUDIO_U8);
icculus@10815
   607
    }
icculus@10815
   608
}
icculus@10815
   609
icculus@10815
   610
static void SDLCALL
icculus@10815
   611
SDL_Convert_F32_to_S16_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10815
   612
{
icculus@10815
   613
    const float *src = (const float *) cvt->buf;
icculus@10815
   614
    Sint16 *dst = (Sint16 *) cvt->buf;
icculus@10815
   615
    int i;
icculus@10815
   616
icculus@10815
   617
    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S16 (using SSE2)");
icculus@10815
   618
icculus@10815
   619
    /* Get dst aligned to 16 bytes */
icculus@10815
   620
    for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
icculus@10815
   621
        *dst = (Sint16) (*src * 32767.0f);
icculus@10815
   622
    }
icculus@10815
   623
icculus@10815
   624
    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
icculus@10815
   625
icculus@10815
   626
    /* Make sure src is aligned too. */
icculus@10815
   627
    if ((((size_t) src) & 15) == 0) {
icculus@10815
   628
        /* Aligned! Do SSE blocks as long as we have 16 bytes available. */
icculus@10815
   629
        const __m128 mulby32767 = _mm_set1_ps(32767.0f);
icculus@10815
   630
        __m128i *mmdst = (__m128i *) dst;
icculus@10815
   631
        while (i >= 8) {   /* 8 * float32 */
icculus@10815
   632
            const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src), mulby32767));  /* load 4 floats, convert to sint32 */
icculus@10815
   633
            const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src+4), mulby32767));  /* load 4 floats, convert to sint32 */
icculus@10815
   634
            _mm_store_si128(mmdst, _mm_packs_epi32(ints1, ints2));  /* pack to sint16, store out. */
icculus@10815
   635
            i -= 8; src += 8; mmdst++;
icculus@10815
   636
        }
icculus@10815
   637
        dst = (Sint16 *) mmdst;
icculus@10815
   638
    }
icculus@10815
   639
icculus@10815
   640
    /* Finish off any leftovers with scalar operations. */
icculus@10815
   641
    while (i) {
icculus@10838
   642
        *dst = (Sint16) (*src * 32767.0f);
icculus@10815
   643
        i--; src++; dst++;
icculus@10815
   644
    }
icculus@10815
   645
icculus@10815
   646
    cvt->len_cvt /= 2;
icculus@10815
   647
    if (cvt->filters[++cvt->filter_index]) {
icculus@10815
   648
        cvt->filters[cvt->filter_index](cvt, AUDIO_S16SYS);
icculus@10815
   649
    }
icculus@10815
   650
}
icculus@10815
   651
icculus@10815
   652
static void SDLCALL
icculus@10815
   653
SDL_Convert_F32_to_U16_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10815
   654
{
icculus@10815
   655
    const float *src = (const float *) cvt->buf;
icculus@10815
   656
    Uint16 *dst = (Uint16 *) cvt->buf;
icculus@10815
   657
    int i;
icculus@10815
   658
icculus@10815
   659
    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U16 (using SSE2)");
icculus@10815
   660
icculus@10815
   661
    /* Get dst aligned to 16 bytes */
icculus@10815
   662
    for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
icculus@10815
   663
        *dst = (Uint16) ((*src + 1.0f) * 32767.0f);
icculus@10815
   664
    }
icculus@10815
   665
icculus@10815
   666
    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
icculus@10815
   667
icculus@10815
   668
    /* Make sure src is aligned too. */
icculus@10815
   669
    if ((((size_t) src) & 15) == 0) {
icculus@10815
   670
        /* Aligned! Do SSE blocks as long as we have 16 bytes available. */
icculus@10815
   671
        /* This calculates differently than the scalar path because SSE2 can't
icculus@10815
   672
           pack int32 data down to unsigned int16. _mm_packs_epi32 does signed
icculus@10815
   673
           saturation, so that would corrupt our data. _mm_packus_epi32 exists,
icculus@10815
   674
           but not before SSE 4.1. So we convert from float to sint16, packing
icculus@10815
   675
           that down with legit signed saturation, and then xor the top bit
icculus@10815
   676
           against 1. This results in the correct unsigned 16-bit value, even
icculus@10815
   677
           though it looks like dark magic. */
icculus@10815
   678
        const __m128 mulby32767 = _mm_set1_ps(32767.0f);
icculus@10815
   679
        const __m128i topbit = _mm_set1_epi16(-32768);
icculus@10815
   680
        __m128i *mmdst = (__m128i *) dst;
icculus@10815
   681
        while (i >= 8) {   /* 8 * float32 */
icculus@10815
   682
            const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src), mulby32767));  /* load 4 floats, convert to sint32 */
icculus@10815
   683
            const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src+4), mulby32767));  /* load 4 floats, convert to sint32 */
icculus@10815
   684
            _mm_store_si128(mmdst, _mm_xor_si128(_mm_packs_epi32(ints1, ints2), topbit));  /* pack to sint16, xor top bit, store out. */
icculus@10815
   685
            i -= 8; src += 8; mmdst++;
icculus@10815
   686
        }
icculus@10815
   687
        dst = (Uint16 *) mmdst;
icculus@10815
   688
    }
icculus@10815
   689
icculus@10815
   690
    /* Finish off any leftovers with scalar operations. */
icculus@10815
   691
    while (i) {
icculus@10815
   692
        *dst = (Uint16) ((*src + 1.0f) * 32767.0f);
icculus@10815
   693
        i--; src++; dst++;
icculus@10815
   694
    }
icculus@10815
   695
icculus@10815
   696
    cvt->len_cvt /= 2;
icculus@10815
   697
    if (cvt->filters[++cvt->filter_index]) {
icculus@10815
   698
        cvt->filters[cvt->filter_index](cvt, AUDIO_U16SYS);
icculus@10815
   699
    }
icculus@10815
   700
}
icculus@10815
   701
icculus@10815
   702
static void SDLCALL
icculus@10815
   703
SDL_Convert_F32_to_S32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10815
   704
{
icculus@10815
   705
    const float *src = (const float *) cvt->buf;
icculus@10815
   706
    Sint32 *dst = (Sint32 *) cvt->buf;
icculus@10815
   707
    int i;
icculus@10815
   708
icculus@10815
   709
    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32 (using SSE2)");
icculus@10815
   710
icculus@10815
   711
    /* Get dst aligned to 16 bytes */
icculus@10815
   712
    for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
icculus@10815
   713
        *dst = (Sint32) (((double) *src) * 2147483647.0);
icculus@10815
   714
    }
icculus@10815
   715
icculus@10815
   716
    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
icculus@10815
   717
    SDL_assert(!i || ((((size_t) src) & 15) == 0));
icculus@10815
   718
icculus@10815
   719
    {
icculus@10815
   720
        /* Aligned! Do SSE blocks as long as we have 16 bytes available. */
icculus@10815
   721
        const __m128d mulby2147483647 = _mm_set1_pd(2147483647.0);
icculus@10815
   722
        __m128i *mmdst = (__m128i *) dst;
icculus@10815
   723
        while (i >= 4) {   /* 4 * float32 */
icculus@10815
   724
            const __m128 floats = _mm_load_ps(src);
icculus@10815
   725
            /* bitshift the whole register over, so _mm_cvtps_pd can read the top floats in the bottom of the vector. */
icculus@10836
   726
            const __m128d doubles1 = _mm_mul_pd(_mm_cvtps_pd(_mm_castsi128_ps(_mm_srli_si128(_mm_castps_si128(floats), 8))), mulby2147483647);
icculus@10815
   727
            const __m128d doubles2 = _mm_mul_pd(_mm_cvtps_pd(floats), mulby2147483647);
icculus@10837
   728
            _mm_store_si128(mmdst, _mm_or_si128(_mm_slli_si128(_mm_cvtpd_epi32(doubles1), 8), _mm_cvtpd_epi32(doubles2)));
icculus@10815
   729
            i -= 4; src += 4; mmdst++;
icculus@10815
   730
        }
icculus@10815
   731
        dst = (Sint32 *) mmdst;
icculus@10815
   732
    }
icculus@10815
   733
icculus@10815
   734
    /* Finish off any leftovers with scalar operations. */
icculus@10815
   735
    while (i) {
icculus@10815
   736
        *dst = (Sint32) (((double) *src) * 2147483647.0);
icculus@10815
   737
        i--; src++; dst++;
icculus@10815
   738
    }
icculus@10815
   739
icculus@10815
   740
    if (cvt->filters[++cvt->filter_index]) {
icculus@10815
   741
        cvt->filters[cvt->filter_index](cvt, AUDIO_S32SYS);
icculus@10815
   742
    }
icculus@10815
   743
}
icculus@10815
   744
#endif
icculus@10815
   745
icculus@10815
   746
icculus@10815
   747
void SDL_ChooseAudioConverters(void)
icculus@10815
   748
{
icculus@10815
   749
    static SDL_bool converters_chosen = SDL_FALSE;
icculus@10815
   750
icculus@10815
   751
    if (converters_chosen) {
icculus@10815
   752
        return;
icculus@10815
   753
    }
icculus@10815
   754
icculus@10815
   755
    #define SET_CONVERTER_FUNCS(fntype) \
icculus@10815
   756
        SDL_Convert_S8_to_F32 = SDL_Convert_S8_to_F32_##fntype; \
icculus@10815
   757
        SDL_Convert_U8_to_F32 = SDL_Convert_U8_to_F32_##fntype; \
icculus@10815
   758
        SDL_Convert_S16_to_F32 = SDL_Convert_S16_to_F32_##fntype; \
icculus@10815
   759
        SDL_Convert_U16_to_F32 = SDL_Convert_U16_to_F32_##fntype; \
icculus@10815
   760
        SDL_Convert_S32_to_F32 = SDL_Convert_S32_to_F32_##fntype; \
icculus@10815
   761
        SDL_Convert_F32_to_S8 = SDL_Convert_F32_to_S8_##fntype; \
icculus@10815
   762
        SDL_Convert_F32_to_U8 = SDL_Convert_F32_to_U8_##fntype; \
icculus@10815
   763
        SDL_Convert_F32_to_S16 = SDL_Convert_F32_to_S16_##fntype; \
icculus@10815
   764
        SDL_Convert_F32_to_U16 = SDL_Convert_F32_to_U16_##fntype; \
icculus@10815
   765
        SDL_Convert_F32_to_S32 = SDL_Convert_F32_to_S32_##fntype; \
icculus@10815
   766
        converters_chosen = SDL_TRUE
icculus@10815
   767
icculus@10815
   768
    #if HAVE_SSE2_INTRINSICS
icculus@10815
   769
    if (SDL_HasSSE2()) {
icculus@10815
   770
        SET_CONVERTER_FUNCS(SSE2);
icculus@10815
   771
        return;
icculus@10815
   772
    }
icculus@10815
   773
    #endif
icculus@10815
   774
icculus@10815
   775
    #if NEED_SCALAR_CONVERTER_FALLBACKS
icculus@10815
   776
    SET_CONVERTER_FUNCS(Scalar);
icculus@10815
   777
    #endif
icculus@10815
   778
icculus@10815
   779
    #undef SET_CONVERTER_FUNCS
icculus@10815
   780
icculus@10815
   781
    SDL_assert(converters_chosen == SDL_TRUE);
icculus@10815
   782
}
icculus@1982
   783
slouken@1985
   784
/* vi: set ts=4 sw=4 expandtab: */