xref: /aosp_15_r20/external/speex/libspeexdsp/resample_neon.h (revision 28e138c64d234588b5cd2a8a403b584bd3036e4e)
1 /* Copyright (C) 2007-2008 Jean-Marc Valin
2  * Copyright (C) 2008 Thorvald Natvig
3  * Copyright (C) 2011 Texas Instruments
4  *               author Jyri Sarha
5  */
6 /**
7    @file resample_neon.h
8    @brief Resampler functions (NEON version)
9 */
10 /*
11    Redistribution and use in source and binary forms, with or without
12    modification, are permitted provided that the following conditions
13    are met:
14 
15    - Redistributions of source code must retain the above copyright
16    notice, this list of conditions and the following disclaimer.
17 
18    - Redistributions in binary form must reproduce the above copyright
19    notice, this list of conditions and the following disclaimer in the
20    documentation and/or other materials provided with the distribution.
21 
22    - Neither the name of the Xiph.org Foundation nor the names of its
23    contributors may be used to endorse or promote products derived from
24    this software without specific prior written permission.
25 
26    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
30    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 
39 #ifdef FIXED_POINT
40 #if defined(__aarch64__)
saturate_32bit_to_16bit(int32_t a)41 static inline int32_t saturate_32bit_to_16bit(int32_t a) {
42     int32_t ret;
43     asm ("fmov s0, %w[a]\n"
44          "sqxtn h0, s0\n"
45          "sxtl v0.4s, v0.4h\n"
46          "fmov %w[ret], s0\n"
47          : [ret] "=r" (ret)
48          : [a] "r" (a)
49          : "v0" );
50     return ret;
51 }
52 #elif defined(__thumb2__)
saturate_32bit_to_16bit(int32_t a)53 static inline int32_t saturate_32bit_to_16bit(int32_t a) {
54     int32_t ret;
55     asm ("ssat %[ret], #16, %[a]"
56          : [ret] "=r" (ret)
57          : [a] "r" (a)
58          : );
59     return ret;
60 }
61 #else
saturate_32bit_to_16bit(int32_t a)62 static inline int32_t saturate_32bit_to_16bit(int32_t a) {
63     int32_t ret;
64     asm ("vmov.s32 d0[0], %[a]\n"
65          "vqmovn.s32 d0, q0\n"
66          "vmov.s16 %[ret], d0[0]\n"
67          : [ret] "=r" (ret)
68          : [a] "r" (a)
69          : "q0");
70     return ret;
71 }
72 #endif
73 #undef WORD2INT
74 #define WORD2INT(x) (saturate_32bit_to_16bit(x))
75 
76 #define OVERRIDE_INNER_PRODUCT_SINGLE
77 /* Only works when len % 4 == 0 and len >= 4 */
78 #if defined(__aarch64__)
inner_product_single(const int16_t * a,const int16_t * b,unsigned int len)79 static inline int32_t inner_product_single(const int16_t *a, const int16_t *b, unsigned int len)
80 {
81     int32_t ret;
82     uint32_t remainder = len % 16;
83     len = len - remainder;
84 
85     asm volatile ("	 cmp %w[len], #0\n"
86 		  "	 b.ne 1f\n"
87 		  "	 ld1 {v16.4h}, [%[b]], #8\n"
88 		  "	 ld1 {v20.4h}, [%[a]], #8\n"
89 		  "	 subs %w[remainder], %w[remainder], #4\n"
90 		  "	 smull v0.4s, v16.4h, v20.4h\n"
91 		  "      b.ne 4f\n"
92 		  "	 b 5f\n"
93 		  "1:"
94 		  "	 ld1 {v16.4h, v17.4h, v18.4h, v19.4h}, [%[b]], #32\n"
95 		  "	 ld1 {v20.4h, v21.4h, v22.4h, v23.4h}, [%[a]], #32\n"
96 		  "	 subs %w[len], %w[len], #16\n"
97 		  "	 smull v0.4s, v16.4h, v20.4h\n"
98 		  "	 smlal v0.4s, v17.4h, v21.4h\n"
99 		  "	 smlal v0.4s, v18.4h, v22.4h\n"
100 		  "	 smlal v0.4s, v19.4h, v23.4h\n"
101 		  "	 b.eq 3f\n"
102 		  "2:"
103 		  "	 ld1 {v16.4h, v17.4h, v18.4h, v19.4h}, [%[b]], #32\n"
104 		  "	 ld1 {v20.4h, v21.4h, v22.4h, v23.4h}, [%[a]], #32\n"
105 		  "	 subs %w[len], %w[len], #16\n"
106 		  "	 smlal v0.4s, v16.4h, v20.4h\n"
107 		  "	 smlal v0.4s, v17.4h, v21.4h\n"
108 		  "	 smlal v0.4s, v18.4h, v22.4h\n"
109 		  "	 smlal v0.4s, v19.4h, v23.4h\n"
110 		  "	 b.ne 2b\n"
111 		  "3:"
112 		  "	 cmp %w[remainder], #0\n"
113 		  "	 b.eq 5f\n"
114 		  "4:"
115 		  "	 ld1 {v18.4h}, [%[b]], #8\n"
116 		  "	 ld1 {v22.4h}, [%[a]], #8\n"
117 		  "	 subs %w[remainder], %w[remainder], #4\n"
118 		  "	 smlal v0.4s, v18.4h, v22.4h\n"
119 		  "	 b.ne 4b\n"
120 		  "5:"
121 		  "	 saddlv d0, v0.4s\n"
122 		  "	 sqxtn s0, d0\n"
123 		  "	 sqrshrn h0, s0, #15\n"
124 		  "	 sxtl v0.4s, v0.4h\n"
125 		  "	 fmov %w[ret], s0\n"
126 		  : [ret] "=r" (ret), [a] "+r" (a), [b] "+r" (b),
127 		    [len] "+r" (len), [remainder] "+r" (remainder)
128 		  :
129 		  : "cc", "v0",
130 		    "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23");
131     return ret;
132 }
133 #else
inner_product_single(const int16_t * a,const int16_t * b,unsigned int len)134 static inline int32_t inner_product_single(const int16_t *a, const int16_t *b, unsigned int len)
135 {
136     int32_t ret;
137     uint32_t remainder = len % 16;
138     len = len - remainder;
139 
140     asm volatile ("	 cmp %[len], #0\n"
141 		  "	 bne 1f\n"
142 		  "	 vld1.16 {d16}, [%[b]]!\n"
143 		  "	 vld1.16 {d20}, [%[a]]!\n"
144 		  "	 subs %[remainder], %[remainder], #4\n"
145 		  "	 vmull.s16 q0, d16, d20\n"
146 		  "      beq 5f\n"
147 		  "	 b 4f\n"
148 		  "1:"
149 		  "	 vld1.16 {d16, d17, d18, d19}, [%[b]]!\n"
150 		  "	 vld1.16 {d20, d21, d22, d23}, [%[a]]!\n"
151 		  "	 subs %[len], %[len], #16\n"
152 		  "	 vmull.s16 q0, d16, d20\n"
153 		  "	 vmlal.s16 q0, d17, d21\n"
154 		  "	 vmlal.s16 q0, d18, d22\n"
155 		  "	 vmlal.s16 q0, d19, d23\n"
156 		  "	 beq 3f\n"
157 		  "2:"
158 		  "	 vld1.16 {d16, d17, d18, d19}, [%[b]]!\n"
159 		  "	 vld1.16 {d20, d21, d22, d23}, [%[a]]!\n"
160 		  "	 subs %[len], %[len], #16\n"
161 		  "	 vmlal.s16 q0, d16, d20\n"
162 		  "	 vmlal.s16 q0, d17, d21\n"
163 		  "	 vmlal.s16 q0, d18, d22\n"
164 		  "	 vmlal.s16 q0, d19, d23\n"
165 		  "	 bne 2b\n"
166 		  "3:"
167 		  "	 cmp %[remainder], #0\n"
168 		  "	 beq 5f\n"
169 		  "4:"
170 		  "	 vld1.16 {d16}, [%[b]]!\n"
171 		  "	 vld1.16 {d20}, [%[a]]!\n"
172 		  "	 subs %[remainder], %[remainder], #4\n"
173 		  "	 vmlal.s16 q0, d16, d20\n"
174 		  "	 bne 4b\n"
175 		  "5:"
176 		  "	 vaddl.s32 q0, d0, d1\n"
177 		  "	 vadd.s64 d0, d0, d1\n"
178 		  "	 vqmovn.s64 d0, q0\n"
179 		  "	 vqrshrn.s32 d0, q0, #15\n"
180 		  "	 vmov.s16 %[ret], d0[0]\n"
181 		  : [ret] "=r" (ret), [a] "+r" (a), [b] "+r" (b),
182 		    [len] "+r" (len), [remainder] "+r" (remainder)
183 		  :
184 		  : "cc", "q0",
185 		    "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23");
186 
187     return ret;
188 }
189 #endif  // !defined(__aarch64__)
190 
191 #elif defined(FLOATING_POINT)
192 #if defined(__aarch64__)
saturate_float_to_16bit(float a)193 static inline int32_t saturate_float_to_16bit(float a) {
194     int32_t ret;
195     asm ("fcvtas s1, %s[a]\n"
196          "sqxtn h1, s1\n"
197          "sxtl v1.4s, v1.4h\n"
198          "fmov %w[ret], s1\n"
199          : [ret] "=r" (ret)
200          : [a] "w" (a)
201          : "v1");
202     return ret;
203 }
204 #else
saturate_float_to_16bit(float a)205 static inline int32_t saturate_float_to_16bit(float a) {
206     int32_t ret;
207     asm ("vmov.f32 d0[0], %[a]\n"
208          "vcvt.s32.f32 d0, d0, #15\n"
209          "vqrshrn.s32 d0, q0, #15\n"
210          "vmov.s16 %[ret], d0[0]\n"
211          : [ret] "=r" (ret)
212          : [a] "r" (a)
213          : "q0");
214     return ret;
215 }
216 #endif
217 
218 #undef WORD2INT
219 #define WORD2INT(x) (saturate_float_to_16bit(x))
220 
221 #define OVERRIDE_INNER_PRODUCT_SINGLE
222 /* Only works when len % 4 == 0 and len >= 4 */
223 #if defined(__aarch64__)
inner_product_single(const float * a,const float * b,unsigned int len)224 static inline float inner_product_single(const float *a, const float *b, unsigned int len)
225 {
226     float ret;
227     uint32_t remainder = len % 16;
228     len = len - remainder;
229 
230     asm volatile ("	 cmp %w[len], #0\n"
231 		  "	 b.ne 1f\n"
232 		  "	 ld1 {v16.4s}, [%[b]], #16\n"
233 		  "	 ld1 {v20.4s}, [%[a]], #16\n"
234 		  "	 subs %w[remainder], %w[remainder], #4\n"
235 		  "	 fmul v1.4s, v16.4s, v20.4s\n"
236 		  "      b.ne 4f\n"
237 		  "	 b 5f\n"
238 		  "1:"
239 		  "	 ld1 {v16.4s, v17.4s, v18.4s, v19.4s}, [%[b]], #64\n"
240 		  "	 ld1 {v20.4s, v21.4s, v22.4s, v23.4s}, [%[a]], #64\n"
241 		  "	 subs %w[len], %w[len], #16\n"
242 		  "	 fmul v1.4s, v16.4s, v20.4s\n"
243 		  "	 fmul v2.4s, v17.4s, v21.4s\n"
244 		  "	 fmul v3.4s, v18.4s, v22.4s\n"
245 		  "	 fmul v4.4s, v19.4s, v23.4s\n"
246 		  "	 b.eq 3f\n"
247 		  "2:"
248 		  "	 ld1 {v16.4s, v17.4s, v18.4s, v19.4s}, [%[b]], #64\n"
249 		  "	 ld1 {v20.4s, v21.4s, v22.4s, v23.4s}, [%[a]], #64\n"
250 		  "	 subs %w[len], %w[len], #16\n"
251 		  "	 fmla v1.4s, v16.4s, v20.4s\n"
252 		  "	 fmla v2.4s, v17.4s, v21.4s\n"
253 		  "	 fmla v3.4s, v18.4s, v22.4s\n"
254 		  "	 fmla v4.4s, v19.4s, v23.4s\n"
255 		  "	 b.ne 2b\n"
256 		  "3:"
257 		  "	 fadd v16.4s, v1.4s, v2.4s\n"
258 		  "	 fadd v17.4s, v3.4s, v4.4s\n"
259 		  "	 cmp %w[remainder], #0\n"
260 		  "	 fadd v1.4s, v16.4s, v17.4s\n"
261 		  "	 b.eq 5f\n"
262 		  "4:"
263 		  "	 ld1 {v18.4s}, [%[b]], #16\n"
264 		  "	 ld1 {v22.4s}, [%[a]], #16\n"
265 		  "	 subs %w[remainder], %w[remainder], #4\n"
266 		  "	 fmla v1.4s, v18.4s, v22.4s\n"
267 		  "	 b.ne 4b\n"
268 		  "5:"
269 		  "	 faddp v1.4s, v1.4s, v1.4s\n"
270 		  "	 faddp %[ret].4s, v1.4s, v1.4s\n"
271 		  : [ret] "=w" (ret), [a] "+r" (a), [b] "+r" (b),
272 		    [len] "+r" (len), [remainder] "+r" (remainder)
273 		  :
274 		  : "cc", "v1", "v2", "v3", "v4",
275 		    "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23");
276     return ret;
277 }
278 #else
inner_product_single(const float * a,const float * b,unsigned int len)279 static inline float inner_product_single(const float *a, const float *b, unsigned int len)
280 {
281     float ret;
282     uint32_t remainder = len % 16;
283     len = len - remainder;
284 
285     asm volatile ("	 cmp %[len], #0\n"
286 		  "	 bne 1f\n"
287 		  "	 vld1.32 {q4}, [%[b]]!\n"
288 		  "	 vld1.32 {q8}, [%[a]]!\n"
289 		  "	 subs %[remainder], %[remainder], #4\n"
290 		  "	 vmul.f32 q0, q4, q8\n"
291 		  "      bne 4f\n"
292 		  "	 b 5f\n"
293 		  "1:"
294 		  "	 vld1.32 {q4, q5}, [%[b]]!\n"
295 		  "	 vld1.32 {q8, q9}, [%[a]]!\n"
296 		  "	 vld1.32 {q6, q7}, [%[b]]!\n"
297 		  "	 vld1.32 {q10, q11}, [%[a]]!\n"
298 		  "	 subs %[len], %[len], #16\n"
299 		  "	 vmul.f32 q0, q4, q8\n"
300 		  "	 vmul.f32 q1, q5, q9\n"
301 		  "	 vmul.f32 q2, q6, q10\n"
302 		  "	 vmul.f32 q3, q7, q11\n"
303 		  "	 beq 3f\n"
304 		  "2:"
305 		  "	 vld1.32 {q4, q5}, [%[b]]!\n"
306 		  "	 vld1.32 {q8, q9}, [%[a]]!\n"
307 		  "	 vld1.32 {q6, q7}, [%[b]]!\n"
308 		  "	 vld1.32 {q10, q11}, [%[a]]!\n"
309 		  "	 subs %[len], %[len], #16\n"
310 		  "	 vmla.f32 q0, q4, q8\n"
311 		  "	 vmla.f32 q1, q5, q9\n"
312 		  "	 vmla.f32 q2, q6, q10\n"
313 		  "	 vmla.f32 q3, q7, q11\n"
314 		  "	 bne 2b\n"
315 		  "3:"
316 		  "	 vadd.f32 q4, q0, q1\n"
317 		  "	 vadd.f32 q5, q2, q3\n"
318 		  "	 cmp %[remainder], #0\n"
319 		  "	 vadd.f32 q0, q4, q5\n"
320 		  "	 beq 5f\n"
321 		  "4:"
322 		  "	 vld1.32 {q6}, [%[b]]!\n"
323 		  "	 vld1.32 {q10}, [%[a]]!\n"
324 		  "	 subs %[remainder], %[remainder], #4\n"
325 		  "	 vmla.f32 q0, q6, q10\n"
326 		  "	 bne 4b\n"
327 		  "5:"
328 		  "	 vadd.f32 d0, d0, d1\n"
329 		  "	 vpadd.f32 d0, d0, d0\n"
330 		  "	 vmov.f32 %[ret], d0[0]\n"
331 		  : [ret] "=r" (ret), [a] "+r" (a), [b] "+r" (b),
332 		    [len] "+l" (len), [remainder] "+l" (remainder)
333 		  :
334 		  : "cc", "q0", "q1", "q2", "q3",
335 		    "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11");
336     return ret;
337 }
338 #endif  // defined(__aarch64__)
339 #endif
340