~alex.barker/mixxx/mixxx-libs

« back to all changes in this revision

Viewing changes to mixxx/lib/soundtouch-1.6.0/mmx_optimized.cpp

  • Committer: Alex Barker
  • Date: 2011-07-24 21:06:29 UTC
  • Revision ID: alex@1stleg.com-20110724210629-zyr2khq2o0nv8iru
Updated libsoundtouch and fidlib to latest versions

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////////
 
2
///
 
3
/// MMX optimized routines. All MMX optimized functions have been gathered into 
 
4
/// this single source code file, regardless to their class or original source 
 
5
/// code file, in order to ease porting the library to other compiler and 
 
6
/// processor platforms.
 
7
///
 
8
/// The MMX-optimizations are programmed using MMX compiler intrinsics that
 
9
/// are supported both by Microsoft Visual C++ and GCC compilers, so this file
 
10
/// should compile with both toolsets.
 
11
///
 
12
/// NOTICE: If using Visual Studio 6.0, you'll need to install the "Visual C++ 
 
13
/// 6.0 processor pack" update to support compiler intrinsic syntax. The update
 
14
/// is available for download at Microsoft Developers Network, see here:
 
15
/// http://msdn.microsoft.com/en-us/vstudio/aa718349.aspx
 
16
///
 
17
/// Author        : Copyright (c) Olli Parviainen
 
18
/// Author e-mail : oparviai 'at' iki.fi
 
19
/// SoundTouch WWW: http://www.surina.net/soundtouch
 
20
///
 
21
////////////////////////////////////////////////////////////////////////////////
 
22
//
 
23
// Last changed  : $Date: 2011-02-13 21:13:57 +0200 (Sun, 13 Feb 2011) $
 
24
// File revision : $Revision: 4 $
 
25
//
 
26
// $Id: mmx_optimized.cpp 104 2011-02-13 19:13:57Z oparviai $
 
27
//
 
28
////////////////////////////////////////////////////////////////////////////////
 
29
//
 
30
// License :
 
31
//
 
32
//  SoundTouch audio processing library
 
33
//  Copyright (c) Olli Parviainen
 
34
//
 
35
//  This library is free software; you can redistribute it and/or
 
36
//  modify it under the terms of the GNU Lesser General Public
 
37
//  License as published by the Free Software Foundation; either
 
38
//  version 2.1 of the License, or (at your option) any later version.
 
39
//
 
40
//  This library is distributed in the hope that it will be useful,
 
41
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
42
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
43
//  Lesser General Public License for more details.
 
44
//
 
45
//  You should have received a copy of the GNU Lesser General Public
 
46
//  License along with this library; if not, write to the Free Software
 
47
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
48
//
 
49
////////////////////////////////////////////////////////////////////////////////
 
50
 
 
51
#include "STTypes.h"
 
52
 
 
53
#ifdef SOUNDTOUCH_ALLOW_MMX
 
54
// MMX routines available only with integer sample type
 
55
 
 
56
#if !(WIN32 || __i386__ || __x86_64__)
 
57
#error "wrong platform - this source code file is exclusively for x86 platforms"
 
58
#endif
 
59
 
 
60
using namespace soundtouch;
 
61
 
 
62
//////////////////////////////////////////////////////////////////////////////
 
63
//
 
64
// implementation of MMX optimized functions of class 'TDStretchMMX'
 
65
//
 
66
//////////////////////////////////////////////////////////////////////////////
 
67
 
 
68
#include "TDStretch.h"
 
69
#include <mmintrin.h>
 
70
#include <limits.h>
 
71
#include <math.h>
 
72
 
 
73
 
 
74
// Calculates cross correlation of two buffers
 
75
long TDStretchMMX::calcCrossCorrStereo(const short *pV1, const short *pV2) const
 
