2 * SpanDSP - a series of DSP components for telephony
4 * fir.h - General telephony FIR routines
6 * Written by Steve Underwood <steveu@coppice.org>
8 * Copyright (C) 2002 Steve Underwood
10 * All rights reserved.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * $Id: fir.h,v 1.8 2006/10/24 13:45:28 steveu Exp $
28 /*! \page fir_page FIR filtering
29 \section fir_page_sec_1 What does it do?
32 \section fir_page_sec_2 How does it work?
40 Blackfin NOTES & IDEAS:
42 A simple dot product function is used to implement the filter. This performs
43 just one MAC/cycle which is inefficient but was easy to implement as a first
44 pass. The current Blackfin code also uses an unrolled form of the filter
45 history to avoid 0 length hardware loop issues. This is wasteful of
48 Ideas for improvement:
50 1/ Rewrite filter for dual MAC inner loop. The issue here is handling
51 history sample offsets that are 16 bit aligned - the dual MAC needs
52 32 bit aligmnent. There are some good examples in libbfdsp.
54 2/ Use the hardware circular buffer facility tohalve memory usage.
56 3/ Consider using internal memory.
58 Using less memory might also improve speed as cache misses will be
59 reduced. A drop in MIPs and memory approaching 50% should be
62 The foreground and background filters currenlty use a total of
63 about 10 MIPs/ch as measured with speedtest.c on a 256 TAP echo
67 #if defined(USE_MMX) || defined(USE_SSE2)
72 16 bit integer FIR descriptor. This defines the working state for a single
73 instance of an FIR filter using 16 bit integer coefficients.
78 const int16_t *coeffs;
83 32 bit integer FIR descriptor. This defines the working state for a single
84 instance of an FIR filter using 32 bit integer coefficients, and filtering
90 const int32_t *coeffs;
95 Floating point FIR descriptor. This defines the working state for a single
96 instance of an FIR filter using floating point coefficients and data.
105 static __inline__ const int16_t *fir16_create(fir16_state_t * fir,
106 const int16_t * coeffs, int taps)
109 fir->curr_pos = taps - 1;
110 fir->coeffs = coeffs;
111 #if defined(USE_MMX) || defined(USE_SSE2) || defined(__bfin__)
112 fir->history = kcalloc(2 * taps, sizeof(int16_t), GFP_KERNEL);
114 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
119 static __inline__ void fir16_flush(fir16_state_t * fir)
121 #if defined(USE_MMX) || defined(USE_SSE2) || defined(__bfin__)
122 memset(fir->history, 0, 2 * fir->taps * sizeof(int16_t));
124 memset(fir->history, 0, fir->taps * sizeof(int16_t));
128 static __inline__ void fir16_free(fir16_state_t * fir)
134 static inline int32_t dot_asm(short *x, short *y, int len)
140 __asm__("I0 = %1;\n\t"
143 "R0.L = W[I0++] || R1.L = W[I1++];\n\t"
144 "LOOP dot%= LC0 = %3;\n\t"
145 "LOOP_BEGIN dot%=;\n\t"
146 "A0 += R0.L * R1.L (IS) || R0.L = W[I0++] || R1.L = W[I1++];\n\t"
147 "LOOP_END dot%=;\n\t"
148 "A0 += R0.L*R1.L (IS);\n\t"
152 :"a"(x), "a"(y), "a"(len)
153 :"I0", "I1", "A1", "A0", "R0", "R1"
160 static __inline__ int16_t fir16(fir16_state_t * fir, int16_t sample)
168 fir->history[fir->curr_pos] = sample;
169 fir->history[fir->curr_pos + fir->taps] = sample;
171 mmx_coeffs = (mmx_t *) fir->coeffs;
172 mmx_hist = (mmx_t *) & fir->history[fir->curr_pos];
175 /* 8 samples per iteration, so the filter must be a multiple of 8 long. */
177 movq_m2r(mmx_coeffs[0], mm0);
178 movq_m2r(mmx_coeffs[1], mm2);
179 movq_m2r(mmx_hist[0], mm1);
180 movq_m2r(mmx_hist[1], mm3);
183 pmaddwd_r2r(mm1, mm0);
184 pmaddwd_r2r(mm3, mm2);
194 #elif defined(USE_SSE2)
199 fir->history[fir->curr_pos] = sample;
200 fir->history[fir->curr_pos + fir->taps] = sample;
202 xmm_coeffs = (xmm_t *) fir->coeffs;
203 xmm_hist = (xmm_t *) & fir->history[fir->curr_pos];
205 pxor_r2r(xmm4, xmm4);
206 /* 16 samples per iteration, so the filter must be a multiple of 16 long. */
208 movdqu_m2r(xmm_coeffs[0], xmm0);
209 movdqu_m2r(xmm_coeffs[1], xmm2);
210 movdqu_m2r(xmm_hist[0], xmm1);
211 movdqu_m2r(xmm_hist[1], xmm3);
214 pmaddwd_r2r(xmm1, xmm0);
215 pmaddwd_r2r(xmm3, xmm2);
216 paddd_r2r(xmm0, xmm4);
217 paddd_r2r(xmm2, xmm4);
220 movdqa_r2r(xmm4, xmm0);
222 paddd_r2r(xmm0, xmm4);
223 movdqa_r2r(xmm4, xmm0);
225 paddd_r2r(xmm0, xmm4);
227 #elif defined(__bfin__)
228 fir->history[fir->curr_pos] = sample;
229 fir->history[fir->curr_pos + fir->taps] = sample;
230 y = dot_asm((int16_t *) fir->coeffs, &fir->history[fir->curr_pos],
237 fir->history[fir->curr_pos] = sample;
239 offset2 = fir->curr_pos;
240 offset1 = fir->taps - offset2;
242 for (i = fir->taps - 1; i >= offset1; i--)
243 y += fir->coeffs[i] * fir->history[i - offset1];
245 y += fir->coeffs[i] * fir->history[i + offset2];
247 if (fir->curr_pos <= 0)
248 fir->curr_pos = fir->taps;
250 return (int16_t) (y >> 15);
253 static __inline__ const int16_t *fir32_create(fir32_state_t * fir,
254 const int32_t * coeffs, int taps)
257 fir->curr_pos = taps - 1;
258 fir->coeffs = coeffs;
259 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
263 static __inline__ void fir32_flush(fir32_state_t * fir)
265 memset(fir->history, 0, fir->taps * sizeof(int16_t));
268 static __inline__ void fir32_free(fir32_state_t * fir)
273 static __inline__ int16_t fir32(fir32_state_t * fir, int16_t sample)
280 fir->history[fir->curr_pos] = sample;
281 offset2 = fir->curr_pos;
282 offset1 = fir->taps - offset2;
284 for (i = fir->taps - 1; i >= offset1; i--)
285 y += fir->coeffs[i] * fir->history[i - offset1];
287 y += fir->coeffs[i] * fir->history[i + offset2];
288 if (fir->curr_pos <= 0)
289 fir->curr_pos = fir->taps;
291 return (int16_t) (y >> 15);
295 /*- End of file ------------------------------------------------------------*/