pitch_sse.c 5.8 KB

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