src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Aug 2007 06:37:22 +0000
changeset 2251 292bee385630
parent 2250 e1d228456537
child 2257 340942cfda48
permissions -rw-r--r--
SSE and MMX intrinsics work with Visual Studio now...
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
#ifndef _SDL_blit_h
slouken@0
    25
#define _SDL_blit_h
slouken@0
    26
slouken@2249
    27
#ifdef __MMX__
slouken@2249
    28
#include <mmintrin.h>
slouken@2249
    29
#endif
slouken@2249
    30
#ifdef __SSE__
slouken@2249
    31
#include <xmmintrin.h>
slouken@2249
    32
#endif
slouken@2249
    33
slouken@2250
    34
#include "SDL_cpuinfo.h"
slouken@0
    35
#include "SDL_endian.h"
slouken@0
    36
slouken@0
    37
/* The structure passed to the low level blit functions */
slouken@1895
    38
typedef struct
slouken@1895
    39
{
slouken@1895
    40
    Uint8 *s_pixels;
slouken@1895
    41
    int s_width;
slouken@1895
    42
    int s_height;
slouken@1895
    43
    int s_skip;
slouken@1895
    44
    Uint8 *d_pixels;
slouken@1895
    45
    int d_width;
slouken@1895
    46
    int d_height;
slouken@1895
    47
    int d_skip;
slouken@1895
    48
    void *aux_data;
slouken@1895
    49
    SDL_PixelFormat *src;
slouken@1895
    50
    Uint8 *table;
slouken@1895
    51
    SDL_PixelFormat *dst;
slouken@0
    52
} SDL_BlitInfo;
slouken@0
    53
slouken@0
    54
/* The type definition for the low level blit functions */
slouken@1895
    55
typedef void (*SDL_loblit) (SDL_BlitInfo * info);
slouken@0
    56
slouken@0
    57
/* This is the private info structure for software accelerated blits */
slouken@1895
    58
struct private_swaccel
slouken@1895
    59
{
slouken@1895
    60
    SDL_loblit blit;
slouken@1895
    61
    void *aux_data;
slouken@0
    62
};
slouken@0
    63
slouken@0
    64
/* Blit mapping definition */
slouken@1895
    65
typedef struct SDL_BlitMap
slouken@1895
    66
{
slouken@1895
    67
    SDL_Surface *dst;
slouken@1895
    68
    int identity;
slouken@1895
    69
    Uint8 *table;
slouken@1895
    70
    SDL_blit sw_blit;
slouken@1895
    71
    struct private_swaccel *sw_data;
slouken@0
    72
slouken@1895
    73
    /* the version count matches the destination; mismatch indicates
slouken@1895
    74
       an invalid mapping */
slouken@1895
    75
    unsigned int format_version;
slouken@0
    76
} SDL_BlitMap;
slouken@0
    77
slouken@2247
    78
#define SDL_BLIT_ANY                0x00000000
slouken@2247
    79
#define SDL_BLIT_MMX                0x00000001
slouken@2247
    80
#define SDL_BLIT_SSE                0x00000002
slouken@2247
    81
#define SDL_BLIT_ALTIVEC_PREFETCH   0x00000004
slouken@2247
    82
#define SDL_BLIT_ALTIVEC_NOPREFETCH 0x00000008
slouken@2247
    83
slouken@2247
    84
typedef struct SDL_BlitEntry
slouken@2247
    85
{
slouken@2247
    86
    Uint32 features;
slouken@2247
    87
    SDL_loblit blit;
slouken@2247
    88
} SDL_BlitEntry;
slouken@0
    89
slouken@0
    90
/* Functions found in SDL_blit.c */
slouken@1895
    91
extern int SDL_CalculateBlit(SDL_Surface * surface);
slouken@0
    92
slouken@0
    93
/* Functions found in SDL_blit_{0,1,N,A}.c */
slouken@1895
    94
extern SDL_loblit SDL_CalculateBlit0(SDL_Surface * surface, int complex);
slouken@1895
    95
extern SDL_loblit SDL_CalculateBlit1(SDL_Surface * surface, int complex);
slouken@1895
    96
extern SDL_loblit SDL_CalculateBlitN(SDL_Surface * surface, int complex);
slouken@1895
    97
