2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../SDL_internal.h"
23 #if (__GNUC__ > 2) && defined(__i386__) && __OPTIMIZE__ && SDL_ASSEMBLY_ROUTINES
25 #include "SDL_stdinc.h"
31 static mmx_t MMX_0080w = { .ud = {0x00800080, 0x00800080} };
32 static mmx_t MMX_00FFw = { .ud = {0x00ff00ff, 0x00ff00ff} };
33 static mmx_t MMX_FF00w = { .ud = {0xff00ff00, 0xff00ff00} };
35 static mmx_t MMX_Ycoeff = { .uw = {0x004a, 0x004a, 0x004a, 0x004a} };
37 static mmx_t MMX_UbluRGB = { .uw = {0x0072, 0x0072, 0x0072, 0x0072} };
38 static mmx_t MMX_VredRGB = { .uw = {0x0059, 0x0059, 0x0059, 0x0059} };
39 static mmx_t MMX_UgrnRGB = { .uw = {0xffea, 0xffea, 0xffea, 0xffea} };
40 static mmx_t MMX_VgrnRGB = { .uw = {0xffd2, 0xffd2, 0xffd2, 0xffd2} };
42 static mmx_t MMX_Ublu5x5 = { .uw = {0x0081, 0x0081, 0x0081, 0x0081} };
43 static mmx_t MMX_Vred5x5 = { .uw = {0x0066, 0x0066, 0x0066, 0x0066} };
44 static mmx_t MMX_Ugrn565 = { .uw = {0xffe8, 0xffe8, 0xffe8, 0xffe8} };
45 static mmx_t MMX_Vgrn565 = { .uw = {0xffcd, 0xffcd, 0xffcd, 0xffcd} };
47 static mmx_t MMX_red565 = { .uw = {0xf800, 0xf800, 0xf800, 0xf800} };
48 static mmx_t MMX_grn565 = { .uw = {0x07e0, 0x07e0, 0x07e0, 0x07e0} };
51 This MMX assembler is my first assembler/MMX program ever.
56 After it worked fine I have "obfuscated" the code a bit to have
57 more parallism in the MMX units. This means I moved
58 initilisation around and delayed other instruction.
59 Performance measurement did not show that this brought any advantage
60 but in theory it _should_ be faster this way.
62 The overall performanve gain to the C based dither was 30%-40%.
63 The MMX routine calculates 256bit=8RGB values in each cycle
64 (4 for row1 & 4 for row2)
66 The red/green/blue.. coefficents are taken from the mpeg_play
67 player. They look nice, but I dont know if you can have
68 better values, to avoid integer rounding errors.
74 It is a requirement that the cr/cb/lum are 8 byte aligned and
75 the out are 16byte aligned or you will/may get segfaults
79 void ColorRGBDitherYV12MMX1X( int *colortab, Uint32 *rgb_2_pix,
80 unsigned char *lum, unsigned char *cr,
81 unsigned char *cb, unsigned char *out,
82 int rows, int cols, int mod )
87 unsigned char* y = lum +cols*rows; /* Pointer to the end */
89 row1 = (Uint32 *)out; /* 32 bit target */
90 row2 = (Uint32 *)out+cols+mod; /* start of second row */
91 mod = (mod+cols+mod)*4; /* increment for row1 in byte */
93 __asm__ __volatile__ (
94 /* tap dance to workaround the inability to use %%ebx at will... */
95 /* move one thing to the stack... */
96 "pushl $0\n" /* save a slot on the stack. */
97 "pushl %%ebx\n" /* save %%ebx. */
98 "movl %0, %%ebx\n" /* put the thing in ebx. */
99 "movl %%ebx,4(%%esp)\n" /* put the thing in the stack slot. */
100 "popl %%ebx\n" /* get back %%ebx (the PIC register). */
105 /* create Cr (result in mm1) */
107 "movl 4(%%esp),%%ebx\n"
108 "movd (%%ebx),%%mm1\n" /* 0 0 0 0 v3 v2 v1 v0 */
110 "pxor %%mm7,%%mm7\n" /* 00 00 00 00 00 00 00 00 */
111 "movd (%2), %%mm2\n" /* 0 0 0 0 l3 l2 l1 l0 */
112 "punpcklbw %%mm7,%%mm1\n" /* 0 v3 0 v2 00 v1 00 v0 */
113 "punpckldq %%mm1,%%mm1\n" /* 00 v1 00 v0 00 v1 00 v0 */
114 "psubw %9,%%mm1\n" /* mm1-128:r1 r1 r0 r0 r1 r1 r0 r0 */
116 /* create Cr_g (result in mm0) */
117 "movq %%mm1,%%mm0\n" /* r1 r1 r0 r0 r1 r1 r0 r0 */
118 "pmullw %10,%%mm0\n" /* red*-46dec=0.7136*64 */
119 "pmullw %11,%%mm1\n" /* red*89dec=1.4013*64 */
120 "psraw $6, %%mm0\n" /* red=red/64 */
121 "psraw $6, %%mm1\n" /* red=red/64 */
123 /* create L1 L2 (result in mm2,mm4) */
125 "movq (%2,%4),%%mm3\n" /* 0 0 0 0 L3 L2 L1 L0 */
126 "punpckldq %%mm3,%%mm2\n" /* L3 L2 L1 L0 l3 l2 l1 l0 */
127 "movq %%mm2,%%mm4\n" /* L3 L2 L1 L0 l3 l2 l1 l0 */
128 "pand %12,%%mm2\n" /* L3 0 L1 0 l3 0 l1 0 */
129 "pand %13,%%mm4\n" /* 0 L2 0 L0 0 l2 0 l0 */
130 "psrlw $8,%%mm2\n" /* 0 L3 0 L1 0 l3 0 l1 */
132 /* create R (result in mm6) */
133 "movq %%mm2,%%mm5\n" /* 0 L3 0 L1 0 l3 0 l1 */
134 "movq %%mm4,%%mm6\n" /* 0 L2 0 L0 0 l2 0 l0 */
135 "paddsw %%mm1, %%mm5\n" /* lum1+red:x R3 x R1 x r3 x r1 */
136 "paddsw %%mm1, %%mm6\n" /* lum1+red:x R2 x R0 x r2 x r0 */
137 "packuswb %%mm5,%%mm5\n" /* R3 R1 r3 r1 R3 R1 r3 r1 */
138 "packuswb %%mm6,%%mm6\n" /* R2 R0 r2 r0 R2 R0 r2 r0 */
139 "pxor %%mm7,%%mm7\n" /* 00 00 00 00 00 00 00 00 */
140 "punpcklbw %%mm5,%%mm6\n" /* R3 R2 R1 R0 r3 r2 r1 r0 */
142 /* create Cb (result in mm1) */
143 "movd (%1), %%mm1\n" /* 0 0 0 0 u3 u2 u1 u0 */
144 "punpcklbw %%mm7,%%mm1\n" /* 0 u3 0 u2 00 u1 00 u0 */
145 "punpckldq %%mm1,%%mm1\n" /* 00 u1 00 u0 00 u1 00 u0 */
146 "psubw %9,%%mm1\n" /* mm1-128:u1 u1 u0 u0 u1 u1 u0 u0 */
148 /* create Cb_g (result in mm5) */
149 "movq %%mm1,%%mm5\n" /* u1 u1 u0 u0 u1 u1 u0 u0 */
150 "pmullw %14,%%mm5\n" /* blue*-109dec=1.7129*64 */
151 "pmullw %15,%%mm1\n" /* blue*114dec=1.78125*64 */
152 "psraw $6, %%mm5\n" /* blue=red/64 */
153 "psraw $6, %%mm1\n" /* blue=blue/64 */
155 /* create G (result in mm7) */
156 "movq %%mm2,%%mm3\n" /* 0 L3 0 L1 0 l3 0 l1 */
157 "movq %%mm4,%%mm7\n" /* 0 L2 0 L0 0 l2 0 l1 */
158 "paddsw %%mm5, %%mm3\n" /* lum1+Cb_g:x G3t x G1t x g3t x g1t */
159 "paddsw %%mm5, %%mm7\n" /* lum1+Cb_g:x G2t x G0t x g2t x g0t */
160 "paddsw %%mm0, %%mm3\n" /* lum1+Cr_g:x G3 x G1 x g3 x g1 */
161 "paddsw %%mm0, %%mm7\n" /* lum1+blue:x G2 x G0 x g2 x g0 */
162 "packuswb %%mm3,%%mm3\n" /* G3 G1 g3 g1 G3 G1 g3 g1 */
163 "packuswb %%mm7,%%mm7\n" /* G2 G0 g2 g0 G2 G0 g2 g0 */
164 "punpcklbw %%mm3,%%mm7\n" /* G3 G2 G1 G0 g3 g2 g1 g0 */
166 /* create B (result in mm5) */
167 "movq %%mm2,%%mm3\n" /* 0 L3 0 L1 0 l3 0 l1 */
168 "movq %%mm4,%%mm5\n" /* 0 L2 0 L0 0 l2 0 l1 */
169 "paddsw %%mm1, %%mm3\n" /* lum1+blue:x B3 x B1 x b3 x b1 */
170 "paddsw %%mm1, %%mm5\n" /* lum1+blue:x B2 x B0 x b2 x b0 */
171 "packuswb %%mm3,%%mm3\n" /* B3 B1 b3 b1 B3 B1 b3 b1 */
172 "packuswb %%mm5,%%mm5\n" /* B2 B0 b2 b0 B2 B0 b2 b0 */
173 "punpcklbw %%mm3,%%mm5\n" /* B3 B2 B1 B0 b3 b2 b1 b0 */
175 /* fill destination row1 (needed are mm6=Rr,mm7=Gg,mm5=Bb) */
177 "pxor %%mm2,%%mm2\n" /* 0 0 0 0 0 0 0 0 */
178 "pxor %%mm4,%%mm4\n" /* 0 0 0 0 0 0 0 0 */
179 "movq %%mm6,%%mm1\n" /* R3 R2 R1 R0 r3 r2 r1 r0 */
180 "movq %%mm5,%%mm3\n" /* B3 B2 B1 B0 b3 b2 b1 b0 */
182 /* process lower lum */
183 "punpcklbw %%mm4,%%mm1\n" /* 0 r3 0 r2 0 r1 0 r0 */
184 "punpcklbw %%mm4,%%mm3\n" /* 0 b3 0 b2 0 b1 0 b0 */
185 "movq %%mm1,%%mm2\n" /* 0 r3 0 r2 0 r1 0 r0 */
186 "movq %%mm3,%%mm0\n" /* 0 b3 0 b2 0 b1 0 b0 */
187 "punpcklwd %%mm1,%%mm3\n" /* 0 r1 0 b1 0 r0 0 b0 */
188 "punpckhwd %%mm2,%%mm0\n" /* 0 r3 0 b3 0 r2 0 b2 */
190 "pxor %%mm2,%%mm2\n" /* 0 0 0 0 0 0 0 0 */
191 "movq %%mm7,%%mm1\n" /* G3 G2 G1 G0 g3 g2 g1 g0 */
192 "punpcklbw %%mm1,%%mm2\n" /* g3 0 g2 0 g1 0 g0 0 */
193 "punpcklwd %%mm4,%%mm2\n" /* 0 0 g1 0 0 0 g0 0 */
194 "por %%mm3, %%mm2\n" /* 0 r1 g1 b1 0 r0 g0 b0 */
195 "movq %%mm2,(%3)\n" /* wrote out ! row1 */
197 "pxor %%mm2,%%mm2\n" /* 0 0 0 0 0 0 0 0 */
198 "punpcklbw %%mm1,%%mm4\n" /* g3 0 g2 0 g1 0 g0 0 */
199 "punpckhwd %%mm2,%%mm4\n" /* 0 0 g3 0 0 0 g2 0 */
200 "por %%mm0, %%mm4\n" /* 0 r3 g3 b3 0 r2 g2 b2 */
201 "movq %%mm4,8(%3)\n" /* wrote out ! row1 */
203 /* fill destination row2 (needed are mm6=Rr,mm7=Gg,mm5=Bb) */
204 /* this can be done "destructive" */
205 "pxor %%mm2,%%mm2\n" /* 0 0 0 0 0 0 0 0 */
206 "punpckhbw %%mm2,%%mm6\n" /* 0 R3 0 R2 0 R1 0 R0 */
207 "punpckhbw %%mm1,%%mm5\n" /* G3 B3 G2 B2 G1 B1 G0 B0 */
208 "movq %%mm5,%%mm1\n" /* G3 B3 G2 B2 G1 B1 G0 B0 */
209 "punpcklwd %%mm6,%%mm1\n" /* 0 R1 G1 B1 0 R0 G0 B0 */
210 "movq %%mm1,(%5)\n" /* wrote out ! row2 */
211 "punpckhwd %%mm6,%%mm5\n" /* 0 R3 G3 B3 0 R2 G2 B2 */
212 "movq %%mm5,8(%5)\n" /* wrote out ! row2 */
214 "addl $4,%2\n" /* lum+4 */
215 "leal 16(%3),%3\n" /* row1+16 */
216 "leal 16(%5),%5\n" /* row2+16 */
217 "addl $2,(%%esp)\n" /* cr+2 */
218 "addl $2,%1\n" /* cb+2 */
220 "addl $4,%6\n" /* x+4 */
224 "addl %4,%2\n" /* lum += cols */
225 "addl %8,%3\n" /* row1+= mod */
226 "addl %8,%5\n" /* row2+= mod */
227 "movl $0,%6\n" /* x=0 */
231 "addl $4,%%esp\n" /* get rid of the stack slot we reserved. */
232 "emms\n" /* reset MMX registers. */
234 : "m" (cr), "r"(cb),"r"(lum),
235 "r"(row1),"r"(cols),"r"(row2),"m"(x),"m"(y),"m"(mod),
236 "m"(MMX_0080w),"m"(MMX_VgrnRGB),"m"(MMX_VredRGB),
237 "m"(MMX_FF00w),"m"(MMX_00FFw),"m"(MMX_UgrnRGB),
242 void Color565DitherYV12MMX1X( int *colortab, Uint32 *rgb_2_pix,
243 unsigned char *lum, unsigned char *cr,
244 unsigned char *cb, unsigned char *out,
245 int rows, int cols, int mod )
250 unsigned char* y = lum +cols*rows; /* Pointer to the end */
252 row1 = (Uint16 *)out; /* 16 bit target */
253 row2 = (Uint16 *)out+cols+mod; /* start of second row */
254 mod = (mod+cols+mod)*2; /* increment for row1 in byte */
256 __asm__ __volatile__(
257 /* tap dance to workaround the inability to use %%ebx at will... */
258 /* move one thing to the stack... */
259 "pushl $0\n" /* save a slot on the stack. */
260 "pushl %%ebx\n" /* save %%ebx. */
261 "movl %0, %%ebx\n" /* put the thing in ebx. */
262 "movl %%ebx, 4(%%esp)\n" /* put the thing in the stack slot. */
263 "popl %%ebx\n" /* get back %%ebx (the PIC register). */
268 "movd (%1), %%mm0\n" /* 4 Cb 0 0 0 0 u3 u2 u1 u0 */
269 "pxor %%mm7, %%mm7\n"
271 "movl 4(%%esp), %%ebx\n"
272 "movd (%%ebx), %%mm1\n" /* 4 Cr 0 0 0 0 v3 v2 v1 v0 */
275 "punpcklbw %%mm7, %%mm0\n" /* 4 W cb 0 u3 0 u2 0 u1 0 u0 */
276 "punpcklbw %%mm7, %%mm1\n" /* 4 W cr 0 v3 0 v2 0 v1 0 v0 */
279 "movq %%mm0, %%mm2\n" /* Cb 0 u3 0 u2 0 u1 0 u0 */
280 "movq %%mm1, %%mm3\n" /* Cr */
281 "pmullw %10, %%mm2\n" /* Cb2green 0 R3 0 R2 0 R1 0 R0 */
282 "movq (%2), %%mm6\n" /* L1 l7 L6 L5 L4 L3 L2 L1 L0 */
283 "pmullw %11, %%mm0\n" /* Cb2blue */
284 "pand %12, %%mm6\n" /* L1 00 L6 00 L4 00 L2 00 L0 */
285 "pmullw %13, %%mm3\n" /* Cr2green */
286 "movq (%2), %%mm7\n" /* L2 */
287 "pmullw %14, %%mm1\n" /* Cr2red */
288 "psrlw $8, %%mm7\n" /* L2 00 L7 00 L5 00 L3 00 L1 */
289 "pmullw %15, %%mm6\n" /* lum1 */
290 "paddw %%mm3, %%mm2\n" /* Cb2green + Cr2green == green */
291 "pmullw %15, %%mm7\n" /* lum2 */
293 "movq %%mm6, %%mm4\n" /* lum1 */
294 "paddw %%mm0, %%mm6\n" /* lum1 +blue 00 B6 00 B4 00 B2 00 B0 */
295 "movq %%mm4, %%mm5\n" /* lum1 */
296 "paddw %%mm1, %%mm4\n" /* lum1 +red 00 R6 00 R4 00 R2 00 R0 */
297 "paddw %%mm2, %%mm5\n" /* lum1 +green 00 G6 00 G4 00 G2 00 G0 */
298 "psraw $6, %%mm4\n" /* R1 0 .. 64 */
299 "movq %%mm7, %%mm3\n" /* lum2 00 L7 00 L5 00 L3 00 L1 */
300 "psraw $6, %%mm5\n" /* G1 - .. + */
301 "paddw %%mm0, %%mm7\n" /* Lum2 +blue 00 B7 00 B5 00 B3 00 B1 */
302 "psraw $6, %%mm6\n" /* B1 0 .. 64 */
303 "packuswb %%mm4, %%mm4\n" /* R1 R1 */
304 "packuswb %%mm5, %%mm5\n" /* G1 G1 */
305 "packuswb %%mm6, %%mm6\n" /* B1 B1 */
306 "punpcklbw %%mm4, %%mm4\n"
307 "punpcklbw %%mm5, %%mm5\n"
310 "psllw $3, %%mm5\n" /* GREEN 1 */
311 "punpcklbw %%mm6, %%mm6\n"
314 "por %%mm5, %%mm4\n" /* */
315 "psrlw $11, %%mm6\n" /* BLUE 1 */
316 "movq %%mm3, %%mm5\n" /* lum2 */
317 "paddw %%mm1, %%mm3\n" /* lum2 +red 00 R7 00 R5 00 R3 00 R1 */
318 "paddw %%mm2, %%mm5\n" /* lum2 +green 00 G7 00 G5 00 G3 00 G1 */
319 "psraw $6, %%mm3\n" /* R2 */
320 "por %%mm6, %%mm4\n" /* MM4 */
321 "psraw $6, %%mm5\n" /* G2 */
322 "movq (%2, %4), %%mm6\n" /* L3 load lum2 */
324 "packuswb %%mm3, %%mm3\n"
325 "packuswb %%mm5, %%mm5\n"
326 "packuswb %%mm7, %%mm7\n"
327 "pand %12, %%mm6\n" /* L3 */
328 "punpcklbw %%mm3, %%mm3\n"
329 "punpcklbw %%mm5, %%mm5\n"
330 "pmullw %15, %%mm6\n" /* lum3 */
331 "punpcklbw %%mm7, %%mm7\n"
332 "psllw $3, %%mm5\n" /* GREEN 2 */
335 "psrlw $11, %%mm7\n" /* BLUE 2 */
338 "movq (%2,%4), %%mm7\n" /* L4 load lum2 */
340 "psrlw $8, %%mm7\n" /* L4 */
341 "movq %%mm4, %%mm5\n"
342 "punpcklwd %%mm3, %%mm4\n"
343 "pmullw %15, %%mm7\n" /* lum4 */
344 "punpckhwd %%mm3, %%mm5\n"
346 "movq %%mm4, (%3)\n" /* write row1 */
347 "movq %%mm5, 8(%3)\n" /* write row1 */
349 "movq %%mm6, %%mm4\n" /* Lum3 */
350 "paddw %%mm0, %%mm6\n" /* Lum3 +blue */
352 "movq %%mm4, %%mm5\n" /* Lum3 */
353 "paddw %%mm1, %%mm4\n" /* Lum3 +red */
354 "paddw %%mm2, %%mm5\n" /* Lum3 +green */
356 "movq %%mm7, %%mm3\n" /* Lum4 */
358 "paddw %%mm0, %%mm7\n" /* Lum4 +blue */
359 "psraw $6, %%mm6\n" /* Lum3 +blue */
360 "movq %%mm3, %%mm0\n" /* Lum4 */
361 "packuswb %%mm4, %%mm4\n"
362 "paddw %%mm1, %%mm3\n" /* Lum4 +red */
363 "packuswb %%mm5, %%mm5\n"
364 "paddw %%mm2, %%mm0\n" /* Lum4 +green */
365 "packuswb %%mm6, %%mm6\n"
366 "punpcklbw %%mm4, %%mm4\n"
367 "punpcklbw %%mm5, %%mm5\n"
368 "punpcklbw %%mm6, %%mm6\n"
369 "psllw $3, %%mm5\n" /* GREEN 3 */
371 "psraw $6, %%mm3\n" /* psr 6 */
373 "pand %16, %%mm6\n" /* BLUE */
375 "psrlw $11, %%mm6\n" /* BLUE 3 */
379 "packuswb %%mm3, %%mm3\n"
380 "packuswb %%mm0, %%mm0\n"
381 "packuswb %%mm7, %%mm7\n"
382 "punpcklbw %%mm3, %%mm3\n"
383 "punpcklbw %%mm0, %%mm0\n"
384 "punpcklbw %%mm7, %%mm7\n"
386 "pand %16, %%mm7\n" /* BLUE */
387 "psllw $3, %%mm0\n" /* GREEN 4 */
393 "movq %%mm4, %%mm5\n"
395 "punpcklwd %%mm3, %%mm4\n"
396 "punpckhwd %%mm3, %%mm5\n"
399 "movq %%mm5, 8(%5)\n"
407 "leal 16(%5),%5\n" /* row2+16 */
410 "addl %4, %2\n" /* lum += cols */
411 "addl %8, %3\n" /* row1+= mod */
412 "addl %8, %5\n" /* row2+= mod */
413 "movl $0, %6\n" /* x=0 */
416 "addl $4, %%esp\n" /* get rid of the stack slot we reserved. */
419 : "m" (cr), "r"(cb),"r"(lum),
420 "r"(row1),"r"(cols),"r"(row2),"m"(x),"m"(y),"m"(mod),
421 "m"(MMX_0080w),"m"(MMX_Ugrn565),"m"(MMX_Ublu5x5),
422 "m"(MMX_00FFw),"m"(MMX_Vgrn565),"m"(MMX_Vred5x5),
423 "m"(MMX_Ycoeff),"m"(MMX_red565),"m"(MMX_grn565)
429 #endif /* GCC3 i386 inline assembly */
431 /* vi: set ts=4 sw=4 expandtab: */