1 /* $Id: macros.h,v 1.30 2002/10/18 17:02:00 kschultz Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * A collection of useful macros.
38 /* Do not reference mtypes.h from this file.
43 #define MAX_GLUSHORT 0xffff
44 #define MAX_GLUINT 0xffffffff
49 #define M_PI (3.1415926)
53 /* Degrees to radians conversion: */
54 #define DEG2RAD (M_PI/180.0)
62 /* Stepping a GLfloat pointer by a byte stride
64 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
65 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
66 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
67 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
68 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
69 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
72 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
73 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
74 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
77 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
82 #define TEST_EQ_3V(a,b) ((a)[0] == (b)[0] && \
87 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
89 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
94 /* Copy short vectors: */
95 #define COPY_2V( DST, SRC ) \
97 (DST)[0] = (SRC)[0]; \
98 (DST)[1] = (SRC)[1]; \
101 #define COPY_3V( DST, SRC ) \
103 (DST)[0] = (SRC)[0]; \
104 (DST)[1] = (SRC)[1]; \
105 (DST)[2] = (SRC)[2]; \
108 #define COPY_4V( DST, SRC ) \
110 (DST)[0] = (SRC)[0]; \
111 (DST)[1] = (SRC)[1]; \
112 (DST)[2] = (SRC)[2]; \
113 (DST)[3] = (SRC)[3]; \
116 #define COPY_2V_CAST( DST, SRC, CAST ) \
118 (DST)[0] = (CAST)(SRC)[0]; \
119 (DST)[1] = (CAST)(SRC)[1]; \
122 #define COPY_3V_CAST( DST, SRC, CAST ) \
124 (DST)[0] = (CAST)(SRC)[0]; \
125 (DST)[1] = (CAST)(SRC)[1]; \
126 (DST)[2] = (CAST)(SRC)[2]; \
129 #define COPY_4V_CAST( DST, SRC, CAST ) \
131 (DST)[0] = (CAST)(SRC)[0]; \
132 (DST)[1] = (CAST)(SRC)[1]; \
133 (DST)[2] = (CAST)(SRC)[2]; \
134 (DST)[3] = (CAST)(SRC)[3]; \
137 #if defined(__i386__)
138 #define COPY_4UBV(DST, SRC) \
140 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
143 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
144 #define COPY_4UBV(DST, SRC) \
146 (DST)[0] = (SRC)[0]; \
147 (DST)[1] = (SRC)[1]; \
148 (DST)[2] = (SRC)[2]; \
149 (DST)[3] = (SRC)[3]; \
153 #define COPY_2FV( DST, SRC ) \
155 const GLfloat *_tmp = (SRC); \
156 (DST)[0] = _tmp[0]; \
157 (DST)[1] = _tmp[1]; \
160 #define COPY_3FV( DST, SRC ) \
162 const GLfloat *_tmp = (SRC); \
163 (DST)[0] = _tmp[0]; \
164 (DST)[1] = _tmp[1]; \
165 (DST)[2] = _tmp[2]; \
168 #define COPY_4FV( DST, SRC ) \
170 const GLfloat *_tmp = (SRC); \
171 (DST)[0] = _tmp[0]; \
172 (DST)[1] = _tmp[1]; \
173 (DST)[2] = _tmp[2]; \
174 (DST)[3] = _tmp[3]; \
179 #define COPY_SZ_4V(DST, SZ, SRC) \
182 case 4: (DST)[3] = (SRC)[3]; \
183 case 3: (DST)[2] = (SRC)[2]; \
184 case 2: (DST)[1] = (SRC)[1]; \
185 case 1: (DST)[0] = (SRC)[0]; \
189 #define COPY_CLEAN_4V(DST, SZ, SRC) \
191 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
192 COPY_SZ_4V( DST, SZ, SRC ); \
195 #define SUB_4V( DST, SRCA, SRCB ) \
197 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
198 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
199 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
200 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
203 #define ADD_4V( DST, SRCA, SRCB ) \
205 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
206 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
207 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
208 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
211 #define SCALE_4V( DST, SRCA, SRCB ) \
213 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
214 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
215 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
216 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
219 #define ACC_4V( DST, SRC ) \
221 (DST)[0] += (SRC)[0]; \
222 (DST)[1] += (SRC)[1]; \
223 (DST)[2] += (SRC)[2]; \
224 (DST)[3] += (SRC)[3]; \
227 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
229 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
230 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
231 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
232 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
235 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
237 (DST)[0] += S * (SRCB)[0]; \
238 (DST)[1] += S * (SRCB)[1]; \
239 (DST)[2] += S * (SRCB)[2]; \
240 (DST)[3] += S * (SRCB)[3]; \
243 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
245 (DST)[0] = S * (SRCB)[0]; \
246 (DST)[1] = S * (SRCB)[1]; \
247 (DST)[2] = S * (SRCB)[2]; \
248 (DST)[3] = S * (SRCB)[3]; \
252 #define SELF_SCALE_SCALAR_4V( DST, S ) \
262 * Similarly for 3-vectors.
264 #define SUB_3V( DST, SRCA, SRCB ) \
266 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
267 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
268 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
271 #define ADD_3V( DST, SRCA, SRCB ) \
273 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
274 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
275 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
278 #define SCALE_3V( DST, SRCA, SRCB ) \
280 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
281 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
282 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
285 #define SELF_SCALE_3V( DST, SRC ) \
287 (DST)[0] *= (SRC)[0]; \
288 (DST)[1] *= (SRC)[1]; \
289 (DST)[2] *= (SRC)[2]; \
292 #define ACC_3V( DST, SRC ) \
294 (DST)[0] += (SRC)[0]; \
295 (DST)[1] += (SRC)[1]; \
296 (DST)[2] += (SRC)[2]; \
299 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
301 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
302 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
303 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
306 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
308 (DST)[0] = S * (SRCB)[0]; \
309 (DST)[1] = S * (SRCB)[1]; \
310 (DST)[2] = S * (SRCB)[2]; \
313 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
315 (DST)[0] += S * (SRCB)[0]; \
316 (DST)[1] += S * (SRCB)[1]; \
317 (DST)[2] += S * (SRCB)[2]; \
320 #define SELF_SCALE_SCALAR_3V( DST, S ) \
327 #define ACC_SCALAR_3V( DST, S ) \
334 /* And also for 2-vectors
336 #define SUB_2V( DST, SRCA, SRCB ) \
338 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
339 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
342 #define ADD_2V( DST, SRCA, SRCB ) \
344 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
345 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
348 #define SCALE_2V( DST, SRCA, SRCB ) \
350 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
351 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
354 #define ACC_2V( DST, SRC ) \
356 (DST)[0] += (SRC)[0]; \
357 (DST)[1] += (SRC)[1]; \
360 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
362 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
363 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
366 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
368 (DST)[0] = S * (SRCB)[0]; \
369 (DST)[1] = S * (SRCB)[1]; \
372 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
374 (DST)[0] += S * (SRCB)[0]; \
375 (DST)[1] += S * (SRCB)[1]; \
378 #define SELF_SCALE_SCALAR_2V( DST, S ) \
384 #define ACC_SCALAR_2V( DST, S ) \
392 /* Assign scalers to short vectors: */
393 #define ASSIGN_2V( V, V0, V1 ) \
399 #define ASSIGN_3V( V, V0, V1, V2 ) \
406 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
417 /* Absolute value (for Int, Float, Double): */
418 #define ABSI(X) ((X) < 0 ? -(X) : (X))
419 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
420 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
424 /* Round a floating-point value to the nearest integer: */
425 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
428 /* Compute ceiling of integer quotient of A divided by B: */
429 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
432 /* Clamp X to [MIN,MAX]: */
433 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
435 /* Assign X to CLAMP(X, MIN, MAX) */
436 #define CLAMP_SELF(x, mn, mx) \
437 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
441 /* Min of two values: */
442 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
444 /* MAX of two values: */
445 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
447 /* Dot product of two 2-element vectors */
448 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
450 /* Dot product of two 3-element vectors */
451 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
453 /* Dot product of two 4-element vectors */
454 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
455 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
457 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
460 #define CROSS3(n, u, v) \
462 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
463 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
464 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
469 /* Generic color packing macros
470 * XXX We may move these into texutil.h at some point.
473 #define PACK_COLOR_8888( a, b, c, d ) \
474 (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
476 #define PACK_COLOR_888( a, b, c ) \
477 (((a) << 16) | ((b) << 8) | (c))
479 #define PACK_COLOR_565( a, b, c ) \
480 ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
482 #define PACK_COLOR_1555( a, b, c, d ) \
483 ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) | \
486 #define PACK_COLOR_4444( a, b, c, d ) \
487 ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
489 #define PACK_COLOR_88( a, b ) \
492 #define PACK_COLOR_332( a, b, c ) \
493 (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
496 #ifdef MESA_BIG_ENDIAN
498 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( d, c, b, a )
500 #define PACK_COLOR_565_LE( a, b, c ) \
501 (((a) & 0xf8) | (((b) & 0xe0) >> 5) | (((b) & 0x1c) << 11) | \
504 #define PACK_COLOR_1555_LE( a, b, c, d ) \
505 ((((b) & 0xf8) >> 1) | (((c) & 0xc0) >> 6) | (((c) & 0x38) << 10) | \
506 (((d) & 0xf8) << 5) | ((a) ? 0x80 : 0))
508 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( c, d, a, b )
510 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( b, a )
512 #else /* little endian */
514 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( a, b, c, d )
516 #define PACK_COLOR_565_LE( a, b, c ) PACK_COLOR_565( a, b, c )
518 #define PACK_COLOR_1555_LE( a, b, c, d ) PACK_COLOR_1555( a, b, c, d )
520 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( a, b, c, d )
522 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( a, b )
524 #endif /* endianness */