mmx_optimized.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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: 2012-11-08 20:53:01 +0200 (Thu, 08 Nov 2012) $
  24. // File revision : $Revision: 4 $
  25. //
  26. // $Id: mmx_optimized.cpp 160 2012-11-08 18:53:01Z 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. #include "STTypes.h"
  51. #ifdef SOUNDTOUCH_ALLOW_MMX
  52. // MMX routines available only with integer sample type
  53. using namespace soundtouch;
  54. //////////////////////////////////////////////////////////////////////////////
  55. //
  56. // implementation of MMX optimized functions of class 'TDStretchMMX'
  57. //
  58. //////////////////////////////////////////////////////////////////////////////
  59. #include "TDStretch.h"
  60. #include <mmintrin.h>
  61. #include <limits.h>
  62. #include <math.h>
  63. // Calculates cross correlation of two buffers
  64. double TDStretchMMX::calcCrossCorr(const short *pV1, const short *pV2) const
  65. {
  66. const __m64 *pVec1, *pVec2;
  67. __m64 shifter;
  68. __m64 accu, normaccu;
  69. long corr, norm;
  70. int i;
  71. pVec1 = (__m64*)pV1;
  72. pVec2 = (__m64*)pV2;
  73. shifter = _m_from_int(overlapDividerBits);
  74. normaccu = accu = _mm_setzero_si64();
  75. // Process 4 parallel sets of 2 * stereo samples or 4 * mono samples
  76. // during each round for improved CPU-level parallellization.
  77. for (i = 0; i < channels * overlapLength / 16; i ++)
  78. {
  79. __m64 temp, temp2;
  80. // dictionary of instructions:
  81. // _m_pmaddwd : 4*16bit multiply-add, resulting two 32bits = [a0*b0+a1*b1 ; a2*b2+a3*b3]
  82. // _mm_add_pi32 : 2*32bit add
  83. // _m_psrad : 32bit right-shift
  84. temp = _mm_add_pi32(_mm_madd_pi16(pVec1[0], pVec2[0]),
  85. _mm_madd_pi16(pVec1[1], pVec2[1]));
  86. temp2 = _mm_add_pi32(_mm_madd_pi16(pVec1[0], pVec1[0]),
  87. _mm_madd_pi16(pVec1[1], pVec1[1]));
  88. accu = _mm_add_pi32(accu, _mm_sra_pi32(temp, shifter));
  89. normaccu = _mm_add_pi32(normaccu, _mm_sra_pi32(temp2, shifter));
  90. temp = _mm_add_pi32(_mm_madd_pi16(pVec1[2], pVec2[2]),
  91. _mm_madd_pi16(pVec1[3], pVec2[3]));
  92. temp2 = _mm_add_pi32(_mm_madd_pi16(pVec1[2], pVec1[2]),
  93. _mm_madd_pi16(pVec1[3], pVec1[3]));
  94. accu = _mm_add_pi32(accu, _mm_sra_pi32(temp, shifter));
  95. normaccu = _mm_add_pi32(normaccu, _mm_sra_pi32(temp2, shifter));
  96. pVec1 += 4;
  97. pVec2 += 4;
  98. }
  99. // copy hi-dword of mm0 to lo-dword of mm1, then sum mmo+mm1
  100. // and finally store the result into the variable "corr"
  101. accu = _mm_add_pi32(accu, _mm_srli_si64(accu, 32));
  102. corr = _m_to_int(accu);
  103. normaccu = _mm_add_pi32(normaccu, _mm_srli_si64(normaccu, 32));
  104. norm = _m_to_int(normaccu);
  105. // Clear MMS state
  106. _m_empty();
  107. // Normalize result by dividing by sqrt(norm) - this step is easiest
  108. // done using floating point operation
  109. if (norm == 0) norm = 1; // to avoid div by zero
  110. return (double)corr / sqrt((double)norm);
  111. // Note: Warning about the missing EMMS instruction is harmless
  112. // as it'll be called elsewhere.
  113. }
  114. void TDStretchMMX::clearCrossCorrState()
  115. {
  116. // Clear MMS state
  117. _m_empty();
  118. //_asm EMMS;
  119. }
  120. // MMX-optimized version of the function overlapStereo
  121. void TDStretchMMX::overlapStereo(short *output, const short *input) const
  122. {
  123. const __m64 *pVinput, *pVMidBuf;
  124. __m64 *pVdest;
  125. __m64 mix1, mix2, adder, shifter;
  126. int i;
  127. pVinput = (const __m64*)input;
  128. pVMidBuf = (const __m64*)pMidBuffer;
  129. pVdest = (__m64*)output;
  130. // mix1 = mixer values for 1st stereo sample
  131. // mix1 = mixer values for 2nd stereo sample
  132. // adder = adder for updating mixer values after each round
  133. mix1 = _mm_set_pi16(0, overlapLength, 0, overlapLength);
  134. adder = _mm_set_pi16(1, -1, 1, -1);
  135. mix2 = _mm_add_pi16(mix1, adder);
  136. adder = _mm_add_pi16(adder, adder);
  137. // Overlaplength-division by shifter. "+1" is to account for "-1" deduced in
  138. // overlapDividerBits calculation earlier.
  139. shifter = _m_from_int(overlapDividerBits + 1);
  140. for (i = 0; i < overlapLength / 4; i ++)
  141. {
  142. __m64 temp1, temp2;
  143. // load & shuffle data so that input & mixbuffer data samples are paired
  144. temp1 = _mm_unpacklo_pi16(pVMidBuf[0], pVinput[0]); // = i0l m0l i0r m0r
  145. temp2 = _mm_unpackhi_pi16(pVMidBuf[0], pVinput[0]); // = i1l m1l i1r m1r
  146. // temp = (temp .* mix) >> shifter
  147. temp1 = _mm_sra_pi32(_mm_madd_pi16(temp1, mix1), shifter);
  148. temp2 = _mm_sra_pi32(_mm_madd_pi16(temp2, mix2), shifter);
  149. pVdest[0] = _mm_packs_pi32(temp1, temp2); // pack 2*2*32bit => 4*16bit
  150. // update mix += adder
  151. mix1 = _mm_add_pi16(mix1, adder);
  152. mix2 = _mm_add_pi16(mix2, adder);
  153. // --- second round begins here ---
  154. // load & shuffle data so that input & mixbuffer data samples are paired
  155. temp1 = _mm_unpacklo_pi16(pVMidBuf[1], pVinput[1]); // = i2l m2l i2r m2r
  156. temp2 = _mm_unpackhi_pi16(pVMidBuf[1], pVinput[1]); // = i3l m3l i3r m3r
  157. // temp = (temp .* mix) >> shifter
  158. temp1 = _mm_sra_pi32(_mm_madd_pi16(temp1, mix1), shifter);
  159. temp2 = _mm_sra_pi32(_mm_madd_pi16(temp2, mix2), shifter);
  160. pVdest[1] = _mm_packs_pi32(temp1, temp2); // pack 2*2*32bit => 4*16bit
  161. // update mix += adder
  162. mix1 = _mm_add_pi16(mix1, adder);
  163. mix2 = _mm_add_pi16(mix2, adder);
  164. pVinput += 2;
  165. pVMidBuf += 2;
  166. pVdest += 2;
  167. }
  168. _m_empty(); // clear MMS state
  169. }
  170. //////////////////////////////////////////////////////////////////////////////
  171. //
  172. // implementation of MMX optimized functions of class 'FIRFilter'
  173. //
  174. //////////////////////////////////////////////////////////////////////////////
  175. #include "FIRFilter.h"
  176. FIRFilterMMX::FIRFilterMMX() : FIRFilter()
  177. {
  178. filterCoeffsUnalign = NULL;
  179. }
  180. FIRFilterMMX::~FIRFilterMMX()
  181. {
  182. delete[] filterCoeffsUnalign;
  183. }
  184. // (overloaded) Calculates filter coefficients for MMX routine
  185. void FIRFilterMMX::setCoefficients(const short *coeffs, uint newLength, uint uResultDivFactor)
  186. {
  187. uint i;
  188. FIRFilter::setCoefficients(coeffs, newLength, uResultDivFactor);
  189. // Ensure that filter coeffs array is aligned to 16-byte boundary
  190. delete[] filterCoeffsUnalign;
  191. filterCoeffsUnalign = new short[2 * newLength + 8];
  192. filterCoeffsAlign = (short *)SOUNDTOUCH_ALIGN_POINTER_16(filterCoeffsUnalign);
  193. // rearrange the filter coefficients for mmx routines
  194. for (i = 0;i < length; i += 4)
  195. {
  196. filterCoeffsAlign[2 * i + 0] = coeffs[i + 0];
  197. filterCoeffsAlign[2 * i + 1] = coeffs[i + 2];
  198. filterCoeffsAlign[2 * i + 2] = coeffs[i + 0];
  199. filterCoeffsAlign[2 * i + 3] = coeffs[i + 2];
  200. filterCoeffsAlign[2 * i + 4] = coeffs[i + 1];
  201. filterCoeffsAlign[2 * i + 5] = coeffs[i + 3];
  202. filterCoeffsAlign[2 * i + 6] = coeffs[i + 1];
  203. filterCoeffsAlign[2 * i + 7] = coeffs[i + 3];
  204. }
  205. }
  206. // mmx-optimized version of the filter routine for stereo sound
  207. uint FIRFilterMMX::evaluateFilterStereo(short *dest, const short *src, uint numSamples) const
  208. {
  209. // Create stack copies of the needed member variables for asm routines :
  210. uint i, j;
  211. __m64 *pVdest = (__m64*)dest;
  212. if (length < 2) return 0;
  213. for (i = 0; i < (numSamples - length) / 2; i ++)
  214. {
  215. __m64 accu1;
  216. __m64 accu2;
  217. const __m64 *pVsrc = (const __m64*)src;
  218. const __m64 *pVfilter = (const __m64*)filterCoeffsAlign;
  219. accu1 = accu2 = _mm_setzero_si64();
  220. for (j = 0; j < lengthDiv8 * 2; j ++)
  221. {
  222. __m64 temp1, temp2;
  223. temp1 = _mm_unpacklo_pi16(pVsrc[0], pVsrc[1]); // = l2 l0 r2 r0
  224. temp2 = _mm_unpackhi_pi16(pVsrc[0], pVsrc[1]); // = l3 l1 r3 r1
  225. accu1 = _mm_add_pi32(accu1, _mm_madd_pi16(temp1, pVfilter[0])); // += l2*f2+l0*f0 r2*f2+r0*f0
  226. accu1 = _mm_add_pi32(accu1, _mm_madd_pi16(temp2, pVfilter[1])); // += l3*f3+l1*f1 r3*f3+r1*f1
  227. temp1 = _mm_unpacklo_pi16(pVsrc[1], pVsrc[2]); // = l4 l2 r4 r2
  228. accu2 = _mm_add_pi32(accu2, _mm_madd_pi16(temp2, pVfilter[0])); // += l3*f2+l1*f0 r3*f2+r1*f0
  229. accu2 = _mm_add_pi32(accu2, _mm_madd_pi16(temp1, pVfilter[1])); // += l4*f3+l2*f1 r4*f3+r2*f1
  230. // accu1 += l2*f2+l0*f0 r2*f2+r0*f0
  231. // += l3*f3+l1*f1 r3*f3+r1*f1
  232. // accu2 += l3*f2+l1*f0 r3*f2+r1*f0
  233. // l4*f3+l2*f1 r4*f3+r2*f1
  234. pVfilter += 2;
  235. pVsrc += 2;
  236. }
  237. // accu >>= resultDivFactor
  238. accu1 = _mm_srai_pi32(accu1, resultDivFactor);
  239. accu2 = _mm_srai_pi32(accu2, resultDivFactor);
  240. // pack 2*2*32bits => 4*16 bits
  241. pVdest[0] = _mm_packs_pi32(accu1, accu2);
  242. src += 4;
  243. pVdest ++;
  244. }
  245. _m_empty(); // clear emms state
  246. return (numSamples & 0xfffffffe) - length;
  247. }
  248. #endif // SOUNDTOUCH_ALLOW_MMX