extern SDL_loblit SDL_CalculateAlphaBlit(SDL_Surface * surface, int complex);
slouken@0
    98
slouken@0
    99
/*
slouken@0
   100
 * Useful macros for blitting routines
slouken@0
   101
 */
slouken@0
   102
slouken@2249
   103
#if defined(__GNUC__)
slouken@2249
   104
#define DECLARE_ALIGNED(t,v,a)  t __attribute__((aligned(a))) v
slouken@2249
   105
#elif defined(_MSC_VER)
slouken@2251
   106
#define DECLARE_ALIGNED(t,v,a)  __declspec(align(a)) t v
slouken@2249
   107
#else
slouken@2249
   108
#define DECLARE_ALIGNED(t,v,a)  t v
slouken@2249
   109
#endif
slouken@2249
   110
slouken@0
   111
#define FORMAT_EQUAL(A, B)						\
slouken@0
   112
    ((A)->BitsPerPixel == (B)->BitsPerPixel				\
slouken@0
   113
     && ((A)->Rmask == (B)->Rmask) && ((A)->Amask == (B)->Amask))
slouken@0
   114
slouken@0
   115
/* Load pixel of the specified format from a buffer and get its R-G-B values */
slouken@0
   116
/* FIXME: rescale values to 0..255 here? */
icculus@1162
   117
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)				\
slouken@0
   118
{									\
icculus@1162
   119
	r = (((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss); 		\
icculus@1162
   120
	g = (((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss); 		\
icculus@1162
   121
	b = (((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss); 		\
slouken@0
   122
}
icculus@1162
   123
#define RGB_FROM_RGB565(Pixel, r, g, b)					\
slouken@0
   124
{									\
icculus@1162
   125
	r = (((Pixel&0xF800)>>11)<<3);		 			\
icculus@1162
   126
	g = (((Pixel&0x07E0)>>5)<<2); 					\
icculus@1162
   127
	b = ((Pixel&0x001F)<<3); 					\
slouken@0
   128
}
icculus@1162
   129
#define RGB_FROM_RGB555(Pixel, r, g, b)					\
slouken@0
   130
{									\
icculus@1162
   131
	r = (((Pixel&0x7C00)>>10)<<3);		 			\
icculus@1162
   132
	g = (((Pixel&0x03E0)>>5)<<3); 					\
icculus@1162
   133
	b = ((Pixel&0x001F)<<3); 					\
slouken@0
   134
}
icculus@1162
   135
#define RGB_FROM_RGB888(Pixel, r, g, b)					\
slouken@0
   136
{									\
icculus@1162
   137
	r = ((Pixel&0xFF0000)>>16);		 			\
icculus@1162
   138
	g = ((Pixel&0xFF00)>>8);		 			\
icculus@1162
   139
	b = (Pixel&0xFF);			 			\
slouken@0
   140
}
icculus@1162
   141
#define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)				   \
slouken@0
   142
do {									   \
slouken@0
   143
	switch (bpp) {							   \
slouken@0
   144
		case 2:							   \
icculus@1162
   145
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   146
		break;							   \
slouken@0
   147
									   \
slouken@0
   148
		case 3: {						   \
slouken@0
   149
		        Uint8 *B = (Uint8 *)(buf);			   \
slouken@0
   150
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   151
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   152
			} else {					   \
icculus@1162
   153
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   154
			}						   \
slouken@0
   155
		}							   \
slouken@0
   156
		break;							   \
slouken@0
   157
									   \
slouken@0
   158
		case 4:							   \
icculus@1162
   159
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   160
		break;							   \
slouken@0
   161
									   \
slouken@0
   162
		default:						   \
icculus@1162
   163
			Pixel = 0; /* appease gcc */			   \
slouken@0
   164
		break;							   \
slouken@0
   165
	}								   \
slouken@0
   166
} while(0)
slouken@0
   167
icculus@1162
   168
#define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)			   \
slouken@0
   169
do {									   \
slouken@0
   170
	switch (bpp) {							   \
slouken@0
   171
		case 2:							   \
icculus@1162
   172
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   173
		break;							   \
slouken@0
   174
									   \
slouken@0
   175
		case 3: {						   \
slouken@0
   176
		        Uint8 *B = (Uint8 *)buf;			   \
slouken@0
   177
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   178
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   179
			} else {					   \
icculus@1162
   180
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   181
			}						   \
slouken@0
   182
		}							   \
slouken@0
   183
		break;							   \
slouken@0
   184
									   \
slouken@0
   185
		case 4:							   \
icculus@1162
   186
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   187
		break;							   \
slouken@0
   188
									   \
slouken@0
   189
	        default:						   \
icculus@1162
   190
		        Pixel = 0;	/* prevent gcc from complaining */ \
slouken@0
   191
		break;							   \
slouken@0
   192
	}								   \
icculus@1162
   193
	RGB_FROM_PIXEL(Pixel, fmt, r, g, b);				   \
slouken@0
   194
} while(0)
slouken@0
   195
slouken@0
   196
/* Assemble R-G-B values into a specified pixel format and store them */
icculus@1162
   197
#define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)				\
slouken@0
   198
{									\
icculus@1162
   199
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   200
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   201
		((b>>fmt->Bloss)<<fmt->Bshift);				\
slouken@0
   202
}
icculus@1162
   203
#define RGB565_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   204
{									\
icculus@1162
   205
	Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);			\
slouken@0
   206
}
icculus@1162
   207
#define RGB555_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   208
{									\
icculus@1162
   209
	Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);			\
slouken@0
   210
}
icculus@1162
   211
#define RGB888_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   212
{									\
icculus@1162
   213
	Pixel = (r<<16)|(g<<8)|b;					\
slouken@0
   214
}
slouken@0
   215
#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) 				\
slouken@0
   216
{									\
slouken@0
   217
	switch (bpp) {							\
slouken@0
   218
		case 2: {						\
icculus@1162
   219
			Uint16 Pixel;					\
slouken@0
   220
									\
icculus@1162
   221
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   222
			*((Uint16 *)(buf)) = Pixel;			\
slouken@0
   223
		}							\
slouken@0
   224
		break;							\
slouken@0
   225
									\
slouken@0
   226
		case 3: {						\
slouken@0
   227
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   228
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   229
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   230
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   231
			} else {					\
slouken@0
   232
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   233
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   234
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   235
			}						\
slouken@0
   236
		}							\
slouken@0
   237
		break;							\
slouken@0
   238
									\
slouken@0
   239
		case 4: {						\
icculus@1162
   240
			Uint32 Pixel;					\
slouken@0
   241
									\
icculus@1162
   242
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   243
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   244
		}							\
slouken@0
   245
		break;							\
slouken@0
   246
	}								\
slouken@0
   247
}
slouken@0
   248
#define ASSEMBLE_RGB_AMASK(buf, bpp, fmt, r, g, b, Amask)		\
slouken@0
   249
{									\
slouken@0
   250
	switch (bpp) {							\
slouken@0
   251
		case 2: {						\
slouken@0
   252
			Uint16 *bufp;					\
icculus@1162
   253
			Uint16 Pixel;					\
slouken@0
   254
									\
slouken@0
   255
			bufp = (Uint16 *)buf;				\
icculus@1162
   256
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   257
			*bufp = Pixel | (*bufp & Amask);		\
slouken@0
   258
		}							\
slouken@0
   259
		break;							\
slouken@0
   260
									\
slouken@0
   261
		case 3: {						\
slouken@0
   262
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   263
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   264
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   265
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   266
			} else {					\
slouken@0
   267
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   268
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   269
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   270
			}						\
slouken@0
   271
		}							\
slouken@0
   272
		break;							\
slouken@0
   273
									\
slouken@0
   274
		case 4: {						\
slouken@0
   275
			Uint32 *bufp;					\
icculus@1162
   276
			Uint32 Pixel;					\
slouken@0
   277
									\
slouken@0
   278
			bufp = (Uint32 *)buf;				\
icculus@1162
   279
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   280
			*bufp = Pixel | (*bufp & Amask);		\
slouken@0
   281
		}							\
slouken@0
   282
		break;							\
slouken@0
   283
	}								\
slouken@0
   284
}
slouken@0
   285
