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