9f5f067db5f7949dafaf41683fb8a3428200353b
[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 #define snow_vertical_compose_sse2_load_add(op,r,t0,t1,t2,t3)\
25         ""op" (%%"r",%%"REG_d",4), %%"t0"      \n\t"\
26         ""op" 16(%%"r",%%"REG_d",4), %%"t1"    \n\t"\
27         ""op" 32(%%"r",%%"REG_d",4), %%"t2"    \n\t"\
28         ""op" 48(%%"r",%%"REG_d",4), %%"t3"    \n\t"
29
30 #define snow_vertical_compose_sse2_load(r,t0,t1,t2,t3)\
31         snow_vertical_compose_sse2_load_add("movdqa",r,t0,t1,t2,t3)
32
33 #define snow_vertical_compose_sse2_add(r,t0,t1,t2,t3)\
34         snow_vertical_compose_sse2_load_add("paddd",r,t0,t1,t2,t3)
35
36 #define snow_vertical_compose_sse2_sub(s0,s1,s2,s3,t0,t1,t2,t3)\
37         "psubd %%"s0", %%"t0" \n\t"\
38         "psubd %%"s1", %%"t1" \n\t"\
39         "psubd %%"s2", %%"t2" \n\t"\
40         "psubd %%"s3", %%"t3" \n\t"
41
42 #define snow_vertical_compose_sse2_store(w,s0,s1,s2,s3)\
43         "movdqa %%"s0", (%%"w",%%"REG_d",4)      \n\t"\
44         "movdqa %%"s1", 16(%%"w",%%"REG_d",4)    \n\t"\
45         "movdqa %%"s2", 32(%%"w",%%"REG_d",4)    \n\t"\
46         "movdqa %%"s3", 48(%%"w",%%"REG_d",4)    \n\t"
47
48 #define snow_vertical_compose_sse2_sra(n,t0,t1,t2,t3)\
49         "psrad $"n", %%"t0" \n\t"\
50         "psrad $"n", %%"t1" \n\t"\
51         "psrad $"n", %%"t2" \n\t"\
52         "psrad $"n", %%"t3" \n\t"
53
54 #define snow_vertical_compose_sse2_r2r_add(s0,s1,s2,s3,t0,t1,t2,t3)\
55         "paddd %%"s0", %%"t0" \n\t"\
56         "paddd %%"s1", %%"t1" \n\t"\
57         "paddd %%"s2", %%"t2" \n\t"\
58         "paddd %%"s3", %%"t3" \n\t"
59
60 #define snow_vertical_compose_sse2_sll(n,t0,t1,t2,t3)\
61         "pslld $"n", %%"t0" \n\t"\
62         "pslld $"n", %%"t1" \n\t"\
63         "pslld $"n", %%"t2" \n\t"\
64         "pslld $"n", %%"t3" \n\t"
65
66 #define snow_vertical_compose_sse2_move(s0,s1,s2,s3,t0,t1,t2,t3)\
67         "movdqa %%"s0", %%"t0" \n\t"\
68         "movdqa %%"s1", %%"t1" \n\t"\
69         "movdqa %%"s2", %%"t2" \n\t"\
70         "movdqa %%"s3", %%"t3" \n\t"
71
72 void ff_snow_vertical_compose97i_sse2(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width){
73     long i = width;
74
75     while(i & 0xF)
76     {
77         i--;
78         b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
79         b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
80         b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
81         b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
82     }
83
84          asm volatile (
85         "jmp 2f                                      \n\t"
86         "1:                                          \n\t"
87
88         "mov %6, %%"REG_a"                           \n\t"
89         "mov %4, %%"REG_b"                           \n\t"
90
91         snow_vertical_compose_sse2_load(REG_b,"xmm0","xmm2","xmm4","xmm6")
92         snow_vertical_compose_sse2_add(REG_a,"xmm0","xmm2","xmm4","xmm6")
93         snow_vertical_compose_sse2_move("xmm0","xmm2","xmm4","xmm6","xmm1","xmm3","xmm5","xmm7")
94         snow_vertical_compose_sse2_sll("1","xmm0","xmm2","xmm4","xmm6")\
95         snow_vertical_compose_sse2_r2r_add("xmm1","xmm3","xmm5","xmm7","xmm0","xmm2","xmm4","xmm6")
96
97         "pcmpeqd %%xmm1, %%xmm1                      \n\t"
98         "pslld $31, %%xmm1                           \n\t"
99         "psrld $29, %%xmm1                           \n\t"
100         "mov %5, %%"REG_a"                           \n\t"
101
102         snow_vertical_compose_sse2_r2r_add("xmm1","xmm1","xmm1","xmm1","xmm0","xmm2","xmm4","xmm6")
103         snow_vertical_compose_sse2_sra("3","xmm0","xmm2","xmm4","xmm6")
104         snow_vertical_compose_sse2_load(REG_a,"xmm1","xmm3","xmm5","xmm7")
105         snow_vertical_compose_sse2_sub("xmm0","xmm2","xmm4","xmm6","xmm1","xmm3","xmm5","xmm7")
106         snow_vertical_compose_sse2_store(REG_a,"xmm1","xmm3","xmm5","xmm7")
107         "mov %3, %%"REG_c"                           \n\t"
108         snow_vertical_compose_sse2_load(REG_b,"xmm0","xmm2","xmm4","xmm6")
109         snow_vertical_compose_sse2_add(REG_c,"xmm1","xmm3","xmm5","xmm7")
110         snow_vertical_compose_sse2_sub("xmm1","xmm3","xmm5","xmm7","xmm0","xmm2","xmm4","xmm6")
111         snow_vertical_compose_sse2_store(REG_b,"xmm0","xmm2","xmm4","xmm6")
112         "mov %2, %%"REG_a"                           \n\t"
113         snow_vertical_compose_sse2_load(REG_c,"xmm1","xmm3","xmm5","xmm7")
114         snow_vertical_compose_sse2_add(REG_a,"xmm0","xmm2","xmm4","xmm6")
115         snow_vertical_compose_sse2_sll("2","xmm1","xmm3","xmm5","xmm7")\
116         snow_vertical_compose_sse2_r2r_add("xmm1","xmm3","xmm5","xmm7","xmm0","xmm2","xmm4","xmm6")
117
118         "pcmpeqd %%xmm1, %%xmm1                      \n\t"
119         "pslld $31, %%xmm1                           \n\t"
120         "psrld $28, %%xmm1                           \n\t"
121         "mov %1, %%"REG_b"                           \n\t"
122
123         snow_vertical_compose_sse2_r2r_add("xmm1","xmm1","xmm1","xmm1","xmm0","xmm2","xmm4","xmm6")
124         snow_vertical_compose_sse2_sra("4","xmm0","xmm2","xmm4","xmm6")
125         snow_vertical_compose_sse2_add(REG_c,"xmm0","xmm2","xmm4","xmm6")
126         snow_vertical_compose_sse2_store(REG_c,"xmm0","xmm2","xmm4","xmm6")
127         snow_vertical_compose_sse2_add(REG_b,"xmm0","xmm2","xmm4","xmm6")
128         snow_vertical_compose_sse2_move("xmm0","xmm2","xmm4","xmm6","xmm1","xmm3","xmm5","xmm7")
129         snow_vertical_compose_sse2_sll("1","xmm0","xmm2","xmm4","xmm6")\
130         snow_vertical_compose_sse2_r2r_add("xmm1","xmm3","xmm5","xmm7","xmm0","xmm2","xmm4","xmm6")
131         snow_vertical_compose_sse2_sra("1","xmm0","xmm2","xmm4","xmm6")
132         snow_vertical_compose_sse2_add(REG_a,"xmm0","xmm2","xmm4","xmm6")
133         snow_vertical_compose_sse2_store(REG_a,"xmm0","xmm2","xmm4","xmm6")
134
135         "2:                                          \n\t"
136         "sub $16, %%"REG_d"                          \n\t"
137         "jge 1b                                      \n\t"
138         :"+d"(i)
139         :
140         "m"(b0),"m"(b1),"m"(b2),"m"(b3),"m"(b4),"m"(b5):
141         "%"REG_a"","%"REG_b"","%"REG_c"");
142 }
143
144 #define snow_vertical_compose_mmx_load_add(op,r,t0,t1,t2,t3)\
145         ""op" (%%"r",%%"REG_d",4), %%"t0"   \n\t"\
146         ""op" 8(%%"r",%%"REG_d",4), %%"t1"  \n\t"\
147         ""op" 16(%%"r",%%"REG_d",4), %%"t2" \n\t"\
148         ""op" 24(%%"r",%%"REG_d",4), %%"t3" \n\t"
149
150 #define snow_vertical_compose_mmx_load(r,t0,t1,t2,t3)\
151         snow_vertical_compose_mmx_load_add("movq",r,t0,t1,t2,t3)
152
153 #define snow_vertical_compose_mmx_add(r,t0,t1,t2,t3)\
154         snow_vertical_compose_mmx_load_add("paddd",r,t0,t1,t2,t3)
155
156 #define snow_vertical_compose_mmx_sub(s0,s1,s2,s3,t0,t1,t2,t3)\
157         snow_vertical_compose_sse2_sub(s0,s1,s2,s3,t0,t1,t2,t3)
158
159 #define snow_vertical_compose_mmx_store(w,s0,s1,s2,s3)\
160         "movq %%"s0", (%%"w",%%"REG_d",4)   \n\t"\
161         "movq %%"s1", 8(%%"w",%%"REG_d",4)  \n\t"\
162         "movq %%"s2", 16(%%"w",%%"REG_d",4) \n\t"\
163         "movq %%"s3", 24(%%"w",%%"REG_d",4) \n\t"
164
165 #define snow_vertical_compose_mmx_sra(n,t0,t1,t2,t3)\
166         snow_vertical_compose_sse2_sra(n,t0,t1,t2,t3)
167
168 #define snow_vertical_compose_mmx_r2r_add(s0,s1,s2,s3,t0,t1,t2,t3)\
169         snow_vertical_compose_sse2_r2r_add(s0,s1,s2,s3,t0,t1,t2,t3)
170
171 #define snow_vertical_compose_mmx_sll(n,t0,t1,t2,t3)\
172         snow_vertical_compose_sse2_sll(n,t0,t1,t2,t3)
173
174 #define snow_vertical_compose_mmx_move(s0,s1,s2,s3,t0,t1,t2,t3)\
175         "movq %%"s0", %%"t0" \n\t"\
176         "movq %%"s1", %%"t1" \n\t"\
177         "movq %%"s2", %%"t2" \n\t"\
178         "movq %%"s3", %%"t3" \n\t"
179
180 void ff_snow_vertical_compose97i_mmx(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width){
181     long i = width;
182     while(i & 0x7)
183     {
184         i--;
185         b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
186         b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
187         b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
188         b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
189     }
190
191     asm volatile(
192         "jmp 2f                                      \n\t"
193         "1:                                          \n\t"
194
195         "mov %6, %%"REG_a"                           \n\t"
196         "mov %4, %%"REG_b"                           \n\t"
197
198         snow_vertical_compose_mmx_load(REG_b,"mm0","mm2","mm4","mm6")
199         snow_vertical_compose_mmx_add(REG_a,"mm0","mm2","mm4","mm6")
200         snow_vertical_compose_mmx_move("mm0","mm2","mm4","mm6","mm1","mm3","mm5","mm7")
201         snow_vertical_compose_mmx_sll("1","mm0","mm2","mm4","mm6")
202         snow_vertical_compose_mmx_r2r_add("mm1","mm3","mm5","mm7","mm0","mm2","mm4","mm6")
203
204         "pcmpeqd %%mm1, %%mm1                        \n\t"
205         "pslld $31, %%mm1                            \n\t"
206         "psrld $29, %%mm1                            \n\t"
207         "mov %5, %%"REG_a"                           \n\t"
208
209         snow_vertical_compose_mmx_r2r_add("mm1","mm1","mm1","mm1","mm0","mm2","mm4","mm6")
210         snow_vertical_compose_mmx_sra("3","mm0","mm2","mm4","mm6")
211         snow_vertical_compose_mmx_load(REG_a,"mm1","mm3","mm5","mm7")
212         snow_vertical_compose_mmx_sub("mm0","mm2","mm4","mm6","mm1","mm3","mm5","mm7")
213         snow_vertical_compose_mmx_store(REG_a,"mm1","mm3","mm5","mm7")
214         "mov %3, %%"REG_c"                           \n\t"
215         snow_vertical_compose_mmx_load(REG_b,"mm0","mm2","mm4","mm6")
216         snow_vertical_compose_mmx_add(REG_c,"mm1","mm3","mm5","mm7")
217         snow_vertical_compose_mmx_sub("mm1","mm3","mm5","mm7","mm0","mm2","mm4","mm6")
218         snow_vertical_compose_mmx_store(REG_b,"mm0","mm2","mm4","mm6")
219         "mov %2, %%"REG_a"                           \n\t"
220         snow_vertical_compose_mmx_load(REG_c,"mm1","mm3","mm5","mm7")
221         snow_vertical_compose_mmx_add(REG_a,"mm0","mm2","mm4","mm6")
222         snow_vertical_compose_mmx_sll("2","mm1","mm3","mm5","mm7")
223         snow_vertical_compose_mmx_r2r_add("mm1","mm3","mm5","mm7","mm0","mm2","mm4","mm6")
224
225         "pcmpeqd %%mm1, %%mm1                        \n\t"
226         "pslld $31, %%mm1                            \n\t"
227         "psrld $28, %%mm1                            \n\t"
228         "mov %1, %%"REG_b"                           \n\t"
229
230         snow_vertical_compose_mmx_r2r_add("mm1","mm1","mm1","mm1","mm0","mm2","mm4","mm6")
231         snow_vertical_compose_mmx_sra("4","mm0","mm2","mm4","mm6")
232         snow_vertical_compose_mmx_add(REG_c,"mm0","mm2","mm4","mm6")
233         snow_vertical_compose_mmx_store(REG_c,"mm0","mm2","mm4","mm6")
234         snow_vertical_compose_mmx_add(REG_b,"mm0","mm2","mm4","mm6")
235         snow_vertical_compose_mmx_move("mm0","mm2","mm4","mm6","mm1","mm3","mm5","mm7")
236         snow_vertical_compose_mmx_sll("1","mm0","mm2","mm4","mm6")
237         snow_vertical_compose_mmx_r2r_add("mm1","mm3","mm5","mm7","mm0","mm2","mm4","mm6")
238         snow_vertical_compose_mmx_sra("1","mm0","mm2","mm4","mm6")
239         snow_vertical_compose_mmx_add(REG_a,"mm0","mm2","mm4","mm6")
240         snow_vertical_compose_mmx_store(REG_a,"mm0","mm2","mm4","mm6")
241
242         "2:                                          \n\t"
243         "sub $8, %%"REG_d"                           \n\t"
244         "jge 1b                                      \n\t"
245         :"+d"(i)
246         :
247         "m"(b0),"m"(b1),"m"(b2),"m"(b3),"m"(b4),"m"(b5):
248         "%"REG_a"","%"REG_b"","%"REG_c"");
249 }