Staging: echo: remove dead code
[firewire-audio.git] / drivers / staging / echo / fir.h
blob19c20cde186be9f20dbb7dbe9d0a9820eedb6478
1 /*
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?
30 ???.
32 \section fir_page_sec_2 How does it work?
33 ???.
36 #if !defined(_FIR_H_)
37 #define _FIR_H_
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
46 memory.
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
60 possible.
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
64 can.
67 #if defined(USE_MMX) || defined(USE_SSE2)
68 #include "mmx.h"
69 #endif
71 /*!
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.
75 typedef struct
77 int taps;
78 int curr_pos;
79 const int16_t *coeffs;
80 int16_t *history;
81 } fir16_state_t;
83 /*!
84 32 bit integer FIR descriptor. This defines the working state for a single
85 instance of an FIR filter using 32 bit integer coefficients, and filtering
86 16 bit integer data.
88 typedef struct
90 int taps;
91 int curr_pos;
92 const int32_t *coeffs;
93 int16_t *history;
94 } fir32_state_t;
96 /*!
97 Floating point FIR descriptor. This defines the working state for a single
98 instance of an FIR filter using floating point coefficients and data.
100 typedef struct
102 int taps;
103 int curr_pos;
104 const float *coeffs;
105 float *history;
106 } fir_float_state_t;
108 #ifdef __cplusplus
109 extern "C" {
110 #endif
112 static __inline__ const int16_t *fir16_create(fir16_state_t *fir,
113 const int16_t *coeffs,
114 int taps)
116 fir->taps = taps;
117 fir->curr_pos = taps - 1;
118 fir->coeffs = coeffs;
119 #if defined(USE_MMX) || defined(USE_SSE2) || defined(__bfin__)
120 fir->history = kcalloc(2*taps, sizeof(int16_t), GFP_KERNEL);
121 #else
122 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
123 #endif
124 return fir->history;
126 /*- End of function --------------------------------------------------------*/
128 static __inline__ void fir16_flush(fir16_state_t *fir)
130 #if defined(USE_MMX) || defined(USE_SSE2) || defined(__bfin__)
131 memset(fir->history, 0, 2*fir->taps*sizeof(int16_t));
132 #else
133 memset(fir->history, 0, fir->taps*sizeof(int16_t));
134 #endif
136 /*- End of function --------------------------------------------------------*/
138 static __inline__ void fir16_free(fir16_state_t *fir)
140 kfree(fir->history);
142 /*- End of function --------------------------------------------------------*/
144 #ifdef __bfin__
145 static inline int32_t dot_asm(short *x, short *y, int len)
147 int dot;
149 len--;
151 __asm__
153 "I0 = %1;\n\t"
154 "I1 = %2;\n\t"
155 "A0 = 0;\n\t"
156 "R0.L = W[I0++] || R1.L = W[I1++];\n\t"
157 "LOOP dot%= LC0 = %3;\n\t"
158 "LOOP_BEGIN dot%=;\n\t"
159 "A0 += R0.L * R1.L (IS) || R0.L = W[I0++] || R1.L = W[I1++];\n\t"
160 "LOOP_END dot%=;\n\t"
161 "A0 += R0.L*R1.L (IS);\n\t"
162 "R0 = A0;\n\t"
163 "%0 = R0;\n\t"
164 : "=&d" (dot)
165 : "a" (x), "a" (y), "a" (len)
166 : "I0", "I1", "A1", "A0", "R0", "R1"
169 return dot;
171 #endif
172 /*- End of function --------------------------------------------------------*/
174 static __inline__ int16_t fir16(fir16_state_t *fir, int16_t sample)
176 int32_t y;
177 #if defined(USE_MMX)
178 int i;
179 mmx_t *mmx_coeffs;
180 mmx_t *mmx_hist;
182 fir->history[fir->curr_pos] = sample;
183 fir->history[fir->curr_pos + fir->taps] = sample;
185 mmx_coeffs = (mmx_t *) fir->coeffs;
186 mmx_hist = (mmx_t *) &fir->history[fir->curr_pos];
187 i = fir->taps;
188 pxor_r2r(mm4, mm4);
189 /* 8 samples per iteration, so the filter must be a multiple of 8 long. */
190 while (i > 0)
192 movq_m2r(mmx_coeffs[0], mm0);
193 movq_m2r(mmx_coeffs[1], mm2);
194 movq_m2r(mmx_hist[0], mm1);
195 movq_m2r(mmx_hist[1], mm3);
196 mmx_coeffs += 2;
197 mmx_hist += 2;
198 pmaddwd_r2r(mm1, mm0);
199 pmaddwd_r2r(mm3, mm2);
200 paddd_r2r(mm0, mm4);
201 paddd_r2r(mm2, mm4);
202 i -= 8;
204 movq_r2r(mm4, mm0);
205 psrlq_i2r(32, mm0);
206 paddd_r2r(mm0, mm4);
207 movd_r2m(mm4, y);
208 emms();
209 #elif defined(USE_SSE2)
210 int i;
211 xmm_t *xmm_coeffs;
212 xmm_t *xmm_hist;
214 fir->history[fir->curr_pos] = sample;
215 fir->history[fir->curr_pos + fir->taps] = sample;
217 xmm_coeffs = (xmm_t *) fir->coeffs;
218 xmm_hist = (xmm_t *) &fir->history[fir->curr_pos];
219 i = fir->taps;
220 pxor_r2r(xmm4, xmm4);
221 /* 16 samples per iteration, so the filter must be a multiple of 16 long. */
222 while (i > 0)
224 movdqu_m2r(xmm_coeffs[0], xmm0);
225 movdqu_m2r(xmm_coeffs[1], xmm2);
226 movdqu_m2r(xmm_hist[0], xmm1);
227 movdqu_m2r(xmm_hist[1], xmm3);
228 xmm_coeffs += 2;
229 xmm_hist += 2;
230 pmaddwd_r2r(xmm1, xmm0);
231 pmaddwd_r2r(xmm3, xmm2);
232 paddd_r2r(xmm0, xmm4);
233 paddd_r2r(xmm2, xmm4);
234 i -= 16;
236 movdqa_r2r(xmm4, xmm0);
237 psrldq_i2r(8, xmm0);
238 paddd_r2r(xmm0, xmm4);
239 movdqa_r2r(xmm4, xmm0);
240 psrldq_i2r(4, xmm0);
241 paddd_r2r(xmm0, xmm4);
242 movd_r2m(xmm4, y);
243 #elif defined(__bfin__)
244 fir->history[fir->curr_pos] = sample;
245 fir->history[fir->curr_pos + fir->taps] = sample;
246 y = dot_asm((int16_t*)fir->coeffs, &fir->history[fir->curr_pos], fir->taps);
247 #else
248 int i;
249 int offset1;
250 int offset2;
252 fir->history[fir->curr_pos] = sample;
254 offset2 = fir->curr_pos;
255 offset1 = fir->taps - offset2;
256 y = 0;
257 for (i = fir->taps - 1; i >= offset1; i--)
258 y += fir->coeffs[i]*fir->history[i - offset1];
259 for ( ; i >= 0; i--)
260 y += fir->coeffs[i]*fir->history[i + offset2];
261 #endif
262 if (fir->curr_pos <= 0)
263 fir->curr_pos = fir->taps;
264 fir->curr_pos--;
265 return (int16_t) (y >> 15);
267 /*- End of function --------------------------------------------------------*/
269 static __inline__ const int16_t *fir32_create(fir32_state_t *fir,
270 const int32_t *coeffs,
271 int taps)
273 fir->taps = taps;
274 fir->curr_pos = taps - 1;
275 fir->coeffs = coeffs;
276 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
277 return fir->history;
279 /*- End of function --------------------------------------------------------*/
281 static __inline__ void fir32_flush(fir32_state_t *fir)
283 memset(fir->history, 0, fir->taps*sizeof(int16_t));
285 /*- End of function --------------------------------------------------------*/
287 static __inline__ void fir32_free(fir32_state_t *fir)
289 kfree(fir->history);
291 /*- End of function --------------------------------------------------------*/
293 static __inline__ int16_t fir32(fir32_state_t *fir, int16_t sample)
295 int i;
296 int32_t y;
297 int offset1;
298 int offset2;
300 fir->history[fir->curr_pos] = sample;
301 offset2 = fir->curr_pos;
302 offset1 = fir->taps - offset2;
303 y = 0;
304 for (i = fir->taps - 1; i >= offset1; i--)
305 y += fir->coeffs[i]*fir->history[i - offset1];
306 for ( ; i >= 0; i--)
307 y += fir->coeffs[i]*fir->history[i + offset2];
308 if (fir->curr_pos <= 0)
309 fir->curr_pos = fir->taps;
310 fir->curr_pos--;
311 return (int16_t) (y >> 15);
313 /*- End of function --------------------------------------------------------*/
315 #ifdef __cplusplus
317 #endif
319 #endif
320 /*- End of file ------------------------------------------------------------*/