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.
26 /*! \page fir_page FIR filtering
27 \section fir_page_sec_1 What does it do?
30 \section fir_page_sec_2 How does it work?
38 Blackfin NOTES & IDEAS:
40 A simple dot product function is used to implement the filter. This performs
41 just one MAC/cycle which is inefficient but was easy to implement as a first
42 pass. The current Blackfin code also uses an unrolled form of the filter
43 history to avoid 0 length hardware loop issues. This is wasteful of
46 Ideas for improvement:
48 1/ Rewrite filter for dual MAC inner loop. The issue here is handling
49 history sample offsets that are 16 bit aligned - the dual MAC needs
50 32 bit aligmnent. There are some good examples in libbfdsp.
52 2/ Use the hardware circular buffer facility tohalve memory usage.
54 3/ Consider using internal memory.
56 Using less memory might also improve speed as cache misses will be
57 reduced. A drop in MIPs and memory approaching 50% should be
60 The foreground and background filters currenlty use a total of
61 about 10 MIPs/ch as measured with speedtest.c on a 256 TAP echo
65 #if defined(USE_MMX) || defined(USE_SSE2)
70 16 bit integer FIR descriptor. This defines the working state for a single
71 instance of an FIR filter using 16 bit integer coefficients.
73 struct fir16_state_t {
76 const int16_t *coeffs;
81 32 bit integer FIR descriptor. This defines the working state for a single
82 instance of an FIR filter using 32 bit integer coefficients, and filtering
85 struct fir32_state_t {
88 const int32_t *coeffs;
93 Floating point FIR descriptor. This defines the working state for a single
94 instance of an FIR filter using floating point coefficients and data.
96 struct fir_float_state_t {
103 static inline const int16_t *fir16_create(struct fir16_state_t *fir,
104 const int16_t *coeffs, int taps)
107 fir->curr_pos = taps - 1;
108 fir->coeffs = coeffs;
109 #if defined(USE_MMX) || defined(USE_SSE2) || defined(__bfin__)
110 fir->history = kcalloc(2 * taps, sizeof(int16_t), GFP_KERNEL);
112 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
117 static inline void fir16_flush(struct fir16_state_t *fir)
119 #if defined(USE_MMX) || defined(USE_SSE2) || defined(__bfin__)
120 memset(fir->history, 0, 2 * fir->taps * sizeof(int16_t));
122 memset(fir->history, 0, fir->taps * sizeof(int16_t));
126 static inline void fir16_free(struct fir16_state_t *fir)
132 static inline int32_t dot_asm(short *x, short *y, int len)
138 __asm__("I0 = %1;\n\t"
141 "R0.L = W[I0++] || R1.L = W[I1++];\n\t"
142 "LOOP dot%= LC0 = %3;\n\t"
143 "LOOP_BEGIN dot%=;\n\t"
144 "A0 += R0.L * R1.L (IS) || R0.L = W[I0++] || R1.L = W[I1++];\n\t"
145 "LOOP_END dot%=;\n\t"
146 "A0 += R0.L*R1.L (IS);\n\t"
150 : "a"(x), "a"(y), "a"(len)
151 : "I0", "I1", "A1", "A0", "R0", "R1"
158 static inline int16_t fir16(struct fir16_state_t *fir, int16_t sample)
163 union mmx_t *mmx_coeffs;
164 union mmx_t *mmx_hist;
166 fir->history[fir->curr_pos] = sample;
167 fir->history[fir->curr_pos + fir->taps] = sample;
169 mmx_coeffs = (union mmx_t *)fir->coeffs;
170 mmx_hist = (union mmx_t *)&fir->history[fir->curr_pos];
173 /* 8 samples per iteration, so the filter must be a multiple of 8 long. */
175 movq_m2r(mmx_coeffs[0], mm0);
176 movq_m2r(mmx_coeffs[1], mm2);
177 movq_m2r(mmx_hist[0], mm1);
178 movq_m2r(mmx_hist[1], mm3);
181 pmaddwd_r2r(mm1, mm0);
182 pmaddwd_r2r(mm3, mm2);
192 #elif defined(USE_SSE2)
194 union xmm_t *xmm_coeffs;
195 union xmm_t *xmm_hist;
197 fir->history[fir->curr_pos] = sample;
198 fir->history[fir->curr_pos + fir->taps] = sample;
200 xmm_coeffs = (union xmm_t *)fir->coeffs;
201 xmm_hist = (union xmm_t *)&fir->history[fir->curr_pos];
203 pxor_r2r(xmm4, xmm4);
204 /* 16 samples per iteration, so the filter must be a multiple of 16 long. */
206 movdqu_m2r(xmm_coeffs[0], xmm0);
207 movdqu_m2r(xmm_coeffs[1], xmm2);
208 movdqu_m2r(xmm_hist[0], xmm1);
209 movdqu_m2r(xmm_hist[1], xmm3);
212 pmaddwd_r2r(xmm1, xmm0);
213 pmaddwd_r2r(xmm3, xmm2);
214 paddd_r2r(xmm0, xmm4);
215 paddd_r2r(xmm2, xmm4);
218 movdqa_r2r(xmm4, xmm0);
220 paddd_r2r(xmm0, xmm4);
221 movdqa_r2r(xmm4, xmm0);
223 paddd_r2r(xmm0, xmm4);
225 #elif defined(__bfin__)
226 fir->history[fir->curr_pos] = sample;
227 fir->history[fir->curr_pos + fir->taps] = sample;
228 y = dot_asm((int16_t *) fir->coeffs, &fir->history[fir->curr_pos],
235 fir->history[fir->curr_pos] = sample;
237 offset2 = fir->curr_pos;
238 offset1 = fir->taps - offset2;
240 for (i = fir->taps - 1; i >= offset1; i--)
241 y += fir->coeffs[i] * fir->history[i - offset1];
243 y += fir->coeffs[i] * fir->history[i + offset2];
245 if (fir->curr_pos <= 0)
246 fir->curr_pos = fir->taps;
248 return (int16_t) (y >> 15);
251 static inline const int16_t *fir32_create(struct fir32_state_t *fir,
252 const int32_t *coeffs, int taps)
255 fir->curr_pos = taps - 1;
256 fir->coeffs = coeffs;
257 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
261 static inline void fir32_flush(struct fir32_state_t *fir)
263 memset(fir->history, 0, fir->taps * sizeof(int16_t));
266 static inline void fir32_free(struct fir32_state_t *fir)
271 static inline int16_t fir32(struct fir32_state_t *fir, int16_t sample)
278 fir->history[fir->curr_pos] = sample;
279 offset2 = fir->curr_pos;
280 offset1 = fir->taps - offset2;
282 for (i = fir->taps - 1; i >= offset1; i--)
283 y += fir->coeffs[i] * fir->history[i - offset1];
285 y += fir->coeffs[i] * fir->history[i + offset2];
286 if (fir->curr_pos <= 0)
287 fir->curr_pos = fir->taps;
289 return (int16_t) (y >> 15);
293 /*- End of file ------------------------------------------------------------*/