slouken@0
   286
/* FIXME: Should we rescale alpha into 0..255 here? */
icculus@1162
   287
#define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)				\
slouken@0
   288
{									\
icculus@1162
   289
	r = ((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss; 		\
icculus@1162
   290
	g = ((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss; 		\
icculus@1162
   291
	b = ((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss; 		\
icculus@1162
   292
	a = ((Pixel&fmt->Amask)>>fmt->Ashift)<<fmt->Aloss;	 	\
slouken@0
   293
}
icculus@1162
   294
#define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)	\
slouken@0
   295
{						\
icculus@1162
   296
	r = (Pixel&fmt->Rmask)>>fmt->Rshift;	\
icculus@1162
   297
	g = (Pixel&fmt->Gmask)>>fmt->Gshift;	\
icculus@1162
   298
	b = (Pixel&fmt->Bmask)>>fmt->Bshift;	\
icculus@1162
   299
	a = (Pixel&fmt->Amask)>>fmt->Ashift;	\
slouken@0
   300
}
icculus@1162
   301
#define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)				\
slouken@0
   302
{									\
icculus@1162
   303
	r = (Pixel>>24);						\
icculus@1162
   304
	g = ((Pixel>>16)&0xFF);						\
icculus@1162
   305
	b = ((Pixel>>8)&0xFF);						\
icculus@1162
   306
	a = (Pixel&0xFF);						\
slouken@0
   307
}
icculus@1162
   308
#define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)				\
slouken@0
   309
{									\
icculus@1162
   310
	r = ((Pixel>>16)&0xFF);						\
icculus@1162
   311
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   312
	b = (Pixel&0xFF);						\
icculus@1162
   313
	a = (Pixel>>24);						\
slouken@0
   314
}
icculus@1162
   315
#define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)				\
slouken@0
   316
{									\
icculus@1162
   317
	r = (Pixel&0xFF);						\
icculus@1162
   318
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   319
	b = ((Pixel>>16)&0xFF);						\
icculus@1162
   320
	a = (Pixel>>24);						\
slouken@0
   321
}
icculus@1162
   322
#define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)			   \
slouken@0
   323
do {									   \
slouken@0
   324
	switch (bpp) {							   \
slouken@0
   325
		case 2:							   \
icculus@1162
   326
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   327
		break;							   \
slouken@0
   328
									   \
slouken@0
   329
		case 3:	{/* FIXME: broken code (no alpha) */		   \
slouken@0
   330
		        Uint8 *b = (Uint8 *)buf;			   \
slouken@0
   331
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   332
			        Pixel = b[0] + (b[1] << 8) + (b[2] << 16); \
slouken@0
   333
			} else {					   \
icculus@1162
   334
			        Pixel = (b[0] << 16) + (b[1] << 8) + b[2]; \
slouken@0
   335
			}						   \
slouken@0
   336
		}							   \
slouken@0
   337
		break;							   \
slouken@0
   338
									   \
slouken@0
   339
		case 4:							   \
icculus@1162
   340
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   341
		break;							   \
slouken@0
   342
									   \
slouken@0
   343
		default:						   \
icculus@1162
   344
		        Pixel = 0; /* stop gcc complaints */		   \
slouken@0
   345
		break;							   \
slouken@0
   346
	}								   \
icculus@1162
   347
	RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);			   \
icculus@1162
   348
	Pixel &= ~fmt->Amask;						   \
slouken@0
   349
} while(0)
slouken@0
   350
slouken@0
   351
/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
icculus@1162
   352
#define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)				\
slouken@0
   353
{									\
icculus@1162
   354
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   355
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   356
		((b>>fmt->Bloss)<<fmt->Bshift)|				\
slouken@535
   357
		((a>>fmt->Aloss)<<fmt->Ashift);				\
slouken@0
   358
}
slouken@0
   359
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
slouken@0
   360
{									\
slouken@0
   361
	switch (bpp) {							\
slouken@0
   362
		case 2: {						\
icculus@1162
   363
			Uint16 Pixel;					\
slouken@0
   364
									\
icculus@1162
   365
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   366
			*((Uint16 *)(buf)) = Pixel;			\
slouken@0
   367
		}							\
slouken@0
   368
		break;							\
slouken@0
   369
									\
slouken@0
   370
		case 3: { /* FIXME: broken code (no alpha) */		\
slouken@0
   371
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   372
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   373
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   374
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   375
			} else {					\
slouken@0
   376
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   377
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   378
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   379
			}						\
slouken@0
   380
		}							\
slouken@0
   381
		break;							\
slouken@0
   382
									\
slouken@0
   383
		case 4: {						\
icculus@1162
   384
			Uint32 Pixel;					\
slouken@0
   385
									\
icculus@1162
   386
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   387
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   388
		}							\
slouken@0
   389
		break;							\
slouken@0
   390
	}								\
slouken@0
   391
}
slouken@0
   392
icculus@1162
   393
/* Blend the RGB values of two Pixels based on a source alpha value */
slouken@0
   394
#define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
slouken@0
   395
do {						\
slouken@0
   396
	dR = (((sR-dR)*(A))>>8)+dR;		\
slouken@0
   397
	dG = (((sG-dG)*(A))>>8)+dG;		\
slouken@0
   398
	dB = (((sB-dB)*(A))>>8)+dB;		\
slouken@0
   399
} while(0)
slouken@0
   400
icculus@1162
   401
/* Blend the RGB values of two Pixels based on a source alpha value */
icculus@1047
   402
#define ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB)	\
icculus@1047
   403
do {						\
icculus@1047
   404
    unsigned tR, tG, tB, tA; \
icculus@1047
   405
    tA = 255 - sA; \
icculus@1047
   406
    tR = 1 + (sR * sA) + (dR * tA); \
icculus@1047
   407
    dR = (tR + (tR >> 8)) >> 8; \
icculus@1047
   408
    tG = 1 + (sG * sA) + (dG * tA); \
icculus@1047
   409
    dG = (tG + (tG >> 8)) >> 8; \
icculus@1047
   410
    tB = 1 + (sB * sA) + (dB * tA); \
icculus@1047
   411
    dB = (tB + (tB >> 8)) >> 8; \
icculus@1047
   412
} while(0)
icculus@1047
   413
icculus@1047
   414
slouken@0
   415
/* This is a very useful loop for optimizing blitters */
slouken@553
   416
#if defined(_MSC_VER) && (_MSC_VER == 1300)
slouken@553
   417
/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
slouken@553
   418
#else
slouken@0
   419
#define USE_DUFFS_LOOP
slouken@553
   420
#endif
slouken@0
   421
#ifdef USE_DUFFS_LOOP
slouken@0
   422
slouken@0
   423
/* 8-times unrolled loop */
slouken@0
   424
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   425
{ int n = (width+7)/8;							\
slouken@91
   426
	switch (width & 7) {						\
slouken@0
   427
	case 0: do {	pixel_copy_increment;				\
slouken@0
   428
	case 7:		pixel_copy_increment;				\
slouken@0
   429
	case 6:		pixel_copy_increment;				\
slouken@0
   430
	case 5:		pixel_copy_increment;				\
slouken@0
   431
	case 4:		pixel_copy_increment;				\
slouken@0
   432
	case 3:		pixel_copy_increment;				\
slouken@0
   433
	case 2:		pixel_copy_increment;				\
slouken@0
   434
	case 1:		pixel_copy_increment;				\
slouken@0
   435
		} while ( --n > 0 );					\
slouken@0
   436
	}								\
slouken@0
   437
}
slouken@0
   438
slouken@0
   439
/* 4-times unrolled loop */
slouken@0
   440
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   441
{ int n = (width+3)/4;							\
slouken@91
   442
	switch (width & 3) {						\
slouken@0
   443
	case 0: do {	pixel_copy_increment;				\
slouken@0
   444
	case 3:		pixel_copy_increment;				\
slouken@0
   445
	case 2:		pixel_copy_increment;				\
slouken@0
   446
	case 1:		pixel_copy_increment;				\
slouken@0
   447
		} while ( --n > 0 );					\
slouken@0
   448
	}								\
slouken@0
   449
}
slouken@0
   450
slouken@689
   451
/* 2 - times unrolled loop */
slouken@689
   452
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
slouken@689
   453
				double_pixel_copy_increment, width)	\
slouken@689
   454
{ int n, w = width;							\
slouken@689
   455
	if( w & 1 ) {							\
slouken@689
   456
	    pixel_copy_increment;					\
slouken@689
   457
	    w--;							\
slouken@689
   458
	}								\
slouken@689
   459
	if ( w > 0 )	{						\
slouken@689
   460
	    n = ( w + 2) / 4;						\
slouken@689
   461
	    switch( w & 2 ) {						\
slouken@689
   462
	    case 0: do {	double_pixel_copy_increment;		\
slouken@689
   463
	    case 2:		double_pixel_copy_increment;		\
slouken@689
   464
		    } while ( --n > 0 );					\
slouken@689
   465
	    }								\
slouken@689
   466
	}								\
slouken@689
   467
}
slouken@689
   468
slouken@689
   469
/* 2 - times unrolled loop 4 pixels */
slouken@689
   470
#define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
slouken@689
   471
				double_pixel_copy_increment,		\
slouken@689
   472
				quatro_pixel_copy_increment, width)	\
slouken@689
   473
{ int n, w = width;								\
slouken@689
   474
        if(w & 1) {							\
slouken@689
   475
	  pixel_copy_increment;						\
slouken@689
   476
	  w--;								\
slouken@689
   477
	}								\
slouken@689
   478
	if(w & 2) {							\
slouken@689
   479
	  double_pixel_copy_increment;					\
slouken@689
   480
	  w -= 2;							\
slouken@689
   481
	}								\
slouken@689
   482
	if ( w > 0 ) {							\
slouken@689
   483
	    n = ( w + 7 ) / 8;						\
slouken@689
   484
	    switch( w & 4 ) {						\
slouken@689
   485
	    case 0: do {	quatro_pixel_copy_increment;		\
slouken@689
   486
	    case 4:		quatro_pixel_copy_increment;		\
slouken@689
   487
		    } while ( --n > 0 );					\
slouken@689
   488
	    }								\
slouken@689
   489
	}								\
slouken@689
   490
}
slouken@689
   491
slouken@0
   492
/* Use the 8-times version of the loop by default */
slouken@0
   493
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   494
	DUFFS_LOOP8(pixel_copy_increment, width)
slouken@0
   495
slouken@0
   496
#else
slouken@0
   497
slouken@0
   498
/* Don't use Duff's device to unroll loops */
slouken@689
   499
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
slouken@689
   500
			 double_pixel_copy_increment, width)		\
