pitch_sse4_1.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <xmmintrin.h>
  27. #include <emmintrin.h>
  28. #include "macros.h"
  29. #include "celt_lpc.h"
  30. #include "stack_alloc.h"
  31. #include "mathops.h"
  32. #include "pitch.h"
  33. #if defined(OPUS_X86_MAY_HAVE_SSE4_1) && defined(FIXED_POINT)
  34. #include <smmintrin.h>
  35. #include "x86cpu.h"
  36. opus_val32 celt_inner_prod_sse4_1(const opus_val16 *x, const opus_val16 *y,
  37. int N)
  38. {
  39. opus_int i, dataSize16;
  40. opus_int32 sum;
  41. __m128i inVec1_76543210, inVec1_FEDCBA98, acc1;
  42. __m128i inVec2_76543210, inVec2_FEDCBA98, acc2;
  43. __m128i inVec1_3210, inVec2_3210;
  44. sum = 0;
  45. dataSize16 = N & ~15;
  46. acc1 = _mm_setzero_si128();
  47. acc2 = _mm_setzero_si128();
  48. for (i=0;i<dataSize16;i+=16) {
  49. inVec1_76543210 = _mm_loadu_si128((__m128i *)(&x[i + 0]));
  50. inVec2_76543210 = _mm_loadu_si128((__m128i *)(&y[i + 0]));
  51. inVec1_FEDCBA98 = _mm_loadu_si128((__m128i *)(&x[i + 8]));
  52. inVec2_FEDCBA98 = _mm_loadu_si128((__m128i *)(&y[i + 8]));
  53. inVec1_76543210 = _mm_madd_epi16(inVec1_76543210, inVec2_76543210);
  54. inVec1_FEDCBA98 = _mm_madd_epi16(inVec1_FEDCBA98, inVec2_FEDCBA98);
  55. acc1 = _mm_add_epi32(acc1, inVec1_76543210);
  56. acc2 = _mm_add_epi32(acc2, inVec1_FEDCBA98);
  57. }
  58. acc1 = _mm_add_epi32(acc1, acc2);
  59. if (N - i >= 8)
  60. {
  61. inVec1_76543210 = _mm_loadu_si128((__m128i *)(&x[i + 0]));
  62. inVec2_76543210 = _mm_loadu_si128((__m128i *)(&y[i + 0]));
  63. inVec1_76543210 = _mm_madd_epi16(inVec1_76543210, inVec2_76543210);
  64. acc1 = _mm_add_epi32(acc1, inVec1_76543210);
  65. i += 8;
  66. }
  67. if (N - i >= 4)
  68. {
  69. inVec1_3210 = OP_CVTEPI16_EPI32_M64(&x[i + 0]);
  70. inVec2_3210 = OP_CVTEPI16_EPI32_M64(&y[i + 0]);
  71. inVec1_3210 = _mm_mullo_epi32(inVec1_3210, inVec2_3210);
  72. acc1 = _mm_add_epi32(acc1, inVec1_3210);
  73. i += 4;
  74. }
  75. acc1 = _mm_add_epi32(acc1, _mm_unpackhi_epi64(acc1, acc1));
  76. acc1 = _mm_add_epi32(acc1, _mm_shufflelo_epi16(acc1, 0x0E));
  77. sum += _mm_cvtsi128_si32(acc1);
  78. for (;i<N;i++)
  79. {
  80. sum = silk_SMLABB(sum, x[i], y[i]);
  81. }
  82. return sum;
  83. }
  84. void xcorr_kernel_sse4_1(const opus_val16 * x, const opus_val16 * y, opus_val32 sum[ 4 ], int len)
  85. {
  86. int j;
  87. __m128i vecX, vecX0, vecX1, vecX2, vecX3;
  88. __m128i vecY0, vecY1, vecY2, vecY3;
  89. __m128i sum0, sum1, sum2, sum3, vecSum;
  90. __m128i initSum;
  91. celt_assert(len >= 3);
  92. sum0 = _mm_setzero_si128();
  93. sum1 = _mm_setzero_si128();
  94. sum2 = _mm_setzero_si128();
  95. sum3 = _mm_setzero_si128();
  96. for (j=0;j<(len-7);j+=8)
  97. {
  98. vecX = _mm_loadu_si128((__m128i *)(&x[j + 0]));
  99. vecY0 = _mm_loadu_si128((__m128i *)(&y[j + 0]));
  100. vecY1 = _mm_loadu_si128((__m128i *)(&y[j + 1]));
  101. vecY2 = _mm_loadu_si128((__m128i *)(&y[j + 2]));
  102. vecY3 = _mm_loadu_si128((__m128i *)(&y[j + 3]));
  103. sum0 = _mm_add_epi32(sum0, _mm_madd_epi16(vecX, vecY0));
  104. sum1 = _mm_add_epi32(sum1, _mm_madd_epi16(vecX, vecY1));
  105. sum2 = _mm_add_epi32(sum2, _mm_madd_epi16(vecX, vecY2));
  106. sum3 = _mm_add_epi32(sum3, _mm_madd_epi16(vecX, vecY3));
  107. }
  108. sum0 = _mm_add_epi32(sum0, _mm_unpackhi_epi64( sum0, sum0));
  109. sum0 = _mm_add_epi32(sum0, _mm_shufflelo_epi16( sum0, 0x0E));
  110. sum1 = _mm_add_epi32(sum1, _mm_unpackhi_epi64( sum1, sum1));
  111. sum1 = _mm_add_epi32(sum1, _mm_shufflelo_epi16( sum1, 0x0E));
  112. sum2 = _mm_add_epi32(sum2, _mm_unpackhi_epi64( sum2, sum2));
  113. sum2 = _mm_add_epi32(sum2, _mm_shufflelo_epi16( sum2, 0x0E));
  114. sum3 = _mm_add_epi32(sum3, _mm_unpackhi_epi64( sum3, sum3));
  115. sum3 = _mm_add_epi32(sum3, _mm_shufflelo_epi16( sum3, 0x0E));
  116. vecSum = _mm_unpacklo_epi64(_mm_unpacklo_epi32(sum0, sum1),
  117. _mm_unpacklo_epi32(sum2, sum3));
  118. for (;j<(len-3);j+=4)
  119. {
  120. vecX = OP_CVTEPI16_EPI32_M64(&x[j + 0]);
  121. vecX0 = _mm_shuffle_epi32(vecX, 0x00);
  122. vecX1 = _mm_shuffle_epi32(vecX, 0x55);
  123. vecX2 = _mm_shuffle_epi32(vecX, 0xaa);
  124. vecX3 = _mm_shuffle_epi32(vecX, 0xff);
  125. vecY0 = OP_CVTEPI16_EPI32_M64(&y[j + 0]);
  126. vecY1 = OP_CVTEPI16_EPI32_M64(&y[j + 1]);
  127. vecY2 = OP_CVTEPI16_EPI32_M64(&y[j + 2]);
  128. vecY3 = OP_CVTEPI16_EPI32_M64(&y[j + 3]);
  129. sum0 = _mm_mullo_epi32(vecX0, vecY0);
  130. sum1 = _mm_mullo_epi32(vecX1, vecY1);
  131. sum2 = _mm_mullo_epi32(vecX2, vecY2);
  132. sum3 = _mm_mullo_epi32(vecX3, vecY3);
  133. sum0 = _mm_add_epi32(sum0, sum1);
  134. sum2 = _mm_add_epi32(sum2, sum3);
  135. vecSum = _mm_add_epi32(vecSum, sum0);
  136. vecSum = _mm_add_epi32(vecSum, sum2);
  137. }
  138. for (;j<len;j++)
  139. {
  140. vecX = OP_CVTEPI16_EPI32_M64(&x[j + 0]);
  141. vecX0 = _mm_shuffle_epi32(vecX, 0x00);
  142. vecY0 = OP_CVTEPI16_EPI32_M64(&y[j + 0]);
  143. sum0 = _mm_mullo_epi32(vecX0, vecY0);
  144. vecSum = _mm_add_epi32(vecSum, sum0);
  145. }
  146. initSum = _mm_loadu_si128((__m128i *)(&sum[0]));
  147. initSum = _mm_add_epi32(initSum, vecSum);
  148. _mm_storeu_si128((__m128i *)sum, initSum);
  149. }
  150. #endif