Debian Codegraph @

drivers/dahdi/arith.h

raw
1 /*
2 * Handy add/subtract functions to operate on chunks of shorts.
3 * Feel free to add customizations for additional architectures
4 *
5 */
6
7 /*
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2 as published by the
16 * Free Software Foundation. See the LICENSE file included with
17 * this program for more details.
18 */
19
20 #ifndef _DAHDI_ARITH_H
21 #define _DAHDI_ARITH_H
22
23 #ifdef CONFIG_DAHDI_MMX
24 #ifdef DAHDI_CHUNKSIZE
25 static inline void __ACSS(volatile short *dst, const short *src)
26 {
27 __asm__ __volatile__ (
28 "movq 0(%0), %%mm0;\n"
29 "movq 0(%1), %%mm1;\n"
30 "movq 8(%0), %%mm2;\n"
31 "movq 8(%1), %%mm3;\n"
32 "paddsw %%mm1, %%mm0;\n"
33 "paddsw %%mm3, %%mm2;\n"
34 "movq %%mm0, 0(%0);\n"
35 "movq %%mm2, 8(%0);\n"
36 : "=r" (dst)
37 : "r" (src), "0" (dst)
38 : "memory"
39 #ifdef CLOBBERMMX
40 , "%mm0", "%mm1", "%mm2", "%mm3"
41 #endif
42 );
43
44 }
45 static inline void __SCSS(volatile short *dst, const short *src)
46 {
47 __asm__ __volatile__ (
48 "movq 0(%0), %%mm0;\n"
49 "movq 0(%1), %%mm1;\n"
50 "movq 8(%0), %%mm2;\n"
51 "movq 8(%1), %%mm3;\n"
52 "psubsw %%mm1, %%mm0;\n"
53 "psubsw %%mm3, %%mm2;\n"
54 "movq %%mm0, 0(%0);\n"
55 "movq %%mm2, 8(%0);\n"
56 : "=r" (dst)
57 : "r" (src), "0" (dst)
58 : "memory"
59 #ifdef CLOBBERMMX
60 , "%mm0", "%mm1", "%mm2", "%mm3"
61 #endif
62 );
63
64 }
65
66 #if (DAHDI_CHUNKSIZE == 8)
67 #define ACSS(a,b) __ACSS(a,b)
68 #define SCSS(a,b) __SCSS(a,b)
69 #elif (DAHDI_CHUNKSIZE > 8)
70 static inline void ACSS(volatile short *dst, const short *src)
71 {
72 int x;
73 for (x=0;x<DAHDI_CHUNKSIZE;x+=8)
74 __ACSS(dst + x, src + x);
75 }
76 static inline void SCSS(volatile short *dst, const short *src)
77 {
78 int x;
79 for (x=0;x<DAHDI_CHUNKSIZE;x+=8)
80 __SCSS(dst + x, src + x);
81 }
82 #else
83 #error No MMX for DAHDI_CHUNKSIZE < 8
84 #endif
85 #endif
86 static inline int CONVOLVE(const int *coeffs, const short *hist, int len)
87 {
88 int sum;
89 /* Divide length by 16 */
90 len >>= 4;
91
92 /* Clear our accumulator, mm4 */
93
94 /*
95
96 For every set of eight...
97
98 Load 16 coefficients into four registers...
99 Shift each word right 16 to make them shorts...
100 Pack the resulting shorts into two registers...
101 With the coefficients now in mm0 and mm2, load the
102 history into mm1 and mm3...
103 Multiply/add mm1 into mm0, and mm3 into mm2...
104 Add mm2 into mm0 (without saturation, alas). Now we have two half-results.
105 Accumulate in mm4 (again, without saturation, alas)
106 */
107 __asm__ (
108 "pxor %%mm4, %%mm4;\n"
109 "mov %1, %%edi;\n"
110 "mov %2, %%esi;\n"
111 "mov %3, %%ecx;\n"
112 "1:"
113 "movq 0(%%edi), %%mm0;\n"
114 "movq 8(%%edi), %%mm1;\n"
115 "movq 16(%%edi), %%mm2;\n"
116 "movq 24(%%edi), %%mm3;\n"
117 /* can't use 4/5 since 4 is the accumulator for us */
118 "movq 32(%%edi), %%mm6;\n"
119 "movq 40(%%edi), %%mm7;\n"
120 "psrad $16, %%mm0;\n"
121 "psrad $16, %%mm1;\n"
122 "psrad $16, %%mm2;\n"
123 "psrad $16, %%mm3;\n"
124 "psrad $16, %%mm6;\n"
125 "psrad $16, %%mm7;\n"
126 "packssdw %%mm1, %%mm0;\n"
127 "packssdw %%mm3, %%mm2;\n"
128 "packssdw %%mm7, %%mm6;\n"
129 "movq 0(%%esi), %%mm1;\n"
130 "movq 8(%%esi), %%mm3;\n"
131 "movq 16(%%esi), %%mm7;\n"
132 "pmaddwd %%mm1, %%mm0;\n"
133 "pmaddwd %%mm3, %%mm2;\n"
134 "pmaddwd %%mm7, %%mm6;\n"
135 "paddd %%mm6, %%mm4;\n"
136 "paddd %%mm2, %%mm4;\n"
137 "paddd %%mm0, %%mm4;\n"
138 /* Come back and do for the last few bytes */
139 "movq 48(%%edi), %%mm6;\n"
140 "movq 56(%%edi), %%mm7;\n"
141 "psrad $16, %%mm6;\n"
142 "psrad $16, %%mm7;\n"
143 "packssdw %%mm7, %%mm6;\n"
144 "movq 24(%%esi), %%mm7;\n"
145 "pmaddwd %%mm7, %%mm6;\n"
146 "paddd %%mm6, %%mm4;\n"
147 "add $64, %%edi;\n"
148 "add $32, %%esi;\n"
149 "dec %%ecx;\n"
150 "jnz 1b;\n"
151 "movq %%mm4, %%mm0;\n"
152 "psrlq $32, %%mm0;\n"
153 "paddd %%mm0, %%mm4;\n"
154 "movd %%mm4, %0;\n"
155 : "=r" (sum)
156 : "r" (coeffs), "r" (hist), "r" (len)
157 : "%ecx", "%edi", "%esi"
158 );
159
160 return sum;
161 }
162
163 static inline void UPDATE(volatile int *taps, const short *history, const int nsuppr, const int ntaps)
164 {
165 int i;
166 int correction;
167 for (i=0;i<ntaps;i++) {
168 correction = history[i] * nsuppr;
169 taps[i] += correction;
170 }
171 }
172
173 static inline void UPDATE2(volatile int *taps, volatile short *taps_short, const short *history, const int nsuppr, const int ntaps)
174 {
175 int i;
176 int correction;
177 #if 0
178 ntaps >>= 4;
179 /* First, load up taps, */
180 __asm__ (
181 "pxor %%mm4, %%mm4;\n"
182 "mov %0, %%edi;\n"
183 "mov %1, %%esi;\n"
184 "mov %3, %%ecx;\n"
185 "1:"
186 "jnz 1b;\n"
187 "movq %%mm4, %%mm0;\n"
188 "psrlq $32, %%mm0;\n"
189 "paddd %%mm0, %%mm4;\n"
190 "movd %%mm4, %0;\n"
191 : "=r" (taps), "=r" (taps_short)
192 : "r" (history), "r" (nsuppr), "r" (ntaps), "0" (taps)
193 : "%ecx", "%edi", "%esi"
194 );
195 #endif
196 #if 1
197 for (i=0;i<ntaps;i++) {
198 correction = history[i] * nsuppr;
199 taps[i] += correction;
200 taps_short[i] = taps[i] >> 16;
201 }
202 #endif
203 }
204
205 static inline int CONVOLVE2(const short *coeffs, const short *hist, int len)
206 {
207 int sum;
208 /* Divide length by 16 */
209 len >>= 4;
210
211 /* Clear our accumulator, mm4 */
212
213 /*
214
215 For every set of eight...
216 Load in eight coefficients and eight historic samples, multliply add and
217 accumulate the result
218 */
219 __asm__ (
220 "pxor %%mm4, %%mm4;\n"
221 "mov %1, %%edi;\n"
222 "mov %2, %%esi;\n"
223 "mov %3, %%ecx;\n"
224 "1:"
225 "movq 0(%%edi), %%mm0;\n"
226 "movq 8(%%edi), %%mm2;\n"
227 "movq 0(%%esi), %%mm1;\n"
228 "movq 8(%%esi), %%mm3;\n"
229 "pmaddwd %%mm1, %%mm0;\n"
230 "pmaddwd %%mm3, %%mm2;\n"
231 "paddd %%mm2, %%mm4;\n"
232 "paddd %%mm0, %%mm4;\n"
233 "movq 16(%%edi), %%mm0;\n"
234 "movq 24(%%edi), %%mm2;\n"
235 "movq 16(%%esi), %%mm1;\n"
236 "movq 24(%%esi), %%mm3;\n"
237 "pmaddwd %%mm1, %%mm0;\n"
238 "pmaddwd %%mm3, %%mm2;\n"
239 "paddd %%mm2, %%mm4;\n"
240 "paddd %%mm0, %%mm4;\n"
241 "add $32, %%edi;\n"
242 "add $32, %%esi;\n"
243 "dec %%ecx;\n"
244 "jnz 1b;\n"
245 "movq %%mm4, %%mm0;\n"
246 "psrlq $32, %%mm0;\n"
247 "paddd %%mm0, %%mm4;\n"
248 "movd %%mm4, %0;\n"
249 : "=r" (sum)
250 : "r" (coeffs), "r" (hist), "r" (len)
251 : "%ecx", "%edi", "%esi"
252 );
253
254 return sum;
255 }
256 static inline short MAX16(const short *y, int len, int *pos)
257 {
258 int k;
259 short max = 0;
260 int bestpos = 0;
261 for (k=0;k<len;k++) {
262 if (max < y[k]) {
263 bestpos = k;
264 max = y[k];
265 }
266 }
267 *pos = (len - 1 - bestpos);
268 return max;
269 }
270
271
272
273 #else
274
275 #ifdef DAHDI_CHUNKSIZE
276 static inline void ACSS(short *dst, short *src)
277 {
278 int x;
279
280 /* Add src to dst with saturation, storing in dst */
281
282 #ifdef BFIN
283 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
284 dst[x] = __builtin_bfin_add_fr1x16(dst[x], src[x]);
285 #else
286 int sum;
287
288 for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
289 sum = dst[x] + src[x];
290 if (sum > 32767)
291 sum = 32767;
292 else if (sum < -32768)
293 sum = -32768;
294 dst[x] = sum;
295 }
296 #endif
297 }
298
299 static inline void SCSS(short *dst, short *src)
300 {
301 int x;
302
303 /* Subtract src from dst with saturation, storing in dst */
304 #ifdef BFIN
305 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
306 dst[x] = __builtin_bfin_sub_fr1x16(dst[x], src[x]);
307 #else
308 int sum;
309
310 for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
311 sum = dst[x] - src[x];
312 if (sum > 32767)
313 sum = 32767;
314 else if (sum < -32768)
315 sum = -32768;
316 dst[x] = sum;
317 }
318 #endif
319 }
320
321 #endif /* DAHDI_CHUNKSIZE */
322
323 static inline int CONVOLVE(const int *coeffs, const short *hist, int len)
324 {
325 int x;
326 int sum = 0;
327 for (x=0;x<len;x++)
328 sum += (coeffs[x] >> 16) * hist[x];
329 return sum;
330 }
331
332 static inline int CONVOLVE2(const short *coeffs, const short *hist, int len)
333 {
334 int x;
335 int sum = 0;
336 for (x=0;x<len;x++)
337 sum += coeffs[x] * hist[x];
338 return sum;
339 }
340
341 static inline void UPDATE(int *taps, const short *history, const int nsuppr, const int ntaps)
342 {
343 int i;
344 int correction;
345 for (i=0;i<ntaps;i++) {
346 correction = history[i] * nsuppr;
347 taps[i] += correction;
348 }
349 }
350
351 static inline void UPDATE2(int *taps, short *taps_short, const short *history, const int nsuppr, const int ntaps)
352 {
353 int i;
354 int correction;
355 for (i=0;i<ntaps;i++) {
356 correction = history[i] * nsuppr;
357 taps[i] += correction;
358 taps_short[i] = taps[i] >> 16;
359 }
360 }
361
362 static inline short MAX16(const short *y, int len, int *pos)
363 {
364 int k;
365 short max = 0;
366 int bestpos = 0;
367 for (k=0;k<len;k++) {
368 if (max < y[k]) {
369 bestpos = k;
370 max = y[k];
371 }
372 }
373 *pos = (len - 1 - bestpos);
374 return max;
375 }
376
377 #endif /* MMX */
378 #endif /* _DAHDI_ARITH_H */