src/video/SDL_blit_A.c
author Ryan C. Gordon
Wed, 04 Apr 2007 09:36:25 +0000
changeset 2101 c4e0afbcf1f6
parent 2086 fffea8d6bf92
child 2120 2c835d58faad
permissions -rw-r--r--
Merge r3005:3006 from branches/SDL-1.2: Alpha blending MMX/3DNow register bug.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include "SDL_video.h"
slouken@0
    25
#include "SDL_blit.h"
slouken@0
    26
slouken@1542
    27
#if SDL_ASSEMBLY_ROUTINES
slouken@1542
    28
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
slouken@1361
    29
#define MMX_ASMBLIT 1
slouken@1542
    30
#define GCC_ASMBLIT 1
slouken@1542
    31
#elif defined(_MSC_VER) && (_MSC_VER >= 1200) && defined(_M_IX86)
slouken@1542
    32
#define MMX_ASMBLIT 1
slouken@1542
    33
#define MSVC_ASMBLIT 1
slouken@880
    34
#endif
slouken@1542
    35
#endif /* SDL_ASSEMBLY_ROUTINES */
slouken@880
    36
slouken@739
    37
/* Function to check the CPU flags */
slouken@739
    38
#include "SDL_cpuinfo.h"
slouken@1542
    39
#if GCC_ASMBLIT
slouken@689
    40
#include "mmx.h"
slouken@1542
    41
#elif MSVC_ASMBLIT
slouken@1542
    42
#include <mmintrin.h>
slouken@1542
    43
#include <mm3dnow.h>
slouken@689
    44
#endif
slouken@689
    45
slouken@0
    46
/* Functions to perform alpha blended blitting */
slouken@0
    47
slouken@0
    48
/* N->1 blending with per-surface alpha */
slouken@1895
    49
static void
slouken@1895
    50
BlitNto1SurfaceAlpha(SDL_BlitInfo * info)
slouken@0
    51
{
slouken@1895
    52
    int width = info->d_width;
slouken@1895
    53
    int height = info->d_height;
slouken@1895
    54
    Uint8 *src = info->s_pixels;
slouken@1895
    55
    int srcskip = info->s_skip;
slouken@1895
    56
    Uint8 *dst = info->d_pixels;
slouken@1895
    57
    int dstskip = info->d_skip;
slouken@1895
    58
    Uint8 *palmap = info->table;
slouken@1895
    59
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
    60
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
    61
    int srcbpp = srcfmt->BytesPerPixel;
slouken@0
    62
slouken@1895
    63
    const unsigned A = srcfmt->alpha;
slouken@0
    64
slouken@1895
    65
    while (height--) {
slouken@1895
    66
	    /* *INDENT-OFF* */
slouken@0
    67
	    DUFFS_LOOP4(
slouken@0
    68
	    {
icculus@1162
    69
		Uint32 Pixel;
slouken@0
    70
		unsigned sR;
slouken@0
    71
		unsigned sG;
slouken@0
    72
		unsigned sB;
slouken@0
    73
		unsigned dR;
slouken@0
    74
		unsigned dG;
slouken@0
    75
		unsigned dB;
icculus@1162
    76
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
slouken@0
    77
		dR = dstfmt->palette->colors[*dst].r;
slouken@0
    78
		dG = dstfmt->palette->colors[*dst].g;
slouken@0
    79
		dB = dstfmt->palette->colors[*dst].b;
slouken@0
    80
		ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
    81
		dR &= 0xff;
slouken@0
    82
		dG &= 0xff;
slouken@0
    83
		dB &= 0xff;
slouken@0
    84
		/* Pack RGB into 8bit pixel */
slouken@0
    85
		if ( palmap == NULL ) {
slouken@0
    86
		    *dst =((dR>>5)<<(3+2))|
slouken@0
    87
			  ((dG>>5)<<(2))|
slouken@0
    88
			  ((dB>>6)<<(0));
slouken@0
    89
		} else {
slouken@0
    90
		    *dst = palmap[((dR>>5)<<(3+2))|
slouken@0
    91
				  ((dG>>5)<<(2))  |
slouken@0
    92
				  ((dB>>6)<<(0))];
slouken@0
    93
		}
slouken@0
    94
		dst++;
slouken@0
    95
		src += srcbpp;
slouken@0
    96
	    },
slouken@0
    97
	    width);
slouken@1895
    98
	    /* *INDENT-ON* */
slouken@1895
    99
        src += srcskip;
slouken@1895
   100
        dst += dstskip;
slouken@1895
   101
    }
slouken@0
   102
}
slouken@0
   103
slouken@0
   104
/* N->1 blending with pixel alpha */
slouken@1895
   105
static void
slouken@1895
   106
BlitNto1PixelAlpha(SDL_BlitInfo * info)
slouken@0
   107
{
slouken@1895
   108
    int width = info->d_width;
slouken@1895
   109
    int height = info->d_height;
slouken@1895
   110
    Uint8 *src = info->s_pixels;
slouken@1895
   111
    int srcskip = info->s_skip;
slouken@1895
   112
    Uint8 *dst = info->d_pixels;
slouken@1895
   113
    int dstskip = info->d_skip;
slouken@1895
   114
    Uint8 *palmap = info->table;
slouken@1895
   115
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
   116
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
   117
    int srcbpp = srcfmt->BytesPerPixel;
slouken@0
   118
slouken@1895
   119
    /* FIXME: fix alpha bit field expansion here too? */
slouken@1895
   120
    while (height--) {
slouken@1895
   121
	    /* *INDENT-OFF* */
slouken@0
   122
	    DUFFS_LOOP4(
slouken@0
   123
	    {
icculus@1162
   124
		Uint32 Pixel;
slouken@0
   125
		unsigned sR;
slouken@0
   126
		unsigned sG;
slouken@0
   127
		unsigned sB;
slouken@0
   128
		unsigned sA;
slouken@0
   129
		unsigned dR;
slouken@0
   130
		unsigned dG;
slouken@0
   131
		unsigned dB;
icculus@1162
   132
		DISEMBLE_RGBA(src,srcbpp,srcfmt,Pixel,sR,sG,sB,sA);
slouken@0
   133
		dR = dstfmt->palette->colors[*dst].r;
slouken@0
   134
		dG = dstfmt->palette->colors[*dst].g;
slouken@0
   135
		dB = dstfmt->palette->colors[*dst].b;
slouken@0
   136
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
   137
		dR &= 0xff;
slouken@0
   138
		dG &= 0xff;
slouken@0
   139
		dB &= 0xff;
slouken@0
   140
		/* Pack RGB into 8bit pixel */
slouken@0
   141
		if ( palmap == NULL ) {
slouken@0
   142
		    *dst =((dR>>5)<<(3+2))|
slouken@0
   143
			  ((dG>>5)<<(2))|
slouken@0
   144
			  ((dB>>6)<<(0));
slouken@0
   145
		} else {
slouken@0
   146
		    *dst = palmap[((dR>>5)<<(3+2))|
slouken@0
   147
				  ((dG>>5)<<(2))  |
slouken@0
   148
				  ((dB>>6)<<(0))  ];
slouken@0
   149
		}
slouken@0
   150
		dst++;
slouken@0
   151
		src += srcbpp;
slouken@0
   152
	    },
slouken@0
   153
	    width);
slouken@1895
   154
	    /* *INDENT-ON* */
slouken@1895
   155
        src += srcskip;
slouken@1895
   156
        dst += dstskip;
slouken@1895
   157
    }
slouken@0
   158
}
slouken@0
   159
slouken@0
   160
/* colorkeyed N->1 blending with per-surface alpha */
slouken@1895
   161
static void
slouken@1895
   162
BlitNto1SurfaceAlphaKey(SDL_BlitInfo * info)
slouken@0
   163
{
slouken@1895
   164
    int width = info->d_width;
slouken@1895
   165
    int height = info->d_height;
slouken@1895
   166
    Uint8 *src = info->s_pixels;
slouken@1895
   167
    int srcskip = info->s_skip;
slouken@1895
   168
    Uint8 *dst = info->d_pixels;
slouken@1895
   169
    int dstskip = info->d_skip;
slouken@1895
   170
    Uint8 *palmap = info->table;
slouken@1895
   171
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
   172
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
   173
    int srcbpp = srcfmt->BytesPerPixel;
slouken@1895
   174
    Uint32 ckey = srcfmt->colorkey;
slouken@0
   175
slouken@1895
   176
    const int A = srcfmt->alpha;
slouken@0
   177
slouken@1895
   178
    while (height--) {
slouken@1895
   179
	    /* *INDENT-OFF* */
slouken@0
   180
	    DUFFS_LOOP(
slouken@0
   181
	    {
icculus@1162
   182
		Uint32 Pixel;
slouken@0
   183
		unsigned sR;
slouken@0
   184
		unsigned sG;
slouken@0
   185
		unsigned sB;
slouken@0
   186
		unsigned dR;
slouken@0
   187
		unsigned dG;
slouken@0
   188
		unsigned dB;
icculus@1162
   189
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
icculus@1162
   190
		if ( Pixel != ckey ) {
slouken@0
   191
		    dR = dstfmt->palette->colors[*dst].r;
slouken@0
   192
		    dG = dstfmt->palette->colors[*dst].g;
slouken@0
   193
		    dB = dstfmt->palette->colors[*dst].b;
slouken@0
   194
		    ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
   195
		    dR &= 0xff;
slouken@0
   196
		    dG &= 0xff;
slouken@0
   197
		    dB &= 0xff;
slouken@0
   198
		    /* Pack RGB into 8bit pixel */
slouken@0
   199
		    if ( palmap == NULL ) {
slouken@0
   200
			*dst =((dR>>5)<<(3+2))|
slouken@0
   201
			      ((dG>>5)<<(2)) |
slouken@0
   202
			      ((dB>>6)<<(0));
slouken@0
   203
		    } else {
slouken@0
   204
			*dst = palmap[((dR>>5)<<(3+2))|
slouken@0
   205
				      ((dG>>5)<<(2))  |
slouken@0
   206
				      ((dB>>6)<<(0))  ];
slouken@0
   207
		    }
slouken@0
   208
		}
slouken@0
   209
		dst++;
slouken@0
   210
		src += srcbpp;
slouken@0
   211
	    },
slouken@0
   212
	    width);
slouken@1895
   213
	    /* *INDENT-ON* */
slouken@1895
   214
        src += srcskip;
slouken@1895
   215
        dst += dstskip;
slouken@1895
   216
    }
slouken@0
   217
}
slouken@0
   218
slouken@1542
   219
#if GCC_ASMBLIT
slouken@689
   220
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1895
   221
static void
slouken@1895
   222
BlitRGBtoRGBSurfaceAlpha128MMX(SDL_BlitInfo * info)
slouken@689
   223
{
slouken@1895
   224
    int width = info->d_width;
slouken@1895
   225
    int height = info->d_height;
slouken@1895
   226
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
   227
    int srcskip = info->s_skip >> 2;
slouken@1895
   228
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
   229
    int dstskip = info->d_skip >> 2;
slouken@1895
   230
    Uint32 dalpha = info->dst->Amask;
slouken@1895
   231
    Uint8 load[8];
slouken@1542
   232
slouken@1895
   233
    *(Uint64 *) load = 0x00fefefe00fefefeULL;   /* alpha128 mask */
slouken@1895
   234
    movq_m2r(*load, mm4);       /* alpha128 mask -> mm4 */
slouken@1895
   235
    *(Uint64 *) load = 0x0001010100010101ULL;   /* !alpha128 mask */
slouken@1895
   236
    movq_m2r(*load, mm3);       /* !alpha128 mask -> mm3 */
slouken@1895
   237
    movd_m2r(dalpha, mm7);      /* dst alpha mask */
slouken@1895
   238
    punpckldq_r2r(mm7, mm7);    /* dst alpha mask | dst alpha mask -> mm7 */
slouken@1895
   239
    while (height--) {
slouken@1895
   240
		/* *INDENT-OFF* */
slouken@1542
   241
		DUFFS_LOOP_DOUBLE2(
slouken@1542
   242
		{
slouken@1542
   243
			Uint32 s = *srcp++;
slouken@1542
   244
			Uint32 d = *dstp;
slouken@1542
   245
			*dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1542
   246
				   + (s & d & 0x00010101)) | dalpha;
slouken@1542
   247
		},{
slouken@1542
   248
			movq_m2r((*dstp), mm2);/* 2 x dst -> mm2(ARGBARGB) */
slouken@1542
   249
			movq_r2r(mm2, mm6); /* 2 x dst -> mm6(ARGBARGB) */
slouken@1542
   250
slouken@1542
   251
			movq_m2r((*srcp), mm1);/* 2 x src -> mm1(ARGBARGB) */
slouken@1542
   252
			movq_r2r(mm1, mm5); /* 2 x src -> mm5(ARGBARGB) */
slouken@1542
   253
slouken@1542
   254
			pand_r2r(mm4, mm6); /* dst & mask -> mm6 */
slouken@1542
   255
			pand_r2r(mm4, mm5); /* src & mask -> mm5 */
slouken@1542
   256
			paddd_r2r(mm6, mm5); /* mm6 + mm5 -> mm5 */
slouken@1542
   257
			pand_r2r(mm1, mm2); /* src & dst -> mm2 */
slouken@1542
   258
			psrld_i2r(1, mm5); /* mm5 >> 1 -> mm5 */
slouken@1542
   259
			pand_r2r(mm3, mm2); /* mm2 & !mask -> mm2 */
slouken@1542
   260
			paddd_r2r(mm5, mm2); /* mm5 + mm2 -> mm2 */
slouken@1542
   261
			
slouken@1542
   262
			por_r2r(mm7, mm2); /* mm7(full alpha) | mm2 -> mm2 */
slouken@1542
   263
			movq_r2m(mm2, (*dstp));/* mm2 -> 2 x dst pixels */
slouken@1542
   264
			dstp += 2;
slouken@1542
   265
			srcp += 2;
slouken@1542
   266
		}, width);
slouken@1895
   267
		/* *INDENT-ON* */
slouken@1895
   268
        srcp += srcskip;
slouken@1895
   269
        dstp += dstskip;
slouken@1895
   270
    }
slouken@1895
   271
    emms();
slouken@689
   272
}
slouken@689
   273
slouken@689
   274
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1895
   275
static void
slouken@1895
   276
BlitRGBtoRGBSurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@689
   277
{
slouken@1895
   278
    SDL_PixelFormat *df = info->dst;
slouken@1895
   279
    unsigned alpha = info->src->alpha;
slouken@1895
   280
slouken@1895
   281
    if (alpha == 128 && (df->Rmask | df->Gmask | df->Bmask) == 0x00FFFFFF) {
slouken@1895
   282
        /* only call a128 version when R,G,B occupy lower bits */
slouken@1895
   283
        BlitRGBtoRGBSurfaceAlpha128MMX(info);
slouken@1895
   284
    } else {
slouken@1895
   285
        int width = info->d_width;
slouken@1895
   286
        int height = info->d_height;
slouken@1895
   287
        Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
   288
        int srcskip = info->s_skip >> 2;
slouken@1895
   289
        Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
   290
        int dstskip = info->d_skip >> 2;
slouken@1542
   291
slouken@1895
   292
        pxor_r2r(mm5, mm5);     /* 0 -> mm5 */
slouken@1895
   293
        /* form the alpha mult */
slouken@1895
   294
        movd_m2r(alpha, mm4);   /* 0000000A -> mm4 */
slouken@1895
   295
        punpcklwd_r2r(mm4, mm4);        /* 00000A0A -> mm4 */
slouken@1895
   296
        punpckldq_r2r(mm4, mm4);        /* 0A0A0A0A -> mm4 */
slouken@1895
   297
        alpha =
slouken@1895
   298
            (0xff << df->Rshift) | (0xff << df->Gshift) | (0xff << df->
slouken@1895
   299
                                                           Bshift);
slouken@1895
   300
        movd_m2r(alpha, mm0);   /* 00000FFF -> mm0 */
slouken@1895
   301
        punpcklbw_r2r(mm0, mm0);        /* 00FFFFFF -> mm0 */
slouken@1895
   302
        pand_r2r(mm0, mm4);     /* 0A0A0A0A -> mm4, minus 1 chan */
slouken@1895
   303
        /* at this point mm4 can be 000A0A0A or 0A0A0A00 or another combo */
slouken@1895
   304
        movd_m2r(df->Amask, mm7);       /* dst alpha mask */
slouken@1895
   305
        punpckldq_r2r(mm7, mm7);        /* dst alpha mask | dst alpha mask -> mm7 */
slouken@1542
   306
slouken@1895
   307
        while (height--) {
slouken@1895
   308
			/* *INDENT-OFF* */
slouken@689
   309
			DUFFS_LOOP_DOUBLE2({
slouken@689
   310
				/* One Pixel Blend */
slouken@1542
   311
				movd_m2r((*srcp), mm1);/* src(ARGB) -> mm1 (0000ARGB)*/
slouken@1542
   312
				movd_m2r((*dstp), mm2);/* dst(ARGB) -> mm2 (0000ARGB)*/
slouken@1542
   313
				punpcklbw_r2r(mm5, mm1); /* 0A0R0G0B -> mm1(src) */
slouken@1542
   314
				punpcklbw_r2r(mm5, mm2); /* 0A0R0G0B -> mm2(dst) */
slouken@1542
   315
slouken@1542
   316
				psubw_r2r(mm2, mm1);/* src - dst -> mm1 */
slouken@1542
   317
				pmullw_r2r(mm4, mm1); /* mm1 * alpha -> mm1 */
slouken@1542
   318
				psrlw_i2r(8, mm1); /* mm1 >> 8 -> mm1 */
slouken@1542
   319
				paddb_r2r(mm1, mm2); /* mm1 + mm2(dst) -> mm2 */
slouken@1542
   320
slouken@1542
   321
				packuswb_r2r(mm5, mm2);  /* ARGBARGB -> mm2 */
slouken@1542
   322
				por_r2r(mm7, mm2); /* mm7(full alpha) | mm2 -> mm2 */
slouken@1542
   323
				movd_r2m(mm2, *dstp);/* mm2 -> pixel */
slouken@689
   324
				++srcp;
slouken@689
   325
				++dstp;
slouken@689
   326
			},{
slouken@1542
   327
				/* Two Pixels Blend */
slouken@689
   328
				movq_m2r((*srcp), mm0);/* 2 x src -> mm0(ARGBARGB)*/
slouken@1542
   329
				movq_m2r((*dstp), mm2);/* 2 x dst -> mm2(ARGBARGB) */
slouken@1542
   330
				movq_r2r(mm0, mm1); /* 2 x src -> mm1(ARGBARGB) */
slouken@1542
   331
				movq_r2r(mm2, mm6); /* 2 x dst -> mm6(ARGBARGB) */
slouken@1542
   332
slouken@1542
   333
				punpcklbw_r2r(mm5, mm0); /* low - 0A0R0G0B -> mm0(src1) */
slouken@1542
   334
				punpckhbw_r2r(mm5, mm1); /* high - 0A0R0G0B -> mm1(src2) */
slouken@1542
   335
				punpcklbw_r2r(mm5, mm2); /* low - 0A0R0G0B -> mm2(dst1) */
slouken@1542
   336
				punpckhbw_r2r(mm5, mm6); /* high - 0A0R0G0B -> mm6(dst2) */
slouken@1542
   337
slouken@1542
   338
				psubw_r2r(mm2, mm0);/* src1 - dst1 -> mm0 */
slouken@1542
   339
				pmullw_r2r(mm4, mm0); /* mm0 * alpha -> mm0 */
slouken@1542
   340
				psrlw_i2r(8, mm0); /* mm0 >> 8 -> mm1 */
slouken@1542
   341
				paddb_r2r(mm0, mm2); /* mm0 + mm2(dst1) -> mm2 */
slouken@1542
   342
slouken@1542
   343
				psubw_r2r(mm6, mm1);/* src2 - dst2 -> mm1 */
slouken@1542
   344
				pmullw_r2r(mm4, mm1); /* mm1 * alpha -> mm1 */
slouken@1542
   345
				psrlw_i2r(8, mm1); /* mm1 >> 8 -> mm1 */
slouken@1542
   346
				paddb_r2r(mm1, mm6); /* mm1 + mm6(dst2) -> mm6 */
slouken@1542
   347
slouken@1542
   348
				packuswb_r2r(mm6, mm2);  /* ARGBARGB -> mm2 */
slouken@1542
   349
				por_r2r(mm7, mm2); /* mm7(dst alpha) | mm2 -> mm2 */
slouken@1542
   350
				
slouken@1542
   351
				movq_r2m(mm2, *dstp);/* mm2 -> 2 x pixel */
slouken@1542
   352
slouken@1542
   353
  				srcp += 2;
slouken@1542
   354
  				dstp += 2;
slouken@1542
   355
  			}, width);
slouken@1895
   356
			/* *INDENT-ON* */
slouken@1895
   357
            srcp += srcskip;
slouken@1895
   358
            dstp += dstskip;
slouken@1895
   359
        }
slouken@1895
   360
        emms();
slouken@1895
   361
    }
slouken@689
   362
}
slouken@689
   363
slouken@689
   364
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
   365
static void
slouken@1895
   366
BlitRGBtoRGBPixelAlphaMMX(SDL_BlitInfo * info)
slouken@689
   367
{
slouken@1895
   368
    int width = info->d_width;
slouken@1895
   369
    int height = info->d_height;
slouken@1895
   370
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
   371
    int srcskip = info->s_skip >> 2;
slouken@1895
   372
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
   373
    int dstskip = info->d_skip >> 2;
slouken@1895
   374
    SDL_PixelFormat *sf = info->src;
slouken@1895
   375
    Uint32 amask = sf->Amask;
slouken@1542
   376
slouken@1895
   377
    pxor_r2r(mm6, mm6);         /* 0 -> mm6 */
slouken@1895
   378
    /* form multiplication mask */
slouken@1895
   379
    movd_m2r(sf->Amask, mm7);   /* 0000F000 -> mm7 */
slouken@1895
   380
    punpcklbw_r2r(mm7, mm7);    /* FF000000 -> mm7 */
slouken@1895
   381
    pcmpeqb_r2r(mm0, mm0);      /* FFFFFFFF -> mm0 */
slouken@1895
   382
    movq_r2r(mm0, mm3);         /* FFFFFFFF -> mm3 (for later) */
slouken@1895
   383
    pxor_r2r(mm0, mm7);         /* 00FFFFFF -> mm7 (mult mask) */
slouken@1895
   384
    /* form channel masks */
slouken@1895
   385
    movq_r2r(mm7, mm0);         /* 00FFFFFF -> mm0 */
slouken@1895
   386
    packsswb_r2r(mm6, mm0);     /* 00000FFF -> mm0 (channel mask) */
slouken@1895
   387
    packsswb_r2r(mm6, mm3);     /* 0000FFFF -> mm3 */
slouken@1895
   388
    pxor_r2r(mm0, mm3);         /* 0000F000 -> mm3 (~channel mask) */
slouken@1895
   389
    /* get alpha channel shift */
icculus@2101
   390
    __asm__ __volatile__ (
icculus@2101
   391
        "movd %0, %%mm5"
icculus@2101
   392
        : : "rm" ((Uint32) sf->Ashift) ); /* Ashift -> mm5 */
slouken@1542
   393
slouken@1895
   394
    while (height--) {
slouken@1895
   395
	    /* *INDENT-OFF* */
slouken@689
   396
	    DUFFS_LOOP4({
slouken@1542
   397
		Uint32 alpha = *srcp & amask;
slouken@689
   398
		/* FIXME: Here we special-case opaque alpha since the
slouken@1542
   399
			compositioning used (>>8 instead of /255) doesn't handle
slouken@1542
   400
			it correctly. Also special-case alpha=0 for speed?
slouken@1542
   401
			Benchmark this! */
slouken@1542
   402
		if(alpha == 0) {
slouken@1542
   403
			/* do nothing */
slouken@1542
   404
		} else if(alpha == amask) {
slouken@1542
   405
			/* opaque alpha -- copy RGB, keep dst alpha */
slouken@1542
   406
			/* using MMX here to free up regular registers for other things */
slouken@1542
   407
			movd_m2r((*srcp), mm1);/* src(ARGB) -> mm1 (0000ARGB)*/
slouken@1542
   408
			movd_m2r((*dstp), mm2);/* dst(ARGB) -> mm2 (0000ARGB)*/
slouken@1542
   409
			pand_r2r(mm0, mm1); /* src & chanmask -> mm1 */
slouken@1542
   410
			pand_r2r(mm3, mm2); /* dst & ~chanmask -> mm2 */
slouken@1542
   411
			por_r2r(mm1, mm2); /* src | dst -> mm2 */
slouken@1542
   412
			movd_r2m(mm2, (*dstp)); /* mm2 -> dst */
slouken@1542
   413
		} else {
slouken@1542
   414
			movd_m2r((*srcp), mm1);/* src(ARGB) -> mm1 (0000ARGB)*/
slouken@1542
   415
			punpcklbw_r2r(mm6, mm1); /* 0A0R0G0B -> mm1 */
slouken@1542
   416
slouken@1542
   417
			movd_m2r((*dstp), mm2);/* dst(ARGB) -> mm2 (0000ARGB)*/
slouken@1542
   418
			punpcklbw_r2r(mm6, mm2); /* 0A0R0G0B -> mm2 */
slouken@1542
   419
slouken@1542
   420
			__asm__ __volatile__ (
slouken@1542
   421
				"movd %0, %%mm4"
slouken@1542
   422
				: : "r" (alpha) ); /* 0000A000 -> mm4 */
slouken@1542
   423
			psrld_r2r(mm5, mm4); /* mm4 >> mm5 -> mm4 (0000000A) */
slouken@1542
   424
			punpcklwd_r2r(mm4, mm4); /* 00000A0A -> mm4 */
slouken@1542
   425
			punpcklwd_r2r(mm4, mm4); /* 0A0A0A0A -> mm4 */
slouken@1542
   426
			pand_r2r(mm7, mm4); /* 000A0A0A -> mm4, preserve dst alpha on add */
slouken@1542
   427
slouken@1542
   428
			/* blend */		    
slouken@1542
   429
			psubw_r2r(mm2, mm1);/* src - dst -> mm1 */
slouken@1542
   430
			pmullw_r2r(mm4, mm1); /* mm1 * alpha -> mm1 */
slouken@1542
   431
			psrlw_i2r(8, mm1); /* mm1 >> 8 -> mm1(000R0G0B) */
slouken@1542
   432
			paddb_r2r(mm1, mm2); /* mm1 + mm2(dst) -> mm2 */
slouken@1542
   433
			
slouken@1542
   434
			packuswb_r2r(mm6, mm2);  /* 0000ARGB -> mm2 */
slouken@1542
   435
			movd_r2m(mm2, *dstp);/* mm2 -> dst */
slouken@689
   436
		}
slouken@689
   437
		++srcp;
slouken@689
   438
		++dstp;
slouken@689
   439
	    }, width);
slouken@1895
   440
	    /* *INDENT-ON* */
slouken@1895
   441
        srcp += srcskip;
slouken@1895
   442
        dstp += dstskip;
slouken@1895
   443
    }
slouken@1895
   444
    emms();
slouken@689
   445
}
slouken@1895
   446
slouken@1542
   447
/* End GCC_ASMBLIT */
slouken@1542
   448
slouken@1542
   449
#elif MSVC_ASMBLIT
slouken@1542
   450
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1895
   451
static void
slouken@1895
   452
BlitRGBtoRGBSurfaceAlpha128MMX(SDL_BlitInfo * info)
slouken@1542
   453
{
slouken@1895
   454
    int width = info->d_width;
slouken@1895
   455
    int height = info->d_height;
slouken@1895
   456
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
   457
    int srcskip = info->s_skip >> 2;
slouken@1895
   458
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
   459
    int dstskip = info->d_skip >> 2;
slouken@1895
   460
    Uint32 dalpha = info->dst->Amask;
slouken@1895
   461
slouken@1895
   462
    __m64 src1, src2, dst1, dst2, lmask, hmask, dsta;
slouken@1542
   463
slouken@1895
   464
    hmask = _mm_set_pi32(0x00fefefe, 0x00fefefe);       /* alpha128 mask -> hmask */
slouken@1895
   465
    lmask = _mm_set_pi32(0x00010101, 0x00010101);       /* !alpha128 mask -> lmask */
slouken@1895
   466
    dsta = _mm_set_pi32(dalpha, dalpha);        /* dst alpha mask -> dsta */
slouken@1895
   467
slouken@1895
   468
    while (height--) {
slouken@1895
   469
        int n = width;
slouken@1895
   470
        if (n & 1) {
slouken@1895
   471
            Uint32 s = *srcp++;
slouken@1895
   472
            Uint32 d = *dstp;
slouken@1895
   473
            *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1895
   474
                       + (s & d & 0x00010101)) | dalpha;
slouken@1895
   475
            n--;
slouken@1895
   476
        }
slouken@1542
   477
slouken@1895
   478
        for (n >>= 1; n > 0; --n) {
slouken@1895
   479
            dst1 = *(__m64 *) dstp;     /* 2 x dst -> dst1(ARGBARGB) */
slouken@1895
   480
            dst2 = dst1;        /* 2 x dst -> dst2(ARGBARGB) */
slouken@1542
   481
slouken@1895
   482
            src1 = *(__m64 *) srcp;     /* 2 x src -> src1(ARGBARGB) */
slouken@1895
   483
            src2 = src1;        /* 2 x src -> src2(ARGBARGB) */
slouken@1895
   484
slouken@1895
   485
            dst2 = _mm_and_si64(dst2, hmask);   /* dst & mask -> dst2 */
slouken@1895
   486
            src2 = _mm_and_si64(src2, hmask);   /* src & mask -> src2 */
slouken@1895
   487
            src2 = _mm_add_pi32(src2, dst2);    /* dst2 + src2 -> src2 */
slouken@1895
   488
            src2 = _mm_srli_pi32(src2, 1);      /* src2 >> 1 -> src2 */
slouken@1542
   489
slouken@1895
   490
            dst1 = _mm_and_si64(dst1, src1);    /* src & dst -> dst1 */
slouken@1895
   491
            dst1 = _mm_and_si64(dst1, lmask);   /* dst1 & !mask -> dst1 */
slouken@1895
   492
            dst1 = _mm_add_pi32(dst1, src2);    /* src2 + dst1 -> dst1 */
slouken@1895
   493
            dst1 = _mm_or_si64(dst1, dsta);     /* dsta(full alpha) | dst1 -> dst1 */
slouken@1542
   494
slouken@1895
   495
            *(__m64 *) dstp = dst1;     /* dst1 -> 2 x dst pixels */
slouken@1895
   496
            dstp += 2;
slouken@1895
   497
            srcp += 2;
slouken@1895
   498
        }
slouken@1895
   499
slouken@1895
   500
        srcp += srcskip;
slouken@1895
   501
        dstp += dstskip;
slouken@1895
   502
    }
slouken@1895
   503
    _mm_empty();
slouken@1542
   504
}
slouken@1542
   505
slouken@1542
   506
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1895
   507
static void
slouken@1895
   508
BlitRGBtoRGBSurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@1542
   509
{
slouken@1895
   510
    SDL_PixelFormat *df = info->dst;
slouken@1895
   511
    Uint32 chanmask = df->Rmask | df->Gmask | df->Bmask;
slouken@1895
   512
    unsigned alpha = info->src->alpha;
slouken@1542
   513
slouken@1895
   514
    if (alpha == 128 && (df->Rmask | df->Gmask | df->Bmask) == 0x00FFFFFF) {
slouken@1895
   515
        /* only call a128 version when R,G,B occupy lower bits */
slouken@1895
   516
        BlitRGBtoRGBSurfaceAlpha128MMX(info);
slouken@1895
   517
    } else {
slouken@1895
   518
        int width = info->d_width;
slouken@1895
   519
        int height = info->d_height;
slouken@1895
   520
        Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
   521
        int srcskip = info->s_skip >> 2;
slouken@1895
   522
        Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
   523
        int dstskip = info->d_skip >> 2;
slouken@1895
   524
        Uint32 dalpha = df->Amask;
slouken@1895
   525
        Uint32 amult;
slouken@1895
   526
slouken@1895
   527
        __m64 src1, src2, dst1, dst2, mm_alpha, mm_zero, dsta;
slouken@1542
   528
slouken@1895
   529
        mm_zero = _mm_setzero_si64();   /* 0 -> mm_zero */
slouken@1895
   530
        /* form the alpha mult */
slouken@1895
   531
        amult = alpha | (alpha << 8);
slouken@1895
   532
        amult = amult | (amult << 16);
slouken@1895
   533
        chanmask =
slouken@1895
   534
            (0xff << df->Rshift) | (0xff << df->Gshift) | (0xff << df->
slouken@1895
   535
                                                           Bshift);
slouken@1895
   536
        mm_alpha = _mm_set_pi32(0, amult & chanmask);   /* 0000AAAA -> mm_alpha, minus 1 chan */
slouken@1895
   537
        mm_alpha = _mm_unpacklo_pi8(mm_alpha, mm_zero); /* 0A0A0A0A -> mm_alpha, minus 1 chan */
slouken@1895
   538
        /* at this point mm_alpha can be 000A0A0A or 0A0A0A00 or another combo */
slouken@1895
   539
        dsta = _mm_set_pi32(dalpha, dalpha);    /* dst alpha mask -> dsta */
slouken@1542
   540
slouken@1895
   541
        while (height--) {
slouken@1895
   542
            int n = width;
slouken@1895
   543
            if (n & 1) {
slouken@1895
   544
                /* One Pixel Blend */
slouken@1895
   545
                src2 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src2 (0000ARGB) */
slouken@1895
   546
                src2 = _mm_unpacklo_pi8(src2, mm_zero); /* 0A0R0G0B -> src2 */
slouken@1895
   547
slouken@1895
   548
                dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB) */
slouken@1895
   549
                dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
   550
slouken@1895
   551
                src2 = _mm_sub_pi16(src2, dst1);        /* src2 - dst2 -> src2 */
slouken@1895
   552
                src2 = _mm_mullo_pi16(src2, mm_alpha);  /* src2 * alpha -> src2 */
slouken@1895
   553
                src2 = _mm_srli_pi16(src2, 8);  /* src2 >> 8 -> src2 */
slouken@1895
   554
                dst1 = _mm_add_pi8(src2, dst1); /* src2 + dst1 -> dst1 */
slouken@1895
   555
slouken@1895
   556
                dst1 = _mm_packs_pu16(dst1, mm_zero);   /* 0000ARGB -> dst1 */
slouken@1895
   557
                dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
slouken@1895
   558
                *dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
   559
slouken@1895
   560
                ++srcp;
slouken@1895
   561
                ++dstp;
slouken@1895
   562
slouken@1895
   563
                n--;
slouken@1895
   564
            }
slouken@1542
   565
slouken@1895
   566
            for (n >>= 1; n > 0; --n) {
slouken@1895
   567
                /* Two Pixels Blend */
slouken@1895
   568
                src1 = *(__m64 *) srcp; /* 2 x src -> src1(ARGBARGB) */
slouken@1895
   569
                src2 = src1;    /* 2 x src -> src2(ARGBARGB) */
slouken@1895
   570
                src1 = _mm_unpacklo_pi8(src1, mm_zero); /* low - 0A0R0G0B -> src1 */
slouken@1895
   571
                src2 = _mm_unpackhi_pi8(src2, mm_zero); /* high - 0A0R0G0B -> src2 */
slouken@1542
   572
slouken@1895
   573
                dst1 = *(__m64 *) dstp; /* 2 x dst -> dst1(ARGBARGB) */
slouken@1895
   574
                dst2 = dst1;    /* 2 x dst -> dst2(ARGBARGB) */
slouken@1895
   575
                dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* low - 0A0R0G0B -> dst1 */
slouken@1895
   576
                dst2 = _mm_unpackhi_pi8(dst2, mm_zero); /* high - 0A0R0G0B -> dst2 */
slouken@1542
   577
slouken@1895
   578
                src1 = _mm_sub_pi16(src1, dst1);        /* src1 - dst1 -> src1 */
slouken@1895
   579
                src1 = _mm_mullo_pi16(src1, mm_alpha);  /* src1 * alpha -> src1 */
slouken@1895
   580
                src1 = _mm_srli_pi16(src1, 8);  /* src1 >> 8 -> src1 */
slouken@1895
   581
                dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst1) -> dst1 */
slouken@1542
   582
slouken@1895
   583
                src2 = _mm_sub_pi16(src2, dst2);        /* src2 - dst2 -> src2 */
slouken@1895
   584
                src2 = _mm_mullo_pi16(src2, mm_alpha);  /* src2 * alpha -> src2 */
slouken@1895
   585
                src2 = _mm_srli_pi16(src2, 8);  /* src2 >> 8 -> src2 */
slouken@1895
   586
                dst2 = _mm_add_pi8(src2, dst2); /* src2 + dst2(dst2) -> dst2 */
slouken@1895
   587
slouken@1895
   588
                dst1 = _mm_packs_pu16(dst1, dst2);      /* 0A0R0G0B(res1), 0A0R0G0B(res2) -> dst1(ARGBARGB) */
slouken@1895
   589
                dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
slouken@1542
   590
slouken@1895
   591
                *(__m64 *) dstp = dst1; /* dst1 -> 2 x pixel */
slouken@1542
   592
slouken@1895
   593
                srcp += 2;
slouken@1895
   594
                dstp += 2;
slouken@1895
   595
            }
slouken@1895
   596
            srcp += srcskip;
slouken@1895
   597
            dstp += dstskip;
slouken@1895
   598
        }
slouken@1895
   599
        _mm_empty();
slouken@1895
   600
    }
slouken@1542
   601
}
slouken@1542
   602
slouken@1542
   603
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
   604
static void
slouken@1895
   605
