Avoid an extra sample of delay in the reverb modulator
[openal-soft.git] / Alc / mixer_sse3.c
blob7085c5372f295bfa3a9ce60c40ab7519180c7abf
1 /**
2 * OpenAL cross platform audio library, SSE3 mixer functions
4 * Copyright (C) 2014 by Timothy Arceri <t_arceri@yahoo.com.au>.
5 * Copyright (C) 2015 by Chris Robinson <chris.kcat@gmail.com>.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Or go to http://www.gnu.org/copyleft/lgpl.html
24 #include "config.h"
26 #include <xmmintrin.h>
27 #include <emmintrin.h>
28 #include <pmmintrin.h>
30 #include "alu.h"
31 #include "mixer_defs.h"
34 const ALfloat *Resample_fir4_32_SSE3(const BsincState* UNUSED(state), const ALfloat *src, ALuint frac, ALuint increment,
35 ALfloat *restrict dst, ALuint numsamples)
37 const __m128i increment4 = _mm_set1_epi32(increment*4);
38 const __m128i fracMask4 = _mm_set1_epi32(FRACTIONMASK);
39 alignas(16) union { ALuint i[4]; float f[4]; } pos_;
40 alignas(16) union { ALuint i[4]; float f[4]; } frac_;
41 __m128i frac4, pos4;
42 ALuint pos;
43 ALuint i;
45 InitiatePositionArrays(frac, increment, frac_.i, pos_.i, 4);
47 frac4 = _mm_castps_si128(_mm_load_ps(frac_.f));
48 pos4 = _mm_castps_si128(_mm_load_ps(pos_.f));
50 --src;
51 for(i = 0;numsamples-i > 3;i += 4)
53 const __m128 val0 = _mm_loadu_ps(&src[pos_.i[0]]);
54 const __m128 val1 = _mm_loadu_ps(&src[pos_.i[1]]);
55 const __m128 val2 = _mm_loadu_ps(&src[pos_.i[2]]);
56 const __m128 val3 = _mm_loadu_ps(&src[pos_.i[3]]);
57 __m128 k0 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[0]]);
58 __m128 k1 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[1]]);
59 __m128 k2 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[2]]);
60 __m128 k3 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[3]]);
61 __m128 out;
63 k0 = _mm_mul_ps(k0, val0);
64 k1 = _mm_mul_ps(k1, val1);
65 k2 = _mm_mul_ps(k2, val2);
66 k3 = _mm_mul_ps(k3, val3);
67 k0 = _mm_hadd_ps(k0, k1);
68 k2 = _mm_hadd_ps(k2, k3);
69 out = _mm_hadd_ps(k0, k2);
71 _mm_store_ps(&dst[i], out);
73 frac4 = _mm_add_epi32(frac4, increment4);
74 pos4 = _mm_add_epi32(pos4, _mm_srli_epi32(frac4, FRACTIONBITS));
75 frac4 = _mm_and_si128(frac4, fracMask4);
77 _mm_store_ps(pos_.f, _mm_castsi128_ps(pos4));
78 _mm_store_ps(frac_.f, _mm_castsi128_ps(frac4));
81 /* NOTE: These four elements represent the position *after* the last four
82 * samples, so the lowest element is the next position to resample.
84 pos = pos_.i[0];
85 frac = frac_.i[0];
87 for(;i < numsamples;i++)
89 dst[i] = resample_fir4(src[pos], src[pos+1], src[pos+2], src[pos+3], frac);
91 frac += increment;
92 pos += frac>>FRACTIONBITS;
93 frac &= FRACTIONMASK;
95 return dst;
98 const ALfloat *Resample_fir8_32_SSE3(const BsincState* UNUSED(state), const ALfloat *src, ALuint frac, ALuint increment,
99 ALfloat *restrict dst, ALuint numsamples)
101 const __m128i increment4 = _mm_set1_epi32(increment*4);
102 const __m128i fracMask4 = _mm_set1_epi32(FRACTIONMASK);
103 alignas(16) union { ALuint i[4]; float f[4]; } pos_;
104 alignas(16) union { ALuint i[4]; float f[4]; } frac_;
105 __m128i frac4, pos4;
106 ALuint pos;
107 ALuint i, j;
109 InitiatePositionArrays(frac, increment, frac_.i, pos_.i, 4);
111 frac4 = _mm_castps_si128(_mm_load_ps(frac_.f));
112 pos4 = _mm_castps_si128(_mm_load_ps(pos_.f));
114 src -= 3;
115 for(i = 0;numsamples-i > 3;i += 4)
117 __m128 out[2];
118 for(j = 0;j < 8;j+=4)
120 const __m128 val0 = _mm_loadu_ps(&src[pos_.i[0]+j]);
121 const __m128 val1 = _mm_loadu_ps(&src[pos_.i[1]+j]);
122 const __m128 val2 = _mm_loadu_ps(&src[pos_.i[2]+j]);
123 const __m128 val3 = _mm_loadu_ps(&src[pos_.i[3]+j]);
124 __m128 k0 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[0]][j]);
125 __m128 k1 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[1]][j]);
126 __m128 k2 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[2]][j]);
127 __m128 k3 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[3]][j]);
129 k0 = _mm_mul_ps(k0, val0);
130 k1 = _mm_mul_ps(k1, val1);
131 k2 = _mm_mul_ps(k2, val2);
132 k3 = _mm_mul_ps(k3, val3);
133 k0 = _mm_hadd_ps(k0, k1);
134 k2 = _mm_hadd_ps(k2, k3);
135 out[j>>2] = _mm_hadd_ps(k0, k2);
138 out[0] = _mm_add_ps(out[0], out[1]);
139 _mm_store_ps(&dst[i], out[0]);
141 frac4 = _mm_add_epi32(frac4, increment4);
142 pos4 = _mm_add_epi32(pos4, _mm_srli_epi32(frac4, FRACTIONBITS));
143 frac4 = _mm_and_si128(frac4, fracMask4);
145 _mm_store_ps(pos_.f, _mm_castsi128_ps(pos4));
146 _mm_store_ps(frac_.f, _mm_castsi128_ps(frac4));
149 pos = pos_.i[0];
150 frac = frac_.i[0];
152 for(;i < numsamples;i++)
154 dst[i] = resample_fir8(src[pos ], src[pos+1], src[pos+2], src[pos+3],
155 src[pos+4], src[pos+5], src[pos+6], src[pos+7], frac);
157 frac += increment;
158 pos += frac>>FRACTIONBITS;
159 frac &= FRACTIONMASK;
161 return dst;