src/video/SDL_blit.h
author Ozkan Sezer <sezeroz@gmail.com>
Sun, 14 Oct 2018 15:25:04 +0300
branchSDL-1.2
changeset 12325 c4f2eeda176f
parent 6137 4720145f848b
permissions -rw-r--r--
remove symlink for libSDL-1.0.so.0 from the rpm spec file.

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