BlitRGBtoRGBPixelAlphaMMX(SDL_BlitInfo * info)
slouken@1542
   606
{
slouken@1895
   607
    int width = info->d_width;
slouken@1895
   608
    int height = info->d_height;
slouken@1895
   609
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
   610
    int srcskip = info->s_skip >> 2;
slouken@1895
   611
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
   612
    int dstskip = info->d_skip >> 2;
slouken@1895
   613
    SDL_PixelFormat *sf = info->src;
slouken@1895
   614
    Uint32 chanmask = sf->Rmask | sf->Gmask | sf->Bmask;
slouken@1895
   615
    Uint32 amask = sf->Amask;
slouken@1895
   616
    Uint32 ashift = sf->Ashift;
slouken@1895
   617
    Uint64 multmask;
slouken@1542
   618
slouken@1895
   619
    __m64 src1, dst1, mm_alpha, mm_zero, dmask;
slouken@1542
   620
slouken@1895
   621
    mm_zero = _mm_setzero_si64();       /* 0 -> mm_zero */
slouken@1895
   622
	/* *INDENT-OFF* */
slouken@1895
   623
	multmask = ~(0xFFFFI64 << (ashift * 2));
slouken@1895
   624
	/* *INDENT-ON* */
slouken@1895
   625
    dmask = *(__m64 *) & multmask;      /* dst alpha mask -> dmask */
slouken@1542
   626
slouken@1895
   627
    while (height--) {
slouken@1895
   628
		/* *INDENT-OFF* */
slouken@1542
   629
		DUFFS_LOOP4({
slouken@1542
   630
		Uint32 alpha = *srcp & amask;
slouken@1542
   631
		if (alpha == 0) {
slouken@1542
   632
			/* do nothing */
slouken@1542
   633
		} else if (alpha == amask) {
slouken@1542
   634
			/* opaque alpha -- copy RGB, keep dst alpha */
slouken@1542
   635
			*dstp = (*srcp & chanmask) | (*dstp & ~chanmask);
slouken@1542
   636
		} else {
slouken@1542
   637
			src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB)*/
slouken@1542
   638
			src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
slouken@1542
   639
slouken@1542
   640
			dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
slouken@1542
   641
			dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
   642
slouken@1542
   643
			mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
slouken@1542
   644
			mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
slouken@1542
   645
			mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
   646
			mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
   647
			mm_alpha = _mm_and_si64(mm_alpha, dmask); /* 000A0A0A -> mm_alpha, preserve dst alpha on add */
slouken@1542
   648
slouken@1542
   649
			/* blend */		    
slouken@1542
   650
			src1 = _mm_sub_pi16(src1, dst1);/* src1 - dst1 -> src1 */
slouken@1542
   651
			src1 = _mm_mullo_pi16(src1, mm_alpha); /* (src1 - dst1) * alpha -> src1 */
slouken@1542
   652
			src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1(000R0G0B) */
slouken@1542
   653
			dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1 -> dst1(0A0R0G0B) */
slouken@1542
   654
			dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
slouken@1542
   655
			
slouken@1542
   656
			*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
   657
		}
slouken@1542
   658
		++srcp;
slouken@1542
   659
		++dstp;
slouken@1542
   660
	    }, width);
slouken@1895
   661
		/* *INDENT-ON* */
slouken@1895
   662
        srcp += srcskip;
slouken@1895
   663
        dstp += dstskip;
slouken@1895
   664
    }
slouken@1895
   665
    _mm_empty();
slouken@1542
   666
}
slouken@1895
   667
slouken@1542
   668
/* End MSVC_ASMBLIT */
slouken@1542
   669
slouken@1542
   670
#endif /* GCC_ASMBLIT, MSVC_ASMBLIT */
slouken@689
   671
slouken@1361
   672
#if SDL_ALTIVEC_BLITTERS
slouken@1795
   673
#if __MWERKS__
slouken@1795
   674
#pragma altivec_model on
slouken@1795
   675
#endif
slouken@1361
   676
#if HAVE_ALTIVEC_H
icculus@1162
   677
#include <altivec.h>
icculus@1175
   678
#endif
icculus@1047
   679
#include <assert.h>
icculus@1162
   680
slouken@1402
   681
#if (defined(__MACOSX__) && (__GNUC__ < 4))
slouken@1895
   682
#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
icculus@1162
   683
        (vector unsigned char) ( a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p )
slouken@1895
   684
#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
icculus@1162
   685
        (vector unsigned short) ( a,b,c,d,e,f,g,h )
icculus@1162
   686
#else
slouken@1895
   687
#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
icculus@1162
   688
        (vector unsigned char) { a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p }
slouken@1895
   689
#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
icculus@1162
   690
        (vector unsigned short) { a,b,c,d,e,f,g,h }
icculus@1162
   691
#endif
icculus@1162
   692
icculus@1047
   693
#define UNALIGNED_PTR(x) (((size_t) x) & 0x0000000F)
icculus@1047
   694
#define VECPRINT(msg, v) do { \
icculus@1047
   695
    vector unsigned int tmpvec = (vector unsigned int)(v); \
icculus@1047
   696
    unsigned int *vp = (unsigned int *)&tmpvec; \
icculus@1047
   697
    printf("%s = %08X %08X %08X %08X\n", msg, vp[0], vp[1], vp[2], vp[3]); \
icculus@1047
   698
} while (0)
icculus@1047
   699
icculus@1047
   700
/* the permuation vector that takes the high bytes out of all the appropriate shorts 
icculus@1047
   701
    (vector unsigned char)(
icculus@1047
   702
        0x00, 0x10, 0x02, 0x12,
icculus@1047
   703
        0x04, 0x14, 0x06, 0x16,
icculus@1047
   704
        0x08, 0x18, 0x0A, 0x1A,
icculus@1047
   705
        0x0C, 0x1C, 0x0E, 0x1E );
icculus@1047
   706
*/
icculus@1047
   707
#define VEC_MERGE_PERMUTE() (vec_add(vec_lvsl(0, (int*)NULL), (vector unsigned char)vec_splat_u16(0x0F)))
icculus@1047
   708
#define VEC_U32_24() (vec_add(vec_splat_u32(12), vec_splat_u32(12)))
icculus@1047
   709
#define VEC_ALPHA_MASK() ((vector unsigned char)vec_sl((vector unsigned int)vec_splat_s8(-1), VEC_U32_24()))
icculus@1047
   710
#define VEC_ALIGNER(src) ((UNALIGNED_PTR(src)) \
icculus@1047
   711
    ? vec_lvsl(0, src) \
icculus@1047
   712
    : vec_add(vec_lvsl(8, src), vec_splat_u8(8)))
icculus@1047
   713
slouken@1895
   714
icculus@1047
   715
#define VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1_16, v8_16) do { \
icculus@1047
   716
    /* vtemp1 contains source AAGGAAGGAAGGAAGG */ \
icculus@1047
   717
    vector unsigned short vtemp1 = vec_mule(vs, valpha); \
icculus@1047
   718
    /* vtemp2 contains source RRBBRRBBRRBBRRBB */ \
icculus@1047
   719
    vector unsigned short vtemp2 = vec_mulo(vs, valpha); \
icculus@1047
   720
    /* valpha2 is 255-alpha */ \
icculus@1047
   721
    vector unsigned char valpha2 = vec_nor(valpha, valpha); \
icculus@1047
   722
    /* vtemp3 contains dest AAGGAAGGAAGGAAGG */ \
icculus@1047
   723
    vector unsigned short vtemp3 = vec_mule(vd, valpha2); \
icculus@1047
   724
    /* vtemp4 contains dest RRBBRRBBRRBBRRBB */ \
icculus@1047
   725
    vector unsigned short vtemp4 = vec_mulo(vd, valpha2); \
icculus@1047
   726
    /* add source and dest */ \
icculus@1047
   727
    vtemp1 = vec_add(vtemp1, vtemp3); \
icculus@1047
   728
    vtemp2 = vec_add(vtemp2, vtemp4); \
icculus@1047
   729
    /* vtemp1 = (vtemp1 + 1) + ((vtemp1 + 1) >> 8) */ \
icculus@1047
   730
    vtemp1 = vec_add(vtemp1, v1_16); \
icculus@1047
   731
    vtemp3 = vec_sr(vtemp1, v8_16); \
icculus@1047
   732
    vtemp1 = vec_add(vtemp1, vtemp3); \
icculus@1047
   733
    /* vtemp2 = (vtemp2 + 1) + ((vtemp2 + 1) >> 8) */ \
icculus@1047
   734
    vtemp2 = vec_add(vtemp2, v1_16); \
icculus@1047
   735
    vtemp4 = vec_sr(vtemp2, v8_16); \
icculus@1047
   736
    vtemp2 = vec_add(vtemp2, vtemp4); \
icculus@1047
   737
    /* (>>8) and get ARGBARGBARGBARGB */ \
icculus@1047
   738
    vd = (vector unsigned char)vec_perm(vtemp1, vtemp2, mergePermute); \
icculus@1047
   739
} while (0)
slouken@1895
   740
icculus@1047
   741
/* Calculate the permute vector used for 32->32 swizzling */
slouken@1895
   742
static vector unsigned char
slouken@1895
   743
calc_swizzle32(const SDL_PixelFormat * srcfmt, const SDL_PixelFormat * dstfmt)
icculus@1047
   744
{
icculus@1047
   745
    /*
icculus@1047
   746
     * We have to assume that the bits that aren't used by other
icculus@1047
   747
     *  colors is alpha, and it's one complete byte, since some formats
icculus@1047
   748
     *  leave alpha with a zero mask, but we should still swizzle the bits.
icculus@1047
   749
     */
icculus@1047
   750
    /* ARGB */
icculus@1047
   751
    const static struct SDL_PixelFormat default_pixel_format = {
icculus@1047
   752
        NULL, 0, 0,
icculus@1047
   753
        0, 0, 0, 0,
icculus@1047
   754
        16, 8, 0, 24,
icculus@1047
   755
        0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
slouken@1895
   756
        0, 0
slouken@1895
   757
    };
icculus@1047
   758
    if (!srcfmt) {
icculus@1047
   759
        srcfmt = &default_pixel_format;
icculus@1047
   760
    }
icculus@1047
   761
    if (!dstfmt) {
icculus@1047
   762
        dstfmt = &default_pixel_format;
icculus@1047
   763
    }
slouken@1895
   764
    const vector unsigned char plus = VECUINT8_LITERAL(0x00, 0x00, 0x00, 0x00,
slouken@1895
   765
                                                       0x04, 0x04, 0x04, 0x04,
slouken@1895
   766
                                                       0x08, 0x08, 0x08, 0x08,
slouken@1895
   767
                                                       0x0C, 0x0C, 0x0C,
slouken@1895
   768
                                                       0x0C);
icculus@1047
   769
    vector unsigned char vswiz;
icculus@1047
   770
    vector unsigned int srcvec;
icculus@1047
   771
#define RESHIFT(X) (3 - ((X) >> 3))
icculus@1047
   772
    Uint32 rmask = RESHIFT(srcfmt->Rshift) << (dstfmt->Rshift);
icculus@1047
   773
    Uint32 gmask = RESHIFT(srcfmt->Gshift) << (dstfmt->Gshift);
icculus@1047
   774
    Uint32 bmask = RESHIFT(srcfmt->Bshift) << (dstfmt->Bshift);
icculus@1047
   775
    Uint32 amask;
icculus@1047
   776
    /* Use zero for alpha if either surface doesn't have alpha */
icculus@1047
   777
    if (dstfmt->Amask) {
slouken@1895
   778
        amask =
slouken@1895
   779
            ((srcfmt->Amask) ? RESHIFT(srcfmt->Ashift) : 0x10) << (dstfmt->
slouken@1895
   780
                                                                   Ashift);
icculus@1047
   781
    } else {
slouken@1895
   782
        amask =
slouken@1895
   783
            0x10101010 & ((dstfmt->Rmask | dstfmt->Gmask | dstfmt->Bmask) ^
slouken@1895
   784
                          0xFFFFFFFF);
icculus@1047
   785
    }
slouken@1895
   786
#undef RESHIFT
slouken@1895
   787
    ((unsigned int *) (char *) &srcvec)[0] = (rmask | gmask | bmask | amask);
slouken@1895
   788
    vswiz = vec_add(plus, (vector unsigned char) vec_splat(srcvec, 0));
slouken@1895
   789
    return (vswiz);
icculus@1047
   790
}
icculus@1047
   791
slouken@1895
   792
static void
slouken@1895
   793
Blit32to565PixelAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
   794
{
icculus@1047
   795
    int height = info->d_height;
slouken@1895
   796
    Uint8 *src = (Uint8 *) info->s_pixels;
icculus@1047
   797
    int srcskip = info->s_skip;
slouken@1895
   798
    Uint8 *dst = (Uint8 *) info->d_pixels;
icculus@1047
   799
    int dstskip = info->d_skip;
icculus@1047
   800
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
   801
icculus@1047
   802
    vector unsigned char v0 = vec_splat_u8(0);
icculus@1047
   803
    vector unsigned short v8_16 = vec_splat_u16(8);
icculus@1047
   804
    vector unsigned short v1_16 = vec_splat_u16(1);
icculus@1047
   805
    vector unsigned short v2_16 = vec_splat_u16(2);
icculus@1047
   806
    vector unsigned short v3_16 = vec_splat_u16(3);
icculus@1047
   807
    vector unsigned int v8_32 = vec_splat_u32(8);
icculus@1047
   808
    vector unsigned int v16_32 = vec_add(v8_32, v8_32);
slouken@1895
   809
    vector unsigned short v3f =
slouken@1895
   810
        VECUINT16_LITERAL(0x003f, 0x003f, 0x003f, 0x003f,
slouken@1895
   811
                          0x003f, 0x003f, 0x003f, 0x003f);
slouken@1895
   812
    vector unsigned short vfc =
slouken@1895
   813
        VECUINT16_LITERAL(0x00fc, 0x00fc, 0x00fc, 0x00fc,
slouken@1895
   814
                          0x00fc, 0x00fc, 0x00fc, 0x00fc);
icculus@1047
   815
icculus@1047
   816
    /* 
slouken@1895
   817
       0x10 - 0x1f is the alpha
slouken@1895
   818
       0x00 - 0x0e evens are the red
slouken@1895
   819
       0x01 - 0x0f odds are zero
slouken@1895
   820
     */
slouken@1895
   821
    vector unsigned char vredalpha1 = VECUINT8_LITERAL(0x10, 0x00, 0x01, 0x01,
slouken@1895
   822
                                                       0x10, 0x02, 0x01, 0x01,
slouken@1895
   823
                                                       0x10, 0x04, 0x01, 0x01,
slouken@1895
   824
                                                       0x10, 0x06, 0x01,
slouken@1895
   825
                                                       0x01);
slouken@1895
   826
    vector unsigned char vredalpha2 =
slouken@1895
   827
        (vector unsigned char) (vec_add((vector unsigned int) vredalpha1,
slouken@1895
   828
                                        vec_sl(v8_32, v16_32))
slouken@1895
   829
        );
icculus@1047
   830
    /*
slouken@1895
   831
       0x00 - 0x0f is ARxx ARxx ARxx ARxx
slouken@1895
   832
       0x11 - 0x0f odds are blue
slouken@1895
   833
     */
slouken@1895
   834
    vector unsigned char vblue1 = VECUINT8_LITERAL(0x00, 0x01, 0x02, 0x11,
slouken@1895
   835
                                                   0x04, 0x05, 0x06, 0x13,
slouken@1895
   836
                                                   0x08, 0x09, 0x0a, 0x15,
slouken@1895
   837
                                                   0x0c, 0x0d, 0x0e, 0x17);
slouken@1895
   838
    vector unsigned char vblue2 =
slouken@1895
   839
        (vector unsigned char) (vec_add((vector unsigned int) vblue1, v8_32)
slouken@1895
   840
        );
icculus@1047
   841
    /*
slouken@1895
   842
       0x00 - 0x0f is ARxB ARxB ARxB ARxB
slouken@1895
   843
       0x10 - 0x0e evens are green
slouken@1895
   844
     */
slouken@1895
   845
    vector unsigned char vgreen1 = VECUINT8_LITERAL(0x00, 0x01, 0x10, 0x03,
slouken@1895
   846
                                                    0x04, 0x05, 0x12, 0x07,
slouken@1895
   847
                                                    0x08, 0x09, 0x14, 0x0b,
slouken@1895
   848
                                                    0x0c, 0x0d, 0x16, 0x0f);
slouken@1895
   849
    vector unsigned char vgreen2 =
slouken@1895
   850
        (vector unsigned
slouken@1895
   851
         char) (vec_add((vector unsigned int) vgreen1, vec_sl(v8_32, v8_32))
slouken@1895
   852
        );
slouken@1895
   853
    vector unsigned char vgmerge = VECUINT8_LITERAL(0x00, 0x02, 0x00, 0x06,
slouken@1895
   854
                                                    0x00, 0x0a, 0x00, 0x0e,
slouken@1895
   855
                                                    0x00, 0x12, 0x00, 0x16,
slouken@1895
   856
                                                    0x00, 0x1a, 0x00, 0x1e);
icculus@1047
   857
    vector unsigned char mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
   858
    vector unsigned char vpermute = calc_swizzle32(srcfmt, NULL);
slouken@1895
   859
    vector unsigned char valphaPermute =
slouken@1895
   860
        vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
icculus@1047
   861
slouken@1895
   862
    vector unsigned short vf800 = (vector unsigned short) vec_splat_u8(-7);
icculus@1047
   863
    vf800 = vec_sl(vf800, vec_splat_u16(8));
icculus@1047
   864
slouken@1895
   865
    while (height--) {
icculus@1047
   866
        int extrawidth;
icculus@1047
   867
        vector unsigned char valigner;
icculus@1047
   868
        vector unsigned char vsrc;
icculus@1047
   869
        vector unsigned char voverflow;
icculus@1047
   870
        int width = info->d_width;
icculus@1047
   871
icculus@1047
   872
#define ONE_PIXEL_BLEND(condition, widthvar) \
icculus@1047
   873
        while (condition) { \
icculus@1162
   874
            Uint32 Pixel; \
icculus@1047
   875
            unsigned sR, sG, sB, dR, dG, dB, sA; \
icculus@1162
   876
            DISEMBLE_RGBA(src, 4, srcfmt, Pixel, sR, sG, sB, sA); \
icculus@1047
   877
            if(sA) { \
icculus@1047
   878
                unsigned short dstpixel = *((unsigned short *)dst); \
icculus@1047
   879
                dR = (dstpixel >> 8) & 0xf8; \
icculus@1047
   880
                dG = (dstpixel >> 3) & 0xfc; \
icculus@1047
   881
                dB = (dstpixel << 3) & 0xf8; \
icculus@1047
   882
                ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
   883
                *((unsigned short *)dst) = ( \
icculus@1047
   884
                    ((dR & 0xf8) << 8) | ((dG & 0xfc) << 3) | (dB >> 3) \
icculus@1047
   885
                ); \
icculus@1047
   886
            } \
icculus@1047
   887
            src += 4; \
icculus@1047
   888
            dst += 2; \
icculus@1047
   889
            widthvar--; \
icculus@1047
   890
        }
icculus@1047
   891
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dst)) && (width), width);
icculus@1047
   892
        extrawidth = (width % 8);
icculus@1047
   893
        valigner = VEC_ALIGNER(src);
slouken@1895
   894
        vsrc = (vector unsigned char) vec_ld(0, src);
icculus@1047
   895
        width -= extrawidth;
