Add a comment that explains why this header has no multiple inclusion guards.
[mplayer/greg.git] / mp3lib / dct64_sse.c
blobad835ef14f94e922ea249518555c581a00ca41cf
1 /*
2 * Discrete Cosine Tansform (DCT) for SSE
3 * Copyright (c) 2006 Zuxy MENG <zuxy.meng@gmail.com>
4 * based upon code from mp3lib/dct64.c, mp3lib/dct64_altivec.c
5 * and mp3lib/dct64_mmx.c
6 */
8 #include <libavutil/mem.h>
10 typedef float real;
12 extern float __attribute__((aligned(16))) costab_mmx[];
14 static const int ppnn[4] __attribute__((aligned(16))) =
15 { 0, 0, 1 << 31, 1 << 31 };
17 static const int pnpn[4] __attribute__((aligned(16))) =
18 { 0, 1 << 31, 0, 1 << 31 };
20 static const int nnnn[4] __attribute__((aligned(16))) =
21 { 1 << 31, 1 << 31, 1 << 31, 1 << 31 };
23 void dct64_sse(short *out0,short *out1,real *c)
25 DECLARE_ALIGNED(16, real, b1[0x20]);
26 DECLARE_ALIGNED(16, real, b2[0x20]);
27 static real const one = 1.f;
30 real *costab = costab_mmx;
31 int i;
33 for (i = 0; i < 0x20 / 2; i += 4)
35 asm(
36 "movaps %2, %%xmm3\n\t"
37 "shufps $27, %%xmm3, %%xmm3\n\t"
38 "movaps %3, %%xmm1\n\t"
39 "movaps %%xmm1, %%xmm4\n\t"
40 "movaps %4, %%xmm2\n\t"
41 "shufps $27, %%xmm4, %%xmm4\n\t"
42 "movaps %%xmm2, %%xmm0\n\t"
43 "shufps $27, %%xmm0, %%xmm0\n\t"
44 "addps %%xmm0, %%xmm1\n\t"
45 "movaps %%xmm1, %0\n\t"
46 "subps %%xmm2, %%xmm4\n\t"
47 "mulps %%xmm3, %%xmm4\n\t"
48 "movaps %%xmm4, %1\n\t"
49 :"=m"(*(b1 + i)), "=m"(*(b1 + 0x1c - i))
50 :"m"(*(costab + i)), "m"(*(c + i)), "m"(*(c + 0x1c - i))
56 int i;
58 for (i = 0; i < 0x20; i += 0x10)
60 asm(
61 "movaps %4, %%xmm1\n\t"
62 "movaps %5, %%xmm3\n\t"
63 "movaps %6, %%xmm4\n\t"
64 "movaps %7, %%xmm6\n\t"
65 "movaps %%xmm1, %%xmm7\n\t"
66 "shufps $27, %%xmm7, %%xmm7\n\t"
67 "movaps %%xmm3, %%xmm5\n\t"
68 "shufps $27, %%xmm5, %%xmm5\n\t"
69 "movaps %%xmm4, %%xmm2\n\t"
70 "shufps $27, %%xmm2, %%xmm2\n\t"
71 "movaps %%xmm6, %%xmm0\n\t"
72 "shufps $27, %%xmm0, %%xmm0\n\t"
73 "addps %%xmm0, %%xmm1\n\t"
74 "movaps %%xmm1, %0\n\t"
75 "addps %%xmm2, %%xmm3\n\t"
76 "movaps %%xmm3, %1\n\t"
77 "subps %%xmm4, %%xmm5\n\t"
78 "movaps %%xmm5, %2\n\t"
79 "subps %%xmm6, %%xmm7\n\t"
80 "movaps %%xmm7, %3\n\t"
81 :"=m"(*(b2 + i)), "=m"(*(b2 + i + 4)), "=m"(*(b2 + i + 8)), "=m"(*(b2 + i + 12))
82 :"m"(*(b1 + i)), "m"(*(b1 + i + 4)), "m"(*(b1 + i + 8)), "m"(*(b1 + i + 12))
88 real *costab = costab_mmx + 16;
89 asm(
90 "movaps %4, %%xmm0\n\t"
91 "movaps %5, %%xmm1\n\t"
92 "movaps %8, %%xmm4\n\t"
93 "xorps %%xmm6, %%xmm6\n\t"
94 "shufps $27, %%xmm4, %%xmm4\n\t"
95 "mulps %%xmm4, %%xmm1\n\t"
96 "movaps %9, %%xmm2\n\t"
97 "xorps %%xmm7, %%xmm7\n\t"
98 "shufps $27, %%xmm2, %%xmm2\n\t"
99 "mulps %%xmm2, %%xmm0\n\t"
100 "movaps %%xmm0, %0\n\t"
101 "movaps %%xmm1, %1\n\t"
102 "movaps %6, %%xmm3\n\t"
103 "mulps %%xmm2, %%xmm3\n\t"
104 "subps %%xmm3, %%xmm6\n\t"
105 "movaps %%xmm6, %2\n\t"
106 "movaps %7, %%xmm5\n\t"
107 "mulps %%xmm4, %%xmm5\n\t"
108 "subps %%xmm5, %%xmm7\n\t"
109 "movaps %%xmm7, %3\n\t"
110 :"=m"(*(b2 + 8)), "=m"(*(b2 + 0xc)), "=m"(*(b2 + 0x18)), "=m"(*(b2 + 0x1c))
111 :"m"(*(b2 + 8)), "m"(*(b2 + 0xc)), "m"(*(b2 + 0x18)), "m"(*(b2 + 0x1c)), "m"(*costab), "m"(*(costab + 4))
116 real *costab = costab_mmx + 24;
117 int i;
119 asm(
120 "movaps %0, %%xmm0\n\t"
121 "shufps $27, %%xmm0, %%xmm0\n\t"
122 "movaps %1, %%xmm5\n\t"
123 "movaps %%xmm5, %%xmm6\n\t"
125 :"m"(*costab), "m"(*nnnn)
128 for (i = 0; i < 0x20; i += 8)
130 asm(
131 "movaps %2, %%xmm2\n\t"
132 "movaps %3, %%xmm3\n\t"
133 "movaps %%xmm2, %%xmm4\n\t"
134 "xorps %%xmm5, %%xmm6\n\t"
135 "shufps $27, %%xmm4, %%xmm4\n\t"
136 "movaps %%xmm3, %%xmm1\n\t"
137 "shufps $27, %%xmm1, %%xmm1\n\t"
138 "addps %%xmm1, %%xmm2\n\t"
139 "movaps %%xmm2, %0\n\t"
140 "subps %%xmm3, %%xmm4\n\t"
141 "xorps %%xmm6, %%xmm4\n\t"
142 "mulps %%xmm0, %%xmm4\n\t"
143 "movaps %%xmm4, %1\n\t"
144 :"=m"(*(b1 + i)), "=m"(*(b1 + i + 4))
145 :"m"(*(b2 + i)), "m"(*(b2 + i + 4))
151 int i;
153 asm(
154 "movss %0, %%xmm1\n\t"
155 "movss %1, %%xmm0\n\t"
156 "movaps %%xmm1, %%xmm3\n\t"
157 "unpcklps %%xmm0, %%xmm3\n\t"
158 "movss %2, %%xmm2\n\t"
159 "movaps %%xmm1, %%xmm0\n\t"
160 "unpcklps %%xmm2, %%xmm0\n\t"
161 "unpcklps %%xmm3, %%xmm0\n\t"
162 "movaps %3, %%xmm2\n\t"
164 :"m"(one), "m"(costab_mmx[28]), "m"(costab_mmx[29]), "m"(*ppnn)
167 for (i = 0; i < 0x20; i += 8)
169 asm(
170 "movaps %2, %%xmm3\n\t"
171 "movaps %%xmm3, %%xmm4\n\t"
172 "shufps $20, %%xmm4, %%xmm4\n\t"
173 "shufps $235, %%xmm3, %%xmm3\n\t"
174 "xorps %%xmm2, %%xmm3\n\t"
175 "addps %%xmm3, %%xmm4\n\t"
176 "mulps %%xmm0, %%xmm4\n\t"
177 "movaps %%xmm4, %0\n\t"
178 "movaps %3, %%xmm6\n\t"
179 "movaps %%xmm6, %%xmm5\n\t"
180 "shufps $27, %%xmm5, %%xmm5\n\t"
181 "xorps %%xmm2, %%xmm5\n\t"
182 "addps %%xmm5, %%xmm6\n\t"
183 "mulps %%xmm0, %%xmm6\n\t"
184 "movaps %%xmm6, %1\n\t"
185 :"=m"(*(b2 + i)), "=m"(*(b2 + i + 4))
186 :"m"(*(b1 + i)), "m"(*(b1 + i + 4))
192 int i;
193 asm(
194 "movss %0, %%xmm0\n\t"
195 "movaps %%xmm1, %%xmm2\n\t"
196 "movaps %%xmm0, %%xmm7\n\t"
197 "unpcklps %%xmm1, %%xmm2\n\t"
198 "unpcklps %%xmm0, %%xmm7\n\t"
199 "movaps %1, %%xmm0\n\t"
200 "unpcklps %%xmm7, %%xmm2\n\t"
202 :"m"(costab_mmx[30]), "m"(*pnpn)
205 for (i = 0x8; i < 0x20; i += 8)
207 asm volatile (
208 "movaps %2, %%xmm1\n\t"
209 "movaps %%xmm1, %%xmm3\n\t"
210 "shufps $224, %%xmm3, %%xmm3\n\t"
211 "shufps $181, %%xmm1, %%xmm1\n\t"
212 "xorps %%xmm0, %%xmm1\n\t"
213 "addps %%xmm1, %%xmm3\n\t"
214 "mulps %%xmm2, %%xmm3\n\t"
215 "movaps %%xmm3, %0\n\t"
216 "movaps %3, %%xmm4\n\t"
217 "movaps %%xmm4, %%xmm5\n\t"
218 "shufps $224, %%xmm5, %%xmm5\n\t"
219 "shufps $181, %%xmm4, %%xmm4\n\t"
220 "xorps %%xmm0, %%xmm4\n\t"
221 "addps %%xmm4, %%xmm5\n\t"
222 "mulps %%xmm2, %%xmm5\n\t"
223 "movaps %%xmm5, %1\n\t"
224 :"=m"(*(b1 + i)), "=m"(*(b1 + i + 4))
225 :"m"(*(b2 + i)), "m"(*(b2 + i + 4))
226 :"memory"
229 for (i = 0x8; i < 0x20; i += 8)
231 b1[i + 2] += b1[i + 3];
232 b1[i + 6] += b1[i + 7];
233 b1[i + 4] += b1[i + 6];
234 b1[i + 6] += b1[i + 5];
235 b1[i + 5] += b1[i + 7];
239 #if 0
240 /* Reference C code */
243 Should run faster than x87 asm, given that the compiler is sane.
244 However, the C code dosen't round with saturation (0x7fff for too
245 large positive float, 0x8000 for too small negative float). You
246 can hear the difference if you listen carefully.
249 out0[256] = (short)(b2[0] + b2[1]);
250 out0[0] = (short)((b2[0] - b2[1]) * costab_mmx[30]);
251 out1[128] = (short)((b2[3] - b2[2]) * costab_mmx[30]);
252 out0[128] = (short)((b2[3] - b2[2]) * costab_mmx[30] + b2[3] + b2[2]);
253 out1[192] = (short)((b2[7] - b2[6]) * costab_mmx[30]);
254 out0[192] = (short)((b2[7] - b2[6]) * costab_mmx[30] + b2[6] + b2[7] + b2[4] + b2[5]);
255 out0[64] = (short)((b2[7] - b2[6]) * costab_mmx[30] + b2[6] + b2[7] + (b2[4] - b2[5]) * costab_mmx[30]);
256 out1[64] = (short)((b2[7] - b2[6]) * costab_mmx[30] + (b2[4] - b2[5]) * costab_mmx[30]);
258 out0[224] = (short)(b1[8] + b1[12]);
259 out0[160] = (short)(b1[12] + b1[10]);
260 out0[96] = (short)(b1[10] + b1[14]);
261 out0[32] = (short)(b1[14] + b1[9]);
262 out1[32] = (short)(b1[9] + b1[13]);
263 out1[96] = (short)(b1[13] + b1[11]);
264 out1[224] = (short)b1[15];
265 out1[160] = (short)(b1[15] + b1[11]);
266 out0[240] = (short)(b1[24] + b1[28] + b1[16]);
267 out0[208] = (short)(b1[24] + b1[28] + b1[20]);
268 out0[176] = (short)(b1[28] + b1[26] + b1[20]);
269 out0[144] = (short)(b1[28] + b1[26] + b1[18]);
270 out0[112] = (short)(b1[26] + b1[30] + b1[18]);
271 out0[80] = (short)(b1[26] + b1[30] + b1[22]);
272 out0[48] = (short)(b1[30] + b1[25] + b1[22]);
273 out0[16] = (short)(b1[30] + b1[25] + b1[17]);
274 out1[16] = (short)(b1[25] + b1[29] + b1[17]);
275 out1[48] = (short)(b1[25] + b1[29] + b1[21]);
276 out1[80] = (short)(b1[29] + b1[27] + b1[21]);
277 out1[112] = (short)(b1[29] + b1[27] + b1[19]);
278 out1[144] = (short)(b1[27] + b1[31] + b1[19]);
279 out1[176] = (short)(b1[27] + b1[31] + b1[23]);
280 out1[240] = (short)(b1[31]);
281 out1[208] = (short)(b1[31] + b1[23]);
283 #else
285 To do saturation efficiently in x86 we can use fist(t)(p),
286 pf2iw, or packssdw. We use fist(p) here.
288 asm(
289 "flds %0\n\t"
290 "flds (%2)\n\t"
291 "fadds 4(%2)\n\t"
292 "fistp 512(%3)\n\t"
294 "flds (%2)\n\t"
295 "fsubs 4(%2)\n\t"
296 "fmul %%st(1)\n\t"
297 "fistp (%3)\n\t"
299 "flds 12(%2)\n\t"
300 "fsubs 8(%2)\n\t"
301 "fmul %%st(1)\n\t"
302 "fist 256(%4)\n\t"
303 "fadds 12(%2)\n\t"
304 "fadds 8(%2)\n\t"
305 "fistp 256(%3)\n\t"
307 "flds 16(%2)\n\t"
308 "fsubs 20(%2)\n\t"
309 "fmul %%st(1)\n\t"
311 "flds 28(%2)\n\t"
312 "fsubs 24(%2)\n\t"
313 "fmul %%st(2)\n\t"
314 "fist 384(%4)\n\t"
315 "fld %%st(0)\n\t"
316 "fadds 24(%2)\n\t"
317 "fadds 28(%2)\n\t"
318 "fld %%st(0)\n\t"
319 "fadds 16(%2)\n\t"
320 "fadds 20(%2)\n\t"
321 "fistp 384(%3)\n\t"
322 "fadd %%st(2)\n\t"
323 "fistp 128(%3)\n\t"
324 "faddp %%st(1)\n\t"
325 "fistp 128(%4)\n\t"
327 "flds 32(%1)\n\t"
328 "fadds 48(%1)\n\t"
329 "fistp 448(%3)\n\t"
331 "flds 48(%1)\n\t"
332 "fadds 40(%1)\n\t"
333 "fistp 320(%3)\n\t"
335 "flds 40(%1)\n\t"
336 "fadds 56(%1)\n\t"
337 "fistp 192(%3)\n\t"
339 "flds 56(%1)\n\t"
340 "fadds 36(%1)\n\t"
341 "fistp 64(%3)\n\t"
343 "flds 36(%1)\n\t"
344 "fadds 52(%1)\n\t"
345 "fistp 64(%4)\n\t"
347 "flds 52(%1)\n\t"
348 "fadds 44(%1)\n\t"
349 "fistp 192(%4)\n\t"
351 "flds 60(%1)\n\t"
352 "fist 448(%4)\n\t"
353 "fadds 44(%1)\n\t"
354 "fistp 320(%4)\n\t"
356 "flds 96(%1)\n\t"
357 "fadds 112(%1)\n\t"
358 "fld %%st(0)\n\t"
359 "fadds 64(%1)\n\t"
360 "fistp 480(%3)\n\t"
361 "fadds 80(%1)\n\t"
362 "fistp 416(%3)\n\t"
364 "flds 112(%1)\n\t"
365 "fadds 104(%1)\n\t"
366 "fld %%st(0)\n\t"
367 "fadds 80(%1)\n\t"
368 "fistp 352(%3)\n\t"
369 "fadds 72(%1)\n\t"
370 "fistp 288(%3)\n\t"
372 "flds 104(%1)\n\t"
373 "fadds 120(%1)\n\t"
374 "fld %%st(0)\n\t"
375 "fadds 72(%1)\n\t"
376 "fistp 224(%3)\n\t"
377 "fadds 88(%1)\n\t"
378 "fistp 160(%3)\n\t"
380 "flds 120(%1)\n\t"
381 "fadds 100(%1)\n\t"
382 "fld %%st(0)\n\t"
383 "fadds 88(%1)\n\t"
384 "fistp 96(%3)\n\t"
385 "fadds 68(%1)\n\t"
386 "fistp 32(%3)\n\t"
388 "flds 100(%1)\n\t"
389 "fadds 116(%1)\n\t"
390 "fld %%st(0)\n\t"
391 "fadds 68(%1)\n\t"
392 "fistp 32(%4)\n\t"
393 "fadds 84(%1)\n\t"
394 "fistp 96(%4)\n\t"
396 "flds 116(%1)\n\t"
397 "fadds 108(%1)\n\t"
398 "fld %%st(0)\n\t"
399 "fadds 84(%1)\n\t"
400 "fistp 160(%4)\n\t"
401 "fadds 76(%1)\n\t"
402 "fistp 224(%4)\n\t"
404 "flds 108(%1)\n\t"
405 "fadds 124(%1)\n\t"
406 "fld %%st(0)\n\t"
407 "fadds 76(%1)\n\t"
408 "fistp 288(%4)\n\t"
409 "fadds 92(%1)\n\t"
410 "fistp 352(%4)\n\t"
412 "flds 124(%1)\n\t"
413 "fist 480(%4)\n\t"
414 "fadds 92(%1)\n\t"
415 "fistp 416(%4)\n\t"
416 ".byte 0xdf, 0xc0\n\t" // ffreep %%st(0)
418 :"m"(costab_mmx[30]), "r"(b1), "r"(b2), "r"(out0), "r"(out1)
419 :"memory"
421 #endif
422 out1[0] = out0[0];