Rename input_gain to b0
[openal-soft.git] / Alc / mixer_sse41.c
blobed49447d21fb3568ceed4fae5f8eb2144a066ba1
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2014 by Timothy Arceri <t_arceri@yahoo.com.au>.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <xmmintrin.h>
24 #include <emmintrin.h>
25 #include <smmintrin.h>
27 #include "alu.h"
28 #include "mixer_defs.h"
31 const ALfloat *Resample_lerp32_SSE41(const BsincState* UNUSED(state), const ALfloat *src, ALuint frac, ALuint increment,
32 ALfloat *restrict dst, ALuint numsamples)
34 const __m128i increment4 = _mm_set1_epi32(increment*4);
35 const __m128 fracOne4 = _mm_set1_ps(1.0f/FRACTIONONE);
36 const __m128i fracMask4 = _mm_set1_epi32(FRACTIONMASK);
37 union { alignas(16) ALuint i[4]; float f[4]; } pos_;
38 union { alignas(16) ALuint i[4]; float f[4]; } frac_;
39 __m128i frac4, pos4;
40 ALuint pos;
41 ALuint i;
43 InitiatePositionArrays(frac, increment, frac_.i, pos_.i, 4);
45 frac4 = _mm_castps_si128(_mm_load_ps(frac_.f));
46 pos4 = _mm_castps_si128(_mm_load_ps(pos_.f));
48 for(i = 0;numsamples-i > 3;i += 4)
50 const __m128 val1 = _mm_setr_ps(src[pos_.i[0]], src[pos_.i[1]], src[pos_.i[2]], src[pos_.i[3]]);
51 const __m128 val2 = _mm_setr_ps(src[pos_.i[0]+1], src[pos_.i[1]+1], src[pos_.i[2]+1], src[pos_.i[3]+1]);
53 /* val1 + (val2-val1)*mu */
54 const __m128 r0 = _mm_sub_ps(val2, val1);
55 const __m128 mu = _mm_mul_ps(_mm_cvtepi32_ps(frac4), fracOne4);
56 const __m128 out = _mm_add_ps(val1, _mm_mul_ps(mu, r0));
58 _mm_store_ps(&dst[i], out);
60 frac4 = _mm_add_epi32(frac4, increment4);
61 pos4 = _mm_add_epi32(pos4, _mm_srli_epi32(frac4, FRACTIONBITS));
62 frac4 = _mm_and_si128(frac4, fracMask4);
64 pos_.i[0] = _mm_extract_epi32(pos4, 0);
65 pos_.i[1] = _mm_extract_epi32(pos4, 1);
66 pos_.i[2] = _mm_extract_epi32(pos4, 2);
67 pos_.i[3] = _mm_extract_epi32(pos4, 3);
70 /* NOTE: These four elements represent the position *after* the last four
71 * samples, so the lowest element is the next position to resample.
73 pos = pos_.i[0];
74 frac = _mm_cvtsi128_si32(frac4);
76 for(;i < numsamples;i++)
78 dst[i] = lerp(src[pos], src[pos+1], frac * (1.0f/FRACTIONONE));
80 frac += increment;
81 pos += frac>>FRACTIONBITS;
82 frac &= FRACTIONMASK;
84 return dst;
87 const ALfloat *Resample_fir4_32_SSE41(const BsincState* UNUSED(state), const ALfloat *src, ALuint frac, ALuint increment,
88 ALfloat *restrict dst, ALuint numsamples)
90 const __m128i increment4 = _mm_set1_epi32(increment*4);
91 const __m128i fracMask4 = _mm_set1_epi32(FRACTIONMASK);
92 union { alignas(16) ALuint i[4]; float f[4]; } pos_;
93 union { alignas(16) ALuint i[4]; float f[4]; } frac_;
94 __m128i frac4, pos4;
95 ALuint pos;
96 ALuint i;
98 InitiatePositionArrays(frac, increment, frac_.i, pos_.i, 4);
100 frac4 = _mm_castps_si128(_mm_load_ps(frac_.f));
101 pos4 = _mm_castps_si128(_mm_load_ps(pos_.f));
103 --src;
104 for(i = 0;numsamples-i > 3;i += 4)
106 const __m128 val0 = _mm_loadu_ps(&src[pos_.i[0]]);
107 const __m128 val1 = _mm_loadu_ps(&src[pos_.i[1]]);
108 const __m128 val2 = _mm_loadu_ps(&src[pos_.i[2]]);
109 const __m128 val3 = _mm_loadu_ps(&src[pos_.i[3]]);
110 __m128 k0 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[0]]);
111 __m128 k1 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[1]]);
112 __m128 k2 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[2]]);
113 __m128 k3 = _mm_load_ps(ResampleCoeffs.FIR4[frac_.i[3]]);
114 __m128 out;
116 k0 = _mm_mul_ps(k0, val0);
117 k1 = _mm_mul_ps(k1, val1);
118 k2 = _mm_mul_ps(k2, val2);
119 k3 = _mm_mul_ps(k3, val3);
120 k0 = _mm_hadd_ps(k0, k1);
121 k2 = _mm_hadd_ps(k2, k3);
122 out = _mm_hadd_ps(k0, k2);
124 _mm_store_ps(&dst[i], out);
126 frac4 = _mm_add_epi32(frac4, increment4);
127 pos4 = _mm_add_epi32(pos4, _mm_srli_epi32(frac4, FRACTIONBITS));
128 frac4 = _mm_and_si128(frac4, fracMask4);
130 pos_.i[0] = _mm_extract_epi32(pos4, 0);
131 pos_.i[1] = _mm_extract_epi32(pos4, 1);
132 pos_.i[2] = _mm_extract_epi32(pos4, 2);
133 pos_.i[3] = _mm_extract_epi32(pos4, 3);
134 frac_.i[0] = _mm_extract_epi32(frac4, 0);
135 frac_.i[1] = _mm_extract_epi32(frac4, 1);
136 frac_.i[2] = _mm_extract_epi32(frac4, 2);
137 frac_.i[3] = _mm_extract_epi32(frac4, 3);
140 pos = pos_.i[0];
141 frac = frac_.i[0];
143 for(;i < numsamples;i++)
145 dst[i] = resample_fir4(src[pos], src[pos+1], src[pos+2], src[pos+3], frac);
147 frac += increment;
148 pos += frac>>FRACTIONBITS;
149 frac &= FRACTIONMASK;
151 return dst;
154 const ALfloat *Resample_fir8_32_SSE41(const BsincState* UNUSED(state), const ALfloat *src, ALuint frac, ALuint increment,
155 ALfloat *restrict dst, ALuint numsamples)
157 const __m128i increment4 = _mm_set1_epi32(increment*4);
158 const __m128i fracMask4 = _mm_set1_epi32(FRACTIONMASK);
159 union { alignas(16) ALuint i[4]; float f[4]; } pos_;
160 union { alignas(16) ALuint i[4]; float f[4]; } frac_;
161 __m128i frac4, pos4;
162 ALuint pos;
163 ALuint i, j;
165 InitiatePositionArrays(frac, increment, frac_.i, pos_.i, 4);
167 frac4 = _mm_castps_si128(_mm_load_ps(frac_.f));
168 pos4 = _mm_castps_si128(_mm_load_ps(pos_.f));
170 src -= 3;
171 for(i = 0;numsamples-i > 3;i += 4)
173 __m128 out[2];
174 for(j = 0;j < 8;j+=4)
176 const __m128 val0 = _mm_loadu_ps(&src[pos_.i[0]+j]);
177 const __m128 val1 = _mm_loadu_ps(&src[pos_.i[1]+j]);
178 const __m128 val2 = _mm_loadu_ps(&src[pos_.i[2]+j]);
179 const __m128 val3 = _mm_loadu_ps(&src[pos_.i[3]+j]);
180 __m128 k0 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[0]][j]);
181 __m128 k1 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[1]][j]);
182 __m128 k2 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[2]][j]);
183 __m128 k3 = _mm_load_ps(&ResampleCoeffs.FIR8[frac_.i[3]][j]);
185 k0 = _mm_mul_ps(k0, val0);
186 k1 = _mm_mul_ps(k1, val1);
187 k2 = _mm_mul_ps(k2, val2);
188 k3 = _mm_mul_ps(k3, val3);
189 k0 = _mm_hadd_ps(k0, k1);
190 k2 = _mm_hadd_ps(k2, k3);
191 out[j>>2] = _mm_hadd_ps(k0, k2);
194 out[0] = _mm_add_ps(out[0], out[1]);
195 _mm_store_ps(&dst[i], out[0]);
197 frac4 = _mm_add_epi32(frac4, increment4);
198 pos4 = _mm_add_epi32(pos4, _mm_srli_epi32(frac4, FRACTIONBITS));
199 frac4 = _mm_and_si128(frac4, fracMask4);
201 pos_.i[0] = _mm_extract_epi32(pos4, 0);
202 pos_.i[1] = _mm_extract_epi32(pos4, 1);
203 pos_.i[2] = _mm_extract_epi32(pos4, 2);
204 pos_.i[3] = _mm_extract_epi32(pos4, 3);
205 frac_.i[0] = _mm_extract_epi32(frac4, 0);
206 frac_.i[1] = _mm_extract_epi32(frac4, 1);
207 frac_.i[2] = _mm_extract_epi32(frac4, 2);
208 frac_.i[3] = _mm_extract_epi32(frac4, 3);
211 pos = pos_.i[0];
212 frac = frac_.i[0];
214 for(;i < numsamples;i++)
216 dst[i] = resample_fir8(src[pos ], src[pos+1], src[pos+2], src[pos+3],
217 src[pos+4], src[pos+5], src[pos+6], src[pos+7], frac);
219 frac += increment;
220 pos += frac>>FRACTIONBITS;
221 frac &= FRACTIONMASK;
223 return dst;