slouken@689
   501
{ int n = width;								\
slouken@689
   502
    if( n & 1 ) {							\
slouken@689
   503
	pixel_copy_increment;						\
slouken@689
   504
	n--;								\
slouken@689
   505
    }									\
slouken@689
   506
    n=n>>1;								\
slouken@689
   507
    for(; n > 0; --n) {   						\
slouken@689
   508
	double_pixel_copy_increment;					\
slouken@689
   509
    }									\
slouken@689
   510
}
slouken@689
   511
slouken@689
   512
/* Don't use Duff's device to unroll loops */
slouken@689
   513
#define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
slouken@689
   514
				double_pixel_copy_increment,		\
slouken@689
   515
				quatro_pixel_copy_increment, width)	\
slouken@689
   516
{ int n = width;								\
slouken@689
   517
        if(n & 1) {							\
slouken@689
   518
	  pixel_copy_increment;						\
slouken@689
   519
	  n--;								\
slouken@689
   520
	}								\
slouken@689
   521
	if(n & 2) {							\
slouken@689
   522
	  double_pixel_copy_increment;					\
slouken@689
   523
	  n -= 2;							\
slouken@689
   524
	}								\
slouken@689
   525
	n=n>>2;								\
slouken@689
   526
	for(; n > 0; --n) {   						\
slouken@689
   527
	  quatro_pixel_copy_increment;					\
slouken@689
   528
        }								\
slouken@689
   529
}
slouken@689
   530
slouken@689
   531
/* Don't use Duff's device to unroll loops */
slouken@0
   532
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   533
{ int n;								\
slouken@0
   534
	for ( n=width; n > 0; --n ) {					\
slouken@0
   535
		pixel_copy_increment;					\
slouken@0
   536
	}								\
slouken@0
   537
}
slouken@0
   538
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   539
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   540
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   541
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   542
slouken@0
   543
#endif /* USE_DUFFS_LOOP */
slouken@0
   544
slouken@0
   545
/* Prevent Visual C++ 6.0 from printing out stupid warnings */
slouken@0
   546
#if defined(_MSC_VER) && (_MSC_VER >= 600)
slouken@0
   547
#pragma warning(disable: 4550)
slouken@0
   548
#endif
slouken@0
   549
slouken@0
   550
#endif /* _SDL_blit_h */
slouken@1895
   551
/* vi: set ts=4 sw=4 expandtab: */