src/audio/SDL_audiotypecvt.c
changeset 11992 08c415f14810
parent 11991 2a487acdb306
child 11993 fdf104726ced
     1.1 --- a/src/audio/SDL_audiotypecvt.c	Tue May 15 02:29:35 2018 -0400
     1.2 +++ b/src/audio/SDL_audiotypecvt.c	Wed May 16 02:03:06 2018 -0400
     1.3 @@ -25,8 +25,9 @@
     1.4  #include "SDL_cpuinfo.h"
     1.5  #include "SDL_assert.h"
     1.6  
     1.7 -/* !!! FIXME: write NEON code. */
     1.8 -#define HAVE_NEON_INTRINSICS 0
     1.9 +#ifdef __ARM_NEON__
    1.10 +#define HAVE_NEON_INTRINSICS 1
    1.11 +#endif
    1.12  
    1.13  #ifdef __SSE2__
    1.14  #define HAVE_SSE2_INTRINSICS 1
    1.15 @@ -850,6 +851,538 @@
    1.16  #endif
    1.17  
    1.18  
    1.19 +#if HAVE_NEON_INTRINSICS
    1.20 +static void SDLCALL
    1.21 +SDL_Convert_S8_to_F32_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
    1.22 +{
    1.23 +    const Sint8 *src = ((const Sint8 *) (cvt->buf + cvt->len_cvt)) - 1;
    1.24 +    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
    1.25 +    int i;
    1.26 +
    1.27 +    LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32 (using NEON)");
    1.28 +
    1.29 +    /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
    1.30 +    for (i = cvt->len_cvt; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) {
    1.31 +        *dst = ((float) *src) * DIVBY128;
    1.32 +    }
    1.33 +
    1.34 +    src -= 15; dst -= 15;  /* adjust to read NEON blocks from the start. */
    1.35 +    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
    1.36 +
    1.37 +    /* Make sure src is aligned too. */
    1.38 +    if ((((size_t) src) & 15) == 0) {
    1.39 +        /* Aligned! Do NEON blocks as long as we have 16 bytes available. */
    1.40 +        const int8_t *mmsrc = (const int8_t *) src;
    1.41 +        const float32x4_t divby128 = vdupq_n_f32(DIVBY128);
    1.42 +        while (i >= 16) {   /* 16 * 8-bit */
    1.43 +            const int8x16_t bytes = vld1q_s8(mmsrc);  /* get 16 sint8 into a NEON register. */
    1.44 +            const int16x8_t int16hi = vmovl_s8(vget_high_s8(bytes));  /* convert top 8 bytes to 8 int16 */
    1.45 +            const int16x8_t int16lo = vmovl_s8(vget_low_s8(bytes));   /* convert bottom 8 bytes to 8 int16 */
    1.46 +            /* split int16 to two int32, then convert to float, then multiply to normalize, store. */
    1.47 +            vst1q_f32(dst, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(int16hi))), divby128));
    1.48 +            vst1q_f32(dst+4, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(int16hi))), divby128));
    1.49 +            vst1q_f32(dst+8, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(int16lo))), divby128));
    1.50 +            vst1q_f32(dst+12, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(int16lo))), divby128));
    1.51 +            i -= 16; mmsrc -= 16; dst -= 16;
    1.52 +        }
    1.53 +
    1.54 +        src = (const Sint8 *) mmsrc;
    1.55 +    }
    1.56 +
    1.57 +    src += 15; dst += 15;  /* adjust for any scalar finishing. */
    1.58 +
    1.59 +    /* Finish off any leftovers with scalar operations. */
    1.60 +    while (i) {
    1.61 +        *dst = ((float) *src) * DIVBY128;
    1.62 +        i--; src--; dst--;
    1.63 +    }
    1.64 +
    1.65 +    cvt->len_cvt *= 4;
    1.66 +    if (cvt->filters[++cvt->filter_index]) {
    1.67 +        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
    1.68 +    }
    1.69 +}
    1.70 +
    1.71 +static void SDLCALL
    1.72 +SDL_Convert_U8_to_F32_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
    1.73 +{
    1.74 +    const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
    1.75 +    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
    1.76 +    int i;
    1.77 +
    1.78 +    LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32 (using NEON)");
    1.79 +
    1.80 +    /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
    1.81 +    for (i = cvt->len_cvt; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) {
    1.82 +        *dst = (((float) *src) * DIVBY128) - 1.0f;
    1.83 +    }
    1.84 +
    1.85 +    src -= 15; dst -= 15;  /* adjust to read NEON blocks from the start. */
    1.86 +    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
    1.87 +
    1.88 +    /* Make sure src is aligned too. */
    1.89 +    if ((((size_t) src) & 15) == 0) {
    1.90 +        /* Aligned! Do NEON blocks as long as we have 16 bytes available. */
    1.91 +        const uint8_t *mmsrc = (const uint8_t *) src;
    1.92 +        const float32x4_t divby128 = vdupq_n_f32(DIVBY128);
    1.93 +        const float32x4_t one = vdupq_n_f32(1.0f);
    1.94 +        while (i >= 16) {   /* 16 * 8-bit */
    1.95 +            const uint8x16_t bytes = vld1q_u8(mmsrc);  /* get 16 uint8 into a NEON register. */
    1.96 +            const uint16x8_t uint16hi = vmovl_u8(vget_high_u8(bytes));  /* convert top 8 bytes to 8 uint16 */
    1.97 +            const uint16x8_t uint16lo = vmovl_u8(vget_low_u8(bytes));   /* convert bottom 8 bytes to 8 uint16 */
    1.98 +            /* split uint16 to two uint32, then convert to float, then multiply to normalize, subtract to adjust for sign, store. */
    1.99 +            vst1q_f32(dst, vmlsq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(uint16hi))), divby128, one));
   1.100 +            vst1q_f32(dst+4, vmlsq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(uint16hi))), divby128, one));
   1.101 +            vst1q_f32(dst+8, vmlsq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(uint16lo))), divby128, one));
   1.102 +            vst1q_f32(dst+12, vmlsq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(uint16lo))), divby128, one));
   1.103 +            i -= 16; mmsrc -= 16; dst -= 16;
   1.104 +        }
   1.105 +
   1.106 +        src = (const Uint8 *) mmsrc;
   1.107 +    }
   1.108 +
   1.109 +    src += 15; dst += 15;  /* adjust for any scalar finishing. */
   1.110 +
   1.111 +    /* Finish off any leftovers with scalar operations. */
   1.112 +    while (i) {
   1.113 +        *dst = (((float) *src) * DIVBY128) - 1.0f;
   1.114 +        i--; src--; dst--;
   1.115 +    }
   1.116 +
   1.117 +    cvt->len_cvt *= 4;
   1.118 +    if (cvt->filters[++cvt->filter_index]) {
   1.119 +        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   1.120 +    }
   1.121 +}
   1.122 +
   1.123 +static void SDLCALL
   1.124 +SDL_Convert_S16_to_F32_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   1.125 +{
   1.126 +    const Sint16 *src = ((const Sint16 *) (cvt->buf + cvt->len_cvt)) - 1;
   1.127 +    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
   1.128 +    int i;
   1.129 +
   1.130 +    LOG_DEBUG_CONVERT("AUDIO_S16", "AUDIO_F32 (using NEON)");
   1.131 +
   1.132 +    /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
   1.133 +    for (i = cvt->len_cvt / sizeof (Sint16); i && (((size_t) (dst-7)) & 15); --i, --src, --dst) {
   1.134 +        *dst = ((float) *src) * DIVBY32768;
   1.135 +    }
   1.136 +
   1.137 +    src -= 7; dst -= 7;  /* adjust to read NEON blocks from the start. */
   1.138 +    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
   1.139 +
   1.140 +    /* Make sure src is aligned too. */
   1.141 +    if ((((size_t) src) & 15) == 0) {
   1.142 +        /* Aligned! Do NEON blocks as long as we have 16 bytes available. */
   1.143 +        const float32x4_t divby32768 = vdupq_n_f32(DIVBY32768);
   1.144 +        while (i >= 8) {   /* 8 * 16-bit */
   1.145 +            const int16x8_t ints = vld1q_s16((int16_t const *) src);  /* get 8 sint16 into a NEON register. */
   1.146 +            /* split int16 to two int32, then convert to float, then multiply to normalize, store. */
   1.147 +            vst1q_f32(dst, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(ints))), divby32768));
   1.148 +            vst1q_f32(dst+4, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(ints))), divby32768));
   1.149 +            i -= 8; src -= 8; dst -= 8;
   1.150 +        }
   1.151 +    }
   1.152 +
   1.153 +    src += 7; dst += 7;  /* adjust for any scalar finishing. */
   1.154 +
   1.155 +    /* Finish off any leftovers with scalar operations. */
   1.156 +    while (i) {
   1.157 +        *dst = ((float) *src) * DIVBY32768;
   1.158 +        i--; src--; dst--;
   1.159 +    }
   1.160 +
   1.161 +    cvt->len_cvt *= 2;
   1.162 +    if (cvt->filters[++cvt->filter_index]) {
   1.163 +        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   1.164 +    }
   1.165 +}
   1.166 +
   1.167 +static void SDLCALL
   1.168 +SDL_Convert_U16_to_F32_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   1.169 +{
   1.170 +    const Uint16 *src = ((const Uint16 *) (cvt->buf + cvt->len_cvt)) - 1;
   1.171 +    float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
   1.172 +    int i;
   1.173 +
   1.174 +    LOG_DEBUG_CONVERT("AUDIO_U16", "AUDIO_F32 (using NEON)");
   1.175 +
   1.176 +    /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
   1.177 +    for (i = cvt->len_cvt / sizeof (Sint16); i && (((size_t) (dst-7)) & 15); --i, --src, --dst) {
   1.178 +        *dst = (((float) *src) * DIVBY32768) - 1.0f;
   1.179 +    }
   1.180 +
   1.181 +    src -= 7; dst -= 7;  /* adjust to read NEON blocks from the start. */
   1.182 +    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
   1.183 +
   1.184 +    /* Make sure src is aligned too. */
   1.185 +    if ((((size_t) src) & 15) == 0) {
   1.186 +        /* Aligned! Do NEON blocks as long as we have 16 bytes available. */
   1.187 +        const float32x4_t divby32768 = vdupq_n_f32(DIVBY32768);
   1.188 +        const float32x4_t one = vdupq_n_f32(1.0f);
   1.189 +        while (i >= 8) {   /* 8 * 16-bit */
   1.190 +            const uint16x8_t uints = vld1q_u16((uint16_t const *) src);  /* get 8 uint16 into a NEON register. */
   1.191 +            /* split uint16 to two int32, then convert to float, then multiply to normalize, subtract for sign, store. */
   1.192 +            vst1q_f32(dst, vmlsq_f32(one, vcvtq_f32_u32(vmovl_u16(vget_low_u16(uints))), divby32768));
   1.193 +            vst1q_f32(dst+4, vmlsq_f32(one, vcvtq_f32_u32(vmovl_u16(vget_high_u16(uints))), divby32768));
   1.194 +            i -= 8; src -= 8; dst -= 8;
   1.195 +        }
   1.196 +    }
   1.197 +
   1.198 +    src += 7; dst += 7;  /* adjust for any scalar finishing. */
   1.199 +
   1.200 +    /* Finish off any leftovers with scalar operations. */
   1.201 +    while (i) {
   1.202 +        *dst = (((float) *src) * DIVBY32768) - 1.0f;
   1.203 +        i--; src--; dst--;
   1.204 +    }
   1.205 +
   1.206 +    cvt->len_cvt *= 2;
   1.207 +    if (cvt->filters[++cvt->filter_index]) {
   1.208 +        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   1.209 +    }
   1.210 +}
   1.211 +
   1.212 +static void SDLCALL
   1.213 +SDL_Convert_S32_to_F32_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   1.214 +{
   1.215 +    const Sint32 *src = (const Sint32 *) cvt->buf;
   1.216 +    float *dst = (float *) cvt->buf;
   1.217 +    int i;
   1.218 +
   1.219 +    LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32 (using NEON)");
   1.220 +
   1.221 +    /* Get dst aligned to 16 bytes */
   1.222 +    for (i = cvt->len_cvt / sizeof (Sint32); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
   1.223 +        *dst = ((float) (*src>>8)) * DIVBY8388607;
   1.224 +    }
   1.225 +
   1.226 +    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
   1.227 +    SDL_assert(!i || ((((size_t) src) & 15) == 0));
   1.228 +
   1.229 +    {
   1.230 +        /* Aligned! Do NEON blocks as long as we have 16 bytes available. */
   1.231 +        const float32x4_t divby8388607 = vdupq_n_f32(DIVBY8388607);
   1.232 +        const int32_t *mmsrc = (const int32_t *) src;
   1.233 +        while (i >= 4) {   /* 4 * sint32 */
   1.234 +            /* shift out lowest bits so int fits in a float32. Small precision loss, but much faster. */
   1.235 +            vst1q_f32(dst, vmulq_f32(vcvtq_f32_s32(vshrq_n_s32(vld1q_s32(mmsrc), 8)), divby8388607));
   1.236 +            i -= 4; mmsrc += 4; dst += 4;
   1.237 +        }
   1.238 +        src = (const Sint32 *) mmsrc;
   1.239 +    }
   1.240 +
   1.241 +    /* Finish off any leftovers with scalar operations. */
   1.242 +    while (i) {
   1.243 +        *dst = ((float) (*src>>8)) * DIVBY8388607;
   1.244 +        i--; src++; dst++;
   1.245 +    }
   1.246 +
   1.247 +    if (cvt->filters[++cvt->filter_index]) {
   1.248 +        cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   1.249 +    }
   1.250 +}
   1.251 +
   1.252 +static void SDLCALL
   1.253 +SDL_Convert_F32_to_S8_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   1.254 +{
   1.255 +    const float *src = (const float *) cvt->buf;
   1.256 +    Sint8 *dst = (Sint8 *) cvt->buf;
   1.257 +    int i;
   1.258 +
   1.259 +    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S8 (using NEON)");
   1.260 +
   1.261 +    /* Get dst aligned to 16 bytes */
   1.262 +    for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
   1.263 +        const float sample = *src;
   1.264 +        if (sample >= 1.0f) {
   1.265 +            *dst = 127;
   1.266 +        } else if (sample <= -1.0f) {
   1.267 +            *dst = -128;
   1.268 +        } else {
   1.269 +            *dst = (Sint8)(sample * 127.0f);
   1.270 +        }
   1.271 +    }
   1.272 +
   1.273 +    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
   1.274 +
   1.275 +    /* Make sure src is aligned too. */
   1.276 +    if ((((size_t) src) & 15) == 0) {
   1.277 +        /* Aligned! Do NEON blocks as long as we have 16 bytes available. */
   1.278 +        const float32x4_t one = vdupq_n_f32(1.0f);
   1.279 +        const float32x4_t negone = vdupq_n_f32(-1.0f);
   1.280 +        const float32x4_t mulby127 = vdupq_n_f32(127.0f);
   1.281 +        int8_t *mmdst = (int8_t *) dst;
   1.282 +        while (i >= 16) {   /* 16 * float32 */
   1.283 +            const int32x4_t ints1 = vcvtq_s32_f32(vmulq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src)), one), mulby127));  /* load 4 floats, clamp, convert to sint32 */
   1.284 +            const int32x4_t ints2 = vcvtq_s32_f32(vmulq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src+4)), one), mulby127));  /* load 4 floats, clamp, convert to sint32 */
   1.285 +            const int32x4_t ints3 = vcvtq_s32_f32(vmulq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src+8)), one), mulby127));  /* load 4 floats, clamp, convert to sint32 */
   1.286 +            const int32x4_t ints4 = vcvtq_s32_f32(vmulq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src+12)), one), mulby127));  /* load 4 floats, clamp, convert to sint32 */
   1.287 +            const int8x8_t i8lo = vmovn_s16(vcombine_s16(vmovn_s32(ints1), vmovn_s32(ints2))); /* narrow to sint16, combine, narrow to sint8 */
   1.288 +            const int8x8_t i8hi = vmovn_s16(vcombine_s16(vmovn_s32(ints3), vmovn_s32(ints4))); /* narrow to sint16, combine, narrow to sint8 */
   1.289 +            vst1q_s8(mmdst, vcombine_s8(i8lo, i8hi));  /* combine to int8x16_t, store out */
   1.290 +            i -= 16; src += 16; mmdst += 16;
   1.291 +        }
   1.292 +        dst = (Sint8 *) mmdst;
   1.293 +    }
   1.294 +
   1.295 +    /* Finish off any leftovers with scalar operations. */
   1.296 +    while (i) {
   1.297 +        const float sample = *src;
   1.298 +        if (sample >= 1.0f) {
   1.299 +            *dst = 127;
   1.300 +        } else if (sample <= -1.0f) {
   1.301 +            *dst = -128;
   1.302 +        } else {
   1.303 +            *dst = (Sint8)(sample * 127.0f);
   1.304 +        }
   1.305 +        i--; src++; dst++;
   1.306 +    }
   1.307 +
   1.308 +    cvt->len_cvt /= 4;
   1.309 +    if (cvt->filters[++cvt->filter_index]) {
   1.310 +        cvt->filters[cvt->filter_index](cvt, AUDIO_S8);
   1.311 +    }
   1.312 +}
   1.313 +
   1.314 +static void SDLCALL
   1.315 +SDL_Convert_F32_to_U8_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   1.316 +{
   1.317 +    const float *src = (const float *) cvt->buf;
   1.318 +    Uint8 *dst = (Uint8 *) cvt->buf;
   1.319 +    int i;
   1.320 +
   1.321 +    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U8 (using NEON)");
   1.322 +
   1.323 +    /* Get dst aligned to 16 bytes */
   1.324 +    for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
   1.325 +        const float sample = *src;
   1.326 +        if (sample >= 1.0f) {
   1.327 +            *dst = 255;
   1.328 +        } else if (sample <= -1.0f) {
   1.329 +            *dst = 0;
   1.330 +        } else {
   1.331 +            *dst = (Uint8)((sample + 1.0f) * 127.0f);
   1.332 +        }
   1.333 +    }
   1.334 +
   1.335 +    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
   1.336 +
   1.337 +    /* Make sure src is aligned too. */
   1.338 +    if ((((size_t) src) & 15) == 0) {
   1.339 +        /* Aligned! Do NEON blocks as long as we have 16 bytes available. */
   1.340 +        const float32x4_t one = vdupq_n_f32(1.0f);
   1.341 +        const float32x4_t negone = vdupq_n_f32(-1.0f);
   1.342 +        const float32x4_t mulby127 = vdupq_n_f32(127.0f);
   1.343 +        uint8_t *mmdst = (uint8_t *) dst;
   1.344 +        while (i >= 16) {   /* 16 * float32 */
   1.345 +            const uint32x4_t uints1 = vcvtq_u32_f32(vmulq_f32(vaddq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src)), one), one), mulby127));  /* load 4 floats, clamp, convert to uint32 */
   1.346 +            const uint32x4_t uints2 = vcvtq_u32_f32(vmulq_f32(vaddq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src+4)), one), one), mulby127));  /* load 4 floats, clamp, convert to uint32 */
   1.347 +            const uint32x4_t uints3 = vcvtq_u32_f32(vmulq_f32(vaddq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src+8)), one), one), mulby127));  /* load 4 floats, clamp, convert to uint32 */
   1.348 +            const uint32x4_t uints4 = vcvtq_u32_f32(vmulq_f32(vaddq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src+12)), one), one), mulby127));  /* load 4 floats, clamp, convert to uint32 */
   1.349 +            const uint8x8_t ui8lo = vmovn_u16(vcombine_u16(vmovn_u32(uints1), vmovn_u32(uints2))); /* narrow to uint16, combine, narrow to uint8 */
   1.350 +            const uint8x8_t ui8hi = vmovn_u16(vcombine_u16(vmovn_u32(uints3), vmovn_u32(uints4))); /* narrow to uint16, combine, narrow to uint8 */
   1.351 +            vst1q_u8(mmdst, vcombine_u8(ui8lo, ui8hi));  /* combine to uint8x16_t, store out */
   1.352 +            i -= 16; src += 16; mmdst += 16;
   1.353 +        }
   1.354 +
   1.355 +        dst = (Uint8 *) mmdst;
   1.356 +    }
   1.357 +
   1.358 +    /* Finish off any leftovers with scalar operations. */
   1.359 +    while (i) {
   1.360 +        const float sample = *src;
   1.361 +        if (sample >= 1.0f) {
   1.362 +            *dst = 255;
   1.363 +        } else if (sample <= -1.0f) {
   1.364 +            *dst = 0;
   1.365 +        } else {
   1.366 +            *dst = (Uint8)((sample + 1.0f) * 127.0f);
   1.367 +        }
   1.368 +        i--; src++; dst++;
   1.369 +    }
   1.370 +
   1.371 +    cvt->len_cvt /= 4;
   1.372 +    if (cvt->filters[++cvt->filter_index]) {
   1.373 +        cvt->filters[cvt->filter_index](cvt, AUDIO_U8);
   1.374 +    }
   1.375 +}
   1.376 +
   1.377 +static void SDLCALL
   1.378 +SDL_Convert_F32_to_S16_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   1.379 +{
   1.380 +    const float *src = (const float *) cvt->buf;
   1.381 +    Sint16 *dst = (Sint16 *) cvt->buf;
   1.382 +    int i;
   1.383 +
   1.384 +    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S16 (using NEON)");
   1.385 +
   1.386 +    /* Get dst aligned to 16 bytes */
   1.387 +    for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
   1.388 +        const float sample = *src;
   1.389 +        if (sample >= 1.0f) {
   1.390 +            *dst = 32767;
   1.391 +        } else if (sample <= -1.0f) {
   1.392 +            *dst = -32768;
   1.393 +        } else {
   1.394 +            *dst = (Sint16)(sample * 32767.0f);
   1.395 +        }
   1.396 +    }
   1.397 +
   1.398 +    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
   1.399 +
   1.400 +    /* Make sure src is aligned too. */
   1.401 +    if ((((size_t) src) & 15) == 0) {
   1.402 +        /* Aligned! Do NEON blocks as long as we have 16 bytes available. */
   1.403 +        const float32x4_t one = vdupq_n_f32(1.0f);
   1.404 +        const float32x4_t negone = vdupq_n_f32(-1.0f);
   1.405 +        const float32x4_t mulby32767 = vdupq_n_f32(32767.0f);
   1.406 +        int16_t *mmdst = (int16_t *) dst;
   1.407 +        while (i >= 8) {   /* 8 * float32 */
   1.408 +            const int32x4_t ints1 = vcvtq_s32_f32(vmulq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src)), one), mulby32767));  /* load 4 floats, clamp, convert to sint32 */
   1.409 +            const int32x4_t ints2 = vcvtq_s32_f32(vmulq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src+4)), one), mulby32767));  /* load 4 floats, clamp, convert to sint32 */
   1.410 +            vst1q_s16(mmdst, vcombine_s16(vmovn_s32(ints1), vmovn_s32(ints2)));  /* narrow to sint16, combine, store out. */
   1.411 +            i -= 8; src += 8; mmdst += 8;
   1.412 +        }
   1.413 +        dst = (Sint16 *) mmdst;
   1.414 +    }
   1.415 +
   1.416 +    /* Finish off any leftovers with scalar operations. */
   1.417 +    while (i) {
   1.418 +        const float sample = *src;
   1.419 +        if (sample >= 1.0f) {
   1.420 +            *dst = 32767;
   1.421 +        } else if (sample <= -1.0f) {
   1.422 +            *dst = -32768;
   1.423 +        } else {
   1.424 +            *dst = (Sint16)(sample * 32767.0f);
   1.425 +        }
   1.426 +        i--; src++; dst++;
   1.427 +    }
   1.428 +
   1.429 +    cvt->len_cvt /= 2;
   1.430 +    if (cvt->filters[++cvt->filter_index]) {
   1.431 +        cvt->filters[cvt->filter_index](cvt, AUDIO_S16SYS);
   1.432 +    }
   1.433 +}
   1.434 +
   1.435 +static void SDLCALL
   1.436 +SDL_Convert_F32_to_U16_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   1.437 +{
   1.438 +    const float *src = (const float *) cvt->buf;
   1.439 +    Uint16 *dst = (Uint16 *) cvt->buf;
   1.440 +    int i;
   1.441 +
   1.442 +    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U16 (using NEON)");
   1.443 +
   1.444 +    /* Get dst aligned to 16 bytes */
   1.445 +    for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
   1.446 +        const float sample = *src;
   1.447 +        if (sample >= 1.0f) {
   1.448 +            *dst = 65535;
   1.449 +        } else if (sample <= -1.0f) {
   1.450 +            *dst = 0;
   1.451 +        } else {
   1.452 +            *dst = (Uint16)((sample + 1.0f) * 32767.0f);
   1.453 +        }
   1.454 +    }
   1.455 +
   1.456 +    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
   1.457 +
   1.458 +    /* Make sure src is aligned too. */
   1.459 +    if ((((size_t) src) & 15) == 0) {
   1.460 +        /* Aligned! Do NEON blocks as long as we have 16 bytes available. */
   1.461 +        const float32x4_t one = vdupq_n_f32(1.0f);
   1.462 +        const float32x4_t negone = vdupq_n_f32(-1.0f);
   1.463 +        const float32x4_t mulby32767 = vdupq_n_f32(32767.0f);
   1.464 +        uint16_t *mmdst = (uint16_t *) dst;
   1.465 +        while (i >= 8) {   /* 8 * float32 */
   1.466 +            const uint32x4_t uints1 = vcvtq_u32_f32(vmulq_f32(vaddq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src)), one), one), mulby32767));  /* load 4 floats, clamp, convert to uint32 */
   1.467 +            const uint32x4_t uints2 = vcvtq_u32_f32(vmulq_f32(vaddq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src+4)), one), one), mulby32767));  /* load 4 floats, clamp, convert to uint32 */
   1.468 +            vst1q_u16(mmdst, vcombine_u16(vmovn_u32(uints1), vmovn_u32(uints2)));  /* narrow to uint16, combine, store out. */
   1.469 +            i -= 8; src += 8; mmdst += 8;
   1.470 +        }
   1.471 +        dst = (Uint16 *) mmdst;
   1.472 +    }
   1.473 +
   1.474 +    /* Finish off any leftovers with scalar operations. */
   1.475 +    while (i) {
   1.476 +        const float sample = *src;
   1.477 +        if (sample >= 1.0f) {
   1.478 +            *dst = 65535;
   1.479 +        } else if (sample <= -1.0f) {
   1.480 +            *dst = 0;
   1.481 +        } else {
   1.482 +            *dst = (Uint16)((sample + 1.0f) * 32767.0f);
   1.483 +        }
   1.484 +        i--; src++; dst++;
   1.485 +    }
   1.486 +
   1.487 +    cvt->len_cvt /= 2;
   1.488 +    if (cvt->filters[++cvt->filter_index]) {
   1.489 +        cvt->filters[cvt->filter_index](cvt, AUDIO_U16SYS);
   1.490 +    }
   1.491 +}
   1.492 +
   1.493 +static void SDLCALL
   1.494 +SDL_Convert_F32_to_S32_NEON(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   1.495 +{
   1.496 +    const float *src = (const float *) cvt->buf;
   1.497 +    Sint32 *dst = (Sint32 *) cvt->buf;
   1.498 +    int i;
   1.499 +
   1.500 +    LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32 (using NEON)");
   1.501 +
   1.502 +    /* Get dst aligned to 16 bytes */
   1.503 +    for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) {
   1.504 +        const float sample = *src;
   1.505 +        if (sample >= 1.0f) {
   1.506 +            *dst = 2147483647;
   1.507 +        } else if (sample <= -1.0f) {
   1.508 +            *dst = -2147483648;
   1.509 +        } else {
   1.510 +            *dst = ((Sint32)(sample * 8388607.0f)) << 8;
   1.511 +        }
   1.512 +    }
   1.513 +
   1.514 +    SDL_assert(!i || ((((size_t) dst) & 15) == 0));
   1.515 +    SDL_assert(!i || ((((size_t) src) & 15) == 0));
   1.516 +
   1.517 +    {
   1.518 +        /* Aligned! Do NEON blocks as long as we have 16 bytes available. */
   1.519 +        const float32x4_t one = vdupq_n_f32(1.0f);
   1.520 +        const float32x4_t negone = vdupq_n_f32(-1.0f);
   1.521 +        const float32x4_t mulby8388607 = vdupq_n_f32(8388607.0f);
   1.522 +        int32_t *mmdst = (int32_t *) dst;
   1.523 +        while (i >= 4) {   /* 4 * float32 */
   1.524 +            vst1q_s32(mmdst, vshlq_n_s32(vcvtq_s32_f32(vmulq_f32(vminq_f32(vmaxq_f32(negone, vld1q_f32(src)), one), mulby8388607)), 8));
   1.525 +            i -= 4; src += 4; mmdst += 4;
   1.526 +        }
   1.527 +        dst = (Sint32 *) mmdst;
   1.528 +    }
   1.529 +
   1.530 +    /* Finish off any leftovers with scalar operations. */
   1.531 +    while (i) {
   1.532 +        const float sample = *src;
   1.533 +        if (sample >= 1.0f) {
   1.534 +            *dst = 2147483647;
   1.535 +        } else if (sample <= -1.0f) {
   1.536 +            *dst = -2147483648;
   1.537 +        } else {
   1.538 +            *dst = ((Sint32)(sample * 8388607.0f)) << 8;
   1.539 +        }
   1.540 +        i--; src++; dst++;
   1.541 +    }
   1.542 +
   1.543 +    if (cvt->filters[++cvt->filter_index]) {
   1.544 +        cvt->filters[cvt->filter_index](cvt, AUDIO_S32SYS);
   1.545 +    }
   1.546 +}
   1.547 +#endif
   1.548 +
   1.549 +
   1.550 +
   1.551  void SDL_ChooseAudioConverters(void)
   1.552  {
   1.553      static SDL_bool converters_chosen = SDL_FALSE;
   1.554 @@ -878,6 +1411,13 @@
   1.555      }
   1.556  #endif
   1.557  
   1.558 +#if HAVE_NEON_INTRINSICS
   1.559 +    if (SDL_HasNEON()) {
   1.560 +        SET_CONVERTER_FUNCS(NEON);
   1.561 +        return;
   1.562 +    }
   1.563 +#endif
   1.564 +
   1.565  #if NEED_SCALAR_CONVERTER_FALLBACKS
   1.566      SET_CONVERTER_FUNCS(Scalar);
   1.567  #endif