summaryrefslogtreecommitdiff
path: root/arith.h
blob: 96541c8313e3acd1c83c167a3e60efee3fdc38ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#ifndef _ZAPTEL_ARITH_H
#define _ZAPTEL_ARITH_H
/*
 * Handy add/subtract functions to operate on chunks of shorts.
 * Feel free to add customizations for additional architectures
 *
 */

#ifdef CONFIG_ZAPTEL_MMX
#ifdef ZT_CHUNKSIZE
static inline void __ACSS(volatile short *dst, const short *src)
{
	__asm__ __volatile__ (
	        "movq 0(%0), %%mm0;\n"
	        "movq 0(%1), %%mm1;\n"
                "movq 8(%0), %%mm2;\n"
	        "movq 8(%1), %%mm3;\n"
	        "paddsw %%mm1, %%mm0;\n"
	        "paddsw %%mm3, %%mm2;\n"
                "movq %%mm0, 0(%0);\n"
                "movq %%mm2, 8(%0);\n"
	    : "=r" (dst)
	    : "r" (src), "0" (dst)
	    : "memory"
#if CLOBBERMMX
	    , "%mm0", "%mm1", "%mm2", "%mm3"
#endif
      );

}
static inline void __SCSS(volatile short *dst, const short *src)
{
	__asm__ __volatile__ (
	        "movq 0(%0), %%mm0;\n"
	        "movq 0(%1), %%mm1;\n"
                "movq 8(%0), %%mm2;\n"
	        "movq 8(%1), %%mm3;\n"
	        "psubsw %%mm1, %%mm0;\n"
	        "psubsw %%mm3, %%mm2;\n"
                "movq %%mm0, 0(%0);\n"
                "movq %%mm2, 8(%0);\n"
	    : "=r" (dst)
	    : "r" (src), "0" (dst)
	    : "memory"
#if CLOBBERMMX
	    , "%mm0", "%mm1", "%mm2", "%mm3"
#endif
      );

}

#if (ZT_CHUNKSIZE == 8)
#define ACSS(a,b) __ACSS(a,b)
#define SCSS(a,b) __SCSS(a,b)
#elif (ZT_CHUNKSIZE > 8)
static inline void ACSS(volatile short *dst, const short *src)
{
	int x;
	for (x=0;x<ZT_CHUNKSIZE;x+=8)
		__ACSS(dst + x, src + x);
}
static inline void SCSS(volatile short *dst, const short *src)
{
	int x;
	for (x=0;x<ZT_CHUNKSIZE;x+=8)
		__SCSS(dst + x, src + x);
}
#else
#error No MMX for ZT_CHUNKSIZE < 8
#endif
#endif
static inline int CONVOLVE(const int *coeffs, const short *hist, int len)
{
	int sum;
	/* Divide length by 16 */
	len >>= 4;
	
	/* Clear our accumulator, mm4 */
	
	/* 
	
	   For every set of eight...
	
	   Load 16 coefficients into four registers...
	   Shift each word right 16 to make them shorts...
	   Pack the resulting shorts into two registers...
	   With the coefficients now in mm0 and mm2, load the 
	        history into mm1 and mm3...
	   Multiply/add mm1 into mm0, and mm3 into mm2...
	   Add mm2 into mm0 (without saturation, alas).  Now we have two half-results.
	   Accumulate in mm4 (again, without saturation, alas)
	*/
	__asm__ (
		"pxor %%mm4, %%mm4;\n"
		"mov %1, %%edi;\n"
		"mov %2, %%esi;\n"
		"mov %3, %%ecx;\n"
		"1:"
			"movq  0(%%edi), %%mm0;\n"
			"movq  8(%%edi), %%mm1;\n"
			"movq 16(%%edi), %%mm2;\n"
			"movq 24(%%edi), %%mm3;\n"
			/* can't use 4/5 since 4 is the accumulator for us */
			"movq 32(%%edi), %%mm6;\n"
			"movq 40(%%edi), %%mm7;\n"
			"psrad $16, %%mm0;\n"
			"psrad $16, %%mm1;\n"
			"psrad $16, %%mm2;\n"
			"psrad $16, %%mm3;\n"
			"psrad $16, %%mm6;\n"
			"psrad $16, %%mm7;\n"
			"packssdw %%mm1, %%mm0;\n"
			"packssdw %%mm3, %%mm2;\n"
			"packssdw %%mm7, %%mm6;\n"
			"movq 0(%%esi), %%mm1;\n"
			"movq 8(%%esi), %%mm3;\n"
			"movq 16(%%esi), %%mm7;\n"
			"pmaddwd %%mm1, %%mm0;\n"
			"pmaddwd %%mm3, %%mm2;\n"
			"pmaddwd %%mm7, %%mm6;\n"
			"paddd %%mm6, %%mm4;\n"
			"paddd %%mm2, %%mm4;\n"
			"paddd %%mm0, %%mm4;\n"
			/* Come back and do for the last few bytes */
			"movq 48(%%edi), %%mm6;\n"
			"movq 56(%%edi), %%mm7;\n"
			"psrad $16, %%mm6;\n"
			"psrad $16, %%mm7;\n"
			"packssdw %%mm7, %%mm6;\n"
			"movq 24(%%esi), %%mm7;\n"
			"pmaddwd %%mm7, %%mm6;\n"
			"paddd %%mm6, %%mm4;\n"
			"add $64, %%edi;\n"
			"add $32, %%esi;\n"
			"dec %%ecx;\n"
		"jnz 1b;\n"
		"movq %%mm4, %%mm0;\n"
		"psrlq $32, %%mm0;\n"
		"paddd %%mm0, %%mm4;\n"
		"movd %%mm4, %0;\n"
		: "=r" (sum)
		: "r" (coeffs), "r" (hist), "r" (len)
		: "%ecx", "%edi", "%esi"
	);
		
	return sum;
}

