Staging: echo: remove annoying "end of function" markers
[firewire-audio.git] / drivers / staging / echo / fir.h
blobe760471e48183bbf724824fd26c5cc11075d7299
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 static __inline__ const int16_t *fir16_create(fir16_state_t *fir,
109 const int16_t *coeffs,
110 int taps)
112 fir->taps = taps;
113 fir->curr_pos = taps - 1;
114 fir->coeffs = coeffs;
115 #if defined(USE_MMX) || defined(USE_SSE2) || defined(__bfin__)
116 fir->history = kcalloc(2*taps, sizeof(int16_t), GFP_KERNEL);
117 #else
118 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
119 #endif
120 return fir->history;
123 static __inline__ void fir16_flush(fir16_state_t *fir)
125 #if defined(USE_MMX) || defined(USE_SSE2) || defined(__bfin__)
126 memset(fir->history, 0, 2*fir->taps*sizeof(int16_t));
127 #else
128 memset(fir->history, 0, fir->taps*sizeof(int16_t));
129 #endif
132 static __inline__ void fir16_free(fir16_state_t *fir)
134 kfree(fir->history);
137 #ifdef __bfin__
138 static inline int32_t dot_asm(short *x, short *y, int len)
140 int dot;
142 len--;
144 __asm__
146 "I0 = %1;\n\t"
147 "I1 = %2;\n\t"
148 "A0 = 0;\n\t"
149 "R0.L = W[I0++] || R1.L = W[I1++];\n\t"
150 "LOOP dot%= LC0 = %3;\n\t"
151 "LOOP_BEGIN dot%=;\n\t"
152 "A0 += R0.L * R1.L (IS) || R0.L = W[I0++] || R1.L = W[I1++];\n\t"
153 "LOOP_END dot%=;\n\t"
154 "A0 += R0.L*R1.L (IS);\n\t"
155 "R0 = A0;\n\t"
156 "%0 = R0;\n\t"
157 : "=&d" (dot)
158 : "a" (x), "a" (y), "a" (len)
159 : "I0", "I1", "A1", "A0", "R0", "R1"
162 return dot;
164 #endif
166 static __inline__ int16_t fir16(fir16_state_t *fir, int16_t sample)
168 int32_t y;
169 #if defined(USE_MMX)
170 int i;
171 mmx_t *mmx_coeffs;
172 mmx_t *mmx_hist;
174 fir->history[fir->curr_pos] = sample;
175 fir->history[fir->curr_pos + fir->taps] = sample;
177 mmx_coeffs = (mmx_t *) fir->coeffs;
178 mmx_hist = (mmx_t *) &fir->history[fir->curr_pos];
179 i = fir->taps;
180 pxor_r2r(mm4, mm4);
181 /* 8 samples per iteration, so the filter must be a multiple of 8 long. */
182 while (i > 0)
184 movq_m2r(mmx_coeffs[0], mm0);
185 movq_m2r(mmx_coeffs[1], mm2);
186 movq_m2r(mmx_hist[0], mm1);
187 movq_m2r(mmx_hist[1], mm3);
188 mmx_coeffs += 2;
189 mmx_hist += 2;
190 pmaddwd_r2r(mm1, mm0);
191 pmaddwd_r2r(mm3, mm2);
192 paddd_r2r(mm0, mm4);
193 paddd_r2r(mm2, mm4);
194 i -= 8;
196 movq_r2r(mm4, mm0);
197 psrlq_i2r(32, mm0);
198 paddd_r2r(mm0, mm4);
199 movd_r2m(mm4, y);
200 emms();
201 #elif defined(USE_SSE2)
202 int i;
203 xmm_t *xmm_coeffs;
204 xmm_t *xmm_hist;
206 fir->history[fir->curr_pos] = sample;
207 fir->history[fir->curr_pos + fir->taps] = sample;
209 xmm_coeffs = (xmm_t *) fir->coeffs;
210 xmm_hist = (xmm_t *) &fir->history[fir->curr_pos];
211 i = fir->taps;
212 pxor_r2r(xmm4, xmm4);
213 /* 16 samples per iteration, so the filter must be a multiple of 16 long. */
214 while (i > 0)
216 movdqu_m2r(xmm_coeffs[0], xmm0);
217 movdqu_m2r(xmm_coeffs[1], xmm2);
218 movdqu_m2r(xmm_hist[0], xmm1);
219 movdqu_m2r(xmm_hist[1], xmm3);
220 xmm_coeffs += 2;
221 xmm_hist += 2;
222 pmaddwd_r2r(xmm1, xmm0);
223 pmaddwd_r2r(xmm3, xmm2);
224 paddd_r2r(xmm0, xmm4);
225 paddd_r2r(xmm2, xmm4);
226 i -= 16;
228 movdqa_r2r(xmm4, xmm0);
229 psrldq_i2r(8, xmm0);
230 paddd_r2r(xmm0, xmm4);
231 movdqa_r2r(xmm4, xmm0);
232 psrldq_i2r(4, xmm0);
233 paddd_r2r(xmm0, xmm4);
234 movd_r2m(xmm4, y);
235 #elif defined(__bfin__)
236 fir->history[fir->curr_pos] = sample;
237 fir->history[fir->curr_pos + fir->taps] = sample;
238 y = dot_asm((int16_t*)fir->coeffs, &fir->history[fir->curr_pos], fir->taps);
239 #else
240 int i;
241 int offset1;
242 int offset2;
244 fir->history[fir->curr_pos] = sample;
246 offset2 = fir->curr_pos;
247 offset1 = fir->taps - offset2;
248 y = 0;
249 for (i = fir->taps - 1; i >= offset1; i--)
250 y += fir->coeffs[i]*fir->history[i - offset1];
251 for ( ; i >= 0; i--)
252 y += fir->coeffs[i]*fir->history[i + offset2];
253 #endif
254 if (fir->curr_pos <= 0)
255 fir->curr_pos = fir->taps;
256 fir->curr_pos--;
257 return (int16_t) (y >> 15);
260 static __inline__ const int16_t *fir32_create(fir32_state_t *fir,
261 const int32_t *coeffs,
262 int taps)
264 fir->taps = taps;
265 fir->curr_pos = taps - 1;
266 fir->coeffs = coeffs;
267 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
268 return fir->history;
271 static __inline__ void fir32_flush(fir32_state_t *fir)
273 memset(fir->history, 0, fir->taps*sizeof(int16_t));
276 static __inline__ void fir32_free(fir32_state_t *fir)
278 kfree(fir->history);
281 static __inline__ int16_t fir32(fir32_state_t *fir, int16_t sample)
283 int i;
284 int32_t y;
285 int offset1;
286 int offset2;
288 fir->history[fir->curr_pos] = sample;
289 offset2 = fir->curr_pos;
290 offset1 = fir->taps - offset2;
291 y = 0;
292 for (i = fir->taps - 1; i >= offset1; i--)
293 y += fir->coeffs[i]*fir->history[i - offset1];
294 for ( ; i >= 0; i--)
295 y += fir->coeffs[i]*fir->history[i + offset2];
296 if (fir->curr_pos <= 0)
297 fir->curr_pos = fir->taps;
298 fir->curr_pos--;
299 return (int16_t) (y >> 15);
302 #endif
303 /*- End of file ------------------------------------------------------------*/