Skip to content

Latest commit

 

History

History
1431 lines (1213 loc) · 53.6 KB

SDL_audiotypecvt.c

File metadata and controls

1431 lines (1213 loc) · 53.6 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 3, 2018
Jan 3, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../SDL_internal.h"
#include "SDL_audio.h"
#include "SDL_audio_c.h"
Jan 16, 2017
Jan 16, 2017
25
#include "SDL_cpuinfo.h"
Nov 5, 2016
Nov 5, 2016
26
#include "SDL_assert.h"
Sep 29, 2018
Sep 29, 2018
28
29
/* !!! FIXME: disabled until we fix https://bugzilla.libsdl.org/show_bug.cgi?id=4186 */
#if 0 /*def __ARM_NEON__*/
May 16, 2018
May 16, 2018
30
31
#define HAVE_NEON_INTRINSICS 1
#endif
Jan 16, 2017
Jan 16, 2017
32
Jan 23, 2017
Jan 23, 2017
33
34
#ifdef __SSE2__
#define HAVE_SSE2_INTRINSICS 1
Jan 16, 2017
Jan 16, 2017
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#endif
#if defined(__x86_64__) && HAVE_SSE2_INTRINSICS
#define NEED_SCALAR_CONVERTER_FALLBACKS 0 /* x86_64 guarantees SSE2. */
#elif __MACOSX__ && HAVE_SSE2_INTRINSICS
#define NEED_SCALAR_CONVERTER_FALLBACKS 0 /* Mac OS X/Intel guarantees SSE2. */
#elif defined(__ARM_ARCH) && (__ARM_ARCH >= 8) && HAVE_NEON_INTRINSICS
#define NEED_SCALAR_CONVERTER_FALLBACKS 0 /* ARMv8+ promise NEON. */
#elif defined(__APPLE__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7) && HAVE_NEON_INTRINSICS
#define NEED_SCALAR_CONVERTER_FALLBACKS 0 /* All Apple ARMv7 chips promise NEON support. */
#endif
/* Set to zero if platform is guaranteed to use a SIMD codepath here. */
#ifndef NEED_SCALAR_CONVERTER_FALLBACKS
#define NEED_SCALAR_CONVERTER_FALLBACKS 1
#endif
/* Function pointers set to a CPU-specific implementation. */
SDL_AudioFilter SDL_Convert_S8_to_F32 = NULL;
SDL_AudioFilter SDL_Convert_U8_to_F32 = NULL;
SDL_AudioFilter SDL_Convert_S16_to_F32 = NULL;
SDL_AudioFilter SDL_Convert_U16_to_F32 = NULL;
SDL_AudioFilter SDL_Convert_S32_to_F32 = NULL;
SDL_AudioFilter SDL_Convert_F32_to_S8 = NULL;
SDL_AudioFilter SDL_Convert_F32_to_U8 = NULL;
SDL_AudioFilter SDL_Convert_F32_to_S16 = NULL;
SDL_AudioFilter SDL_Convert_F32_to_U16 = NULL;
SDL_AudioFilter SDL_Convert_F32_to_S32 = NULL;
Aug 29, 2017
Aug 29, 2017
65
66
#define DIVBY128 0.0078125f
#define DIVBY32768 0.000030517578125f
May 15, 2018
May 15, 2018
67
#define DIVBY8388607 0.00000011920930376163766f
Jan 16, 2017
Jan 16, 2017
69
70
71
72
#if NEED_SCALAR_CONVERTER_FALLBACKS
static void SDLCALL
SDL_Convert_S8_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Jan 15, 2017
Jan 15, 2017
74
const Sint8 *src = ((const Sint8 *) (cvt->buf + cvt->len_cvt)) - 1;
Nov 5, 2016
Nov 5, 2016
75
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
Nov 5, 2016
Nov 5, 2016
78
LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32");
Jan 15, 2017
Jan 15, 2017
80
for (i = cvt->len_cvt; i; --i, --src, --dst) {
Aug 29, 2017
Aug 29, 2017
81
*dst = ((float) *src) * DIVBY128;
82
83
84
85
}
cvt->len_cvt *= 4;
if (cvt->filters[++cvt->filter_index]) {
Nov 5, 2016
Nov 5, 2016
86
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
Jan 16, 2017
Jan 16, 2017
90
91
static void SDLCALL
SDL_Convert_U8_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Nov 5, 2016
Nov 5, 2016
93
94
const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
Nov 5, 2016
Nov 5, 2016
97
LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32");
Jan 15, 2017
Jan 15, 2017
99
for (i = cvt->len_cvt; i; --i, --src, --dst) {
Aug 29, 2017
Aug 29, 2017
100
*dst = (((float) *src) * DIVBY128) - 1.0f;
101
102
103
104
}
cvt->len_cvt *= 4;
if (cvt->filters[++cvt->filter_index]) {
Nov 5, 2016
Nov 5, 2016
105
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
Jan 16, 2017
Jan 16, 2017
109
110
static void SDLCALL
SDL_Convert_S16_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Nov 5, 2016
Nov 5, 2016
112
113
const Sint16 *src = ((const Sint16 *) (cvt->buf + cvt->len_cvt)) - 1;
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
Nov 5, 2016
Nov 5, 2016
116
LOG_DEBUG_CONVERT("AUDIO_S16", "AUDIO_F32");
Nov 5, 2016
Nov 5, 2016
118
for (i = cvt->len_cvt / sizeof (Sint16); i; --i, --src, --dst) {
Aug 29, 2017
Aug 29, 2017
119
*dst = ((float) *src) * DIVBY32768;
120
121
122
123
}
cvt->len_cvt *= 2;
if (cvt->filters[++cvt->filter_index]) {
Nov 5, 2016
Nov 5, 2016
124
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
Jan 16, 2017
Jan 16, 2017
128
129
static void SDLCALL
SDL_Convert_U16_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Nov 5, 2016
Nov 5, 2016
131
132
const Uint16 *src = ((const Uint16 *) (cvt->buf + cvt->len_cvt)) - 1;
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
Nov 5, 2016
Nov 5, 2016
135
LOG_DEBUG_CONVERT("AUDIO_U16", "AUDIO_F32");
Nov 5, 2016
Nov 5, 2016
137
for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
Aug 29, 2017
Aug 29, 2017
138
*dst = (((float) *src) * DIVBY32768) - 1.0f;
139
140
141
142
}
cvt->len_cvt *= 2;
if (cvt->filters[++cvt->filter_index]) {
Nov 5, 2016
Nov 5, 2016
143
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
Jan 16, 2017
Jan 16, 2017
147
148
static void SDLCALL
SDL_Convert_S32_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Jan 15, 2017
Jan 15, 2017
150
const Sint32 *src = (const Sint32 *) cvt->buf;
Nov 5, 2016
Nov 5, 2016
151
float *dst = (float *) cvt->buf;
Nov 5, 2016
Nov 5, 2016
154
LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32");
Nov 5, 2016
Nov 5, 2016
156
for (i = cvt->len_cvt / sizeof (Sint32); i; --i, ++src, ++dst) {
May 15, 2018
May 15, 2018
157
*dst = ((float) (*src>>8)) * DIVBY8388607;
158
159
160
}
if (cvt->filters[++cvt->filter_index]) {
Nov 5, 2016
Nov 5, 2016
161
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
Jan 16, 2017
Jan 16, 2017
165
166
static void SDLCALL
SDL_Convert_F32_to_S8_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Nov 5, 2016
Nov 5, 2016
168
169
const float *src = (const float *) cvt->buf;
Sint8 *dst = (Sint8 *) cvt->buf;
Nov 5, 2016
Nov 5, 2016
172
LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S8");
Nov 5, 2016
Nov 5, 2016
174
for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
Oct 19, 2017
Oct 19, 2017
175
const float sample = *src;
May 15, 2018
May 15, 2018
176
if (sample >= 1.0f) {
Oct 19, 2017
Oct 19, 2017
177
*dst = 127;
May 15, 2018
May 15, 2018
178
} else if (sample <= -1.0f) {
May 15, 2018
May 15, 2018
179
*dst = -128;
Oct 19, 2017
Oct 19, 2017
180
181
182
} else {
*dst = (Sint8)(sample * 127.0f);
}
Nov 5, 2016
Nov 5, 2016
185
cvt->len_cvt /= 4;
186
if (cvt->filters[++cvt->filter_index]) {
Nov 5, 2016
Nov 5, 2016
187
cvt->filters[cvt->filter_index](cvt, AUDIO_S8);
Jan 16, 2017
Jan 16, 2017
191
192
static void SDLCALL
SDL_Convert_F32_to_U8_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Nov 5, 2016
Nov 5, 2016
194
195
const float *src = (const float *) cvt->buf;
Uint8 *dst = (Uint8 *) cvt->buf;
Nov 5, 2016
Nov 5, 2016
198
LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U8");
Nov 5, 2016
Nov 5, 2016
200
for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
Oct 19, 2017
Oct 19, 2017
201
const float sample = *src;
May 15, 2018
May 15, 2018
202
if (sample >= 1.0f) {
Oct 19, 2017
Oct 19, 2017
203
*dst = 255;
May 15, 2018
May 15, 2018
204
} else if (sample <= -1.0f) {
Oct 19, 2017
Oct 19, 2017
205
206
207
208
*dst = 0;
} else {
*dst = (Uint8)((sample + 1.0f) * 127.0f);
}
Nov 5, 2016
Nov 5, 2016
211
cvt->len_cvt /= 4;
212
if (cvt->filters[++cvt->filter_index]) {
Nov 5, 2016
Nov 5, 2016
213
cvt->filters[cvt->filter_index](cvt, AUDIO_U8);
Jan 16, 2017
Jan 16, 2017
217
218
static void SDLCALL
SDL_Convert_F32_to_S16_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Nov 5, 2016
Nov 5, 2016
220
221
const float *src = (const float *) cvt->buf;
Sint16 *dst = (Sint16 *) cvt->buf;
Nov 5, 2016
Nov 5, 2016
224
LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S16");
Nov 5, 2016
Nov 5, 2016
226
for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
Oct 19, 2017
Oct 19, 2017
227
const float sample = *src;
May 15, 2018
May 15, 2018
228
if (sample >= 1.0f) {
Oct 19, 2017
Oct 19, 2017
229
*dst = 32767;
May 15, 2018
May 15, 2018
230
} else if (sample <= -1.0f) {
May 15, 2018
May 15, 2018
231
*dst = -32768;
Oct 19, 2017
Oct 19, 2017
232
233
234
} else {
*dst = (Sint16)(sample * 32767.0f);
}
Nov 5, 2016
Nov 5, 2016
237
cvt->len_cvt /= 2;
238
if (cvt->filters[++cvt->filter_index]) {
Nov 5, 2016
Nov 5, 2016
239
cvt->filters[cvt->filter_index](cvt, AUDIO_S16SYS);
Jan 16, 2017
Jan 16, 2017
243
244
static void SDLCALL
SDL_Convert_F32_to_U16_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Nov 5, 2016
Nov 5, 2016
246
247
const float *src = (const float *) cvt->buf;
Uint16 *dst = (Uint16 *) cvt->buf;
Nov 5, 2016
Nov 5, 2016
250
LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U16");
Nov 5, 2016
Nov 5, 2016
252
for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
Oct 19, 2017
Oct 19, 2017
253
const float sample = *src;
May 15, 2018
May 15, 2018
254
if (sample >= 1.0f) {
May 15, 2018
May 15, 2018
255
*dst = 65535;
May 15, 2018
May 15, 2018
256
} else if (sample <= -1.0f) {
Oct 19, 2017
Oct 19, 2017
257
258
259
260
*dst = 0;
} else {
*dst = (Uint16)((sample + 1.0f) * 32767.0f);
}
Nov 5, 2016
Nov 5, 2016
263
cvt->len_cvt /= 2;
264
if (cvt->filters[++cvt->filter_index]) {
Nov 5, 2016
Nov 5, 2016
265
cvt->filters[cvt->filter_index](cvt, AUDIO_U16SYS);
Jan 16, 2017
Jan 16, 2017
269
270
static void SDLCALL
SDL_Convert_F32_to_S32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Nov 5, 2016
Nov 5, 2016
272
273
const float *src = (const float *) cvt->buf;
Sint32 *dst = (Sint32 *) cvt->buf;
Nov 5, 2016
Nov 5, 2016
276
LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32");
Nov 5, 2016
Nov 5, 2016
278
for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
Oct 19, 2017
Oct 19, 2017
279
const float sample = *src;
May 15, 2018
May 15, 2018
280
if (sample >= 1.0f) {
Oct 19, 2017
Oct 19, 2017
281
*dst = 2147483647;
May 15, 2018
May 15, 2018
282
} else if (sample <= -1.0f) {
May 21, 2018
May 21, 2018
283
*dst = (Sint32) -2147483648LL;
Oct 19, 2017
Oct 19, 2017
284
} else {
May 15, 2018
May 15, 2018
285
*dst = ((Sint32)(sample * 8388607.0f)) << 8;
Oct 19, 2017
Oct 19, 2017
286
}
287
288
289
}
if (cvt->filters[++cvt->filter_index]) {
Nov 5, 2016
Nov 5, 2016
290
cvt->filters[cvt->filter_index](cvt, AUDIO_S32SYS);
Jan 16, 2017
Jan 16, 2017
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#endif
#if HAVE_SSE2_INTRINSICS
static void SDLCALL
SDL_Convert_S8_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const Sint8 *src = ((const Sint8 *) (cvt->buf + cvt->len_cvt)) - 1;
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
int i;
LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32 (using SSE2)");
/* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
for (i = cvt->len_cvt; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) {
Aug 29, 2017
Aug 29, 2017
308
*dst = ((float) *src) * DIVBY128;
Jan 16, 2017
Jan 16, 2017
309
310
311
312
313
314
315
316
317
318
}
src -= 15; dst -= 15; /* adjust to read SSE blocks from the start. */
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
const __m128i *mmsrc = (const __m128i *) src;
const __m128i zero = _mm_setzero_si128();
Aug 29, 2017
Aug 29, 2017
319
const __m128 divby128 = _mm_set1_ps(DIVBY128);
Jan 16, 2017
Jan 16, 2017
320
321
322
323
324
325
326
while (i >= 16) { /* 16 * 8-bit */
const __m128i bytes = _mm_load_si128(mmsrc); /* get 16 sint8 into an XMM register. */
/* treat as int16, shift left to clear every other sint16, then back right with sign-extend. Now sint16. */
const __m128i shorts1 = _mm_srai_epi16(_mm_slli_epi16(bytes, 8), 8);
/* right-shift-sign-extend gets us sint16 with the other set of values. */
const __m128i shorts2 = _mm_srai_epi16(bytes, 8);
/* unpack against zero to make these int32, shift to make them sign-extend, convert to float, multiply. Whew! */
Aug 29, 2017
Aug 29, 2017
327
328
329
330
const __m128 floats1 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpacklo_epi16(shorts1, zero), 16), 16)), divby128);
const __m128 floats2 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpacklo_epi16(shorts2, zero), 16), 16)), divby128);
const __m128 floats3 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpackhi_epi16(shorts1, zero), 16), 16)), divby128);
const __m128 floats4 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpackhi_epi16(shorts2, zero), 16), 16)), divby128);
Jan 16, 2017
Jan 16, 2017
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* Interleave back into correct order, store. */
_mm_store_ps(dst, _mm_unpacklo_ps(floats1, floats2));
_mm_store_ps(dst+4, _mm_unpackhi_ps(floats1, floats2));
_mm_store_ps(dst+8, _mm_unpacklo_ps(floats3, floats4));
_mm_store_ps(dst+12, _mm_unpackhi_ps(floats3, floats4));
i -= 16; mmsrc--; dst -= 16;
}
src = (const Sint8 *) mmsrc;
}
src += 15; dst += 15; /* adjust for any scalar finishing. */
/* Finish off any leftovers with scalar operations. */
while (i) {
Aug 29, 2017
Aug 29, 2017
346
*dst = ((float) *src) * DIVBY128;
Jan 16, 2017
Jan 16, 2017
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
i--; src--; dst--;
}
cvt->len_cvt *= 4;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
}
}
static void SDLCALL
SDL_Convert_U8_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
int i;
LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32 (using SSE2)");
/* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
for (i = cvt->len_cvt; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) {
Aug 29, 2017
Aug 29, 2017
367
*dst = (((float) *src) * DIVBY128) - 1.0f;
Jan 16, 2017
Jan 16, 2017
368
369
370
371
372
373
374
375
376
377
}
src -= 15; dst -= 15; /* adjust to read SSE blocks from the start. */
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
const __m128i *mmsrc = (const __m128i *) src;
const __m128i zero = _mm_setzero_si128();
Aug 29, 2017
Aug 29, 2017
378
const __m128 divby128 = _mm_set1_ps(DIVBY128);
Jan 16, 2017
Jan 16, 2017
379
380
381
382
383
384
385
386
387
const __m128 minus1 = _mm_set1_ps(-1.0f);
while (i >= 16) { /* 16 * 8-bit */
const __m128i bytes = _mm_load_si128(mmsrc); /* get 16 uint8 into an XMM register. */
/* treat as int16, shift left to clear every other sint16, then back right with zero-extend. Now uint16. */
const __m128i shorts1 = _mm_srli_epi16(_mm_slli_epi16(bytes, 8), 8);
/* right-shift-zero-extend gets us uint16 with the other set of values. */
const __m128i shorts2 = _mm_srli_epi16(bytes, 8);
/* unpack against zero to make these int32, convert to float, multiply, add. Whew! */
/* Note that AVX2 can do floating point multiply+add in one instruction, fwiw. SSE2 cannot. */
Aug 29, 2017
Aug 29, 2017
388
389
390
391
const __m128 floats1 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(shorts1, zero)), divby128), minus1);
const __m128 floats2 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(shorts2, zero)), divby128), minus1);
const __m128 floats3 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(shorts1, zero)), divby128), minus1);
const __m128 floats4 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(shorts2, zero)), divby128), minus1);
Jan 16, 2017
Jan 16, 2017
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* Interleave back into correct order, store. */
_mm_store_ps(dst, _mm_unpacklo_ps(floats1, floats2));
_mm_store_ps(dst+4, _mm_unpackhi_ps(floats1, floats2));
_mm_store_ps(dst+8, _mm_unpacklo_ps(floats3, floats4));
_mm_store_ps(dst+12, _mm_unpackhi_ps(floats3, floats4));
i -= 16; mmsrc--; dst -= 16;
}
src = (const Uint8 *) mmsrc;
}
src += 15; dst += 15; /* adjust for any scalar finishing. */
/* Finish off any leftovers with scalar operations. */
while (i) {
Aug 29, 2017
Aug 29, 2017
407
*dst = (((float) *src) * DIVBY128) - 1.0f;
Jan 16, 2017
Jan 16, 2017
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
i--; src--; dst--;
}
cvt->len_cvt *= 4;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
}
}
static void SDLCALL
SDL_Convert_S16_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const Sint16 *src = ((const Sint16 *) (cvt->buf + cvt->len_cvt)) - 1;
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
int i;
LOG_DEBUG_CONVERT("AUDIO_S16", "AUDIO_F32 (using SSE2)");
/* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
for (i = cvt->len_cvt / sizeof (Sint16); i && (((size_t) (dst-7)) & 15); --i, --src, --dst) {
Aug 29, 2017
Aug 29, 2017
428
*dst = ((float) *src) * DIVBY32768;
Jan 16, 2017
Jan 16, 2017
429
430
431
432
433
434
435
436
}
src -= 7; dst -= 7; /* adjust to read SSE blocks from the start. */
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
Aug 29, 2017
Aug 29, 2017
437
const __m128 divby32768 = _mm_set1_ps(DIVBY32768);
Jan 16, 2017
Jan 16, 2017
438
439
440
441
442
443
444
while (i >= 8) { /* 8 * 16-bit */
const __m128i ints = _mm_load_si128((__m128i const *) src); /* get 8 sint16 into an XMM register. */
/* treat as int32, shift left to clear every other sint16, then back right with sign-extend. Now sint32. */
const __m128i a = _mm_srai_epi32(_mm_slli_epi32(ints, 16), 16);
/* right-shift-sign-extend gets us sint32 with the other set of values. */
const __m128i b = _mm_srai_epi32(ints, 16);
/* Interleave these back into the right order, convert to float, multiply, store. */
Aug 29, 2017
Aug 29, 2017
445
446
_mm_store_ps(dst, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi32(a, b)), divby32768));
_mm_store_ps(dst+4, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi32(a, b)), divby32768));
Jan 16, 2017
Jan 16, 2017
447
448
449
450
451
452
453
454
i -= 8; src -= 8; dst -= 8;
}
}
src += 7; dst += 7; /* adjust for any scalar finishing. */
/* Finish off any leftovers with scalar operations. */
while (i) {
Aug 29, 2017
Aug 29, 2017
455
*dst = ((float) *src) * DIVBY32768;
Jan 16, 2017
Jan 16, 2017
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
i--; src--; dst--;
}
cvt->len_cvt *= 2;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
}
}
static void SDLCALL
SDL_Convert_U16_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const Uint16 *src = ((const Uint16 *) (cvt->buf + cvt->len_cvt)) - 1;
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
int i;
LOG_DEBUG_CONVERT("AUDIO_U16", "AUDIO_F32 (using SSE2)");
/* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
for (i = cvt->len_cvt / sizeof (Sint16); i && (((size_t) (dst-7)) & 15); --i, --src, --dst) {
Aug 29, 2017
Aug 29, 2017
476
*dst = (((float) *src) * DIVBY32768) - 1.0f;
Jan 16, 2017
Jan 16, 2017
477
478
479
480
481
482
483
484
}
src -= 7; dst -= 7; /* adjust to read SSE blocks from the start. */
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
Aug 29, 2017
Aug 29, 2017
485
const __m128 divby32768 = _mm_set1_ps(DIVBY32768);
Jan 16, 2017
Jan 16, 2017
486
487
488
489
490
491
492
493
const __m128 minus1 = _mm_set1_ps(1.0f);
while (i >= 8) { /* 8 * 16-bit */
const __m128i ints = _mm_load_si128((__m128i const *) src); /* get 8 sint16 into an XMM register. */
/* treat as int32, shift left to clear every other sint16, then back right with zero-extend. Now sint32. */
const __m128i a = _mm_srli_epi32(_mm_slli_epi32(ints, 16), 16);
/* right-shift-sign-extend gets us sint32 with the other set of values. */
const __m128i b = _mm_srli_epi32(ints, 16);
/* Interleave these back into the right order, convert to float, multiply, store. */
Aug 29, 2017
Aug 29, 2017
494
495
_mm_store_ps(dst, _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi32(a, b)), divby32768), minus1));
_mm_store_ps(dst+4, _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi32(a, b)), divby32768), minus1));
Jan 16, 2017
Jan 16, 2017
496
497
498
499
500
501
502
503
i -= 8; src -= 8; dst -= 8;
}
}
src += 7; dst += 7; /* adjust for any scalar finishing. */
/* Finish off any leftovers with scalar operations. */
while (i) {
Aug 29, 2017
Aug 29, 2017
504
*dst = (((float) *src) * DIVBY32768) - 1.0f;
Jan 16, 2017
Jan 16, 2017
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
i--; src--; dst--;
}
cvt->len_cvt *= 2;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
}
}
static void SDLCALL
SDL_Convert_S32_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const Sint32 *src = (const Sint32 *) cvt->buf;
float *dst = (float *) cvt->buf;
int i;
LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32 (using SSE2)");
/* Get dst aligned to 16 bytes */
for (i = cvt->len_cvt / sizeof (Sint32); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
May 15, 2018
May 15, 2018
525
*dst = ((float) (*src>>8)) * DIVBY8388607;
Jan 16, 2017
Jan 16, 2017
526
527
528
529
530
531
532
}
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
SDL_assert(!i || ((((size_t) src) & 15) == 0));
{
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
May 15, 2018
May 15, 2018
533
const __m128 divby8388607 = _mm_set1_ps(DIVBY8388607);
Jan 16, 2017
Jan 16, 2017
534
535
const __m128i *mmsrc = (const __m128i *) src;
while (i >= 4) { /* 4 * sint32 */
May 15, 2018
May 15, 2018
536
/* shift out lowest bits so int fits in a float32. Small precision loss, but much faster. */
Jul 2, 2018
Jul 2, 2018
537
_mm_store_ps(dst, _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_load_si128(mmsrc), 8)), divby8388607));
Jan 16, 2017
Jan 16, 2017
538
539
540
541
542
543
544
i -= 4; mmsrc++; dst += 4;
}
src = (const Sint32 *) mmsrc;
}
/* Finish off any leftovers with scalar operations. */
while (i) {
May 15, 2018
May 15, 2018
545
*dst = ((float) (*src>>8)) * DIVBY8388607;
Jan 16, 2017
Jan 16, 2017
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
i--; src++; dst++;
}
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
}
}
static void SDLCALL
SDL_Convert_F32_to_S8_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const float *src = (const float *) cvt->buf;
Sint8 *dst = (Sint8 *) cvt->buf;
int i;
LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S8 (using SSE2)");
/* Get dst aligned to 16 bytes */
for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
May 15, 2018
May 15, 2018
565
566
567
568
569
570
571
572
const float sample = *src;
if (sample >= 1.0f) {
*dst = 127;
} else if (sample <= -1.0f) {
*dst = -128;
} else {
*dst = (Sint8)(sample * 127.0f);
}
Jan 16, 2017
Jan 16, 2017
573
574
575
576
577
578
579
}
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
May 15, 2018
May 15, 2018
580
581
const __m128 one = _mm_set1_ps(1.0f);
const __m128 negone = _mm_set1_ps(-1.0f);
Jan 16, 2017
Jan 16, 2017
582
583
584
const __m128 mulby127 = _mm_set1_ps(127.0f);
__m128i *mmdst = (__m128i *) dst;
while (i >= 16) { /* 16 * float32 */
May 15, 2018
May 15, 2018
585
586
587
588
const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src)), one), mulby127)); /* load 4 floats, clamp, convert to sint32 */
const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src+4)), one), mulby127)); /* load 4 floats, clamp, convert to sint32 */
const __m128i ints3 = _mm_cvtps_epi32(_mm_mul_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src+8)), one), mulby127)); /* load 4 floats, clamp, convert to sint32 */
const __m128i ints4 = _mm_cvtps_epi32(_mm_mul_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src+12)), one), mulby127)); /* load 4 floats, clamp, convert to sint32 */
Jan 16, 2017
Jan 16, 2017
589
590
591
592
593
594
595
596
_mm_store_si128(mmdst, _mm_packs_epi16(_mm_packs_epi32(ints1, ints2), _mm_packs_epi32(ints3, ints4))); /* pack down, store out. */
i -= 16; src += 16; mmdst++;
}
dst = (Sint8 *) mmdst;
}
/* Finish off any leftovers with scalar operations. */
while (i) {
May 15, 2018
May 15, 2018
597
598
599
600
601
602
603
604
const float sample = *src;
if (sample >= 1.0f) {
*dst = 127;
} else if (sample <= -1.0f) {
*dst = -128;
} else {
*dst = (Sint8)(sample * 127.0f);
}
Jan 16, 2017
Jan 16, 2017
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
i--; src++; dst++;
}
cvt->len_cvt /= 4;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_S8);
}
}
static void SDLCALL
SDL_Convert_F32_to_U8_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const float *src = (const float *) cvt->buf;
Uint8 *dst = (Uint8 *) cvt->buf;
int i;
LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U8 (using SSE2)");
/* Get dst aligned to 16 bytes */
for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
May 15, 2018
May 15, 2018
625
626
627
628
629
630
631
632
const float sample = *src;
if (sample >= 1.0f) {
*dst = 255;
} else if (sample <= -1.0f) {
*dst = 0;
} else {
*dst = (Uint8)((sample + 1.0f) * 127.0f);
}
Jan 16, 2017
Jan 16, 2017
633
634
635
636
637
638
639
}
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
May 15, 2018
May 15, 2018
640
641
const __m128 one = _mm_set1_ps(1.0f);
const __m128 negone = _mm_set1_ps(-1.0f);
Jan 16, 2017
Jan 16, 2017
642
643
644
const __m128 mulby127 = _mm_set1_ps(127.0f);
__m128i *mmdst = (__m128i *) dst;
while (i >= 16) { /* 16 * float32 */
May 15, 2018
May 15, 2018
645
646
647
648
const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src)), one), one), mulby127)); /* load 4 floats, clamp, convert to sint32 */
const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src+4)), one), one), mulby127)); /* load 4 floats, clamp, convert to sint32 */
const __m128i ints3 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src+8)), one), one), mulby127)); /* load 4 floats, clamp, convert to sint32 */
const __m128i ints4 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src+12)), one), one), mulby127)); /* load 4 floats, clamp, convert to sint32 */
Jan 16, 2017
Jan 16, 2017
649
650
651
652
653
654
655
656
_mm_store_si128(mmdst, _mm_packus_epi16(_mm_packs_epi32(ints1, ints2), _mm_packs_epi32(ints3, ints4))); /* pack down, store out. */
i -= 16; src += 16; mmdst++;
}
dst = (Uint8 *) mmdst;
}
/* Finish off any leftovers with scalar operations. */
while (i) {
May 15, 2018
May 15, 2018
657
658
659
660
661
662
663
664
const float sample = *src;
if (sample >= 1.0f) {
*dst = 255;
} else if (sample <= -1.0f) {
*dst = 0;
} else {
*dst = (Uint8)((sample + 1.0f) * 127.0f);
}
Jan 16, 2017
Jan 16, 2017
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
i--; src++; dst++;
}
cvt->len_cvt /= 4;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_U8);
}
}
static void SDLCALL
SDL_Convert_F32_to_S16_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const float *src = (const float *) cvt->buf;
Sint16 *dst = (Sint16 *) cvt->buf;
int i;
LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S16 (using SSE2)");
/* Get dst aligned to 16 bytes */
for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
May 15, 2018
May 15, 2018
685
686
687
688
689
690
691
692
const float sample = *src;
if (sample >= 1.0f) {
*dst = 32767;
} else if (sample <= -1.0f) {
*dst = -32768;
} else {
*dst = (Sint16)(sample * 32767.0f);
}
Jan 16, 2017
Jan 16, 2017
693
694
695
696
697
698
699
}
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
May 15, 2018
May 15, 2018
700
701
const __m128 one = _mm_set1_ps(1.0f);
const __m128 negone = _mm_set1_ps(-1.0f);
Jan 16, 2017
Jan 16, 2017
702
703
704
const __m128 mulby32767 = _mm_set1_ps(32767.0f);
__m128i *mmdst = (__m128i *) dst;
while (i >= 8) { /* 8 * float32 */
May 15, 2018
May 15, 2018
705
706
const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src)), one), mulby32767)); /* load 4 floats, clamp, convert to sint32 */
const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src+4)), one), mulby32767)); /* load 4 floats, clamp, convert to sint32 */
Jan 16, 2017
Jan 16, 2017
707
708
709
710
711
712
713
714
_mm_store_si128(mmdst, _mm_packs_epi32(ints1, ints2)); /* pack to sint16, store out. */
i -= 8; src += 8; mmdst++;
}
dst = (Sint16 *) mmdst;
}
/* Finish off any leftovers with scalar operations. */
while (i) {
May 15, 2018
May 15, 2018
715
716
717
718
719
720
721
722
const float sample = *src;
if (sample >= 1.0f) {
*dst = 32767;
} else if (sample <= -1.0f) {
*dst = -32768;
} else {
*dst = (Sint16)(sample * 32767.0f);
}
Jan 16, 2017
Jan 16, 2017
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
i--; src++; dst++;
}
cvt->len_cvt /= 2;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_S16SYS);
}
}
static void SDLCALL
SDL_Convert_F32_to_U16_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const float *src = (const float *) cvt->buf;
Uint16 *dst = (Uint16 *) cvt->buf;
int i;
LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U16 (using SSE2)");
/* Get dst aligned to 16 bytes */
for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
May 15, 2018
May 15, 2018
743
744
745
746
747
748
749
750
const float sample = *src;
if (sample >= 1.0f) {
*dst = 65535;
} else if (sample <= -1.0f) {
*dst = 0;
} else {
*dst = (Uint16)((sample + 1.0f) * 32767.0f);
}
Jan 16, 2017
Jan 16, 2017
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
}
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
/* This calculates differently than the scalar path because SSE2 can't
pack int32 data down to unsigned int16. _mm_packs_epi32 does signed
saturation, so that would corrupt our data. _mm_packus_epi32 exists,
but not before SSE 4.1. So we convert from float to sint16, packing
that down with legit signed saturation, and then xor the top bit
against 1. This results in the correct unsigned 16-bit value, even
though it looks like dark magic. */
const __m128 mulby32767 = _mm_set1_ps(32767.0f);
const __m128i topbit = _mm_set1_epi16(-32768);
May 15, 2018
May 15, 2018
767
768
const __m128 one = _mm_set1_ps(1.0f);
const __m128 negone = _mm_set1_ps(-1.0f);
Jan 16, 2017
Jan 16, 2017
769
770
__m128i *mmdst = (__m128i *) dst;
while (i >= 8) { /* 8 * float32 */
May 15, 2018
May 15, 2018
771
772
const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src)), one), mulby32767)); /* load 4 floats, clamp, convert to sint32 */
const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src+4)), one), mulby32767)); /* load 4 floats, clamp, convert to sint32 */
Jan 16, 2017
Jan 16, 2017
773
774
775
776
777
778
779
780
_mm_store_si128(mmdst, _mm_xor_si128(_mm_packs_epi32(ints1, ints2), topbit)); /* pack to sint16, xor top bit, store out. */
i -= 8; src += 8; mmdst++;
}
dst = (Uint16 *) mmdst;
}
/* Finish off any leftovers with scalar operations. */
while (i) {
May 15, 2018
May 15, 2018
781
782
783
784
785
786
787
788
const float sample = *src;
if (sample >= 1.0f) {
*dst = 65535;
} else if (sample <= -1.0f) {
*dst = 0;
} else {
*dst = (Uint16)((sample + 1.0f) * 32767.0f);
}
Jan 16, 2017
Jan 16, 2017
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
i--; src++; dst++;
}
cvt->len_cvt /= 2;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_U16SYS);
}
}
static void SDLCALL
SDL_Convert_F32_to_S32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const float *src = (const float *) cvt->buf;
Sint32 *dst = (Sint32 *) cvt->buf;
int i;
LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32 (using SSE2)");
/* Get dst aligned to 16 bytes */
for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
May 15, 2018
May 15, 2018
809
810
811
812
const float sample = *src;
if (sample >= 1.0f) {
*dst = 2147483647;
} else if (sample <= -1.0f) {
May 21, 2018
May 21, 2018
813
*dst = (Sint32) -2147483648LL;
May 15, 2018
May 15, 2018
814
815
816
} else {
*dst = ((Sint32)(sample * 8388607.0f)) << 8;
}
Jan 16, 2017
Jan 16, 2017
817
818
819
820
821
822
823
}
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
SDL_assert(!i || ((((size_t) src) & 15) == 0));
{
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
May 15, 2018
May 15, 2018
824
825
const __m128 one = _mm_set1_ps(1.0f);
const __m128 negone = _mm_set1_ps(-1.0f);
May 15, 2018
May 15, 2018
826
const __m128 mulby8388607 = _mm_set1_ps(8388607.0f);
Jan 16, 2017
Jan 16, 2017
827
828
__m128i *mmdst = (__m128i *) dst;
while (i >= 4) { /* 4 * float32 */
May 15, 2018
May 15, 2018
829
_mm_store_si128(mmdst, _mm_slli_epi32(_mm_cvtps_epi32(_mm_mul_ps(_mm_min_ps(_mm_max_ps(negone, _mm_load_ps(src)), one), mulby8388607)), 8)); /* load 4 floats, clamp, convert to sint32 */
Jan 16, 2017
Jan 16, 2017
830
831
832
833
834
835
836
i -= 4; src += 4; mmdst++;
}
dst = (Sint32 *) mmdst;
}
/* Finish off any leftovers with scalar operations. */
while (i) {
May 15, 2018
May 15, 2018
837
838
839
840
const float sample = *src;
if (sample >= 1.0f) {
*dst = 2147483647;
} else if (sample <= -1.0f) {
May 21, 2018
May 21, 2018
841
*dst = (Sint32) -2147483648LL;
May 15, 2018
May 15, 2018
842
843
844
} else {
*dst = ((Sint32)(sample * 8388607.0f)) << 8;
}
Jan 16, 2017
Jan 16, 2017
845
846
847
848
849
850
851
852
853
854
i--; src++; dst++;
}
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_S32SYS);
}
}
#endif
May 16, 2018
May 16, 2018
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if HAVE_NEON_INTRINSICS
static void SDLCALL
SDL_Convert_S8_to_F32_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const Sint8 *src = ((const Sint8 *) (cvt->buf + cvt->len_cvt)) - 1;
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
int i;
LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32 (using NEON)");
/* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
for (i = cvt->len_cvt; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) {
*dst = ((float) *src) * DIVBY128;
}
src -= 15; dst -= 15; /* adjust to read NEON blocks from the start. */
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do NEON blocks as long as we have 16 bytes available. */
const int8_t *mmsrc = (const int8_t *) src;
const float32x4_t divby128 = vdupq_n_f32(DIVBY128);
while (i >= 16) { /* 16 * 8-bit */
const int8x16_t bytes = vld1q_s8(mmsrc); /* get 16 sint8 into a NEON register. */
const int16x8_t int16hi = vmovl_s8(vget_high_s8(bytes)); /* convert top 8 bytes to 8 int16 */
const int16x8_t int16lo = vmovl_s8(vget_low_s8(bytes)); /* convert bottom 8 bytes to 8 int16 */
/* split int16 to two int32, then convert to float, then multiply to normalize, store. */
vst1q_f32(dst, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(int16hi))), divby128));
vst1q_f32(dst+4, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(int16hi))), divby128));
vst1q_f32(dst+8, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(int16lo))), divby128));
vst1q_f32(dst+12, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(int16lo))), divby128));
i -= 16; mmsrc -= 16; dst -= 16;
}
src = (const Sint8 *) mmsrc;
}
src += 15; dst += 15; /* adjust for any scalar finishing. */
/* Finish off any leftovers with scalar operations. */
while (i) {
*dst = ((float) *src) * DIVBY128;
i--; src--; dst--;
}
cvt->len_cvt *= 4;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
}
}
static void SDLCALL
SDL_Convert_U8_to_F32_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
int i;
LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32 (using NEON)");
/* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
for (i = cvt->len_cvt; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) {
*dst = (((float) *src) * DIVBY128) - 1.0f;
}
src -= 15; dst -= 15; /* adjust to read NEON blocks from the start. */
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do NEON blocks as long as we have 16 bytes available. */
const uint8_t *mmsrc = (const uint8_t *) src;
const float32x4_t divby128 = vdupq_n_f32(DIVBY128);
const float32x4_t one = vdupq_n_f32(1.0f);
while (i >= 16) { /* 16 * 8-bit */
const uint8x16_t bytes = vld1q_u8(mmsrc); /* get 16 uint8 into a NEON register. */
const uint16x8_t uint16hi = vmovl_u8(vget_high_u8(bytes)); /* convert top 8 bytes to 8 uint16 */
const uint16x8_t uint16lo = vmovl_u8(vget_low_u8(bytes)); /* convert bottom 8 bytes to 8 uint16 */
/* split uint16 to two uint32, then convert to float, then multiply to normalize, subtract to adjust for sign, store. */
vst1q_f32(dst, vmlsq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(uint16hi))), divby128, one));
vst1q_f32(dst+4, vmlsq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(uint16hi))), divby128, one));
vst1q_f32(dst+8, vmlsq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(uint16lo))), divby128, one));
vst1q_f32(dst+12, vmlsq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(uint16lo))), divby128, one));
i -= 16; mmsrc -= 16; dst -= 16;
}
src = (const Uint8 *) mmsrc;
}
src += 15; dst += 15; /* adjust for any scalar finishing. */
/* Finish off any leftovers with scalar operations. */
while (i) {
*dst = (((float) *src) * DIVBY128) - 1.0f;
i--; src--; dst--;
}
cvt->len_cvt *= 4;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
}
}
static void SDLCALL
SDL_Convert_S16_to_F32_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const Sint16 *src = ((const Sint16 *) (cvt->buf + cvt->len_cvt)) - 1;
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
int i;
LOG_DEBUG_CONVERT("AUDIO_S16", "AUDIO_F32 (using NEON)");
/* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
for (i = cvt->len_cvt / sizeof (Sint16); i && (((size_t) (dst-7)) & 15); --i, --src, --dst) {
*dst = ((float) *src) * DIVBY32768;
}
src -= 7; dst -= 7; /* adjust to read NEON blocks from the start. */
SDL_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do NEON blocks as long as we have 16 bytes available. */
const float32x4_t divby32768 = vdupq_n_f32(DIVBY32768);
while (i >= 8) { /* 8 * 16-bit */
const int16x8_t ints = vld1q_s16((int16_t const *) src); /* get 8 sint16 into a NEON register. */
/* split int16 to two int32, then convert to float, then multiply to normalize, store. */
vst1q_f32(dst, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(ints))), divby32768));
vst1q_f32(dst+4, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(ints))), divby32768));
i -= 8; src -= 8; dst -= 8;
}
}
src += 7; dst += 7; /* adjust for any scalar finishing. */
/* Finish off any leftovers with scalar operations. */
while (i) {
*dst = ((float) *src) * DIVBY32768;
i--; src--; dst--;
}
cvt->len_cvt *= 2;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
}