static inline void UPDATE(volatile int *taps, const short *history, const int nsuppr, const int ntaps)
{
	int i;
	int correction;
	for (i=0;i<ntaps;i++) {
		correction = history[i] * nsuppr;
		taps[i] += correction;
	}
}

static inline void UPDATE2(volatile int *taps, volatile short *taps_short, const short *history, const int nsuppr, const int ntaps)
{
	int i;
	int correction;
#if 0
	ntaps >>= 4;
	/* First, load up taps, */
	__asm__ (
		"pxor %%mm4, %%mm4;\n"
		"mov %0, %%edi;\n"
		"mov %1, %%esi;\n"
		"mov %3, %%ecx;\n"
		"1:"
		"jnz 1b;\n"
		"movq %%mm4, %%mm0;\n"
		"psrlq $32, %%mm0;\n"
		"paddd %%mm0, %%mm4;\n"
		"movd %%mm4, %0;\n"
		: "=r" (taps), "=r" (taps_short)
		: "r" (history), "r" (nsuppr), "r" (ntaps), "0" (taps)
		: "%ecx", "%edi", "%esi"
	);
#endif
#if 1
	for (i=0;i<ntaps;i++) {
		correction = history[i] * nsuppr;
		taps[i] += correction;
		taps_short[i] = taps[i] >> 16;
	}
#endif	
}