76
{
 
77
    const __m64 *pVec1, *pVec2;
 
78
    __m64 shifter;
 
79
    __m64 accu, normaccu;
 
80
    long corr, norm;
 
81
    int i;
 
82
   
 
83
    pVec1 = (__m64*)pV1;
 
84
    pVec2 = (__m64*)pV2;
 
85
 
 
86
    shifter = _m_from_int(overlapDividerBits);
 
87
    normaccu = accu = _mm_setzero_si64();
 
88
 
 
89
    // Process 4 parallel sets of 2 * stereo samples each during each 
 
90
    // round to improve CPU-level parallellization.
 
91
    for (i = 0; i < overlapLength / 8; i ++)
 
92
    {
 
93
        __m64 temp, temp2;
 
94
 
 
95
        // dictionary of instructions:
 
96
        // _m_pmaddwd   : 4*16bit multiply-add, resulting two 32bits = [a0*b0+a1*b1 ; a2*b2+a3*b3]
 
97
        // _mm_add_pi32 : 2*32bit add
 
98
        // _m_psrad     : 32bit right-shift
 
99
 
 
100
        temp = _mm_add_pi32(_mm_madd_pi16(pVec1[0], pVec2[0]),
 
101
                            _mm_madd_pi16(pVec1[1], pVec2[1]));
 
102
        temp2 = _mm_add_pi32(_mm_madd_pi16(pVec1[0], pVec1[0]),
 
103
                             _mm_madd_pi16(pVec1[1], pVec1[1]));
 
104
        accu = _mm_add_pi32(accu, _mm_sra_pi32(temp, shifter));
 
105
        normaccu = _mm_add_pi32(normaccu, _mm_sra_pi32(temp2, shifter));
 
106
 
 
107
        temp = _mm_add_pi32(_mm_madd_pi16(pVec1[2], pVec2[2]),
 
108
                            _mm_madd_pi16(pVec1[3], pVec2[3]));
 
109
        temp2 = _mm_add_pi32(_mm_madd_pi16(pVec1[2], pVec1[2]),
 
110
                             _mm_madd_pi16(pVec1[3], pVec1[3]));
 
111
        accu = _mm_add_pi32(accu, _mm_sra_pi32(temp, shifter));
 
112
        normaccu = _mm_add_pi32(normaccu, _mm_sra_pi32(temp2, shifter));
 
113
 
 
114
        pVec1 += 4;
 
115
        pVec2 += 4;
 
116
    }
 
117
 
 
118
    // copy hi-dword of mm0 to lo-dword of mm1, then sum mmo+mm1
 
119
    // and finally store the result into the variable "corr"
 
120
 
 
121
    accu = _mm_add_pi32(accu, _mm_srli_si64(accu, 32));
 
122
    corr = _m_to_int(accu);
 
123
 
 
124
    normaccu = _mm_add_pi32(normaccu, _mm_srli_si64(normaccu, 32));
 
125
    norm = _m_to_int(normaccu);
 
126
 
 
127
    // Clear MMS state
 
128
    _m_empty();
 
129
 
 
130
    // Normalize result by dividing by sqrt(norm) - this step is easiest 
 
131
    // done using floating point operation
 
132
    if (norm == 0) norm = 1;    // to avoid div by zero
 
133
    return (long)((double)corr * USHRT_MAX / sqrt((double)norm));
 
134
    // Note: Warning about the missing EMMS instruction is harmless
 
135
    // as it'll be called elsewhere.
 
136
}
 
137
 
 
138
 
 
139
 
 
140
void TDStretchMMX::clearCrossCorrState()
 
141
{
 
142
    // Clear MMS state
 
143
    _m_empty();
 
144
    //_asm EMMS;
 
145
}
 
146
 
 
147
 
 
148
 
 
149
// MMX-optimized version of the function overlapStereo
 
150
void TDStretchMMX::overlapStereo(short *output, const short *input) const
 
