treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / misc / echo / fir.h
blob4d0821025223dec8c8cb2fa8452cfe92f4b01e9b
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * SpanDSP - a series of DSP components for telephony
5 * fir.h - General telephony FIR routines
7 * Written by Steve Underwood <steveu@coppice.org>
9 * Copyright (C) 2002 Steve Underwood
11 * All rights reserved.
14 #if !defined(_FIR_H_)
15 #define _FIR_H_
18 Ideas for improvement:
20 1/ Rewrite filter for dual MAC inner loop. The issue here is handling
21 history sample offsets that are 16 bit aligned - the dual MAC needs
22 32 bit aligmnent. There are some good examples in libbfdsp.
24 2/ Use the hardware circular buffer facility tohalve memory usage.
26 3/ Consider using internal memory.
28 Using less memory might also improve speed as cache misses will be
29 reduced. A drop in MIPs and memory approaching 50% should be
30 possible.
32 The foreground and background filters currenlty use a total of
33 about 10 MIPs/ch as measured with speedtest.c on a 256 TAP echo
34 can.
38 * 16 bit integer FIR descriptor. This defines the working state for a single
39 * instance of an FIR filter using 16 bit integer coefficients.
41 struct fir16_state_t {
42 int taps;
43 int curr_pos;
44 const int16_t *coeffs;
45 int16_t *history;
49 * 32 bit integer FIR descriptor. This defines the working state for a single
50 * instance of an FIR filter using 32 bit integer coefficients, and filtering
51 * 16 bit integer data.
53 struct fir32_state_t {
54 int taps;
55 int curr_pos;
56 const int32_t *coeffs;
57 int16_t *history;
61 * Floating point FIR descriptor. This defines the working state for a single
62 * instance of an FIR filter using floating point coefficients and data.
64 struct fir_float_state_t {
65 int taps;
66 int curr_pos;
67 const float *coeffs;
68 float *history;
71 static inline const int16_t *fir16_create(struct fir16_state_t *fir,
72 const int16_t *coeffs, int taps)
74 fir->taps = taps;
75 fir->curr_pos = taps - 1;
76 fir->coeffs = coeffs;
77 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
78 return fir->history;
81 static inline void fir16_flush(struct fir16_state_t *fir)
83 memset(fir->history, 0, fir->taps * sizeof(int16_t));
86 static inline void fir16_free(struct fir16_state_t *fir)
88 kfree(fir->history);
91 static inline int16_t fir16(struct fir16_state_t *fir, int16_t sample)
93 int32_t y;
94 int i;
95 int offset1;
96 int offset2;
98 fir->history[fir->curr_pos] = sample;
100 offset2 = fir->curr_pos;
101 offset1 = fir->taps - offset2;
102 y = 0;
103 for (i = fir->taps - 1; i >= offset1; i--)
104 y += fir->coeffs[i] * fir->history[i - offset1];
105 for (; i >= 0; i--)
106 y += fir->coeffs[i] * fir->history[i + offset2];
107 if (fir->curr_pos <= 0)
108 fir->curr_pos = fir->taps;
109 fir->curr_pos--;
110 return (int16_t) (y >> 15);
113 static inline const int16_t *fir32_create(struct fir32_state_t *fir,
114 const int32_t *coeffs, int taps)
116 fir->taps = taps;
117 fir->curr_pos = taps - 1;
118 fir->coeffs = coeffs;
119 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
120 return fir->history;
123 static inline void fir32_flush(struct fir32_state_t *fir)
125 memset(fir->history, 0, fir->taps * sizeof(int16_t));
128 static inline void fir32_free(struct fir32_state_t *fir)
130 kfree(fir->history);
133 static inline int16_t fir32(struct fir32_state_t *fir, int16_t sample)
135 int i;
136 int32_t y;
137 int offset1;
138 int offset2;
140 fir->history[fir->curr_pos] = sample;
141 offset2 = fir->curr_pos;
142 offset1 = fir->taps - offset2;
143 y = 0;
144 for (i = fir->taps - 1; i >= offset1; i--)
145 y += fir->coeffs[i] * fir->history[i - offset1];
146 for (; i >= 0; i--)
147 y += fir->coeffs[i] * fir->history[i + offset2];
148 if (fir->curr_pos <= 0)
149 fir->curr_pos = fir->taps;
150 fir->curr_pos--;
151 return (int16_t) (y >> 15);
154 #endif