• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*
9    Redistribution and use in source and binary forms, with or without
10    modification, are permitted provided that the following conditions
11    are met:
12 
13    - Redistributions of source code must retain the above copyright
14    notice, this list of conditions and the following disclaimer.
15 
16    - Redistributions in binary form must reproduce the above copyright
17    notice, this list of conditions and the following disclaimer in the
18    documentation and/or other materials provided with the distribution.
19 
20    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include <arm_neon.h>
38 #include "../pitch.h"
39 
40 #if defined(FIXED_POINT)
xcorr_kernel_neon_fixed(const opus_val16 * x,const opus_val16 * y,opus_val32 sum[4],int len)41 void xcorr_kernel_neon_fixed(const opus_val16 * x, const opus_val16 * y, opus_val32 sum[4], int len)
42 {
43    int j;
44    int32x4_t a = vld1q_s32(sum);
45    /* Load y[0...3] */
46    /* This requires len>0 to always be valid (which we assert in the C code). */
47    int16x4_t y0 = vld1_s16(y);
48    y += 4;
49 
50    for (j = 0; j + 8 <= len; j += 8)
51    {
52       /* Load x[0...7] */
53       int16x8_t xx = vld1q_s16(x);
54       int16x4_t x0 = vget_low_s16(xx);
55       int16x4_t x4 = vget_high_s16(xx);
56       /* Load y[4...11] */
57       int16x8_t yy = vld1q_s16(y);
58       int16x4_t y4 = vget_low_s16(yy);
59       int16x4_t y8 = vget_high_s16(yy);
60       int32x4_t a0 = vmlal_lane_s16(a, y0, x0, 0);
61       int32x4_t a1 = vmlal_lane_s16(a0, y4, x4, 0);
62 
63       int16x4_t y1 = vext_s16(y0, y4, 1);
64       int16x4_t y5 = vext_s16(y4, y8, 1);
65       int32x4_t a2 = vmlal_lane_s16(a1, y1, x0, 1);
66       int32x4_t a3 = vmlal_lane_s16(a2, y5, x4, 1);
67 
68       int16x4_t y2 = vext_s16(y0, y4, 2);
69       int16x4_t y6 = vext_s16(y4, y8, 2);
70       int32x4_t a4 = vmlal_lane_s16(a3, y2, x0, 2);
71       int32x4_t a5 = vmlal_lane_s16(a4, y6, x4, 2);
72 
73       int16x4_t y3 = vext_s16(y0, y4, 3);
74       int16x4_t y7 = vext_s16(y4, y8, 3);
75       int32x4_t a6 = vmlal_lane_s16(a5, y3, x0, 3);
76       int32x4_t a7 = vmlal_lane_s16(a6, y7, x4, 3);
77 
78       y0 = y8;
79       a = a7;
80       x += 8;
81       y += 8;
82    }
83 
84    for (; j < len; j++)
85    {
86       int16x4_t x0 = vld1_dup_s16(x);  /* load next x */
87       int32x4_t a0 = vmlal_s16(a, y0, x0);
88 
89       int16x4_t y4 = vld1_dup_s16(y);  /* load next y */
90       y0 = vext_s16(y0, y4, 1);
91       a = a0;
92       x++;
93       y++;
94    }
95 
96    vst1q_s32(sum, a);
97 }
98 
99 #else
100 /*
101  * Function: xcorr_kernel_neon_float
102  * ---------------------------------
103  * Computes 4 correlation values and stores them in sum[4]
104  */
xcorr_kernel_neon_float(const float32_t * x,const float32_t * y,float32_t sum[4],int len)105 static void xcorr_kernel_neon_float(const float32_t *x, const float32_t *y,
106       float32_t sum[4], int len) {
107    float32x4_t YY[3];
108    float32x4_t YEXT[3];
109    float32x4_t XX[2];
110    float32x2_t XX_2;
111    float32x4_t SUMM;
112    const float32_t *xi = x;
113    const float32_t *yi = y;
114 
115    celt_assert(len>0);
116 
117    YY[0] = vld1q_f32(yi);
118    SUMM = vdupq_n_f32(0);
119 
120    /* Consume 8 elements in x vector and 12 elements in y
121     * vector. However, the 12'th element never really gets
122     * touched in this loop. So, if len == 8, then we only
123     * must access y[0] to y[10]. y[11] must not be accessed
124     * hence make sure len > 8 and not len >= 8
125     */
126    while (len > 8) {
127       yi += 4;
128       YY[1] = vld1q_f32(yi);
129       yi += 4;
130       YY[2] = vld1q_f32(yi);
131 
132       XX[0] = vld1q_f32(xi);
133       xi += 4;
134       XX[1] = vld1q_f32(xi);
135       xi += 4;
136 
137       SUMM = vmlaq_lane_f32(SUMM, YY[0], vget_low_f32(XX[0]), 0);
138       YEXT[0] = vextq_f32(YY[0], YY[1], 1);
139       SUMM = vmlaq_lane_f32(SUMM, YEXT[0], vget_low_f32(XX[0]), 1);
140       YEXT[1] = vextq_f32(YY[0], YY[1], 2);
141       SUMM = vmlaq_lane_f32(SUMM, YEXT[1], vget_high_f32(XX[0]), 0);
142       YEXT[2] = vextq_f32(YY[0], YY[1], 3);
143       SUMM = vmlaq_lane_f32(SUMM, YEXT[2], vget_high_f32(XX[0]), 1);
144 
145       SUMM = vmlaq_lane_f32(SUMM, YY[1], vget_low_f32(XX[1]), 0);
146       YEXT[0] = vextq_f32(YY[1], YY[2], 1);
147       SUMM = vmlaq_lane_f32(SUMM, YEXT[0], vget_low_f32(XX[1]), 1);
148       YEXT[1] = vextq_f32(YY[1], YY[2], 2);
149       SUMM = vmlaq_lane_f32(SUMM, YEXT[1], vget_high_f32(XX[1]), 0);
150       YEXT[2] = vextq_f32(YY[1], YY[2], 3);
151       SUMM = vmlaq_lane_f32(SUMM, YEXT[2], vget_high_f32(XX[1]), 1);
152 
153       YY[0] = YY[2];
154       len -= 8;
155    }
156 
157    /* Consume 4 elements in x vector and 8 elements in y
158     * vector. However, the 8'th element in y never really gets
159     * touched in this loop. So, if len == 4, then we only
160     * must access y[0] to y[6]. y[7] must not be accessed
161     * hence make sure len>4 and not len>=4
162     */
163    if (len > 4) {
164       yi += 4;
165       YY[1] = vld1q_f32(yi);
166 
167       XX[0] = vld1q_f32(xi);
168       xi += 4;
169 
170       SUMM = vmlaq_lane_f32(SUMM, YY[0], vget_low_f32(XX[0]), 0);
171       YEXT[0] = vextq_f32(YY[0], YY[1], 1);
172       SUMM = vmlaq_lane_f32(SUMM, YEXT[0], vget_low_f32(XX[0]), 1);
173       YEXT[1] = vextq_f32(YY[0], YY[1], 2);
174       SUMM = vmlaq_lane_f32(SUMM, YEXT[1], vget_high_f32(XX[0]), 0);
175       YEXT[2] = vextq_f32(YY[0], YY[1], 3);
176       SUMM = vmlaq_lane_f32(SUMM, YEXT[2], vget_high_f32(XX[0]), 1);
177 
178       YY[0] = YY[1];
179       len -= 4;
180    }
181 
182    while (--len > 0) {
183       XX_2 = vld1_dup_f32(xi++);
184       SUMM = vmlaq_lane_f32(SUMM, YY[0], XX_2, 0);
185       YY[0]= vld1q_f32(++yi);
186    }
187 
188    XX_2 = vld1_dup_f32(xi);
189    SUMM = vmlaq_lane_f32(SUMM, YY[0], XX_2, 0);
190 
191    vst1q_f32(sum, SUMM);
192 }
193 
194 /*
195  * Function: xcorr_kernel_neon_float_process1
196  * ---------------------------------
197  * Computes single correlation values and stores in *sum
198  */
xcorr_kernel_neon_float_process1(const float32_t * x,const float32_t * y,float32_t * sum,int len)199 static void xcorr_kernel_neon_float_process1(const float32_t *x,
200       const float32_t *y, float32_t *sum, int len) {
201    float32x4_t XX[4];
202    float32x4_t YY[4];
203    float32x2_t XX_2;
204    float32x2_t YY_2;
205    float32x4_t SUMM;
206    float32x2_t SUMM_2[2];
207    const float32_t *xi = x;
208    const float32_t *yi = y;
209 
210    SUMM = vdupq_n_f32(0);
211 
212    /* Work on 16 values per iteration */
213    while (len >= 16) {
214       XX[0] = vld1q_f32(xi);
215       xi += 4;
216       XX[1] = vld1q_f32(xi);
217       xi += 4;
218       XX[2] = vld1q_f32(xi);
219       xi += 4;
220       XX[3] = vld1q_f32(xi);
221       xi += 4;
222 
223       YY[0] = vld1q_f32(yi);
224       yi += 4;
225       YY[1] = vld1q_f32(yi);
226       yi += 4;
227       YY[2] = vld1q_f32(yi);
228       yi += 4;
229       YY[3] = vld1q_f32(yi);
230       yi += 4;
231 
232       SUMM = vmlaq_f32(SUMM, YY[0], XX[0]);
233       SUMM = vmlaq_f32(SUMM, YY[1], XX[1]);
234       SUMM = vmlaq_f32(SUMM, YY[2], XX[2]);
235       SUMM = vmlaq_f32(SUMM, YY[3], XX[3]);
236       len -= 16;
237    }
238 
239    /* Work on 8 values */
240    if (len >= 8) {
241       XX[0] = vld1q_f32(xi);
242       xi += 4;
243       XX[1] = vld1q_f32(xi);
244       xi += 4;
245 
246       YY[0] = vld1q_f32(yi);
247       yi += 4;
248       YY[1] = vld1q_f32(yi);
249       yi += 4;
250 
251       SUMM = vmlaq_f32(SUMM, YY[0], XX[0]);
252       SUMM = vmlaq_f32(SUMM, YY[1], XX[1]);
253       len -= 8;
254    }
255 
256    /* Work on 4 values */
257    if (len >= 4) {
258       XX[0] = vld1q_f32(xi);
259       xi += 4;
260       YY[0] = vld1q_f32(yi);
261       yi += 4;
262       SUMM = vmlaq_f32(SUMM, YY[0], XX[0]);
263       len -= 4;
264    }
265 
266    /* Start accumulating results */
267    SUMM_2[0] = vget_low_f32(SUMM);
268    if (len >= 2) {
269       /* While at it, consume 2 more values if available */
270       XX_2 = vld1_f32(xi);
271       xi += 2;
272       YY_2 = vld1_f32(yi);
273       yi += 2;
274       SUMM_2[0] = vmla_f32(SUMM_2[0], YY_2, XX_2);
275       len -= 2;
276    }
277    SUMM_2[1] = vget_high_f32(SUMM);
278    SUMM_2[0] = vadd_f32(SUMM_2[0], SUMM_2[1]);
279    SUMM_2[0] = vpadd_f32(SUMM_2[0], SUMM_2[0]);
280    /* Ok, now we have result accumulated in SUMM_2[0].0 */
281 
282    if (len > 0) {
283       /* Case when you have one value left */
284       XX_2 = vld1_dup_f32(xi);
285       YY_2 = vld1_dup_f32(yi);
286       SUMM_2[0] = vmla_f32(SUMM_2[0], XX_2, YY_2);
287    }
288 
289    vst1_lane_f32(sum, SUMM_2[0], 0);
290 }
291 
celt_pitch_xcorr_float_neon(const opus_val16 * _x,const opus_val16 * _y,opus_val32 * xcorr,int len,int max_pitch)292 void celt_pitch_xcorr_float_neon(const opus_val16 *_x, const opus_val16 *_y,
293                         opus_val32 *xcorr, int len, int max_pitch) {
294    int i;
295    celt_assert(max_pitch > 0);
296    celt_assert((((unsigned char *)_x-(unsigned char *)NULL)&3)==0);
297 
298    for (i = 0; i < (max_pitch-3); i += 4) {
299       xcorr_kernel_neon_float((const float32_t *)_x, (const float32_t *)_y+i,
300             (float32_t *)xcorr+i, len);
301    }
302 
303    /* In case max_pitch isn't multiple of 4
304     * compute single correlation value per iteration
305     */
306    for (; i < max_pitch; i++) {
307       xcorr_kernel_neon_float_process1((const float32_t *)_x,
308             (const float32_t *)_y+i, (float32_t *)xcorr+i, len);
309    }
310 }
311 #endif
312