Debian Codegraph @

libspeex/filters_bfin.h

raw
1 /* Copyright (C) 2005 Analog Devices */
2 /**
3 @file filters_bfin.h
4 @brief Various analysis/synthesis filters (Blackfin version)
5 */
6 /*
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 - Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13
14 - Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17
18 - Neither the name of the Xiph.org Foundation nor the names of its
19 contributors may be used to endorse or promote products derived from
20 this software without specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
26 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include "bfin.h"
36
37 #define OVERRIDE_NORMALIZE16
38 int normalize16(const spx_sig_t *x, spx_word16_t *y, spx_sig_t max_scale, int len)
39 {
40 spx_sig_t max_val=1;
41 int sig_shift;
42 __asm__
43 (
44 "%0 = 0;\n\t"
45 "I0 = %1;\n\t"
46 "L0 = 0;\n\t"
47 "R1 = [I0++];\n\t"
48 "LOOP norm_max%= LC0 = %2;\n\t"
49 "LOOP_BEGIN norm_max%=;\n\t"
50 "R2 = ABS R1 || R1 = [I0++];\n\t"
51 "%0 = MAX(%0, R2);\n\t"
52 "LOOP_END norm_max%=;\n\t"
53 : "=&d" (max_val)
54 : "a" (x), "a" (len)
55 : "R1", "R2", "ASTAT" BFIN_HWLOOP0_REGS
56 );
57
58 sig_shift=0;
59 while (max_val>max_scale)
60 {
61 sig_shift++;
62 max_val >>= 1;
63 }
64
65 __asm__ __volatile__
66 (
67 "I0 = %0;\n\t"
68 "L0 = 0;\n\t"
69 "P1 = %1;\n\t"
70 "R0 = [I0++];\n\t"
71 "LOOP norm_shift%= LC0 = %3;\n\t"
72 "LOOP_BEGIN norm_shift%=;\n\t"
73 "R1 = ASHIFT R0 by %2.L || R0 = [I0++];\n\t"
74 "W[P1++] = R1;\n\t"
75 "LOOP_END norm_shift%=;\n\t"
76 "R1 = ASHIFT R0 by %2.L;\n\t"
77 "W[P1++] = R1;\n\t"
78 : : "a" (x), "a" (y), "d" (-sig_shift), "a" (len-1)
79 : "I0", "L0", "P1", "R0", "R1", "memory", "ASTAT" BFIN_HWLOOP0_REGS
80 );
81 return sig_shift;
82 }
83
84
85
86 #define OVERRIDE_FILTER_MEM16
87 void filter_mem16(const spx_word16_t *_x, const spx_coef_t *num, const spx_coef_t *den, spx_word16_t *_y, int N, int ord, spx_mem_t *mem, char *stack)
88 {
89 VARDECL(spx_word32_t *xy2);
90 VARDECL(spx_word32_t *numden_a);
91 spx_word32_t *xy;
92 spx_word16_t *numden;
93 int i;
94
95 ALLOC(xy2, (N+1), spx_word32_t);
96 ALLOC(numden_a, (2*ord+2), spx_word32_t);
97 xy = xy2+1;
98 numden = (spx_word16_t*) numden_a;
99
100 for (i=0;i<ord;i++)
101 {
102 numden[2*i] = num[i];
103 numden[2*i+1] = den[i];
104 }
105 __asm__ __volatile__
106 (
107 /* Register setup */
108 "R0 = %5;\n\t" /*ord */
109
110 "P0 = %3;\n\t"
111 "I0 = P0;\n\t"
112 "B0 = P0;\n\t" /* numden */
113 "L0 = 0;\n\t"
114
115 "P2 = %0;\n\t" /* Fused xy */
116 "I2 = P2;\n\t"
117 "L2 = 0;\n\t"
118
119 "P4 = %6;\n\t" /* mem */
120 "P0 = %1;\n\t" /* _x */
121 "P1 = %2;\n\t" /* _y */
122
123 /* First sample */
124 "R1 = [P4++];\n\t"
125 "R1 <<= 3;\n\t" /* shift mem */
126 "R1.L = R1 (RND);\n\t"
127 "R2 = W[P0++];\n\t" /* load x[0] */
128 "R1.L = R1.L + R2.L;\n\t"
129 "W[P1++] = R1;\n\t" /* store y[0] */
130 "R2 = PACK(R1.L, R2.L);\n\t" /* pack x16 and y16 */
131 "[P2] = R2;\n\t"
132
133 /* Samples 1 to ord-1 (using memory) */
134 "R0 += -1;\n\t"
135 "R3 = 0;\n\t"
136 "LC0 = R0;\n\t"
137 "LOOP filter_start%= LC0;\n\t"
138 "LOOP_BEGIN filter_start%=;\n\t"
139 "R3 += 1;\n\t"
140 "LC1 = R3;\n\t"
141
142 "R1 = [P4++];\n\t"
143 "A1 = R1;\n\t"
144 "A0 = 0;\n\t"
145 "I0 = B0;\n\t"
146 "I2 = P2;\n\t"
147 "P2 += 4;\n\t"
148 "R4 = [I0++] || R5 = [I2--];\n\t"
149 "LOOP filter_start_inner%= LC1;\n\t"
150 "LOOP_BEGIN filter_start_inner%=;\n\t"
151 "A1 -= R4.H*R5.H, A0 += R4.L*R5.L (IS) || R4 = [I0++] || R5 = [I2--];\n\t"
152 "LOOP_END filter_start_inner%=;\n\t"
153 "A0 += A1;\n\t"
154 "R4 = A0;\n\t"
155 "R4 <<= 3;\n\t" /* shift mem */
156 "R4.L = R4 (RND);\n\t"
157 "R2 = W[P0++];\n\t" /* load x */
158 "R4.L = R4.L + R2.L;\n\t"
159 "W[P1++] = R4;\n\t" /* store y */
160 //"R4 <<= 2;\n\t"
161 //"R2 <<= 2;\n\t"
162 "R2 = PACK(R4.L, R2.L);\n\t" /* pack x16 and y16 */
163 "[P2] = R2;\n\t"
164
165 "LOOP_END filter_start%=;\n\t"
166
167 /* Samples ord to N*/
168 "R0 = %5;\n\t"
169 "R0 <<= 1;\n\t"
170 "I0 = B0;\n\t" /* numden */
171 "R0 <<= 1;\n\t"
172 "L0 = R0;\n\t"
173
174 "R0 = %5;\n\t" /* org */
175 "R2 = %4;\n\t" /* N */
176 "R2 = R2 - R0;\n\t"
177 "R4 = [I0++];\n\t" /* numden */
178 "LC0 = R2;\n\t"
179 "P3 = R0;\n\t"
180 "R0 <<= 2;\n\t"
181 "R0 += 8;\n\t"
182 "I2 = P2;\n\t"
183 "M0 = R0;\n\t"
184 "A1 = A0 = 0;\n\t"
185 "R5 = [I2--];\n\t" /* load xy */
186 "LOOP filter_mid%= LC0;\n\t"
187 "LOOP_BEGIN filter_mid%=;\n\t"
188 "LOOP filter_mid_inner%= LC1=P3;\n\t"
189 "LOOP_BEGIN filter_mid_inner%=;\n\t"
190 "A1 -= R4.H*R5.H, A0 += R4.L*R5.L (IS) || R4 = [I0++] || R5 = [I2--];\n\t"
191 "LOOP_END filter_mid_inner%=;\n\t"
192 "R0 = (A0 += A1) || I2 += M0;\n\t"
193 "R0 = R0 << 3 || R5 = W[P0++];\n\t" /* load x */
194 "R0.L = R0 (RND);\n\t"
195 "R0.L = R0.L + R5.L;\n\t"
196 "R5 = PACK(R0.L, R5.L) || W[P1++] = R0;\n\t" /* shift y | store y */
197 "A1 = A0 = 0 || [I2--] = R5\n\t"
198 "LOOP_END filter_mid%=;\n\t"
199 "I2 += 4;\n\t"
200 "P2 = I2;\n\t"
201 /* Update memory */
202 "P4 = %6;\n\t"
203 "R0 = %5;\n\t"
204 "LC0 = R0;\n\t"
205 "P0 = B0;\n\t"
206 "A1 = A0 = 0;\n\t"
207 "LOOP mem_update%= LC0;\n\t"
208 "LOOP_BEGIN mem_update%=;\n\t"
209 "I2 = P2;\n\t"
210 "I0 = P0;\n\t"
211 "P0 += 4;\n\t"
212 "R0 = LC0;\n\t"
213 "LC1 = R0;\n\t"
214 "R5 = [I2--] || R4 = [I0++];\n\t"
215 "LOOP mem_accum%= LC1;\n\t"
216 "LOOP_BEGIN mem_accum%=;\n\t"
217 "A1 -= R4.H*R5.H, A0 += R4.L*R5.L (IS) || R4 = [I0++] || R5 = [I2--];\n\t"
218 "LOOP_END mem_accum%=;\n\t"
219 "R0 = (A0 += A1);\n\t"
220 "A1 = A0 = 0 || [P4++] = R0;\n\t"
221 "LOOP_END mem_update%=;\n\t"
222 "L0 = 0;\n\t"
223 : : "m" (xy), "m" (_x), "m" (_y), "m" (numden), "m" (N), "m" (ord), "m" (mem)
224 : "A0", "A1", "R0", "R1", "R2", "R3", "R4", "R5", "P0", "P1", "P2", "P3", "P4", "B0", "I0", "I2", "L0", "L2", "M0", "memory",
225 "ASTAT" BFIN_HWLOOP0_REGS BFIN_HWLOOP1_REGS
226 );
227
228 }
229
230
231
232 #define OVERRIDE_IIR_MEM16
233 void iir_mem16(const spx_word16_t *_x, const spx_coef_t *den, spx_word16_t *_y, int N, int ord, spx_mem_t *mem, char *stack)
234 {
235 VARDECL(spx_word16_t *y);
236 spx_word16_t *yy;
237
238 ALLOC(y, (N+2), spx_word16_t);
239 yy = y+2;
240
241 __asm__ __volatile__
242 (
243 /* Register setup */
244 "R0 = %5;\n\t" /*ord */
245
246 "P1 = %3;\n\t"
247 "I1 = P1;\n\t"
248 "B1 = P1;\n\t"
249 "L1 = 0;\n\t"
250
251 "P3 = %0;\n\t"
252 "I3 = P3;\n\t"
253 "L3 = 0;\n\t"
254
255 "P4 = %6;\n\t"
256 "P0 = %1;\n\t"
257 "P1 = %2;\n\t"
258
259 /* First sample */
260 "R1 = [P4++];\n\t"
261 "R1 = R1 << 3 (S);\n\t"
262 "R1.L = R1 (RND);\n\t"
263 "R2 = W[P0++];\n\t"
264 "R1 = R1 + R2;\n\t"
265 "W[P1++] = R1;\n\t"
266 "W[P3] = R1;\n\t"
267
268 /* Samples 1 to ord-1 (using memory) */
269 "R0 += -1;\n\t"
270 "R3 = 0;\n\t"
271 "LC0 = R0;\n\t"
272 "LOOP filter_start%= LC0;\n\t"
273 "LOOP_BEGIN filter_start%=;\n\t"
274 "R3 += 1;\n\t"
275 "LC1 = R3;\n\t"
276
277 "R1 = [P4++];\n\t"
278 "A1 = R1;\n\t"
279 "I1 = B1;\n\t"
280 "I3 = P3;\n\t"
281 "P3 += 2;\n\t"
282 "LOOP filter_start_inner%= LC1;\n\t"
283 "LOOP_BEGIN filter_start_inner%=;\n\t"
284 "R4.L = W[I1++];\n\t"
285 "R5.L = W[I3--];\n\t"
286 "A1 -= R4.L*R5.L (IS);\n\t"
287 "LOOP_END filter_start_inner%=;\n\t"
288
289 "R1 = A1;\n\t"
290 "R1 <<= 3;\n\t"
291 "R1.L = R1 (RND);\n\t"
292 "R2 = W[P0++];\n\t"
293 "R1 = R1 + R2;\n\t"
294 "W[P1++] = R1;\n\t"
295 "W[P3] = R1;\n\t"
296 "LOOP_END filter_start%=;\n\t"
297
298 /* Samples ord to N*/
299 "R0 = %5;\n\t"
300 "R0 <<= 1;\n\t"
301 "I1 = B1;\n\t"
302 "L1 = R0;\n\t"
303
304 "R0 = %5;\n\t"
305 "R2 = %4;\n\t"
306 "R2 = R2 - R0;\n\t"
307 "R4.L = W[I1++];\n\t"
308 "LC0 = R2;\n\t"
309 "LOOP filter_mid%= LC0;\n\t"
310 "LOOP_BEGIN filter_mid%=;\n\t"
311 "LC1 = R0;\n\t"
312 "A1 = 0;\n\t"
313 "I3 = P3;\n\t"
314 "P3 += 2;\n\t"
315 "R5.L = W[I3--];\n\t"
316 "LOOP filter_mid_inner%= LC1;\n\t"
317 "LOOP_BEGIN filter_mid_inner%=;\n\t"
318 "A1 -= R4.L*R5.L (IS) || R4.L = W[I1++] || R5.L = W[I3--];\n\t"
319 "LOOP_END filter_mid_inner%=;\n\t"
320 "R1 = A1;\n\t"
321 "R1 = R1 << 3 || R2 = W[P0++];\n\t"
322 "R1.L = R1 (RND);\n\t"
323 "R1 = R1 + R2;\n\t"
324 "W[P1++] = R1;\n\t"
325 "W[P3] = R1;\n\t"
326 "LOOP_END filter_mid%=;\n\t"
327
328 /* Update memory */
329 "P4 = %6;\n\t"
330 "R0 = %5;\n\t"
331 "LC0 = R0;\n\t"
332 "P1 = B1;\n\t"
333 "LOOP mem_update%= LC0;\n\t"
334 "LOOP_BEGIN mem_update%=;\n\t"
335 "A0 = 0;\n\t"
336 "I3 = P3;\n\t"
337 "I1 = P1;\n\t"
338 "P1 += 2;\n\t"
339 "R0 = LC0;\n\t"
340 "LC1=R0;\n\t"
341 "R5.L = W[I3--] || R4.L = W[I1++];\n\t"
342 "LOOP mem_accum%= LC1;\n\t"
343 "LOOP_BEGIN mem_accum%=;\n\t"
344 "A0 -= R4.L*R5.L (IS) || R4.L = W[I1++] || R5.L = W[I3--];\n\t"
345 "LOOP_END mem_accum%=;\n\t"
346 "R0 = A0;\n\t"
347 "[P4++] = R0;\n\t"
348 "LOOP_END mem_update%=;\n\t"
349 "L1 = 0;\n\t"
350 : : "m" (yy), "m" (_x), "m" (_y), "m" (den), "m" (N), "m" (ord), "m" (mem)
351 : "A0", "A1", "R0", "R1", "R2", "R3", "R4", "R5", "P0", "P1", "P2", "P3", "P4", "B1", "I1", "I3", "L1", "L3", "memory",
352 "ASTAT" BFIN_HWLOOP0_REGS BFIN_HWLOOP1_REGS
353 );
354
355 }
356
357
358 #define OVERRIDE_FIR_MEM16
359 void fir_mem16(const spx_word16_t *x, const spx_coef_t *num, spx_word16_t *y, int N, int ord, spx_mem_t *mem, char *stack)
360 {
361 int i;
362 spx_coef_t den2[12];
363 spx_coef_t *den;
364 den = (spx_coef_t*)((((int)den2)+4)&0xfffffffc);
365 for (i=0;i<10;i++)
366 den[i] = 0;
367 filter_mem16(x, num, den, y, N, ord, mem, stack);
368 }
369
370
371 #define OVERRIDE_COMPUTE_IMPULSE_RESPONSE
372 void compute_impulse_response(const spx_coef_t *ak, const spx_coef_t *awk1, const spx_coef_t *awk2, spx_word16_t *y, int N, int ord, char *stack)
373 {
374 int i;
375 VARDECL(spx_word16_t *ytmp);
376 ALLOC(ytmp, N, spx_word16_t);
377 spx_word16_t *ytmp2 = ytmp;
378 y[0] = LPC_SCALING;
379 for (i=0;i<ord;i++)
380 y[i+1] = awk1[i];
381 i++;
382 for (;i<N;i++)
383 y[i] = 0;
384
385 N-=1;
386 __asm__ __volatile__
387 (
388 "I0 = %0;\n\t"
389 "I1 = %1;\n\t"
390 "L0 = 0;\n\t"
391 "L1 = 0;\n\t"
392 "L2 = 0;\n\t"
393 "L3 = 0;\n\t"
394 "R0 = 1;\n\t"
395 "R0 <<= 13;\n\t"
396 "W[I0] = R0.L;\n\t"
397 "R0 <<= 1;\n\t"
398 "W[I1] = R0.L;\n\t"
399 "R0 = %5;\n\t"
400 "LC0 = R0;\n\t"
401 "R2 = 0;\n\t"
402 "LOOP samples%= LC0;\n\t"
403 "LOOP_BEGIN samples%=;\n\t"
404 "R2 += 1;\n\t"
405 "R2 = MIN(R2, %4);\n\t"
406 "I0 = %0;\n\t"
407 "I1 = %1;\n\t"
408 "I2 = %2;\n\t"
409 "I3 = %3;\n\t"
410 "%0 += 2;\n\t"
411 "%1 += 2;\n\t"
412 "A1 = A0 = 0;\n\t"
413 "R0.L = W[I0--] || R1.L = W[I2++];\n\t"
414 "LC1 = R2;\n\t"
415 "LOOP filter%= LC1;\n\t"
416 "LOOP_BEGIN filter%=;\n\t"
417 "A0 -= R0.L*R1.L (IS) || R0.L = W[I1--] || R1.L = W[I3++];\n\t"
418 "A1 -= R0.L*R1.L (IS) || R0.L = W[I0--] || R1.L = W[I2++];\n\t"
419 "LOOP_END filter%=;\n\t"
420 "R0 = A0, R1 = A1;\n\t"
421 "R3 = W[%1] (X);\n\t"
422 "R3 <<= 13;\n\t"
423 "R0 = R0 + R3;\n\t"
424 "R3 = R0 >>> 13;\n\t"
425 "W[%0] = R3.L;\n\t"
426 "R0 <<= 1;\n\t"
427 "R1 = R1 + R0;\n\t"
428 "R1 >>>= 13;\n\t"
429 "W[%1] = R1.L;\n\t"
430 "LOOP_END samples%=;\n\t"
431 : "=a" (ytmp2), "=a" (y)
432 : "a" (awk2), "a" (ak), "d" (ord), "m" (N), "0" (ytmp2), "1" (y)
433 : "A0", "A1", "R0", "R1", "R2", "R3", "I0", "I1", "I2", "I3", "L0", "L1", "L2", "L3",
434 "ASTAT" BFIN_HWLOOP0_REGS BFIN_HWLOOP1_REGS
435 );
436 }
437
438
439
440 #if 0 /* Equivalent C function for filter_mem2 and compute_impulse_response */
441 #define min(a,b) ((a)<(b) ? (a):(b))
442
443 void compute_impulse_response(const spx_coef_t *ak, const spx_coef_t *awk1, const spx_coef_t *awk2, spx_word16_t *y, int N, int ord, char *stack)
444 {
445 int i,j;
446 VARDECL(spx_word16_t *ytmp);
447 ALLOC(ytmp, N, spx_word16_t);
448
449 y[0] = LPC_SCALING;
450 for (i=0;i<ord;i++)
451 y[i+1] = awk1[i];
452 i++;
453 for (;i<N;i++)
454 y[i] = 0;
455
456 for (i=0;i<N;i++)
457 {
458 spx_word32_t yi = SHL32(EXTEND32(y[i]),LPC_SHIFT);
459 spx_word32_t yi2 = 0;
460 for (j=0;j<min(i,ord);j++)
461 {
462 yi = MAC16_16(yi, awk2[j], -ytmp[i-j-1]);
463 yi2 = MAC16_16(yi2, ak[j], -y[i-j-1]);
464 }
465 ytmp[i] = EXTRACT16(SHR32(yi,LPC_SHIFT));
466 yi2 = ADD32(yi2,SHL32(yi,1));
467 y[i] = EXTRACT16(SHR32(yi2,LPC_SHIFT));
468 }
469
470 }
471
472
473 void filter_mem2(const spx_sig_t *_x, const spx_coef_t *num, const spx_coef_t *den, spx_sig_t *_y, int N, int ord, spx_mem_t *mem)
474 {
475 int i,j;
476 spx_word16_t xi,yi,nyi;
477 spx_word16_t x[N],y[N];
478 spx_word16_t *xx, *yy;
479 xx = x;
480 yy = y;
481 for (i=0;i<N;i++)
482 {
483 x[i] = EXTRACT16(SHR32(_x[i],SIG_SHIFT));
484 }
485
486 for (i=0;i<ord;i++)
487 {
488 spx_word32_t yi = mem[i];
489 for (j=0;j<i;j++)
490 {
491 yi = MAC16_16(yi, num[j], x[i-j-1]);
492 yi = MAC16_16(yi, den[j], -y[i-j-1]);
493 }
494 _y[i] = ADD32(_x[i],SHL32(yi,1));
495 y[i] = EXTRACT16(SHR32(_y[i],SIG_SHIFT));
496 }
497 for (i=ord;i<N;i++)
498 {
499 spx_word32_t yi = 0;
500 for (j=0;j<ord;j++)
501 {
502 yi = MAC16_16(yi, num[j], x[i-j-1]);
503 yi = MAC16_16(yi, den[j], -y[i-j-1]);
504 }
505 _y[i] = ADD32(_x[i],SHL32(yi,1));
506 y[i] = EXTRACT16(SHR32(_y[i],SIG_SHIFT));
507 }
508
509 for (i=0;i<ord;i++)
510 {
511 spx_mem_t m = 0;
512 for (j=0;j<ord-i;j++)
513 {
514 m = MAC16_16(m, x[N-1-j], num[j+i]);
515 m = MAC16_16(m, -y[N-1-j], den[j+i]);
516 }
517 mem[i] = m;
518 }
519 }
520 #endif