mmx_optimized.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. // License :
  24. //
  25. // SoundTouch audio processing library
  26. // Copyright (c) Olli Parviainen
  27. //
  28. // This library is free software; you can redistribute it and/or
  29. // modify it under the terms of the GNU Lesser General Public
  30. // License as published by the Free Software Foundation; either
  31. // version 2.1 of the License, or (at your option) any later version.
  32. //
  33. // This library is distributed in the hope that it will be useful,
  34. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  36. // Lesser General Public License for more details.
  37. //
  38. // You should have received a copy of the GNU Lesser General Public
  39. // License along with this library; if not, write to the Free Software
  40. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  41. //
  42. ////////////////////////////////////////////////////////////////////////////////
  43. #include "STTypes.h"
  44. #ifdef SOUNDTOUCH_ALLOW_MMX
  45. // MMX routines available only with integer sample type
  46. using namespace soundtouch;
  47. //////////////////////////////////////////////////////////////////////////////
  48. //
  49. // implementation of MMX optimized functions of class 'TDStretchMMX'
  50. //
  51. //////////////////////////////////////////////////////////////////////////////
  52. #include "TDStretch.h"
  53. #include <mmintrin.h>
  54. #include <limits.h>
  55. #include <math.h>
  56. // Calculates cross correlation of two buffers
  57. double TDStretchMMX::calcCrossCorr(const short *pV1, const short *pV2, double &dnorm)
  58. {
  59. const __m64 *pVec1, *pVec2;
  60. __m64 shifter;
  61. __m64 accu, normaccu;
  62. long corr, norm;
  63. int i;
  64. pVec1 = (__m64*)pV1;
  65. pVec2 = (__m64*)pV2;
  66. shifter = _m_from_int(overlapDividerBitsNorm);
  67. normaccu = accu = _mm_setzero_si64();
  68. // Process 4 parallel sets of 2 * stereo samples or 4 * mono samples
  69. // during each round for improved CPU-level parallellization.
  70. for (i = 0; i < channels * overlapLength / 16; i ++)
  71. {
  72. __m64 temp, temp2;
  73. // dictionary of instructions:
  74. // _m_pmaddwd : 4*16bit multiply-add, resulting two 32bits = [a0*b0+a1*b1 ; a2*b2+a3*b3]
  75. // _mm_add_pi32 : 2*32bit add
  76. // _m_psrad : 32bit right-shift
  77. temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[0], pVec2[0]), shifter),
  78. _mm_sra_pi32(_mm_madd_pi16(pVec1[1], pVec2[1]), shifter));
  79. temp2 = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[0], pVec1[0]), shifter),
  80. _mm_sra_pi32(_mm_madd_pi16(pVec1[1], pVec1[1]), shifter));
  81. accu = _mm_add_pi32(accu, temp);
  82. normaccu = _mm_add_pi32(normaccu, temp2);
  83. temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[2], pVec2[2]), shifter),
  84. _mm_sra_pi32(_mm_madd_pi16(pVec1[3], pVec2[3]), shifter));
  85. temp2 = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[2], pVec1[2]), shifter),
  86. _mm_sra_pi32(_mm_madd_pi16(pVec1[3], pVec1[3]), shifter));
  87. accu = _mm_add_pi32(accu, temp);
  88. normaccu = _mm_add_pi32(normaccu, temp2);
  89. pVec1 += 4;
  90. pVec2 += 4;
  91. }
  92. // copy hi-dword of mm0 to lo-dword of mm1, then sum mmo+mm1
  93. // and finally store the result into the variable "corr"
  94. accu = _mm_add_pi32(accu, _mm_srli_si64(accu, 32));
  95. corr = _m_to_int(accu);
  96. normaccu = _mm_add_pi32(normaccu, _mm_srli_si64(normaccu, 32));
  97. norm = _m_to_int(normaccu);
  98. // Clear MMS state
  99. _m_empty();
  100. if (norm > (long)maxnorm)
  101. {
  102. // modify 'maxnorm' inside critical section to avoid multi-access conflict if in OpenMP mode
  103. #pragma omp critical
  104. if (norm > (long)maxnorm)
  105. {
  106. maxnorm = norm;
  107. }
  108. }
  109. // Normalize result by dividing by sqrt(norm) - this step is easiest
  110. // done using floating point operation
  111. dnorm = (double)norm;
  112. return (double)corr / sqrt(dnorm < 1e-9 ? 1.0 : dnorm);
  113. // Note: Warning about the missing EMMS instruction is harmless
  114. // as it'll be called elsewhere.
  115. }
  116. /// Update cross-correlation by accumulating "norm" coefficient by previously calculated value
  117. double TDStretchMMX::calcCrossCorrAccumulate(const short *pV1, const short *pV2, double &dnorm)
  118. {
  119. const __m64 *pVec1, *pVec2;
  120. __m64 shifter;
  121. __m64 accu;
  122. long corr, lnorm;
  123. int i;
  124. // cancel first normalizer tap from previous round
  125. lnorm = 0;
  126. for (i = 1; i <= channels; i ++)
  127. {
  128. lnorm -= (pV1[-i] * pV1[-i]) >> overlapDividerBitsNorm;
  129. }
  130. pVec1 = (__m64*)pV1;
  131. pVec2 = (__m64*)pV2;
  132. shifter = _m_from_int(overlapDividerBitsNorm);
  133. accu = _mm_setzero_si64();
  134. // Process 4 parallel sets of 2 * stereo samples or 4 * mono samples
  135. // during each round for improved CPU-level parallellization.
  136. for (i = 0; i < channels * overlapLength / 16; i ++)
  137. {
  138. __m64 temp;
  139. // dictionary of instructions:
  140. // _m_pmaddwd : 4*16bit multiply-add, resulting two 32bits = [a0*b0+a1*b1 ; a2*b2+a3*b3]
  141. // _mm_add_pi32 : 2*32bit add
  142. // _m_psrad : 32bit right-shift
  143. temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[0], pVec2[0]), shifter),
  144. _mm_sra_pi32(_mm_madd_pi16(pVec1[1], pVec2[1]), shifter));
  145. accu = _mm_add_pi32(accu, temp);
  146. temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[2], pVec2[2]), shifter),
  147. _mm_sra_pi32(_mm_madd_pi16(pVec1[3], pVec2[3]), shifter));
  148. accu = _mm_add_pi32(accu, temp);
  149. pVec1 += 4;
  150. pVec2 += 4;
  151. }
  152. // copy hi-dword of mm0 to lo-dword of mm1, then sum mmo+mm1
  153. // and finally store the result into the variable "corr"
  154. accu = _mm_add_pi32(accu, _mm_srli_si64(accu, 32));
  155. corr = _m_to_int(accu);
  156. // Clear MMS state
  157. _m_empty();
  158. // update normalizer with last samples of this round
  159. pV1 = (short *)pVec1;
  160. for (int j = 1; j <= channels; j ++)
  161. {
  162. lnorm += (pV1[-j] * pV1[-j]) >> overlapDividerBitsNorm;
  163. }
  164. dnorm += (double)lnorm;
  165. if (lnorm > (long)maxnorm)
  166. {
  167. maxnorm = lnorm;
  168. }
  169. // Normalize result by dividing by sqrt(norm) - this step is easiest
  170. // done using floating point operation
  171. return (double)corr / sqrt((dnorm < 1e-9) ? 1.0 : dnorm);
  172. }
  173. void TDStretchMMX::clearCrossCorrState()
  174. {
  175. // Clear MMS state
  176. _m_empty();
  177. //_asm EMMS;
  178. }
  179. // MMX-optimized version of the function overlapStereo
  180. void TDStretchMMX::overlapStereo(short *output, const short *input) const
  181. {
  182. const __m64 *pVinput, *pVMidBuf;
  183. __m64 *pVdest;
  184. __m64 mix1, mix2, adder, shifter;
  185. int i;
  186. pVinput = (const __m64*)input;
  187. pVMidBuf = (const __m64*)pMidBuffer;
  188. pVdest = (__m64*)output;
  189. // mix1 = mixer values for 1st stereo sample
  190. // mix1 = mixer values for 2nd stereo sample
  191. // adder = adder for updating mixer values after each round
  192. mix1 = _mm_set_pi16(0, overlapLength, 0, overlapLength);
  193. adder = _mm_set_pi16(1, -1, 1, -1);
  194. mix2 = _mm_add_pi16(mix1, adder);
  195. adder = _mm_add_pi16(adder, adder);
  196. // Overlaplength-division by shifter. "+1" is to account for "-1" deduced in
  197. // overlapDividerBits calculation earlier.
  198. shifter = _m_from_int(overlapDividerBitsPure + 1);
  199. for (i = 0; i < overlapLength / 4; i ++)
  200. {
  201. __m64 temp1, temp2;
  202. // load & shuffle data so that input & mixbuffer data samples are paired
  203. temp1 = _mm_unpacklo_pi16(pVMidBuf[0], pVinput[0]); // = i0l m0l i0r m0r
  204. temp2 = _mm_unpackhi_pi16(pVMidBuf[0], pVinput[0]); // = i1l m1l i1r m1r
  205. // temp = (temp .* mix) >> shifter
  206. temp1 = _mm_sra_pi32(_mm_madd_pi16(temp1, mix1), shifter);
  207. temp2 = _mm_sra_pi32(_mm_madd_pi16(temp2, mix2), shifter);
  208. pVdest[0] = _mm_packs_pi32(temp1, temp2); // pack 2*2*32bit => 4*16bit
  209. // update mix += adder
  210. mix1 = _mm_add_pi16(mix1, adder);
  211. mix2 = _mm_add_pi16(mix2, adder);
  212. // --- second round begins here ---
  213. // load & shuffle data so that input & mixbuffer data samples are paired
  214. temp1 = _mm_unpacklo_pi16(pVMidBuf[1], pVinput[1]); // = i2l m2l i2r m2r
  215. temp2 = _mm_unpackhi_pi16(pVMidBuf[1], pVinput[1]); // = i3l m3l i3r m3r
  216. // temp = (temp .* mix) >> shifter
  217. temp1 = _mm_sra_pi32(_mm_madd_pi16(temp1, mix1), shifter);
  218. temp2 = _mm_sra_pi32(_mm_madd_pi16(temp2, mix2), shifter);
  219. pVdest[1] = _mm_packs_pi32(temp1, temp2); // pack 2*2*32bit => 4*16bit
  220. // update mix += adder
  221. mix1 = _mm_add_pi16(mix1, adder);
  222. mix2 = _mm_add_pi16(mix2, adder);
  223. pVinput += 2;
  224. pVMidBuf += 2;
  225. pVdest += 2;
  226. }
  227. _m_empty(); // clear MMS state
  228. }
  229. //////////////////////////////////////////////////////////////////////////////
  230. //
  231. // implementation of MMX optimized functions of class 'FIRFilter'
  232. //
  233. //////////////////////////////////////////////////////////////////////////////
  234. #include "FIRFilter.h"
  235. FIRFilterMMX::FIRFilterMMX() : FIRFilter()
  236. {
  237. filterCoeffsAlign = NULL;
  238. filterCoeffsUnalign = NULL;
  239. }
  240. FIRFilterMMX::~FIRFilterMMX()
  241. {
  242. delete[] filterCoeffsUnalign;
  243. }
  244. // (overloaded) Calculates filter coefficients for MMX routine
  245. void FIRFilterMMX::setCoefficients(const short *coeffs, uint newLength, uint uResultDivFactor)
  246. {
  247. uint i;
  248. FIRFilter::setCoefficients(coeffs, newLength, uResultDivFactor);
  249. // Ensure that filter coeffs array is aligned to 16-byte boundary
  250. delete[] filterCoeffsUnalign;
  251. filterCoeffsUnalign = new short[2 * newLength + 8];
  252. filterCoeffsAlign = (short *)SOUNDTOUCH_ALIGN_POINTER_16(filterCoeffsUnalign);
  253. // rearrange the filter coefficients for mmx routines
  254. for (i = 0;i < length; i += 4)
  255. {
  256. filterCoeffsAlign[2 * i + 0] = coeffs[i + 0];
  257. filterCoeffsAlign[2 * i + 1] = coeffs[i + 2];
  258. filterCoeffsAlign[2 * i + 2] = coeffs[i + 0];
  259. filterCoeffsAlign[2 * i + 3] = coeffs[i + 2];
  260. filterCoeffsAlign[2 * i + 4] = coeffs[i + 1];
  261. filterCoeffsAlign[2 * i + 5] = coeffs[i + 3];
  262. filterCoeffsAlign[2 * i + 6] = coeffs[i + 1];
  263. filterCoeffsAlign[2 * i + 7] = coeffs[i + 3];
  264. }
  265. }
  266. // mmx-optimized version of the filter routine for stereo sound
  267. uint FIRFilterMMX::evaluateFilterStereo(short *dest, const short *src, uint numSamples) const
  268. {
  269. // Create stack copies of the needed member variables for asm routines :
  270. uint i, j;
  271. __m64 *pVdest = (__m64*)dest;
  272. if (length < 2) return 0;
  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. accu1 = accu2 = _mm_setzero_si64();
  280. for (j = 0; j < lengthDiv8 * 2; j ++)
  281. {
  282. __m64 temp1, temp2;
  283. temp1 = _mm_unpacklo_pi16(pVsrc[0], pVsrc[1]); // = l2 l0 r2 r0
  284. temp2 = _mm_unpackhi_pi16(pVsrc[0], pVsrc[1]); // = l3 l1 r3 r1
  285. accu1 = _mm_add_pi32(accu1, _mm_madd_pi16(temp1, pVfilter[0])); // += l2*f2+l0*f0 r2*f2+r0*f0
  286. accu1 = _mm_add_pi32(accu1, _mm_madd_pi16(temp2, pVfilter[1])); // += l3*f3+l1*f1 r3*f3+r1*f1
  287. temp1 = _mm_unpacklo_pi16(pVsrc[1], pVsrc[2]); // = l4 l2 r4 r2
  288. accu2 = _mm_add_pi32(accu2, _mm_madd_pi16(temp2, pVfilter[0])); // += l3*f2+l1*f0 r3*f2+r1*f0
  289. accu2 = _mm_add_pi32(accu2, _mm_madd_pi16(temp1, pVfilter[1])); // += l4*f3+l2*f1 r4*f3+r2*f1
  290. // accu1 += l2*f2+l0*f0 r2*f2+r0*f0
  291. // += l3*f3+l1*f1 r3*f3+r1*f1
  292. // accu2 += l3*f2+l1*f0 r3*f2+r1*f0
  293. // l4*f3+l2*f1 r4*f3+r2*f1
  294. pVfilter += 2;
  295. pVsrc += 2;
  296. }
  297. // accu >>= resultDivFactor
  298. accu1 = _mm_srai_pi32(accu1, resultDivFactor);
  299. accu2 = _mm_srai_pi32(accu2, resultDivFactor);
  300. // pack 2*2*32bits => 4*16 bits
  301. pVdest[0] = _mm_packs_pi32(accu1, accu2);
  302. src += 4;
  303. pVdest ++;
  304. }
  305. _m_empty(); // clear emms state
  306. return (numSamples & 0xfffffffe) - length;
  307. }
  308. #else
  309. // workaround to not complain about empty module
  310. bool _dontcomplain_mmx_empty;
  311. #endif // SOUNDTOUCH_ALLOW_MMX