2 * (C) Copyright IBM Corporation 2004
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file read_rgba_span_x86.S
27 * Optimized routines to transfer pixel data from the framebuffer to a
28 * buffer in main memory.
30 * \author Ian Romanick <idr@us.ibm.com>
33 .file "read_rgba_span_x86.S"
34 #if !defined(__DJGPP__) && !defined(__MINGW32__) /* this one cries for assyntax.h */
50 /* I implemented these as macros because the appear in quite a few places,
51 * and I've tweaked them a number of times. I got tired of changing every
52 * place they appear. :)
55 #define DO_ONE_PIXEL() \
58 bswap %eax /* ARGB -> BGRA */ ; \
59 rorl $8, %eax /* BGRA -> ABGR */ ; \
60 movl %eax, (%ecx) /* ABGR -> R, G, B, A */ ; \
63 #define DO_ONE_LAST_PIXEL() \
65 bswap %eax /* ARGB -> BGRA */ ; \
66 rorl $8, %eax /* BGRA -> ABGR */ ; \
67 movl %eax, (%ecx) /* ABGR -> R, G, B, A */ ; \
71 * MMX optimized version of the BGRA8888_REV to RGBA copy routine.
74 * This function assumes that the caller will issue the EMMS instruction
75 * at the correct places.
78 .globl _generic_read_RGBA_span_BGRA8888_REV_MMX
79 .type _generic_read_RGBA_span_BGRA8888_REV_MMX, @function
80 _generic_read_RGBA_span_BGRA8888_REV_MMX:
89 movl 8(%esp), %ebx /* source pointer */
90 movl 16(%esp), %edx /* number of pixels to copy */
91 movl 12(%esp), %ecx /* destination pointer */
94 je .L20 /* Bail if there's nothing to do. */
107 /* Would it be faster to unroll this loop once and process 4 pixels
108 * per pass, instead of just two?
118 /* These 9 instructions do what PSHUFB (if there were such an
119 * instruction) could do in 1. :(
140 #ifdef USE_INNER_EMMS
144 /* At this point there are either 1 or 0 pixels remaining to be
145 * converted. Convert the last pixel, if needed.
156 .size _generic_read_RGBA_span_BGRA8888_REV_MMX, .-_generic_read_RGBA_span_BGRA8888_REV_MMX
160 * SSE optimized version of the BGRA8888_REV to RGBA copy routine. SSE
161 * instructions are only actually used to read data from the framebuffer.
162 * In practice, the speed-up is pretty small.
165 * Do some more testing and determine if there's any reason to have this
166 * function in addition to the MMX version.
169 * This function assumes that the caller will issue the EMMS instruction
170 * at the correct places.
173 .globl _generic_read_RGBA_span_BGRA8888_REV_SSE
174 .type _generic_read_RGBA_span_BGRA8888_REV_SSE, @function
175 _generic_read_RGBA_span_BGRA8888_REV_SSE:
180 #ifdef USE_INNER_EMMS
186 movl 16(%esp), %ebx /* source pointer */
187 movl 24(%esp), %edx /* number of pixels to copy */
188 movl 20(%esp), %ecx /* destination pointer */
192 andl $0xfffffff0, %esp
240 /* This would be so much better if we could just move directly from
241 * an SSE register to an MMX register. Unfortunately, that
242 * functionality wasn't introduced until SSE2 with the MOVDQ2Q
284 #ifdef USE_INNER_EMMS
289 /* At this point there are either [0, 3] pixels remaining to be
324 .size _generic_read_RGBA_span_BGRA8888_REV_SSE, .-_generic_read_RGBA_span_BGRA8888_REV_SSE
328 * SSE2 optimized version of the BGRA8888_REV to RGBA copy routine.
332 .globl _generic_read_RGBA_span_BGRA8888_REV_SSE2
333 .type _generic_read_RGBA_span_BGRA8888_REV_SSE2, @function
334 _generic_read_RGBA_span_BGRA8888_REV_SSE2:
339 movdqa mask+16, %xmm2
341 movl 12(%esp), %ebx /* source pointer */
342 movl 20(%esp), %edx /* number of pixels to copy */
343 movl 16(%esp), %ecx /* destination pointer */
348 /* If the source pointer isn't a multiple of 16 we have to process
349 * a few pixels the "slow" way to get the address aligned for
350 * the SSE fetch intsructions.
388 /* Would it be worth having a specialized version of this loop for
389 * the case where the destination is 16-byte aligned? That version
390 * would be identical except that it could use movedqa instead of
420 /* There may be upto 3 pixels remaining to be copied. Take care
421 * of them now. We do the 2 pixel case first because the data
454 .size _generic_read_RGBA_span_BGRA8888_REV_SSE2, .-_generic_read_RGBA_span_BGRA8888_REV_SSE2
467 /* Setting SCALE_ADJUST to 5 gives a perfect match with the classic C
468 * implementation in Mesa. Setting SCALE_ADJUST to 0 is slightly faster but
469 * at a small cost to accuracy.
472 #define SCALE_ADJUST 5
473 #if SCALE_ADJUST == 5
481 .word 0x20e8 /* (0x00ff0000 / 0x000007c0) + 1 */
482 .word 0x40c5 /* (0x00ff0000 / 0x000003f0) + 1 */
483 .word 0x839d /* (0x00ff0000 / 0x000001f0) + 1 */
485 #elif SCALE_ADJUST == 0
493 .word 0x0108 /* (0x00ff0000 / 0x0000f800) + 1 */
494 .word 0x0104 /* (0x00ff0000 / 0x0000fc00) + 1 */
495 .word 0x0108 /* (0x00ff0000 / 0x0000f800) + 1 */
498 #error SCALE_ADJUST must either be 5 or 0.
502 alpha: .long 0x00000000
506 * MMX optimized version of the RGB565 to RGBA copy routine.
510 .globl _generic_read_RGBA_span_RGB565_MMX
511 .type _generic_read_RGBA_span_RGB565_MMX, @function
513 _generic_read_RGBA_span_RGB565_MMX:
515 #ifdef USE_INNER_EMMS
519 movl 4(%esp), %eax /* source pointer */
520 movl 8(%esp), %edx /* destination pointer */
521 movl 12(%esp), %ecx /* number of pixels to copy */
531 /* Fetch 4 RGB565 pixels into %mm4. Distribute the first and
532 * second pixels into the four words of %mm0 and %mm2.
538 pshufw $0x00, %mm4, %mm0
539 pshufw $0x55, %mm4, %mm2
542 /* Mask the pixels so that each word of each register contains only
543 * one color component.
550 /* Adjust the component values so that they are as small as possible,
551 * but large enough so that we can multiply them by an unsigned 16-bit
552 * number and get a value as large as 0x00ff0000.
558 psrlw $SCALE_ADJUST, %mm0
559 psrlw $SCALE_ADJUST, %mm2
562 /* Scale the input component values to be on the range
563 * [0, 0x00ff0000]. This it the real magic of the whole routine.
570 /* Always set the alpha value to 0xff.
577 /* Pack the 16-bit values to 8-bit values and store the converted
587 pshufw $0xaa, %mm4, %mm0
588 pshufw $0xff, %mm4, %mm2
595 psrlw $SCALE_ADJUST, %mm0
596 psrlw $SCALE_ADJUST, %mm2
614 /* At this point there can be at most 3 pixels left to process. If
615 * there is either 2 or 3 left, process 2.
625 pshufw $0x00, %mm4, %mm0
626 pshufw $0x55, %mm4, %mm2
633 psrlw $SCALE_ADJUST, %mm0
634 psrlw $SCALE_ADJUST, %mm2
648 /* At this point there can be at most 1 pixel left to process.
649 * Process it if needed.
658 pshufw $0x00, %mm4, %mm0
663 psrlw $SCALE_ADJUST, %mm0
674 #ifdef USE_INNER_EMMS
678 #endif /* !defined(__DJGPP__) && !defined(__MINGW32__) */