FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dsputil_rnd_template.c
Go to the documentation of this file.
1 /*
2  * DSP utils mmx functions are compiled twice for rnd/no_rnd
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2003-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
7  * mostly rewritten by Michael Niedermayer <michaelni@gmx.at>
8  * and improved by Zdenek Kabelac <kabi@users.sf.net>
9  *
10  * This file is part of FFmpeg.
11  *
12  * FFmpeg is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * FFmpeg is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with FFmpeg; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 
27 // put_pixels
28 static void DEF(put, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
29 {
30  MOVQ_ZERO(mm7);
31  SET_RND(mm6); // =2 for rnd and =1 for no_rnd version
32  __asm__ volatile(
33  "movq (%1), %%mm0 \n\t"
34  "movq 1(%1), %%mm4 \n\t"
35  "movq %%mm0, %%mm1 \n\t"
36  "movq %%mm4, %%mm5 \n\t"
37  "punpcklbw %%mm7, %%mm0 \n\t"
38  "punpcklbw %%mm7, %%mm4 \n\t"
39  "punpckhbw %%mm7, %%mm1 \n\t"
40  "punpckhbw %%mm7, %%mm5 \n\t"
41  "paddusw %%mm0, %%mm4 \n\t"
42  "paddusw %%mm1, %%mm5 \n\t"
43  "xor %%"REG_a", %%"REG_a" \n\t"
44  "add %3, %1 \n\t"
45  ".p2align 3 \n\t"
46  "1: \n\t"
47  "movq (%1, %%"REG_a"), %%mm0 \n\t"
48  "movq 1(%1, %%"REG_a"), %%mm2 \n\t"
49  "movq %%mm0, %%mm1 \n\t"
50  "movq %%mm2, %%mm3 \n\t"
51  "punpcklbw %%mm7, %%mm0 \n\t"
52  "punpcklbw %%mm7, %%mm2 \n\t"
53  "punpckhbw %%mm7, %%mm1 \n\t"
54  "punpckhbw %%mm7, %%mm3 \n\t"
55  "paddusw %%mm2, %%mm0 \n\t"
56  "paddusw %%mm3, %%mm1 \n\t"
57  "paddusw %%mm6, %%mm4 \n\t"
58  "paddusw %%mm6, %%mm5 \n\t"
59  "paddusw %%mm0, %%mm4 \n\t"
60  "paddusw %%mm1, %%mm5 \n\t"
61  "psrlw $2, %%mm4 \n\t"
62  "psrlw $2, %%mm5 \n\t"
63  "packuswb %%mm5, %%mm4 \n\t"
64  "movq %%mm4, (%2, %%"REG_a") \n\t"
65  "add %3, %%"REG_a" \n\t"
66 
67  "movq (%1, %%"REG_a"), %%mm2 \n\t" // 0 <-> 2 1 <-> 3
68  "movq 1(%1, %%"REG_a"), %%mm4 \n\t"
69  "movq %%mm2, %%mm3 \n\t"
70  "movq %%mm4, %%mm5 \n\t"
71  "punpcklbw %%mm7, %%mm2 \n\t"
72  "punpcklbw %%mm7, %%mm4 \n\t"
73  "punpckhbw %%mm7, %%mm3 \n\t"
74  "punpckhbw %%mm7, %%mm5 \n\t"
75  "paddusw %%mm2, %%mm4 \n\t"
76  "paddusw %%mm3, %%mm5 \n\t"
77  "paddusw %%mm6, %%mm0 \n\t"
78  "paddusw %%mm6, %%mm1 \n\t"
79  "paddusw %%mm4, %%mm0 \n\t"
80  "paddusw %%mm5, %%mm1 \n\t"
81  "psrlw $2, %%mm0 \n\t"
82  "psrlw $2, %%mm1 \n\t"
83  "packuswb %%mm1, %%mm0 \n\t"
84  "movq %%mm0, (%2, %%"REG_a") \n\t"
85  "add %3, %%"REG_a" \n\t"
86 
87  "subl $2, %0 \n\t"
88  "jnz 1b \n\t"
89  :"+g"(h), "+S"(pixels)
90  :"D"(block), "r"((x86_reg)line_size)
91  :REG_a, "memory");
92 }
93 
94 // avg_pixels
95 #ifndef NO_RND
96 // in case more speed is needed - unroling would certainly help
97 static void DEF(avg, pixels8)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
98 {
99  MOVQ_BFE(mm6);
100  JUMPALIGN();
101  do {
102  __asm__ volatile(
103  "movq %0, %%mm0 \n\t"
104  "movq %1, %%mm1 \n\t"
105  OP_AVG(%%mm0, %%mm1, %%mm2, %%mm6)
106  "movq %%mm2, %0 \n\t"
107  :"+m"(*block)
108  :"m"(*pixels)
109  :"memory");
110  pixels += line_size;
111  block += line_size;
112  }
113  while (--h);
114 }
115 #endif /* NO_RND */
116 
117 static void DEF(avg, pixels16)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
118 {
119  MOVQ_BFE(mm6);
120  JUMPALIGN();
121  do {
122  __asm__ volatile(
123  "movq %0, %%mm0 \n\t"
124  "movq %1, %%mm1 \n\t"
125  OP_AVG(%%mm0, %%mm1, %%mm2, %%mm6)
126  "movq %%mm2, %0 \n\t"
127  "movq 8%0, %%mm0 \n\t"
128  "movq 8%1, %%mm1 \n\t"
129  OP_AVG(%%mm0, %%mm1, %%mm2, %%mm6)
130  "movq %%mm2, 8%0 \n\t"
131  :"+m"(*block)
132  :"m"(*pixels)
133  :"memory");
134  pixels += line_size;
135  block += line_size;
136  }
137  while (--h);
138 }
139 
140 // this routine is 'slightly' suboptimal but mostly unused
141 static void DEF(avg, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
142 {
143  MOVQ_ZERO(mm7);
144  SET_RND(mm6); // =2 for rnd and =1 for no_rnd version
145  __asm__ volatile(
146  "movq (%1), %%mm0 \n\t"
147  "movq 1(%1), %%mm4 \n\t"
148  "movq %%mm0, %%mm1 \n\t"
149  "movq %%mm4, %%mm5 \n\t"
150  "punpcklbw %%mm7, %%mm0 \n\t"
151  "punpcklbw %%mm7, %%mm4 \n\t"
152  "punpckhbw %%mm7, %%mm1 \n\t"
153  "punpckhbw %%mm7, %%mm5 \n\t"
154  "paddusw %%mm0, %%mm4 \n\t"
155  "paddusw %%mm1, %%mm5 \n\t"
156  "xor %%"REG_a", %%"REG_a" \n\t"
157  "add %3, %1 \n\t"
158  ".p2align 3 \n\t"
159  "1: \n\t"
160  "movq (%1, %%"REG_a"), %%mm0 \n\t"
161  "movq 1(%1, %%"REG_a"), %%mm2 \n\t"
162  "movq %%mm0, %%mm1 \n\t"
163  "movq %%mm2, %%mm3 \n\t"
164  "punpcklbw %%mm7, %%mm0 \n\t"
165  "punpcklbw %%mm7, %%mm2 \n\t"
166  "punpckhbw %%mm7, %%mm1 \n\t"
167  "punpckhbw %%mm7, %%mm3 \n\t"
168  "paddusw %%mm2, %%mm0 \n\t"
169  "paddusw %%mm3, %%mm1 \n\t"
170  "paddusw %%mm6, %%mm4 \n\t"
171  "paddusw %%mm6, %%mm5 \n\t"
172  "paddusw %%mm0, %%mm4 \n\t"
173  "paddusw %%mm1, %%mm5 \n\t"
174  "psrlw $2, %%mm4 \n\t"
175  "psrlw $2, %%mm5 \n\t"
176  "movq (%2, %%"REG_a"), %%mm3 \n\t"
177  "packuswb %%mm5, %%mm4 \n\t"
178  "pcmpeqd %%mm2, %%mm2 \n\t"
179  "paddb %%mm2, %%mm2 \n\t"
180  OP_AVG(%%mm3, %%mm4, %%mm5, %%mm2)
181  "movq %%mm5, (%2, %%"REG_a") \n\t"
182  "add %3, %%"REG_a" \n\t"
183 
184  "movq (%1, %%"REG_a"), %%mm2 \n\t" // 0 <-> 2 1 <-> 3
185  "movq 1(%1, %%"REG_a"), %%mm4 \n\t"
186  "movq %%mm2, %%mm3 \n\t"
187  "movq %%mm4, %%mm5 \n\t"
188  "punpcklbw %%mm7, %%mm2 \n\t"
189  "punpcklbw %%mm7, %%mm4 \n\t"
190  "punpckhbw %%mm7, %%mm3 \n\t"
191  "punpckhbw %%mm7, %%mm5 \n\t"
192  "paddusw %%mm2, %%mm4 \n\t"
193  "paddusw %%mm3, %%mm5 \n\t"
194  "paddusw %%mm6, %%mm0 \n\t"
195  "paddusw %%mm6, %%mm1 \n\t"
196  "paddusw %%mm4, %%mm0 \n\t"
197  "paddusw %%mm5, %%mm1 \n\t"
198  "psrlw $2, %%mm0 \n\t"
199  "psrlw $2, %%mm1 \n\t"
200  "movq (%2, %%"REG_a"), %%mm3 \n\t"
201  "packuswb %%mm1, %%mm0 \n\t"
202  "pcmpeqd %%mm2, %%mm2 \n\t"
203  "paddb %%mm2, %%mm2 \n\t"
204  OP_AVG(%%mm3, %%mm0, %%mm1, %%mm2)
205  "movq %%mm1, (%2, %%"REG_a") \n\t"
206  "add %3, %%"REG_a" \n\t"
207 
208  "subl $2, %0 \n\t"
209  "jnz 1b \n\t"
210  :"+g"(h), "+S"(pixels)
211  :"D"(block), "r"((x86_reg)line_size)
212  :REG_a, "memory");
213 }
214 
215 //FIXME optimize
216 static void DEF(put, pixels16_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){
217  DEF(put, pixels8_xy2)(block , pixels , line_size, h);
218  DEF(put, pixels8_xy2)(block+8, pixels+8, line_size, h);
219 }
220 
221 static void DEF(avg, pixels16_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){
222  DEF(avg, pixels8_xy2)(block , pixels , line_size, h);
223  DEF(avg, pixels8_xy2)(block+8, pixels+8, line_size, h);
224 }