vq_sse2.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2009 Xiph.Org Foundation
  3. Copyright (c) 2007-2016 Jean-Marc Valin */
  4. /*
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  17. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include <xmmintrin.h>
  29. #include <emmintrin.h>
  30. #include "celt_lpc.h"
  31. #include "stack_alloc.h"
  32. #include "mathops.h"
  33. #include "vq.h"
  34. #include "x86cpu.h"
  35. #ifndef FIXED_POINT
  36. opus_val16 op_pvq_search_sse2(celt_norm *_X, int *iy, int K, int N, int arch)
  37. {
  38. int i, j;
  39. int pulsesLeft;
  40. float xy, yy;
  41. VARDECL(celt_norm, y);
  42. VARDECL(celt_norm, X);
  43. VARDECL(float, signy);
  44. __m128 signmask;
  45. __m128 sums;
  46. __m128i fours;
  47. SAVE_STACK;
  48. (void)arch;
  49. /* All bits set to zero, except for the sign bit. */
  50. signmask = _mm_set_ps1(-0.f);
  51. fours = _mm_set_epi32(4, 4, 4, 4);
  52. ALLOC(y, N+3, celt_norm);
  53. ALLOC(X, N+3, celt_norm);
  54. ALLOC(signy, N+3, float);
  55. OPUS_COPY(X, _X, N);
  56. X[N] = X[N+1] = X[N+2] = 0;
  57. sums = _mm_setzero_ps();
  58. for (j=0;j<N;j+=4)
  59. {
  60. __m128 x4, s4;
  61. x4 = _mm_loadu_ps(&X[j]);
  62. s4 = _mm_cmplt_ps(x4, _mm_setzero_ps());
  63. /* Get rid of the sign */
  64. x4 = _mm_andnot_ps(signmask, x4);
  65. sums = _mm_add_ps(sums, x4);
  66. /* Clear y and iy in case we don't do the projection. */
  67. _mm_storeu_ps(&y[j], _mm_setzero_ps());
  68. _mm_storeu_si128((__m128i*)&iy[j], _mm_setzero_si128());
  69. _mm_storeu_ps(&X[j], x4);
  70. _mm_storeu_ps(&signy[j], s4);
  71. }
  72. sums = _mm_add_ps(sums, _mm_shuffle_ps(sums, sums, _MM_SHUFFLE(1, 0, 3, 2)));
  73. sums = _mm_add_ps(sums, _mm_shuffle_ps(sums, sums, _MM_SHUFFLE(2, 3, 0, 1)));
  74. xy = yy = 0;
  75. pulsesLeft = K;
  76. /* Do a pre-search by projecting on the pyramid */
  77. if (K > (N>>1))
  78. {
  79. __m128i pulses_sum;
  80. __m128 yy4, xy4;
  81. __m128 rcp4;
  82. opus_val32 sum = _mm_cvtss_f32(sums);
  83. /* If X is too small, just replace it with a pulse at 0 */
  84. /* Prevents infinities and NaNs from causing too many pulses
  85. to be allocated. 64 is an approximation of infinity here. */
  86. if (!(sum > EPSILON && sum < 64))
  87. {
  88. X[0] = QCONST16(1.f,14);
  89. j=1; do
  90. X[j]=0;
  91. while (++j<N);
  92. sums = _mm_set_ps1(1.f);
  93. }
  94. /* Using K+e with e < 1 guarantees we cannot get more than K pulses. */
  95. rcp4 = _mm_mul_ps(_mm_set_ps1((float)(K+.8)), _mm_rcp_ps(sums));
  96. xy4 = yy4 = _mm_setzero_ps();
  97. pulses_sum = _mm_setzero_si128();
  98. for (j=0;j<N;j+=4)
  99. {
  100. __m128 rx4, x4, y4;
  101. __m128i iy4;
  102. x4 = _mm_loadu_ps(&X[j]);
  103. rx4 = _mm_mul_ps(x4, rcp4);
  104. iy4 = _mm_cvttps_epi32(rx4);
  105. pulses_sum = _mm_add_epi32(pulses_sum, iy4);
  106. _mm_storeu_si128((__m128i*)&iy[j], iy4);
  107. y4 = _mm_cvtepi32_ps(iy4);
  108. xy4 = _mm_add_ps(xy4, _mm_mul_ps(x4, y4));
  109. yy4 = _mm_add_ps(yy4, _mm_mul_ps(y4, y4));
  110. /* double the y[] vector so we don't have to do it in the search loop. */
  111. _mm_storeu_ps(&y[j], _mm_add_ps(y4, y4));
  112. }
  113. pulses_sum = _mm_add_epi32(pulses_sum, _mm_shuffle_epi32(pulses_sum, _MM_SHUFFLE(1, 0, 3, 2)));
  114. pulses_sum = _mm_add_epi32(pulses_sum, _mm_shuffle_epi32(pulses_sum, _MM_SHUFFLE(2, 3, 0, 1)));
  115. pulsesLeft -= _mm_cvtsi128_si32(pulses_sum);
  116. xy4 = _mm_add_ps(xy4, _mm_shuffle_ps(xy4, xy4, _MM_SHUFFLE(1, 0, 3, 2)));
  117. xy4 = _mm_add_ps(xy4, _mm_shuffle_ps(xy4, xy4, _MM_SHUFFLE(2, 3, 0, 1)));
  118. xy = _mm_cvtss_f32(xy4);
  119. yy4 = _mm_add_ps(yy4, _mm_shuffle_ps(yy4, yy4, _MM_SHUFFLE(1, 0, 3, 2)));
  120. yy4 = _mm_add_ps(yy4, _mm_shuffle_ps(yy4, yy4, _MM_SHUFFLE(2, 3, 0, 1)));
  121. yy = _mm_cvtss_f32(yy4);
  122. }
  123. X[N] = X[N+1] = X[N+2] = -100;
  124. y[N] = y[N+1] = y[N+2] = 100;
  125. celt_sig_assert(pulsesLeft>=0);
  126. /* This should never happen, but just in case it does (e.g. on silence)
  127. we fill the first bin with pulses. */
  128. if (pulsesLeft > N+3)
  129. {
  130. opus_val16 tmp = (opus_val16)pulsesLeft;
  131. yy = MAC16_16(yy, tmp, tmp);
  132. yy = MAC16_16(yy, tmp, y[0]);
  133. iy[0] += pulsesLeft;
  134. pulsesLeft=0;
  135. }
  136. for (i=0;i<pulsesLeft;i++)
  137. {
  138. int best_id;
  139. __m128 xy4, yy4;
  140. __m128 max, max2;
  141. __m128i count;
  142. __m128i pos;
  143. /* The squared magnitude term gets added anyway, so we might as well
  144. add it outside the loop */
  145. yy = ADD16(yy, 1);
  146. xy4 = _mm_load1_ps(&xy);
  147. yy4 = _mm_load1_ps(&yy);
  148. max = _mm_setzero_ps();
  149. pos = _mm_setzero_si128();
  150. count = _mm_set_epi32(3, 2, 1, 0);
  151. for (j=0;j<N;j+=4)
  152. {
  153. __m128 x4, y4, r4;
  154. x4 = _mm_loadu_ps(&X[j]);
  155. y4 = _mm_loadu_ps(&y[j]);
  156. x4 = _mm_add_ps(x4, xy4);
  157. y4 = _mm_add_ps(y4, yy4);
  158. y4 = _mm_rsqrt_ps(y4);
  159. r4 = _mm_mul_ps(x4, y4);
  160. /* Update the index of the max. */
  161. pos = _mm_max_epi16(pos, _mm_and_si128(count, _mm_castps_si128(_mm_cmpgt_ps(r4, max))));
  162. /* Update the max. */
  163. max = _mm_max_ps(max, r4);
  164. /* Update the indices (+4) */
  165. count = _mm_add_epi32(count, fours);
  166. }
  167. /* Horizontal max */
  168. max2 = _mm_max_ps(max, _mm_shuffle_ps(max, max, _MM_SHUFFLE(1, 0, 3, 2)));
  169. max2 = _mm_max_ps(max2, _mm_shuffle_ps(max2, max2, _MM_SHUFFLE(2, 3, 0, 1)));
  170. /* Now that max2 contains the max at all positions, look at which value(s) of the
  171. partial max is equal to the global max. */
  172. pos = _mm_and_si128(pos, _mm_castps_si128(_mm_cmpeq_ps(max, max2)));
  173. pos = _mm_max_epi16(pos, _mm_unpackhi_epi64(pos, pos));
  174. pos = _mm_max_epi16(pos, _mm_shufflelo_epi16(pos, _MM_SHUFFLE(1, 0, 3, 2)));
  175. best_id = _mm_cvtsi128_si32(pos);
  176. /* Updating the sums of the new pulse(s) */
  177. xy = ADD32(xy, EXTEND32(X[best_id]));
  178. /* We're multiplying y[j] by two so we don't have to do it here */
  179. yy = ADD16(yy, y[best_id]);
  180. /* Only now that we've made the final choice, update y/iy */
  181. /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
  182. y[best_id] += 2;
  183. iy[best_id]++;
  184. }
  185. /* Put the original sign back */
  186. for (j=0;j<N;j+=4)
  187. {
  188. __m128i y4;
  189. __m128i s4;
  190. y4 = _mm_loadu_si128((__m128i*)&iy[j]);
  191. s4 = _mm_castps_si128(_mm_loadu_ps(&signy[j]));
  192. y4 = _mm_xor_si128(_mm_add_epi32(y4, s4), s4);
  193. _mm_storeu_si128((__m128i*)&iy[j], y4);
  194. }
  195. RESTORE_STACK;
  196. return yy;
  197. }
  198. #endif