icculus@1047
   896
        while (width) {
icculus@1047
   897
            vector unsigned char valpha;
icculus@1047
   898
            vector unsigned char vsrc1, vsrc2;
icculus@1047
   899
            vector unsigned char vdst1, vdst2;
icculus@1047
   900
            vector unsigned short vR, vG, vB;
icculus@1047
   901
            vector unsigned short vpixel, vrpixel, vgpixel, vbpixel;
icculus@1047
   902
icculus@1047
   903
            /* Load 8 pixels from src as ARGB */
slouken@1895
   904
            voverflow = (vector unsigned char) vec_ld(15, src);
icculus@1047
   905
            vsrc = vec_perm(vsrc, voverflow, valigner);
icculus@1047
   906
            vsrc1 = vec_perm(vsrc, vsrc, vpermute);
icculus@1047
   907
            src += 16;
slouken@1895
   908
            vsrc = (vector unsigned char) vec_ld(15, src);
icculus@1047
   909
            voverflow = vec_perm(voverflow, vsrc, valigner);
icculus@1047
   910
            vsrc2 = vec_perm(voverflow, voverflow, vpermute);
icculus@1047
   911
            src += 16;
icculus@1047
   912
icculus@1047
   913
            /* Load 8 pixels from dst as XRGB */
icculus@1047
   914
            voverflow = vec_ld(0, dst);
slouken@1895
   915
            vR = vec_and((vector unsigned short) voverflow, vf800);
slouken@1895
   916
            vB = vec_sl((vector unsigned short) voverflow, v3_16);
icculus@1047
   917
            vG = vec_sl(vB, v2_16);
slouken@1895
   918
            vdst1 =
slouken@1895
   919
                (vector unsigned char) vec_perm((vector unsigned char) vR,
slouken@1895
   920
                                                (vector unsigned char) vR,
slouken@1895
   921
                                                vredalpha1);
slouken@1895
   922
            vdst1 = vec_perm(vdst1, (vector unsigned char) vB, vblue1);
slouken@1895
   923
            vdst1 = vec_perm(vdst1, (vector unsigned char) vG, vgreen1);
slouken@1895
   924
            vdst2 =
slouken@1895
   925
                (vector unsigned char) vec_perm((vector unsigned char) vR,
slouken@1895
   926
                                                (vector unsigned char) vR,
slouken@1895
   927
                                                vredalpha2);
slouken@1895
   928
            vdst2 = vec_perm(vdst2, (vector unsigned char) vB, vblue2);
slouken@1895
   929
            vdst2 = vec_perm(vdst2, (vector unsigned char) vG, vgreen2);
icculus@1047
   930
icculus@1047
   931
            /* Alpha blend 8 pixels as ARGB */
icculus@1047
   932
            valpha = vec_perm(vsrc1, v0, valphaPermute);
slouken@1895
   933
            VEC_MULTIPLY_ALPHA(vsrc1, vdst1, valpha, mergePermute, v1_16,
slouken@1895
   934
                               v8_16);
icculus@1047
   935
            valpha = vec_perm(vsrc2, v0, valphaPermute);
slouken@1895
   936
            VEC_MULTIPLY_ALPHA(vsrc2, vdst2, valpha, mergePermute, v1_16,
slouken@1895
   937
                               v8_16);
icculus@1047
   938
icculus@1047
   939
            /* Convert 8 pixels to 565 */
slouken@1895
   940
            vpixel = (vector unsigned short) vec_packpx((vector unsigned int)
slouken@1895
   941
                                                        vdst1,
slouken@1895
   942
                                                        (vector unsigned int)
slouken@1895
   943
                                                        vdst2);
slouken@1895
   944
            vgpixel = (vector unsigned short) vec_perm(vdst1, vdst2, vgmerge);
icculus@1047
   945
            vgpixel = vec_and(vgpixel, vfc);
icculus@1047
   946
            vgpixel = vec_sl(vgpixel, v3_16);
icculus@1047
   947
            vrpixel = vec_sl(vpixel, v1_16);
icculus@1047
   948
            vrpixel = vec_and(vrpixel, vf800);
icculus@1047
   949
            vbpixel = vec_and(vpixel, v3f);
slouken@1895
   950
            vdst1 =
slouken@1895
   951
                vec_or((vector unsigned char) vrpixel,
slouken@1895
   952
                       (vector unsigned char) vgpixel);
slouken@1895
   953
            vdst1 = vec_or(vdst1, (vector unsigned char) vbpixel);
slouken@1895
   954
icculus@1047
   955
            /* Store 8 pixels */
icculus@1047
   956
            vec_st(vdst1, 0, dst);
icculus@1047
   957
icculus@1047
   958
            width -= 8;
icculus@1047
   959
            dst += 16;
icculus@1047
   960
        }
icculus@1047
   961
        ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
   962
#undef ONE_PIXEL_BLEND
icculus@1047
   963
        src += srcskip;
icculus@1047
   964
        dst += dstskip;
icculus@1047
   965
    }
icculus@1047
   966
}
icculus@1047
   967
slouken@1895
   968
static void
slouken@1895
   969
Blit32to32SurfaceAlphaKeyAltivec(SDL_BlitInfo * info)
icculus@1047
   970
{
icculus@1047
   971
    unsigned alpha = info->src->alpha;
icculus@1047
   972
    int height = info->d_height;
slouken@1895
   973
    Uint32 *srcp = (Uint32 *) info->s_pixels;
icculus@1047
   974
    int srcskip = info->s_skip >> 2;
slouken@1895
   975
    Uint32 *dstp = (Uint32 *) info->d_pixels;
icculus@1047
   976
    int dstskip = info->d_skip >> 2;
icculus@1047
   977
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
   978
    SDL_PixelFormat *dstfmt = info->dst;
icculus@1047
   979
    unsigned sA = srcfmt->alpha;
icculus@1047
   980
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
icculus@1047
   981
    Uint32 rgbmask = srcfmt->Rmask | srcfmt->Gmask | srcfmt->Bmask;
icculus@1047
   982
    Uint32 ckey = info->src->colorkey;
icculus@1047
   983
    vector unsigned char mergePermute;
icculus@1047
   984
    vector unsigned char vsrcPermute;
icculus@1047
   985
    vector unsigned char vdstPermute;
icculus@1047
   986
    vector unsigned char vsdstPermute;
icculus@1047
   987
    vector unsigned char valpha;
icculus@1047
   988
    vector unsigned char valphamask;
icculus@1047
   989
    vector unsigned char vbits;
icculus@1047
   990
    vector unsigned char v0;
icculus@1047
   991
    vector unsigned short v1;
icculus@1047
   992
    vector unsigned short v8;
icculus@1047
   993
    vector unsigned int vckey;
icculus@1047
   994
    vector unsigned int vrgbmask;
icculus@1047
   995
icculus@1047
   996
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
   997
    v0 = vec_splat_u8(0);
icculus@1047
   998
    v1 = vec_splat_u16(1);
icculus@1047
   999
    v8 = vec_splat_u16(8);
icculus@1047
  1000
icculus@1047
  1001
    /* set the alpha to 255 on the destination surf */
icculus@1047
  1002
    valphamask = VEC_ALPHA_MASK();
icculus@1047
  1003
icculus@1047
  1004
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
  1005
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
  1006
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
  1007
icculus@1047
  1008
    /* set a vector full of alpha and 255-alpha */
slouken@1895
  1009
    ((unsigned char *) &valpha)[0] = alpha;
icculus@1047
  1010
    valpha = vec_splat(valpha, 0);
slouken@1895
  1011
    vbits = (vector unsigned char) vec_splat_s8(-1);
icculus@1047
  1012
icculus@1047
  1013
    ckey &= rgbmask;
slouken@1895
  1014
    ((unsigned int *) (char *) &vckey)[0] = ckey;
icculus@1047
  1015
    vckey = vec_splat(vckey, 0);
slouken@1895
  1016
    ((unsigned int *) (char *) &vrgbmask)[0] = rgbmask;
icculus@1047
  1017
    vrgbmask = vec_splat(vrgbmask, 0);
icculus@1047
  1018
slouken@1895
  1019
    while (height--) {
icculus@1047
  1020
        int width = info->d_width;
icculus@1047
  1021
#define ONE_PIXEL_BLEND(condition, widthvar) \
icculus@1047
  1022
        while (condition) { \
icculus@1162
  1023
            Uint32 Pixel; \
icculus@1047
  1024
            unsigned sR, sG, sB, dR, dG, dB; \
icculus@1162
  1025
            RETRIEVE_RGB_PIXEL(((Uint8 *)srcp), 4, Pixel); \
icculus@1162
  1026
            if(sA && Pixel != ckey) { \
icculus@1162
  1027
                RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB); \
icculus@1162
  1028
                DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
icculus@1047
  1029
                ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
  1030
                ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
  1031
            } \
icculus@1162
  1032
            dstp++; \
icculus@1162
  1033
            srcp++; \
icculus@1047
  1034
            widthvar--; \
icculus@1047
  1035
        }
icculus@1047
  1036
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1037
        if (width > 0) {
icculus@1047
  1038
            int extrawidth = (width % 4);
icculus@1047
  1039
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
  1040
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
  1041
            width -= extrawidth;
icculus@1047
  1042
            while (width) {
icculus@1047
  1043
                vector unsigned char vsel;
icculus@1047
  1044
                vector unsigned char voverflow;
icculus@1047
  1045
                vector unsigned char vd;
icculus@1047
  1046
                vector unsigned char vd_orig;
icculus@1047
  1047
icculus@1047
  1048
                /* s = *srcp */
slouken@1895
  1049
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
  1050
                vs = vec_perm(vs, voverflow, valigner);
slouken@1895
  1051
icculus@1047
  1052
                /* vsel is set for items that match the key */
slouken@1895
  1053
                vsel =
slouken@1895
  1054
                    (vector unsigned char) vec_and((vector unsigned int) vs,
slouken@1895
  1055
                                                   vrgbmask);
slouken@1895
  1056
                vsel = (vector unsigned char) vec_cmpeq((vector unsigned int)
slouken@1895
  1057
                                                        vsel, vckey);
icculus@1047
  1058
icculus@1047
  1059
                /* permute to source format */
icculus@1047
  1060
                vs = vec_perm(vs, valpha, vsrcPermute);
icculus@1047
  1061
icculus@1047
  1062
                /* d = *dstp */
slouken@1895
  1063
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
  1064
                vd_orig = vd = vec_perm(vd, v0, vsdstPermute);
icculus@1047
  1065
icculus@1047
  1066
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1067
icculus@1047
  1068
                /* set the alpha channel to full on */
icculus@1047
  1069
                vd = vec_or(vd, valphamask);
icculus@1047
  1070
icculus@1047
  1071
                /* mask out color key */
icculus@1047
  1072
                vd = vec_sel(vd, vd_orig, vsel);
slouken@1895
  1073
icculus@1047
  1074
                /* permute to dest format */
icculus@1047
  1075
                vd = vec_perm(vd, vbits, vdstPermute);
icculus@1047
  1076
icculus@1047
  1077
                /* *dstp = res */
slouken@1895
  1078
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
  1079
icculus@1047
  1080
                srcp += 4;
icculus@1047
  1081
                dstp += 4;
icculus@1047
  1082
                width -= 4;
icculus@1047
  1083
                vs = voverflow;
icculus@1047
  1084
            }
icculus@1047
  1085
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1086
        }
icculus@1047
  1087
#undef ONE_PIXEL_BLEND
slouken@1895
  1088
icculus@1047
  1089
        srcp += srcskip;
icculus@1047
  1090
        dstp += dstskip;
icculus@1047
  1091
    }
icculus@1047
  1092
}
icculus@1047
  1093
icculus@1047
  1094
slouken@1895
  1095
static void
slouken@1895
  1096
Blit32to32PixelAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
  1097
{
icculus@1047
  1098
    int width = info->d_width;
icculus@1047
  1099
    int height = info->d_height;
slouken@1895
  1100
    Uint32 *srcp = (Uint32 *) info->s_pixels;
icculus@1047
  1101
    int srcskip = info->s_skip >> 2;
slouken@1895
  1102
    Uint32 *dstp = (Uint32 *) info->d_pixels;
icculus@1047
  1103
    int dstskip = info->d_skip >> 2;
icculus@1047
  1104
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
  1105
    SDL_PixelFormat *dstfmt = info->dst;
icculus@1047
  1106
    vector unsigned char mergePermute;
icculus@1047
  1107
    vector unsigned char valphaPermute;
icculus@1047
  1108
    vector unsigned char vsrcPermute;
icculus@1047
  1109
    vector unsigned char vdstPermute;
icculus@1047
  1110
    vector unsigned char vsdstPermute;
icculus@1047
  1111
    vector unsigned char valphamask;
icculus@1047
  1112
    vector unsigned char vpixelmask;
icculus@1047
  1113
    vector unsigned char v0;
icculus@1047
  1114
    vector unsigned short v1;
icculus@1047
  1115
    vector unsigned short v8;
icculus@1047
  1116
icculus@1047
  1117
    v0 = vec_splat_u8(0);
icculus@1047
  1118
    v1 = vec_splat_u16(1);
icculus@1047
  1119
    v8 = vec_splat_u16(8);
icculus@1047
  1120
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1121
    valphamask = VEC_ALPHA_MASK();
slouken@1895
  1122
    valphaPermute = vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
icculus@1047
  1123
    vpixelmask = vec_nor(valphamask, v0);
icculus@1047
  1124
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
  1125
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
  1126
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
  1127
slouken@1895
  1128
    while (height--) {
icculus@1047
  1129
        width = info->d_width;
icculus@1047
  1130
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1162
  1131
            Uint32 Pixel; \
icculus@1047
  1132
            unsigned sR, sG, sB, dR, dG, dB, sA, dA; \
icculus@1162
  1133
            DISEMBLE_RGBA((Uint8 *)srcp, 4, srcfmt, Pixel, sR, sG, sB, sA); \
icculus@1047
  1134
            if(sA) { \
icculus@1162
  1135
              DISEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, Pixel, dR, dG, dB, dA); \
icculus@1047
  1136
              ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
  1137
              ASSEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
  1138
            } \
icculus@1047
  1139
            ++srcp; \
icculus@1047
  1140
            ++dstp; \
icculus@1047
  1141
            widthvar--; \
icculus@1047
  1142
        }
icculus@1047
  1143
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1144
        if (width > 0) {
slouken@1487
  1145
            /* vsrcPermute */
slouken@1487
  1146
            /* vdstPermute */
icculus@1047
  1147
            int extrawidth = (width % 4);
icculus@1047
  1148
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
  1149
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
  1150
            width -= extrawidth;
icculus@1047
  1151
            while (width) {
icculus@1047
  1152
                vector unsigned char voverflow;
icculus@1047
  1153
                vector unsigned char vd;
icculus@1047
  1154
                vector unsigned char valpha;
icculus@1047
  1155
                vector unsigned char vdstalpha;
icculus@1047
  1156
                /* s = *srcp */
slouken@1895
  1157
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
  1158
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1159
                vs = vec_perm(vs, v0, vsrcPermute);
icculus@1047
  1160
icculus@1047
  1161
                valpha = vec_perm(vs, v0, valphaPermute);
slouken@1895
  1162
icculus@1047
  1163
                /* d = *dstp */
slouken@1895
  1164
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
  1165
                vd = vec_perm(vd, v0, vsdstPermute);
icculus@1047
  1166
                vdstalpha = vec_and(vd, valphamask);
icculus@1047
  1167
icculus@1047
  1168
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1169
icculus@1047
  1170
                /* set the alpha to the dest alpha */
icculus@1047
  1171
                vd = vec_and(vd, vpixelmask);
icculus@1047
  1172
                vd = vec_or(vd, vdstalpha);
icculus@1047
  1173
                vd = vec_perm(vd, v0, vdstPermute);
icculus@1047
  1174
icculus@1047
  1175
                /* *dstp = res */
slouken@1895
  1176
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
  1177
icculus@1047
  1178
                srcp += 4;
icculus@1047
  1179
                dstp += 4;
icculus@1047
  1180
                width -= 4;
icculus@1047
  1181
                vs = voverflow;
icculus@1047
  1182
icculus@1047
  1183
            }
icculus@1047
  1184
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1185
        }
slouken@1895
  1186
        srcp += srcskip;
slouken@1895
  1187
        dstp += dstskip;
icculus@1047
  1188
#undef ONE_PIXEL_BLEND
slouken@1895
  1189
    }
icculus@1047
  1190
}
icculus@1047
  1191
icculus@1047
  1192
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
  1193
static void
slouken@1895
  1194
BlitRGBtoRGBPixelAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
  1195
{
slouken@1895
  1196
    int width = info->d_width;
slouken@1895
  1197
    int height = info->d_height;
slouken@1895
  1198
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1199
    int srcskip = info->s_skip >> 2;
slouken@1895
  1200
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
  1201
    int dstskip = info->d_skip >> 2;
icculus@1047
  1202
    vector unsigned char mergePermute;
icculus@1047
  1203
    vector unsigned char valphaPermute;
icculus@1047
  1204
    vector unsigned char valphamask;
icculus@1047
  1205
    vector unsigned char vpixelmask;
icculus@1047
  1206
    vector unsigned char v0;
icculus@1047
  1207
    vector unsigned short v1;
icculus@1047
  1208
    vector unsigned short v8;
icculus@1047
  1209
    v0 = vec_splat_u8(0);
icculus@1047
  1210
    v1 = vec_splat_u16(1);
icculus@1047
  1211
    v8 = vec_splat_u16(8);
icculus@1047
  1212
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1213
    valphamask = VEC_ALPHA_MASK();
slouken@1895
  1214
    valphaPermute = vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
slouken@1895
  1215
slouken@1895
  1216
icculus@1047
  1217
    vpixelmask = vec_nor(valphamask, v0);
slouken@1895
  1218
    while (height--) {
icculus@1047
  1219
        width = info->d_width;
icculus@1047
  1220
#define ONE_PIXEL_BLEND(condition, widthvar) \
icculus@1047
  1221
        while ((condition)) { \
icculus@1047
  1222
            Uint32 dalpha; \
icculus@1047
  1223
            Uint32 d; \
icculus@1047
  1224
            Uint32 s1; \
icculus@1047
  1225
            Uint32 d1; \
icculus@1047
  1226
            Uint32 s = *srcp; \
icculus@1047
  1227
            Uint32 alpha = s >> 24; \
icculus@1047
  1228
            if(alpha) { \
icculus@1047
  1229
              if(alpha == SDL_ALPHA_OPAQUE) { \
icculus@1047
  1230
                *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000); \
icculus@1047
  1231
              } else { \
icculus@1047
  1232
                d = *dstp; \
icculus@1047
  1233
                dalpha = d & 0xff000000; \
icculus@1047
  1234
                s1 = s & 0xff00ff; \
icculus@1047
  1235
                d1 = d & 0xff00ff; \
icculus@1047
  1236
                d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
icculus@1047
  1237
                s &= 0xff00; \
icculus@1047
  1238
                d &= 0xff00; \
icculus@1047
  1239
                d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
icculus@1047
  1240
                *dstp = d1 | d | dalpha; \
icculus@1047
  1241
              } \
icculus@1047
  1242
            } \
icculus@1047
  1243
            ++srcp; \
icculus@1047
  1244
            ++dstp; \
icculus@1047
  1245
            widthvar--; \
icculus@1047
  1246
	    }
icculus@1047
  1247
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1248
        if (width > 0) {
icculus@1047
  1249
            int extrawidth = (width % 4);
icculus@1047
  1250
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
  1251
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
  1252
            width -= extrawidth;
icculus@1047
  1253
            while (width) {
icculus@1047
  1254
                vector unsigned char voverflow;
icculus@1047
  1255
                vector unsigned char vd;
icculus@1047
  1256
                vector unsigned char valpha;
icculus@1047
  1257
                vector unsigned char vdstalpha;
icculus@1047
  1258
                /* s = *srcp */
slouken@1895
  1259
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
  1260
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1261
icculus@1047
  1262
                valpha = vec_perm(vs, v0, valphaPermute);
slouken@1895
  1263
icculus@1047
  1264
                /* d = *dstp */
slouken@1895
  1265
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
  1266
                vdstalpha = vec_and(vd, valphamask);
icculus@1047
  1267
icculus@1047
  1268
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1269
icculus@1047
  1270
                /* set the alpha to the dest alpha */
icculus@1047
  1271
                vd = vec_and(vd, vpixelmask);
icculus@1047
  1272
                vd = vec_or(vd, vdstalpha);
icculus@1047
  1273
icculus@1047
  1274
                /* *dstp = res */
slouken@1895
  1275
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
  1276
icculus@1047
  1277
                srcp += 4;
icculus@1047
  1278
                dstp += 4;
icculus@1047
  1279
                width -= 4;
icculus@1047
  1280
                vs = voverflow;
icculus@1047
  1281
            }
icculus@1047
  1282
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1283
        }
slouken@1895
  1284
        srcp += srcskip;
slouken@1895
  1285
        dstp += dstskip;
slouken@1895
  1286
    }
