SDL: Implemented NEON audio type conversion

From e9e9424b9ab2744b0f7a7812294ebfb037fe1b0e Mon Sep 17 00:00:00 2001
From: Brick <[EMAIL REDACTED]>
Date: Wed, 3 Apr 2024 18:45:34 +0100
Subject: [PATCH] Implemented NEON audio type conversion

Compiler support for loading/storing multiple registers at once (i.e vld1q_f32_x4) seems very poor, so avoiding them for now.

Also switched to aligned stores with SSE. Although both SSE and NEON support unaligned stores, there is more likely to be a penalty to them, i.e when crossing a cache line. So might as align them.
---
 src/audio/SDL_audiotypecvt.c | 840 ++++++++++++-----------------------
 1 file changed, 284 insertions(+), 556 deletions(-)

diff --git a/src/audio/SDL_audiotypecvt.c b/src/audio/SDL_audiotypecvt.c
index 43cabd9b5687b..23ad9d443edbe 100644
--- a/src/audio/SDL_audiotypecvt.c
+++ b/src/audio/SDL_audiotypecvt.c
@@ -22,9 +22,6 @@
 
 #include "SDL_sysaudio.h"
 
-// TODO: NEON is disabled until https://github.com/libsdl-org/SDL/issues/8352 can be fixed
-#undef SDL_NEON_INTRINSICS
-
 #define DIVBY2147483648 0.0000000004656612873077392578125f // 0x1p-31f
 
 // start fallback scalar converters
@@ -186,11 +183,27 @@ static void SDL_Convert_F32_to_S32_Scalar(Sint32 *dst, const float *src, int num
 
 // end fallback scalar converters
 
+// Convert forwards, when sizeof(*src) >= sizeof(*dst)
+#define CONVERT_16_FWD(CVT1, CVT16)                          \
+    int i = 0;                                               \
+    if (num_samples >= 16) {                                 \
+        while ((uintptr_t)(&dst[i]) & 15) { CVT1  ++i;     } \
+        while ((i + 16) <= num_samples)   { CVT16 i += 16; } \
+    }                                                        \
+    while (i < num_samples)               { CVT1  ++i;     }
+
+// Convert backwards, when sizeof(*src) <= sizeof(*dst)
+#define CONVERT_16_REV(CVT1, CVT16)                          \
+    int i = num_samples;                                     \
+    if (i >= 16) {                                           \
+        while ((uintptr_t)(&dst[i]) & 15) { --i;     CVT1  } \
+        while (i >= 16)                   { i -= 16; CVT16 } \
+    }                                                        \
+    while (i > 0)                         { --i;     CVT1  }
+
 #ifdef SDL_SSE2_INTRINSICS
 static void SDL_TARGETING("sse2") SDL_Convert_S8_to_F32_SSE2(float *dst, const Sint8 *src, int num_samples)
 {
-    int i = num_samples;
-
     /* 1) Flip the sign bit to convert from S8 to U8 format
      * 2) Construct a float in the range [65536.0, 65538.0)
      * 3) Shift the float range to [-1.0, 1.0)
@@ -202,35 +215,28 @@ static void SDL_TARGETING("sse2") SDL_Convert_S8_to_F32_SSE2(float *dst, const S
 
     LOG_DEBUG_AUDIO_CONVERT("S8", "F32 (using SSE2)");
 
-    while (i >= 16) {
-        i -= 16;
-
+    CONVERT_16_REV({
+        _mm_store_ss(&dst[i], _mm_add_ss(_mm_castsi128_ps(_mm_cvtsi32_si128((Uint8)src[i] ^ 0x47800080u)), offset));
+    }, {
         const __m128i bytes = _mm_xor_si128(_mm_loadu_si128((const __m128i *)&src[i]), flipper);
 
-        const __m128i shorts1 = _mm_unpacklo_epi8(bytes, zero);
-        const __m128i shorts2 = _mm_unpackhi_epi8(bytes, zero);
-
-        const __m128 floats1 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts1, caster)), offset);
-        const __m128 floats2 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts1, caster)), offset);
-        const __m128 floats3 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts2, caster)), offset);
-        const __m128 floats4 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts2, caster)), offset);
+        const __m128i shorts0 = _mm_unpacklo_epi8(bytes, zero);
+        const __m128i shorts1 = _mm_unpackhi_epi8(bytes, zero);
 
-        _mm_storeu_ps(&dst[i], floats1);
-        _mm_storeu_ps(&dst[i + 4], floats2);
-        _mm_storeu_ps(&dst[i + 8], floats3);
-        _mm_storeu_ps(&dst[i + 12], floats4);
-    }
+        const __m128 floats0 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts0, caster)), offset);
+        const __m128 floats1 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts0, caster)), offset);
+        const __m128 floats2 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts1, caster)), offset);
+        const __m128 floats3 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts1, caster)), offset);
 
-    while (i) {
-        --i;
-        _mm_store_ss(&dst[i], _mm_add_ss(_mm_castsi128_ps(_mm_cvtsi32_si128((Uint8)src[i] ^ 0x47800080u)), offset));
-    }
+        _mm_store_ps(&dst[i], floats0);
+        _mm_store_ps(&dst[i + 4], floats1);
+        _mm_store_ps(&dst[i + 8], floats2);
+        _mm_store_ps(&dst[i + 12], floats3);
+    })
 }
 
 static void SDL_TARGETING("sse2") SDL_Convert_U8_to_F32_SSE2(float *dst, const Uint8 *src, int num_samples)
 {
-    int i = num_samples;
-
     /* 1) Construct a float in the range [65536.0, 65538.0)
      * 2) Shift the float range to [-1.0, 1.0)
      * dst[i] = i2f(src[i] | 0x47800000) - 65537.0 */
@@ -240,35 +246,28 @@ static void SDL_TARGETING("sse2") SDL_Convert_U8_to_F32_SSE2(float *dst, const U
 
     LOG_DEBUG_AUDIO_CONVERT("U8", "F32 (using SSE2)");
 
-    while (i >= 16) {
-        i -= 16;
-
+    CONVERT_16_REV({
+        _mm_store_ss(&dst[i], _mm_add_ss(_mm_castsi128_ps(_mm_cvtsi32_si128((Uint8)src[i] ^ 0x47800000u)), offset));
+    }, {
         const __m128i bytes = _mm_loadu_si128((const __m128i *)&src[i]);
 
-        const __m128i shorts1 = _mm_unpacklo_epi8(bytes, zero);
-        const __m128i shorts2 = _mm_unpackhi_epi8(bytes, zero);
-
-        const __m128 floats1 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts1, caster)), offset);
-        const __m128 floats2 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts1, caster)), offset);
-        const __m128 floats3 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts2, caster)), offset);
-        const __m128 floats4 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts2, caster)), offset);
+        const __m128i shorts0 = _mm_unpacklo_epi8(bytes, zero);
+        const __m128i shorts1 = _mm_unpackhi_epi8(bytes, zero);
 
