filters_bfin.h
资源名称:tcpmp.rar [点击查看]
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:13k
源码类别:
Windows CE
开发平台:
C/C++
- /* Copyright (C) 2005 Analog Devices
- Author: Jean-Marc Valin
- File: filters_bfin.h
- Various analysis/synthesis filters (Blackfin version)
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- - Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- - Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- - Neither the name of the Xiph.org Foundation nor the names of its
- contributors may be used to endorse or promote products derived from
- this software without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <stdio.h>
- #define OVERRIDE_NORMALIZE16
- int normalize16(const spx_sig_t *x, spx_word16_t *y, spx_sig_t max_scale, int len)
- {
- spx_sig_t max_val=1;
- int sig_shift;
- __asm__
- (
- "%0 = 0;nt"
- "I0 = %1;nt"
- "L0 = 0;nt"
- "R1 = [I0++];nt"
- "LOOP norm_max%= LC0 = %2;nt"
- "LOOP_BEGIN norm_max%=;nt"
- "R2 = ABS R1 || R1 = [I0++];nt"
- "%0 = MAX(%0, R2);nt"
- "LOOP_END norm_max%=;nt"
- : "=&d" (max_val)
- : "a" (x), "a" (len)
- : "R1", "R2"
- );
- sig_shift=0;
- while (max_val>max_scale)
- {
- sig_shift++;
- max_val >>= 1;
- }
- __asm__ __volatile__
- (
- "I0 = %0;nt"
- "L0 = 0;nt"
- "I1 = %1;nt"
- "L1 = 0;nt"
- "R0 = [I0++];nt"
- "LOOP norm_shift%= LC0 = %3 >> 1;nt"
- "LOOP_BEGIN norm_shift%=;nt"
- "R1 = ASHIFT R0 by %2.L || R2 = [I0++];nt"
- "R3 = ASHIFT R2 by %2.L || R0 = [I0++];nt"
- "R3 = PACK(R3.L, R1.L);nt"
- "[I1++] = R3;nt"
- "LOOP_END norm_shift%=;nt"
- : : "a" (x), "a" (y), "d" (-sig_shift), "a" (len)
- : "I0", "L0", "I1", "L1", "R0", "R1", "R2", "R3", "memory"
- );
- return sig_shift;
- }
- #define OVERRIDE_FILTER_MEM2
- 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)
- {
- spx_word32_t xy2[N+1];
- spx_word32_t *xy = xy2+1;
- spx_word32_t numden_a[2*ord+2];
- spx_word16_t *numden = (spx_word16_t*) numden_a;
- int i;
- for (i=0;i<ord;i++)
- {
- numden[2*i] = num[i];
- numden[2*i+1] = den[i];
- }
- __asm__ __volatile__
- (
- /* Register setup */
- "R0 = %5;nt" /*ord */
- "P0 = %3;nt"
- "I0 = P0;nt"
- "B0 = P0;nt"
- "L0 = 0;nt"
- "P2 = %0;nt"
- "I2 = P2;nt"
- "L2 = 0;nt"
- "P4 = %6;nt"
- "P0 = %1;nt"
- "P1 = %2;nt"
- /* First sample */
- "R1 = [P4++];nt"
- "R1 <<= 1;nt"
- "R2 = [P0++];nt"
- "R1 = R1 + R2;nt"
- "[P1++] = R1;nt"
- "R1 <<= 2;nt"
- "R2 <<= 2;nt"
- "R2 = PACK(R1.H, R2.H);nt"
- "[P2] = R2;nt"
- /* Samples 1 to ord-1 (using memory) */
- "R0 += -1;nt"
- "R3 = 0;nt"
- "LC0 = R0;nt"
- "LOOP filter_start%= LC0;nt"
- "LOOP_BEGIN filter_start%=;nt"
- "R3 += 1;nt"
- "LC1 = R3;nt"
- "R1 = [P4++];nt"
- "A1 = R1;nt"
- "A0 = 0;nt"
- "I0 = B0;nt"
- "I2 = P2;nt"
- "P2 += 4;nt"
- "R4 = [I0++] || R5 = [I2--];nt"
- "LOOP filter_start_inner%= LC1;nt"
- "LOOP_BEGIN filter_start_inner%=;nt"
- "A0 += R4.L*R5.L (IS), A1 -= R4.H*R5.H (IS) || R4 = [I0++] || R5 = [I2--];nt"
- "LOOP_END filter_start_inner%=;nt"
- "A0 += A1;nt"
- "R4 = A0;nt"
- "R4 <<= 1;nt"
- "R2 = [P0++];nt"
- "R4 = R4 + R2;nt"
- "[P1++] = R4;nt"
- "R4 <<= 2;nt"
- "R2 <<= 2;nt"
- "R2 = PACK(R4.H, R2.H);nt"
- "[P2] = R2;nt"
- "LOOP_END filter_start%=;nt"
- /* Samples ord to N*/
- "R0 = %5;nt"
- "R0 <<= 1;nt"
- "I0 = B0;nt"
- "R0 <<= 1;nt"
- "L0 = R0;nt"
- "R0 = %5;nt"
- "R2 = %4;nt"
- "R2 = R2 - R0;nt"
- "R4 = [I0++];nt"
- "LC0 = R2;nt"
- "P3 = R0;nt"
- "R0 <<= 2;nt"
- "R0 += 8;nt"
- "I2 = P2;nt"
- "M0 = R0;nt"
- "A0 = A1 = 0;nt"
- "R5 = [I2--];nt"
- "LOOP filter_mid%= LC0;nt"
- "LOOP_BEGIN filter_mid%=;nt"
- "LOOP filter_mid_inner%= LC1=P3;nt"
- "LOOP_BEGIN filter_mid_inner%=;nt"
- "A0 += R4.L*R5.L (IS), A1 -= R4.H*R5.H (IS) || R4 = [I0++] || R5 = [I2--];nt"
- "LOOP_END filter_mid_inner%=;nt"
- "R0 = (A0 += A1) || I2 += M0;nt"
- "R0 = R0 << 1 || R5 = [P0++];nt"
- "R0 = R0 + R5;nt"
- "R0 = R0 << 2 || [P1++] = R0;nt"
- "R5 = R5 << 2;nt"
- "R5 = PACK(R0.H, R5.H);nt"
- "A0 = A1 = 0 || [I2--] = R5nt"
- "LOOP_END filter_mid%=;nt"
- "I2 += 4;nt"
- "P2 = I2;nt"
- /* Update memory */
- "P4 = %6;nt"
- "R0 = %5;nt"
- "LC0 = R0;nt"
- "P0 = B0;nt"
- "A0 = A1 = 0;nt"
- "LOOP mem_update%= LC0;nt"
- "LOOP_BEGIN mem_update%=;nt"
- "I2 = P2;nt"
- "I0 = P0;nt"
- "P0 += 4;nt"
- "R0 = LC0;nt"
- "LC1 = R0;nt"
- "R5 = [I2--] || R4 = [I0++];nt"
- "LOOP mem_accum%= LC1;nt"
- "LOOP_BEGIN mem_accum%=;nt"
- "A0 += R4.L*R5.L (IS), A1 -= R4.H*R5.H (IS) || R4 = [I0++] || R5 = [I2--];nt"
- "LOOP_END mem_accum%=;nt"
- "R0 = (A0 += A1);nt"
- "A0 = A1 = 0 || [P4++] = R0;nt"
- "LOOP_END mem_update%=;nt"
- "L0 = 0;nt"
- : : "m" (xy), "m" (_x), "m" (_y), "m" (numden), "m" (N), "m" (ord), "m" (mem)
- : "A0", "A1", "R0", "R1", "R2", "R3", "R4", "R5", "P0", "P1", "P2", "P3", "P4", "B0", "I0", "I2", "L0", "L2", "M0", "memory"
- );
- }
- #define OVERRIDE_IIR_MEM2
- void iir_mem2(const spx_sig_t *_x, const spx_coef_t *den, spx_sig_t *_y, int N, int ord, spx_mem_t *mem)
- {
- spx_word16_t y[N+2];
- spx_word16_t *yy;
- yy = y+2;
- __asm__ __volatile__
- (
- /* Register setup */
- "R0 = %5;nt" /*ord */
- "P1 = %3;nt"
- "I1 = P1;nt"
- "B1 = P1;nt"
- "L1 = 0;nt"
- "P3 = %0;nt"
- "I3 = P3;nt"
- "L3 = 0;nt"
- "P4 = %6;nt"
- "P0 = %1;nt"
- "P1 = %2;nt"
- /* First sample */
- "R1 = [P4++];nt"
- "R1 <<= 1;nt"
- "R2 = [P0++];nt"
- "R1 = R1 + R2;nt"
- "[P1++] = R1;nt"
- "R1 <<= 2;nt"
- "W[P3] = R1.H;nt"
- "R2 <<= 2;nt"
- /* Samples 1 to ord-1 (using memory) */
- "R0 += -1;nt"
- "R3 = 0;nt"
- "LC0 = R0;nt"
- "LOOP filter_start%= LC0;nt"
- "LOOP_BEGIN filter_start%=;nt"
- "R3 += 1;nt"
- "LC1 = R3;nt"
- "R1 = [P4++];nt"
- "A1 = R1;nt"
- "I1 = B1;nt"
- "I3 = P3;nt"
- "P3 += 2;nt"
- "LOOP filter_start_inner%= LC1;nt"
- "LOOP_BEGIN filter_start_inner%=;nt"
- "R4.L = W[I1++];nt"
- "R5.L = W[I3--];nt"
- "A1 -= R4.L*R5.L (IS);nt"
- "LOOP_END filter_start_inner%=;nt"
- "R1 = A1;nt"
- "R1 <<= 1;nt"
- "R2 = [P0++];nt"
- "R1 = R1 + R2;nt"
- "[P1++] = R1;nt"
- "R1 <<= 2;nt"
- "W[P3] = R1.H;nt"
- "R2 <<= 2;nt"
- "LOOP_END filter_start%=;nt"
- /* Samples ord to N*/
- "R0 = %5;nt"
- "R0 <<= 1;nt"
- "I1 = B1;nt"
- "L1 = R0;nt"
- "R0 = %5;nt"
- "R2 = %4;nt"
- "R2 = R2 - R0;nt"
- "R4.L = W[I1++];nt"
- "LC0 = R2;nt"
- "LOOP filter_mid%= LC0;nt"
- "LOOP_BEGIN filter_mid%=;nt"
- "LC1 = R0;nt"
- "A1 = 0;nt"
- "I3 = P3;nt"
- "P3 += 2;nt"
- "R5.L = W[I3--];nt"
- "LOOP filter_mid_inner%= LC1;nt"
- "LOOP_BEGIN filter_mid_inner%=;nt"
- "A1 -= R4.L*R5.L (IS) || R4.L = W[I1++] || R5.L = W[I3--];nt"
- "LOOP_END filter_mid_inner%=;nt"
- "R1 = A1;nt"
- "R1 = R1 << 1 || R2 = [P0++];nt"
- "R1 = R1 + R2;nt"
- "R1 = R1 << 2 || [P1++] = R1;nt"
- "W[P3] = R1.H;nt"
- "LOOP_END filter_mid%=;nt"
- /* Update memory */
- "P4 = %6;nt"
- "R0 = %5;nt"
- "LC0 = R0;nt"
- "P1 = B1;nt"
- "LOOP mem_update%= LC0;nt"
- "LOOP_BEGIN mem_update%=;nt"
- "A0 = 0;nt"
- "I3 = P3;nt"
- "I1 = P1;nt"
- "P1 += 2;nt"
- "R0 = LC0;nt"
- "LC1=R0;nt"
- "R5.L = W[I3--] || R4.L = W[I1++];nt"
- "LOOP mem_accum%= LC1;nt"
- "LOOP_BEGIN mem_accum%=;nt"
- "A0 -= R4.L*R5.L (IS) || R4.L = W[I1++] || R5.L = W[I3--];nt"
- "LOOP_END mem_accum%=;nt"
- "R0 = A0;nt"
- "[P4++] = R0;nt"
- "LOOP_END mem_update%=;nt"
- "L1 = 0;nt"
- : : "m" (yy), "m" (_x), "m" (_y), "m" (den), "m" (N), "m" (ord), "m" (mem)
- : "A0", "A1", "R0", "R1", "R2", "R3", "R4", "R5", "P0", "P1", "P2", "P3", "P4", "B1", "I1", "I3", "L1", "L3", "memory"
- );
- }
- #define OVERRIDE_FIR_MEM2
- void fir_mem2(const spx_sig_t *x, const spx_coef_t *num, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
- {
- int i;
- spx_coef_t den2[12];
- spx_coef_t *den;
- den = (spx_coef_t*)((((int)den2)+4)&0xfffffffc);
- for (i=0;i<10;i++)
- den[i] = 0;
- filter_mem2(x, num, den, y, N, ord, mem);
- }
- #define OVERRIDE_COMPUTE_IMPULSE_RESPONSE
- 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)
- {
- int i;
- VARDECL(spx_word16_t *ytmp);
- ALLOC(ytmp, N, spx_word16_t);
- spx_word16_t *ytmp2 = ytmp;
- y[0] = LPC_SCALING;
- for (i=0;i<ord;i++)
- y[i+1] = awk1[i];
- i++;
- for (;i<N;i++)
- y[i] = 0;
- N-=1;
- __asm__ __volatile__
- (
- "I0 = %0;nt"
- "I1 = %1;nt"
- "L0 = 0;nt"
- "L1 = 0;nt"
- "L2 = 0;nt"
- "L3 = 0;nt"
- "R0 = 1;nt"
- "R0 <<= 13;nt"
- "W[I0] = R0.L;nt"
- "R0 <<= 1;nt"
- "W[I1] = R0.L;nt"
- "R0 = %5;nt"
- "LC0 = R0;nt"
- "R2 = 0;nt"
- "LOOP samples%= LC0;nt"
- "LOOP_BEGIN samples%=;nt"
- "R2 += 1;nt"
- "R2 = MIN(R2, %4);nt"
- "I0 = %0;nt"
- "I1 = %1;nt"
- "I2 = %2;nt"
- "I3 = %3;nt"
- "%0 += 2;nt"
- "%1 += 2;nt"
- "A0 = A1 = 0;nt"
- "R0.L = W[I0--] || R1.L = W[I2++];nt"
- "LC1 = R2;nt"
- "LOOP filter%= LC1;nt"
- "LOOP_BEGIN filter%=;nt"
- "A0 -= R0.L*R1.L (IS) || R0.L = W[I1--] || R1.L = W[I3++];nt"
- "A1 -= R0.L*R1.L (IS) || R0.L = W[I0--] || R1.L = W[I2++];nt"
- "LOOP_END filter%=;nt"
- "R0 = A0, R1 = A1;nt"
- "R3 = W[%1] (X);nt"
- "R3 <<= 13;nt"
- "R0 = R0 + R3;nt"
- "R3 = R0 >>> 13;nt"
- "W[%0] = R3.L;nt"
- "R0 <<= 1;nt"
- "R1 = R1 + R0;nt"
- "R1 >>>= 13;nt"
- "W[%1] = R1.L;nt"
- "LOOP_END samples%=;nt"
- : "=a" (ytmp2), "=a" (y)
- : "a" (awk2), "a" (ak), "d" (ord), "m" (N), "0" (ytmp2), "1" (y)
- : "A0", "A1", "R0", "R1", "R2", "R3", "I0", "I1", "I2", "I3", "L0", "L1", "L2", "L3", "A0", "A1"
- );
- }
- #if 0 /* Equivalent C function for filter_mem2 and compute_impulse_response */
- #define min(a,b) ((a)<(b) ? (a):(b))
- 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)
- {
- int i,j;
- VARDECL(spx_word16_t *ytmp);
- ALLOC(ytmp, N, spx_word16_t);
- y[0] = LPC_SCALING;
- for (i=0;i<ord;i++)
- y[i+1] = awk1[i];
- i++;
- for (;i<N;i++)
- y[i] = 0;
- for (i=0;i<N;i++)
- {
- spx_word32_t yi = SHL32(EXTEND32(y[i]),LPC_SHIFT);
- spx_word32_t yi2 = 0;
- for (j=0;j<min(i,ord);j++)
- {
- yi = MAC16_16(yi, awk2[j], -ytmp[i-j-1]);
- yi2 = MAC16_16(yi2, ak[j], -y[i-j-1]);
- }
- ytmp[i] = EXTRACT16(SHR32(yi,LPC_SHIFT));
- yi2 = ADD32(yi2,SHL32(yi,1));
- y[i] = EXTRACT16(SHR32(yi2,LPC_SHIFT));
- }
- }
- 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)
- {
- int i,j;
- spx_word16_t xi,yi,nyi;
- spx_word16_t x[N],y[N];
- spx_word16_t *xx, *yy;
- xx = x;
- yy = y;
- for (i=0;i<N;i++)
- {
- x[i] = EXTRACT16(SHR32(_x[i],SIG_SHIFT));
- }
- for (i=0;i<ord;i++)
- {
- spx_word32_t yi = mem[i];
- for (j=0;j<i;j++)
- {
- yi = MAC16_16(yi, num[j], x[i-j-1]);
- yi = MAC16_16(yi, den[j], -y[i-j-1]);
- }
- _y[i] = ADD32(_x[i],SHL32(yi,1));
- y[i] = EXTRACT16(SHR32(_y[i],SIG_SHIFT));
- }
- for (i=ord;i<N;i++)
- {
- spx_word32_t yi = 0;
- for (j=0;j<ord;j++)
- {
- yi = MAC16_16(yi, num[j], x[i-j-1]);
- yi = MAC16_16(yi, den[j], -y[i-j-1]);
- }
- _y[i] = ADD32(_x[i],SHL32(yi,1));
- y[i] = EXTRACT16(SHR32(_y[i],SIG_SHIFT));
- }
- for (i=0;i<ord;i++)
- {
- spx_mem_t m = 0;
- for (j=0;j<ord-i;j++)
- {
- m = MAC16_16(m, x[N-1-j], num[j+i]);
- m = MAC16_16(m, -y[N-1-j], den[j+i]);
- }
- mem[i] = m;
- }
- }
- #endif