celt_neon_intr.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Copyright (c) 2014-2015 Xiph.Org Foundation
  2. Written by Viswanath Puttagunta */
  3. /**
  4. @file celt_neon_intr.c
  5. @brief ARM Neon Intrinsic optimizations for celt
  6. */
  7. /*
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. - Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. - Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  20. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include <arm_neon.h>
  32. #include "../pitch.h"
  33. #if defined(FIXED_POINT)
  34. void xcorr_kernel_neon_fixed(const opus_val16 * x, const opus_val16 * y, opus_val32 sum[4], int len)
  35. {
  36. int j;
  37. int32x4_t a = vld1q_s32(sum);
  38. /* Load y[0...3] */
  39. /* This requires len>0 to always be valid (which we assert in the C code). */
  40. int16x4_t y0 = vld1_s16(y);
  41. y += 4;
  42. for (j = 0; j + 8 <= len; j += 8)
  43. {
  44. /* Load x[0...7] */
  45. int16x8_t xx = vld1q_s16(x);
  46. int16x4_t x0 = vget_low_s16(xx);
  47. int16x4_t x4 = vget_high_s16(xx);
  48. /* Load y[4...11] */
  49. int16x8_t yy = vld1q_s16(y);
  50. int16x4_t y4 = vget_low_s16(yy);
  51. int16x4_t y8 = vget_high_s16(yy);
  52. int32x4_t a0 = vmlal_lane_s16(a, y0, x0, 0);
  53. int32x4_t a1 = vmlal_lane_s16(a0, y4, x4, 0);
  54. int16x4_t y1 = vext_s16(y0, y4, 1);
  55. int16x4_t y5 = vext_s16(y4, y8, 1);
  56. int32x4_t a2 = vmlal_lane_s16(a1, y1, x0, 1);
  57. int32x4_t a3 = vmlal_lane_s16(a2, y5, x4, 1);
  58. int16x4_t y2 = vext_s16(y0, y4, 2);
  59. int16x4_t y6 = vext_s16(y4, y8, 2);
  60. int32x4_t a4 = vmlal_lane_s16(a3, y2, x0, 2);
  61. int32x4_t a5 = vmlal_lane_s16(a4, y6, x4, 2);
  62. int16x4_t y3 = vext_s16(y0, y4, 3);
  63. int16x4_t y7 = vext_s16(y4, y8, 3);
  64. int32x4_t a6 = vmlal_lane_s16(a5, y3, x0, 3);
  65. int32x4_t a7 = vmlal_lane_s16(a6, y7, x4, 3);
  66. y0 = y8;
  67. a = a7;
  68. x += 8;
  69. y += 8;
  70. }
  71. for (; j < len; j++)
  72. {
  73. int16x4_t x0 = vld1_dup_s16(x); /* load next x */
  74. int32x4_t a0 = vmlal_s16(a, y0, x0);
  75. int16x4_t y4 = vld1_dup_s16(y); /* load next y */
  76. y0 = vext_s16(y0, y4, 1);
  77. a = a0;
  78. x++;
  79. y++;
  80. }
  81. vst1q_s32(sum, a);
  82. }
  83. #else
  84. /*
  85. * Function: xcorr_kernel_neon_float
  86. * ---------------------------------
  87. * Computes 4 correlation values and stores them in sum[4]
  88. */
  89. static void xcorr_kernel_neon_float(const float32_t *x, const float32_t *y,
  90. float32_t sum[4], int len) {
  91. float32x4_t YY[3];
  92. float32x4_t YEXT[3];
  93. float32x4_t XX[2];
  94. float32x2_t XX_2;
  95. float32x4_t SUMM;
  96. const float32_t *xi = x;
  97. const float32_t *yi = y;
  98. celt_assert(len>0);
  99. YY[0] = vld1q_f32(yi);
  100. SUMM = vdupq_n_f32(0);
  101. /* Consume 8 elements in x vector and 12 elements in y
  102. * vector. However, the 12'th element never really gets
  103. * touched in this loop. So, if len == 8, then we only
  104. * must access y[0] to y[10]. y[11] must not be accessed
  105. * hence make sure len > 8 and not len >= 8
  106. */
  107. while (len > 8) {
  108. yi += 4;
  109. YY[1] = vld1q_f32(yi);
  110. yi += 4;
  111. YY[2] = vld1q_f32(yi);
  112. XX[0] = vld1q_f32(xi);
  113. xi += 4;
  114. XX[1] = vld1q_f32(xi);
  115. xi += 4;
  116. SUMM = vmlaq_lane_f32(SUMM, YY[0], vget_low_f32(XX[0]), 0);
  117. YEXT[0] = vextq_f32(YY[0], YY[1], 1);
  118. SUMM = vmlaq_lane_f32(SUMM, YEXT[0], vget_low_f32(XX[0]), 1);
  119. YEXT[1] = vextq_f32(YY[0], YY[1], 2);
  120. SUMM = vmlaq_lane_f32(SUMM, YEXT[1], vget_high_f32(XX[0]), 0);
  121. YEXT[2] = vextq_f32(YY[0], YY[1], 3);
  122. SUMM = vmlaq_lane_f32(SUMM, YEXT[2], vget_high_f32(XX[0]), 1);
  123. SUMM = vmlaq_lane_f32(SUMM, YY[1], vget_low_f32(XX[1]), 0);
  124. YEXT[0] = vextq_f32(YY[1], YY[2], 1);
  125. SUMM = vmlaq_lane_f32(SUMM, YEXT[0], vget_low_f32(XX[1]), 1);
  126. YEXT[1] = vextq_f32(YY[1], YY[2], 2);
  127. SUMM = vmlaq_lane_f32(SUMM, YEXT[1], vget_high_f32(XX[1]), 0);
  128. YEXT[2] = vextq_f32(YY[1], YY[2], 3);
  129. SUMM = vmlaq_lane_f32(SUMM, YEXT[2], vget_high_f32(XX[1]), 1);
  130. YY[0] = YY[2];
  131. len -= 8;
  132. }
  133. /* Consume 4 elements in x vector and 8 elements in y
  134. * vector. However, the 8'th element in y never really gets
  135. * touched in this loop. So, if len == 4, then we only
  136. * must access y[0] to y[6]. y[7] must not be accessed
  137. * hence make sure len>4 and not len>=4
  138. */
  139. if (len > 4) {
  140. yi += 4;
  141. YY[1] = vld1q_f32(yi);
  142. XX[0] = vld1q_f32(xi);
  143. xi += 4;
  144. SUMM = vmlaq_lane_f32(SUMM, YY[0], vget_low_f32(XX[0]), 0);
  145. YEXT[0] = vextq_f32(YY[0], YY[1], 1);
  146. SUMM = vmlaq_lane_f32(SUMM, YEXT[0], vget_low_f32(XX[0]), 1);
  147. YEXT[1] = vextq_f32(YY[0], YY[1], 2);
  148. SUMM = vmlaq_lane_f32(SUMM, YEXT[1], vget_high_f32(XX[0]), 0);
  149. YEXT[2] = vextq_f32(YY[0], YY[1], 3);
  150. SUMM = vmlaq_lane_f32(SUMM, YEXT[2], vget_high_f32(XX[0]), 1);
  151. YY[0] = YY[1];
  152. len -= 4;
  153. }
  154. while (--len > 0) {
  155. XX_2 = vld1_dup_f32(xi++);
  156. SUMM = vmlaq_lane_f32(SUMM, YY[0], XX_2, 0);
  157. YY[0]= vld1q_f32(++yi);
  158. }
  159. XX_2 = vld1_dup_f32(xi);
  160. SUMM = vmlaq_lane_f32(SUMM, YY[0], XX_2, 0);
  161. vst1q_f32(sum, SUMM);
  162. }
  163. void celt_pitch_xcorr_float_neon(const opus_val16 *_x, const opus_val16 *_y,
  164. opus_val32 *xcorr, int len, int max_pitch, int arch) {
  165. int i;
  166. (void)arch;
  167. celt_assert(max_pitch > 0);
  168. celt_sig_assert((((unsigned char *)_x-(unsigned char *)NULL)&3)==0);
  169. for (i = 0; i < (max_pitch-3); i += 4) {
  170. xcorr_kernel_neon_float((const float32_t *)_x, (const float32_t *)_y+i,
  171. (float32_t *)xcorr+i, len);
  172. }
  173. /* In case max_pitch isn't a multiple of 4, do non-unrolled version. */
  174. for (; i < max_pitch; i++) {
  175. xcorr[i] = celt_inner_prod_neon(_x, _y+i, len);
  176. }
  177. }
  178. #endif