1 /* $Id: macros.h,v 1.19 2001/03/12 00:48:38 gareth Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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)
66 #define SET_BITS(WORD, BITS) (WORD) |= (BITS)
67 #define CLEAR_BITS(WORD, BITS) (WORD) &= ~(BITS)
68 #define TEST_BITS(WORD, BITS) ((WORD) & (BITS))
71 /* Stepping a GLfloat pointer by a byte stride
73 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
74 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
75 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
76 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLchan *)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 #define COPY_4UBV(DST, SRC) \
126 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
127 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
130 (DST)[0] = (SRC)[0]; \
131 (DST)[1] = (SRC)[1]; \
132 (DST)[2] = (SRC)[2]; \
133 (DST)[3] = (SRC)[3]; \
138 #define COPY_2FV( DST, SRC ) \
140 const GLfloat *_tmp = (SRC); \
141 (DST)[0] = _tmp[0]; \
142 (DST)[1] = _tmp[1]; \
145 #define COPY_3FV( DST, SRC ) \
147 const GLfloat *_tmp = (SRC); \
148 (DST)[0] = _tmp[0]; \
149 (DST)[1] = _tmp[1]; \
150 (DST)[2] = _tmp[2]; \
153 #define COPY_4FV( DST, SRC ) \
155 const GLfloat *_tmp = (SRC); \
156 (DST)[0] = _tmp[0]; \
157 (DST)[1] = _tmp[1]; \
158 (DST)[2] = _tmp[2]; \
159 (DST)[3] = _tmp[3]; \
164 #define COPY_SZ_4V(DST, SZ, SRC) \
167 case 4: (DST)[3] = (SRC)[3]; \
168 case 3: (DST)[2] = (SRC)[2]; \
169 case 2: (DST)[1] = (SRC)[1]; \
170 case 1: (DST)[0] = (SRC)[0]; \
174 #define COPY_CLEAN_4V(DST, SZ, SRC) \
176 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
177 COPY_SZ_4V( DST, SZ, SRC ); \
180 #define SUB_4V( DST, SRCA, SRCB ) \
182 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
183 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
184 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
185 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
188 #define ADD_4V( DST, SRCA, SRCB ) \
190 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
191 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
192 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
193 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
196 #define SCALE_4V( DST, SRCA, SRCB ) \
198 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
199 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
200 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
201 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
204 #define ACC_4V( DST, SRC ) \
206 (DST)[0] += (SRC)[0]; \
207 (DST)[1] += (SRC)[1]; \
208 (DST)[2] += (SRC)[2]; \
209 (DST)[3] += (SRC)[3]; \
212 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
214 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
215 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
216 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
217 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
220 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
222 (DST)[0] += S * (SRCB)[0]; \
223 (DST)[1] += S * (SRCB)[1]; \
224 (DST)[2] += S * (SRCB)[2]; \
225 (DST)[3] += S * (SRCB)[3]; \
228 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
230 (DST)[0] = S * (SRCB)[0]; \
231 (DST)[1] = S * (SRCB)[1]; \
232 (DST)[2] = S * (SRCB)[2]; \
233 (DST)[3] = S * (SRCB)[3]; \
237 #define SELF_SCALE_SCALAR_4V( DST, S ) \
247 * Similarly for 3-vectors.
249 #define SUB_3V( DST, SRCA, SRCB ) \
251 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
252 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
253 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
256 #define ADD_3V( DST, SRCA, SRCB ) \
258 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
259 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
260 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
263 #define SCALE_3V( DST, SRCA, SRCB ) \
265 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
266 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
267 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
270 #define ACC_3V( DST, SRC ) \
272 (DST)[0] += (SRC)[0]; \
273 (DST)[1] += (SRC)[1]; \
274 (DST)[2] += (SRC)[2]; \
277 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
279 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
280 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
281 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
284 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
286 (DST)[0] = S * (SRCB)[0]; \
287 (DST)[1] = S * (SRCB)[1]; \
288 (DST)[2] = S * (SRCB)[2]; \
291 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
293 (DST)[0] += S * (SRCB)[0]; \
294 (DST)[1] += S * (SRCB)[1]; \
295 (DST)[2] += S * (SRCB)[2]; \
298 #define SELF_SCALE_SCALAR_3V( DST, S ) \
305 #define ACC_SCALAR_3V( DST, S ) \
312 /* And also for 2-vectors
314 #define SUB_2V( DST, SRCA, SRCB ) \
316 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
317 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
320 #define ADD_2V( DST, SRCA, SRCB ) \
322 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
323 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
326 #define SCALE_2V( DST, SRCA, SRCB ) \
328 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
329 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
332 #define ACC_2V( DST, SRC ) \
334 (DST)[0] += (SRC)[0]; \
335 (DST)[1] += (SRC)[1]; \
338 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
340 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
341 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
344 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
346 (DST)[0] = S * (SRCB)[0]; \
347 (DST)[1] = S * (SRCB)[1]; \
350 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
352 (DST)[0] += S * (SRCB)[0]; \
353 (DST)[1] += S * (SRCB)[1]; \
356 #define SELF_SCALE_SCALAR_2V( DST, S ) \
362 #define ACC_SCALAR_2V( DST, S ) \
370 /* Assign scalers to short vectors: */
371 #define ASSIGN_2V( V, V0, V1 ) \
377 #define ASSIGN_3V( V, V0, V1, V2 ) \
384 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
395 /* Absolute value (for Int, Float, Double): */
396 #define ABSI(X) ((X) < 0 ? -(X) : (X))
397 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
398 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
402 /* Round a floating-point value to the nearest integer: */
403 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
406 /* Compute ceiling of integer quotient of A divided by B: */
407 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
410 /* Clamp X to [MIN,MAX]: */
411 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
413 /* Assign X to CLAMP(X, MIN, MAX) */
414 #define CLAMP_SELF(x, mn, mx) \
415 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
419 /* Min of two values: */
420 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
422 /* MAX of two values: */
423 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
425 /* Dot product of two 2-element vectors */
426 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
428 /* Dot product of two 3-element vectors */
429 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
431 /* Dot product of two 4-element vectors */
432 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
433 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
435 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
438 #define CROSS3(n, u, v) \
440 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
441 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
442 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \