src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1402 d910939febfa
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

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