1 ////////////////////////////////////////////////////////////////////////////////
3 /// Win32 version of the SSE optimized routines for Pentium-III, Athlon-XP and
4 /// later. All SSE optimized functions have been gathered into this single source
5 /// code file, regardless to their class or original source code file, in order
6 /// to ease porting the library to other compiler and processor platforms.
8 /// NOTICE: If using Visual Studio 6.0, you'll need to install the "Visual C++
9 /// 6.0 processor pack" update to support SSE instruction set. The update is
10 /// available for download at Microsoft Developers Network, see here:
11 /// http://msdn.microsoft.com/vstudio/downloads/tools/ppack/default.aspx
13 /// If the above URL is expired or removed, go to "http://msdn.microsoft.com" and
14 /// perform a search with keywords "processor pack".
16 /// This file is to be compiled in Windows platform with Microsoft Visual C++
17 /// Compiler. Please see 'sse_gcc.cpp' for the gcc compiler version for all
18 /// GNU platforms (if file supplied).
20 /// Author : Copyright (c) Olli Parviainen
21 /// Author e-mail : oparviai @ iki.fi
22 /// SoundTouch WWW: http://www.iki.fi/oparviai/soundtouch
24 ////////////////////////////////////////////////////////////////////////////////
26 // Last changed : $Date$
27 // File revision : $Revision$
31 ////////////////////////////////////////////////////////////////////////////////
35 // SoundTouch audio processing library
36 // Copyright (c) Olli Parviainen
38 // This library is free software; you can redistribute it and/or
39 // modify it under the terms of the GNU Lesser General Public
40 // License as published by the Free Software Foundation; either
41 // version 2.1 of the License, or (at your option) any later version.
43 // This library is distributed in the hope that it will be useful,
44 // but WITHOUT ANY WARRANTY; without even the implied warranty of
45 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
46 // Lesser General Public License for more details.
48 // You should have received a copy of the GNU Lesser General Public
49 // License along with this library; if not, write to the Free Software
50 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
52 ////////////////////////////////////////////////////////////////////////////////
54 #include "cpu_detect.h"
58 #error "wrong platform - this source code file is exclusively for Win32 platform"
61 using namespace soundtouch;
64 // SSE routines available only with float sample type
66 //////////////////////////////////////////////////////////////////////////////
68 // implementation of SSE optimized functions of class 'TDStretchSSE'
70 //////////////////////////////////////////////////////////////////////////////
72 #include "TDStretch.h"
75 // these are declared in 'TDStretch.cpp'
76 extern int scanOffsets[4][24];
78 // Calculates cross correlation of two buffers
79 double TDStretchSSE::calcCrossCorrStereo(const float *pV1, const float *pV2) const
81 uint overlapLengthLocal = overlapLength;
88 // Calculates the cross-correlation value between 'pV1' and 'pV2' vectors
90 for (i = 0; i < overlapLength / 8; i ++)
92 corr += pV1[0] * pV2[0] +
116 // Very important note: data in 'pV2' _must_ be aligned to
119 // give prefetch hints to CPU of what data are to be needed soonish
120 // give more aggressive hints on pV1 as that changes while pV2 stays
124 prefetcht0 [pV1 + 32]
126 mov eax, dword ptr pV1
127 mov ebx, dword ptr pV2
131 mov ecx, overlapLengthLocal
132 shr ecx, 3 // div by eight
135 prefetcht0 [eax + 64] // give a prefetch hint to CPU what data are to be needed soonish
136 prefetcht0 [ebx + 32] // give a prefetch hint to CPU what data are to be needed soonish
141 movups xmm2, [eax + 16]
142 mulps xmm2, [ebx + 16]
145 prefetcht0 [eax + 96] // give a prefetch hint to CPU what data are to be needed soonish
146 prefetcht0 [ebx + 64] // give a prefetch hint to CPU what data are to be needed soonish
148 movups xmm3, [eax + 32]
149 mulps xmm3, [ebx + 32]
152 movups xmm4, [eax + 48]
153 mulps xmm4, [ebx + 48]
162 // add the four floats of xmm0 together and return the result.
164 movhlps xmm1, xmm0 // move 3 & 4 of xmm0 to 1 & 2 of xmm1
167 shufps xmm2, xmm2, 0x01 // move 2 of xmm2 as 1 of xmm2
176 //////////////////////////////////////////////////////////////////////////////
178 // implementation of SSE optimized functions of class 'FIRFilter'
180 //////////////////////////////////////////////////////////////////////////////
182 #include "FIRFilter.h"
184 FIRFilterSSE::FIRFilterSSE() : FIRFilter()
186 filterCoeffsUnalign = NULL;
190 FIRFilterSSE::~FIRFilterSSE()
192 delete[] filterCoeffsUnalign;
196 // (overloaded) Calculates filter coefficients for SSE routine
197 void FIRFilterSSE::setCoefficients(const float *coeffs, uint newLength, uint uResultDivFactor)
202 FIRFilter::setCoefficients(coeffs, newLength, uResultDivFactor);
204 // Scale the filter coefficients so that it won't be necessary to scale the filtering result
205 // also rearrange coefficients suitably for 3DNow!
206 // Ensure that filter coeffs array is aligned to 16-byte boundary
207 delete[] filterCoeffsUnalign;
208 filterCoeffsUnalign = new float[2 * newLength + 4];
209 filterCoeffsAlign = (float *)(((uint)filterCoeffsUnalign + 15) & -16);
211 fDivider = (float)resultDivider;
213 // rearrange the filter coefficients for mmx routines
214 for (i = 0; i < newLength; i ++)
216 filterCoeffsAlign[2 * i + 0] =
217 filterCoeffsAlign[2 * i + 1] = coeffs[i + 0] / fDivider;
223 // SSE-optimized version of the filter routine for stereo sound
224 uint FIRFilterSSE::evaluateFilterStereo(float *dest, const float *src, const uint numSamples) const
226 int count = (numSamples - length) & -2;
227 uint lengthLocal = length / 8;
228 float *filterCoeffsLocal = filterCoeffsAlign;
230 assert(count % 2 == 0);
232 if (count < 2) return 0;
239 for (j = 0; j < count; j += 2)
248 for (i = 0; i < lengthLocal; i ++)
250 // unroll loop for efficiency.
252 suml1 += ptr[0] * pFil[0] +
257 sumr1 += ptr[1] * pFil[1] +
262 suml2 += ptr[8] * pFil[0] +
267 sumr2 += ptr[9] * pFil[1] +
275 dest[0] = (float)suml1;
276 dest[1] = (float)sumr1;
277 dest[2] = (float)suml2;
278 dest[3] = (float)sumr2;
287 // Very important note: data in 'src' _must_ be aligned to
290 mov ebx, dword ptr src
291 mov eax, dword ptr dest
295 // "outer loop" : during each round 2*2 output samples are calculated
297 // give prefetch hints to CPU of what data are to be needed soonish
299 prefetcht0 [filterCoeffsLocal]
302 mov edi, filterCoeffsLocal
308 // "inner loop" : during each round eight FIR filter taps are evaluated for 2*2 samples
309 prefetcht0 [esi + 32] // give a prefetch hint to CPU what data are to be needed soonish
310 prefetcht0 [edi + 32] // give a prefetch hint to CPU what data are to be needed soonish
312 movups xmm2, [esi] // possibly unaligned load
313 movups xmm3, [esi + 8] // possibly unaligned load
319 movups xmm4, [esi + 16] // possibly unaligned load
320 movups xmm5, [esi + 24] // possibly unaligned load
321 mulps xmm4, [edi + 16]
322 mulps xmm5, [edi + 16]
326 prefetcht0 [esi + 64] // give a prefetch hint to CPU what data are to be needed soonish
327 prefetcht0 [edi + 64] // give a prefetch hint to CPU what data are to be needed soonish
329 movups xmm6, [esi + 32] // possibly unaligned load
330 movups xmm7, [esi + 40] // possibly unaligned load
331 mulps xmm6, [edi + 32]
332 mulps xmm7, [edi + 32]
336 movups xmm4, [esi + 48] // possibly unaligned load
337 movups xmm5, [esi + 56] // possibly unaligned load
338 mulps xmm4, [edi + 48]
339 mulps xmm5, [edi + 48]
348 // Now xmm0 and xmm1 both have a filtered 2-channel sample each, but we still need
349 // to sum the two hi- and lo-floats of these registers together.
351 movhlps xmm2, xmm0 // xmm2 = xmm2_3 xmm2_2 xmm0_3 xmm0_2
352 movlhps xmm2, xmm1 // xmm2 = xmm1_1 xmm1_0 xmm0_3 xmm0_2
353 shufps xmm0, xmm1, 0xe4 // xmm0 = xmm1_3 xmm1_2 xmm0_1 xmm0_0