celt_neon_intr.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /*
  35. * Function: xcorr_kernel_neon_float
  36. * ---------------------------------
  37. * Computes 4 correlation values and stores them in sum[4]
  38. */
  39. static void xcorr_kernel_neon_float(const float32_t *x, const float32_t *y,
  40. float32_t sum[4], int len) {
  41. float32x4_t YY[3];
  42. float32x4_t YEXT[3];
  43. float32x4_t XX[2];
  44. float32x2_t XX_2;
  45. float32x4_t SUMM;
  46. const float32_t *xi = x;
  47. const float32_t *yi = y;
  48. celt_assert(len>0);
  49. YY[0] = vld1q_f32(yi);
  50. SUMM = vdupq_n_f32(0);
  51. /* Consume 8 elements in x vector and 12 elements in y
  52. * vector. However, the 12'th element never really gets
  53. * touched in this loop. So, if len == 8, then we only
  54. * must access y[0] to y[10]. y[11] must not be accessed
  55. * hence make sure len > 8 and not len >= 8
  56. */
  57. while (len > 8) {
  58. yi += 4;
  59. YY[1] = vld1q_f32(yi);
  60. yi += 4;
  61. YY[2] = vld1q_f32(yi);
  62. XX[0] = vld1q_f32(xi);
  63. xi += 4;
  64. XX[1] = vld1q_f32(xi);
  65. xi += 4;
  66. SUMM = vmlaq_lane_f32(SUMM, YY[0], vget_low_f32(XX[0]), 0);
  67. YEXT[0] = vextq_f32(YY[0], YY[1], 1);
  68. SUMM = vmlaq_lane_f32(SUMM, YEXT[0], vget_low_f32(XX[0]), 1);
  69. YEXT[1] = vextq_f32(YY[0], YY[1], 2);
  70. SUMM = vmlaq_lane_f32(SUMM, YEXT[1], vget_high_f32(XX[0]), 0);
  71. YEXT[2] = vextq_f32(YY[0], YY[1], 3);
  72. SUMM = vmlaq_lane_f32(SUMM, YEXT[2], vget_high_f32(XX[0]), 1);
  73. SUMM = vmlaq_lane_f32(SUMM, YY[1], vget_low_f32(XX[1]), 0);
  74. YEXT[0] = vextq_f32(YY[1], YY[2], 1);
  75. SUMM = vmlaq_lane_f32(SUMM, YEXT[0], vget_low_f32(XX[1]), 1);
  76. YEXT[1] = vextq_f32(YY[1], YY[2], 2);
  77. SUMM = vmlaq_lane_f32(SUMM, YEXT[1], vget_high_f32(XX[1]), 0);
  78. YEXT[2] = vextq_f32(YY[1], YY[2], 3);
  79. SUMM = vmlaq_lane_f32(SUMM, YEXT[2], vget_high_f32(XX[1]), 1);
  80. YY[0] = YY[2];
  81. len -= 8;
  82. }
  83. /* Consume 4 elements in x vector and 8 elements in y
  84. * vector. However, the 8'th element in y never really gets
  85. * touched in this loop. So, if len == 4, then we only
  86. * must access y[0] to y[6]. y[7] must not be accessed
  87. * hence make sure len>4 and not len>=4
  88. */
  89. if (len > 4) {
  90. yi += 4;
  91. YY[1] = vld1q_f32(yi);
  92. XX[0] = vld1q_f32(xi);
  93. xi += 4;
  94. SUMM = vmlaq_lane_f32(SUMM, YY[0], vget_low_f32(XX[0]), 0);
  95. YEXT[0] = vextq_f32(YY[0], YY[1], 1);
  96. SUMM = vmlaq_lane_f32(SUMM, YEXT[0], vget_low_f32(XX[0]), 1);
  97. YEXT[1] = vextq_f32(YY[0], YY[1], 2);
  98. SUMM = vmlaq_lane_f32(SUMM, YEXT[1], vget_high_f32(XX[0]), 0);
  99. YEXT[2] = vextq_f32(YY[0], YY[1], 3);
  100. SUMM = vmlaq_lane_f32(SUMM, YEXT[2], vget_high_f32(XX[0]), 1);
  101. YY[0] = YY[1];
  102. len -= 4;
  103. }
  104. while (--len > 0) {
  105. XX_2 = vld1_dup_f32(xi++);
  106. SUMM = vmlaq_lane_f32(SUMM, YY[0], XX_2, 0);
  107. YY[0]= vld1q_f32(++yi);
  108. }
  109. XX_2 = vld1_dup_f32(xi);
  110. SUMM = vmlaq_lane_f32(SUMM, YY[0], XX_2, 0);
  111. vst1q_f32(sum, SUMM);
  112. }
  113. /*
  114. * Function: xcorr_kernel_neon_float_process1
  115. * ---------------------------------
  116. * Computes single correlation values and stores in *sum
  117. */
  118. static void xcorr_kernel_neon_float_process1(const float32_t *x,
  119. const float32_t *y, float32_t *sum, int len) {
  120. float32x4_t XX[4];
  121. float32x4_t YY[4];
  122. float32x2_t XX_2;
  123. float32x2_t YY_2;
  124. float32x4_t SUMM;
  125. float32x2_t SUMM_2[2];
  126. const float32_t *xi = x;
  127. const float32_t *yi = y;
  128. SUMM = vdupq_n_f32(0);
  129. /* Work on 16 values per iteration */
  130. while (len >= 16) {
  131. XX[0] = vld1q_f32(xi);
  132. xi += 4;
  133. XX[1] = vld1q_f32(xi);
  134. xi += 4;
  135. XX[2] = vld1q_f32(xi);
  136. xi += 4;
  137. XX[3] = vld1q_f32(xi);
  138. xi += 4;
  139. YY[0] = vld1q_f32(yi);
  140. yi += 4;
  141. YY[1] = vld1q_f32(yi);
  142. yi += 4;
  143. YY[2] = vld1q_f32(yi);
  144. yi += 4;
  145. YY[3] = vld1q_f32(yi);
  146. yi += 4;
  147. SUMM = vmlaq_f32(SUMM, YY[0], XX[0]);
  148. SUMM = vmlaq_f32(SUMM, YY[1], XX[1]);
  149. SUMM = vmlaq_f32(SUMM, YY[2], XX[2]);
  150. SUMM = vmlaq_f32(SUMM, YY[3], XX[3]);
  151. len -= 16;
  152. }
  153. /* Work on 8 values */
  154. if (len >= 8) {
  155. XX[0] = vld1q_f32(xi);
  156. xi += 4;
  157. XX[1] = vld1q_f32(xi);
  158. xi += 4;
  159. YY[0] = vld1q_f32(yi);
  160. yi += 4;
  161. YY[1] = vld1q_f32(yi);
  162. yi += 4;
  163. SUMM = vmlaq_f32(SUMM, YY[0], XX[0]);
  164. SUMM = vmlaq_f32(SUMM, YY[1], XX[1]);
  165. len -= 8;
  166. }
  167. /* Work on 4 values */
  168. if (len >= 4) {
  169. XX[0] = vld1q_f32(xi);
  170. xi += 4;
  171. YY[0] = vld1q_f32(yi);
  172. yi += 4;
  173. SUMM = vmlaq_f32(SUMM, YY[0], XX[0]);
  174. len -= 4;
  175. }
  176. /* Start accumulating results */
  177. SUMM_2[0] = vget_low_f32(SUMM);
  178. if (len >= 2) {
  179. /* While at it, consume 2 more values if available */
  180. XX_2 = vld1_f32(xi);
  181. xi += 2;
  182. YY_2 = vld1_f32(yi);
  183. yi += 2;
  184. SUMM_2[0] = vmla_f32(SUMM_2[0], YY_2, XX_2);
  185. len -= 2;
  186. }
  187. SUMM_2[1] = vget_high_f32(SUMM);
  188. SUMM_2[0] = vadd_f32(SUMM_2[0], SUMM_2[1]);
  189. SUMM_2[0] = vpadd_f32(SUMM_2[0], SUMM_2[0]);
  190. /* Ok, now we have result accumulated in SUMM_2[0].0 */
  191. if (len > 0) {
  192. /* Case when you have one value left */
  193. XX_2 = vld1_dup_f32(xi);
  194. YY_2 = vld1_dup_f32(yi);
  195. SUMM_2[0] = vmla_f32(SUMM_2[0], XX_2, YY_2);
  196. }
  197. vst1_lane_f32(sum, SUMM_2[0], 0);
  198. }
  199. void celt_pitch_xcorr_float_neon(const opus_val16 *_x, const opus_val16 *_y,
  200. opus_val32 *xcorr, int len, int max_pitch) {
  201. int i;
  202. celt_assert(max_pitch > 0);
  203. celt_assert((((unsigned char *)_x-(unsigned char *)NULL)&3)==0);
  204. for (i = 0; i < (max_pitch-3); i += 4) {
  205. xcorr_kernel_neon_float((const float32_t *)_x, (const float32_t *)_y+i,
  206. (float32_t *)xcorr+i, len);
  207. }
  208. /* In case max_pitch isn't multiple of 4
  209. * compute single correlation value per iteration
  210. */
  211. for (; i < max_pitch; i++) {
  212. xcorr_kernel_neon_float_process1((const float32_t *)_x,
  213. (const float32_t *)_y+i, (float32_t *)xcorr+i, len);
  214. }
  215. }
  216. #endif