-        _mm_storeu_ps(&dst[i], floats1);
-        _mm_storeu_ps(&dst[i + 4], floats2);
-        _mm_storeu_ps(&dst[i + 8], floats3);
-        _mm_storeu_ps(&dst[i + 12], floats4);
-    }
+        const __m128 floats0 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts0, caster)), offset);
+        const __m128 floats1 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts0, caster)), offset);
+        const __m128 floats2 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts1, caster)), offset);
+        const __m128 floats3 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts1, caster)), offset);
 
-    while (i) {
-        --i;
-        _mm_store_ss(&dst[i], _mm_add_ss(_mm_castsi128_ps(_mm_cvtsi32_si128((Uint8)src[i] ^ 0x47800000u)), offset));
-    }
+        _mm_store_ps(&dst[i], floats0);
+        _mm_store_ps(&dst[i + 4], floats1);
+        _mm_store_ps(&dst[i + 8], floats2);
+        _mm_store_ps(&dst[i + 12], floats3);
+    })
 }
 
 static void SDL_TARGETING("sse2") SDL_Convert_S16_to_F32_SSE2(float *dst, const Sint16 *src, int num_samples)
 {
-    int i = num_samples;
-
     /* 1) Flip the sign bit to convert from S16 to U16 format
      * 2) Construct a float in the range [256.0, 258.0)
      * 3) Shift the float range to [-1.0, 1.0)
@@ -279,67 +278,53 @@ static void SDL_TARGETING("sse2") SDL_Convert_S16_to_F32_SSE2(float *dst, const
 
     LOG_DEBUG_AUDIO_CONVERT("S16", "F32 (using SSE2)");
 
-    while (i >= 16) {
-        i -= 16;
-
-        const __m128i shorts1 = _mm_xor_si128(_mm_loadu_si128((const __m128i *)&src[i]), flipper);
-        const __m128i shorts2 = _mm_xor_si128(_mm_loadu_si128((const __m128i *)&src[i + 8]), flipper);
-
-        const __m128 floats1 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts1, caster)), offset);
-        const __m128 floats2 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts1, caster)), offset);
-        const __m128 floats3 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts2, caster)), offset);
-        const __m128 floats4 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts2, caster)), offset);
-
-        _mm_storeu_ps(&dst[i], floats1);
-        _mm_storeu_ps(&dst[i + 4], floats2);
-        _mm_storeu_ps(&dst[i + 8], floats3);
-        _mm_storeu_ps(&dst[i + 12], floats4);
-    }
-
-    while (i) {
-        --i;
+    CONVERT_16_REV({
         _mm_store_ss(&dst[i], _mm_add_ss(_mm_castsi128_ps(_mm_cvtsi32_si128((Uint16)src[i] ^ 0x43808000u)), offset));
-    }
+    }, {
+        const __m128i shorts0 = _mm_xor_si128(_mm_loadu_si128((const __m128i *)&src[i]), flipper);
+        const __m128i shorts1 = _mm_xor_si128(_mm_loadu_si128((const __m128i *)&src[i + 8]), flipper);
+
+        const __m128 floats0 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts0, caster)), offset);
+        const __m128 floats1 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts0, caster)), offset);
+        const __m128 floats2 = _mm_add_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(shorts1, caster)), offset);
+        const __m128 floats3 = _mm_add_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(shorts1, caster)), offset);
+
+        _mm_store_ps(&dst[i], floats0);
+        _mm_store_ps(&dst[i + 4], floats1);
+        _mm_store_ps(&dst[i + 8], floats2);
+        _mm_store_ps(&dst[i + 12], floats3);
+    })
 }
 
 static void SDL_TARGETING("sse2") SDL_Convert_S32_to_F32_SSE2(float *dst, const Sint32 *src, int num_samples)
 {
-    int i = num_samples;
-
     // dst[i] = f32(src[i]) / f32(0x80000000)
     const __m128 scaler = _mm_set1_ps(DIVBY2147483648);
 
     LOG_DEBUG_AUDIO_CONVERT("S32", "F32 (using SSE2)");
 
-    while (i >= 16) {
-        i -= 16;
-
-        const __m128i ints1 = _mm_loadu_si128((const __m128i *)&src[i]);
-        const __m128i ints2 = _mm_loadu_si128((const __m128i *)&src[i + 4]);
-        const __m128i ints3 = _mm_loadu_si128((const __m128i *)&src[i + 8]);
-        const __m128i ints4 = _mm_loadu_si128((const __m128i *)&src[i + 12]);
+    CONVERT_16_FWD({
+        _mm_store_ss(&dst[i], _mm_mul_ss(_mm_cvt_si2ss(_mm_setzero_ps(), src[i]), scaler));
+    }, {
+        const __m128i ints0 = _mm_loadu_si128((const __m128i *)&src[i]);
+        const __m128i ints1 = _mm_loadu_si128((const __m128i *)&src[i + 4]);
+        const __m128i ints2 = _mm_loadu_si128((const __m128i *)&src[i + 8]);
+        const __m128i ints3 = _mm_loadu_si128((const __m128i *)&src[i + 12]);
 
+        const __m128 floats0 = _mm_mul_ps(_mm_cvtepi32_ps(ints0), scaler);
         const __m128 floats1 = _mm_mul_ps(_mm_cvtepi32_ps(ints1), scaler);
         const __m128 floats2 = _mm_mul_ps(_mm_cvtepi32_ps(ints2), scaler);
         const __m128 floats3 = _mm_mul_ps(_mm_cvtepi32_ps(ints3), scaler);
-        const __m128 floats4 = _mm_mul_ps(_mm_cvtepi32_ps(ints4), scaler);
-
-        _mm_storeu_ps(&dst[i], floats1);
-        _mm_storeu_ps(&dst[i + 4], floats2);
-        _mm_storeu_ps(&dst[i + 8], floats3);
-        _mm_storeu_ps(&dst[i + 12], floats4);
-    }
 
-    while (i) {
-        --i;
-        _mm_store_ss(&dst[i], _mm_mul_ss(_mm_cvt_si2ss(_mm_setzero_ps(), src[i]), scaler));
-    }
+        _mm_store_ps(&dst[i], floats0);
+        _mm_store_ps(&dst[i + 4], floats1);
+        _mm_store_ps(&dst[i + 8], floats2);
+        _mm_store_ps(&dst[i + 12], floats3);
+    })
 }
 
 static void SDL_TARGETING("sse2") SDL_Convert_F32_to_S8_SSE2(Sint8 *dst, const float *src, int num_samples)
 {
-    int i = num_samples;
-
     /* 1) Shift the float range from [-1.0, 1.0] to [98303.0, 98305.0]
      * 2) Extract the lowest 16 bits and clamp to [-128, 127]
      * Overflow is correctly handled for inputs between roughly [-255.0, 255.0]
@@ -349,43 +334,31 @@ static void SDL_TARGETING("sse2") SDL_Convert_F32_to_S8_SSE2(Sint8 *dst, const f
 
     LOG_DEBUG_AUDIO_CONVERT("F32", "S8 (using SSE2)");
 
-    while (i >= 16) {
-        const __m128 floats1 = _mm_loadu_ps(&src[0]);
-        const __m128 floats2 = _mm_loadu_ps(&src[4]);
-        const __m128 floats3 = _mm_loadu_ps(&src[8]);
-        const __m128 floats4 = _mm_loadu_ps(&src[12]);
+    CONVERT_16_FWD({
+        const __m128i ints = _mm_castps_si128(_mm_add_ss(_mm_load_ss(&src[i]), offset));
+        dst[i] = (Sint8)(_mm_cvtsi128_si32(_mm_packs_epi16(ints, ints)) & 0xFF);
+    }, {
+        const __m128 floats0 = _mm_loadu_ps(&src[i]);
+        const __m128 floats1 = _mm_loadu_ps(&src[i + 4]);
+        const __m128 floats2 = _mm_loadu_ps(&src[i + 8]);
+        const __m128 floats3 = _mm_loadu_ps(&src[i + 12]);
 
+        const __m128i ints0 = _mm_castps_si128(_mm_add_ps(floats0, offset));
         const __m128i ints1 = _mm_castps_si128(_mm_add_ps(floats1, offset));
         const __m128i ints2 = _mm_castps_si128(_mm_add_ps(floats2, offset));
         const __m128i ints3 = _mm_castps_si128(_mm_add_ps(floats3, offset));
-        const __m128i ints4 = _mm_castps_si128(_mm_add_ps(floats4, offset));
-
-        const __m128i shorts1 = _mm_and_si128(_mm_packs_epi16(ints1, ints2), mask);
-        const __m128i shorts2 = _mm_and_si128(_mm_packs_epi16(ints3, ints4), mask);
 
-        const __m128i bytes = _mm_packus_epi16(shorts1, shorts2);
-
-        _mm_storeu_si128((__m128i*)dst, bytes);
-
-        i -= 16;
-        src += 16;
-        dst += 16;
-    }
+        const __m128i shorts0 = _mm_and_si128(_mm_packs_epi16(ints0, ints1), mask);
+        const __m128i shorts1 = _mm_and_si128(_mm_packs_epi16(ints2, ints3), mask);
 
-    while (i) {
-        const __m128i ints = _mm_castps_si128(_mm_add_ss(_mm_load_ss(src), offset));
-        *dst = (Sint8)(_mm_cvtsi128_si32(_mm_packs_epi16(ints, ints)) & 0xFF);
+        const __m128i bytes = _mm_packus_epi16(shorts0, shorts1);
 
-        --i;
-        ++src;
-        ++dst;
-    }
+        _mm_store_si128((__m128i*)&dst[i], bytes);
+    })
 }
 
 static void SDL_TARGETING("sse2") SDL_Convert_F32_to_U8_SSE2(Uint8 *dst, const float *src, int num_samples)
 {
-    int i = num_samples;
-
     /* 1) Shift the float range from [-1.0, 1.0] to [98304.0, 98306.0]
      * 2) Extract the lowest 16 bits and clamp to [0, 255]
      * Overflow is correctly handled for inputs between roughly [-254.0, 254.0]
@@ -395,43 +368,31 @@ static void SDL_TARGETING("sse2") SDL_Convert_F32_to_U8_SSE2(Uint8 *dst, const f
 
     LOG_DEBUG_AUDIO_CONVERT("F32", "U8 (using SSE2)");
 
-    while (i >= 16) {
-        const __m128 floats1 = _mm_loadu_ps(&src[0]);
-        const __m128 floats2 = _mm_loadu_ps(&src[4]);
-        const __m128 floats3 = _mm_loadu_ps(&src[8]);
-        const __m128 floats4 = _mm_loadu_ps(&src[12]);
+    CONVERT_16_FWD({
+        const __m128i ints = _mm_castps_si128(_mm_add_ss(_mm_load_ss(&src[i]), offset));
+        dst[i] = (Uint8)(_mm_cvtsi128_si32(_mm_packus_epi16(ints, ints)) & 0xFF);
+    }, {
+        const __m128 floats0 = _mm_loadu_ps(&src[i]);
+        const __m128 floats1 = _mm_loadu_ps(&src[i + 4]);
+        const __m128 floats2 = _mm_loadu_ps(&src[i + 8]);
+        const __m128 floats3 = _mm_loadu_ps(&src[i + 12]);
 
+        const __m128i ints0 = _mm_castps_si128(_mm_add_ps(floats0, offset));
         const __m128i ints1 = _mm_castps_si128(_mm_add_ps(floats1, offset));
         const __m128i ints2 = _mm_castps_si128(_mm_add_ps(floats2, offset));
         const __m128i ints3 = _mm_castps_si128(_mm_add_ps(floats3, offset));
-        const __m128i ints4 = _mm_castps_si128(_mm_add_ps(floats4, offset));
 
-        const __m128i shorts1 = _mm_and_si128(_mm_packus_epi16(ints1, ints2), mask);
-        const __m128i shorts2 = _mm_and_si128(_mm_packus_epi16(ints3, ints4), mask);
+        const __m128i shorts0 = _mm_and_si128(_mm_packus_epi16(ints0, ints1), mask);
+        const __m128i shorts1 = _mm_and_si128(_mm_packus_epi16(ints2, ints3), mask);
 
-        const __m128i bytes = _mm_packus_epi16(shorts1, shorts2);
+        const __m128i bytes = _mm_packus_epi16(shorts0, shorts1);
 
-        _mm_storeu_si128((__m128i*)dst, bytes);
-
-        i -= 16;
-        src += 16;
-        dst += 16;
-    }
-
-    while (i) {
-        const __m128i ints = _mm_castps_si128(_mm_add_ss(_mm_load_ss(src), offset));
-        *dst = (Uint8)(_mm_cvtsi128_si32(_mm_packus_epi16(ints, ints)) & 0xFF);
-
-        --i;
-        ++src;
-        ++dst;
-    }
+        _mm_store_si128((__m128i*)&dst[i], bytes);
+    })
 }
 
 static void SDL_TARGETING("sse2") SDL_Convert_F32_to_S16_SSE2(Sint16 *dst, const float *src, int num_samples)
 {
-    int i = num_samples;
-
     /* 1) Shift the float range from [-1.0, 1.0] to [256.0, 258.0]
      * 2) Shift the int range from [0x43800000, 0x43810000] to [-32768,32768]
      * 3) Clamp to range [-32768,32767]
@@ -441,42 +402,30 @@ static void SDL_TARGETING("sse2") SDL_Convert_F32_to_S16_SSE2(Sint16 *dst, const
 
     LOG_DEBUG_AUDIO_CONVERT("F32", "S16 (using SSE2)");
 
-    while (i >= 16) {
-        const __m128 floats1 = _mm_loadu_ps(&src[0]);
-        const __m128 floats2 = _mm_loadu_ps(&src[4]);
-        const __m128 floats3 = _mm_loadu_ps(&src[8]);
-        const __m128 floats4 = _mm_loadu_ps(&src[12]);
+    CONVERT_16_FWD({
+        const __m128i ints = _mm_sub_epi32(_mm_castps_si128(_mm_add_ss(_mm_load_ss(&src[i]), offset)), _mm_castps_si128(offset));
+        dst[i] = (Sint16)(_mm_cvtsi128_si32(_mm_packs_epi32(ints, ints)) & 0xFFFF);
+    }, {
+        const __m128 floats0 = _mm_loadu_ps(&src[i]);
+        const __m128 floats1 = _mm_loadu_ps(&src[i + 4]);
+        const __m128 floats2 = _mm_loadu_ps(&src[i + 8]);
+        const __m128 floats3 = _mm_loadu_ps(&src[i + 12]);
 
+        const __m128i ints0 = _mm_sub_epi32(_mm_castps_si128(_mm_add_ps(floats0, offset)), _mm_castps_si128(offset));
         const __m128i ints1 = _mm_sub_epi32(_mm_castps_si128(_mm_add_ps(floats1, offset)), _mm_castps_si128(offset));
         const __m128i ints2 = _mm_sub_epi32(_mm_castps_si128(_mm_add_ps(floats2, offset)), _mm_castps_si128(offset));
         const __m128i ints3 = _mm_sub_epi32(_mm_castps_si128(_mm_add_ps(floats3, offset)), _mm_castps_si128(offset));
-        const __m128i ints4 = _mm_sub_epi32(_mm_castps_si128(_mm_add_ps(floats4, offset)), _mm_castps_si128(offset));
-
-        const __m128i shorts1 = _mm_packs_epi32(ints1, ints2);
-        const __m128i shorts2 = _mm_packs_epi32(ints3, ints4);
 
-        _mm_storeu_si128((__m128i*)&dst[0], shorts1);
-        _mm_storeu_si128((__m128i*)&dst[8], shorts2);
-
-        i -= 16;
-        src += 16;
-        dst += 16;
-    }
+        const __m128i shorts0 = _mm_packs_epi32(ints0, ints1);
+        const __m128i shorts1 = _mm_packs_epi32(ints2, ints3);
 
-    while (i) {
-        const __m128i ints = _mm_sub_epi32(_mm_castps_si128(_mm_add_ss(_mm_load_ss(src), offset)), _mm_castps_si128(offset));
-        *dst = (Sint16)(_mm_cvtsi128_si32(_mm_packs_epi32(ints, ints)) & 0xFFFF);
-
-        --i;
-        ++src;
-        ++dst;
-    }
+        _mm_store_si128((__m128i*)&dst[i], shorts0);
+        _mm_store_si128((__m128i*)&dst[i + 8], shorts1);
+    })
 }
 
 static void SDL_TARGETING("sse2") SDL_Convert_F32_to_S32_SSE2(Sint32 *dst, const float *src, int num_samples)
 {
-    int i = num_samples;
-
     /* 1) Scale the float range from [-1.0, 1.0] to [-2147483648.0, 2147483648.0]
      * 2) Convert to integer (values too small/large become 0x80000000 = -2147483648)
      * 3) Fixup values which were too large (0x80000000 ^ 0xFFFFFFFF = 2147483647)
@@ -485,458 +434,237 @@ static void SDL_TARGETING("sse2") SDL_Convert_F32_to_S32_SSE2(Sint32 *dst, const
 
     LOG_DEBUG_AUDIO_CONVERT("F32", "S32 (using SSE2)");
 
-    while (i >= 16) {
-        const __m128 floats1 = _mm_loadu_ps(&src[0]);
-        const __m128 floats2 = _mm_loadu_ps(&src[4]);
-        const __m128 floats3 = _mm_loadu_ps(&src[8]);
-        const __m128 floats4 = _mm_loadu_ps(&src[12]);
-
-        const __m128 values1 = _mm_mul_ps(floats1, limit);
-        const __m128 values2 = _mm_mul_ps(floats2, limit);
-        const __m128 values3 = _mm_mul_ps(floats3, limit);
-        const __m128 values4 = _mm_mul_ps(floats4, limit);
-
-        const __m128i ints1 = _mm_xor_si128(_mm_cvttps_epi32(values1), _mm_castps_si128(_mm_cmpge_ps(values1, limit)));
-        const __m128i ints2 = _mm_xor_si128(_mm_cvttps_epi32(values2), _mm_castps_si128(_mm_cmpge_ps(values2, limit)));
-        const __m128i ints3 = _mm_xor_si128(_mm_cvttps_epi32(values3), _mm_castps_si128(_mm_cmpge_ps(values3, limit)));
-        const __m128i ints4 = _mm_xor_si128(_mm_cvttps_epi32(values4), _mm_castps_si128(_mm_cmpge_ps(values4, limit)));
-
-        _mm_storeu_si128((__m128i*)&dst[0], ints1);
-        _mm_storeu_si128((__m128i*)&dst[4], ints2);
-        _mm_storeu_si128((__m128i*)&dst[8], ints3);
-        _mm_storeu_si128((__m128i*)&dst[12], ints4);
-
-        i -= 16;
-        src += 16;
-        dst += 16;
-    }
-
-    while (i) {
-        const __m128 floats = _mm_load_ss(src);
+    CONVERT_16_FWD({
+        const __m128 floats = _mm_load_ss(&src[i]);
         const __m128 values = _mm_mul_ss(floats, limit);
         const __m128i ints = _mm_xor_si128(_mm_cvttps_epi32(values), _mm_castps_si128(_mm_cmpge_ss(values, limit)));
-        *dst = (Sint32)_mm_cvtsi128_si32(ints);
-
-        --i;
-        ++src;
-        ++dst;
-    }
+        dst[i] = (Sint32)_mm_cvtsi128_si32(ints);
+    }, {
+        const __m128 floats0 = _mm_loadu_ps(&src[i]);
+        const __m128 floats1 = _mm_loadu_ps(&src[i + 4]);
+        const __m128 floats2 = _mm_loadu_ps(&src[i + 8]);
+        const __m128 floats3 = _mm_loadu_ps(&src[i + 12]);
+
+        const __m128 values1 = _mm_mul_ps(floats0, limit);
+        const __m128 values2 = _mm_mul_ps(floats1, limit);
+        const __m128 values3 = _mm_mul_ps(floats2, limit);
+        const __m128 values4 = _mm_mul_ps(floats3, limit);
+
+        const __m128i ints0 = _mm_xor_si128(_mm_cvttps_epi32(values1), _mm_castps_si128(_mm_cmpge_ps(values1, limit)));
+        const __m128i ints1 = _mm_xor_si128(_mm_cvttps_epi32(values2), _mm_castps_si128(_mm_cmpge_ps(values2, limit)));
+        const __m128i ints2 = _mm_xor_si128(_mm_cvttps_epi32(values3), _mm_castps_si128(_mm_cmpge_ps(values3, limit)));
+        const __m128i ints3 = _mm_xor_si128(_mm_cvttps_epi32(values4), _mm_castps_si128(_mm_cmpge_ps(values4, limit)));
+
+        _mm_store_si128((__m128i*)&dst[i], ints0);
+        _mm_store_si128((__m128i*)&dst[i + 4], ints1);
+        _mm_store_si128((__m128i*)&dst[i + 8], ints2);
+        _mm_store_si128((__m128i*)&dst[i + 12], ints3);
+    })
 }
 #endif
 
 #ifdef SDL_NEON_INTRINSICS
-#define DIVBY128     0.0078125f // 0x1p-7f
-#define DIVBY32768   0.000030517578125f // 0x1p-15f
-#define DIVBY8388607 0.00000011920930376163766f // 0x1.000002p-23f
-
 static void SDL_Convert_S8_to_F32_NEON(float *dst, const Sint8 *src, int num_samples)
 {
-    int i;
-
     LOG_DEBUG_AUDIO_CONVERT("S8", "F32 (using NEON)");
 
-    src += num_samples - 1;
-    dst += num_samples - 1;
-
-    // Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src)
-    for (i = num_samples; i && (((size_t)(dst - 15)) & 15); --i, --src, --dst) {
-        *dst = ((float)*src) * DIVBY128;
-    }
+    CONVERT_16_REV({
+        vst1_lane_f32(&dst[i], vcvt_n_f32_s32(vdup_n_s32(src[i]), 7), 0);
+    }, {
+        int8x16_t bytes = vld1q_s8(&src[i]);
 
-    src -= 15;
-    dst -= 15; // adjust to read NEON blocks from the start.
-    SDL_assert(!i || !(((size_t)dst) & 15));
-
-    // Make sure src is aligned too.
-    if (!(((size_t)src) & 15)) {
-        // 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_low_s16(int16lo))), divby128));
-            vst1q_f32(dst + 4, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(int16lo))), divby128));
-            vst1q_f32(dst + 8, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(int16hi))), divby128));
-            vst1q_f32(dst + 12, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(int16hi))), divby128));
-            i -= 16;
-            mmsrc -= 16;
-            dst -= 16;
-        }
-
-        src = (const Sint8 *)mmsrc;
-    }
+        int16x8_t shorts0 = vmovl_s8(vget_low_s8(bytes));
+        int16x8_t shorts1 = vmovl_s8(vget_high_s8(bytes));
 
-    src += 15;
-    dst += 15; // adjust for any scalar finishing.
+        float32x4_t floats0 = vcvtq_n_f32_s32(vmovl_s16(vget_low_s16(shorts0)), 7);
+        float32x4_t floats1 = vcvtq_n_f32_s32(vmovl_s16(vget_high_s16(shorts0)), 7);
+        float32x4_t floats2 = vcvtq_n_f32_s32(vmovl_s16(vget_low_s16(shorts1)), 7);
+        float32x4_t floats3 = vcvtq_n_f32_s32(vmovl_s16(vget_high_s16(shorts1)), 7);
 
-    // Finish off any leftovers with scalar operations.
-    while (i) {
-        *dst = ((float)*src) * DIVBY128;
-        i--;
-        src--;
-        dst--;
-    }
+        vst1q_f32(&dst[i], floats0);
+        vst1q_f32(&dst[i + 4], floats1);
+        vst1q_f32(&dst[i + 8], floats2);
+        vst1q_f32(&dst[i + 12], floats3);
+    })
 }
 
 static void SDL_Convert_U8_to_F32_NEON(float *dst, const Uint8 *src, int num_samples)
 {
-    int i;
-
     LOG_DEBUG_AUDIO_CONVERT("U8", "F32 (using NEON)");
 
-    src += num_samples - 1;
-    dst += num_samples - 1;
+    uint8x16_t flipper = vdupq_n_u8(0x80);
 
-    // Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src)
-    for (i = num_samples; i && (((size_t)(dst - 15)) & 15); --i, --src, --dst) {
-        *dst = (((float)*src) * DIVBY128) - 1.0f;
-    }
+    CONVERT_16_REV({
+        vst1_lane_f32(&dst[i], vcvt_n_f32_s32(vdup_n_s32((Sint8)(src[i] ^ 0x80)), 7), 0);
+    }, {
+        int8x16_t bytes = vreinterpretq_s8_u8(veorq_u8(vld1q_u8(&src[i]), flipper));
 
-    src -= 15;
-    dst -= 15; // adjust to read NEON blocks from the start.
-    SDL_assert(!i || !(((size_t)dst) & 15));
-
-    // Make sure src is aligned too.
-    if (!(((size_t)src) & 15)) {
-        // 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 negone = 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, vmlaq_f32(negone, vcvtq_f32_u32(vmovl_u16(vget_low_u16(uint16lo))), divby128));
-            vst1q_f32(dst + 4, vmlaq_f32(negone, vcvtq_f32_u32(vmovl_u16(vget_high_u16(uint16lo))), divby128));
-            vst1q_f32(dst + 8, vmlaq_f32(negone, vcvtq_f32_u32(vmovl_u16(vget_low_u16(uint16hi))), divby128));
-            vst1q_f32(dst + 12, vmlaq_f32(negone, vcvtq_f32_u32(vmovl_u16(vget_high_u16(uint16hi))), divby128));
-            i -= 16;
-            mmsrc -= 16;
-            dst -= 16;
-        }
-
-        src = (const Uint8 *)mmsrc;
-    }
+        int16x8_t shorts0 = vmovl_s8(vget_low_s8(bytes));
+        int16x8_t shorts1 = vmovl_s8(vget_high_s8(bytes));
 
-    src += 15;
-    dst += 15; // adjust for any scalar finishing.
+        float32x4_t floats0 = vcvtq_n_f32_s32(vmovl_s16(vget_low_s16(shorts0)), 7);
+        float32x4_t floats1 = vcvtq_n_f32_s32(vmovl_s16(vget_high_s16(shorts0)), 7);
+        float32x4_t floats2 = vcvtq_n_f32_s32(vmovl_s16(vget_low_s16(shorts1)), 7);
+        float32x4_t floats3 = vcvtq_n_f32_s32(vmovl_s16(vget_high_s16(shorts1)), 7);
 
-    // Finish off any leftovers with scalar operations.
-    while (i) {
-        *dst = (((float)*src) * DIVBY128) - 1.0f;
-        i--;
-        src--;
-        dst--;
-    }
+        vst1q_f32(&dst[i], floats0);
+        vst1q_f32(&dst[i + 4], floats1);
+        vst1q_f32(&dst[i + 8], floats2);
+        vst1q_f32(&dst[i + 12], floats3);
+    })
 }
 
 static void SDL_Convert_S16_to_F32_NEON(float *dst, const Sint16 *src, int num_samples)
 {
-    int i;
-
     LOG_DEBUG_AUDIO_CONVERT("S16", "F32 (using NEON)");
 
-    src += num_samples - 1;
-    dst += num_samples - 1;
-
-    // Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src)
-    for (i = num_samples; 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));
-
-    // Make sure src is aligned too.
-    if (!(((size_t)src) & 15)) {
-        // 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--;
-    }
+    CONVERT_16_REV({
+        vst1_lane_f32(&dst[i], vcvt_n_f32_s32(vdup_n_s32(src[i]), 15), 0);
+    }, {
+        int16x8_t shorts0 = vld1q_s16(&src[i]);
+        int16x8_t shorts1 = vld1q_s16(&src[i + 8]);
+
+        float32x4_t floats0 = vcvtq_n_f32_s32(vmovl_s16(vget_low_s16(shorts0)), 15);
+        float32x4_t floats1 = vcvtq_n_f32_s32(vmovl_s16(vget_high_s16(shorts0)), 15);
+        float32x4_t floats2 = vcvtq_n_f32_s32(vmovl_s16(vget_low_s16(shorts1)), 15);
+        float32x4_t floats3 = vcvtq_n_f32_s32(vmovl_s16(vget_high_s16(shorts1)), 15);
+
+        vst1q_f32(&dst[i], floats0);
+        vst1q_f32(&dst[i + 4], floats1);
+        vst1q_f32(&dst[i + 8], floats2);
+        vst1q_f32(&dst[i + 12], floats3);
+    })
 }
 
 static void SDL_Convert_S32_to_F32_NEON(float *dst, const Sint32 *src, int num_samples)
 {
-    int i;
-
     LOG_DEBUG_AUDIO_CONVERT("S32", "F32 (using NEON)");
 
-    // Get dst aligned to 16 bytes
-    for (i = num_samples; i && (((size_t)dst) & 15); --i, ++src, ++dst) {
-        *dst = ((float)(*src >> 8)) * DIVBY8388607;
-    }
-
-    SDL_assert(!i || !(((size_t)dst) & 15));
-
-    // Make sure src is aligned too.
-    if (!(((s

(Patch may be truncated, please check the link at the top of this post.)