3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * A collection of useful macros.
37 /* Do not reference mtypes.h from this file.
42 #define MAX_GLUSHORT 0xffff
43 #define MAX_GLUINT 0xffffffff
48 #define M_PI (3.1415926)
52 /* Degrees to radians conversion: */
53 #define DEG2RAD (M_PI/180.0)
65 #define SET_BITS(WORD, BITS) (WORD) |= (BITS)
66 #define CLEAR_BITS(WORD, BITS) (WORD) &= ~(BITS)
67 #define TEST_BITS(WORD, BITS) ((WORD) & (BITS))
70 /* Stepping a GLfloat pointer by a byte stride
72 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
73 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
74 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
75 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
76 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
77 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
80 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
81 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
82 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
85 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
90 #define TEST_EQ_3V(a,b) ((a)[0] == (b)[0] && \
95 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
97 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
102 /* Copy short vectors: */
103 #define COPY_2V( DST, SRC ) \
105 (DST)[0] = (SRC)[0]; \
106 (DST)[1] = (SRC)[1]; \
109 #define COPY_3V( DST, SRC ) \
111 (DST)[0] = (SRC)[0]; \
112 (DST)[1] = (SRC)[1]; \
113 (DST)[2] = (SRC)[2]; \
116 #define COPY_4V( DST, SRC ) \
118 (DST)[0] = (SRC)[0]; \
119 (DST)[1] = (SRC)[1]; \
120 (DST)[2] = (SRC)[2]; \
121 (DST)[3] = (SRC)[3]; \
124 #if defined(__i386__)
125 #define COPY_4UBV(DST, SRC) \
127 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
130 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
131 #define COPY_4UBV(DST, SRC) \
133 (DST)[0] = (SRC)[0]; \
134 (DST)[1] = (SRC)[1]; \
135 (DST)[2] = (SRC)[2]; \
136 (DST)[3] = (SRC)[3]; \
140 #define COPY_2FV( DST, SRC ) \
142 const GLfloat *_tmp = (SRC); \
143 (DST)[0] = _tmp[0]; \
144 (DST)[1] = _tmp[1]; \
147 #define COPY_3FV( DST, SRC ) \
149 const GLfloat *_tmp = (SRC); \
150 (DST)[0] = _tmp[0]; \
151 (DST)[1] = _tmp[1]; \
152 (DST)[2] = _tmp[2]; \
155 #define COPY_4FV( DST, SRC ) \
157 const GLfloat *_tmp = (SRC); \
158 (DST)[0] = _tmp[0]; \
159 (DST)[1] = _tmp[1]; \
160 (DST)[2] = _tmp[2]; \
161 (DST)[3] = _tmp[3]; \
166 #define COPY_SZ_4V(DST, SZ, SRC) \
169 case 4: (DST)[3] = (SRC)[3]; \
170 case 3: (DST)[2] = (SRC)[2]; \
171 case 2: (DST)[1] = (SRC)[1]; \
172 case 1: (DST)[0] = (SRC)[0]; \
176 #define COPY_CLEAN_4V(DST, SZ, SRC) \
178 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
179 COPY_SZ_4V( DST, SZ, SRC ); \
182 #define SUB_4V( DST, SRCA, SRCB ) \
184 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
185 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
186 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
187 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
190 #define ADD_4V( DST, SRCA, SRCB ) \
192 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
193 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
194 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
195 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
198 #define SCALE_4V( DST, SRCA, SRCB ) \
200 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
201 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
202 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
203 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
206 #define ACC_4V( DST, SRC ) \
208 (DST)[0] += (SRC)[0]; \
209 (DST)[1] += (SRC)[1]; \
210 (DST)[2] += (SRC)[2]; \
211 (DST)[3] += (SRC)[3]; \
214 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
216 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
217 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
218 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
219 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
222 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
224 (DST)[0] += S * (SRCB)[0]; \
225 (DST)[1] += S * (SRCB)[1]; \
226 (DST)[2] += S * (SRCB)[2]; \
227 (DST)[3] += S * (SRCB)[3]; \
230 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
232 (DST)[0] = S * (SRCB)[0]; \
233 (DST)[1] = S * (SRCB)[1]; \
234 (DST)[2] = S * (SRCB)[2]; \
235 (DST)[3] = S * (SRCB)[3]; \
239 #define SELF_SCALE_SCALAR_4V( DST, S ) \
249 * Similarly for 3-vectors.
251 #define SUB_3V( DST, SRCA, SRCB ) \
253 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
254 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
255 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
258 #define ADD_3V( DST, SRCA, SRCB ) \
260 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
261 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
262 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
265 #define SCALE_3V( DST, SRCA, SRCB ) \
267 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
268 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
269 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
272 #define SELF_SCALE_3V( DST, SRC ) \
274 (DST)[0] *= (SRC)[0]; \
275 (DST)[1] *= (SRC)[1]; \
276 (DST)[2] *= (SRC)[2]; \
279 #define ACC_3V( DST, SRC ) \
281 (DST)[0] += (SRC)[0]; \
282 (DST)[1] += (SRC)[1]; \
283 (DST)[2] += (SRC)[2]; \
286 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
288 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
289 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
290 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
293 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
295 (DST)[0] = S * (SRCB)[0]; \
296 (DST)[1] = S * (SRCB)[1]; \
297 (DST)[2] = S * (SRCB)[2]; \
300 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
302 (DST)[0] += S * (SRCB)[0]; \
303 (DST)[1] += S * (SRCB)[1]; \
304 (DST)[2] += S * (SRCB)[2]; \
307 #define SELF_SCALE_SCALAR_3V( DST, S ) \
314 #define ACC_SCALAR_3V( DST, S ) \
321 /* And also for 2-vectors
323 #define SUB_2V( DST, SRCA, SRCB ) \
325 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
326 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
329 #define ADD_2V( DST, SRCA, SRCB ) \
331 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
332 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
335 #define SCALE_2V( DST, SRCA, SRCB ) \
337 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
338 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
341 #define ACC_2V( DST, SRC ) \
343 (DST)[0] += (SRC)[0]; \
344 (DST)[1] += (SRC)[1]; \
347 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
349 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
350 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
353 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
355 (DST)[0] = S * (SRCB)[0]; \
356 (DST)[1] = S * (SRCB)[1]; \
359 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
361 (DST)[0] += S * (SRCB)[0]; \
362 (DST)[1] += S * (SRCB)[1]; \
365 #define SELF_SCALE_SCALAR_2V( DST, S ) \
371 #define ACC_SCALAR_2V( DST, S ) \
379 /* Assign scalers to short vectors: */
380 #define ASSIGN_2V( V, V0, V1 ) \
386 #define ASSIGN_3V( V, V0, V1, V2 ) \
393 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
404 /* Absolute value (for Int, Float, Double): */
405 #define ABSI(X) ((X) < 0 ? -(X) : (X))
406 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
407 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
411 /* Round a floating-point value to the nearest integer: */
412 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
415 /* Compute ceiling of integer quotient of A divided by B: */
416 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
419 /* Clamp X to [MIN,MAX]: */
420 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
422 /* Assign X to CLAMP(X, MIN, MAX) */
423 #define CLAMP_SELF(x, mn, mx) \
424 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
428 /* Min of two values: */
429 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
431 /* MAX of two values: */
432 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
434 /* Dot product of two 2-element vectors */
435 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
437 /* Dot product of two 3-element vectors */
438 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
440 /* Dot product of two 4-element vectors */
441 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
442 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
444 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
447 #define CROSS3(n, u, v) \
449 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
450 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
451 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
460 #include <byteswap.h>
461 #define CPU_TO_LE32( x ) bswap_32( x )
463 #define CPU_TO_LE32( x ) ( x )
466 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
469 /* Generic color packing macros
472 #define PACK_COLOR_8888( a, b, c, d ) \
473 (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
475 #define PACK_COLOR_888( a, b, c ) \
476 (((a) << 16) | ((b) << 8) | (c))
478 #define PACK_COLOR_565( a, b, c ) \
479 ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
481 #define PACK_COLOR_1555( a, b, c, d ) \
482 ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) | \
485 #define PACK_COLOR_4444( a, b, c, d ) \
486 ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
488 #define PACK_COLOR_88( a, b ) \
491 #define PACK_COLOR_332( a, b, c ) \
492 (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
497 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( d, c, b, a )
499 #define PACK_COLOR_565_LE( a, b, c ) \
500 (((a) & 0xf8) | (((b) & 0xe0) >> 5) | (((b) & 0x1c) << 11) | \
503 #define PACK_COLOR_1555_LE( a, b, c, d ) \
504 ((((b) & 0xf8) >> 1) | (((c) & 0xc0) >> 6) | (((c) & 0x38) << 10) | \
505 (((d) & 0xf8) << 5) | ((a) ? 0x80 : 0))
507 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( c, d, a, b )
509 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( b, a )
511 #else /* little endian */
513 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( a, b, c, d )
515 #define PACK_COLOR_565_LE( a, b, c ) PACK_COLOR_565( a, b, c )
517 #define PACK_COLOR_1555_LE( a, b, c, d ) PACK_COLOR_1555( a, b, c, d )
519 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( a, b, c, d )
521 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( a, b )
523 #endif /* endianness */