icculus@1047
  1287
#undef ONE_PIXEL_BLEND
icculus@1047
  1288
}
icculus@1047
  1289
slouken@1895
  1290
static void
slouken@1895
  1291
Blit32to32SurfaceAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
  1292
{
icculus@1047
  1293
    /* XXX : 6 */
slouken@1895
  1294
    unsigned alpha = info->src->alpha;
icculus@1047
  1295
    int height = info->d_height;
slouken@1895
  1296
    Uint32 *srcp = (Uint32 *) info->s_pixels;
icculus@1047
  1297
    int srcskip = info->s_skip >> 2;
slouken@1895
  1298
    Uint32 *dstp = (Uint32 *) info->d_pixels;
icculus@1047
  1299
    int dstskip = info->d_skip >> 2;
icculus@1047
  1300
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
  1301
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
  1302
    unsigned sA = srcfmt->alpha;
slouken@1895
  1303
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
icculus@1047
  1304
    vector unsigned char mergePermute;
icculus@1047
  1305
    vector unsigned char vsrcPermute;
icculus@1047
  1306
    vector unsigned char vdstPermute;
icculus@1047
  1307
    vector unsigned char vsdstPermute;
icculus@1047
  1308
    vector unsigned char valpha;
icculus@1047
  1309
    vector unsigned char valphamask;
icculus@1047
  1310
    vector unsigned char vbits;
icculus@1047
  1311
    vector unsigned short v1;
icculus@1047
  1312
    vector unsigned short v8;
icculus@1047
  1313
icculus@1047
  1314
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1315
    v1 = vec_splat_u16(1);
icculus@1047
  1316
    v8 = vec_splat_u16(8);
icculus@1047
  1317
icculus@1047
  1318
    /* set the alpha to 255 on the destination surf */
icculus@1047
  1319
    valphamask = VEC_ALPHA_MASK();
icculus@1047
  1320
icculus@1047
  1321
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
  1322
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
  1323
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
  1324
icculus@1047
  1325
    /* set a vector full of alpha and 255-alpha */
slouken@1895
  1326
    ((unsigned char *) &valpha)[0] = alpha;
icculus@1047
  1327
    valpha = vec_splat(valpha, 0);
slouken@1895
  1328
    vbits = (vector unsigned char) vec_splat_s8(-1);
icculus@1047
  1329
slouken@1895
  1330
    while (height--) {
icculus@1047
  1331
        int width = info->d_width;
icculus@1047
  1332
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1162
  1333
            Uint32 Pixel; \
icculus@1047
  1334
            unsigned sR, sG, sB, dR, dG, dB; \
icculus@1162
  1335
            DISEMBLE_RGB(((Uint8 *)srcp), 4, srcfmt, Pixel, sR, sG, sB); \
icculus@1162
  1336
            DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
icculus@1047
  1337
            ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
  1338
            ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
  1339
            ++srcp; \
icculus@1047
  1340
            ++dstp; \
icculus@1047
  1341
            widthvar--; \
icculus@1047
  1342
        }
icculus@1047
  1343
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1344
        if (width > 0) {
icculus@1047
  1345
            int extrawidth = (width % 4);
icculus@2086
  1346
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
  1347
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
  1348
            width -= extrawidth;
icculus@1047
  1349
            while (width) {
icculus@1047
  1350
                vector unsigned char voverflow;
icculus@1047
  1351
                vector unsigned char vd;
icculus@1047
  1352
icculus@1047
  1353
                /* s = *srcp */
slouken@1895
  1354
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
  1355
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1356
                vs = vec_perm(vs, valpha, vsrcPermute);
slouken@1895
  1357
icculus@1047
  1358
                /* d = *dstp */
slouken@1895
  1359
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
  1360
                vd = vec_perm(vd, vd, vsdstPermute);
icculus@1047
  1361
icculus@1047
  1362
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1363
icculus@1047
  1364
                /* set the alpha channel to full on */
icculus@1047
  1365
                vd = vec_or(vd, valphamask);
icculus@1047
  1366
                vd = vec_perm(vd, vbits, vdstPermute);
icculus@1047
  1367
icculus@1047
  1368
                /* *dstp = res */
slouken@1895
  1369
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
  1370
icculus@1047
  1371
                srcp += 4;
icculus@1047
  1372
                dstp += 4;
icculus@1047
  1373
                width -= 4;
icculus@1047
  1374
                vs = voverflow;
icculus@1047
  1375
            }
icculus@1047
  1376
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1377
        }
icculus@1047
  1378
#undef ONE_PIXEL_BLEND
slouken@1895
  1379
icculus@1047
  1380
        srcp += srcskip;
icculus@1047
  1381
        dstp += dstskip;
icculus@1047
  1382
    }
icculus@1047
  1383
icculus@1047
  1384
}
icculus@1047
  1385
icculus@1047
  1386
icculus@1047
  1387
/* fast RGB888->(A)RGB888 blending */
slouken@1895
  1388
static void
slouken@1895
  1389
BlitRGBtoRGBSurfaceAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
  1390
{
slouken@1895
  1391
    unsigned alpha = info->src->alpha;
icculus@1047
  1392
    int height = info->d_height;
slouken@1895
  1393
    Uint32 *srcp = (Uint32 *) info->s_pixels;
icculus@1047
  1394
    int srcskip = info->s_skip >> 2;
slouken@1895
  1395
    Uint32 *dstp = (Uint32 *) info->d_pixels;
icculus@1047
  1396
    int dstskip = info->d_skip >> 2;
icculus@1047
  1397
    vector unsigned char mergePermute;
icculus@1047
  1398
    vector unsigned char valpha;
icculus@1047
  1399
    vector unsigned char valphamask;
icculus@1047
  1400
    vector unsigned short v1;
icculus@1047
  1401
    vector unsigned short v8;
icculus@1047
  1402
icculus@1047
  1403
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1404
    v1 = vec_splat_u16(1);
icculus@1047
  1405
    v8 = vec_splat_u16(8);
icculus@1047
  1406
icculus@1047
  1407
    /* set the alpha to 255 on the destination surf */
icculus@1047
  1408
    valphamask = VEC_ALPHA_MASK();
icculus@1047
  1409
icculus@1047
  1410
    /* set a vector full of alpha and 255-alpha */
slouken@1895
  1411
    ((unsigned char *) &valpha)[0] = alpha;
icculus@1047
  1412
    valpha = vec_splat(valpha, 0);
icculus@1047
  1413
slouken@1895
  1414
    while (height--) {
icculus@1047
  1415
        int width = info->d_width;
icculus@1047
  1416
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1047
  1417
            Uint32 s = *srcp; \
icculus@1047
  1418
            Uint32 d = *dstp; \
icculus@1047
  1419
            Uint32 s1 = s & 0xff00ff; \
icculus@1047
  1420
            Uint32 d1 = d & 0xff00ff; \
icculus@1047
  1421
            d1 = (d1 + ((s1 - d1) * alpha >> 8)) \
icculus@1047
  1422
                 & 0xff00ff; \
icculus@1047
  1423
            s &= 0xff00; \
icculus@1047
  1424
            d &= 0xff00; \
icculus@1047
  1425
            d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
icculus@1047
  1426
            *dstp = d1 | d | 0xff000000; \
icculus@1047
  1427
            ++srcp; \
icculus@1047
  1428
            ++dstp; \
icculus@1047
  1429
            widthvar--; \
icculus@1047
  1430
        }
icculus@1047
  1431
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1432
        if (width > 0) {
icculus@1047
  1433
            int extrawidth = (width % 4);
icculus@1047
  1434
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
  1435
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
  1436
            width -= extrawidth;
icculus@1047
  1437
            while (width) {
icculus@1047
  1438
                vector unsigned char voverflow;
icculus@1047
  1439
                vector unsigned char vd;
icculus@1047
  1440
icculus@1047
  1441
                /* s = *srcp */
slouken@1895
  1442
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
  1443
                vs = vec_perm(vs, voverflow, valigner);
slouken@1895
  1444
icculus@1047
  1445
                /* d = *dstp */
slouken@1895
  1446
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
  1447
icculus@1047
  1448
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1449
icculus@1047
  1450
                /* set the alpha channel to full on */
icculus@1047
  1451
                vd = vec_or(vd, valphamask);
icculus@1047
  1452
icculus@1047
  1453
                /* *dstp = res */
slouken@1895
  1454
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
  1455
icculus@1047
  1456
                srcp += 4;
icculus@1047
  1457
                dstp += 4;
icculus@1047
  1458
                width -= 4;
icculus@1047
  1459
                vs = voverflow;
icculus@1047
  1460
            }
icculus@1047
  1461
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1462
        }
icculus@1047
  1463
#undef ONE_PIXEL_BLEND
slouken@1895
  1464
icculus@1047
  1465
        srcp += srcskip;
icculus@1047
  1466
        dstp += dstskip;
icculus@1047
  1467
    }
icculus@1047
  1468
}
slouken@1895
  1469
slouken@1795
  1470
#if __MWERKS__
slouken@1795
  1471
#pragma altivec_model off
slouken@1795
  1472
#endif
slouken@1361
  1473
#endif /* SDL_ALTIVEC_BLITTERS */
icculus@1047
  1474
slouken@1
  1475
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1895
  1476
static void
slouken@1895
  1477
BlitRGBtoRGBSurfaceAlpha128(SDL_BlitInfo * info)
slouken@0
  1478
{
slouken@1895
  1479
    int width = info->d_width;
slouken@1895
  1480
    int height = info->d_height;
slouken@1895
  1481
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1482
    int srcskip = info->s_skip >> 2;
slouken@1895
  1483
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
  1484
    int dstskip = info->d_skip >> 2;
slouken@0
  1485
slouken@1895
  1486
    while (height--) {
slouken@1895
  1487
	    /* *INDENT-OFF* */
slouken@0
  1488
	    DUFFS_LOOP4({
slouken@1
  1489
		    Uint32 s = *srcp++;
slouken@1
  1490
		    Uint32 d = *dstp;
slouken@1
  1491
		    *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1
  1492
			       + (s & d & 0x00010101)) | 0xff000000;
slouken@0
  1493
	    }, width);
slouken@1895
  1494
	    /* *INDENT-ON* */
slouken@1895
  1495
        srcp += srcskip;
slouken@1895
  1496
        dstp += dstskip;
slouken@1895
  1497
    }
slouken@0
  1498
}
slouken@0
  1499
slouken@1
  1500
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1895
  1501
static void
slouken@1895
  1502
BlitRGBtoRGBSurfaceAlpha(SDL_BlitInfo * info)
slouken@1
  1503
{
slouken@1895
  1504
    unsigned alpha = info->src->alpha;
slouken@1895
  1505
    if (alpha == 128) {
slouken@1895
  1506
        BlitRGBtoRGBSurfaceAlpha128(info);
slouken@1895
  1507
    } else {
slouken@1895
  1508
        int width = info->d_width;
slouken@1895
  1509
        int height = info->d_height;
slouken@1895
  1510
        Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1511
        int srcskip = info->s_skip >> 2;
slouken@1895
  1512
        Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
  1513
        int dstskip = info->d_skip >> 2;
slouken@1895
  1514
        Uint32 s;
slouken@1895
  1515
        Uint32 d;
slouken@1895
  1516
        Uint32 s1;
slouken@1895
  1517
        Uint32 d1;
slouken@1
  1518
slouken@1895
  1519
        while (height--) {
slouken@1895
  1520
			/* *INDENT-OFF* */
slouken@689
  1521
			DUFFS_LOOP_DOUBLE2({
slouken@689
  1522
				/* One Pixel Blend */
slouken@1
  1523
				s = *srcp;
slouken@1
  1524
				d = *dstp;
slouken@1
  1525
				s1 = s & 0xff00ff;
slouken@1
  1526
				d1 = d & 0xff00ff;
slouken@1
  1527
				d1 = (d1 + ((s1 - d1) * alpha >> 8))
slouken@1
  1528
				     & 0xff00ff;
slouken@1
  1529
				s &= 0xff00;
slouken@1
  1530
				d &= 0xff00;
slouken@1
  1531
				d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@1
  1532
				*dstp = d1 | d | 0xff000000;
slouken@1
  1533
				++srcp;
slouken@1
  1534
				++dstp;
slouken@689
  1535
			},{
slouken@689
  1536
			        /* Two Pixels Blend */
slouken@689
  1537
				s = *srcp;
slouken@689
  1538
				d = *dstp;
slouken@689
  1539
				s1 = s & 0xff00ff;
slouken@689
  1540
				d1 = d & 0xff00ff;
slouken@689
  1541
				d1 += (s1 - d1) * alpha >> 8;
slouken@689
  1542
				d1 &= 0xff00ff;
slouken@689
  1543
				     
slouken@689
  1544
				s = ((s & 0xff00) >> 8) | 
slouken@689
  1545
					((srcp[1] & 0xff00) << 8);
slouken@689
  1546
				d = ((d & 0xff00) >> 8) |
slouken@689
  1547
					((dstp[1] & 0xff00) << 8);
slouken@689
  1548
				d += (s - d) * alpha >> 8;
slouken@689
  1549
				d &= 0x00ff00ff;
slouken@689
  1550
				
slouken@689
  1551
				*dstp++ = d1 | ((d << 8) & 0xff00) | 0xff000000;
slouken@689
  1552
				++srcp;
slouken@689
  1553
				
slouken@689
  1554
			        s1 = *srcp;
slouken@689
  1555
				d1 = *dstp;
slouken@689
  1556
				s1 &= 0xff00ff;
slouken@689
  1557
				d1 &= 0xff00ff;
slouken@689
  1558
				d1 += (s1 - d1) * alpha >> 8;
slouken@689
  1559
				d1 &= 0xff00ff;
slouken@689
  1560
				
slouken@689
  1561
				*dstp = d1 | ((d >> 8) & 0xff00) | 0xff000000;
slouken@689
  1562
				++srcp;
slouken@689
  1563
				++dstp;
slouken@1
  1564
			}, width);
slouken@1895
  1565
			/* *INDENT-ON* */
slouken@1895
  1566
            srcp += srcskip;
slouken@1895
  1567
            dstp += dstskip;
slouken@1895
  1568
        }
slouken@1895
  1569
    }
slouken@1
  1570
}
slouken@1
  1571
slouken@0
  1572
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
  1573
static void
slouken@1895
  1574
BlitRGBtoRGBPixelAlpha(SDL_BlitInfo * info)
slouken@0
  1575
{
slouken@1895
  1576
    int width = info->d_width;
slouken@1895
  1577
    int height = info->d_height;
slouken@1895
  1578
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1579
    int srcskip = info->s_skip >> 2;
slouken@1895
  1580
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
  1581
    int dstskip = info->d_skip >> 2;
slouken@0
  1582
slouken@1895
  1583
    while (height--) {
slouken@1895
  1584
	    /* *INDENT-OFF* */
slouken@0
  1585
	    DUFFS_LOOP4({
slouken@0
  1586
		Uint32 dalpha;
slouken@0
  1587
		Uint32 d;
slouken@0
  1588
		Uint32 s1;
slouken@0
  1589
		Uint32 d1;
slouken@0
  1590
		Uint32 s = *srcp;
slouken@0
  1591
		Uint32 alpha = s >> 24;
slouken@0
  1592
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1593
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1594
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1595
		   Benchmark this! */
slouken@689
  1596
		if(alpha) {   
slouken@689
  1597
		  if(alpha == SDL_ALPHA_OPAQUE) {
slouken@0
  1598
		    *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000);
slouken@689
  1599
		  } else {
slouken@0
  1600
		    /*
slouken@0
  1601
		     * take out the middle component (green), and process
slouken@0
  1602
		     * the other two in parallel. One multiply less.
slouken@0
  1603
		     */
slouken@0
  1604
		    d = *dstp;
slouken@0
  1605
		    dalpha = d & 0xff000000;
slouken@0
  1606
		    s1 = s & 0xff00ff;
slouken@0
  1607
		    d1 = d & 0xff00ff;
slouken@0
  1608
		    d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
slouken@0
  1609
		    s &= 0xff00;
slouken@0
  1610
		    d &= 0xff00;
slouken@0
  1611
		    d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@0
  1612
		    *dstp = d1 | d | dalpha;
slouken@689
  1613
		  }
slouken@0
  1614
		}
slouken@0
  1615
		++srcp;
slouken@0
  1616
		++dstp;
slouken@0
  1617
	    }, width);
slouken@1895
  1618
	    /* *INDENT-ON* */
slouken@1895
  1619
        srcp += srcskip;
slouken@1895
  1620
        dstp += dstskip;
slouken@1895
  1621
    }
slouken@0
  1622
}
slouken@0
  1623
slouken@1542
  1624
#if GCC_ASMBLIT
slouken@689
  1625
/* fast (as in MMX with prefetch) ARGB888->(A)RGB888 blending with pixel alpha */
slouken@2038
  1626
static void
slouken@1895
  1627
