Debian Codegraph @

cvd_src/i686/yuv411_to_stuff_MMX_64.C

raw
1 /*
2 This file is part of the CVD Library.
3
4 Copyright (C) 2005 The Authors
5
6 This library is free software, see LICENSE file for details
7 */
8
9 // This contains YUV411-to-foo conversions, ported from
10 // yuv411_to_stuff_MMX.C to work on x86_64 processors.
11
12 #include <cvd/colourspace.h>
13 #include <cvd/config.h>
14 #include <cvd/timer.h>
15
16 namespace CVD
17 {
18 namespace ColourSpace
19 {
20
21 void yuv411_to_rgb_y(const unsigned char* in, int size, unsigned char* out, unsigned char *lum_out)
22 {
23 const unsigned char* in_end = in + size * 6 / 4;
24
25 size /=8;
26
27 __asm__ __volatile__(
28 //Put some zeros in mm7
29 "mov $0, %%eax \n\t"
30 "movd %%eax, %%mm7 \n\t"
31
32 //Load out and in
33 "mov %0, %%rdi \n\t"
34 "mov %1, %%rsi \n\t"
35 "mov %[luma], %%r10 \n\t"
36 "push %[end] \n\t"
37
38 ".Lfoo: \n\t"
39
40 "prefetchnta 128(%%rsi) \n\t"
41
42 ////////////////////////////////////////////////////////////
43 //
44 // Load and convert the chrominance values to mmxable things
45 //
46
47 //Create the register mm0 = * bu guv rv
48 //Let ecx, edx contain (u-128), (v-128)
49
50 //Note the following 4 instructions are in a different order in the second
51 //block, since it's faster to have them different (!)
52
53 "movzbl 3(%%rsi), %%edx \n\t" //dx = v move-(with-zero-extend)-byte-(in to)-long
54 "sub $128, %%edx \n\t"
55 "movzbl (%%rsi), %%ecx \n\t" //cx = u
56 "sub $128, %%ecx \n\t"
57
58 "mov %%edx, %%eax \n\t" //Calculate and insert rv = v * 147
59 "imul $147, %%eax \n\t"
60 "pinsrw $0, %%eax, %%mm0 \n\t"
61
62 "mov %%ecx, %%ebx \n\t" //Calculate and insert bu = v * 256
63 "imul $256, %%ebx \n\t"
64 "pinsrw $2, %%ebx, %%mm0 \n\t"
65
66 "imul $-38, %%ecx \n\t" //Calculate and insert guv = -38u + -74v GKMOD
67 "imul $-74, %%edx \n\t"
68
69 //There's a gap here while we wait for the imuls to finish. Maybe?
70
71 "add %%ecx, %%edx \n\t"
72 "pinsrw $1, %%edx, %%mm0 \n\t" //now, mm0 = 0 bu guv rv
73
74 ////////////////////////////////////////////////////////////
75 //
76 // Load and convert luminance
77 //
78 //Put y4y3y2y1 in to eax
79 "mov 2(%%rsi), %%eax \n\t" //Load y4y3?? in to eax
80 "movw 1(%%rsi), %%ax \n\t" //load y2y1 in to ax
81
82 //Unpack and shift left by 7, so that mm3 contains the fixed point version of y4--y1
83 "movd %%eax, %%mm3 \n\t"
84 "punpcklbw %%mm7, %%mm3 \n\t"
85 "psllw $7, %%mm3 \n\t"
86
87 "pshufw $0x40, %%mm3, %%mm1 \n\t" //Create y2 y1 y1 y1 register mm1: 0x40 = 01 00 00 00
88 "pshufw $0xa5, %%mm3, %%mm2 \n\t" //Create y3 y3 y2 y2 register mm2: 0xa5 = 10 10 01 01
89 "pshufw $0xfe, %%mm3, %%mm3 \n\t" //Create y4 y4 y4 y3 register mm3: 0xfe = 11 11 11 10
90
91
92 #ifdef CVD_HAVE_SSE2
93 "movnti %%eax, (%%r10) \n\t"
94 #else
95 "mov %%eax, (%%r10) \n\t"
96 #endif
97
98 /////////////////////////////////////////////////////////////
99 //
100 // Perform fixed point conversion to RGB
101 //
102
103 //Perform mm1 += mm0, mm2 += mm0, etc
104 //permute mm0 each time in to the right shape
105 "pshufw $0x24, %%mm0, %%mm0 \n\t" //mm0 = r b g r 0x24 = 00 10 01 00
106 "paddw %%mm0, %%mm1 \n\t"
107
108 "pshufw $0x49, %%mm0, %%mm0 \n\t" //mm0 = g r b g 0x49 = 01 00 10 01
109 "paddw %%mm0, %%mm2 \n\t"
110
111 "pshufw $0x49, %%mm0, %%mm0 \n\t" //mm0 = b g r b 0x49 = 01 00 10 01
112 "paddw %%mm0, %%mm3 \n\t"
113
114 //Perform right shift of 7 (mm5) on mm1, 2, 3, 4
115 //!!!!! Apparently, there is only one shifter. Might be good to reorganise a bit
116 "psrlw $7, %%mm1 \n\t"
117 "psrlw $7, %%mm2 \n\t"
118 "psrlw $7, %%mm3 \n\t"
119
120 //Pack mm1,2 in to mm1 with unsigned saturation
121 "packuswb %%mm2, %%mm1 \n\t"
122
123 //At this point we only have 3 pixels, which won't leave the writing
124 //position in a 64 bit aligned state, so do another 3.
125
126 ////////////////////////////////////////////////////////////////////////////////
127 ////////////////////////////////////////////////////////////////////////////////
128 ////////////////////////////////////////////////////////////////////////////////
129 ////////////////////////////////////////////////////////////////////////////////
130 //Perform the conversion on the second set of 3 pixels.
131
132
133 ////////////////////////////////////////////////////////////
134 //
135 // Load and convert the chrominance values to mmxable things
136 //
137
138 //Create the register mm0 = * bu guv rv
139 //Let ecx, edx contain (u-128), (v-128)
140
141 "movzbl 9(%%rsi), %%edx \n\t" //dx = v
142 "movzbl 6(%%rsi), %%ecx \n\t" //cx = u move-(with-zero-extend)-byte-(in to)-long
143 "sub $128, %%edx \n\t"
144 "sub $128, %%ecx \n\t"
145
146 "mov %%edx, %%eax \n\t" //Calculate and insert rv = v * 147
147 "imul $147, %%eax \n\t"
148 "pinsrw $0, %%eax, %%mm0 \n\t"
149
150 "mov %%ecx, %%ebx \n\t" //Calculate and insert bu = v * 256
151 "imul $256, %%ebx \n\t"
152 "pinsrw $2, %%ebx, %%mm0 \n\t"
153
154 "imul $-38, %%ecx \n\t" //Calculate and insert guv = -38u + -74v
155 "imul $-74, %%edx \n\t"
156 "add %%ecx, %%edx \n\t"
157 "pinsrw $1, %%edx, %%mm0 \n\t" //now, mm0 = 0 bu guv rv
158
159
160 ////////////////////////////////////////////////////////////
161 //
162 // Load and convert luminance
163 //
164
165 //Put y4y3y2y1 in to eax
166 "mov 8(%%rsi), %%eax \n\t" //Load y4y3?? in to eax
167 "movw 7(%%rsi), %%ax \n\t" //load y2y1 in to ax
168
169 //Unpack and shift left by 7, so that mm5 contains the fixed point version of y4--y1
170 "movd %%eax, %%mm6 \n\t"
171 "punpcklbw %%mm7, %%mm6 \n\t"
172 "psllw $7, %%mm6 \n\t"
173
174
175 #ifdef CVD_HAVE_SSE2
176 "movnti %%eax, 4(%%r10) \n\t"
177 #else
178 "mov %%eax, 4(%%r10) \n\t"
179 #endif
180
181 "pshufw $0x40, %%mm6, %%mm4 \n\t" //Create y2 y1 y1 y1 register mm1: 0x40 = 01 00 00 00
182 "pshufw $0xa5, %%mm6, %%mm5 \n\t" //Create y3 y3 y2 y2 register mm2: 0xa5 = 10 10 01 01
183 "pshufw $0xfe, %%mm6, %%mm6 \n\t" //Create y4 y4 y4 y3 register mm3: 0xfe = 11 11 11 10
184
185
186 /////////////////////////////////////////////////////////////
187 //
188 // Perform fixed point conversion to RGB
189 //
190
191 //Perform mm1 += mm0, mm2 += mm0, etc
192 //permute mm0 each time in to the right shape
193 "pshufw $0x24, %%mm0, %%mm0 \n\t" //mm0 = r b g r 0x24 = 00 10 01 00
194 "paddw %%mm0, %%mm4 \n\t"
195
196 "pshufw $0x49, %%mm0, %%mm0 \n\t" //mm0 = g r b g 0x49 = 01 00 10 01
197 "paddw %%mm0, %%mm5 \n\t"
198
199 "pshufw $0x49, %%mm0, %%mm0 \n\t" //mm0 = b g r b 0x49 = 01 00 10 01
200 "paddw %%mm0, %%mm6 \n\t"
201
202 //Perform right shift of 7 (mm5) on mm1, 2, 3, 4
203 "psrlw $7, %%mm4 \n\t"
204 "psrlw $7, %%mm5 \n\t"
205 "psrlw $7, %%mm6 \n\t"
206
207 //Pack mm1,2 in to mm1 and mm3,4 in to mm3 with unsigned saturation
208 "packuswb %%mm4, %%mm3 \n\t"
209 "packuswb %%mm6, %%mm5 \n\t"
210
211 //movntq uses write-combining. Fast.
212 "movntq %%mm1, (%%rdi) \n\t"
213 "movntq %%mm3, 8(%%rdi) \n\t"
214 "movntq %%mm5, 16(%%rdi)\n\t"
215
216
217 ////////////////////////////////////////////////////////////////////////////////
218 ////////////////////////////////////////////////////////////////////////////////
219 ////////////////////////////////////////////////////////////////////////////////
220
221 //Increment counters
222 "add $12, %%rsi \n\t"
223 "add $24, %%rdi \n\t"
224 "add $8, %%r10 \n\t"
225 "cmp %%rsi, (%%rsp) \n\t" //in_end is on the top of the stack
226 "jne .Lfoo \n\t"
227 "pop %%rax \n\t" //discard the top
228 "emms \n\t" //End mmx
229 :
230 : "m" (out), "m" (in), "g" (size), [luma] "m" (lum_out), [end] "m" (in_end)
231 : "eax", "ecx", "edx", "rdi", "rsi", "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7", "r10"
232 );
233 }
234
235 void yuv411_to_rgb(const unsigned char* in, int size, unsigned char* out)
236 {
237 //Time: 5.0ms memcpy on number of luma pixels only takes 1.5 ms
238 size /=8;
239 __asm__ __volatile__(
240 //Put some zeros in mm7
241 "mov $0, %%eax \n\t"
242 "movd %%eax, %%mm7 \n\t"
243
244 //Load out and in
245 "mov %0, %%rdi \n\t"
246 "mov %1, %%rsi \n\t"
247 "mov %2, %%ebx \n\t"
248
249 ".Lyuv411dec: \n\t"
250
251 "prefetchnta 64(%%rsi) \n\t"
252 /*Convert yuv411 to rgb8
253 format is u1 y1 y2 v1 y3 y4
254 u, v are int8 - 128
255 y are uint8
256
257 For YUV in general:
258 r = y + 1.140 * v = y + rv
259 g = y - 0.394*u - 0.581*v = y + guv
260 b = y + 2.028 * u = y + bu
261
262 Arithmetic is performed using fixed point with a left shift of 7 (ie *128)
263 u1, v1 are not interpolated between y pixels, ie u1,v1 are used for y1,y2,y3,y4
264 b is truncated slightly, ie b= y + 2.0 * u
265
266 The resultant 8 bit r,g,b values need to be saturated, since overflow occurs
267 and this leads to very ugly results.
268 */
269
270 ////////////////////////////////////////////////////////////
271 //
272 // Load and convert the chrominance values to mmxable things
273 //
274
275 //Create the register mm0 = * bu guv rv
276 //Let ecx, edx contain (u-128), (v-128)
277
278 //Note the following 4 instructions are in a different order in the second
279 //block, since it's faster to have them different (!)
280
281 "movzbl 3(%%rsi), %%edx \n\t" //dx = v move-(with-zero-extend)-byte-(in to)-long
282 "sub $128, %%edx \n\t"
283 "movzbl (%%rsi), %%ecx \n\t" //cx = u
284 "sub $128, %%ecx \n\t"
285
286 "mov %%edx, %%eax \n\t" //Calculate and insert rv = v * 147
287 "imul $147, %%eax \n\t"
288 "pinsrw $0, %%eax, %%mm0 \n\t"
289
290 "mov %%ecx, %%eax \n\t" //Calculate and insert bu = v * 256
291 "imul $256, %%eax \n\t"
292 "pinsrw $2, %%eax, %%mm0 \n\t"
293
294 "imul $-38, %%ecx \n\t" //Calculate and insert guv = -38u + -74v
295 "imul $-74, %%edx \n\t"
296
297 //There's a gap here while we wait for the imuls to finish. Maybe?
298
299
300
301 "add %%ecx, %%edx \n\t"
302 "pinsrw $1, %%edx, %%mm0 \n\t" //now, mm0 = 0 bu guv rv
303
304 ////////////////////////////////////////////////////////////
305 //
306 // Load and convert luminance
307 //
308
309 //Put y4y3y2y1 in to eax
310 "mov 2(%%rsi), %%eax \n\t" //Load y4y3?? in to eax
311 "movw 1(%%rsi), %%ax \n\t" //load y2y1 in to ax
312
313 //Unpack and shift left by 7, so that mm3 contains the fixed point version of y4--y1
314 "movd %%eax, %%mm3 \n\t"
315 "punpcklbw %%mm7, %%mm3 \n\t"
316 "psllw $7, %%mm3 \n\t"
317
318 "pshufw $0x40, %%mm3, %%mm1 \n\t" //Create y2 y1 y1 y1 register mm1: 0x40 = 01 00 00 00
319 "pshufw $0xa5, %%mm3, %%mm2 \n\t" //Create y3 y3 y2 y2 register mm2: 0xa5 = 10 10 01 01
320 "pshufw $0xfe, %%mm3, %%mm3 \n\t" //Create y4 y4 y4 y3 register mm3: 0xfe = 11 11 11 10
321
322
323 /////////////////////////////////////////////////////////////
324 //
325 // Perform fixed point conversion to RGB
326 //
327
328 //Perform mm1 += mm0, mm2 += mm0, etc
329 //permute mm0 each time in to the right shape
330 "pshufw $0x24, %%mm0, %%mm0 \n\t" //mm0 = r b g r 0x24 = 00 10 01 00
331 "paddw %%mm0, %%mm1 \n\t"
332
333 "pshufw $0x49, %%mm0, %%mm0 \n\t" //mm0 = g r b g 0x49 = 01 00 10 01
334 "paddw %%mm0, %%mm2 \n\t"
335
336 "pshufw $0x49, %%mm0, %%mm0 \n\t" //mm0 = b g r b 0x49 = 01 00 10 01
337 "paddw %%mm0, %%mm3 \n\t"
338
339 //Perform right shift of 7 (mm5) on mm1, 2, 3, 4
340 //!!!!! Apparently, there is only one shifter. Might be good to reorganise a bit
341 "psrlw $7, %%mm1 \n\t"
342 "psrlw $7, %%mm2 \n\t"
343 "psrlw $7, %%mm3 \n\t"
344
345 //Pack mm1,2 in to mm1 with unsigned saturation
346 "packuswb %%mm2, %%mm1 \n\t"
347
348 //At this point we only have 3 pixels, which won't leave the writing
349 //position in a 64 bit aligned state, so do another 3.
350
351 ////////////////////////////////////////////////////////////////////////////////
352 ////////////////////////////////////////////////////////////////////////////////
353 ////////////////////////////////////////////////////////////////////////////////
354 ////////////////////////////////////////////////////////////////////////////////
355 //Perform the conversion on the second set of 3 pixels.
356
357
358 ////////////////////////////////////////////////////////////
359 //
360 // Load and convert the chrominance values to mmxable things
361 //
362
363 //Create the register mm0 = * bu guv rv
364 //Let ecx, edx contain (u-128), (v-128)
365
366 "movzbl 9(%%rsi), %%edx \n\t" //dx = v
367 "movzbl 6(%%rsi), %%ecx \n\t" //cx = u move-(with-zero-extend)-byte-(in to)-long
368 "sub $128, %%edx \n\t"
369 "sub $128, %%ecx \n\t"
370
371 "mov %%edx, %%eax \n\t" //Calculate and insert rv = v * 147
372 "imul $147, %%eax \n\t"
373 "pinsrw $0, %%eax, %%mm0 \n\t"
374
375 "mov %%ecx, %%eax \n\t" //Calculate and insert bu = v * 256
376 "imul $256, %%eax \n\t"
377 "pinsrw $2, %%eax, %%mm0 \n\t"
378
379 "imul $-38, %%ecx \n\t" //Calculate and insert guv = -38u + -74v
380 "imul $-74, %%edx \n\t"
381 "add %%ecx, %%edx \n\t"
382 "pinsrw $1, %%edx, %%mm0 \n\t" //now, mm0 = 0 bu guv rv
383
384
385 ////////////////////////////////////////////////////////////
386 //
387 // Load and convert luminance
388 //
389
390 //Put y4y3y2y1 in to eax
391 "mov 8(%%rsi), %%eax \n\t" //Load y4y3?? in to eax
392 "movw 7(%%rsi), %%ax \n\t" //load y2y1 in to ax
393
394 //Unpack and shift left by 7, so that mm5 contains the fixed point version of y4--y1
395 "movd %%eax, %%mm6 \n\t"
396 "punpcklbw %%mm7, %%mm6 \n\t"
397 "psllw $7, %%mm6 \n\t"
398
399 "pshufw $0x40, %%mm6, %%mm4 \n\t" //Create y2 y1 y1 y1 register mm1: 0x40 = 01 00 00 00
400 "pshufw $0xa5, %%mm6, %%mm5 \n\t" //Create y3 y3 y2 y2 register mm2: 0xa5 = 10 10 01 01
401 "pshufw $0xfe, %%mm6, %%mm6 \n\t" //Create y4 y4 y4 y3 register mm3: 0xfe = 11 11 11 10
402
403
404 /////////////////////////////////////////////////////////////
405 //
406 // Perform fixed point conversion to RGB
407 //
408
409 //Perform mm1 += mm0, mm2 += mm0, etc
410 //permute mm0 each time in to the right shape
411 "pshufw $0x24, %%mm0, %%mm0 \n\t" //mm0 = r b g r 0x24 = 00 10 01 00
412 "paddw %%mm0, %%mm4 \n\t"
413
414 "pshufw $0x49, %%mm0, %%mm0 \n\t" //mm0 = g r b g 0x49 = 01 00 10 01
415 "paddw %%mm0, %%mm5 \n\t"
416
417 "pshufw $0x49, %%mm0, %%mm0 \n\t" //mm0 = b g r b 0x49 = 01 00 10 01
418 "paddw %%mm0, %%mm6 \n\t"
419
420 //Perform right shift of 7 (mm5) on mm1, 2, 3, 4
421 "psrlw $7, %%mm4 \n\t"
422 "psrlw $7, %%mm5 \n\t"
423 "psrlw $7, %%mm6 \n\t"
424
425 //Pack mm1,2 in to mm1 and mm3,4 in to mm3 with unsigned saturation
426 "packuswb %%mm4, %%mm3 \n\t"
427 "packuswb %%mm6, %%mm5 \n\t"
428
429 //movntq uses write-combining. Fast.
430 "movntq %%mm1, (%%rdi) \n\t"
431 "movntq %%mm3, 8(%%rdi) \n\t"
432 "movntq %%mm5, 16(%%rdi)\n\t"
433
434
435 ////////////////////////////////////////////////////////////////////////////////
436 ////////////////////////////////////////////////////////////////////////////////
437 ////////////////////////////////////////////////////////////////////////////////
438
439 //Increment counters
440 "add $24, %%rdi \n\t"
441 "add $12, %%rsi \n\t"
442 "dec %%ebx \n\t"
443 "jnz .Lyuv411dec \n\t"
444 "emms \n\t" //End mmx
445 // 0 1 2
446 :
447 : "m" (out), "m" (in), "g" (size)
448 : "eax", "ecx", "edx", "rbx", "rdi", "rsi", "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "r10"
449 );
450 }
451
452 void yuv411_to_y(const unsigned char* in, int size, unsigned char* out)
453 {
454 //Time, 2.5ms. Comparison, memcpy on the same number of output bytes=1.5ms
455 size /=16;
456 __asm__ __volatile__(
457 "mov %0, %%rdi \n\t"
458 "mov %1, %%rsi \n\t"
459 "mov %2, %%ecx \n\t"
460 ".Lyuvtolum: \n\t"
461 "prefetchnta 64(%%rsi) \n\t"
462 "mov 2(%%rsi), %%eax \n\t"
463 "movw 1(%%rsi), %%ax \n\t"
464 "mov 8(%%rsi), %%ebx \n\t"
465 "movw 7(%%rsi), %%bx \n\t"
466
467 "movd %%eax, %%mm0 \n\t"
468 "movd %%ebx, %%mm1 \n\t"
469 "psllq $32, %%mm1 \n\t"
470 "por %%mm1, %%mm0 \n\t" //mm0 = ebx,eax
471
472 "mov 14(%%rsi), %%eax \n\t"
473 "movw 13(%%rsi), %%ax \n\t"
474 "mov 20(%%rsi), %%ebx \n\t"
475 "movw 19(%%rsi), %%bx \n\t"
476
477 "movd %%eax, %%mm1 \n\t"
478 "movd %%ebx, %%mm2 \n\t"
479 "psllq $32, %%mm2 \n\t"
480 "por %%mm2, %%mm1 \n\t" //mm1 = ebx,eax
481
482 "movntq %%mm0, (%%rdi) \n\t"
483 "movntq %%mm1, 8(%%rdi) \n\t"
484
485 "add $24, %%rsi \n\t"
486 "add $16, %%rdi \n\t"
487 "dec %%ecx \n\t"
488 "jnz .Lyuvtolum \n\t"
489 "emms \n\t"
490 :
491 : "m" (out), "m" (in), "g" (size)
492 : "rdi", "rsi", "mm0", "mm1", "mm2", "eax", "ebx", "ecx", "memory");
493 }
494
495 }
496 }
497