src/audio/SDL_audiotypecvt.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 16 Jan 2017 00:58:28 -0500
changeset 10815 71bbe3233508
parent 10814 938218064f67
child 10835 0e9e7a128391
permissions -rw-r--r--
audio: Implemented SIMD support for audio data type converters.

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