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
8 #include "libavutil/mem.h"
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
;
33 for (i
= 0; i
< 0x20 / 2; i
+= 4)
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
))
58 for (i
= 0; i
< 0x20; i
+= 0x10)
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;
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;
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)
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))
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)
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))
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)
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))
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];
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]);
285 To do saturation efficiently in x86 we can use fist(p)s,
286 pf2iw, or packssdw. We use fist(p)s here.
416 ".byte 0xdf, 0xc0\n\t" // ffreep %%st(0)
418 :"m"(costab_mmx
[30]), "r"(b1
), "r"(b2
), "r"(out0
), "r"(out1
)