151
{
 
152
    const __m64 *pVinput, *pVMidBuf;
 
153
    __m64 *pVdest;
 
154
    __m64 mix1, mix2, adder, shifter;
 
155
    int i;
 
156
 
 
157
    pVinput  = (const __m64*)input;
 
158
    pVMidBuf = (const __m64*)pMidBuffer;
 
159
    pVdest   = (__m64*)output;
 
160
 
 
161
    // mix1  = mixer values for 1st stereo sample
 
162
    // mix1  = mixer values for 2nd stereo sample
 
163
    // adder = adder for updating mixer values after each round
 
164
    
 
165
    mix1  = _mm_set_pi16(0, overlapLength,   0, overlapLength);
 
166
    adder = _mm_set_pi16(1, -1, 1, -1);
 
167
    mix2  = _mm_add_pi16(mix1, adder);
 
168
    adder = _mm_add_pi16(adder, adder);
 
169
 
 
170
    // Overlaplength-division by shifter. "+1" is to account for "-1" deduced in
 
171
    // overlapDividerBits calculation earlier.
 
172
    shifter = _m_from_int(overlapDividerBits + 1);
 
173
 
 
174
    for (i = 0; i < overlapLength / 4; i ++)
 
175
    {
 
176
        __m64 temp1, temp2;
 
177
                
 
178
        // load & shuffle data so that input & mixbuffer data samples are paired
 
179
        temp1 = _mm_unpacklo_pi16(pVMidBuf[0], pVinput[0]);     // = i0l m0l i0r m0r
 
180
        temp2 = _mm_unpackhi_pi16(pVMidBuf[0], pVinput[0]);     // = i1l m1l i1r m1r
 
181
 
 
182
        // temp = (temp .* mix) >> shifter
 
183
        temp1 = _mm_sra_pi32(_mm_madd_pi16(temp1, mix1), shifter);
 
184
        temp2 = _mm_sra_pi32(_mm_madd_pi16(temp2, mix2), shifter);
 
185
        pVdest[0] = _mm_packs_pi32(temp1, temp2); // pack 2*2*32bit => 4*16bit
 
186
 
 
187
        // update mix += adder
 
188
        mix1 = _mm_add_pi16(mix1, adder);
 
189
        mix2 = _mm_add_pi16(mix2, adder);
 
190
 
 
191
        // --- second round begins here ---
 
192
 
 
193
        // load & shuffle data so that input & mixbuffer data samples are paired
 
194
        temp1 = _mm_unpacklo_pi16(pVMidBuf[1], pVinput[1]);       // = i2l m2l i2r m2r
 
195
        temp2 = _mm_unpackhi_pi16(pVMidBuf[1], pVinput[1]);       // = i3l m3l i3r m3r
 
196
 
 
197
        // temp = (temp .* mix) >> shifter
 
198
        temp1 = _mm_sra_pi32(_mm_madd_pi16(temp1, mix1), shifter);
 
199
        temp2 = _mm_sra_pi32(_mm_madd_pi16(temp2, mix2), shifter);
 
200
        pVdest[1] = _mm_packs_pi32(temp1, temp2); // pack 2*2*32bit => 4*16bit
 
201
 
 
202
        // update mix += adder
 
203
        mix1 = _mm_add_pi16(mix1, adder);
 
204
        mix2 = _mm_add_pi16(mix2, adder);
 
205
 
 
206
        pVinput  += 2;
 
207
        pVMidBuf += 2;
 
208
        pVdest   += 2;
 
209
    }
 
210
 
 
211
    _m_empty(); // clear MMS state
 
212
}
 
213
 
 
214
 
 
215
//////////////////////////////////////////////////////////////////////////////
 
216
//
 
217
// implementation of MMX optimized functions of class 'FIRFilter'
 
218
//
 
219
//////////////////////////////////////////////////////////////////////////////
 
220
 
 
221
#include "FIRFilter.h"
 
222
 
 
223
 
 
224
FIRFilterMMX::FIRFilterMMX() : FIRFilter()
 
225
{
 
226
    filterCoeffsUnalign = NULL;
 
227
}
 
228
 
 
229
 
 
230
FIRFilterMMX::~FIRFilterMMX()
 
231
{
 
232
    delete[] filterCoeffsUnalign;
 
233
}
 
234
 
 
235
 
 
236
// (overloaded) Calculates filter coefficients for MMX routine
 
237
void FIRFilterMMX::setCoefficients(const short *coeffs, uint newLength, uint uResultDivFactor)
 