BlitRGBtoRGBPixelAlphaMMX3DNOW(SDL_BlitInfo * info)
slouken@689
  1628
{
slouken@1895
  1629
    int width = info->d_width;
slouken@1895
  1630
    int height = info->d_height;
slouken@1895
  1631
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1632
    int srcskip = info->s_skip >> 2;
slouken@1895
  1633
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
  1634
    int dstskip = info->d_skip >> 2;
slouken@1895
  1635
    SDL_PixelFormat *sf = info->src;
slouken@1895
  1636
    Uint32 amask = sf->Amask;
slouken@689
  1637
slouken@1895
  1638
    __asm__(
slouken@1895
  1639
               /* make mm6 all zeros. */
slouken@1895
  1640
               "pxor       %%mm6, %%mm6\n"
slouken@1895
  1641
               /* Make a mask to preserve the alpha. */
slouken@1895
  1642
               "movd      %0, %%mm7\n\t"        /* 0000F000 -> mm7 */
slouken@1895
  1643
               "punpcklbw %%mm7, %%mm7\n\t"     /* FF000000 -> mm7 */
slouken@1895
  1644
               "pcmpeqb   %%mm4, %%mm4\n\t"     /* FFFFFFFF -> mm4 */
slouken@1895
  1645
               "movq      %%mm4, %%mm3\n\t"     /* FFFFFFFF -> mm3 (for later) */
slouken@1895
  1646
               "pxor      %%mm4, %%mm7\n\t"     /* 00FFFFFF -> mm7 (mult mask) */
slouken@1895
  1647
               /* form channel masks */
slouken@1895
  1648
               "movq      %%mm7, %%mm4\n\t"     /* 00FFFFFF -> mm4 */
slouken@1895
  1649
               "packsswb  %%mm6, %%mm4\n\t"     /* 00000FFF -> mm4 (channel mask) */
slouken@1895
  1650
               "packsswb  %%mm6, %%mm3\n\t"     /* 0000FFFF -> mm3 */
slouken@1895
  1651
               "pxor      %%mm4, %%mm3\n\t"     /* 0000F000 -> mm3 (~channel mask) */
slouken@1895
  1652
               /* get alpha channel shift */
slouken@1895
  1653
               "movd      %1, %%mm5\n\t"        /* Ashift -> mm5 */
icculus@2101
  1654
  : /* nothing */ :            "rm"(amask), "rm"((Uint32) sf->Ashift));
slouken@689
  1655
slouken@1895
  1656
    while (height--) {
slouken@1542
  1657
slouken@1895
  1658
	    /* *INDENT-OFF* */
slouken@689
  1659
	    DUFFS_LOOP4({
slouken@1542
  1660
		Uint32 alpha;
slouken@689
  1661
slouken@689
  1662
		__asm__ (
slouken@689
  1663
		"prefetch 64(%0)\n"
slouken@689
  1664
		"prefetch 64(%1)\n"
slouken@689
  1665
			: : "r" (srcp), "r" (dstp) );
slouken@689
  1666
slouken@1542
  1667
		alpha = *srcp & amask;
slouken@689
  1668
		/* FIXME: Here we special-case opaque alpha since the
slouken@689
  1669
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@689
  1670
		   it correctly. Also special-case alpha=0 for speed?
slouken@689
  1671
		   Benchmark this! */
slouken@1542
  1672
		if(alpha == 0) {
slouken@1542
  1673
		    /* do nothing */
slouken@1542
  1674
		}
slouken@1542
  1675
		else if(alpha == amask) {
slouken@1542
  1676
			/* opaque alpha -- copy RGB, keep dst alpha */
slouken@1542
  1677
		    /* using MMX here to free up regular registers for other things */
slouken@1542
  1678
			    __asm__ (
slouken@1542
  1679
		    "movd      (%0),  %%mm0\n\t" /* src(ARGB) -> mm0 (0000ARGB)*/
slouken@1542
  1680
		    "movd      (%1),  %%mm1\n\t" /* dst(ARGB) -> mm1 (0000ARGB)*/
slouken@1542
  1681
		    "pand      %%mm4, %%mm0\n\t" /* src & chanmask -> mm0 */
slouken@1542
  1682
		    "pand      %%mm3, %%mm1\n\t" /* dst & ~chanmask -> mm2 */
slouken@1542
  1683
		    "por       %%mm0, %%mm1\n\t" /* src | dst -> mm1 */
slouken@1542
  1684
		    "movd      %%mm1, (%1) \n\t" /* mm1 -> dst */
slouken@1542
  1685
slouken@1542
  1686
		     : : "r" (srcp), "r" (dstp) );
slouken@689
  1687
		} 
slouken@689
  1688
slouken@689
  1689
		else {
slouken@689
  1690
			    __asm__ (
slouken@689
  1691
		    /* load in the source, and dst. */
slouken@689
  1692
		    "movd      (%0), %%mm0\n"		    /* mm0(s) = 0 0 0 0 | As Rs Gs Bs */
slouken@689
  1693
		    "movd      (%1), %%mm1\n"		    /* mm1(d) = 0 0 0 0 | Ad Rd Gd Bd */
slouken@689
  1694
slouken@689
  1695
		    /* Move the src alpha into mm2 */
slouken@689
  1696
slouken@689
  1697
		    /* if supporting pshufw */
slouken@689
  1698
		    /*"pshufw     $0x55, %%mm0, %%mm2\n" */ /* mm2 = 0 As 0 As |  0 As  0  As */
slouken@689
  1699
		    /*"psrlw     $8, %%mm2\n" */
slouken@689
  1700
		    
slouken@689
  1701
		    /* else: */
slouken@1542
  1702
		    "movd       %2,    %%mm2\n"
slouken@1542
  1703
		    "psrld      %%mm5, %%mm2\n"                /* mm2 = 0 0 0 0 | 0  0  0  As */
slouken@689
  1704
		    "punpcklwd	%%mm2, %%mm2\n"	            /* mm2 = 0 0 0 0 |  0 As  0  As */
slouken@689
  1705
		    "punpckldq	%%mm2, %%mm2\n"             /* mm2 = 0 As 0 As |  0 As  0  As */
slouken@1542
  1706
		    "pand       %%mm7, %%mm2\n"              /* to preserve dest alpha */
slouken@689
  1707
slouken@689
  1708
		    /* move the colors into words. */
slouken@689
  1709
		    "punpcklbw %%mm6, %%mm0\n"		    /* mm0 = 0 As 0 Rs | 0 Gs 0 Bs */
slouken@689
  1710
		    "punpcklbw %%mm6, %%mm1\n"              /* mm0 = 0 Ad 0 Rd | 0 Gd 0 Bd */
slouken@689
  1711
slouken@689
  1712
		    /* src - dst */
slouken@689
  1713
		    "psubw    %%mm1, %%mm0\n"		    /* mm0 = As-Ad Rs-Rd | Gs-Gd  Bs-Bd */
slouken@689
  1714
slouken@689
  1715
		    /* A * (src-dst) */
slouken@1542
  1716
		    "pmullw    %%mm2, %%mm0\n"		    /* mm0 = 0*As-d As*Rs-d | As*Gs-d  As*Bs-d */
slouken@1542
  1717
		    "psrlw     $8,    %%mm0\n"		    /* mm0 = 0>>8 Rc>>8 | Gc>>8  Bc>>8 */
slouken@1542
  1718
		    "paddb     %%mm1, %%mm0\n"		    /* mm0 = 0+Ad Rc+Rd | Gc+Gd  Bc+Bd */
slouken@689
  1719
slouken@689
  1720
		    "packuswb  %%mm0, %%mm0\n"              /* mm0 =             | Ac Rc Gc Bc */
slouken@689
  1721
		    
slouken@689
  1722
		    "movd      %%mm0, (%1)\n"               /* result in mm0 */
slouken@689
  1723
slouken@1542
  1724
		     : : "r" (srcp), "r" (dstp), "r" (alpha) );
slouken@689
  1725
slouken@689
  1726
		}
slouken@689
  1727
		++srcp;
slouken@689
  1728
		++dstp;
slouken@689
  1729
	    }, width);
slouken@1895
  1730
	    /* *INDENT-ON* */
slouken@1895
  1731
        srcp += srcskip;
slouken@1895
  1732
        dstp += dstskip;
slouken@1895
  1733
    }
slouken@689
  1734
slouken@1895
  1735
  __asm__("emms\n":);
slouken@689
  1736
}
slouken@1895
  1737
slouken@1542
  1738
/* End GCC_ASMBLIT*/
slouken@1542
  1739
slouken@1542
  1740
#elif MSVC_ASMBLIT
slouken@1542
  1741
/* fast (as in MMX with prefetch) ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
  1742
static void
slouken@1895
  1743
BlitRGBtoRGBPixelAlphaMMX3DNOW(SDL_BlitInfo * info)
slouken@1542
  1744
{
slouken@1895
  1745
    int width = info->d_width;
slouken@1895
  1746
    int height = info->d_height;
slouken@1895
  1747
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1748
    int srcskip = info->s_skip >> 2;
slouken@1895
  1749
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
  1750
    int dstskip = info->d_skip >> 2;
slouken@1895
  1751
    SDL_PixelFormat *sf = info->src;
slouken@1895
  1752
    Uint32 chanmask = sf->Rmask | sf->Gmask | sf->Bmask;
slouken@1895
  1753
    Uint32 amask = sf->Amask;
slouken@1895
  1754
    Uint32 ashift = sf->Ashift;
slouken@1895
  1755
    Uint64 multmask;
slouken@1542
  1756
slouken@1895
  1757
    __m64 src1, dst1, mm_alpha, mm_zero, dmask;
slouken@1542
  1758
slouken@1895
  1759
    mm_zero = _mm_setzero_si64();       /* 0 -> mm_zero */
slouken@1895
  1760
	/* *INDENT-OFF* */
slouken@1895
  1761
    multmask = ~(0xFFFFI64 << (ashift * 2));
slouken@1895
  1762
	/* *INDENT-ON* */
slouken@1895
  1763
    dmask = *(__m64 *) & multmask;      /* dst alpha mask -> dmask */
slouken@1895
  1764
slouken@1895
  1765
    while (height--) {
slouken@1895
  1766
	    /* *INDENT-OFF* */
slouken@1542
  1767
	    DUFFS_LOOP4({
slouken@1542
  1768
		Uint32 alpha;
slouken@1542
  1769
slouken@1542
  1770
		_m_prefetch(srcp + 16);
slouken@1542
  1771
		_m_prefetch(dstp + 16);
slouken@1542
  1772
slouken@1542
  1773
		alpha = *srcp & amask;
slouken@1542
  1774
		if (alpha == 0) {
slouken@1542
  1775
			/* do nothing */
slouken@1542
  1776
		} else if (alpha == amask) {
slouken@1542
  1777
			/* copy RGB, keep dst alpha */
slouken@1542
  1778
			*dstp = (*srcp & chanmask) | (*dstp & ~chanmask);
slouken@1542
  1779
		} else {
slouken@1542
  1780
			src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB)*/
slouken@1542
  1781
			src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
slouken@1542
  1782
slouken@1542
  1783
			dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
slouken@1542
  1784
			dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
  1785
slouken@1542
  1786
			mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
slouken@1542
  1787
			mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
slouken@1542
  1788
			mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
  1789
			mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
  1790
			mm_alpha = _mm_and_si64(mm_alpha, dmask); /* 000A0A0A -> mm_alpha, preserve dst alpha on add */
slouken@1542
  1791
slouken@1542
  1792
			/* blend */		    
slouken@1542
  1793
			src1 = _mm_sub_pi16(src1, dst1);/* src - dst -> src1 */
slouken@1542
  1794
			src1 = _mm_mullo_pi16(src1, mm_alpha); /* (src - dst) * alpha -> src1 */
slouken@1542
  1795
			src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1(000R0G0B) */
slouken@1542
  1796
			dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst) -> dst1(0A0R0G0B) */
slouken@1542
  1797
			dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
slouken@1542
  1798
			
slouken@1542
  1799
			*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
  1800
		}
slouken@1542
  1801
		++srcp;
slouken@1542
  1802
		++dstp;
slouken@1542
  1803
	    }, width);
slouken@1895
  1804
	    /* *INDENT-ON* */
slouken@1895
  1805
        srcp += srcskip;
slouken@1895
  1806
        dstp += dstskip;
slouken@1895
  1807
    }
slouken@1895
  1808
    _mm_empty();
slouken@1542
  1809
}
slouken@1895
  1810
slouken@1542
  1811
/* End MSVC_ASMBLIT */
slouken@1542
  1812
slouken@1542
  1813
#endif /* GCC_ASMBLIT, MSVC_ASMBLIT */
slouken@689
  1814
slouken@1
  1815
/* 16bpp special case for per-surface alpha=50%: blend 2 pixels in parallel */
slouken@1
  1816
slouken@1
  1817
/* blend a single 16 bit pixel at 50% */
slouken@1
  1818
#define BLEND16_50(d, s, mask)						\
slouken@1
  1819
	((((s & mask) + (d & mask)) >> 1) + (s & d & (~mask & 0xffff)))
slouken@1
  1820
slouken@1
  1821
/* blend two 16 bit pixels at 50% */
slouken@1
  1822
