d84d2edd7c8ccf93c6a68205a336a4f97f4a8347
1 /* $Id: macros.h,v 1.13 2000/11/05 18:40:58 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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.
41 #define MAX_GLUSHORT 0xffff
42 #define MAX_GLUINT 0xffffffff
47 #define M_PI (3.1415926)
51 /* Degrees to radians conversion: */
52 #define DEG2RAD (M_PI/180.0)
64 #define SET_BITS(WORD, BITS) (WORD) |= (BITS)
65 #define CLEAR_BITS(WORD, BITS) (WORD) &= ~(BITS)
66 #define TEST_BITS(WORD, BITS) ((WORD) & (BITS))
69 /* Stepping a GLfloat pointer by a byte stride
71 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
72 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
73 #define STRIDE_T(p, t, i) (p = (t *)((GLubyte *)p + i))
76 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
77 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
78 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
81 /* Copy short vectors: */
82 #define COPY_2V( DST, SRC ) \
84 (DST)[0] = (SRC)[0]; \
85 (DST)[1] = (SRC)[1]; \
88 #define COPY_3V( DST, SRC ) \
90 (DST)[0] = (SRC)[0]; \
91 (DST)[1] = (SRC)[1]; \
92 (DST)[2] = (SRC)[2]; \
95 #define COPY_4V( DST, SRC ) \
97 (DST)[0] = (SRC)[0]; \
98 (DST)[1] = (SRC)[1]; \
99 (DST)[2] = (SRC)[2]; \
100 (DST)[3] = (SRC)[3]; \
103 #define COPY_4UBV(DST, SRC) \
105 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
106 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
109 (DST)[0] = (SRC)[0]; \
110 (DST)[1] = (SRC)[1]; \
111 (DST)[2] = (SRC)[2]; \
112 (DST)[3] = (SRC)[3]; \
117 #define COPY_2FV( DST, SRC ) \
119 const GLfloat *_tmp = (SRC); \
120 (DST)[0] = _tmp[0]; \
121 (DST)[1] = _tmp[1]; \
124 #define COPY_3FV( DST, SRC ) \
126 const GLfloat *_tmp = (SRC); \
127 (DST)[0] = _tmp[0]; \
128 (DST)[1] = _tmp[1]; \
129 (DST)[2] = _tmp[2]; \
132 #define COPY_4FV( DST, SRC ) \
134 const GLfloat *_tmp = (SRC); \
135 (DST)[0] = _tmp[0]; \
136 (DST)[1] = _tmp[1]; \
137 (DST)[2] = _tmp[2]; \
138 (DST)[3] = _tmp[3]; \
143 #define COPY_SZ_4V(DST, SZ, SRC) \
146 case 4: (DST)[3] = (SRC)[3]; \
147 case 3: (DST)[2] = (SRC)[2]; \
148 case 2: (DST)[1] = (SRC)[1]; \
149 case 1: (DST)[0] = (SRC)[0]; \
153 #define COPY_CLEAN_4V(DST, SZ, SRC) \
155 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
156 COPY_SZ_4V( DST, SZ, SRC ); \
159 #define SUB_4V( DST, SRCA, SRCB ) \
161 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
162 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
163 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
164 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
167 #define ADD_4V( DST, SRCA, SRCB ) \
169 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
170 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
171 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
172 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
175 #define SCALE_4V( DST, SRCA, SRCB ) \
177 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
178 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
179 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
180 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
183 #define ACC_4V( DST, SRC ) \
185 (DST)[0] += (SRC)[0]; \
186 (DST)[1] += (SRC)[1]; \
187 (DST)[2] += (SRC)[2]; \
188 (DST)[3] += (SRC)[3]; \
191 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
193 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
194 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
195 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
196 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
199 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
201 (DST)[0] += S * (SRCB)[0]; \
202 (DST)[1] += S * (SRCB)[1]; \
203 (DST)[2] += S * (SRCB)[2]; \
204 (DST)[3] += S * (SRCB)[3]; \
207 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
209 (DST)[0] = S * (SRCB)[0]; \
210 (DST)[1] = S * (SRCB)[1]; \
211 (DST)[2] = S * (SRCB)[2]; \
212 (DST)[3] = S * (SRCB)[3]; \
216 #define SELF_SCALE_SCALAR_4V( DST, S ) \
226 * Similarly for 3-vectors.
228 #define SUB_3V( DST, SRCA, SRCB ) \
230 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
231 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
232 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
235 #define ADD_3V( DST, SRCA, SRCB ) \
237 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
238 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
239 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
242 #define SCALE_3V( DST, SRCA, SRCB ) \
244 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
245 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
246 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
249 #define ACC_3V( DST, SRC ) \
251 (DST)[0] += (SRC)[0]; \
252 (DST)[1] += (SRC)[1]; \
253 (DST)[2] += (SRC)[2]; \
256 #define ACC_SCALE_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_SCALAR_3V( DST, S, SRCB ) \
265 (DST)[0] = S * (SRCB)[0]; \
266 (DST)[1] = S * (SRCB)[1]; \
267 (DST)[2] = S * (SRCB)[2]; \
270 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
272 (DST)[0] += S * (SRCB)[0]; \
273 (DST)[1] += S * (SRCB)[1]; \
274 (DST)[2] += S * (SRCB)[2]; \
277 #define SELF_SCALE_SCALAR_3V( DST, S ) \
284 #define ACC_SCALAR_3V( DST, S ) \
291 /* And also for 2-vectors
293 #define SUB_2V( DST, SRCA, SRCB ) \
295 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
296 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
299 #define ADD_2V( DST, SRCA, SRCB ) \
301 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
302 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
305 #define SCALE_2V( DST, SRCA, SRCB ) \
307 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
308 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
311 #define ACC_2V( DST, SRC ) \
313 (DST)[0] += (SRC)[0]; \
314 (DST)[1] += (SRC)[1]; \
317 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
319 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
320 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
323 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
325 (DST)[0] = S * (SRCB)[0]; \
326 (DST)[1] = S * (SRCB)[1]; \
329 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
331 (DST)[0] += S * (SRCB)[0]; \
332 (DST)[1] += S * (SRCB)[1]; \
335 #define SELF_SCALE_SCALAR_2V( DST, S ) \
341 #define ACC_SCALAR_2V( DST, S ) \
349 /* Assign scalers to short vectors: */
350 #define ASSIGN_2V( V, V0, V1 ) \
356 #define ASSIGN_3V( V, V0, V1, V2 ) \
363 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
374 /* Absolute value (for Int, Float, Double): */
375 #define ABSI(X) ((X) < 0 ? -(X) : (X))
376 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
377 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
381 /* Round a floating-point value to the nearest integer: */
382 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
385 /* Compute ceiling of integer quotient of A divided by B: */
386 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
389 /* Clamp X to [MIN,MAX]: */
390 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
392 /* Assign X to CLAMP(X, MIN, MAX) */
393 #define CLAMP_SELF(x, mn, mx) \
394 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
398 /* Min of two values: */
399 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
401 /* MAX of two values: */
402 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
404 /* Dot product of two 2-element vectors */
405 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
407 /* Dot product of two 3-element vectors */
408 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
410 /* Dot product of two 4-element vectors */
411 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
412 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
414 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
417 #define CROSS3(n, u, v) \
419 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
420 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
421 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \