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__) && !defined(__APPLE__) /* this one cries for assyntax.h */
35 /* Kevin F. Quinn 2nd July 2006
36 * Replaced data segment constants with text-segment instructions.
38 #define LOAD_MASK(mvins,m1,m2) \
51 /* I implemented these as macros because they appear in several places,
52 * and I've tweaked them a number of times. I got tired of changing every
53 * place they appear. :)
56 #define DO_ONE_PIXEL() \
59 bswap %eax /* ARGB -> BGRA */ ; \
60 rorl $8, %eax /* BGRA -> ABGR */ ; \
61 movl %eax, (%ecx) /* ABGR -> R, G, B, A */ ; \
64 #define DO_ONE_LAST_PIXEL() \
66 bswap %eax /* ARGB -> BGRA */ ; \
67 rorl $8, %eax /* BGRA -> ABGR */ ; \
68 movl %eax, (%ecx) /* ABGR -> R, G, B, A */ ; \
72 * MMX optimized version of the BGRA8888_REV to RGBA copy routine.
75 * This function assumes that the caller will issue the EMMS instruction
76 * at the correct places.
79 .globl _generic_read_RGBA_span_BGRA8888_REV_MMX
81 .hidden _generic_read_RGBA_span_BGRA8888_REV_MMX
83 .type _generic_read_RGBA_span_BGRA8888_REV_MMX, @function
84 _generic_read_RGBA_span_BGRA8888_REV_MMX:
90 LOAD_MASK(movq,%mm1,%mm2)
92 movl 8(%esp), %ebx /* source pointer */
93 movl 16(%esp), %edx /* number of pixels to copy */
94 movl 12(%esp), %ecx /* destination pointer */
97 jle .L20 /* Bail if there's nothing to do. */
110 /* Would it be faster to unroll this loop once and process 4 pixels
111 * per pass, instead of just two?
121 /* These 9 instructions do what PSHUFB (if there were such an
122 * instruction) could do in 1. :(
143 #ifdef USE_INNER_EMMS
147 /* At this point there are either 1 or 0 pixels remaining to be
148 * converted. Convert the last pixel, if needed.
159 .size _generic_read_RGBA_span_BGRA8888_REV_MMX, .-_generic_read_RGBA_span_BGRA8888_REV_MMX
163 * SSE optimized version of the BGRA8888_REV to RGBA copy routine. SSE
164 * instructions are only actually used to read data from the framebuffer.
165 * In practice, the speed-up is pretty small.
168 * Do some more testing and determine if there's any reason to have this
169 * function in addition to the MMX version.
172 * This function assumes that the caller will issue the EMMS instruction
173 * at the correct places.
176 .globl _generic_read_RGBA_span_BGRA8888_REV_SSE
178 .hidden _generic_read_RGBA_span_BGRA8888_REV_SSE
180 .type _generic_read_RGBA_span_BGRA8888_REV_SSE, @function
181 _generic_read_RGBA_span_BGRA8888_REV_SSE:
186 #ifdef USE_INNER_EMMS
190 LOAD_MASK(movq,%mm1,%mm2)
192 movl 16(%esp), %ebx /* source pointer */
193 movl 24(%esp), %edx /* number of pixels to copy */
194 movl 20(%esp), %ecx /* destination pointer */
197 jle .L35 /* Bail if there's nothing to do. */
201 andl $0xfffffff0, %esp
249 /* This would be so much better if we could just move directly from
250 * an SSE register to an MMX register. Unfortunately, that
251 * functionality wasn't introduced until SSE2 with the MOVDQ2Q
293 #ifdef USE_INNER_EMMS
298 /* At this point there are either [0, 3] pixels remaining to be
333 .size _generic_read_RGBA_span_BGRA8888_REV_SSE, .-_generic_read_RGBA_span_BGRA8888_REV_SSE
337 * SSE2 optimized version of the BGRA8888_REV to RGBA copy routine.
341 .globl _generic_read_RGBA_span_BGRA8888_REV_SSE2
343 .hidden _generic_read_RGBA_span_BGRA8888_REV_SSE2
345 .type _generic_read_RGBA_span_BGRA8888_REV_SSE2, @function
346 _generic_read_RGBA_span_BGRA8888_REV_SSE2:
350 LOAD_MASK(movdqu,%xmm1,%xmm2)
352 movl 12(%esp), %ebx /* source pointer */
353 movl 20(%esp), %edx /* number of pixels to copy */
354 movl 16(%esp), %ecx /* destination pointer */
360 jle .L46 /* Bail if there's nothing to do. */
362 /* If the source pointer isn't a multiple of 16 we have to process
363 * a few pixels the "slow" way to get the address aligned for
364 * the SSE fetch intsructions.
402 /* Would it be worth having a specialized version of this loop for
403 * the case where the destination is 16-byte aligned? That version
404 * would be identical except that it could use movedqa instead of
434 /* There may be upto 3 pixels remaining to be copied. Take care
435 * of them now. We do the 2 pixel case first because the data
470 .size _generic_read_RGBA_span_BGRA8888_REV_SSE2, .-_generic_read_RGBA_span_BGRA8888_REV_SSE2
474 #define MASK_565_L 0x07e0f800
475 #define MASK_565_H 0x0000001f
476 /* Setting SCALE_ADJUST to 5 gives a perfect match with the
477 * classic C implementation in Mesa. Setting SCALE_ADJUST
478 * to 0 is slightly faster but at a small cost to accuracy.
480 #define SCALE_ADJUST 5
481 #if SCALE_ADJUST == 5
482 #define PRESCALE_L 0x00100001
483 #define PRESCALE_H 0x00000200
484 #define SCALE_L 0x40C620E8
485 #define SCALE_H 0x0000839d
486 #elif SCALE_ADJUST == 0
487 #define PRESCALE_L 0x00200001
488 #define PRESCALE_H 0x00000800
489 #define SCALE_L 0x01040108
490 #define SCALE_H 0x00000108
492 #error SCALE_ADJUST must either be 5 or 0.
494 #define ALPHA_L 0x00000000
495 #define ALPHA_H 0x00ff0000
498 * MMX optimized version of the RGB565 to RGBA copy routine.
502 .globl _generic_read_RGBA_span_RGB565_MMX
504 .hidden _generic_read_RGBA_span_RGB565_MMX
506 .type _generic_read_RGBA_span_RGB565_MMX, @function
508 _generic_read_RGBA_span_RGB565_MMX:
510 #ifdef USE_INNER_EMMS
514 movl 4(%esp), %eax /* source pointer */
515 movl 8(%esp), %edx /* destination pointer */
516 movl 12(%esp), %ecx /* number of pixels to copy */
533 jl .L01 /* Bail early if the count is negative. */
537 /* Fetch 4 RGB565 pixels into %mm4. Distribute the first and
538 * second pixels into the four words of %mm0 and %mm2.
544 pshufw $0x00, %mm4, %mm0
545 pshufw $0x55, %mm4, %mm2
548 /* Mask the pixels so that each word of each register contains only
549 * one color component.
556 /* Adjust the component values so that they are as small as possible,
557 * but large enough so that we can multiply them by an unsigned 16-bit
558 * number and get a value as large as 0x00ff0000.
564 psrlw $SCALE_ADJUST, %mm0
565 psrlw $SCALE_ADJUST, %mm2
568 /* Scale the input component values to be on the range
569 * [0, 0x00ff0000]. This it the real magic of the whole routine.
576 /* Always set the alpha value to 0xff.
583 /* Pack the 16-bit values to 8-bit values and store the converted
591 pshufw $0xaa, %mm4, %mm0
592 pshufw $0xff, %mm4, %mm2
599 psrlw $SCALE_ADJUST, %mm0
600 psrlw $SCALE_ADJUST, %mm2
618 /* At this point there can be at most 3 pixels left to process. If
619 * there is either 2 or 3 left, process 2.
629 pshufw $0x00, %mm4, %mm0
630 pshufw $0x55, %mm4, %mm2
637 psrlw $SCALE_ADJUST, %mm0
638 psrlw $SCALE_ADJUST, %mm2
652 /* At this point there can be at most 1 pixel left to process.
653 * Process it if needed.
662 pshufw $0x00, %mm4, %mm0
667 psrlw $SCALE_ADJUST, %mm0
678 #ifdef USE_INNER_EMMS
682 #endif /* !defined(__DJGPP__) && !defined(__MINGW32__) && !defined(__APPLE__) */
684 #if defined (__ELF__) && defined (__linux__)
685 .section .note.GNU-stack,"",%progbits