static inline int CONVOLVE2(const short *coeffs, const short *hist, int len)
{
	int sum;
	/* Divide length by 16 */
	len >>= 4;
	
	/* Clear our accumulator, mm4 */
	
	/* 
	
	   For every set of eight...
	   Load in eight coefficients and eight historic samples, multliply add and
	   accumulate the result
	*/
	__asm__ (
		"pxor %%mm4, %%mm4;\n"
		"mov %1, %%edi;\n"
		"mov %2, %%esi;\n"
		"mov %3, %%ecx;\n"
		"1:"
			"movq  0(%%edi), %%mm0;\n"
			"movq  8(%%edi), %%mm2;\n"
			"movq 0(%%esi), %%mm1;\n"
			"movq 8(%%esi), %%mm3;\n"
			"pmaddwd %%mm1, %%mm0;\n"
			"pmaddwd %%mm3, %%mm2;\n"
			"paddd %%mm2, %%mm4;\n"
			"paddd %%mm0, %%mm4;\n"
			"movq  16(%%edi), %%mm0;\n"
			"movq  24(%%edi), %%mm2;\n"
			"movq 16(%%esi), %%mm1;\n"
			"movq 24(%%esi), %%mm3;\n"
			"pmaddwd %%mm1, %%mm0;\n"
			"pmaddwd %%mm3, %%mm2;\n"
			"paddd %%mm2, %%mm4;\n"
			"paddd %%mm0, %%mm4;\n"
			"add $32, %%edi;\n"
			"add $32, %%esi;\n"
			"dec %%ecx;\n"
		"jnz 1b;\n"
		"movq %%mm4, %%mm0;\n"
		"psrlq $32, %%mm0;\n"
		"paddd %%mm0, %%mm4;\n"
		"movd %%mm4, %0;\n"
		: "=r" (sum)
		: "r" (coeffs), "r" (hist), "r" (len)
		: "%ecx", "%edi", "%esi"
	);
		
	return sum;
}
static inline short MAX16(const short *y, int len, int *pos)
{
	int k;
	short max = 0;
	int bestpos = 0;
	for (k=0;k<len;k++) {
		if (max < y[k]) {
			bestpos = k;
			max = y[k];
		}
	}
	*pos = (len - 1 - bestpos);
	return max;
}



#else

#ifdef ZT_CHUNKSIZE
static inline void ACSS(short *dst, short *src)
{
	int x,sum;
	/* Add src to dst with saturation, storing in dst */
	for (x=0;x<ZT_CHUNKSIZE;x++) {
		sum = dst[x]+src[x];
		if (sum > 32767)
			sum = 32767;
		else if (sum < -32768)
			sum = -32768;
		dst[x] = sum;
	}
}

static inline void SCSS(short *dst, short *src)
{
	int x,sum;
	/* Add src to dst with saturation, storing in dst */
	for (x=0;x<ZT_CHUNKSIZE;x++) {
		sum = dst[x]-src[x];
		if (sum > 32767)
			sum = 32767;
		else if (sum < -32768)
			sum = -32768;
		dst[x] = sum;
	}
}

#endif	/* ZT_CHUNKSIZE */

static inline int CONVOLVE(const int *coeffs, const short *hist, int len)
{
	int x;
	int sum = 0;
	for (x=0;x<len;x++)
		sum += (coeffs[x] >> 16) * hist[x];
	return sum;
}

static inline int CONVOLVE2(const short *coeffs, const short *hist, int len)
{
	int x;
	int sum = 0;
	for (x=0;x<len;x++)
		sum += coeffs[x] * hist[x];
	return sum;
}

static inline void UPDATE(int *taps, const short *history, const int nsuppr, const int ntaps)
{
	int i;
	int correction;
	for (i=0;i<ntaps;i++) {
		correction = history[i] * nsuppr;
		taps[i] += correction;
	}
}

static inline void UPDATE2(int *taps, short *taps_short, const short *history, const int nsuppr, const int ntaps)
{
	int i;
	int correction;
	for (i=0;i<ntaps;i++) {
		correction = history[i] * nsuppr;
		taps[i] += correction;
		taps_short[i] = taps[i] >> 16;
	}
}

static inline short MAX16(const short *y, int len, int *pos)
{
	int k;
	short max = 0;
	int bestpos = 0;
	for (k=0;k<len;k++) {
		if (max < y[k]) {
			bestpos = k;
			max = y[k];
		}
	}
	*pos = (len - 1 - bestpos);
	return max;
}

#endif	/* MMX */
#endif	/* _ZAPTEL_ARITH_H */