snow mmx+sse2 optimizations, part 4
[ffmpeg.git] / libavcodec / i386 / snowdsp_mmx.c
1 /*
2  * MMX and SSE2 optimized snow DSP utils
3  * Copyright (c) 2005-2006 Robert Edele <yartrebo@earthlink.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 #include "../avcodec.h"
21 #include "../snow.h"
22 #include "mmx.h"
23
24 static void always_inline snow_interleave_line_header(int * i, int width, DWTELEM * low, DWTELEM * high){
25     (*i) = (width) - 2;
26
27     if (width & 1){
28         low[(*i)+1] = low[((*i)+1)>>1];
29         (*i)--;
30     }
31 }
32
33 static void always_inline snow_horizontal_compose_lift_lead_out(int i, DWTELEM * dst, DWTELEM * src, DWTELEM * ref, int width, int w, int lift_high, int mul, int add, int shift){
34     for(; i<w; i++){
35         dst[i] = src[i] - ((mul * (ref[i] + ref[i + 1]) + add) >> shift);
36     }
37
38     if((width^lift_high)&1){
39         dst[w] = src[w] - ((mul * 2 * ref[w] + add) >> shift);
40     }
41 }
42
43 static void always_inline snow_horizontal_compose_liftS_lead_out(int i, DWTELEM * dst, DWTELEM * src, DWTELEM * ref, int width, int w){
44         for(; i<w; i++){
45             dst[i] = src[i] - (((-(ref[i] + ref[(i+1)])+W_BO) - 4 * src[i]) >> W_BS);
46         }
47
48         if(width&1){
49             dst[w] = src[w] - (((-2 * ref[w] + W_BO) - 4 * src[w]) >> W_BS);
50         }
51 }
52
53 void ff_snow_horizontal_compose97i_sse2(DWTELEM *b, int width){
54     const int w2= (width+1)>>1;
55     // SSE2 code runs faster with pointers aligned on a 32-byte boundary.
56     DWTELEM temp_buf[(width>>1) + 4];
57     DWTELEM * const temp = temp_buf + 4 - (((int)temp_buf & 0xF) >> 2);
58     const int w_l= (width>>1);
59     const int w_r= w2 - 1;
60     int i;
61
62     { // Lift 0
63         DWTELEM * const ref = b + w2 - 1;
64         DWTELEM b_0 = b[0]; //By allowing the first entry in b[0] to be calculated twice
65         // (the first time erroneously), we allow the SSE2 code to run an extra pass.
66         // The savings in code and time are well worth having to store this value and
67         // calculate b[0] correctly afterwards.
68
69         i = 0;
70         asm volatile(
71             "pcmpeqd   %%xmm7, %%xmm7         \n\t"
72             "pslld        $31, %%xmm7         \n\t"
73             "psrld        $29, %%xmm7         \n\t"
74         ::);
75         for(; i<w_l-7; i+=8){
76             asm volatile(
77                 "movdqu   (%1), %%xmm1        \n\t"
78                 "movdqu 16(%1), %%xmm5        \n\t"
79                 "movdqu  4(%1), %%xmm2        \n\t"
80                 "movdqu 20(%1), %%xmm6        \n\t"
81                 "paddd  %%xmm1, %%xmm2        \n\t"
82                 "paddd  %%xmm5, %%xmm6        \n\t"
83                 "movdqa %%xmm2, %%xmm0        \n\t"
84                 "movdqa %%xmm6, %%xmm4        \n\t"
85                 "paddd  %%xmm2, %%xmm2        \n\t"
86                 "paddd  %%xmm6, %%xmm6        \n\t"
87                 "paddd  %%xmm0, %%xmm2        \n\t"
88                 "paddd  %%xmm4, %%xmm6        \n\t"
89                 "paddd  %%xmm7, %%xmm2        \n\t"
90                 "paddd  %%xmm7, %%xmm6        \n\t"
91                 "psrad      $3, %%xmm2        \n\t"
92                 "psrad      $3, %%xmm6        \n\t"
93                 "movdqa   (%0), %%xmm0        \n\t"
94                 "movdqa 16(%0), %%xmm4        \n\t"
95                 "psubd  %%xmm2, %%xmm0        \n\t"
96                 "psubd  %%xmm6, %%xmm4        \n\t"
97                 "movdqa %%xmm0, (%0)          \n\t"
98                 "movdqa %%xmm4, 16(%0)        \n\t"
99                 :: "r"(&b[i]), "r"(&ref[i])
100                 : "memory"
101             );
102         }
103         snow_horizontal_compose_lift_lead_out(i, b, b, ref, width, w_l, 0, W_DM, W_DO, W_DS);
104         b[0] = b_0 - ((W_DM * 2 * ref[1]+W_DO)>>W_DS);
105     }
106
107     { // Lift 1
108         DWTELEM * const dst = b+w2;
109
110         i = 0;
111         for(; (((long)&dst[i]) & 0xF) && i<w_r; i++){
112             dst[i] = dst[i] - (b[i] + b[i + 1]);
113         }
114         for(; i<w_r-7; i+=8){
115             asm volatile(
116                 "movdqu   (%1), %%xmm1        \n\t"
117                 "movdqu 16(%1), %%xmm5        \n\t"
118                 "movdqu  4(%1), %%xmm2        \n\t"
119                 "movdqu 20(%1), %%xmm6        \n\t"
120                 "paddd  %%xmm1, %%xmm2        \n\t"
121                 "paddd  %%xmm5, %%xmm6        \n\t"
122                 "movdqa   (%0), %%xmm0        \n\t"
123                 "movdqa 16(%0), %%xmm4        \n\t"
124                 "psubd  %%xmm2, %%xmm0        \n\t"
125                 "psubd  %%xmm6, %%xmm4        \n\t"
126                 "movdqa %%xmm0, (%0)          \n\t"
127                 "movdqa %%xmm4, 16(%0)        \n\t"
128                 :: "r"(&dst[i]), "r"(&b[i])
129                 : "memory"
130             );
131         }
132         snow_horizontal_compose_lift_lead_out(i, dst, dst, b, width, w_r, 1, W_CM, W_CO, W_CS);
133     }
134
135     { // Lift 2
136         DWTELEM * const ref = b+w2 - 1;
137         DWTELEM b_0 = b[0];
138
139         i = 0;
140         asm volatile(
141             "pslld          $1, %%xmm7       \n\t" /* xmm7 already holds a '4' from 2 lifts ago. */
142         ::);
143         for(; i<w_l-7; i+=8){
144             asm volatile(
145                 "movdqu   (%1), %%xmm1        \n\t"
146                 "movdqu 16(%1), %%xmm5        \n\t"
147                 "movdqu  4(%1), %%xmm0        \n\t"
148                 "movdqu 20(%1), %%xmm4        \n\t"
149                 "paddd  %%xmm1, %%xmm0        \n\t"
150                 "paddd  %%xmm5, %%xmm4        \n\t"
151                 "movdqa %%xmm7, %%xmm1        \n\t"
152                 "movdqa %%xmm7, %%xmm5        \n\t"
153                 "psubd  %%xmm0, %%xmm1        \n\t"
154                 "psubd  %%xmm4, %%xmm5        \n\t"
155                 "movdqa   (%0), %%xmm0        \n\t"
156                 "movdqa 16(%0), %%xmm4        \n\t"
157                 "pslld      $2, %%xmm0        \n\t"
158                 "pslld      $2, %%xmm4        \n\t"
159                 "psubd  %%xmm0, %%xmm1        \n\t"
160                 "psubd  %%xmm4, %%xmm5        \n\t"
161                 "psrad      $4, %%xmm1        \n\t"
162                 "psrad      $4, %%xmm5        \n\t"
163                 "movdqa   (%0), %%xmm0        \n\t"
164                 "movdqa 16(%0), %%xmm4        \n\t"
165                 "psubd  %%xmm1, %%xmm0        \n\t"
166                 "psubd  %%xmm5, %%xmm4        \n\t"
167                 "movdqa %%xmm0, (%0)          \n\t"
168                 "movdqa %%xmm4, 16(%0)        \n\t"
169                 :: "r"(&b[i]), "r"(&ref[i])
170                 : "memory"
171             );
172         }
173         snow_horizontal_compose_liftS_lead_out(i, b, b, ref, width, w_l);
174         b[0] = b_0 - (((-2 * ref[1] + W_BO) - 4 * b_0) >> W_BS);
175     }
176
177     { // Lift 3
178         DWTELEM * const src = b+w2;
179
180         i = 0;
181         for(; (((long)&temp[i]) & 0xF) && i<w_r; i++){
182             temp[i] = src[i] - ((-W_AM*(b[i] + b[i+1]))>>W_AS);
183         }
184         for(; i<w_r-7; i+=8){
185             asm volatile(
186                 "movdqu  4(%1), %%xmm2        \n\t"
187                 "movdqu 20(%1), %%xmm6        \n\t"
188                 "paddd    (%1), %%xmm2        \n\t"
189                 "paddd  16(%1), %%xmm6        \n\t"
190                 "movdqa %%xmm2, %%xmm0        \n\t"
191                 "movdqa %%xmm6, %%xmm4        \n\t"
192                 "pslld      $2, %%xmm2        \n\t"
193                 "pslld      $2, %%xmm6        \n\t"
194                 "psubd  %%xmm2, %%xmm0        \n\t"
195                 "psubd  %%xmm6, %%xmm4        \n\t"
196                 "psrad      $1, %%xmm0        \n\t"
197                 "psrad      $1, %%xmm4        \n\t"
198                 "movdqu   (%0), %%xmm2        \n\t"
199                 "movdqu 16(%0), %%xmm6        \n\t"
200                 "psubd  %%xmm0, %%xmm2        \n\t"
201                 "psubd  %%xmm4, %%xmm6        \n\t"
202                 "movdqa %%xmm2, (%2)          \n\t"
203                 "movdqa %%xmm6, 16(%2)        \n\t"
204                 :: "r"(&src[i]), "r"(&b[i]), "r"(&temp[i])
205                  : "memory"
206                );
207         }
208         snow_horizontal_compose_lift_lead_out(i, temp, src, b, width, w_r, 1, -W_AM, W_AO, W_AS);
209     }
210
211     {
212         snow_interleave_line_header(&i, width, b, temp);
213
214         for (; (i & 0x1E) != 0x1E; i-=2){
215             b[i+1] = temp[i>>1];
216             b[i] = b[i>>1];
217         }
218         for (i-=30; i>=0; i-=32){
219             asm volatile(
220                 "movdqa      (%1), %%xmm0       \n\t"
221                 "movdqa    16(%1), %%xmm2       \n\t"
222                 "movdqa    32(%1), %%xmm4       \n\t"
223                 "movdqa    48(%1), %%xmm6       \n\t"
224                 "movdqa      (%1), %%xmm1       \n\t"
225                 "movdqa    16(%1), %%xmm3       \n\t"
226                 "movdqa    32(%1), %%xmm5       \n\t"
227                 "movdqa    48(%1), %%xmm7       \n\t"
228                 "punpckldq   (%2), %%xmm0       \n\t"
229                 "punpckldq 16(%2), %%xmm2       \n\t"
230                 "punpckldq 32(%2), %%xmm4       \n\t"
231                 "punpckldq 48(%2), %%xmm6       \n\t"
232                 "movdqa    %%xmm0, (%0)         \n\t"
233                 "movdqa    %%xmm2, 32(%0)       \n\t"
234                 "movdqa    %%xmm4, 64(%0)       \n\t"
235                 "movdqa    %%xmm6, 96(%0)       \n\t"
236                 "punpckhdq   (%2), %%xmm1       \n\t"
237                 "punpckhdq 16(%2), %%xmm3       \n\t"
238                 "punpckhdq 32(%2), %%xmm5       \n\t"
239                 "punpckhdq 48(%2), %%xmm7       \n\t"
240                 "movdqa    %%xmm1, 16(%0)       \n\t"
241                 "movdqa    %%xmm3, 48(%0)       \n\t"
242                 "movdqa    %%xmm5, 80(%0)       \n\t"
243                 "movdqa    %%xmm7, 112(%0)      \n\t"
244                 :: "r"(&(b)[i]), "r"(&(b)[i>>1]), "r"(&(temp)[i>>1])
245                  : "memory"
246                );
247         }
248     }
249 }
250
251 void ff_snow_horizontal_compose97i_mmx(DWTELEM *b, int width){
252     const int w2= (width+1)>>1;
253     DWTELEM temp[width >> 1];
254     const int w_l= (width>>1);
255     const int w_r= w2 - 1;
256     int i;
257
258     { // Lift 0
259         DWTELEM * const ref = b + w2 - 1;
260
261         i = 1;
262         b[0] = b[0] - ((W_DM * 2 * ref[1]+W_DO)>>W_DS);
263         asm volatile(
264             "pcmpeqd    %%mm7, %%mm7         \n\t"
265             "pslld        $31, %%mm7         \n\t"
266             "psrld        $29, %%mm7         \n\t"
267            ::);
268         for(; i<w_l-3; i+=4){
269             asm volatile(
270                 "movq     (%1), %%mm2        \n\t"
271                 "movq    8(%1), %%mm6        \n\t"
272                 "paddd   4(%1), %%mm2        \n\t"
273                 "paddd  12(%1), %%mm6        \n\t"
274                 "movq    %%mm2, %%mm0        \n\t"
275                 "movq    %%mm6, %%mm4        \n\t"
276                 "paddd   %%mm2, %%mm2        \n\t"
277                 "paddd   %%mm6, %%mm6        \n\t"
278                 "paddd   %%mm0, %%mm2        \n\t"
279                 "paddd   %%mm4, %%mm6        \n\t"
280                 "paddd   %%mm7, %%mm2        \n\t"
281                 "paddd   %%mm7, %%mm6        \n\t"
282                 "psrad      $3, %%mm2        \n\t"
283                 "psrad      $3, %%mm6        \n\t"
284                 "movq     (%0), %%mm0        \n\t"
285                 "movq    8(%0), %%mm4        \n\t"
286                 "psubd   %%mm2, %%mm0        \n\t"
287                 "psubd   %%mm6, %%mm4        \n\t"
288                 "movq    %%mm0, (%0)         \n\t"
289                 "movq    %%mm4, 8(%0)        \n\t"
290                 :: "r"(&b[i]), "r"(&ref[i])
291                  : "memory"
292                );
293         }
294         snow_horizontal_compose_lift_lead_out(i, b, b, ref, width, w_l, 0, W_DM, W_DO, W_DS);
295     }
296
297     { // Lift 1
298         DWTELEM * const dst = b+w2;
299
300         i = 0;
301         for(; i<w_r-3; i+=4){
302             asm volatile(
303                 "movq     (%1), %%mm2        \n\t"
304                 "movq    8(%1), %%mm6        \n\t"
305                 "paddd   4(%1), %%mm2        \n\t"
306                 "paddd  12(%1), %%mm6        \n\t"
307                 "movq     (%0), %%mm0        \n\t"
308                 "movq    8(%0), %%mm4        \n\t"
309                 "psubd   %%mm2, %%mm0        \n\t"
310                 "psubd   %%mm6, %%mm4        \n\t"
311                 "movq    %%mm0, (%0)         \n\t"
312                 "movq    %%mm4, 8(%0)        \n\t"
313                 :: "r"(&dst[i]), "r"(&b[i])
314                  : "memory"
315                );
316         }
317         snow_horizontal_compose_lift_lead_out(i, dst, dst, b, width, w_r, 1, W_CM, W_CO, W_CS);
318     }
319
320     { // Lift 2
321         DWTELEM * const ref = b+w2 - 1;
322
323         i = 1;
324         b[0] = b[0] - (((-2 * ref[1] + W_BO) - 4 * b[0]) >> W_BS);
325         asm volatile(
326             "pslld          $1, %%mm7       \n\t" /* xmm7 already holds a '4' from 2 lifts ago. */
327            ::);
328         for(; i<w_l-3; i+=4){
329             asm volatile(
330                 "movq     (%1), %%mm0        \n\t"
331                 "movq    8(%1), %%mm4        \n\t"
332                 "paddd   4(%1), %%mm0        \n\t"
333                 "paddd  12(%1), %%mm4        \n\t"
334                 "movq    %%mm7, %%mm1        \n\t"
335                 "movq    %%mm7, %%mm5        \n\t"
336                 "psubd   %%mm0, %%mm1        \n\t"
337                 "psubd   %%mm4, %%mm5        \n\t"
338                 "movq     (%0), %%mm0        \n\t"
339                 "movq    8(%0), %%mm4        \n\t"
340                 "pslld      $2, %%mm0        \n\t"
341                 "pslld      $2, %%mm4        \n\t"
342                 "psubd   %%mm0, %%mm1        \n\t"
343                 "psubd   %%mm4, %%mm5        \n\t"
344                 "psrad      $4, %%mm1        \n\t"
345                 "psrad      $4, %%mm5        \n\t"
346                 "movq     (%0), %%mm0        \n\t"
347                 "movq    8(%0), %%mm4        \n\t"
348                 "psubd   %%mm1, %%mm0        \n\t"
349                 "psubd   %%mm5, %%mm4        \n\t"
350                 "movq    %%mm0, (%0)         \n\t"
351                 "movq    %%mm4, 8(%0)        \n\t"
352                 :: "r"(&b[i]), "r"(&ref[i])
353                  : "memory"
354                );
355         }
356         snow_horizontal_compose_liftS_lead_out(i, b, b, ref, width, w_l);
357     }
358
359     { // Lift 3
360         DWTELEM * const src = b+w2;
361         i = 0;
362
363         for(; i<w_r-3; i+=4){
364             asm volatile(
365                 "movq    4(%1), %%mm2        \n\t"
366                 "movq   12(%1), %%mm6        \n\t"
367                 "paddd    (%1), %%mm2        \n\t"
368                 "paddd   8(%1), %%mm6        \n\t"
369                 "movq    %%mm2, %%mm0        \n\t"
370                 "movq    %%mm6, %%mm4        \n\t"
371                 "pslld      $2, %%mm2        \n\t"
372                 "pslld      $2, %%mm6        \n\t"
373                 "psubd   %%mm2, %%mm0        \n\t"
374                 "psubd   %%mm6, %%mm4        \n\t"
375                 "psrad      $1, %%mm0        \n\t"
376                 "psrad      $1, %%mm4        \n\t"
377                 "movq     (%0), %%mm2        \n\t"
378                 "movq    8(%0), %%mm6        \n\t"
379                 "psubd   %%mm0, %%mm2        \n\t"
380                 "psubd   %%mm4, %%mm6        \n\t"
381                 "movq    %%mm2, (%2)         \n\t"
382                 "movq    %%mm6, 8(%2)        \n\t"
383                 :: "r"(&src[i]), "r"(&b[i]), "r"(&temp[i])
384                  : "memory"
385                );
386         }
387         snow_horizontal_compose_lift_lead_out(i, temp, src, b, width, w_r, 1, -W_AM, W_AO, W_AS);
388     }
389
390     {
391         snow_interleave_line_header(&i, width, b, temp);
392
393         for (; (i & 0xE) != 0xE; i-=2){
394             b[i+1] = temp[i>>1];
395             b[i] = b[i>>1];
396         }
397         for (i-=14; i>=0; i-=16){
398             asm volatile(
399                 "movq        (%1), %%mm0       \n\t"
400                 "movq       8(%1), %%mm2       \n\t"
401                 "movq      16(%1), %%mm4       \n\t"
402                 "movq      24(%1), %%mm6       \n\t"
403                 "movq        (%1), %%mm1       \n\t"
404                 "movq       8(%1), %%mm3       \n\t"
405                 "movq      16(%1), %%mm5       \n\t"
406                 "movq      24(%1), %%mm7       \n\t"
407                 "punpckldq   (%2), %%mm0       \n\t"
408                 "punpckldq  8(%2), %%mm2       \n\t"
409                 "punpckldq 16(%2), %%mm4       \n\t"
410                 "punpckldq 24(%2), %%mm6       \n\t"
411                 "movq       %%mm0, (%0)        \n\t"
412                 "movq       %%mm2, 16(%0)      \n\t"
413                 "movq       %%mm4, 32(%0)      \n\t"
414                 "movq       %%mm6, 48(%0)      \n\t"
415                 "punpckhdq   (%2), %%mm1       \n\t"
416                 "punpckhdq  8(%2), %%mm3       \n\t"
417                 "punpckhdq 16(%2), %%mm5       \n\t"
418                 "punpckhdq 24(%2), %%mm7       \n\t"
419                 "movq       %%mm1, 8(%0)       \n\t"
420                 "movq       %%mm3, 24(%0)      \n\t"
421                 "movq       %%mm5, 40(%0)      \n\t"
422                 "movq       %%mm7, 56(%0)      \n\t"
423                 :: "r"(&b[i]), "r"(&b[i>>1]), "r"(&temp[i>>1])
424                  : "memory"
425                );
426         }
427     }
428 }
429
430 #define snow_vertical_compose_sse2_load_add(op,r,t0,t1,t2,t3)\
431         ""op" (%%"r",%%"REG_d",4), %%"t0"      \n\t"\
432         ""op" 16(%%"r",%%"REG_d",4), %%"t1"    \n\t"\
433         ""op" 32(%%"r",%%"REG_d",4), %%"t2"    \n\t"\
434         ""op" 48(%%"r",%%"REG_d",4), %%"t3"    \n\t"
435
436 #define snow_vertical_compose_sse2_load(r,t0,t1,t2,t3)\
437         snow_vertical_compose_sse2_load_add("movdqa",r,t0,t1,t2,t3)
438
439 #define snow_vertical_compose_sse2_add(r,t0,t1,t2,t3)\
440         snow_vertical_compose_sse2_load_add("paddd",r,t0,t1,t2,t3)
441
442 #define snow_vertical_compose_sse2_sub(s0,s1,s2,s3,t0,t1,t2,t3)\
443         "psubd %%"s0", %%"t0" \n\t"\
444         "psubd %%"s1", %%"t1" \n\t"\
445         "psubd %%"s2", %%"t2" \n\t"\
446         "psubd %%"s3", %%"t3" \n\t"
447
448 #define snow_vertical_compose_sse2_store(w,s0,s1,s2,s3)\
449         "movdqa %%"s0", (%%"w",%%"REG_d",4)      \n\t"\
450         "movdqa %%"s1", 16(%%"w",%%"REG_d",4)    \n\t"\
451         "movdqa %%"s2", 32(%%"w",%%"REG_d",4)    \n\t"\
452         "movdqa %%"s3", 48(%%"w",%%"REG_d",4)    \n\t"
453
454 #define snow_vertical_compose_sse2_sra(n,t0,t1,t2,t3)\
455         "psrad $"n", %%"t0" \n\t"\
456         "psrad $"n", %%"t1" \n\t"\
457         "psrad $"n", %%"t2" \n\t"\
458         "psrad $"n", %%"t3" \n\t"
459
460 #define snow_vertical_compose_sse2_r2r_add(s0,s1,s2,s3,t0,t1,t2,t3)\
461         "paddd %%"s0", %%"t0" \n\t"\
462         "paddd %%"s1", %%"t1" \n\t"\
463         "paddd %%"s2", %%"t2" \n\t"\
464         "paddd %%"s3", %%"t3" \n\t"
465
466 #define snow_vertical_compose_sse2_sll(n,t0,t1,t2,t3)\
467         "pslld $"n", %%"t0" \n\t"\
468         "pslld $"n", %%"t1" \n\t"\
469         "pslld $"n", %%"t2" \n\t"\
470         "pslld $"n", %%"t3" \n\t"
471
472 #define snow_vertical_compose_sse2_move(s0,s1,s2,s3,t0,t1,t2,t3)\
473         "movdqa %%"s0", %%"t0" \n\t"\
474         "movdqa %%"s1", %%"t1" \n\t"\
475         "movdqa %%"s2", %%"t2" \n\t"\
476         "movdqa %%"s3", %%"t3" \n\t"
477
478 void ff_snow_vertical_compose97i_sse2(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width){
479     long i = width;
480
481     while(i & 0xF)
482     {
483         i--;
484         b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
485         b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
486         b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
487         b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
488     }
489
490          asm volatile (
491         "jmp 2f                                      \n\t"
492         "1:                                          \n\t"
493
494         "mov %6, %%"REG_a"                           \n\t"
495         "mov %4, %%"REG_b"                           \n\t"
496
497         snow_vertical_compose_sse2_load(REG_b,"xmm0","xmm2","xmm4","xmm6")
498         snow_vertical_compose_sse2_add(REG_a,"xmm0","xmm2","xmm4","xmm6")
499         snow_vertical_compose_sse2_move("xmm0","xmm2","xmm4","xmm6","xmm1","xmm3","xmm5","xmm7")
500         snow_vertical_compose_sse2_sll("1","xmm0","xmm2","xmm4","xmm6")\
501         snow_vertical_compose_sse2_r2r_add("xmm1","xmm3","xmm5","xmm7","xmm0","xmm2","xmm4","xmm6")
502
503         "pcmpeqd %%xmm1, %%xmm1                      \n\t"
504         "pslld $31, %%xmm1                           \n\t"
505         "psrld $29, %%xmm1                           \n\t"
506         "mov %5, %%"REG_a"                           \n\t"
507
508         snow_vertical_compose_sse2_r2r_add("xmm1","xmm1","xmm1","xmm1","xmm0","xmm2","xmm4","xmm6")
509         snow_vertical_compose_sse2_sra("3","xmm0","xmm2","xmm4","xmm6")
510         snow_vertical_compose_sse2_load(REG_a,"xmm1","xmm3","xmm5","xmm7")
511         snow_vertical_compose_sse2_sub("xmm0","xmm2","xmm4","xmm6","xmm1","xmm3","xmm5","xmm7")
512         snow_vertical_compose_sse2_store(REG_a,"xmm1","xmm3","xmm5","xmm7")
513         "mov %3, %%"REG_c"                           \n\t"
514         snow_vertical_compose_sse2_load(REG_b,"xmm0","xmm2","xmm4","xmm6")
515         snow_vertical_compose_sse2_add(REG_c,"xmm1","xmm3","xmm5","xmm7")
516         snow_vertical_compose_sse2_sub("xmm1","xmm3","xmm5","xmm7","xmm0","xmm2","xmm4","xmm6")
517         snow_vertical_compose_sse2_store(REG_b,"xmm0","xmm2","xmm4","xmm6")
518         "mov %2, %%"REG_a"                           \n\t"
519         snow_vertical_compose_sse2_load(REG_c,"xmm1","xmm3","xmm5","xmm7")
520         snow_vertical_compose_sse2_add(REG_a,"xmm0","xmm2","xmm4","xmm6")
521         snow_vertical_compose_sse2_sll("2","xmm1","xmm3","xmm5","xmm7")\
522         snow_vertical_compose_sse2_r2r_add("xmm1","xmm3","xmm5","xmm7","xmm0","xmm2","xmm4","xmm6")
523
524         "pcmpeqd %%xmm1, %%xmm1                      \n\t"
525         "pslld $31, %%xmm1                           \n\t"
526         "psrld $28, %%xmm1                           \n\t"
527         "mov %1, %%"REG_b"                           \n\t"
528
529         snow_vertical_compose_sse2_r2r_add("xmm1","xmm1","xmm1","xmm1","xmm0","xmm2","xmm4","xmm6")
530         snow_vertical_compose_sse2_sra("4","xmm0","xmm2","xmm4","xmm6")
531         snow_vertical_compose_sse2_add(REG_c,"xmm0","xmm2","xmm4","xmm6")
532         snow_vertical_compose_sse2_store(REG_c,"xmm0","xmm2","xmm4","xmm6")
533         snow_vertical_compose_sse2_add(REG_b,"xmm0","xmm2","xmm4","xmm6")
534         snow_vertical_compose_sse2_move("xmm0","xmm2","xmm4","xmm6","xmm1","xmm3","xmm5","xmm7")
535         snow_vertical_compose_sse2_sll("1","xmm0","xmm2","xmm4","xmm6")\
536         snow_vertical_compose_sse2_r2r_add("xmm1","xmm3","xmm5","xmm7","xmm0","xmm2","xmm4","xmm6")
537         snow_vertical_compose_sse2_sra("1","xmm0","xmm2","xmm4","xmm6")
538         snow_vertical_compose_sse2_add(REG_a,"xmm0","xmm2","xmm4","xmm6")
539         snow_vertical_compose_sse2_store(REG_a,"xmm0","xmm2","xmm4","xmm6")
540
541         "2:                                          \n\t"
542         "sub $16, %%"REG_d"                          \n\t"
543         "jge 1b                                      \n\t"
544         :"+d"(i)
545         :
546         "m"(b0),"m"(b1),"m"(b2),"m"(b3),"m"(b4),"m"(b5):
547         "%"REG_a"","%"REG_b"","%"REG_c"");
548 }
549
550 #define snow_vertical_compose_mmx_load_add(op,r,t0,t1,t2,t3)\
551         ""op" (%%"r",%%"REG_d",4), %%"t0"   \n\t"\
552         ""op" 8(%%"r",%%"REG_d",4), %%"t1"  \n\t"\
553         ""op" 16(%%"r",%%"REG_d",4), %%"t2" \n\t"\
554         ""op" 24(%%"r",%%"REG_d",4), %%"t3" \n\t"
555
556 #define snow_vertical_compose_mmx_load(r,t0,t1,t2,t3)\
557         snow_vertical_compose_mmx_load_add("movq",r,t0,t1,t2,t3)
558
559 #define snow_vertical_compose_mmx_add(r,t0,t1,t2,t3)\
560         snow_vertical_compose_mmx_load_add("paddd",r,t0,t1,t2,t3)
561
562 #define snow_vertical_compose_mmx_sub(s0,s1,s2,s3,t0,t1,t2,t3)\
563         snow_vertical_compose_sse2_sub(s0,s1,s2,s3,t0,t1,t2,t3)
564
565 #define snow_vertical_compose_mmx_store(w,s0,s1,s2,s3)\
566         "movq %%"s0", (%%"w",%%"REG_d",4)   \n\t"\
567         "movq %%"s1", 8(%%"w",%%"REG_d",4)  \n\t"\
568         "movq %%"s2", 16(%%"w",%%"REG_d",4) \n\t"\
569         "movq %%"s3", 24(%%"w",%%"REG_d",4) \n\t"
570
571 #define snow_vertical_compose_mmx_sra(n,t0,t1,t2,t3)\
572         snow_vertical_compose_sse2_sra(n,t0,t1,t2,t3)
573
574 #define snow_vertical_compose_mmx_r2r_add(s0,s1,s2,s3,t0,t1,t2,t3)\
575         snow_vertical_compose_sse2_r2r_add(s0,s1,s2,s3,t0,t1,t2,t3)
576
577 #define snow_vertical_compose_mmx_sll(n,t0,t1,t2,t3)\
578         snow_vertical_compose_sse2_sll(n,t0,t1,t2,t3)
579
580 #define snow_vertical_compose_mmx_move(s0,s1,s2,s3,t0,t1,t2,t3)\
581         "movq %%"s0", %%"t0" \n\t"\
582         "movq %%"s1", %%"t1" \n\t"\
583         "movq %%"s2", %%"t2" \n\t"\
584         "movq %%"s3", %%"t3" \n\t"
585
586 void ff_snow_vertical_compose97i_mmx(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width){
587     long i = width;
588     while(i & 0x7)
589     {
590         i--;
591         b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
592         b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
593         b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
594         b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
595     }
596
597     asm volatile(
598         "jmp 2f                                      \n\t"
599         "1:                                          \n\t"
600
601         "mov %6, %%"REG_a"                           \n\t"
602         "mov %4, %%"REG_b"                           \n\t"
603
604         snow_vertical_compose_mmx_load(REG_b,"mm0","mm2","mm4","mm6")
605         snow_vertical_compose_mmx_add(REG_a,"mm0","mm2","mm4","mm6")
606         snow_vertical_compose_mmx_move("mm0","mm2","mm4","mm6","mm1","mm3","mm5","mm7")
607         snow_vertical_compose_mmx_sll("1","mm0","mm2","mm4","mm6")
608         snow_vertical_compose_mmx_r2r_add("mm1","mm3","mm5","mm7","mm0","mm2","mm4","mm6")
609
610         "pcmpeqd %%mm1, %%mm1                        \n\t"
611         "pslld $31, %%mm1                            \n\t"
612         "psrld $29, %%mm1                            \n\t"
613         "mov %5, %%"REG_a"                           \n\t"
614
615         snow_vertical_compose_mmx_r2r_add("mm1","mm1","mm1","mm1","mm0","mm2","mm4","mm6")
616         snow_vertical_compose_mmx_sra("3","mm0","mm2","mm4","mm6")
617         snow_vertical_compose_mmx_load(REG_a,"mm1","mm3","mm5","mm7")
618         snow_vertical_compose_mmx_sub("mm0","mm2","mm4","mm6","mm1","mm3","mm5","mm7")
619         snow_vertical_compose_mmx_store(REG_a,"mm1","mm3","mm5","mm7")
620         "mov %3, %%"REG_c"                           \n\t"
621         snow_vertical_compose_mmx_load(REG_b,"mm0","mm2","mm4","mm6")
622         snow_vertical_compose_mmx_add(REG_c,"mm1","mm3","mm5","mm7")
623         snow_vertical_compose_mmx_sub("mm1","mm3","mm5","mm7","mm0","mm2","mm4","mm6")
624         snow_vertical_compose_mmx_store(REG_b,"mm0","mm2","mm4","mm6")
625         "mov %2, %%"REG_a"                           \n\t"
626         snow_vertical_compose_mmx_load(REG_c,"mm1","mm3","mm5","mm7")
627         snow_vertical_compose_mmx_add(REG_a,"mm0","mm2","mm4","mm6")
628         snow_vertical_compose_mmx_sll("2","mm1","mm3","mm5","mm7")
629         snow_vertical_compose_mmx_r2r_add("mm1","mm3","mm5","mm7","mm0","mm2","mm4","mm6")
630
631         "pcmpeqd %%mm1, %%mm1                        \n\t"
632         "pslld $31, %%mm1                            \n\t"
633         "psrld $28, %%mm1                            \n\t"
634         "mov %1, %%"REG_b"                           \n\t"
635
636         snow_vertical_compose_mmx_r2r_add("mm1","mm1","mm1","mm1","mm0","mm2","mm4","mm6")
637         snow_vertical_compose_mmx_sra("4","mm0","mm2","mm4","mm6")
638         snow_vertical_compose_mmx_add(REG_c,"mm0","mm2","mm4","mm6")
639         snow_vertical_compose_mmx_store(REG_c,"mm0","mm2","mm4","mm6")
640         snow_vertical_compose_mmx_add(REG_b,"mm0","mm2","mm4","mm6")
641         snow_vertical_compose_mmx_move("mm0","mm2","mm4","mm6","mm1","mm3","mm5","mm7")
642         snow_vertical_compose_mmx_sll("1","mm0","mm2","mm4","mm6")
643         snow_vertical_compose_mmx_r2r_add("mm1","mm3","mm5","mm7","mm0","mm2","mm4","mm6")
644         snow_vertical_compose_mmx_sra("1","mm0","mm2","mm4","mm6")
645         snow_vertical_compose_mmx_add(REG_a,"mm0","mm2","mm4","mm6")
646         snow_vertical_compose_mmx_store(REG_a,"mm0","mm2","mm4","mm6")
647
648         "2:                                          \n\t"
649         "sub $8, %%"REG_d"                           \n\t"
650         "jge 1b                                      \n\t"
651         :"+d"(i)
652         :
653         "m"(b0),"m"(b1),"m"(b2),"m"(b3),"m"(b4),"m"(b5):
654         "%"REG_a"","%"REG_b"","%"REG_c"");
655 }