238
{
 
239
    uint i;
 
240
    FIRFilter::setCoefficients(coeffs, newLength, uResultDivFactor);
 
241
 
 
242
    // Ensure that filter coeffs array is aligned to 16-byte boundary
 
243
    delete[] filterCoeffsUnalign;
 
244
    filterCoeffsUnalign = new short[2 * newLength + 8];
 
245
    filterCoeffsAlign = (short *)(((ulong)filterCoeffsUnalign + 15) & -16);
 
246
 
 
247
    // rearrange the filter coefficients for mmx routines 
 
248
    for (i = 0;i < length; i += 4) 
 
249
    {
 
250
        filterCoeffsAlign[2 * i + 0] = coeffs[i + 0];
 
251
        filterCoeffsAlign[2 * i + 1] = coeffs[i + 2];
 
252
        filterCoeffsAlign[2 * i + 2] = coeffs[i + 0];
 
253
        filterCoeffsAlign[2 * i + 3] = coeffs[i + 2];
 
254
 
 
255
        filterCoeffsAlign[2 * i + 4] = coeffs[i + 1];
 
256
        filterCoeffsAlign[2 * i + 5] = coeffs[i + 3];
 
257
        filterCoeffsAlign[2 * i + 6] = coeffs[i + 1];
 
258
        filterCoeffsAlign[2 * i + 7] = coeffs[i + 3];
 
259
    }
 
260
}
 
261
 
 
262
 
 
263
 
 
264
// mmx-optimized version of the filter routine for stereo sound
 
265
uint FIRFilterMMX::evaluateFilterStereo(short *dest, const short *src, uint numSamples) const
 
266
{
 
267
    // Create stack copies of the needed member variables for asm routines :
 
268
    uint i, j;
 
269
    __m64 *pVdest = (__m64*)dest;
 
270
 
 
271
    if (length < 2) return 0;
 
272
 
 
273
    for (i = 0; i < (numSamples - length) / 2; i ++)
 
274
    {
 
275
        __m64 accu1;
 
276
        __m64 accu2;
 
277
        const __m64 *pVsrc = (const __m64*)src;
 
278
        const __m64 *pVfilter = (const __m64*)filterCoeffsAlign;
 
279
 
 
280
        accu1 = accu2 = _mm_setzero_si64();
 
281
        for (j = 0; j < lengthDiv8 * 2; j ++)
 
282
        {
 
283
            __m64 temp1, temp2;
 
284
 
 
285
            temp1 = _mm_unpacklo_pi16(pVsrc[0], pVsrc[1]);  // = l2 l0 r2 r0
 
286
            temp2 = _mm_unpackhi_pi16(pVsrc[0], pVsrc[1]);  // = l3 l1 r3 r1
 
287
 
 
288
            accu1 = _mm_add_pi32(accu1, _mm_madd_pi16(temp1, pVfilter[0]));  // += l2*f2+l0*f0 r2*f2+r0*f0
 
289
            accu1 = _mm_add_pi32(accu1, _mm_madd_pi16(temp2, pVfilter[1]));  // += l3*f3+l1*f1 r3*f3+r1*f1
 
290
 
 
291
            temp1 = _mm_unpacklo_pi16(pVsrc[1], pVsrc[2]);  // = l4 l2 r4 r2
 
292
 
 
293
            accu2 = _mm_add_pi32(accu2, _mm_madd_pi16(temp2, pVfilter[0]));  // += l3*f2+l1*f0 r3*f2+r1*f0
 
294
            accu2 = _mm_add_pi32(accu2, _mm_madd_pi16(temp1, pVfilter[1]));  // += l4*f3+l2*f1 r4*f3+r2*f1
 
295
 
 
296
            // accu1 += l2*f2+l0*f0 r2*f2+r0*f0
 
297
            //       += l3*f3+l1*f1 r3*f3+r1*f1
 
298
 
 
299
            // accu2 += l3*f2+l1*f0 r3*f2+r1*f0
 
300
            //          l4*f3+l2*f1 r4*f3+r2*f1
 
301
 
 
302
            pVfilter += 2;
 
303
            pVsrc += 2;
 
304
        }
 
305
        // accu >>= resultDivFactor
 
306
        accu1 = _mm_srai_pi32(accu1, resultDivFactor);
 
307
        accu2 = _mm_srai_pi32(accu2, resultDivFactor);
 
308
 
 
309
        // pack 2*2*32bits => 4*16 bits
 
310
        pVdest[0] = _mm_packs_pi32(accu1, accu2);
 
311
        src += 4;
 
312
        pVdest ++;
 
313
    }
 
314
 
 
315
   _m_empty();  // clear emms state
 
316
 
 
317
    return (numSamples & 0xfffffffe) - length;
 
318
}
 
319
 
 
320
#endif  // SOUNDTOUCH_ALLOW_MMX