#define BLEND2x16_50(d, s, mask)					     \
slouken@1
  1823
	(((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) \
slouken@1
  1824
	 + (s & d & (~(mask | mask << 16))))
slouken@1
  1825
slouken@1895
  1826
static void
slouken@1895
  1827
Blit16to16SurfaceAlpha128(SDL_BlitInfo * info, Uint16 mask)
slouken@0
  1828
{
slouken@1895
  1829
    int width = info->d_width;
slouken@1895
  1830
    int height = info->d_height;
slouken@1895
  1831
    Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  1832
    int srcskip = info->s_skip >> 1;
slouken@1895
  1833
    Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  1834
    int dstskip = info->d_skip >> 1;
slouken@0
  1835
slouken@1895
  1836
    while (height--) {
slouken@1895
  1837
        if (((uintptr_t) srcp ^ (uintptr_t) dstp) & 2) {
slouken@1895
  1838
            /*
slouken@1895
  1839
             * Source and destination not aligned, pipeline it.
slouken@1895
  1840
             * This is mostly a win for big blits but no loss for
slouken@1895
  1841
             * small ones
slouken@1895
  1842
             */
slouken@1895
  1843
            Uint32 prev_sw;
slouken@1895
  1844
            int w = width;
slouken@1
  1845
slouken@1895
  1846
            /* handle odd destination */
slouken@1895
  1847
            if ((uintptr_t) dstp & 2) {
slouken@1895
  1848
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1849
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1850
                dstp++;
slouken@1895
  1851
                srcp++;
slouken@1895
  1852
                w--;
slouken@1895
  1853
            }
slouken@1895
  1854
            srcp++;             /* srcp is now 32-bit aligned */
slouken@1
  1855
slouken@1895
  1856
            /* bootstrap pipeline with first halfword */
slouken@1895
  1857
            prev_sw = ((Uint32 *) srcp)[-1];
slouken@1
  1858
slouken@1895
  1859
            while (w > 1) {
slouken@1895
  1860
                Uint32 sw, dw, s;
slouken@1895
  1861
                sw = *(Uint32 *) srcp;
slouken@1895
  1862
                dw = *(Uint32 *) dstp;
slouken@1443
  1863
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
  1864
                s = (prev_sw << 16) + (sw >> 16);
slouken@1443
  1865
#else
slouken@1895
  1866
                s = (prev_sw >> 16) + (sw << 16);
slouken@1443
  1867
#endif
slouken@1895
  1868
                prev_sw = sw;
slouken@1895
  1869
                *(Uint32 *) dstp = BLEND2x16_50(dw, s, mask);
slouken@1895
  1870
                dstp += 2;
slouken@1895
  1871
                srcp += 2;
slouken@1895
  1872
                w -= 2;
slouken@1895
  1873
            }
slouken@1
  1874
slouken@1895
  1875
            /* final pixel if any */
slouken@1895
  1876
            if (w) {
slouken@1895
  1877
                Uint16 d = *dstp, s;
slouken@1443
  1878
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
  1879
                s = (Uint16) prev_sw;
slouken@1443
  1880
#else
slouken@1895
  1881
                s = (Uint16) (prev_sw >> 16);
slouken@1443
  1882
#endif
slouken@1895
  1883
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1884
                srcp++;
slouken@1895
  1885
                dstp++;
slouken@1895
  1886
            }
slouken@1895
  1887
            srcp += srcskip - 1;
slouken@1895
  1888
            dstp += dstskip;
slouken@1895
  1889
        } else {
slouken@1895
  1890
            /* source and destination are aligned */
slouken@1895
  1891
            int w = width;
slouken@1
  1892
slouken@1895
  1893
            /* first odd pixel? */
slouken@1895
  1894
            if ((uintptr_t) srcp & 2) {
slouken@1895
  1895
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1896
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1897
                srcp++;
slouken@1895
  1898
                dstp++;
slouken@1895
  1899
                w--;
slouken@1895
  1900
            }
slouken@1895
  1901
            /* srcp and dstp are now 32-bit aligned */
slouken@1
  1902
slouken@1895
  1903
            while (w > 1) {
slouken@1895
  1904
                Uint32 sw = *(Uint32 *) srcp;
slouken@1895
  1905
                Uint32 dw = *(Uint32 *) dstp;
slouken@1895
  1906
                *(Uint32 *) dstp = BLEND2x16_50(dw, sw, mask);
slouken@1895
  1907
                srcp += 2;
slouken@1895
  1908
                dstp += 2;
slouken@1895
  1909
                w -= 2;
slouken@1895
  1910
            }
slouken@1
  1911
slouken@1895
  1912
            /* last odd pixel? */
slouken@1895
  1913
            if (w) {
slouken@1895
  1914
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1915
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1916
                srcp++;
slouken@1895
  1917
                dstp++;
slouken@1895
  1918
            }
slouken@1895
  1919
            srcp += srcskip;
slouken@1895
  1920
            dstp += dstskip;
slouken@1895
  1921
        }
slouken@1895
  1922
    }
slouken@1
  1923
}
slouken@1
  1924
slouken@1542
  1925
#if GCC_ASMBLIT
slouken@689
  1926
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1895
  1927
static void
slouken@1895
  1928
Blit565to565SurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@689
  1929
{
slouken@1895
  1930
    unsigned alpha = info->src->alpha;  /* downscale alpha to 5 bits */
slouken@1895
  1931
    if (alpha == 128) {
slouken@1895
  1932
        Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1895
  1933
    } else {
slouken@1895
  1934
        int width = info->d_width;
slouken@1895
  1935
        int height = info->d_height;
slouken@1895
  1936
        Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  1937
        int srcskip = info->s_skip >> 1;
slouken@1895
  1938
        Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  1939
        int dstskip = info->d_skip >> 1;
slouken@1895
  1940
        Uint32 s, d;
slouken@1895
  1941
        Uint8 load[8];
slouken@689
  1942
slouken@1895
  1943
        alpha &= ~(1 + 2 + 4);  /* cut alpha to get the exact same behaviour */
slouken@1895
  1944
        *(Uint64 *) load = alpha;
slouken@1895
  1945
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1895
  1946
slouken@1895
  1947
        movq_m2r(*load, mm0);   /* alpha(0000000A) -> mm0 */
slouken@1895
  1948
        punpcklwd_r2r(mm0, mm0);        /* 00000A0A -> mm0 */
slouken@1895
  1949
        punpcklwd_r2r(mm0, mm0);        /* 0A0A0A0A -> mm0 */
slouken@1895
  1950
        /* position alpha to allow for mullo and mulhi on diff channels
slouken@1895
  1951
           to reduce the number of operations */
slouken@1895
  1952
        psllq_i2r(3, mm0);
slouken@1895
  1953
slouken@1895
  1954
        /* Setup the 565 color channel masks */
slouken@1895
  1955
        *(Uint64 *) load = 0x07E007E007E007E0ULL;
slouken@1895
  1956
        movq_m2r(*load, mm4);   /* MASKGREEN -> mm4 */
slouken@1895
  1957
        *(Uint64 *) load = 0x001F001F001F001FULL;
slouken@1895
  1958
        movq_m2r(*load, mm7);   /* MASKBLUE -> mm7 */
slouken@1895
  1959
        while (height--) {
slouken@1895
  1960
			/* *INDENT-OFF* */
slouken@1542
  1961
			DUFFS_LOOP_QUATRO2(
slouken@1542
  1962
			{
slouken@1542
  1963
				s = *srcp++;
slouken@689
  1964
				d = *dstp;
slouken@689
  1965
				/*
slouken@689
  1966
				 * shift out the middle component (green) to
slouken@689
  1967
				 * the high 16 bits, and process all three RGB
slouken@689
  1968
				 * components at the same time.
slouken@689
  1969
				 */
slouken@689
  1970
				s = (s | s << 16) & 0x07e0f81f;
slouken@689
  1971
				d = (d | d << 16) & 0x07e0f81f;
slouken@689
  1972
				d += (s - d) * alpha >> 5;
slouken@689
  1973
				d &= 0x07e0f81f;
slouken@689
  1974
				*dstp++ = d | d >> 16;
slouken@1542
  1975
			},{
slouken@1542
  1976
				s = *srcp++;
slouken@689
  1977
				d = *dstp;
slouken@689
  1978
				/*
slouken@689
  1979
				 * shift out the middle component (green) to
slouken@689
  1980
				 * the high 16 bits, and process all three RGB
slouken@689
  1981
				 * components at the same time.
slouken@689
  1982
				 */
slouken@689
  1983
				s = (s | s << 16) & 0x07e0f81f;
slouken@689
  1984
				d = (d | d << 16) & 0x07e0f81f;
slouken@689
  1985
				d += (s - d) * alpha >> 5;
slouken@689
  1986
				d &= 0x07e0f81f;
slouken@689
  1987
				*dstp++ = d | d >> 16;
slouken@1542
  1988
				s = *srcp++;
slouken@689
  1989
				d = *dstp;
slouken@689
  1990
				/*
slouken@689
  1991
				 * shift out the middle component (green) to
slouken@689
  1992
				 * the high 16 bits, and process all three RGB
slouken@689
  1993
				 * components at the same time.
slouken@689
  1994
				 */
slouken@689
  1995
				s = (s | s << 16) & 0x07e0f81f;
slouken@689
  1996
				d = (d | d << 16) & 0x07e0f81f;
slouken@689
  1997
				d += (s - d) * alpha >> 5;
slouken@689
  1998
				d &= 0x07e0f81f;
slouken@689
  1999
				*dstp++ = d | d >> 16;
slouken@1542
  2000
			},{
slouken@1542
  2001
				movq_m2r((*srcp), mm2);/* 4 src pixels -> mm2 */
slouken@1542
  2002
				movq_m2r((*dstp), mm3);/* 4 dst pixels -> mm3 */
slouken@1542
  2003
slouken@1542
  2004
				/* red -- does not need a mask since the right shift clears
slouken@1542
  2005
				   the uninteresting bits */
slouken@1542
  2006
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2007
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2008
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 [000r 000r 000r 000r] */
slouken@1542
  2009
				psrlw_i2r(11, mm6); /* mm6 >> 11 -> mm6 [000r 000r 000r 000r] */
slouken@1542
  2010
slouken@1542
  2011
				/* blend */
slouken@1542
  2012
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2013
				pmullw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2014
				/* alpha used is actually 11 bits
slouken@1542
  2015
				   11 + 5 = 16 bits, so the sign bits are lost */
slouken@1542
  2016
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 */
slouken@1542
  2017
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2018
				psllw_i2r(11, mm6); /* mm6 << 11 -> mm6 */
slouken@1542
  2019
slouken@1542
  2020
				movq_r2r(mm6, mm1); /* save new reds in dsts */
slouken@1542
  2021
slouken@1542
  2022
				/* green -- process the bits in place */
slouken@1542
  2023
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2024
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2025
				pand_r2r(mm4, mm5); /* src & MASKGREEN -> mm5 */
slouken@1542
  2026
				pand_r2r(mm4, mm6); /* dst & MASKGREEN -> mm6 */
slouken@1542
  2027
slouken@1542
  2028
				/* blend */
slouken@1542
  2029
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2030
				pmulhw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2031
				/* 11 + 11 - 16 = 6 bits, so all the lower uninteresting
slouken@1542
  2032
				   bits are gone and the sign bits present */
slouken@1542
  2033
				psllw_i2r(5, mm5); /* mm5 << 5 -> mm5 */
slouken@1542
  2034
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2035
slouken@1542
  2036
				por_r2r(mm6, mm1); /* save new greens in dsts */
slouken@1542
  2037
slouken@1542
  2038
				/* blue */
slouken@1542
  2039
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2040
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2041
				pand_r2r(mm7, mm5); /* src & MASKBLUE -> mm5[000b 000b 000b 000b] */
slouken@1542
  2042
				pand_r2r(mm7, mm6); /* dst & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  2043
slouken@1542
  2044
				/* blend */
slouken@1542
  2045
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2046
				pmullw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2047
				/* 11 + 5 = 16 bits, so the sign bits are lost and
slouken@1542
  2048
				   the interesting bits will need to be MASKed */
slouken@1542
  2049
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 */
slouken@1542
  2050
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2051
				pand_r2r(mm7, mm6); /* mm6 & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  2052
slouken@1542
  2053
				por_r2r(mm6, mm1); /* save new blues in dsts */
slouken@1542
  2054
slouken@1542
  2055
				movq_r2m(mm1, *dstp); /* mm1 -> 4 dst pixels */
slouken@1542
  2056
slouken@1542
  2057
				srcp += 4;
slouken@1542
  2058
				dstp += 4;
slouken@1542
  2059
			}, width);			
slouken@1895
  2060
			/* *INDENT-ON* */
slouken@1895
  2061
            srcp += srcskip;
slouken@1895
  2062
            dstp += dstskip;
slouken@1895
  2063
        }
slouken@1895
  2064
        emms();
slouken@1895
  2065
    }
slouken@689
  2066
}
slouken@689
  2067
slouken@689
  2068
/* fast RGB555->RGB555 blending with surface alpha */
slouken@1895
  2069
static void
slouken@1895
  2070
Blit555to555SurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@689
  2071
{
slouken@1895
  2072
    unsigned alpha = info->src->alpha;  /* downscale alpha to 5 bits */
slouken@1895
  2073
    if (alpha == 128) {
slouken@1895
  2074
        Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1895
  2075
    } else {
slouken@1895
  2076
        int width = info->d_width;
slouken@1895
  2077
        int height = info->d_height;
slouken@1895
  2078
        Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  2079
        int srcskip = info->s_skip >> 1;
slouken@1895
  2080
        Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  2081
        int dstskip = info->d_skip >> 1;
slouken@1895
  2082
        Uint32 s, d;
slouken@1895
  2083
        Uint8 load[8];
slouken@689
  2084
slouken@1895
  2085
        alpha &= ~(1 + 2 + 4);  /* cut alpha to get the exact same behaviour */
slouken@1895
  2086
        *(Uint64 *) load = alpha;
slouken@1895
  2087
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1542
  2088
slouken@1895
  2089
        movq_m2r(*load, mm0);   /* alpha(0000000A) -> mm0 */
slouken@1895
  2090
        punpcklwd_r2r(mm0, mm0);        /* 00000A0A -> mm0 */
slouken@1895
  2091
        punpcklwd_r2r(mm0, mm0);        /* 0A0A0A0A -> mm0 */
slouken@1895
  2092
        /* position alpha to allow for mullo and mulhi on diff channels
slouken@1895
  2093
           to reduce the number of operations */
slouken@1895
  2094
        psllq_i2r(3, mm0);
slouken@1895
  2095
slouken@1895
  2096
        /* Setup the 555 color channel masks */
slouken@1895
  2097
        *(Uint64 *) load = 0x03E003E003E003E0ULL;
slouken@1895
  2098
        movq_m2r(*load, mm4);   /* MASKGREEN -> mm4 */
slouken@1895
  2099
        *(Uint64 *) load = 0x001F001F001F001FULL;
slouken@1895
  2100
        movq_m2r(*load, mm7);   /* MASKBLUE -> mm7 */
slouken@1895
  2101
        while (height--) {
slouken@1895
  2102
			/* *INDENT-OFF* */
slouken@1542
  2103
			DUFFS_LOOP_QUATRO2(
slouken@1542
  2104
			{
slouken@1542
  2105
				s = *srcp++;
slouken@689
  2106
				d = *dstp;
slouken@689
  2107
				/*
slouken@689
  2108
				 * shift out the middle component (green) to
slouken@689
  2109
				 * the high 16 bits, and process all three RGB
slouken@689
  2110
				 * components at the same time.
slouken@689
  2111
				 */
slouken@689
  2112
				s = (s | s << 16) & 0x03e07c1f;
slouken@689
  2113
				d = (d | d << 16) & 0x03e07c1f;
slouken@689
  2114
				d += (s - d) * alpha >> 5;
slouken@689
  2115
				d &= 0x03e07c1f;
slouken@689
  2116
				*dstp++ = d | d >> 16;
slouken@1542
  2117
			},{
slouken@1542
  2118
				s = *srcp++;
slouken@689
  2119
				d = *dstp;
slouken@689
  2120
				/*
slouken@689
  2121
				 * shift out the middle component (green) to
slouken@689
  2122
				 * the high 16 bits, and process all three RGB
slouken@689
  2123
				 * components at the same time.
slouken@689
  2124
				 */
slouken@689
  2125
				s = (s | s << 16) & 0x03e07c1f;
slouken@689
  2126
				d = (d | d << 16) & 0x03e07c1f;
slouken@689
  2127
				d += (s - d) * alpha >> 5;
slouken@689
  2128
				d &= 0x03e07c1f;
slouken@689
  2129
				*dstp++ = d | d >> 16;
slouken@689
  2130
			        s = *srcp++;
slouken@689
  2131
				d = *dstp;
slouken@689
  2132
				/*
slouken@689
  2133
				 * shift out the middle component (green) to
slouken@689
  2134
				 * the high 16 bits, and process all three RGB
slouken@689
  2135
				 * components at the same time.
slouken@689
  2136
				 */
slouken@689
  2137
				s = (s | s << 16) & 0x03e07c1f;
slouken@689
  2138
				d = (d | d << 16) & 0x03e07c1f;
slouken@689
  2139
				d += (s - d) * alpha >> 5;
slouken@689
  2140
				d &= 0x03e07c1f;
slouken@689
  2141
				*dstp++ = d | d >> 16;
slouken@1542
  2142
			},{
slouken@1542
  2143
				movq_m2r((*srcp), mm2);/* 4 src pixels -> mm2 */
slouken@1542
  2144
				movq_m2r((*dstp), mm3);/* 4 dst pixels -> mm3 */
slouken@1542
  2145
slouken@1542
  2146
				/* red -- process the bits in place */
slouken@1542
  2147
				psllq_i2r(5, mm4); /* turn MASKGREEN into MASKRED */
slouken@1542
  2148
					/* by reusing the GREEN mask we free up another mmx
slouken@1542
  2149
					   register to accumulate the result */
slouken@1542
  2150
slouken@1542
  2151
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2152
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2153
				pand_r2r(mm4, mm5); /* src & MASKRED -> mm5 */
slouken@1542
  2154
				pand_r2r(mm4, mm6); /* dst & MASKRED -> mm6 */
slouken@1542
  2155
slouken@1542
  2156
				/* blend */
slouken@1542
  2157
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2158
				pmulhw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2159
				/* 11 + 15 - 16 = 10 bits, uninteresting bits will be
slouken@1542
  2160
				   cleared by a MASK below */
slouken@1542
  2161
				psllw_i2r(5, mm5); /* mm5 << 5 -> mm5 */
slouken@1542
  2162
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2163
				pand_r2r(mm4, mm6); /* mm6 & MASKRED -> mm6 */
slouken@1542
  2164
slouken@1542
  2165
				psrlq_i2r(5, mm4); /* turn MASKRED back into MASKGREEN */
slouken@1542
  2166
slouken@1542
  2167
				movq_r2r(mm6, mm1); /* save new reds in dsts */
slouken@1542
  2168
slouken@1542
  2169
				/* green -- process the bits in place */
slouken@1542
  2170
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2171
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2172
				pand_r2r(mm4, mm5); /* src & MASKGREEN -> mm5 */
slouken@1542
  2173
				pand_r2r(mm4, mm6); /* dst & MASKGREEN -> mm6 */
slouken@1542
  2174
slouken@1542
  2175
				/* blend */
slouken@1542
  2176
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2177
				pmulhw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2178
				/* 11 + 10 - 16 = 5 bits,  so all the lower uninteresting
slouken@1542
  2179
				   bits are gone and the sign bits present */
slouken@1542
  2180
				psllw_i2r(5, mm5); /* mm5 << 5 -> mm5 */
slouken@1542
  2181
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2182
slouken@1542
  2183
				por_r2r(mm6, mm1); /* save new greens in dsts */
slouken@1542
  2184
slouken@1542
  2185
				/* blue */
slouken@1542
  2186
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2187
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2188
				pand_r2r(mm7, mm5); /* src & MASKBLUE -> mm5[000b 000b 000b 000b] */
slouken@1542
  2189
				pand_r2r(mm7, mm6); /* dst & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  2190
slouken@1542
  2191
				/* blend */
slouken@1542
  2192
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2193
				pmullw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2194
				/* 11 + 5 = 16 bits, so the sign bits are lost and
slouken@1542
  2195
				   the interesting bits will need to be MASKed */
slouken@1542
  2196
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 */
slouken@1542
  2197
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2198
				pand_r2r(mm7, mm6); /* mm6 & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  2199
slouken@1542
  2200
				por_r2r(mm6, mm1); /* save new blues in dsts */
slouken@1542
  2201
slouken@1542
  2202
				movq_r2m(mm1, *dstp);/* mm1 -> 4 dst pixels */
slouken@1542
  2203
slouken@1542
  2204
				srcp += 4;
slouken@1542
  2205
				dstp += 4;
slouken@1895
  2206
			}, width);
slouken@1895
  2207
			/* *INDENT-ON* */
slouken@1895
  2208
            srcp += srcskip;
slouken@1895
  2209
            dstp += dstskip;
slouken@1895
  2210
        }
slouken@1895
  2211
        emms();
slouken@1895
  2212
    }
slouken@689
  2213
}
slouken@1895
  2214
slouken@1542
  2215
/* End GCC_ASMBLIT */
slouken@1542
  2216
slouken@1542
  2217
#elif MSVC_ASMBLIT
slouken@1542
  2218
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1895
  2219
static void
slouken@1895
  2220
Blit565to565SurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@1542
  2221
{
slouken@1895
  2222
    unsigned alpha = info->src->alpha;
slouken@1895
  2223
    if (alpha == 128) {
slouken@1895
  2224
        Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1895
  2225
    } else {
slouken@1895
  2226
        int width = info->d_width;
slouken@1895
  2227
        int height = info->d_height;
slouken@1895
  2228
        Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  2229
        int srcskip = info->s_skip >> 1;
slouken@1895
  2230
        Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  2231
        int dstskip = info->d_skip >> 1;
slouken@1895
  2232
        Uint32 s, d;
slouken@1895
  2233
slouken@1895
  2234
        __m64 src1, dst1, src2, dst2, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  2235
slouken@1895
  2236
        alpha &= ~(1 + 2 + 4);  /* cut alpha to get the exact same behaviour */
slouken@1895
  2237
        mm_alpha = _mm_set_pi32(0, alpha);      /* 0000000A -> mm_alpha */
slouken@1895
  2238
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1542
  2239
slouken@1895
  2240
        mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha);       /* 00000A0A -> mm_alpha */
slouken@1895
  2241
        mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha);       /* 0A0A0A0A -> mm_alpha */
slouken@1895
  2242
        /* position alpha to allow for mullo and mulhi on diff channels
slouken@1895
  2243
           to reduce the number of operations */
slouken@1895
  2244
        mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1895
  2245
slouken@1895
  2246
        /* Setup the 565 color channel masks */
slouken@1895
  2247
        gmask = _mm_set_pi32(0x07E007E0, 0x07E007E0);   /* MASKGREEN -> gmask */
slouken@1895
  2248
        bmask = _mm_set_pi32(0x001F001F, 0x001F001F);   /* MASKBLUE -> bmask */
slouken@1895
  2249
slouken@1895
  2250
        while (height--) {
slouken@1895
  2251
			/* *INDENT-OFF* */
slouken@1542
  2252
			DUFFS_LOOP_QUATRO2(
slouken@1542
  2253
			{
slouken@1542
  2254
				s = *srcp++;
slouken@1542
  2255
				d = *dstp;
slouken@1542
  2256
				/*
slouken@1542
  2257
				 * shift out the middle component (green) to
slouken@1542
  2258
				 * the high 16 bits, and process all three RGB
slouken@1542
  2259
				 * components at the same time.
slouken@1542
  2260
				 */
slouken@1542
  2261
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  2262
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  2263
				d += (s - d) * alpha >> 5;
slouken@1542
  2264
				d &= 0x07e0f81f;
slouken@1546
  2265
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2266
			},{
slouken@1542
  2267
				s = *srcp++;
slouken@1542
  2268
				d = *dstp;
slouken@1542
  2269
				/*
slouken@1542
  2270
				 * shift out the middle component (green) to
slouken@1542
  2271
				 * the high 16 bits, and process all three RGB
slouken@1542
  2272
				 * components at the same time.
slouken@1542
  2273
				 */
slouken@1542
  2274
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  2275
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  2276
				d += (s - d) * alpha >> 5;
slouken@1542
  2277
				d &= 0x07e0f81f;
slouken@1546
  2278
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2279
				s = *srcp++;
slouken@1542
  2280
				d = *dstp;
slouken@1542
  2281
				/*
slouken@1542
  2282
				 * shift out the middle component (green) to
slouken@1542
  2283
				 * the high 16 bits, and process all three RGB
slouken@1542
  2284
				 * components at the same time.
slouken@1542
  2285
				 */
slouken@1542
  2286
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  2287
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  2288
				d += (s - d) * alpha >> 5;
slouken@1542
  2289
				d &= 0x07e0f81f;
slouken@1546
  2290
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2291
			},{
slouken@1542
  2292
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  2293
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  2294
slouken@1542
  2295
				/* red */
slouken@1542
  2296
				src2 = src1;
slouken@1542
  2297
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 [000r 000r 000r 000r] */
slouken@1542
  2298
slouken@1542
  2299
				dst2 = dst1;
slouken@1542
  2300
				dst2 = _mm_srli_pi16(dst2, 11); /* dst2 >> 11 -> dst2 [000r 000r 000r 000r] */
slouken@1542
  2301
slouken@1542
  2302
				/* blend */
slouken@1542
  2303
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2304
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2305
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  2306
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2307
				dst2 = _mm_slli_pi16(dst2, 11); /* dst2 << 11 -> dst2 */
slouken@1542
  2308
slouken@1542
  2309
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  2310
slouken@1542
  2311
				/* green -- process the bits in place */
slouken@1542
  2312
				src2 = src1;
slouken@1542
  2313
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  2314
slouken@1542
  2315
				dst2 = dst1;
slouken@1542
  2316
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  2317
slouken@1542
  2318
				/* blend */
slouken@1542
  2319
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2320
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2321
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  2322
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2323
slouken@1542
  2324
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  2325
slouken@1542
  2326
				/* blue */
slouken@1542
  2327
				src2 = src1;
slouken@1542
  2328
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  2329
slouken@1542
  2330
				dst2 = dst1;
slouken@1542
  2331
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  2332
slouken@1542
  2333
				/* blend */
slouken@1542
  2334
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2335
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2336
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  2337
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2338
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  2339
slouken@1542
  2340
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  2341
slouken@1542
  2342
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  2343
slouken@1542
  2344
				srcp += 4;
slouken@1542
  2345
				dstp += 4;
slouken@1895
  2346
			}, width);
slouken@1895
  2347
			/* *INDENT-ON* */
slouken@1895
  2348
            srcp += srcskip;
slouken@1895
  2349
            dstp += dstskip;
slouken@1895
  2350
        }
slouken@1895
  2351
        _mm_empty();
slouken@1895
  2352
    }
