pitch_sse.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Copyright (c) 2013 Jean-Marc Valin and John Ridges */
  2. /**
  3. @file pitch_sse.h
  4. @brief Pitch analysis
  5. */
  6. /*
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions
  9. are met:
  10. - Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. - Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  19. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef PITCH_SSE_H
  28. #define PITCH_SSE_H
  29. #include <xmmintrin.h>
  30. #include "arch.h"
  31. #define OVERRIDE_XCORR_KERNEL
  32. static OPUS_INLINE void xcorr_kernel(const opus_val16 *x, const opus_val16 *y, opus_val32 sum[4], int len)
  33. {
  34. int j;
  35. __m128 xsum1, xsum2;
  36. xsum1 = _mm_loadu_ps(sum);
  37. xsum2 = _mm_setzero_ps();
  38. for (j = 0; j < len-3; j += 4)
  39. {
  40. __m128 x0 = _mm_loadu_ps(x+j);
  41. __m128 yj = _mm_loadu_ps(y+j);
  42. __m128 y3 = _mm_loadu_ps(y+j+3);
  43. xsum1 = _mm_add_ps(xsum1,_mm_mul_ps(_mm_shuffle_ps(x0,x0,0x00),yj));
  44. xsum2 = _mm_add_ps(xsum2,_mm_mul_ps(_mm_shuffle_ps(x0,x0,0x55),
  45. _mm_shuffle_ps(yj,y3,0x49)));
  46. xsum1 = _mm_add_ps(xsum1,_mm_mul_ps(_mm_shuffle_ps(x0,x0,0xaa),
  47. _mm_shuffle_ps(yj,y3,0x9e)));
  48. xsum2 = _mm_add_ps(xsum2,_mm_mul_ps(_mm_shuffle_ps(x0,x0,0xff),y3));
  49. }
  50. if (j < len)
  51. {
  52. xsum1 = _mm_add_ps(xsum1,_mm_mul_ps(_mm_load1_ps(x+j),_mm_loadu_ps(y+j)));
  53. if (++j < len)
  54. {
  55. xsum2 = _mm_add_ps(xsum2,_mm_mul_ps(_mm_load1_ps(x+j),_mm_loadu_ps(y+j)));
  56. if (++j < len)
  57. {
  58. xsum1 = _mm_add_ps(xsum1,_mm_mul_ps(_mm_load1_ps(x+j),_mm_loadu_ps(y+j)));
  59. }
  60. }
  61. }
  62. _mm_storeu_ps(sum,_mm_add_ps(xsum1,xsum2));
  63. }
  64. #define OVERRIDE_DUAL_INNER_PROD
  65. static OPUS_INLINE void dual_inner_prod(const opus_val16 *x, const opus_val16 *y01, const opus_val16 *y02,
  66. int N, opus_val32 *xy1, opus_val32 *xy2)
  67. {
  68. int i;
  69. __m128 xsum1, xsum2;
  70. xsum1 = _mm_setzero_ps();
  71. xsum2 = _mm_setzero_ps();
  72. for (i=0;i<N-3;i+=4)
  73. {
  74. __m128 xi = _mm_loadu_ps(x+i);
  75. __m128 y1i = _mm_loadu_ps(y01+i);
  76. __m128 y2i = _mm_loadu_ps(y02+i);
  77. xsum1 = _mm_add_ps(xsum1,_mm_mul_ps(xi, y1i));
  78. xsum2 = _mm_add_ps(xsum2,_mm_mul_ps(xi, y2i));
  79. }
  80. /* Horizontal sum */
  81. xsum1 = _mm_add_ps(xsum1, _mm_movehl_ps(xsum1, xsum1));
  82. xsum1 = _mm_add_ss(xsum1, _mm_shuffle_ps(xsum1, xsum1, 0x55));
  83. _mm_store_ss(xy1, xsum1);
  84. xsum2 = _mm_add_ps(xsum2, _mm_movehl_ps(xsum2, xsum2));
  85. xsum2 = _mm_add_ss(xsum2, _mm_shuffle_ps(xsum2, xsum2, 0x55));
  86. _mm_store_ss(xy2, xsum2);
  87. for (;i<N;i++)
  88. {
  89. *xy1 = MAC16_16(*xy1, x[i], y01[i]);
  90. *xy2 = MAC16_16(*xy2, x[i], y02[i]);
  91. }
  92. }
  93. #define OVERRIDE_COMB_FILTER_CONST
  94. static OPUS_INLINE void comb_filter_const(opus_val32 *y, opus_val32 *x, int T, int N,
  95. opus_val16 g10, opus_val16 g11, opus_val16 g12)
  96. {
  97. int i;
  98. __m128 x0v;
  99. __m128 g10v, g11v, g12v;
  100. g10v = _mm_load1_ps(&g10);
  101. g11v = _mm_load1_ps(&g11);
  102. g12v = _mm_load1_ps(&g12);
  103. x0v = _mm_loadu_ps(&x[-T-2]);
  104. for (i=0;i<N-3;i+=4)
  105. {
  106. __m128 yi, yi2, x1v, x2v, x3v, x4v;
  107. const opus_val32 *xp = &x[i-T-2];
  108. yi = _mm_loadu_ps(x+i);
  109. x4v = _mm_loadu_ps(xp+4);
  110. #if 0
  111. /* Slower version with all loads */
  112. x1v = _mm_loadu_ps(xp+1);
  113. x2v = _mm_loadu_ps(xp+2);
  114. x3v = _mm_loadu_ps(xp+3);
  115. #else
  116. x2v = _mm_shuffle_ps(x0v, x4v, 0x4e);
  117. x1v = _mm_shuffle_ps(x0v, x2v, 0x99);
  118. x3v = _mm_shuffle_ps(x2v, x4v, 0x99);
  119. #endif
  120. yi = _mm_add_ps(yi, _mm_mul_ps(g10v,x2v));
  121. #if 0 /* Set to 1 to make it bit-exact with the non-SSE version */
  122. yi = _mm_add_ps(yi, _mm_mul_ps(g11v,_mm_add_ps(x3v,x1v)));
  123. yi = _mm_add_ps(yi, _mm_mul_ps(g12v,_mm_add_ps(x4v,x0v)));
  124. #else
  125. /* Use partial sums */
  126. yi2 = _mm_add_ps(_mm_mul_ps(g11v,_mm_add_ps(x3v,x1v)),
  127. _mm_mul_ps(g12v,_mm_add_ps(x4v,x0v)));
  128. yi = _mm_add_ps(yi, yi2);
  129. #endif
  130. x0v=x4v;
  131. _mm_storeu_ps(y+i, yi);
  132. }
  133. #ifdef CUSTOM_MODES
  134. for (;i<N;i++)
  135. {
  136. y[i] = x[i]
  137. + MULT16_32_Q15(g10,x[i-T])
  138. + MULT16_32_Q15(g11,ADD32(x[i-T+1],x[i-T-1]))
  139. + MULT16_32_Q15(g12,ADD32(x[i-T+2],x[i-T-2]));
  140. }
  141. #endif
  142. }
  143. #endif