From 4cc1c646466737ba411aa23ce4a6116936ada8c2 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 7 Jan 2018 05:32:07 -0800 Subject: [PATCH] Replace the sinc4 resampler with cubic Turns out the C version of the cubic resampler is just slightly faster than even the SSE3 version of the FIR4 resampler. This is likely due to not using a 64KB random-access lookup table along with unaligned loads, both offseting the gains from SSE. --- Alc/ALu.c | 2 - Alc/mixer.c | 20 ++------ Alc/mixer_c.c | 39 +++++++-------- Alc/mixer_defs.h | 12 +---- Alc/mixer_neon.c | 71 --------------------------- Alc/mixer_sse3.c | 98 -------------------------------------- Alc/mixer_sse41.c | 69 --------------------------- OpenAL32/Include/alu.h | 4 -- OpenAL32/alState.c | 4 +- alsoftrc.sample | 2 +- utils/alsoft-config/mainwindow.cpp | 2 +- 11 files changed, 25 insertions(+), 298 deletions(-) rewrite Alc/mixer_sse3.c (100%) diff --git a/Alc/ALu.c b/Alc/ALu.c index 5f0c556a..1964e6c7 100644 --- a/Alc/ALu.c +++ b/Alc/ALu.c @@ -69,8 +69,6 @@ extern inline ALuint64 maxu64(ALuint64 a, ALuint64 b); extern inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max); extern inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu); -extern inline ALfloat resample_fir4(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, - const ALfloat *restrict filter); extern inline void aluVectorSet(aluVector *restrict vector, ALfloat x, ALfloat y, ALfloat z, ALfloat w); diff --git a/Alc/mixer.c b/Alc/mixer.c index 5d6d14d7..a7f0f302 100644 --- a/Alc/mixer.c +++ b/Alc/mixer.c @@ -128,19 +128,7 @@ ResamplerFunc SelectResampler(enum Resampler resampler) #endif return Resample_lerp_C; case FIR4Resampler: -#ifdef HAVE_NEON - if((CPUCapFlags&CPU_CAP_NEON)) - return Resample_fir4_Neon; -#endif -#ifdef HAVE_SSE4_1 - if((CPUCapFlags&CPU_CAP_SSE4_1)) - return Resample_fir4_SSE41; -#endif -#ifdef HAVE_SSE3 - if((CPUCapFlags&CPU_CAP_SSE3)) - return Resample_fir4_SSE3; -#endif - return Resample_fir4_C; + return Resample_cubic_C; case BSinc12Resampler: case BSinc24Resampler: #ifdef HAVE_NEON @@ -168,7 +156,7 @@ void aluInitMixer(void) ResamplerDefault = PointResampler; else if(strcasecmp(str, "linear") == 0) ResamplerDefault = LinearResampler; - else if(strcasecmp(str, "sinc4") == 0) + else if(strcasecmp(str, "cubic") == 0) ResamplerDefault = FIR4Resampler; else if(strcasecmp(str, "bsinc12") == 0) ResamplerDefault = BSinc12Resampler; @@ -179,9 +167,9 @@ void aluInitMixer(void) WARN("Resampler option \"%s\" is deprecated, using bsinc12\n", str); ResamplerDefault = BSinc12Resampler; } - else if(strcasecmp(str, "cubic") == 0 || strcasecmp(str, "sinc8") == 0) + else if(strcasecmp(str, "sinc4") == 0 || strcasecmp(str, "sinc8") == 0) { - WARN("Resampler option \"%s\" is deprecated, using sinc4\n", str); + WARN("Resampler option \"%s\" is deprecated, using cubic\n", str); ResamplerDefault = FIR4Resampler; } else diff --git a/Alc/mixer_c.c b/Alc/mixer_c.c index e468fedb..41445fc4 100644 --- a/Alc/mixer_c.c +++ b/Alc/mixer_c.c @@ -12,7 +12,16 @@ static inline ALfloat do_point(const ALfloat *restrict vals, ALsizei UNUSED(frac { return vals[0]; } static inline ALfloat do_lerp(const ALfloat *restrict vals, ALsizei frac) { return lerp(vals[0], vals[1], frac * (1.0f/FRACTIONONE)); } - +static inline ALfloat do_cubic(const ALfloat *restrict vals, ALsizei frac) +{ + ALfloat mu = frac * (1.0f/FRACTIONONE); + ALfloat mu2 = mu*mu, mu3 = mu2*mu; + ALfloat a0 = -0.5f*mu3 + mu2 + -0.5f*mu; + ALfloat a1 = 1.5f*mu3 + -2.5f*mu2 + 1.0f; + ALfloat a2 = -1.5f*mu3 + 2.0f*mu2 + 0.5f*mu; + ALfloat a3 = 0.5f*mu3 + -0.5f*mu2; + return vals[0]*a0 + vals[1]*a1 + vals[2]*a2 + vals[3]*a3; +} const ALfloat *Resample_copy_C(const InterpState* UNUSED(state), const ALfloat *restrict src, ALsizei UNUSED(frac), ALint UNUSED(increment), @@ -27,12 +36,14 @@ const ALfloat *Resample_copy_C(const InterpState* UNUSED(state), return dst; } -#define DECL_TEMPLATE(Tag, Sampler) \ +#define DECL_TEMPLATE(Tag, Sampler, O) \ const ALfloat *Resample_##Tag##_C(const InterpState* UNUSED(state), \ const ALfloat *restrict src, ALsizei frac, ALint increment, \ ALfloat *restrict dst, ALsizei numsamples) \ { \ ALsizei i; \ + \ + src -= O; \ for(i = 0;i < numsamples;i++) \ { \ dst[i] = Sampler(src, frac); \ @@ -44,30 +55,12 @@ const ALfloat *Resample_##Tag##_C(const InterpState* UNUSED(state), \ return dst; \ } -DECL_TEMPLATE(point, do_point) -DECL_TEMPLATE(lerp, do_lerp) +DECL_TEMPLATE(point, do_point, 0) +DECL_TEMPLATE(lerp, do_lerp, 0) +DECL_TEMPLATE(cubic, do_cubic, 1) #undef DECL_TEMPLATE -const ALfloat *Resample_fir4_C(const InterpState *state, const ALfloat *restrict src, - ALsizei frac, ALint increment, ALfloat *restrict dst, - ALsizei numsamples) -{ - const ALfloat (*restrict filter)[4] = ASSUME_ALIGNED(state->sinc4.filter, 16); - ALsizei i; - - src -= 1; - for(i = 0;i < numsamples;i++) - { - dst[i] = resample_fir4(src[0], src[1], src[2], src[3], filter[frac]); - - frac += increment; - src += frac>>FRACTIONBITS; - frac &= FRACTIONMASK; - } - return dst; -} - const ALfloat *Resample_bsinc_C(const InterpState *state, const ALfloat *restrict src, ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei dstlen) diff --git a/Alc/mixer_defs.h b/Alc/mixer_defs.h index 364bbf7a..fe19cef4 100644 --- a/Alc/mixer_defs.h +++ b/Alc/mixer_defs.h @@ -15,7 +15,7 @@ struct HrtfState; const ALfloat *Resample_copy_C(const InterpState *state, const ALfloat *restrict src, ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei dstlen); const ALfloat *Resample_point_C(const InterpState *state, const ALfloat *restrict src, ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei dstlen); const ALfloat *Resample_lerp_C(const InterpState *state, const ALfloat *restrict src, ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei dstlen); -const ALfloat *Resample_fir4_C(const InterpState *state, const ALfloat *restrict src, ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei dstlen); +const ALfloat *Resample_cubic_C(const InterpState *state, const ALfloat *restrict src, ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei dstlen); const ALfloat *Resample_bsinc_C(const InterpState *state, const ALfloat *restrict src, ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei dstlen); @@ -83,13 +83,6 @@ const ALfloat *Resample_lerp_SSE41(const InterpState *state, const ALfloat *rest ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei numsamples); -const ALfloat *Resample_fir4_SSE3(const InterpState *state, const ALfloat *restrict src, - ALsizei frac, ALint increment, ALfloat *restrict dst, - ALsizei numsamples); -const ALfloat *Resample_fir4_SSE41(const InterpState *state, const ALfloat *restrict src, - ALsizei frac, ALint increment, ALfloat *restrict dst, - ALsizei numsamples); - const ALfloat *Resample_bsinc_SSE(const InterpState *state, const ALfloat *restrict src, ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei dstlen); @@ -119,9 +112,6 @@ void MixRow_Neon(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat *Resample_lerp_Neon(const InterpState *state, const ALfloat *restrict src, ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei numsamples); -const ALfloat *Resample_fir4_Neon(const InterpState *state, const ALfloat *restrict src, - ALsizei frac, ALint increment, ALfloat *restrict dst, - ALsizei numsamples); const ALfloat *Resample_bsinc_Neon(const InterpState *state, const ALfloat *restrict src, ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei dstlen); diff --git a/Alc/mixer_neon.c b/Alc/mixer_neon.c index b99dcf69..631e4f7c 100644 --- a/Alc/mixer_neon.c +++ b/Alc/mixer_neon.c @@ -66,77 +66,6 @@ const ALfloat *Resample_lerp_Neon(const InterpState* UNUSED(state), return dst; } -const ALfloat *Resample_fir4_Neon(const InterpState *state, - const ALfloat *restrict src, ALsizei frac, ALint increment, - ALfloat *restrict dst, ALsizei numsamples) -{ - const ALfloat (*restrict filter)[4] = ASSUME_ALIGNED(state->sinc4.filter, 16); - const int32x4_t increment4 = vdupq_n_s32(increment*4); - const int32x4_t fracMask4 = vdupq_n_s32(FRACTIONMASK); - alignas(16) ALint pos_[4]; - alignas(16) ALsizei frac_[4]; - int32x4_t pos4; - int32x4_t frac4; - ALsizei i; - - InitiatePositionArrays(frac, increment, frac_, pos_, 4); - - frac4 = vld1q_s32(frac_); - pos4 = vld1q_s32(pos_); - - --src; - for(i = 0;numsamples-i > 3;i += 4) - { - const float32x4_t val0 = vld1q_f32(&src[pos_[0]]); - const float32x4_t val1 = vld1q_f32(&src[pos_[1]]); - const float32x4_t val2 = vld1q_f32(&src[pos_[2]]); - const float32x4_t val3 = vld1q_f32(&src[pos_[3]]); - float32x4_t k0 = vld1q_f32(filter[frac_[0]]); - float32x4_t k1 = vld1q_f32(filter[frac_[1]]); - float32x4_t k2 = vld1q_f32(filter[frac_[2]]); - float32x4_t k3 = vld1q_f32(filter[frac_[3]]); - float32x4_t out; - - k0 = vmulq_f32(k0, val0); - k1 = vmulq_f32(k1, val1); - k2 = vmulq_f32(k2, val2); - k3 = vmulq_f32(k3, val3); - k0 = vcombine_f32(vpadd_f32(vget_low_f32(k0), vget_high_f32(k0)), - vpadd_f32(vget_low_f32(k1), vget_high_f32(k1))); - k2 = vcombine_f32(vpadd_f32(vget_low_f32(k2), vget_high_f32(k2)), - vpadd_f32(vget_low_f32(k3), vget_high_f32(k3))); - out = vcombine_f32(vpadd_f32(vget_low_f32(k0), vget_high_f32(k0)), - vpadd_f32(vget_low_f32(k2), vget_high_f32(k2))); - - vst1q_f32(&dst[i], out); - - frac4 = vaddq_s32(frac4, increment4); - pos4 = vaddq_s32(pos4, vshrq_n_s32(frac4, FRACTIONBITS)); - frac4 = vandq_s32(frac4, fracMask4); - - vst1q_s32(pos_, pos4); - vst1q_s32(frac_, frac4); - } - - if(i < numsamples) - { - /* NOTE: These four elements represent the position *after* the last - * four samples, so the lowest element is the next position to - * resample. - */ - ALint pos = pos_[0]; - frac = frac_[0]; - do { - dst[i] = resample_fir4(src[pos], src[pos+1], src[pos+2], src[pos+3], filter[frac]); - - frac += increment; - pos += frac>>FRACTIONBITS; - frac &= FRACTIONMASK; - } while(++i < numsamples); - } - return dst; -} - const ALfloat *Resample_bsinc_Neon(const InterpState *state, const ALfloat *restrict src, ALsizei frac, ALint increment, ALfloat *restrict dst, ALsizei dstlen) diff --git a/Alc/mixer_sse3.c b/Alc/mixer_sse3.c dissimilarity index 100% index d60fed5f..e69de29b 100644 --- a/Alc/mixer_sse3.c +++ b/Alc/mixer_sse3.c @@ -1,98 +0,0 @@ -/** - * OpenAL cross platform audio library, SSE3 mixer functions - * - * Copyright (C) 2014 by Timothy Arceri . - * Copyright (C) 2015 by Chris Robinson . - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * Or go to http://www.gnu.org/copyleft/lgpl.html - */ - -#include "config.h" - -#include -#include -#include - -#include "alu.h" -#include "mixer_defs.h" - - -const ALfloat *Resample_fir4_SSE3(const InterpState *state, - const ALfloat *restrict src, ALsizei frac, ALint increment, - ALfloat *restrict dst, ALsizei numsamples) -{ - const ALfloat (*restrict filter)[4] = ASSUME_ALIGNED(state->sinc4.filter, 16); - const __m128i increment4 = _mm_set1_epi32(increment*4); - const __m128i fracMask4 = _mm_set1_epi32(FRACTIONMASK); - union { alignas(16) ALint i[4]; float f[4]; } pos_; - union { alignas(16) ALsizei i[4]; float f[4]; } frac_; - __m128i frac4, pos4; - ALint pos; - ALsizei i; - - InitiatePositionArrays(frac, increment, frac_.i, pos_.i, 4); - - frac4 = _mm_castps_si128(_mm_load_ps(frac_.f)); - pos4 = _mm_castps_si128(_mm_load_ps(pos_.f)); - - --src; - for(i = 0;numsamples-i > 3;i += 4) - { - const __m128 val0 = _mm_loadu_ps(&src[pos_.i[0]]); - const __m128 val1 = _mm_loadu_ps(&src[pos_.i[1]]); - const __m128 val2 = _mm_loadu_ps(&src[pos_.i[2]]); - const __m128 val3 = _mm_loadu_ps(&src[pos_.i[3]]); - __m128 k0 = _mm_load_ps(filter[frac_.i[0]]); - __m128 k1 = _mm_load_ps(filter[frac_.i[1]]); - __m128 k2 = _mm_load_ps(filter[frac_.i[2]]); - __m128 k3 = _mm_load_ps(filter[frac_.i[3]]); - __m128 out; - - k0 = _mm_mul_ps(k0, val0); - k1 = _mm_mul_ps(k1, val1); - k2 = _mm_mul_ps(k2, val2); - k3 = _mm_mul_ps(k3, val3); - k0 = _mm_hadd_ps(k0, k1); - k2 = _mm_hadd_ps(k2, k3); - out = _mm_hadd_ps(k0, k2); - - _mm_store_ps(&dst[i], out); - - frac4 = _mm_add_epi32(frac4, increment4); - pos4 = _mm_add_epi32(pos4, _mm_srli_epi32(frac4, FRACTIONBITS)); - frac4 = _mm_and_si128(frac4, fracMask4); - - _mm_store_ps(pos_.f, _mm_castsi128_ps(pos4)); - _mm_store_ps(frac_.f, _mm_castsi128_ps(frac4)); - } - - /* NOTE: These four elements represent the position *after* the last four - * samples, so the lowest element is the next position to resample. - */ - pos = pos_.i[0]; - frac = frac_.i[0]; - - for(;i < numsamples;i++) - { - dst[i] = resample_fir4(src[pos], src[pos+1], src[pos+2], src[pos+3], filter[frac]); - - frac += increment; - pos += frac>>FRACTIONBITS; - frac &= FRACTIONMASK; - } - return dst; -} diff --git a/Alc/mixer_sse41.c b/Alc/mixer_sse41.c index 354d16b7..4f88d540 100644 --- a/Alc/mixer_sse41.c +++ b/Alc/mixer_sse41.c @@ -84,72 +84,3 @@ const ALfloat *Resample_lerp_SSE41(const InterpState* UNUSED(state), } return dst; } - -const ALfloat *Resample_fir4_SSE41(const InterpState *state, - const ALfloat *restrict src, ALsizei frac, ALint increment, - ALfloat *restrict dst, ALsizei numsamples) -{ - const ALfloat (*restrict filter)[4] = ASSUME_ALIGNED(state->sinc4.filter, 16); - const __m128i increment4 = _mm_set1_epi32(increment*4); - const __m128i fracMask4 = _mm_set1_epi32(FRACTIONMASK); - union { alignas(16) ALint i[4]; float f[4]; } pos_; - union { alignas(16) ALsizei i[4]; float f[4]; } frac_; - __m128i frac4, pos4; - ALint pos; - ALsizei i; - - InitiatePositionArrays(frac, increment, frac_.i, pos_.i, 4); - - frac4 = _mm_castps_si128(_mm_load_ps(frac_.f)); - pos4 = _mm_castps_si128(_mm_load_ps(pos_.f)); - - --src; - for(i = 0;numsamples-i > 3;i += 4) - { - const __m128 val0 = _mm_loadu_ps(&src[pos_.i[0]]); - const __m128 val1 = _mm_loadu_ps(&src[pos_.i[1]]); - const __m128 val2 = _mm_loadu_ps(&src[pos_.i[2]]); - const __m128 val3 = _mm_loadu_ps(&src[pos_.i[3]]); - __m128 k0 = _mm_load_ps(filter[frac_.i[0]]); - __m128 k1 = _mm_load_ps(filter[frac_.i[1]]); - __m128 k2 = _mm_load_ps(filter[frac_.i[2]]); - __m128 k3 = _mm_load_ps(filter[frac_.i[3]]); - __m128 out; - - k0 = _mm_mul_ps(k0, val0); - k1 = _mm_mul_ps(k1, val1); - k2 = _mm_mul_ps(k2, val2); - k3 = _mm_mul_ps(k3, val3); - k0 = _mm_hadd_ps(k0, k1); - k2 = _mm_hadd_ps(k2, k3); - out = _mm_hadd_ps(k0, k2); - - _mm_store_ps(&dst[i], out); - - frac4 = _mm_add_epi32(frac4, increment4); - pos4 = _mm_add_epi32(pos4, _mm_srli_epi32(frac4, FRACTIONBITS)); - frac4 = _mm_and_si128(frac4, fracMask4); - - pos_.i[0] = _mm_extract_epi32(pos4, 0); - pos_.i[1] = _mm_extract_epi32(pos4, 1); - pos_.i[2] = _mm_extract_epi32(pos4, 2); - pos_.i[3] = _mm_extract_epi32(pos4, 3); - frac_.i[0] = _mm_extract_epi32(frac4, 0); - frac_.i[1] = _mm_extract_epi32(frac4, 1); - frac_.i[2] = _mm_extract_epi32(frac4, 2); - frac_.i[3] = _mm_extract_epi32(frac4, 3); - } - - pos = pos_.i[0]; - frac = frac_.i[0]; - - for(;i < numsamples;i++) - { - dst[i] = resample_fir4(src[pos], src[pos+1], src[pos+2], src[pos+3], filter[frac]); - - frac += increment; - pos += frac>>FRACTIONBITS; - frac &= FRACTIONMASK; - } - return dst; -} diff --git a/OpenAL32/Include/alu.h b/OpenAL32/Include/alu.h index 625da0dc..c427df0c 100644 --- a/OpenAL32/Include/alu.h +++ b/OpenAL32/Include/alu.h @@ -396,10 +396,6 @@ inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu) { return val1 + (val2-val1)*mu; } -inline ALfloat resample_fir4(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, const ALfloat *restrict filter) -{ - return filter[0]*val0 + filter[1]*val1 + filter[2]*val2 + filter[3]*val3; -} enum HrtfRequestMode { diff --git a/OpenAL32/alState.c b/OpenAL32/alState.c index 623e1d6b..c8c81065 100644 --- a/OpenAL32/alState.c +++ b/OpenAL32/alState.c @@ -50,7 +50,7 @@ static const ALchar alErrOutOfMemory[] = "Out of Memory"; /* Resampler strings */ static const ALchar alPointResampler[] = "Nearest"; static const ALchar alLinearResampler[] = "Linear"; -static const ALchar alSinc4Resampler[] = "3rd order Sinc"; +static const ALchar alCubicResampler[] = "Cubic"; static const ALchar alBSinc12Resampler[] = "11th order Sinc"; static const ALchar alBSinc24Resampler[] = "23rd order Sinc"; @@ -761,7 +761,7 @@ AL_API const ALchar* AL_APIENTRY alGetStringiSOFT(ALenum pname, ALsizei index) { const char *ResamplerNames[] = { alPointResampler, alLinearResampler, - alSinc4Resampler, alBSinc12Resampler, + alCubicResampler, alBSinc12Resampler, alBSinc24Resampler, }; const ALchar *value = NULL; diff --git a/alsoftrc.sample b/alsoftrc.sample index 41c884ad..68d45220 100644 --- a/alsoftrc.sample +++ b/alsoftrc.sample @@ -149,7 +149,7 @@ # Selects the resampler used when mixing sources. Valid values are: # point - nearest sample, no interpolation # linear - extrapolates samples using a linear slope between samples -# sinc4 - extrapolates samples using a 4-point Sinc filter +# cubic - extrapolates samples using a Catmull-Rom spline # bsinc12 - extrapolates samples using a band-limited Sinc filter (varying # between 12 and 24 points, with anti-aliasing) # bsinc24 - extrapolates samples using a band-limited Sinc filter (varying diff --git a/utils/alsoft-config/mainwindow.cpp b/utils/alsoft-config/mainwindow.cpp index 31419479..f1bbf7db 100644 --- a/utils/alsoft-config/mainwindow.cpp +++ b/utils/alsoft-config/mainwindow.cpp @@ -100,7 +100,7 @@ static const struct NameValuePair { { "Point", "point" }, { "Linear", "linear" }, { "Default (Linear)", "" }, - { "3rd order Sinc", "sinc4" }, + { "Cubic Spline", "cubic" }, { "11th order Sinc", "bsinc12" }, { "23rd order Sinc", "bsinc24" }, -- 2.11.4.GIT