slouken@1542
  2353
}
slouken@1542
  2354
slouken@1542
  2355
/* fast RGB555->RGB555 blending with surface alpha */
slouken@1895
  2356
static void
slouken@1895
  2357
Blit555to555SurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@1542
  2358
{
slouken@1895
  2359
    unsigned alpha = info->src->alpha;
slouken@1895
  2360
    if (alpha == 128) {
slouken@1895
  2361
        Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1895
  2362
    } else {
slouken@1895
  2363
        int width = info->d_width;
slouken@1895
  2364
        int height = info->d_height;
slouken@1895
  2365
        Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  2366
        int srcskip = info->s_skip >> 1;
slouken@1895
  2367
        Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  2368
        int dstskip = info->d_skip >> 1;
slouken@1895
  2369
        Uint32 s, d;
slouken@1895
  2370
slouken@1895
  2371
        __m64 src1, dst1, src2, dst2, rmask, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  2372
slouken@1895
  2373
        alpha &= ~(1 + 2 + 4);  /* cut alpha to get the exact same behaviour */
slouken@1895
  2374
        mm_alpha = _mm_set_pi32(0, alpha);      /* 0000000A -> mm_alpha */
slouken@1895
  2375
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1542
  2376
slouken@1895
  2377
        mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha);       /* 00000A0A -> mm_alpha */
slouken@1895
  2378
        mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha);       /* 0A0A0A0A -> mm_alpha */
slouken@1895
  2379
        /* position alpha to allow for mullo and mulhi on diff channels
slouken@1895
  2380
           to reduce the number of operations */
slouken@1895
  2381
        mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1542
  2382
slouken@1895
  2383
        /* Setup the 555 color channel masks */
slouken@1895
  2384
        rmask = _mm_set_pi32(0x7C007C00, 0x7C007C00);   /* MASKRED -> rmask */
slouken@1895
  2385
        gmask = _mm_set_pi32(0x03E003E0, 0x03E003E0);   /* MASKGREEN -> gmask */
slouken@1895
  2386
        bmask = _mm_set_pi32(0x001F001F, 0x001F001F);   /* MASKBLUE -> bmask */
slouken@1895
  2387
slouken@1895
  2388
        while (height--) {
slouken@1895
  2389
			/* *INDENT-OFF* */
slouken@1542
  2390
			DUFFS_LOOP_QUATRO2(
slouken@1542
  2391
			{
slouken@1542
  2392
				s = *srcp++;
slouken@1542
  2393
				d = *dstp;
slouken@1542
  2394
				/*
slouken@1542
  2395
				 * shift out the middle component (green) to
slouken@1542
  2396
				 * the high 16 bits, and process all three RGB
slouken@1542
  2397
				 * components at the same time.
slouken@1542
  2398
				 */
slouken@1542
  2399
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  2400
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  2401
				d += (s - d) * alpha >> 5;
slouken@1542
  2402
				d &= 0x03e07c1f;
slouken@1546
  2403
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2404
			},{
slouken@1542
  2405
				s = *srcp++;
slouken@1542
  2406
				d = *dstp;
slouken@1542
  2407
				/*
slouken@1542
  2408
				 * shift out the middle component (green) to
slouken@1542
  2409
				 * the high 16 bits, and process all three RGB
slouken@1542
  2410
				 * components at the same time.
slouken@1542
  2411
				 */
slouken@1542
  2412
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  2413
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  2414
				d += (s - d) * alpha >> 5;
slouken@1542
  2415
				d &= 0x03e07c1f;
slouken@1546
  2416
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2417
			        s = *srcp++;
slouken@1542
  2418
				d = *dstp;
slouken@1542
  2419
				/*
slouken@1542
  2420
				 * shift out the middle component (green) to
slouken@1542
  2421
				 * the high 16 bits, and process all three RGB
slouken@1542
  2422
				 * components at the same time.
slouken@1542
  2423
				 */
slouken@1542
  2424
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  2425
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  2426
				d += (s - d) * alpha >> 5;
slouken@1542
  2427
				d &= 0x03e07c1f;
slouken@1546
  2428
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2429
			},{
slouken@1542
  2430
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  2431
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  2432
slouken@1542
  2433
				/* red -- process the bits in place */
slouken@1542
  2434
				src2 = src1;
slouken@1542
  2435
				src2 = _mm_and_si64(src2, rmask); /* src & MASKRED -> src2 */
slouken@1542
  2436
slouken@1542
  2437
				dst2 = dst1;
slouken@1542
  2438
				dst2 = _mm_and_si64(dst2, rmask); /* dst & MASKRED -> dst2 */
slouken@1542
  2439
slouken@1542
  2440
				/* blend */
slouken@1542
  2441
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2442
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2443
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  2444
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2445
				dst2 = _mm_and_si64(dst2, rmask); /* dst2 & MASKRED -> dst2 */
slouken@1542
  2446
slouken@1542
  2447
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  2448
				
slouken@1542
  2449
				/* green -- process the bits in place */
slouken@1542
  2450
				src2 = src1;
slouken@1542
  2451
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  2452
slouken@1542
  2453
				dst2 = dst1;
slouken@1542
  2454
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  2455
slouken@1542
  2456
				/* blend */
slouken@1542
  2457
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2458
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2459
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  2460
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2461
slouken@1542
  2462
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  2463
slouken@1542
  2464
				/* blue */
slouken@1542
  2465
				src2 = src1; /* src -> src2 */
slouken@1542
  2466
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  2467
slouken@1542
  2468
				dst2 = dst1; /* dst -> dst2 */
slouken@1542
  2469
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  2470
slouken@1542
  2471
				/* blend */
slouken@1542
  2472
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2473
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2474
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  2475
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2476
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  2477
slouken@1542
  2478
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  2479
slouken@1542
  2480
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  2481
slouken@1542
  2482
				srcp += 4;
slouken@1542
  2483
				dstp += 4;
slouken@1895
  2484
			}, width);
slouken@1895
  2485
			/* *INDENT-ON* */
slouken@1895
  2486
            srcp += srcskip;
slouken@1895
  2487
            dstp += dstskip;
slouken@1895
  2488
        }
slouken@1895
  2489
        _mm_empty();
slouken@1895
  2490
    }
slouken@1542
  2491
}
slouken@1542
  2492
#endif /* GCC_ASMBLIT, MSVC_ASMBLIT */
slouken@689
  2493
slouken@1
  2494
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1895
  2495
static void
slouken@1895
  2496
Blit565to565SurfaceAlpha(SDL_BlitInfo * info)
slouken@1
  2497
{
slouken@1895
  2498
    unsigned alpha = info->src->alpha;
slouken@1895
  2499
    if (alpha == 128) {
slouken@1895
  2500
        Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1895
  2501
    } else {
slouken@1895
  2502
        int width = info->d_width;
slouken@1895
  2503
        int height = info->d_height;
slouken@1895
  2504
        Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  2505
        int srcskip = info->s_skip >> 1;
slouken@1895
  2506
        Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  2507
        int dstskip = info->d_skip >> 1;
slouken@1895
  2508
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1
  2509
slouken@1895
  2510
        while (height--) {
slouken@1895
  2511
			/* *INDENT-OFF* */
slouken@1
  2512
			DUFFS_LOOP4({
slouken@1
  2513
				Uint32 s = *srcp++;
slouken@1
  2514
				Uint32 d = *dstp;
slouken@1
  2515
				/*
slouken@1
  2516
				 * shift out the middle component (green) to
slouken@1
  2517
				 * the high 16 bits, and process all three RGB
slouken@1
  2518
				 * components at the same time.
slouken@1
  2519
				 */
slouken@1
  2520
				s = (s | s << 16) & 0x07e0f81f;
slouken@1
  2521
				d = (d | d << 16) & 0x07e0f81f;
slouken@1
  2522
				d += (s - d) * alpha >> 5;
slouken@1
  2523
				d &= 0x07e0f81f;
slouken@1428
  2524
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  2525
			}, width);
slouken@1895
  2526
			/* *INDENT-ON* */
slouken@1895
  2527
            srcp += srcskip;
slouken@1895
  2528
            dstp += dstskip;
slouken@1895
  2529
        }
slouken@1895
  2530
    }
slouken@0
  2531
}
slouken@0
  2532
slouken@0
  2533
/* fast RGB555->RGB555 blending with surface alpha */
slouken@1895
  2534
static void
slouken@1895
  2535
Blit555to555SurfaceAlpha(SDL_BlitInfo * info)
slouken@0
  2536
{
slouken@1895
  2537
    unsigned alpha = info->src->alpha;  /* downscale alpha to 5 bits */
slouken@1895
  2538
    if (alpha == 128) {
slouken@1895
  2539
        Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1895
  2540
    } else {
slouken@1895
  2541
        int width = info->d_width;
slouken@1895
  2542
        int height = info->d_height;
slouken@1895
  2543
        Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  2544
        int srcskip = info->s_skip >> 1;
slouken@1895
  2545
        Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  2546
        int dstskip = info->d_skip >> 1;
slouken@1895
  2547
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@0
  2548
slouken@1895
  2549
        while (height--) {
slouken@1895
  2550
			/* *INDENT-OFF* */
slouken@1
  2551
			DUFFS_LOOP4({
slouken@1
  2552
				Uint32 s = *srcp++;
slouken@1
  2553
				Uint32 d = *dstp;
slouken@1
  2554
				/*
slouken@1
  2555
				 * shift out the middle component (green) to
slouken@1
  2556
				 * the high 16 bits, and process all three RGB
slouken@1
  2557
				 * components at the same time.
slouken@1
  2558
				 */
slouken@1
  2559
				s = (s | s << 16) & 0x03e07c1f;
slouken@1
  2560
				d = (d | d << 16) & 0x03e07c1f;
slouken@1
  2561
				d += (s - d) * alpha >> 5;
slouken@1
  2562
				d &= 0x03e07c1f;
slouken@1428
  2563
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  2564
			}, width);
slouken@1895
  2565
			/* *INDENT-ON* */
slouken@1895
  2566
            srcp += srcskip;
slouken@1895
  2567
            dstp += dstskip;
slouken@1895
  2568
        }
slouken@1895
  2569
    }
slouken@0
  2570
}
slouken@0
  2571
slouken@0
  2572
/* fast ARGB8888->RGB565 blending with pixel alpha */
slouken@1895
  2573
static void
slouken@1895
  2574
BlitARGBto565PixelAlpha(SDL_BlitInfo * info)
slouken@0
  2575
{
slouken@1895
  2576
    int width = info->d_width;
slouken@1895
  2577
    int height = info->d_height;
slouken@1895
  2578
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  2579
    int srcskip = info->s_skip >> 2;
slouken@1895
  2580
    Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  2581
    int dstskip = info->d_skip >> 1;
slouken@0
  2582
slouken@1895
  2583
    while (height--) {
slouken@1895
  2584
	    /* *INDENT-OFF* */
slouken@0
  2585
	    DUFFS_LOOP4({
slouken@0
  2586
		Uint32 s = *srcp;
slouken@0
  2587
		unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  2588
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  2589
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  2590
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  2591
		   Benchmark this! */
slouken@689
  2592
		if(alpha) {   
slouken@689
  2593
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  2594
		    *dstp = (Uint16)((s >> 8 & 0xf800) + (s >> 5 & 0x7e0) + (s >> 3  & 0x1f));
slouken@689
  2595
		  } else {
slouken@0
  2596
		    Uint32 d = *dstp;
slouken@0
  2597
		    /*
slouken@0
  2598
		     * convert source and destination to G0RAB65565
slouken@0
  2599
		     * and blend all components at the same time
slouken@0
  2600
		     */
slouken@0
  2601
		    s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
slouken@0
  2602
		      + (s >> 3 & 0x1f);
slouken@0
  2603
		    d = (d | d << 16) & 0x07e0f81f;
slouken@0
  2604
		    d += (s - d) * alpha >> 5;
slouken@0
  2605
		    d &= 0x07e0f81f;
slouken@1428
  2606
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  2607
		  }
slouken@0
  2608
		}
slouken@0
  2609
		srcp++;
slouken@0
  2610
		dstp++;
slouken@0
  2611
	    }, width);
slouken@1895
  2612
	    /* *INDENT-ON* */
slouken@1895
  2613
        srcp += srcskip;
slouken@1895
  2614
        dstp += dstskip;
slouken@1895
  2615
    }
slouken@0
  2616
}
slouken@0
  2617
slouken@0
  2618
/* fast ARGB8888->RGB555 blending with pixel alpha */
slouken@1895
  2619
static void
slouken@1895
  2620
BlitARGBto555PixelAlpha(SDL_BlitInfo * info)
slouken@0
  2621
{
slouken@1895
  2622
    int width = info->d_width;
slouken@1895
  2623
    int height = info->d_height;
slouken@1895
  2624
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  2625
    int srcskip = info->s_skip >> 2;
slouken@1895
  2626
    Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  2627
    int dstskip = info->d_skip >> 1;
slouken@0
  2628
slouken@1895
  2629
    while (height--) {
slouken@1895
  2630
	    /* *INDENT-OFF* */
slouken@0
  2631
	    DUFFS_LOOP4({
slouken@0
  2632
		unsigned alpha;
slouken@0
  2633
		Uint32 s = *srcp;
slouken@0
  2634
		alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  2635
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  2636
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  2637
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  2638
		   Benchmark this! */
slouken@689
  2639
		if(alpha) {   
slouken@689
  2640
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  2641
		    *dstp = (Uint16)((s >> 9 & 0x7c00) + (s >> 6 & 0x3e0) + (s >> 3  & 0x1f));
slouken@689
  2642
		  } else {
slouken@0
  2643
		    Uint32 d = *dstp;
slouken@0
  2644
		    /*
slouken@0
  2645
		     * convert source and destination to G0RAB65565
slouken@0
  2646
		     * and blend all components at the same time
slouken@0
  2647
		     */
slouken@0
  2648
		    s = ((s & 0xf800) << 10) + (s >> 9 & 0x7c00)
slouken@0
  2649
		      + (s >> 3 & 0x1f);
slouken@0
  2650
		    d = (d | d << 16) & 0x03e07c1f;
slouken@0
  2651
		    d += (s - d) * alpha >> 5;
slouken@0
  2652
		    d &= 0x03e07c1f;
slouken@1428
  2653
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  2654
		  }
slouken@0
  2655
		}
slouken@0
  2656
		srcp++;
slouken@0
  2657
		dstp++;
slouken@0
  2658
	    }, width);
slouken@1895
  2659
	    /* *INDENT-ON* */
slouken@1895
  2660
        srcp += srcskip;
slouken@1895
  2661
        dstp += dstskip;
slouken@1895
  2662
    }
slouken@0
  2663
}
slouken@0
  2664
slouken@0
  2665
/* General (slow) N->N blending with per-surface alpha */
slouken@1895
  2666
static void
slouken@1895
  2667
BlitNtoNSurfaceAlpha(SDL_BlitInfo * info)
slouken@0
  2668
{
slouken@1895
  2669
    int width = info->d_width;
slouken@1895
  2670
    int height = info->d_height;
slouken@1895
  2671
    Uint8 *src = info->s_pixels;
slouken@1895
  2672
    int srcskip = info->s_skip;
slouken@1895
  2673
    Uint8 *dst = info->d_pixels;
slouken@1895
  2674
    int dstskip = info->d_skip;
slouken@1895
  2675
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
  2676
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
  2677
    int srcbpp = srcfmt->BytesPerPixel;
slouken@1895
  2678
    int dstbpp = dstfmt->BytesPerPixel;
slouken@1895
  2679
    unsigned sA = srcfmt->alpha;
slouken@1895
  2680
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  2681
slouken@1895
  2682
    if (sA) {
slouken@1895
  2683
        while (height--) {
slouken@1895
  2684
	    /* *INDENT-OFF* */
slouken@0
  2685
	    DUFFS_LOOP4(
slouken@0
  2686
	    {
icculus@1162
  2687
		Uint32 Pixel;
slouken@0
  2688
		unsigned sR;
slouken@0
  2689
		unsigned sG;
slouken@0
  2690
		unsigned sB;
slouken@0
  2691
		unsigned dR;
slouken@0
  2692
		unsigned dG;
slouken@0
  2693
		unsigned dB;
icculus@1162
  2694
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
icculus@1162
  2695
		DISEMBLE_RGB(dst, dstbpp, dstfmt, Pixel, dR, dG, dB);
slouken@0
  2696
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
  2697
		ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
  2698
		src += srcbpp;
slouken@0
  2699
		dst += dstbpp;
slouken@0
  2700
	    },
slouken@0
  2701
	    width);
slouken@1895
  2702
	    /* *INDENT-ON* */
slouken@1895
  2703
            src += srcskip;
slouken@1895
  2704
            dst += dstskip;
slouken@1895
  2705
        }
slouken@1895
  2706
    }
slouken@0
  2707
}
slouken@0
  2708
slouken@0
  2709
/* General (slow) colorkeyed N->N blending with per-surface alpha */
slouken@1895
  2710
static void
slouken@1895
  2711
BlitNtoNSurfaceAlphaKey(SDL_BlitInfo * info)
slouken@0
  2712
{
slouken@1895
  2713
    int width = info->d_width;
slouken@1895
  2714
    int height = info->d_height;
slouken@1895
  2715
    Uint8 *src = info->s_pixels;
slouken@1895
  2716
    int srcskip = info->s_skip;
slouken@1895
  2717
    Uint8 *dst = info->d_pixels;
slouken@1895
  2718
    int dstskip = info->d_skip;
slouken@1895
  2719
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
  2720
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
  2721
    Uint32 ckey = srcfmt->colorkey;
slouken@1895
  2722
    int srcbpp = srcfmt->BytesPerPixel;
slouken@1895
  2723
    int dstbpp = dstfmt->BytesPerPixel;
slouken@1895
  2724
    unsigned sA = srcfmt->alpha;
slouken@1895
  2725
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  2726
slouken@1895
  2727
    while (height--) {
slouken@1895
  2728
	    /* *INDENT-OFF* */
slouken@0
  2729
	    DUFFS_LOOP4(
slouken@0
  2730
	    {
icculus@1162
  2731
		Uint32 Pixel;
slouken@0
  2732
		unsigned sR;
slouken@0
  2733
		unsigned sG;
slouken@0
  2734
		unsigned sB;
slouken@0
  2735
		unsigned dR;
slouken@0
  2736
		unsigned dG;