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