David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SpanDSP - a series of DSP components for telephony |
| 3 | * |
| 4 | * fir.h - General telephony FIR routines |
| 5 | * |
| 6 | * Written by Steve Underwood <steveu@coppice.org> |
| 7 | * |
| 8 | * Copyright (C) 2002 Steve Underwood |
| 9 | * |
| 10 | * All rights reserved. |
| 11 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | * |
| 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. |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 26 | #if !defined(_FIR_H_) |
| 27 | #define _FIR_H_ |
| 28 | |
| 29 | /* |
| 30 | Blackfin NOTES & IDEAS: |
| 31 | |
| 32 | A simple dot product function is used to implement the filter. This performs |
| 33 | just one MAC/cycle which is inefficient but was easy to implement as a first |
| 34 | pass. The current Blackfin code also uses an unrolled form of the filter |
| 35 | history to avoid 0 length hardware loop issues. This is wasteful of |
| 36 | memory. |
| 37 | |
| 38 | Ideas for improvement: |
| 39 | |
| 40 | 1/ Rewrite filter for dual MAC inner loop. The issue here is handling |
| 41 | history sample offsets that are 16 bit aligned - the dual MAC needs |
| 42 | 32 bit aligmnent. There are some good examples in libbfdsp. |
| 43 | |
| 44 | 2/ Use the hardware circular buffer facility tohalve memory usage. |
| 45 | |
| 46 | 3/ Consider using internal memory. |
| 47 | |
| 48 | Using less memory might also improve speed as cache misses will be |
| 49 | reduced. A drop in MIPs and memory approaching 50% should be |
| 50 | possible. |
| 51 | |
| 52 | The foreground and background filters currenlty use a total of |
| 53 | about 10 MIPs/ch as measured with speedtest.c on a 256 TAP echo |
| 54 | can. |
| 55 | */ |
| 56 | |
Greg Kroah-Hartman | 56791f0 | 2009-08-25 22:07:56 -0700 | [diff] [blame] | 57 | /* |
| 58 | * 16 bit integer FIR descriptor. This defines the working state for a single |
| 59 | * instance of an FIR filter using 16 bit integer coefficients. |
| 60 | */ |
J.R. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 61 | struct fir16_state_t { |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 62 | int taps; |
| 63 | int curr_pos; |
| 64 | const int16_t *coeffs; |
| 65 | int16_t *history; |
J.R. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 66 | }; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 67 | |
Greg Kroah-Hartman | 56791f0 | 2009-08-25 22:07:56 -0700 | [diff] [blame] | 68 | /* |
| 69 | * 32 bit integer FIR descriptor. This defines the working state for a single |
| 70 | * instance of an FIR filter using 32 bit integer coefficients, and filtering |
| 71 | * 16 bit integer data. |
| 72 | */ |
J.R. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 73 | struct fir32_state_t { |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 74 | int taps; |
| 75 | int curr_pos; |
| 76 | const int32_t *coeffs; |
| 77 | int16_t *history; |
J.R. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 78 | }; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 79 | |
Greg Kroah-Hartman | 56791f0 | 2009-08-25 22:07:56 -0700 | [diff] [blame] | 80 | /* |
| 81 | * Floating point FIR descriptor. This defines the working state for a single |
| 82 | * instance of an FIR filter using floating point coefficients and data. |
| 83 | */ |
J.R. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 84 | struct fir_float_state_t { |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 85 | int taps; |
| 86 | int curr_pos; |
| 87 | const float *coeffs; |
| 88 | float *history; |
J.R. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 89 | }; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 90 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 91 | static inline const int16_t *fir16_create(struct fir16_state_t *fir, |
| 92 | const int16_t *coeffs, int taps) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 93 | { |
| 94 | fir->taps = taps; |
| 95 | fir->curr_pos = taps - 1; |
| 96 | fir->coeffs = coeffs; |
Greg Kroah-Hartman | c8b3953 | 2009-08-25 22:07:13 -0700 | [diff] [blame] | 97 | #if defined(__bfin__) |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 98 | fir->history = kcalloc(2 * taps, sizeof(int16_t), GFP_KERNEL); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 99 | #else |
Pekka Enberg | db2af14 | 2008-10-17 20:55:03 +0300 | [diff] [blame] | 100 | fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 101 | #endif |
| 102 | return fir->history; |
| 103 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 104 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 105 | static inline void fir16_flush(struct fir16_state_t *fir) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 106 | { |
Greg Kroah-Hartman | c8b3953 | 2009-08-25 22:07:13 -0700 | [diff] [blame] | 107 | #if defined(__bfin__) |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 108 | memset(fir->history, 0, 2 * fir->taps * sizeof(int16_t)); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 109 | #else |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 110 | memset(fir->history, 0, fir->taps * sizeof(int16_t)); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 111 | #endif |
| 112 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 113 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 114 | static inline void fir16_free(struct fir16_state_t *fir) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 115 | { |
Pekka Enberg | db2af14 | 2008-10-17 20:55:03 +0300 | [diff] [blame] | 116 | kfree(fir->history); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 117 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 118 | |
Tzafrir Cohen | f55ccbf | 2008-10-12 08:13:21 +0200 | [diff] [blame] | 119 | #ifdef __bfin__ |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 120 | static inline int32_t dot_asm(short *x, short *y, int len) |
| 121 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 122 | int dot; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 123 | |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 124 | len--; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 125 | |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 126 | __asm__("I0 = %1;\n\t" |
| 127 | "I1 = %2;\n\t" |
| 128 | "A0 = 0;\n\t" |
| 129 | "R0.L = W[I0++] || R1.L = W[I1++];\n\t" |
| 130 | "LOOP dot%= LC0 = %3;\n\t" |
| 131 | "LOOP_BEGIN dot%=;\n\t" |
| 132 | "A0 += R0.L * R1.L (IS) || R0.L = W[I0++] || R1.L = W[I1++];\n\t" |
| 133 | "LOOP_END dot%=;\n\t" |
| 134 | "A0 += R0.L*R1.L (IS);\n\t" |
| 135 | "R0 = A0;\n\t" |
| 136 | "%0 = R0;\n\t" |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 137 | : "=&d"(dot) |
| 138 | : "a"(x), "a"(y), "a"(len) |
| 139 | : "I0", "I1", "A1", "A0", "R0", "R1" |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 140 | ); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 141 | |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 142 | return dot; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 143 | } |
| 144 | #endif |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 145 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 146 | static inline int16_t fir16(struct fir16_state_t *fir, int16_t sample) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 147 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 148 | int32_t y; |
Greg Kroah-Hartman | c8b3953 | 2009-08-25 22:07:13 -0700 | [diff] [blame] | 149 | #if defined(__bfin__) |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 150 | fir->history[fir->curr_pos] = sample; |
| 151 | fir->history[fir->curr_pos + fir->taps] = sample; |
| 152 | y = dot_asm((int16_t *) fir->coeffs, &fir->history[fir->curr_pos], |
| 153 | fir->taps); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 154 | #else |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 155 | int i; |
| 156 | int offset1; |
| 157 | int offset2; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 158 | |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 159 | fir->history[fir->curr_pos] = sample; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 160 | |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 161 | offset2 = fir->curr_pos; |
| 162 | offset1 = fir->taps - offset2; |
| 163 | y = 0; |
| 164 | for (i = fir->taps - 1; i >= offset1; i--) |
| 165 | y += fir->coeffs[i] * fir->history[i - offset1]; |
| 166 | for (; i >= 0; i--) |
| 167 | y += fir->coeffs[i] * fir->history[i + offset2]; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 168 | #endif |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 169 | if (fir->curr_pos <= 0) |
| 170 | fir->curr_pos = fir->taps; |
| 171 | fir->curr_pos--; |
| 172 | return (int16_t) (y >> 15); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 173 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 174 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 175 | static inline const int16_t *fir32_create(struct fir32_state_t *fir, |
| 176 | const int32_t *coeffs, int taps) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 177 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 178 | fir->taps = taps; |
| 179 | fir->curr_pos = taps - 1; |
| 180 | fir->coeffs = coeffs; |
| 181 | fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL); |
| 182 | return fir->history; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 183 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 184 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 185 | static inline void fir32_flush(struct fir32_state_t *fir) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 186 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 187 | memset(fir->history, 0, fir->taps * sizeof(int16_t)); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 188 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 189 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 190 | static inline void fir32_free(struct fir32_state_t *fir) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 191 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 192 | kfree(fir->history); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 193 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 194 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 195 | static inline int16_t fir32(struct fir32_state_t *fir, int16_t sample) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 196 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 197 | int i; |
| 198 | int32_t y; |
| 199 | int offset1; |
| 200 | int offset2; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 201 | |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 202 | fir->history[fir->curr_pos] = sample; |
| 203 | offset2 = fir->curr_pos; |
| 204 | offset1 = fir->taps - offset2; |
| 205 | y = 0; |
| 206 | for (i = fir->taps - 1; i >= offset1; i--) |
| 207 | y += fir->coeffs[i] * fir->history[i - offset1]; |
| 208 | for (; i >= 0; i--) |
| 209 | y += fir->coeffs[i] * fir->history[i + offset2]; |
| 210 | if (fir->curr_pos <= 0) |
| 211 | fir->curr_pos = fir->taps; |
| 212 | fir->curr_pos--; |
| 213 | return (int16_t) (y >> 15); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 214 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 215 | |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 216 | #endif |